1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/**
** Items.cc - Names of items.
**
** Written: 11/5/98 - JSF
**/
/*
Copyright (C) 1998 Jeffrey S. Freedman
Copyright (C) 2000-2022 The Exult Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "items.h"
#include "databuf.h"
#include "exult_flx.h"
#include "fnames.h"
#include "msgfile.h"
#include "utils.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::cerr;
using std::endl;
using std::ifstream;
using std::istream;
using std::ofstream;
using std::ostream;
using std::string;
using std::stringstream;
using std::vector;
// Names of U7 items.
vector<string> item_names;
// Msgs. (0x400 - in text.flx).
vector<string> text_msgs;
// Frames, etc (0x500 - 0x5ff/0x685 (BG/SI) in text.flx).
vector<string> misc_names;
static inline int remap_index(bool remap, int index, bool sibeta) {
if (!remap) {
return index;
}
if (sibeta) {
// Account for differences between SI Beta and SI Final when remapping
// to SS indices.
if (index >= 0x146) {
return index + 17;
} else if (index >= 0x135) {
return index + 16;
} else if (index >= 0x0f6) {
return index + 15;
} else if (index >= 0x0ea) {
return index + 14;
} else if (index >= 0x0e5) {
return index + 13;
} else if (index >= 0x0b1) {
return index + 11;
} else if (index >= 0x0ae) {
return index + 10;
} else if (index >= 0x0a2) {
return index + 9;
} else if (index >= 0x094) {
return index + 8;
} else if (index >= 0x08b) {
return index + 7;
} else if (index >= 0x07f) {
return index + 2;
} else {
return index;
}
} else {
if (index >= 0x0fa) {
return index + 11;
} else if (index >= 0x0b2) {
return index + 10;
} else if (index >= 0x0af) {
return index + 9;
} else if (index >= 0x094) {
return index + 8;
} else if (index >= 0x08b) {
return index + 7;
} else if (index >= 0x07f) {
return index + 2;
} else {
return index;
}
}
}
static inline const char* get_text_internal(
const vector<string>& src, unsigned num) {
return src[num].c_str();
}
static inline void add_text_internal(
vector<string>& src, unsigned num, const char* name) {
if (num >= src.size()) {
src.resize(num + 1);
}
src[num] = name;
}
/*
* Get how many item names there are.
*/
int get_num_item_names() {
return item_names.size();
}
/*
* Get an item name.
*/
const char* get_item_name(unsigned num) {
return get_text_internal(item_names, num);
}
/*
* Create an item name.
*/
void Set_item_name(unsigned num, const char* name) {
add_text_internal(item_names, num, name);
}
/*
* Get how many text messages there are.
*/
int get_num_text_msgs() {
return text_msgs.size();
}
/*
* Get a text message.
*/
const char* get_text_msg(unsigned num) {
return get_text_internal(text_msgs, num);
}
/*
* Create a text message.
*/
void Set_text_msg(unsigned num, const char* msg) {
add_text_internal(text_msgs, num, msg);
}
/*
* Get how many misc names there are.
*/
int get_num_misc_names() {
return misc_names.size();
}
/*
* Get a misc name.
*/
const char* get_misc_name(unsigned num) {
return get_text_internal(misc_names, num);
}
/*
* Create a misc name.
*/
void Set_misc_name(unsigned num, const char* name) {
add_text_internal(misc_names, num, name);
}
/*
* Set up names of items.
*
* Msg. names start at 0x400.
* Frame names start at entry 0x500 (reagents,medallions,food,etc.).
*/
static void Setup_item_names(
IDataSource& items, IDataSource& msgs, bool si, bool expansion,
bool sibeta) {
vector<string> msglist;
int first_msg; // First in exultmsg.txt. Should
// follow those in text.flx.
int total_msgs = 0;
int num_item_names = 0;
int num_text_msgs = 0;
int num_misc_names = 0;
items.seek(0x54);
int flxcnt = items.read4();
first_msg = num_item_names = flxcnt;
if (flxcnt > 0x400) {
num_item_names = 0x400;
num_text_msgs = flxcnt - 0x400;
if (flxcnt > 0x500) {
num_text_msgs = 0x100;
num_misc_names = flxcnt - 0x500;
int last_name; // Discard all starting from this.
if (si) {
last_name = 0x686;
} else {
last_name = 0x600;
}
if (flxcnt > last_name) {
num_misc_names = last_name - 0x500;
flxcnt = last_name;
}
}
total_msgs = num_text_msgs;
}
if (msgs.good()) {
// Exult msgs. too?
Text_msg_file_reader reader(msgs);
first_msg = reader.get_global_section_strings(msglist);
if (first_msg >= 0) {
first_msg -= 0x400;
if (first_msg < num_text_msgs) {
cerr << "Exult msg. # " << first_msg
<< " conflicts with 'text.flx'" << endl;
first_msg = num_text_msgs;
}
total_msgs = static_cast<int>(msglist.size() - 0x400);
} else {
first_msg = num_text_msgs;
}
}
item_names.resize(num_item_names);
text_msgs.resize(total_msgs);
misc_names.resize(num_misc_names);
// Hack alert: move SI misc_names around to match those of SS.
const bool doremap = si && (!expansion || sibeta);
if (doremap) {
flxcnt -= 17; // Just to be safe.
}
int i;
for (i = 0; i < flxcnt; i++) {
items.seek(0x80 + (i * 8));
const int itemoffs = items.read4();
if (itemoffs == 0) {
continue;
}
const int itemlen = items.read4();
items.seek(itemoffs);
string newitem;
items.read(newitem, itemlen);
if (i < num_item_names) {
item_names[i] = std::move(newitem);
} else if (i - num_item_names < num_text_msgs) {
if (sibeta && (i - num_item_names) >= 0xd2) {
text_msgs[i - num_item_names + 1] = std::move(newitem);
} else {
text_msgs[i - num_item_names] = std::move(newitem);
}
} else {
const size_t new_index = remap_index(
doremap, i - num_item_names - num_text_msgs, sibeta);
misc_names[new_index] = std::move(newitem);
}
}
for (i = first_msg; i < total_msgs; i++) {
text_msgs[i] = msglist[i + 0x400];
}
}
#define SHAPES_SECT "shapes"
#define MSGS_SECT "msgs"
#define MISC_SECT "miscnames"
/*
* This sets up item names and messages from Exult's new file,
* "textmsgs.txt".
*/
static void Setup_text(
IDataSource& txtfile, // All text.
IDataSource& exultmsg) {
// Start by reading from exultmsg
vector<string> msglist;
int first_msg;
{
Text_msg_file_reader reader(exultmsg);
first_msg = reader.get_global_section_strings(msglist);
}
const unsigned total_msgs = static_cast<int>(msglist.size() - 0x400);
if (first_msg >= 0) {
first_msg -= 0x400;
}
text_msgs.resize(total_msgs);
for (unsigned i = first_msg; i < total_msgs; i++) {
text_msgs[i] = msglist[i + 0x400];
}
// Now read in textmsg.txt
Text_msg_file_reader reader(txtfile);
reader.get_section_strings(SHAPES_SECT, item_names);
reader.get_section_strings(MSGS_SECT, text_msgs);
reader.get_section_strings(MISC_SECT, misc_names);
}
/*
* Setup item names and text messages.
*/
void Setup_text(bool si, bool expansion, bool sibeta) {
Free_text();
const bool is_patch = is_system_path_defined("<PATCH>");
// Always read from exultmsg.txt
// TODO: allow multilingual exultmsg.txt files.
auto exultmsg = [&]() {
if (is_patch && U7exists(PATCH_EXULTMSG)) {
return IExultDataSource(PATCH_EXULTMSG, -1);
}
return IExultDataSource(
BUNDLE_CHECK(BUNDLE_EXULT_FLX, EXULT_FLX),
EXULT_FLX_EXULTMSG_TXT);
}();
// Exult new-style messages?
if (is_patch && U7exists(PATCH_TEXTMSGS)) {
IFileDataSource txtfile(PATCH_TEXTMSGS, true);
if (!txtfile.good()) {
return;
}
Setup_text(txtfile, exultmsg);
} else if (U7exists(TEXTMSGS)) {
IFileDataSource txtfile(TEXTMSGS, true);
if (!txtfile.good()) {
return;
}
Setup_text(txtfile, exultmsg);
} else {
IFileDataSource textflx = [&]() {
if (is_patch && U7exists(PATCH_TEXT)) {
return IFileDataSource(PATCH_TEXT);
}
return IFileDataSource(TEXT_FLX);
}();
if (!textflx.good()) {
return;
}
Setup_item_names(textflx, exultmsg, si, expansion, sibeta);
}
}
/*
* Free memory.
*/
static void Free_text_list(vector<string>& items) {
items.clear();
}
void Free_text() {
Free_text_list(item_names);
Free_text_list(text_msgs);
Free_text_list(misc_names);
}
/*
* Write out new-style Exult text file.
*/
void Write_text_file() {
auto pOut = U7open_out(PATCH_TEXTMSGS, true); // (It's a text file.)
if (!pOut) {
return;
}
auto& out = *pOut;
out << "Exult " << VERSION << " text message file."
<< " Written by ExultStudio." << endl;
Write_msg_file_section(out, SHAPES_SECT, item_names);
Write_msg_file_section(out, MSGS_SECT, text_msgs);
Write_msg_file_section(out, MISC_SECT, misc_names);
}
|