Debian Codegraph @

similar/main/text.cpp

raw ◀ ▶
1 Debian patch touch this file
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Code for localizable text
23 *
24 */
25
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "physfsx.h"
31 #include "pstypes.h"
32 #include "dxxerror.h"
33 #include "text.h"
34 #include "strutil.h"
35 #include "args.h"
36 #include <memory>
37 #include "d_range.h"
38
39 #ifdef GENERATE_BUILTIN_TEXT_TABLE
40 #include <ctype.h>
41 #endif
42
43 #if DXX_BUILD_DESCENT == 1
44 #define IDX_TEXT_OVERWRITTEN 330
45 #elif DXX_BUILD_DESCENT == 2
46 #define IDX_TEXT_OVERWRITTEN 350
47 #define SHAREWARE_TEXTSIZE 14677
48 #endif
49
50 namespace dcx {
51
52 namespace {
53
54 static std::unique_ptr<char[]> text;
55 static std::unique_ptr<char[]> overwritten_text;
56
57 static constexpr uint8_t decode_char(const uint8_t c)
58 {
59 constexpr uint8_t BITMAP_TBL_XOR{0xd3};
60 const uint8_t c2 = std::rotl(c, 1) ^ BITMAP_TBL_XOR;
61 return {std::rotl(c2, 1)};
62 }
63
64 static std::pair<PHYSFS_sint64, std::unique_ptr<char[]>> load_blob_from_file(const char *const filename, const RAIIPHYSFS_File file)
65 {
66 const auto len{PHYSFS_fileLength(file)};
67 auto p{std::make_unique_for_overwrite<char[]>(len + 1)};
68 const auto bytes_read{PHYSFSX_readBytes(file, p.get(), len)};
69 if (bytes_read < 0)
70 Error("Failed to read file %s: \"%s\"", filename, PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
71 else if (bytes_read > len)
72 /* This should never happen. */
73 Error("Failed to read file %s: read more bytes than requested", filename);
74 else
75 return {bytes_read, std::move(p)};
76 }
77
78 static std::unique_ptr<char[]> parse_blob_as_text_strings(const char *const filename, std::unique_ptr<char[]> loaded_blob, const bool have_binary, const PHYSFS_sint64 bytes_read)
79 {
80 char *tptr = loaded_blob.get();
81 #if DXX_BUILD_DESCENT == 1
82 static const char *const extra_strings[] = {
83 "done",
84 "I am a",
85 "CHEATER!",
86 "Loading Data",
87 "ALT-F2\t Save Game",
88 "ALT-F3\t Load Game",
89 "Only in Registered version!",
90 "Concussion",
91 "Homing",
92 "ProxBomb",
93 "Smart",
94 "Mega",
95 "Mission '%s' not found.\nYou must have this mission\nfile in order to playback\nthis demo.",
96 "All player callsigns on screen",
97 "There is already a game\nin progress with that name",
98 "This mission is designated\nAnarchy-only",
99 "Force level start",
100 "Quitting now means ending the\nentire netgame\n\nAre you sure?",
101 "The mission for that netgame\nis not installed on your\nsystem. Cannot join.",
102 "Start Multiplayer Game\n\nSelect mission",
103 "Error loading mission file",
104 "Custom (return to set)",
105 "Base address (in Hex)",
106 "IRQ Number",
107 "Reset to Default",
108 "Valid IRQ values are 2-7",
109 "No UART was detected\nat those settings",
110 "You will pay dearly for that!",
111 "Revenge is mine!!",
112 "Man I'm good!",
113 "Its almost too easy!",
114 " Mission:",
115 "+/- Changes viewing distance",
116 "Alternate exit found!\n\nProceeding to Secret Level!",
117 "Show all players on automap",
118 "Killed by a robot",
119 "Baud",
120 "A consistency error has been\ndetected in your network connection.\nCheck you hardware and re-join",
121 "Press any key to continue (Print Screen to save screenshot)",
•122 "An error occurred while writing\ndemo. Demo is likely corrupted.\nEnter demo name now or\npress ESC to delete demo.",
123 "The main reactor is invulnerable for",
124 "The level being loaded is not\navailable in Destination Saturn.\nUnable to continue demo playback.\n\nPress any key to continue.",
125 "Reactor life",
126 "min",
127 "Current IPX Socket is default",
128 "This program requires MS-DOS 5.0 or higher.\nYou are using MS-DOS",
129 "You can use the -nodoscheck command line\nswitch to override this check, but it\nmay have unpredictable results, namely\nwith DOS file error handling.\n",
130 "Not enough file handles!",
131 "of the necessary file handles\nthat Descent requires to execute properly. You will\nneed to increase the FILES=n line in your config.sys.",
132 "If you are running with a clean boot, then you will need\nto create a CONFIG.SYS file in your root directory, with\nthe line FILES=15 in it. If you need help with this,\ncontact Interplay technical support.",
133 "You may also run with the -nofilecheck command line option\nthat will disable this check, but you might get errors\nwhen loading saved games or playing demos.",
134 "Available memory",
135 "more bytes of DOS memory needed!",
136 "more bytes of virtual memory needed. Reconfigure VMM.",
137 "more bytes of extended/expanded memory needed!",
138 "Or else you you need to use virtual memory (See README.TXT)",
139 "more bytes of physical memory needed!",
140 "Check to see that your virtual memory settings allow\nyou to use all of your physical memory (See README.TXT)",
141 "Initializing DPMI services",
142 "Initializing critical error handler",
143 "Enables Virtual I/O Iglasses! stereo display",
144 "Enables Iglasses! head tracking via COM port",
145 "Enables Kasan's 3dMax stereo display in low res.",
146 "3DBios must be installed for 3dMax operation.",
147 "Enables Kasan's 3dMax stereo display in high res",
148 "Press any key for more options...",
149 "Enables dynamic socket changing",
150 "Disables the file handles check",
151 "Getting settings from DESCENT.CFG...",
152 "Initializing timer system...",
153 "Initializing keyboard handler...",
154 "Initializing mouse handler...",
155 "Mouse support disabled...",
156 "Initializing joystick handler...",
157 "Slow joystick reading enabled...",
158 "Polled joystick reading enabled...",
159 "BIOS joystick reading enabled...",
160 "Joystick support disabled...",
161 "Initializing divide by zero handler...",
162 "Initializing network...",
163 "Using IPX network support on channel",
164 "No IPX compatible network found.",
165 "Error opening socket",
166 "Not enough low memory for IPX buffers.",
167 "Error initializing IPX. Error code:",
168 "Network support disabled...",
169 "Initializing graphics system...",
170 "SOUND: Error opening",
171 "SOUND: Error locking down instruments",
172 "SOUND: (HMI)",
173 "SOUND: Error locking down drums",
174 "SOUND: Error locking midi track map!",
175 "SOUND: Error locking midi callback function!",
176 "Using external control:",
177 "Invalid serial port parameter for -itrak!",
178 "Initializing i-glasses! head tracking on serial port %d",
179 "Make sure the glasses are turned on!",
180 "Press ESC to abort",
181 "Failed to open serial port. Status =",
182 "Message",
183 "Macro",
184 "Error locking serial interrupt routine!",
185 "Error locking serial port data!",
186 "Robots are normal",
187 "Robots move fast, fire seldom",
188 "Robot painting OFF",
189 "Robot painting with texture %d"
190 };
191 (void)bytes_read;
192 #endif
193
194 for (const auto i : xrange(N_TEXT_STRINGS))
195 {
196 #if DXX_BUILD_DESCENT == 1
197 if (!tptr)
198 {
199 if (i >= N_TEXT_STRINGS_MIN) // account for non-registered 1.4/1.5 text files
200 {
201 Text_string[i - 1] = extra_strings[i - N_TEXT_STRINGS_MIN - 1];
202 Text_string[i] = extra_strings[i - N_TEXT_STRINGS_MIN];
203 continue;
204 }
205 else
206 {
207 Error("Not enough strings in text file - expecting %d (or at least %d), found %d", N_TEXT_STRINGS, N_TEXT_STRINGS_MIN, i);
208 }
209 }
210 #endif
211 Text_string[i] = tptr;
212 char *ts = tptr;
213
214 tptr = strchr(tptr,'\n');
215
216 #if DXX_BUILD_DESCENT == 2
217 if (!tptr)
218 {
219 if (i == 644) /* older datafiles */
220 {
221 if (bytes_read == SHAREWARE_TEXTSIZE)
222 {
223 Text_string[173] = Text_string[172];
224 Text_string[172] = Text_string[171];
225 Text_string[171] = Text_string[170];
226 Text_string[170] = Text_string[169];
227 Text_string[169] = "Windows Joystick";
228 }
229
230 Text_string[644] = "Z1";
231 Text_string[645] = "UN";
232 Text_string[646] = "P1";
233 Text_string[647] = "R1";
234 Text_string[648] = "Y1";
235 break;
236 }
237
238 Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
239 }
240 #endif
241 if (tptr)
242 *tptr++ = 0;
243
244 if (have_binary)
245 decode_text_line(ts);
246
247 //scan for special chars (like \n)
248 if (auto p = strchr(ts, '\\'))
249 {
250 for (auto q = p; assert(*p == '\\'), *p;)
251 {
252 char newchar;
253
254 if (p[1] == 'n') newchar = '\n';
255 else if (p[1] == 't') newchar = '\t';
256 else if (p[1] == '\\') newchar = '\\';
257 else
258 Error("Unsupported key sequence <\\%c> on line %d of file <%s>", p[1], i + 1, filename);
259
260 *q++ = newchar;
261 p += 2;
262 char *r = strchr(p, '\\');
263 if (!r)
264 {
265 r = tptr;
266 assert(!r[-1]);
267 assert(r == 1 + p + strlen(p));
268 memmove(q, p, r - p);
269 break;
270 }
271 memmove(q, p, r - p);
272 q += (r - p);
273 p = r;
274 }
275 }
276
277 switch(i) {
278 #if DXX_BUILD_DESCENT == 1
279 case 116:
280 if (!d_stricmp(ts, "SPREADFIRE")) // This string is too long to fit in the cockpit-box
281 {
282 ts[6] = 0;
283 }
284 break;
285 #endif
286
287 case IDX_TEXT_OVERWRITTEN:
288 {
289 static const char extra[] = "\n<Ctrl-C> converts format\nIntel <-> PowerPC";
290 std::size_t l = strlen(ts);
291 overwritten_text = std::make_unique<char[]>(l + sizeof(extra));
292 char *o = overwritten_text.get();
293 std::copy_n(extra, sizeof(extra), std::copy_n(ts, l, o));
294 Text_string[i] = o;
295 break;
296 }
297 }
298 }
299 return loaded_blob;
300 }
301
302 static std::unique_ptr<char[]> load_text_from_texfile(const char *const filename, RAIIPHYSFS_File texfile)
303 {
304 auto &&[bytes_read, p] = load_blob_from_file(filename, std::move(texfile));
305 const auto pg = p.get();
306 const auto iter_remove = std::remove(pg, std::next(pg, bytes_read), '\r');
307 *iter_remove = 0;
308 return parse_blob_as_text_strings(filename, std::move(p), false, bytes_read);
309 }
310
311 static std::unique_ptr<char[]> load_text_from_txbfile(const char *const filename, RAIIPHYSFS_File txbfile)
312 {
313 auto &&[bytes_read, p] = load_blob_from_file(filename, std::move(txbfile));
314 p[bytes_read] = 0;
315 return parse_blob_as_text_strings(filename, std::move(p), true, bytes_read);
316 }
317
318 static std::unique_ptr<char[]> load_text_from_file(const char *const texfilename)
319 {
320 if (auto &&[texfile, physfserr] = PHYSFSX_openReadBuffered(texfilename); texfile)
321 return load_text_from_texfile(texfilename, std::move(texfile));
322 #define DXX_TXB_FILENAME "descent.txb"
323 else if (auto &&[txbfile, physfserr2] = PHYSFSX_openReadBuffered(DXX_TXB_FILENAME); txbfile)
324 return load_text_from_txbfile(DXX_TXB_FILENAME, std::move(txbfile));
325 else
326 Error("Failed to open files %s, " DXX_TXB_FILENAME ": \"%s\", \"%s\"", texfilename, PHYSFS_getErrorByCode(physfserr), PHYSFS_getErrorByCode(physfserr2));
327 #undef DXX_TXB_FILENAME
328 }
329
330 }
331
332 }
333
334 //decode an encoded line of text of bitmaps.tbl
335 void decode_text_line(char *p)
336 {
337 for (; const char c = *p; p++)
338 *p = decode_char(c);
339 }
340
341 // decode buffer of text, preserves newlines
342 void decode_text(std::span<char> ptr)
343 {
344 for (auto &c : ptr)
345 {
346 if (c != '\n')
347 c = decode_char(c);
348 }
349 }
350
351 //load all the text strings for Descent
352 namespace dsx {
353
354 #ifdef USE_BUILTIN_ENGLISH_TEXT_STRINGS
355 static
356 #endif
357 std::array<const char *, N_TEXT_STRINGS> Text_string;
358
359 void load_text()
360 {
361 auto &&local_text = load_text_from_file(CGameArg.DbgAltTex.empty() ? "descent.tex" : CGameArg.DbgAltTex.c_str());
362 #ifdef GENERATE_BUILTIN_TEXT_TABLE
363 for (unsigned u = 0; u < std::size(Text_string); ++u)
364 {
365 printf("\t%u\t\"", u);
366 for (char *px = Text_string[u]; *px; ++px) {
367 unsigned x = (unsigned)*px;
368 if (isprint(x))
369 putchar(x);
370 else if (x == '\t')
371 printf("\\t");
372 else if (x == '\r')
373 printf("\\r");
374 else if (x == '\n')
375 printf("\\n");
376 else
377 printf("\\x%.2x", x);
378 }
379 printf("\"\n");
380 }
381 #endif
382 text = std::move(local_text);
383 }
384 }
385
386