| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/manage/manage.cpp $ |
| 21 | * $Revision: 103 $ |
| 22 | * $Date: 10/10/01 11:32a $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * Jason should put something here |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/manage/manage.cpp $ |
| 28 | * |
| 29 | * 103 10/10/01 11:32a Matt |
| 30 | * Added system to check for errors when reading in add-on data. |
| 31 | * |
| 32 | * 102 10/08/01 1:50p Matt |
| 33 | * Added a case for gamefile pagetype to avoid int3 |
| 34 | * |
| 35 | * 101 4/19/00 5:07p Matt |
| 36 | * From Duane for 1.4 |
| 37 | * Added checks, asserts, and fixes for bad return values |
| 38 | * |
| 39 | * 100 3/20/00 12:27p Matt |
| 40 | * Merge of Duane's post-1.3 changes. |
| 41 | * Mac pilot directory stuff. |
| 42 | * |
| 43 | * 99 10/26/99 3:30p Jeff |
| 44 | * handle extra.gam addon tablefile |
| 45 | * |
| 46 | * 98 10/20/99 6:27p Jeff |
| 47 | * sped up addon page popping (by saving page offsets) |
| 48 | * |
| 49 | * 97 10/19/99 9:14p Chris |
| 50 | * Fixed a memory free bug |
| 51 | * |
| 52 | * 96 8/11/99 5:32p Jeff |
| 53 | * changes to fix addon tablefile support so it works correctly |
| 54 | * |
| 55 | * 95 7/28/99 2:29p Kevin |
| 56 | * Added macintosh DLL extentions (msl) |
| 57 | * |
| 58 | * 94 5/14/99 12:45p Matt |
| 59 | * Removed yet more static data |
| 60 | * |
| 61 | * 93 5/14/99 12:33p Matt |
| 62 | * Fixed another case of too much local data for the Mac. |
| 63 | * |
| 64 | * 92 5/13/99 8:36p Matt |
| 65 | * Made some local variables global to get around the 32K local variable |
| 66 | * limit on the Mac. |
| 67 | * |
| 68 | * 91 5/12/99 3:01p Matt |
| 69 | * Declared one texpage structure statically for all the functions that |
| 70 | * need it, because the Mac limits local data to 32K. |
| 71 | * |
| 72 | * 90 4/30/99 8:53p Matt |
| 73 | * Added a "voice" directory for gamefiles. |
| 74 | * |
| 75 | * 89 4/22/99 3:26p Jason |
| 76 | * added transferring of pagelocks |
| 77 | * |
| 78 | * 88 4/20/99 12:06a Jeff |
| 79 | * added so files to data/scripts search path |
| 80 | * |
| 81 | * 87 4/15/99 5:21p Jason |
| 82 | * sped up table file loading |
| 83 | * |
| 84 | * 86 4/14/99 10:46a Kevin |
| 85 | * Removed OutrageMessageBox from release builds |
| 86 | * |
| 87 | * 85 4/14/99 1:33a Jeff |
| 88 | * fixed case mismatched #includes |
| 89 | * |
| 90 | * 84 4/12/99 3:05p Jason |
| 91 | * changes for 256 textures |
| 92 | * |
| 93 | * 83 3/05/99 10:42a Jason |
| 94 | * more deletion of pagelocls |
| 95 | * |
| 96 | * 82 3/04/99 1:46p Jason |
| 97 | * fixed some manage problems |
| 98 | * |
| 99 | * 81 2/27/99 5:15p Jason |
| 100 | * fixed search path bug |
| 101 | * |
| 102 | * 80 2/17/99 12:11p Jason |
| 103 | * added music directory to searchable list |
| 104 | * |
| 105 | * 79 2/16/99 11:35a Samir |
| 106 | * added art directory. |
| 107 | * |
| 108 | * 78 2/10/99 3:47p Jason |
| 109 | * before doing a backup, makes sure that the tablefile version is the |
| 110 | * same on the net and on the local machine |
| 111 | * |
| 112 | * 77 1/29/99 6:29p Jason |
| 113 | * first pass at adding bumpmaps |
| 114 | * |
| 115 | * 76 1/21/99 11:16p Jeff |
| 116 | * pulled out some structs and defines from header files and moved them |
| 117 | * into seperate header files so that multiplayer dlls don't require major |
| 118 | * game headers, just those new headers. Side effect is a shorter build |
| 119 | * time. Also cleaned up some header file #includes that weren't needed. |
| 120 | * This affected polymodel.h, object.h, player.h, vecmat.h, room.h, |
| 121 | * manage.h and multi.h |
| 122 | * |
| 123 | * 75 1/13/99 2:49p Jeff |
| 124 | * added .msg to the search path for data\scripts |
| 125 | * |
| 126 | * 74 1/13/99 7:08a Jeff |
| 127 | * put some #ifdef's around some window's specific code (really only used |
| 128 | * in the editor, but EDITOR is never defined when building manage) so it |
| 129 | * builds in linux |
| 130 | * |
| 131 | * 73 12/30/98 6:52p Matt |
| 132 | * Fixed compile warnings |
| 133 | * |
| 134 | * 72 12/29/98 4:30p Jason |
| 135 | * added add-on data functionality |
| 136 | * |
| 137 | * 71 12/13/98 7:51p Jeff |
| 138 | * only check the script directory for cpp,dll and def files |
| 139 | * |
| 140 | * 70 12/11/98 5:50p Jeff |
| 141 | * implemented and added changes regarding Level&Scripting manage system |
| 142 | * and compiler interface |
| 143 | * |
| 144 | * 69 11/28/98 2:19p Jason |
| 145 | * fixed stupid filecopy bug |
| 146 | * |
| 147 | * 68 11/18/98 11:02a Jason |
| 148 | * temp fix for table problems |
| 149 | * |
| 150 | * 67 11/16/98 3:49p Jason |
| 151 | * changes for manage system |
| 152 | * |
| 153 | * 66 11/16/98 2:43p Jason |
| 154 | * better file checking for old files |
| 155 | * |
| 156 | * 65 11/13/98 12:30p Jason |
| 157 | * fixed reordered pages bug |
| 158 | * |
| 159 | * 64 11/13/98 12:30p Jason |
| 160 | * changes for weapons |
| 161 | * |
| 162 | * 63 11/06/98 6:00p Josh |
| 163 | * fixed dumb bug |
| 164 | * |
| 165 | * 62 11/06/98 5:28p Josh |
| 166 | * FROM JASON:upped tracklock limit |
| 167 | * |
| 168 | * 61 11/06/98 12:35p Jason |
| 169 | * more speedups for manage system |
| 170 | * |
| 171 | * 60 11/05/98 7:55p Jason |
| 172 | * changes for new manage system |
| 173 | * |
| 174 | * 59 11/04/98 11:02a Jason |
| 175 | * added levels and briefing directories to new "old files" update method |
| 176 | * |
| 177 | * 58 11/02/98 6:35p Jason |
| 178 | * changes for filter |
| 179 | * |
| 180 | * 57 11/02/98 6:02p Jason |
| 181 | * made yes network updates much faster |
| 182 | * |
| 183 | * 56 10/15/98 8:48a Matt |
| 184 | * Changed some errors to use Error() instead of OutrageMessageBox() |
| 185 | * |
| 186 | * 55 10/14/98 5:15p Jason |
| 187 | * added version checking to the table file |
| 188 | * |
| 189 | * 54 10/12/98 11:38p Jeff |
| 190 | * wrapped all the Object_info[].description whenever freed...trying to |
| 191 | * find an obscure bug. Added icon_name to manage page of Generic |
| 192 | * |
| 193 | * 53 10/12/98 10:31a Jason |
| 194 | * don't seach data directories if release |
| 195 | * |
| 196 | * 52 10/09/98 4:39p Jason |
| 197 | * fixed local table file message |
| 198 | * |
| 199 | * 51 10/09/98 2:27p Jason |
| 200 | * reorganized table file system |
| 201 | * |
| 202 | * 50 10/09/98 2:40a Jason |
| 203 | * fixed table file issues with demo |
| 204 | * |
| 205 | * 49 10/08/98 10:03p Jason |
| 206 | * more filtered table file stuff |
| 207 | * |
| 208 | * 48 10/08/98 7:05p Jason |
| 209 | * added file filter support |
| 210 | * |
| 211 | * 47 9/28/98 6:53p Kevin |
| 212 | * localized some multiplayer menus |
| 213 | * |
| 214 | * 46 9/25/98 4:37p Jason |
| 215 | * fixed dedicated server printing out progress messages |
| 216 | * |
| 217 | * 45 9/25/98 2:53p Jason |
| 218 | * added progress bar |
| 219 | * |
| 220 | * 44 9/25/98 12:24p Samir |
| 221 | * fixed bugs for release version. |
| 222 | * |
| 223 | * 43 9/24/98 6:22p Jason |
| 224 | * fixed RELEASE version asking to update network files |
| 225 | * |
| 226 | * 42 9/18/98 3:58p Jason |
| 227 | * change weapon reordering to do countermeasure weapons after generics |
| 228 | * |
| 229 | * 41 9/15/98 4:31p Jason |
| 230 | * added more functionality for the dedicated server |
| 231 | * |
| 232 | * 40 9/14/98 6:28p Jason |
| 233 | * first pass at getting dedicated server working |
| 234 | * |
| 235 | * 39 8/25/98 3:42p Jason |
| 236 | * fixed generic object problems |
| 237 | * |
| 238 | * 38 8/25/98 3:25p Jason |
| 239 | * turned off fast load trick |
| 240 | * |
| 241 | * 37 8/17/98 4:00p Jason |
| 242 | * Added mprintf |
| 243 | * |
| 244 | * 36 8/15/98 5:17p Matt |
| 245 | * Added new Base_directory variable. Got rid of D3_LOCAL check and |
| 246 | * 'local directory' registry variable. |
| 247 | * |
| 248 | * 35 8/13/98 6:34p Jason |
| 249 | * made table file loading much faster |
| 250 | * |
| 251 | * 34 8/10/98 1:49p Samir |
| 252 | * added music directory. |
| 253 | * |
| 254 | * 33 8/03/98 6:44p Jason |
| 255 | * set custom graphics in the search path |
| 256 | * |
| 257 | * 32 7/27/98 6:25p Jeff |
| 258 | * added creation of custom directories |
| 259 | * |
| 260 | * 31 6/23/98 2:43p Matt |
| 261 | * Changed calls to OutrageMessageBox() & Debug_MessageBox() to deal with |
| 262 | * int return value (instead of bool). |
| 263 | * |
| 264 | * 30 6/12/98 1:06p Jason |
| 265 | * added smart loading from local table file |
| 266 | * |
| 267 | * 29 5/04/98 5:00p Keneta |
| 268 | * FROM JASON:Fixed copyfile bug |
| 269 | * |
| 270 | * 28 5/04/98 4:42p Jason |
| 271 | * even better error checking |
| 272 | * |
| 273 | * 26 5/04/98 4:24p Jason |
| 274 | * upped MAX_TRIES |
| 275 | * |
| 276 | * 25 5/04/98 4:18p Jason |
| 277 | * added assert to prevent table file problems |
| 278 | * |
| 279 | * 24 3/31/98 3:49p Jason |
| 280 | * added memory lib |
| 281 | * |
| 282 | * 23 3/19/98 3:51p Samir |
| 283 | * added misc data directory. |
| 284 | * |
| 285 | * 22 2/23/98 2:00p Jason |
| 286 | * Pop up a message box when table file couldn't be opened |
| 287 | * |
| 288 | * 21 2/06/98 12:15p Jason |
| 289 | * upped max times program will try to delete the table file before |
| 290 | * bailing |
| 291 | * |
| 292 | * 20 2/04/98 11:47a Jason |
| 293 | * added dynamic description field to generic pages |
| 294 | * |
| 295 | * 19 1/26/98 11:32a Jason |
| 296 | * upped the number of times the system will try to delete a table file |
| 297 | * |
| 298 | * 18 1/22/98 2:49p Samir |
| 299 | * Added D3 Local Dir to the search path. |
| 300 | * |
| 301 | * 17 1/15/98 6:22p Jason |
| 302 | * added safety checks so the network won't copy over a primitive you have |
| 303 | * held locally |
| 304 | * |
| 305 | * 16 1/15/98 4:54p Mark |
| 306 | * FROM JASON:Do switcheroo a few times before giving up |
| 307 | * |
| 308 | * 15 12/22/97 3:50p Chris |
| 309 | * |
| 310 | * 14 11/17/97 4:16p Jason |
| 311 | * added briefings directory |
| 312 | * |
| 313 | * 13 9/09/97 4:07p Matt |
| 314 | * Added mprintf() |
| 315 | * |
| 316 | * 12 9/04/97 2:53p Samir |
| 317 | * Added gamefile and generic page strings to PageNames array. |
| 318 | * |
| 319 | * 11 8/12/97 12:47p Matt |
| 320 | * Only copy pagefile from net if different from local copy. |
| 321 | * When loading pages, print different char for each type |
| 322 | * Show how long it took to load the pagefile |
| 323 | * |
| 324 | * 10 8/11/97 1:54p Matt |
| 325 | * Ripped out robot & powerup pages, and added generic page |
| 326 | * |
| 327 | * 9 8/08/97 5:17p Jason |
| 328 | * made it so that when you update from the network it doesn't halt other |
| 329 | * users |
| 330 | * |
| 331 | * 8 8/08/97 3:44p Jason |
| 332 | * added code to support new generic page |
| 333 | * |
| 334 | * 7 8/08/97 1:57p Matt |
| 335 | * Took out error message now handled by mng_MakeLocker() |
| 336 | * |
| 337 | * 6 7/29/97 12:07p Jason |
| 338 | * added gamefile page for auto retrieval of non-standard page types |
| 339 | * |
| 340 | * 50 6/27/97 3:11p Jason |
| 341 | * added room directories |
| 342 | * |
| 343 | * |
| 344 | * 49 6/11/97 1:07p Samir |
| 345 | * The removal of gameos and replaced with oeApplication, oeDatabase |
| 346 | * |
| 347 | * 48 6/10/97 5:08p Jason |
| 348 | * added reorderpages menu item |
| 349 | * |
| 350 | * 47 6/05/97 2:52p Jason |
| 351 | * added megacell functions |
| 352 | * |
| 353 | * 46 5/30/97 11:41a Jason |
| 354 | * made a better error message if someone already has the table file |
| 355 | * locked upon startup |
| 356 | * |
| 357 | * 45 5/22/97 3:08p Jason |
| 358 | * added the ReorderPage function |
| 359 | * |
| 360 | * 44 5/16/97 3:53p Jason |
| 361 | * added filepage dialog |
| 362 | * |
| 363 | * 43 5/15/97 5:56 PM Jeremy |
| 364 | * made initlocaltable files check if the file exists by using cfexist |
| 365 | * rather than trying to open the file and checking the error code |
| 366 | * |
| 367 | * 42 5/14/97 6:38p Jason |
| 368 | * fixed a plethora of potential problems by locking the table file when |
| 369 | * someone is starting up. |
| 370 | * |
| 371 | * 41 5/14/97 6:44 PM Jeremy |
| 372 | * fixed a bug where local dir backup directory was not being set |
| 373 | * correctly in init local table files |
| 374 | * |
| 375 | * 40 5/13/97 3:41p Jason |
| 376 | * made all manage code work with the new device independant database |
| 377 | * |
| 378 | * 39 5/08/97 12:41p Jason |
| 379 | * made manage system work with device dependant path names |
| 380 | * |
| 381 | * 38 4/29/97 5:07p Samir |
| 382 | * Added levels directory to search path |
| 383 | * |
| 384 | * 37 4/25/97 6:16p Jason |
| 385 | * added switcheroo function |
| 386 | * |
| 387 | * 36 4/01/97 2:13p Jason |
| 388 | * changes for sound page functionality |
| 389 | * |
| 390 | * 35 3/31/97 4:35p Jason |
| 391 | * added player ship and weapon pages |
| 392 | * |
| 393 | * 34 3/25/97 3:10p Jason |
| 394 | * added robots and robot page functionality |
| 395 | * |
| 396 | * 33 3/17/97 4:27p Jason |
| 397 | * added sounds directory to path list |
| 398 | * |
| 399 | * 32 3/14/97 7:18p Matt |
| 400 | * Added missing include |
| 401 | * |
| 402 | * 31 3/14/97 6:13 PM Jeremy |
| 403 | * changed calls to windows "MessageBox" to OutrageMessageBox, changed |
| 404 | * call of GetUserName to os_database->get_user_name, #included descent.h |
| 405 | * in order to refer to the OS_database object, unincluded <windows.h> and |
| 406 | * <direct.h> |
| 407 | * |
| 408 | * 30 3/13/97 7:39p Matt |
| 409 | * Changed code to use getenv() (ANSI-standard) instead of |
| 410 | * GetEnvironmentVariable() |
| 411 | * |
| 412 | * 29 3/13/97 12:34p Matt |
| 413 | * Changed code to not leave directory changed after checking for presence |
| 414 | * of a directory. |
| 415 | * |
| 416 | * 28 3/13/97 11:37a Samir |
| 417 | * Changed os file functions to ddio file functions |
| 418 | * |
| 419 | * 27 3/10/97 2:23p Jason |
| 420 | * added auto creation of models directory |
| 421 | * |
| 422 | * 26 3/07/97 1:02p Jason |
| 423 | * Now uses Samir's OS specific directory functions |
| 424 | * |
| 425 | * 25 3/05/97 3:10p Jason |
| 426 | * added more door functionality |
| 427 | * |
| 428 | * 24 3/05/97 12:16p Jason |
| 429 | * added code to support our new 3d doors |
| 430 | * |
| 431 | * 23 3/03/97 6:21p Matt |
| 432 | * Changed cfile functions to use D3 naming convention |
| 433 | * |
| 434 | * $NoKeywords: $ |
| 435 | */ |
| 436 | |
| 437 | #include <cerrno> |
| 438 | #include <cstdarg> |
| 439 | #include <cstdio> |
| 440 | #include <cstdlib> |
| 441 | #include <cstring> |
| 442 | #include <filesystem> |
| 443 | |
| 444 | #if defined(POSIX) |
| 445 | #include "linux_fix.h" |
| 446 | #endif |
| 447 | #include "descent.h" |
| 448 | #include "manage.h" |
| 449 | #include "pserror.h" |
| 450 | #include "gametexture.h" |
| 451 | #include "texpage.h" |
| 452 | #include "doorpage.h" |
| 453 | #include "soundpage.h" |
| 454 | #include "megapage.h" |
| 455 | #include "shippage.h" |
| 456 | #include "weaponpage.h" |
| 457 | #include "gamefilepage.h" |
| 458 | #include "mono.h" |
| 459 | #include "object.h" |
| 460 | #include "ddio.h" |
| 461 | #include "cfile.h" |
| 462 | #include "appdatabase.h" |
| 463 | #include "genericpage.h" |
| 464 | #include "mem.h" |
| 465 | #include "dedicated_server.h" |
| 466 | #include "AppConsole.h" |
| 467 | #include "init.h" |
| 468 | #include "stringtable.h" |
| 469 | #include "args.h" |
| 470 | #include "vclip.h" |
| 471 | #include "polymodel.h" |
| 472 | int Old_table_method = 0; |
| 473 | void mng_WriteNewUnknownPage(CFILE *outfile); |
| 474 | // This is for levels |
| 475 | char LocalLevelsDir[TABLE_NAME_LEN]; |
| 476 | // This is for pages |
| 477 | std::filesystem::path TableLockFilename; |
| 478 | char TableFilename[TABLE_NAME_LEN]; |
| 479 | char TempTableLockFilename[TABLE_NAME_LEN], TempTableFilename[TABLE_NAME_LEN]; |
| 480 | char LocalTableFilename[TABLE_NAME_LEN], LocalTempTableFilename[TABLE_NAME_LEN]; |
| 481 | char BackupTableFilename[TABLE_NAME_LEN], BackupLockFilename[TABLE_NAME_LEN]; |
| 482 | std::filesystem::path ManageGraphicsDir, LocalManageGraphicsDir; |
| 483 | std::filesystem::path LocalModelsDir, NetModelsDir; |
| 484 | std::filesystem::path LocalSoundsDir, NetSoundsDir; |
| 485 | std::filesystem::path LocalRoomsDir, NetRoomsDir; |
| 486 | std::filesystem::path LocalBriefingDir, NetBriefingDir; |
| 487 | char LocalScriptDir[TABLE_NAME_LEN], NetScriptDir[TABLE_NAME_LEN]; |
| 488 | std::filesystem::path LocalMiscDir, NetMiscDir; |
| 489 | std::filesystem::path LocalArtDir, NetArtDir; |
| 490 | std::filesystem::path LocalMusicDir, NetMusicDir; |
| 491 | std::filesystem::path LocalVoiceDir, NetVoiceDir; |
| 492 | std::filesystem::path NetTableDir, LocalTableDir; |
| 493 | char LocalD3Dir[TABLE_NAME_LEN], NetD3Dir[TABLE_NAME_LEN]; |
| 494 | char LocalCustomGraphicsDir[TABLE_NAME_LEN]; |
| 495 | char LocalCustomSoundsDir[TABLE_NAME_LEN]; |
| 496 | std::filesystem::path LockerFile; |
| 497 | std::filesystem::path VersionFile; |
| 498 | char TableUser[TABLE_NAME_LEN]; |
| 499 | char ErrorString[INFO_STRING_LEN], InfoString[INFO_STRING_LEN]; |
| 500 | mngs_track_lock GlobalTrackLocks[MAX_TRACKLOCKS]; |
| 501 | bool Use_old_update_method = false; |
| 502 | char *TablefileNameOverride = NULL; |
| 503 | // Only valid when first starting the editor |
| 504 | #define MAX_LOCKLIST_ELEMENTS 1000 |
| 505 | mngs_Pagelock *LockList; |
| 506 | int Num_locklist, Starting_editor = 0, Loading_locals = 0, Fast_load_trick = 0; |
| 507 | #define PRIMTYPE_OOF 0 |
| 508 | #define PRIMTYPE_OGF 1 |
| 509 | #define PRIMTYPE_WAV 2 |
| 510 | #define PRIMTYPE_OAF 3 |
| 511 | #define PRIMTYPE_FILE 4 |
| 512 | #if defined(WIN32) |
| 513 | FILETIME TableTimeThreshold; |
| 514 | // Builds a list of old files so we know which ones to update |
| 515 | // Searches through all our netdirectories for old files |
| 516 | void BuildOldFileList(FILETIME threshold); |
| 517 | #endif |
| 518 | struct old_file { |
| 519 | uint8_t type; |
| 520 | char name[PAGENAME_LEN]; |
| 521 | }; |
| 522 | #define MAX_OLDFILE_ELEMENTS 10000 |
| 523 | int Num_old_files = 0; |
| 524 | old_file *OldFiles; |
| 525 | const char *PageNames[] = {"Unknown", "Texture", "Weapon", "Robot", "Powerup", "Door", |
| 526 | "Player ship", "Sound", "Megacell", "Files", "Generic objects"}; |
| 527 | #ifndef RELEASE |
| 528 | int Network_up = 1; |
| 529 | int Stand_alone = 0; |
| 530 | #else |
| 531 | int Network_up = 0; |
| 532 | int Stand_alone = 1; |
| 533 | #endif |
| 534 | void mng_BackupTableFile(); |
| 535 | |
| 536 | // returns 1 if network is up, 0 if down |
| 537 | int mng_IsNetworkUp() { |
| 538 | char dir[100]; |
| 539 | if (Stand_alone) |
| 540 | return 0; |
| 541 | |
| 542 | char net_dir[255] = {0}; |
| 543 | int dirlen = 255; |
| 544 | Database->read("net directory", net_dir, &dirlen); |
| 545 | if (net_dir[0] == 0) |
| 546 | return 0; |
| 547 | ddio_MakePath(dir, net_dir, "data", NULL); |
| 548 | |
| 549 | std::error_code ec; |
| 550 | std::filesystem::create_directories(dir, ec); |
| 551 | if (!ec) { |
| 552 | char old_dir[100]; |
| 553 | ddio_GetWorkingDir(old_dir, 100); |
| 554 | if (!ddio_SetWorkingDir(dir)) |
| 555 | return 0; // network down |
| 556 | else { |
| 557 | ddio_SetWorkingDir(old_dir); // restore directory |
| 558 | return 1; // directory is already there |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return 1; |
| 563 | } |
| 564 | void ReorderPages(int); |
| 565 | // #define JASONS_REORDERING |
| 566 | void Read256TextureNames(); |
| 567 | // Sets up our table files, get their filenames, etc. |
| 568 | // Returns 1 on success, zero on error |
| 569 | int mng_InitTableFiles() { |
| 570 | size_t size = TABLE_NAME_LEN; |
| 571 | int answer; |
| 572 | Database->get_user_name(TableUser, &size); |
| 573 | if (FindArg("-filter")) |
| 574 | Use_old_update_method = true; |
| 575 | // Read256TextureNames (); |
| 576 | |
| 577 | if (FindArg("-oldmethod")) |
| 578 | Use_old_update_method = true; |
| 579 | if (mng_IsNetworkUp()) { |
| 580 | #ifndef RELEASE |
| 581 | answer = OutrageMessageBox(MBOX_YESNO, "Do you wish to update your data files from the network?\n(If NO is " |
| 582 | "selected then you will have to restart to use networking functions)"); |
| 583 | #else |
| 584 | answer = IDNO; |
| 585 | #endif |
| 586 | if (answer == IDNO) |
| 587 | Network_up = 0; |
| 588 | else { |
| 589 | Network_up = 1; |
| 590 | #ifndef RELEASE |
| 591 | #if defined(WIN32) |
| 592 | if (cfexist("c:\\edload")) |
| 593 | Use_old_update_method = true; |
| 594 | else { |
| 595 | CFILE *fp = cfopen("c:\\edload", "wt"); |
| 596 | cfclose(fp); |
| 597 | } |
| 598 | #endif |
| 599 | #endif |
| 600 | } |
| 601 | } else { |
| 602 | mprintf(0, "Network is down...\n"); |
| 603 | Network_up = 0; |
| 604 | } |
| 605 | |
| 606 | if (Network_up == 0) { |
| 607 | mng_InitLocalTables(); |
| 608 | mng_InitLocalDirectories(); |
| 609 | mng_CheckToCreateLocalTables(); |
| 610 | mng_InitTrackLocks(); |
| 611 | } else { |
| 612 | // Do locals |
| 613 | mng_InitLocalTables(); |
| 614 | mng_InitLocalDirectories(); |
| 615 | mng_CheckToCreateLocalTables(); |
| 616 | |
| 617 | // Do network |
| 618 | mng_InitNetTables(); |
| 619 | mng_InitNetDirectories(); |
| 620 | mng_CheckToCreateNetTables(); |
| 621 | mng_BackupTableFile(); |
| 622 | mng_InitPagelocks(); |
| 623 | mng_InitTrackLocks(); |
| 624 | #ifdef JASONS_REORDERING |
| 625 | ReorderPages(0); |
| 626 | return 0; |
| 627 | #endif |
| 628 | } |
| 629 | return 1; |
| 630 | } |
| 631 | // Loads our tables |
| 632 | int mng_LoadTableFiles(int show_progress) { |
| 633 | if (Network_up) { |
| 634 | LockList = (mngs_Pagelock *)mem_malloc(MAX_LOCKLIST_ELEMENTS * sizeof(mngs_Pagelock)); |
| 635 | Num_locklist = mng_GetListOfLocks(LockList, MAX_LOCKLIST_ELEMENTS, TableUser); |
| 636 | OldFiles = (old_file *)mem_malloc(MAX_OLDFILE_ELEMENTS * sizeof(old_file)); |
| 637 | Num_old_files = 0; |
| 638 | ASSERT(OldFiles); |
| 639 | #if defined(WIN32) |
| 640 | if (TableTimeThreshold.dwHighDateTime != -1) |
| 641 | BuildOldFileList(TableTimeThreshold); |
| 642 | #endif |
| 643 | |
| 644 | Starting_editor = 1; |
| 645 | } |
| 646 | int ret1, ret2; |
| 647 | if (Fast_load_trick && !FindArg("-filter")) |
| 648 | Network_up = 0; |
| 649 | |
| 650 | ret1 = mng_LoadNetPages(show_progress); |
| 651 | if (Fast_load_trick) { |
| 652 | Network_up = 1; |
| 653 | Fast_load_trick = 0; |
| 654 | } |
| 655 | ret2 = mng_LoadLocalPages(); |
| 656 | if (Network_up) { |
| 657 | Starting_editor = 0; |
| 658 | Num_locklist = 0; |
| 659 | Num_old_files = 0; |
| 660 | mem_free(OldFiles); |
| 661 | mem_free(LockList); |
| 662 | #ifndef RELEASE |
| 663 | #if defined(WIN32) |
| 664 | remove("c:\\edload"); |
| 665 | #endif |
| 666 | #endif |
| 667 | } |
| 668 | RemapEverything(); |
| 669 | |
| 670 | if (!ret1 || !ret2) |
| 671 | return 0; |
| 672 | return 1; |
| 673 | } |
| 674 | // This is for initting tables on STAND_ALONE, if the network is down, or if |
| 675 | // the user doesn't want network support |
| 676 | int mng_InitLocalTables() { |
| 677 | // Set the local table directory from the base directory |
| 678 | strcpy(LocalD3Dir, Base_directory); |
| 679 | mprintf(1, "Local dir:%s\n", LocalD3Dir); |
| 680 | |
| 681 | // Make the CFILE system first look at our local directories. If the goods aren't |
| 682 | // found there, try out on the network |
| 683 | // TODO: temp variable until LocalD3Dir will be std::fs::path |
| 684 | std::filesystem::path localdir = std::filesystem::path(LocalD3Dir); |
| 685 | LocalTableDir = localdir / "data" / "tables"; |
| 686 | LocalManageGraphicsDir = localdir / "data" / "graphics"; |
| 687 | LocalModelsDir = localdir / "data" / "models"; |
| 688 | LocalSoundsDir = localdir / "data" / "sounds"; |
| 689 | ddio_MakePath(LocalCustomSoundsDir, LocalD3Dir, "custom", "sounds", NULL); |
| 690 | ddio_MakePath(LocalCustomGraphicsDir, LocalD3Dir, "custom", "graphics", NULL); |
| 691 | LocalRoomsDir = localdir / "data" / "rooms"; |
| 692 | LocalBriefingDir = localdir / "data" / "briefings"; |
| 693 | ddio_MakePath(LocalScriptDir, LocalD3Dir, "data", "scripts", NULL); |
| 694 | LocalMiscDir = localdir / "data" / "misc"; |
| 695 | LocalArtDir = localdir / "data" / "art"; |
| 696 | LocalMusicDir = localdir / "data" / "music"; |
| 697 | LocalVoiceDir = localdir / "data" / "voice"; |
| 698 | ddio_MakePath(LocalLevelsDir, LocalD3Dir, "data", "levels", NULL); |
| 699 | cf_SetSearchPath(LocalD3Dir); |
| 700 | #ifndef RELEASE |
| 701 | cf_SetSearchPath(LocalLevelsDir); |
| 702 | cf_SetSearchPath(LocalTableDir); // Local table directory |
| 703 | |
| 704 | cf_SetSearchPath(LocalManageGraphicsDir); |
| 705 | cf_SetSearchPath(LocalModelsDir); |
| 706 | cf_SetSearchPath(LocalSoundsDir); |
| 707 | cf_SetSearchPath(LocalRoomsDir); |
| 708 | cf_SetSearchPath(LocalBriefingDir); |
| 709 | cf_SetSearchPath(LocalScriptDir, {".cpp", ".dll", ".def", ".msg", ".so", ".msl", ".dylib"}); |
| 710 | cf_SetSearchPath(LocalMiscDir); |
| 711 | cf_SetSearchPath(LocalArtDir); |
| 712 | cf_SetSearchPath(LocalMusicDir); |
| 713 | cf_SetSearchPath(LocalVoiceDir); |
| 714 | #endif |
| 715 | |
| 716 | if (Network_up) { |
| 717 | ddio_MakePath(LocalTableFilename, LocalTableDir.u8string().c_str(), LOCAL_TABLE, NULL); |
| 718 | ddio_MakePath(LocalTempTableFilename, LocalTableDir.u8string().c_str(), TEMP_LOCAL_TABLE, NULL); |
| 719 | } else { |
| 720 | strcpy(LocalTableFilename, LOCAL_TABLE); |
| 721 | strcpy(LocalTempTableFilename, TEMP_LOCAL_TABLE); |
| 722 | } |
| 723 | |
| 724 | return 1; |
| 725 | } |
| 726 | int mng_InitNetTables() { |
| 727 | char dir[255]; |
| 728 | int dirlen = 255; |
| 729 | Database->read("net directory", dir, &dirlen); |
| 730 | if (dir[0] == 0) |
| 731 | Error("D3_DIR environment variable not set."); |
| 732 | |
| 733 | strcpy(NetD3Dir, dir); |
| 734 | mprintf(1, "Net dir:%s\n", NetD3Dir); |
| 735 | // TODO: temp variable until NetD3Dir will be std::fs::path |
| 736 | std::filesystem::path netdir = std::filesystem::path(NetD3Dir); |
| 737 | |
| 738 | NetModelsDir = netdir / "data" / "models"; |
| 739 | NetSoundsDir = netdir / "data" / "sounds"; |
| 740 | NetRoomsDir = netdir / "data" / "rooms"; |
| 741 | NetBriefingDir = netdir / "data" / "briefings"; |
| 742 | ddio_MakePath(NetScriptDir, NetD3Dir, "data", "scripts", NULL); |
| 743 | NetMiscDir = netdir / "data" / "misc"; |
| 744 | ManageGraphicsDir = netdir / "data" / "graphics"; |
| 745 | NetTableDir = netdir / "data" / "tables"; |
| 746 | NetArtDir = netdir / "data" / "art"; |
| 747 | NetMusicDir = netdir / "data" / "music"; |
| 748 | NetVoiceDir = netdir / "data" / "voice"; |
| 749 | TableLockFilename = NetTableDir / "table.lok"; |
| 750 | ddio_MakePath(BackupLockFilename, NetTableDir.u8string().c_str(), "tablelok.bak", NULL); |
| 751 | ddio_MakePath(BackupTableFilename, NetTableDir.u8string().c_str(), "table.bak", NULL); |
| 752 | ddio_MakePath(TableFilename, NetTableDir.u8string().c_str(), NET_TABLE, NULL); |
| 753 | ddio_MakePath(TempTableLockFilename, NetTableDir.u8string().c_str(), "lock.tmp", NULL); |
| 754 | ddio_MakePath(TempTableFilename, NetTableDir.u8string().c_str(), TEMP_NET_TABLE, NULL); |
| 755 | LockerFile = NetTableDir / "locker"; |
| 756 | VersionFile = NetTableDir / "TableVersion"; |
| 757 | |
| 758 | cf_SetSearchPath(ManageGraphicsDir); |
| 759 | cf_SetSearchPath(NetModelsDir); |
| 760 | cf_SetSearchPath(NetSoundsDir); |
| 761 | cf_SetSearchPath(NetRoomsDir); |
| 762 | cf_SetSearchPath(NetMiscDir); |
| 763 | cf_SetSearchPath(NetMusicDir); |
| 764 | cf_SetSearchPath(NetVoiceDir); |
| 765 | return 1; |
| 766 | } |
| 767 | void mng_CheckToCreateNetTables() { |
| 768 | CFILE *infile, *outfile; |
| 769 | |
| 770 | ASSERT(Stand_alone != 1); |
| 771 | |
| 772 | infile = (CFILE *)cfopen(TableFilename, "rb"); |
| 773 | if (infile == NULL) { |
| 774 | if (errno == ENOENT) { |
| 775 | outfile = (CFILE *)cfopen(TableFilename, "wb"); |
| 776 | if (!outfile) { |
| 777 | mprintf(0, "Error creating table file! The network must be down...\n"); |
| 778 | Network_up = 0; |
| 779 | } else { |
| 780 | mng_WriteNewUnknownPage(outfile); |
| 781 | cfclose(outfile); |
| 782 | } |
| 783 | } else { |
| 784 | mprintf(0, "Error creating table file! The network must be down...\n"); |
| 785 | Network_up = 0; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | if (infile) |
| 790 | cfclose(infile); |
| 791 | } |
| 792 | // Checks to see if there is a table file...if not, create one with a dummy page |
| 793 | void mng_CheckToCreateLocalTables() { |
| 794 | CFILE *outfile; |
| 795 | |
| 796 | if (!Network_up) { |
| 797 | strcpy(TableFilename, NET_TABLE); |
| 798 | mprintf(0, "table filename = %s\n", TableFilename); |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | if (!cfexist(LocalTableFilename)) { |
| 803 | outfile = (CFILE *)cfopen(LocalTableFilename, "wb"); |
| 804 | if (!outfile) { |
| 805 | Error("Error creating local table file!"); |
| 806 | return; |
| 807 | } else { |
| 808 | mng_WriteNewUnknownPage(outfile); |
| 809 | cfclose(outfile); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | // Creates directories if needed |
| 814 | void mng_InitLocalDirectories() { |
| 815 | std::filesystem::path dir = LocalD3Dir; |
| 816 | std::error_code ec; |
| 817 | std::filesystem::create_directories(dir / "custom", ec); |
| 818 | std::filesystem::create_directories(dir / "custom" / "graphics", ec); |
| 819 | std::filesystem::create_directories(dir / "custom" / "sounds", ec); |
| 820 | std::filesystem::create_directories(dir / "custom" / "settings", ec); |
| 821 | |
| 822 | cf_SetSearchPath(LocalCustomGraphicsDir); |
| 823 | cf_SetSearchPath(LocalCustomSoundsDir); |
| 824 | |
| 825 | if (Network_up) { |
| 826 | std::filesystem::create_directories(dir / "data", ec); |
| 827 | std::filesystem::create_directories(dir / "data" / "tables", ec); |
| 828 | std::filesystem::create_directories(dir / "data" / "graphics", ec); |
| 829 | std::filesystem::create_directories(dir / "data" / "sounds", ec); |
| 830 | std::filesystem::create_directories(dir / "data" / "rooms", ec); |
| 831 | std::filesystem::create_directories(dir / "data" / "levels", ec); |
| 832 | std::filesystem::create_directories(dir / "data" / "models", ec); |
| 833 | std::filesystem::create_directories(dir / "data" / "briefings", ec); |
| 834 | std::filesystem::create_directories(dir / "data" / "scripts", ec); |
| 835 | std::filesystem::create_directories(dir / "data" / "misc", ec); |
| 836 | std::filesystem::create_directories(dir / "data" / "art", ec); |
| 837 | std::filesystem::create_directories(dir / "data" / "music", ec); |
| 838 | std::filesystem::create_directories(dir / "data" / "voice", ec); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | void mng_InitNetDirectories() { |
| 843 | std::filesystem::path dir = NetD3Dir; |
| 844 | std::error_code ec; |
| 845 | |
| 846 | if (Stand_alone) |
| 847 | return; |
| 848 | std::filesystem::create_directories(dir / "data", ec); |
| 849 | std::filesystem::create_directories(dir / "data" / "tables", ec); |
| 850 | std::filesystem::create_directories(dir / "data" / "graphics", ec); |
| 851 | std::filesystem::create_directories(dir / "data" / "sounds", ec); |
| 852 | std::filesystem::create_directories(dir / "data" / "rooms", ec); |
| 853 | std::filesystem::create_directories(dir / "data" / "levels", ec); |
| 854 | std::filesystem::create_directories(dir / "data" / "models", ec); |
| 855 | std::filesystem::create_directories(dir / "data" / "briefings", ec); |
| 856 | std::filesystem::create_directories(dir / "data" / "scripts", ec); |
| 857 | std::filesystem::create_directories(dir / "data" / "misc", ec); |
| 858 | std::filesystem::create_directories(dir / "data" / "art", ec); |
| 859 | std::filesystem::create_directories(dir / "data" / "music", ec); |
| 860 | std::filesystem::create_directories(dir / "data" / "voice", ec); |
| 861 | } |
| 862 | |
| 863 | extern int TableVersionCurrent(); |
| 864 | #if !defined(WIN32) |
| 865 | void mng_BackupTableFile() {} |
| 866 | #else |
| 867 | void mng_BackupTableFile() { |
| 868 | Fast_load_trick = 0; |
| 869 | if (!TableVersionCurrent()) { |
| 870 | Error("You must do a source update and recompile. The data on the network is newer that your sourcecode."); |
| 871 | return; |
| 872 | } |
| 873 | |
| 874 | std::filesystem::path str = LocalTableDir / NET_TABLE; |
| 875 | if (!cfexist(str)) { |
| 876 | TableTimeThreshold.dwHighDateTime = 0; |
| 877 | TableTimeThreshold.dwLowDateTime = 0; |
| 878 | } else { |
| 879 | WIN32_FIND_DATA filedata; |
| 880 | HANDLE filehandle = FindFirstFile(str.u8string().c_str(), &filedata); |
| 881 | if (filehandle == INVALID_HANDLE_VALUE) { |
| 882 | Error("Couldn't open net table file for some reason!"); |
| 883 | return; |
| 884 | } |
| 885 | TableTimeThreshold = filedata.ftLastWriteTime; |
| 886 | FindClose(filehandle); |
| 887 | } |
| 888 | |
| 889 | if (!cfexist(str) || cf_Diff(str, TableFilename)) { |
| 890 | mprintf(0, "Making local copy of table file.\n"); |
| 891 | |
| 892 | if (!cf_CopyFile(str, TableFilename, 1)) |
| 893 | Error("There was an error making a backup copy of the table file.\n"); |
| 894 | str = LocalTableDir / "tablelok.loc"; |
| 895 | if (!cf_CopyFile(str, TableLockFilename, 1)) |
| 896 | Error("There was an error making a backup copy of the locker table file.\n"); |
| 897 | } else { |
| 898 | mprintf(0, "Local table file same as network copy.\n"); |
| 899 | TableTimeThreshold.dwHighDateTime = -1; |
| 900 | Fast_load_trick = 1; |
| 901 | } |
| 902 | } |
| 903 | #endif |
| 904 | void mng_WriteUnknownPage(CFILE *outfile) { |
| 905 | // Function for writing out "undefined" page...useful for placeholding |
| 906 | cf_WriteByte(outfile, PAGETYPE_UNKNOWN); |
| 907 | } |
| 908 | void mng_WriteNewUnknownPage(CFILE *outfile) { |
| 909 | // Function for writing out "undefined" page...useful for placeholding |
| 910 | int offset = StartManagePage(outfile, PAGETYPE_UNKNOWN); |
| 911 | EndManagePage(outfile, offset); |
| 912 | } |
| 913 | // Clear out tracklocks |
| 914 | void mng_InitTrackLocks() { |
| 915 | for (int i = 0; i < MAX_TRACKLOCKS; i++) { |
| 916 | GlobalTrackLocks[i].used = 0; |
| 917 | GlobalTrackLocks[i].pagetype = PAGETYPE_UNKNOWN; |
| 918 | GlobalTrackLocks[i].name[0] = 0; |
| 919 | } |
| 920 | } |
| 921 | // Given a name, returns the index of the tracklock with that name |
| 922 | // -1 indicates that it wasn't found |
| 923 | int mng_FindTrackLock(char *name, int pagetype) { |
| 924 | int i; |
| 925 | for (i = 0; i < MAX_TRACKLOCKS; i++) { |
| 926 | if (GlobalTrackLocks[i].used && GlobalTrackLocks[i].pagetype == pagetype && |
| 927 | !stricmp(name, GlobalTrackLocks[i].name)) |
| 928 | return i; |
| 929 | } |
| 930 | return -1; |
| 931 | } |
| 932 | // Searches through global array of tracklocks and returns first free one |
| 933 | // Sets the tracklock to be named "name" and its type "pagetype" |
| 934 | // returns -1 if none free |
| 935 | int mng_AllocTrackLock(char *name, int pagetype) { |
| 936 | int i; |
| 937 | for (i = 0; i < MAX_TRACKLOCKS; i++) |
| 938 | if (GlobalTrackLocks[i].used == 0) { |
| 939 | strcpy(GlobalTrackLocks[i].name, name); |
| 940 | GlobalTrackLocks[i].pagetype = pagetype; |
| 941 | GlobalTrackLocks[i].used = 1; |
| 942 | mprintf(0, "Tracklock %s allocated.\n", name); |
| 943 | return i; |
| 944 | } |
| 945 | Error("Couldn't get a free tracklock!"); |
| 946 | return -1; |
| 947 | } |
| 948 | // Frees a tracklock |
| 949 | void mng_FreeTrackLock(int n) { |
| 950 | mprintf(0, "Tracklock %s freed.\n", GlobalTrackLocks[n].name); |
| 951 | GlobalTrackLocks[n].pagetype = PAGETYPE_UNKNOWN; |
| 952 | GlobalTrackLocks[n].used = 0; |
| 953 | GlobalTrackLocks[n].name[0] = 0; |
| 954 | } |
| 955 | // Displays all the network locks of "name" |
| 956 | void mng_DisplayLockList(char *name) { |
| 957 | mngs_Pagelock list[100]; |
| 958 | char temp[200]; |
| 959 | int max = 100; |
| 960 | int num, i; |
| 961 | int length = 0; |
| 962 | char str[5000]; |
| 963 | #ifndef RELEASE |
| 964 | // Get the list |
| 965 | if ((num = mng_GetListOfLocks(list, max, name)) < 0) { |
| 966 | OutrageMessageBox(MBOX_OK, ErrorString); |
| 967 | return; |
| 968 | } else if (num == 0) { |
| 969 | OutrageMessageBox(MBOX_OK, "User has no pages locked."); |
| 970 | return; |
| 971 | } |
| 972 | |
| 973 | // Make a large string with all the info in it |
| 974 | snprintf(str, sizeof(str), "User %s has the following pages locked:\n\n", TableUser); |
| 975 | for (i = 0; i < num; i++) { |
| 976 | snprintf(temp, sizeof(temp), "%s:%s", PageNames[list[i].pagetype], list[i].name); |
| 977 | strncat(str, temp, sizeof(str) - strlen(str) - 1); |
| 978 | strncat(str, "\n", sizeof(str) - strlen(str) - 1); |
| 979 | length += strlen(temp); |
| 980 | if (length > 5000 - 100) |
| 981 | break; |
| 982 | } |
| 983 | // Display that string |
| 984 | OutrageMessageBox(MBOX_OK, str); |
| 985 | #endif |
| 986 | } |
| 987 | // Declare these here because it's too big to put on the stack on the Mac |
| 988 | static mngs_texture_page texpage; |
| 989 | static mngs_door_page doorpage; |
| 990 | static mngs_generic_page genericpage; |
| 991 | static mngs_sound_page soundpage; |
| 992 | static mngs_megacell_page megacellpage; |
| 993 | static mngs_ship_page shippage; |
| 994 | static mngs_weapon_page weaponpage; |
| 995 | static mngs_gamefile_page gamefilepage; |
| 996 | // IF YOU ADD ANY NEW PAGETYPE YOU MUST CHANGE THE FUNCTIONS LISTED UNDER THIS LINE |
| 997 | // TO DEAL WITH YOUR PAGE TYPE. IF YOU FORGET, YOU CAN CORRUPT THE PAGEFILE!!!!! |
| 998 | //------------------------------------------------------------------------------ |
| 999 | // Given a pagetype, reads it in but discards it. Useful for parsing. |
| 1000 | void mng_ReadDummyPage(CFILE *infile, uint8_t pagetype) { |
| 1001 | switch (pagetype) { |
| 1002 | case PAGETYPE_TEXTURE: |
| 1003 | mng_ReadNewTexturePage(infile, &texpage); |
| 1004 | break; |
| 1005 | case PAGETYPE_POWERUP: |
| 1006 | case PAGETYPE_ROBOT: |
| 1007 | Error("Your local table file is invalid. You must update from the network."); |
| 1008 | break; |
| 1009 | case PAGETYPE_DOOR: |
| 1010 | mng_ReadNewDoorPage(infile, &doorpage); |
| 1011 | break; |
| 1012 | case PAGETYPE_GENERIC: |
| 1013 | mng_ReadNewGenericPage(infile, &genericpage); |
| 1014 | if (genericpage.objinfo_struct.description != NULL) { |
| 1015 | mem_free(genericpage.objinfo_struct.description); |
| 1016 | genericpage.objinfo_struct.description = NULL; |
| 1017 | } |
| 1018 | break; |
| 1019 | case PAGETYPE_GAMEFILE: |
| 1020 | mng_ReadNewGamefilePage(infile, &gamefilepage); |
| 1021 | break; |
| 1022 | case PAGETYPE_SOUND: |
| 1023 | mng_ReadNewSoundPage(infile, &soundpage); |
| 1024 | break; |
| 1025 | case PAGETYPE_SHIP: |
| 1026 | mng_ReadNewShipPage(infile, &shippage); |
| 1027 | break; |
| 1028 | case PAGETYPE_WEAPON: |
| 1029 | mng_ReadNewWeaponPage(infile, &weaponpage); |
| 1030 | break; |
| 1031 | case PAGETYPE_MEGACELL: |
| 1032 | mng_ReadNewMegacellPage(infile, &megacellpage); |
| 1033 | break; |
| 1034 | case PAGETYPE_UNKNOWN: |
| 1035 | break; |
| 1036 | default: |
| 1037 | Int3(); // unrecognized pagetype |
| 1038 | break; |
| 1039 | } |
| 1040 | } |
| 1041 | // Reads a page in that we don't care about, and writes it right back out |
| 1042 | // This is useful for replacing a specific page in a file but ignoring others |
| 1043 | void mng_ReadWriteDummyPage(CFILE *infile, CFILE *outfile, uint8_t pagetype) { |
| 1044 | switch (pagetype) { |
| 1045 | case PAGETYPE_TEXTURE: |
| 1046 | // Read it in, write it out. |
| 1047 | mng_ReadNewTexturePage(infile, &texpage); |
| 1048 | mng_WriteNewTexturePage(outfile, &texpage); |
| 1049 | break; |
| 1050 | case PAGETYPE_DOOR: |
| 1051 | // Read it in, write it out. |
| 1052 | mng_ReadNewDoorPage(infile, &doorpage); |
| 1053 | mng_WriteNewDoorPage(outfile, &doorpage); |
| 1054 | break; |
| 1055 | case PAGETYPE_GENERIC: |
| 1056 | // Read it in, write it out. |
| 1057 | mng_ReadNewGenericPage(infile, &genericpage); |
| 1058 | mng_WriteNewGenericPage(outfile, &genericpage); |
| 1059 | if (genericpage.objinfo_struct.description != NULL) { |
| 1060 | mem_free(genericpage.objinfo_struct.description); |
| 1061 | genericpage.objinfo_struct.description = NULL; |
| 1062 | } |
| 1063 | break; |
| 1064 | case PAGETYPE_GAMEFILE: |
| 1065 | // Read it in, write it out. |
| 1066 | mng_ReadNewGamefilePage(infile, &gamefilepage); |
| 1067 | mng_WriteNewGamefilePage(outfile, &gamefilepage); |
| 1068 | break; |
| 1069 | case PAGETYPE_SOUND: |
| 1070 | // Read it in, write it out. |
| 1071 | mng_ReadNewSoundPage(infile, &soundpage); |
| 1072 | mng_WriteNewSoundPage(outfile, &soundpage); |
| 1073 | break; |
| 1074 | case PAGETYPE_MEGACELL: |
| 1075 | // Read it in, write it out. |
| 1076 | mng_ReadNewMegacellPage(infile, &megacellpage); |
| 1077 | mng_WriteNewMegacellPage(outfile, &megacellpage); |
| 1078 | break; |
| 1079 | case PAGETYPE_SHIP: |
| 1080 | // Read it in, write it out. |
| 1081 | mng_ReadNewShipPage(infile, &shippage); |
| 1082 | mng_WriteNewShipPage(outfile, &shippage); |
| 1083 | break; |
| 1084 | case PAGETYPE_WEAPON: |
| 1085 | // Read it in, write it out. |
| 1086 | mng_ReadNewWeaponPage(infile, &weaponpage); |
| 1087 | mng_WriteNewWeaponPage(outfile, &weaponpage); |
| 1088 | break; |
| 1089 | case PAGETYPE_UNKNOWN: |
| 1090 | mng_WriteNewUnknownPage(outfile); |
| 1091 | break; |
| 1092 | default: |
| 1093 | Int3(); // unrecognized pagetype |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | // Renames a page on the network |
| 1098 | // This function is called when you rename your object, regardless if you check |
| 1099 | // it in |
| 1100 | int mng_RenamePage(char *oldname, char *newname, int pagetype) { |
| 1101 | int l, i; |
| 1102 | mngs_Pagelock pl; |
| 1103 | char oname[PAGENAME_LEN]; |
| 1104 | |
| 1105 | mprintf(0, "Renaming %s to %s...\n", oldname, newname); |
| 1106 | strcpy(oname, oldname); |
| 1107 | strcpy(pl.name, oname); |
| 1108 | pl.pagetype = pagetype; |
| 1109 | // Make sure we own it |
| 1110 | l = mng_CheckIfPageOwned(&pl, TableUser); |
| 1111 | ASSERT(l == 1); |
| 1112 | strcpy(pl.name, newname); |
| 1113 | strcpy(pl.holder, TableUser); |
| 1114 | |
| 1115 | // First, change the name of the network pagelock |
| 1116 | l = mng_ReplacePagelock(oname, &pl); |
| 1117 | ASSERT(l == 1); |
| 1118 | switch (pagetype) { |
| 1119 | // Find the page type with this name and rename it |
| 1120 | case PAGETYPE_TEXTURE: |
| 1121 | i = FindTextureName(oname); |
| 1122 | ASSERT(i != -1); |
| 1123 | strcpy(GameTextures[i].name, newname); |
| 1124 | |
| 1125 | l = mng_ReplacePage(oname, GameTextures[i].name, i, PAGETYPE_TEXTURE, 0); |
| 1126 | ASSERT(l == 1); |
| 1127 | if (mng_FindTrackLock(oname, PAGETYPE_TEXTURE) != -1) |
| 1128 | mng_ReplacePage(oname, GameTextures[i].name, i, PAGETYPE_TEXTURE, 1); |
| 1129 | |
| 1130 | break; |
| 1131 | case PAGETYPE_DOOR: |
| 1132 | i = FindDoorName(oname); |
| 1133 | ASSERT(i != -1); |
| 1134 | strcpy(Doors[i].name, newname); |
| 1135 | l = mng_ReplacePage(oname, Doors[i].name, i, PAGETYPE_DOOR, 0); |
| 1136 | if (mng_FindTrackLock(oname, PAGETYPE_DOOR) != -1) |
| 1137 | mng_ReplacePage(oname, Doors[i].name, i, PAGETYPE_DOOR, 1); |
| 1138 | ASSERT(l == 1); |
| 1139 | break; |
| 1140 | case PAGETYPE_GENERIC: |
| 1141 | i = FindObjectIDName(oname); |
| 1142 | ASSERT(i != -1); |
| 1143 | strcpy(Object_info[i].name, newname); |
| 1144 | |
| 1145 | l = mng_ReplacePage(oname, Object_info[i].name, i, PAGETYPE_GENERIC, 0); |
| 1146 | |
| 1147 | if (mng_FindTrackLock(oname, PAGETYPE_GENERIC) != -1) |
| 1148 | mng_ReplacePage(oname, Object_info[i].name, i, PAGETYPE_GENERIC, 1); |
| 1149 | |
| 1150 | ASSERT(l == 1); |
| 1151 | break; |
| 1152 | case PAGETYPE_GAMEFILE: |
| 1153 | i = FindGamefileName(oname); |
| 1154 | ASSERT(i != -1); |
| 1155 | strcpy(Gamefiles[i].name, newname); |
| 1156 | |
| 1157 | l = mng_ReplacePage(oname, Gamefiles[i].name, i, PAGETYPE_GAMEFILE, 0); |
| 1158 | if (mng_FindTrackLock(oname, PAGETYPE_GAMEFILE) != -1) |
| 1159 | mng_ReplacePage(oname, Gamefiles[i].name, i, PAGETYPE_GAMEFILE, 1); |
| 1160 | ASSERT(l == 1); |
| 1161 | break; |
| 1162 | case PAGETYPE_SOUND: |
| 1163 | i = FindSoundName(oname); |
| 1164 | ASSERT(i != -1); |
| 1165 | strcpy(Sounds[i].name, newname); |
| 1166 | l = mng_ReplacePage(oname, Sounds[i].name, i, PAGETYPE_SOUND, 0); |
| 1167 | if (mng_FindTrackLock(oname, PAGETYPE_SOUND) != -1) |
| 1168 | mng_ReplacePage(oname, Sounds[i].name, i, PAGETYPE_SOUND, 1); |
| 1169 | ASSERT(l == 1); |
| 1170 | break; |
| 1171 | case PAGETYPE_MEGACELL: |
| 1172 | i = FindMegacellName(oname); |
| 1173 | ASSERT(i != -1); |
| 1174 | strcpy(Megacells[i].name, newname); |
| 1175 | l = mng_ReplacePage(oname, Megacells[i].name, i, PAGETYPE_MEGACELL, 0); |
| 1176 | |
| 1177 | if (mng_FindTrackLock(oname, PAGETYPE_MEGACELL) != -1) |
| 1178 | mng_ReplacePage(oname, Megacells[i].name, i, PAGETYPE_MEGACELL, 1); |
| 1179 | ASSERT(l == 1); |
| 1180 | break; |
| 1181 | case PAGETYPE_SHIP: |
| 1182 | i = FindShipName(oname); |
| 1183 | ASSERT(i != -1); |
| 1184 | strcpy(Ships[i].name, newname); |
| 1185 | |
| 1186 | l = mng_ReplacePage(oname, Ships[i].name, i, PAGETYPE_SHIP, 0); |
| 1187 | if (mng_FindTrackLock(oname, PAGETYPE_SHIP) != -1) |
| 1188 | mng_ReplacePage(oname, Ships[i].name, i, PAGETYPE_SHIP, 1); |
| 1189 | |
| 1190 | ASSERT(l == 1); |
| 1191 | break; |
| 1192 | case PAGETYPE_WEAPON: |
| 1193 | i = FindWeaponName(oname); |
| 1194 | ASSERT(i != -1); |
| 1195 | strcpy(Weapons[i].name, newname); |
| 1196 | |
| 1197 | l = mng_ReplacePage(oname, Weapons[i].name, i, PAGETYPE_WEAPON, 0); |
| 1198 | if (mng_FindTrackLock(oname, PAGETYPE_WEAPON) != -1) |
| 1199 | mng_ReplacePage(oname, Weapons[i].name, i, PAGETYPE_WEAPON, 1); |
| 1200 | ASSERT(l == 1); |
| 1201 | break; |
| 1202 | default: |
| 1203 | Int3(); // Unknown type, get Jason |
| 1204 | break; |
| 1205 | } |
| 1206 | return 1; |
| 1207 | } |
| 1208 | #define PROGRESS_PERCENTAGE_THRESHOLD 20 |
| 1209 | // This is the function that opens the table files and reads in the individual pages |
| 1210 | // If you want your data to be in the game, it must hook into this function |
| 1211 | int mng_LoadNetPages(int show_progress) { |
| 1212 | CFILE *infile; |
| 1213 | uint8_t pagetype; |
| 1214 | std::filesystem::path tablename; |
| 1215 | float start_time; |
| 1216 | int n_pages = 0; |
| 1217 | int total_bytes; |
| 1218 | int current_byte; |
| 1219 | float progress; |
| 1220 | int int_progress = 0; |
| 1221 | int len; |
| 1222 | |
| 1223 | mprintf(0, "Loading pages..."); |
| 1224 | if (Dedicated_server) |
| 1225 | show_progress = 0; // turn off progress meter for dedicated server |
| 1226 | // If the network is up we still want to read from the local table because it |
| 1227 | // will allow others to start the game at the same time |
| 1228 | if (Network_up) { |
| 1229 | int farg = FindArg("-filter"); |
| 1230 | if (farg) |
| 1231 | tablename = GameArgs[farg + 1]; |
| 1232 | else { |
| 1233 | tablename = LocalTableDir / NET_TABLE; |
| 1234 | } |
| 1235 | infile = cfopen(tablename, "rb"); |
| 1236 | } else |
| 1237 | infile = cfopen(TableFilename, "rb"); |
| 1238 | if (!infile) { |
| 1239 | mprintf(0, "Couldn't open table file (%s) to read pages!\n", TableFilename); |
| 1240 | Error("Cannot open table file <%s>", TableFilename); |
| 1241 | return 0; |
| 1242 | } |
| 1243 | if (show_progress) { |
| 1244 | cfseek(infile, 0, SEEK_END); |
| 1245 | total_bytes = cftell(infile); |
| 1246 | cfseek(infile, 0, SEEK_SET); |
| 1247 | } |
| 1248 | start_time = timer_GetTime(); |
| 1249 | while (!cfeof(infile)) { |
| 1250 | // Read in a pagetype. If its a page we recognize, load it |
| 1251 | // mprintf(0,"."); |
| 1252 | if (show_progress) { |
| 1253 | |
| 1254 | current_byte = cftell(infile); |
| 1255 | progress = (float)current_byte / (float)total_bytes; |
| 1256 | progress *= PROGRESS_PERCENTAGE_THRESHOLD; |
| 1257 | int temp_int_progress = progress; |
| 1258 | if (temp_int_progress > int_progress) { |
| 1259 | int_progress = temp_int_progress; |
| 1260 | InitMessage(TXT_INITDATA, progress / PROGRESS_PERCENTAGE_THRESHOLD); |
| 1261 | } |
| 1262 | } |
| 1263 | pagetype = cf_ReadByte(infile); |
| 1264 | if (!Old_table_method) |
| 1265 | len = cf_ReadInt(infile); |
| 1266 | switch (pagetype) { |
| 1267 | case PAGETYPE_TEXTURE: |
| 1268 | mprintf(0, "T"); |
| 1269 | |
| 1270 | PrintDedicatedMessage("T"); |
| 1271 | mng_LoadNetTexturePage(infile); |
| 1272 | break; |
| 1273 | case PAGETYPE_POWERUP: |
| 1274 | case PAGETYPE_ROBOT: |
| 1275 | Error("Your local table file is invalid. You must update from the network."); |
| 1276 | break; |
| 1277 | case PAGETYPE_DOOR: |
| 1278 | mprintf(0, "D"); |
| 1279 | PrintDedicatedMessage("D"); |
| 1280 | mng_LoadNetDoorPage(infile); |
| 1281 | break; |
| 1282 | case PAGETYPE_GENERIC: |
| 1283 | mprintf(0, "G"); |
| 1284 | PrintDedicatedMessage("G"); |
| 1285 | mng_LoadNetGenericPage(infile); |
| 1286 | break; |
| 1287 | case PAGETYPE_GAMEFILE: |
| 1288 | mprintf(0, "F"); |
| 1289 | PrintDedicatedMessage("F"); |
| 1290 | mng_LoadNetGamefilePage(infile); |
| 1291 | break; |
| 1292 | case PAGETYPE_SOUND: |
| 1293 | mprintf(0, "S"); |
| 1294 | PrintDedicatedMessage("S"); |
| 1295 | mng_LoadNetSoundPage(infile); |
| 1296 | break; |
| 1297 | case PAGETYPE_SHIP: |
| 1298 | mprintf(0, "P"); |
| 1299 | PrintDedicatedMessage("P"); |
| 1300 | mng_LoadNetShipPage(infile); |
| 1301 | break; |
| 1302 | case PAGETYPE_WEAPON: |
| 1303 | mprintf(0, "W"); |
| 1304 | PrintDedicatedMessage("W"); |
| 1305 | mng_LoadNetWeaponPage(infile); |
| 1306 | break; |
| 1307 | case PAGETYPE_MEGACELL: |
| 1308 | mprintf(0, "M"); |
| 1309 | PrintDedicatedMessage("M"); |
| 1310 | mng_LoadNetMegacellPage(infile); |
| 1311 | break; |
| 1312 | case PAGETYPE_UNKNOWN: |
| 1313 | mprintf(0, "?"); |
| 1314 | break; |
| 1315 | default: |
| 1316 | Int3(); // Unrecognized pagetype, possible corrupt data following |
| 1317 | return 0; |
| 1318 | break; |
| 1319 | } |
| 1320 | n_pages++; |
| 1321 | } |
| 1322 | mprintf(0, "\n%d pages read in %.1f seconds.\n", n_pages, timer_GetTime() - start_time); |
| 1323 | mprintf(0, "\n"); |
| 1324 | PrintDedicatedMessage((0, "\nPage reading completed.\n")); |
| 1325 | |
| 1326 | cfclose(infile); |
| 1327 | |
| 1328 | // attempt to load extra.gam if found |
| 1329 | char name_override[256]; |
| 1330 | strcpy(name_override, "extra.gam"); |
| 1331 | infile = cfopen(name_override, "rb"); |
| 1332 | if (!infile) |
| 1333 | return 1; |
| 1334 | |
| 1335 | mprintf(0, "==================================================\n"); |
| 1336 | mprintf(0, " Loading extra.gam \n"); |
| 1337 | mprintf(0, "==================================================\n"); |
| 1338 | PrintDedicatedMessage("\nLoading extra.gam.....\n"); |
| 1339 | n_pages = 0; |
| 1340 | |
| 1341 | TablefileNameOverride = name_override; |
| 1342 | |
| 1343 | while (!cfeof(infile)) { |
| 1344 | pagetype = cf_ReadByte(infile); |
| 1345 | len = cf_ReadInt(infile); |
| 1346 | switch (pagetype) { |
| 1347 | case PAGETYPE_TEXTURE: |
| 1348 | mprintf(0, "T"); |
| 1349 | PrintDedicatedMessage("T"); |
| 1350 | mng_LoadNetTexturePage(infile, true); |
| 1351 | break; |
| 1352 | case PAGETYPE_DOOR: |
| 1353 | mprintf(0, "D"); |
| 1354 | PrintDedicatedMessage("D"); |
| 1355 | mng_LoadNetDoorPage(infile, true); |
| 1356 | break; |
| 1357 | case PAGETYPE_GENERIC: |
| 1358 | mprintf(0, "G"); |
| 1359 | PrintDedicatedMessage("G"); |
| 1360 | mng_LoadNetGenericPage(infile, true); |
| 1361 | break; |
| 1362 | case PAGETYPE_GAMEFILE: |
| 1363 | mprintf(0, "F"); |
| 1364 | PrintDedicatedMessage("F"); |
| 1365 | mng_LoadNetGamefilePage(infile, true); |
| 1366 | break; |
| 1367 | case PAGETYPE_SOUND: |
| 1368 | mprintf(0, "S"); |
| 1369 | PrintDedicatedMessage("S"); |
| 1370 | mng_LoadNetSoundPage(infile, true); |
| 1371 | break; |
| 1372 | case PAGETYPE_SHIP: |
| 1373 | mprintf(0, "P"); |
| 1374 | PrintDedicatedMessage("P"); |
| 1375 | mng_LoadNetShipPage(infile, true); |
| 1376 | break; |
| 1377 | case PAGETYPE_WEAPON: |
| 1378 | mprintf(0, "W"); |
| 1379 | PrintDedicatedMessage("W"); |
| 1380 | mng_LoadNetWeaponPage(infile, true); |
| 1381 | break; |
| 1382 | case PAGETYPE_UNKNOWN: |
| 1383 | mprintf(0, "?"); |
| 1384 | break; |
| 1385 | default: |
| 1386 | Int3(); // Unrecognized pagetype, possible corrupt data following |
| 1387 | cfclose(infile); |
| 1388 | TablefileNameOverride = NULL; |
| 1389 | return 0; |
| 1390 | break; |
| 1391 | } |
| 1392 | n_pages++; |
| 1393 | } |
| 1394 | mprintf(0, "\n%d extra pages read.\n", n_pages); |
| 1395 | TablefileNameOverride = NULL; |
| 1396 | cfclose(infile); |
| 1397 | return 1; |
| 1398 | } |
| 1399 | // Loads and allocs all pages found locally |
| 1400 | int mng_LoadLocalPages() { |
| 1401 | CFILE *infile; |
| 1402 | uint8_t pagetype; |
| 1403 | int len; |
| 1404 | |
| 1405 | mprintf(0, "Overlaying local pages..."); |
| 1406 | infile = cfopen(LocalTableFilename, "rb"); |
| 1407 | if (!infile) { |
| 1408 | mprintf(0, "Couldn't open local table file (%s) to read pages!\n", LocalTableFilename); |
| 1409 | return 1; |
| 1410 | } |
| 1411 | Loading_locals = 1; |
| 1412 | while (!cfeof(infile)) { |
| 1413 | // Read in a pagetype. If its a page we recognize, load it |
| 1414 | |
| 1415 | pagetype = cf_ReadByte(infile); |
| 1416 | if (!Old_table_method) |
| 1417 | len = cf_ReadInt(infile); |
| 1418 | mprintf(0, "."); |
| 1419 | switch (pagetype) { |
| 1420 | case PAGETYPE_TEXTURE: |
| 1421 | mng_LoadLocalTexturePage(infile); |
| 1422 | break; |
| 1423 | case PAGETYPE_POWERUP: |
| 1424 | case PAGETYPE_ROBOT: |
| 1425 | Error("Your local table file is invalid. You must update from the network."); |
| 1426 | break; |
| 1427 | case PAGETYPE_DOOR: |
| 1428 | mng_LoadLocalDoorPage(infile); |
| 1429 | break; |
| 1430 | case PAGETYPE_GENERIC: |
| 1431 | mng_LoadLocalGenericPage(infile); |
| 1432 | break; |
| 1433 | case PAGETYPE_GAMEFILE: |
| 1434 | mng_LoadLocalGamefilePage(infile); |
| 1435 | break; |
| 1436 | case PAGETYPE_SOUND: |
| 1437 | mng_LoadLocalSoundPage(infile); |
| 1438 | break; |
| 1439 | case PAGETYPE_SHIP: |
| 1440 | mng_LoadLocalShipPage(infile); |
| 1441 | break; |
| 1442 | case PAGETYPE_WEAPON: |
| 1443 | mng_LoadLocalWeaponPage(infile); |
| 1444 | break; |
| 1445 | case PAGETYPE_MEGACELL: |
| 1446 | mng_LoadLocalMegacellPage(infile); |
| 1447 | break; |
| 1448 | case PAGETYPE_UNKNOWN: |
| 1449 | break; |
| 1450 | default: |
| 1451 | Int3(); // Unrecognized pagetype, possible corrupt data following |
| 1452 | return 0; |
| 1453 | break; |
| 1454 | } |
| 1455 | } |
| 1456 | mprintf(0, "\n"); |
| 1457 | cfclose(infile); |
| 1458 | Loading_locals = 0; |
| 1459 | return 1; |
| 1460 | } |
| 1461 | #define MAX_TRIES 10000 |
| 1462 | // Removes a file, then renames another file to be the removed file. Get it? |
| 1463 | // Returns 1 on success, else 0 on fail |
| 1464 | int SwitcherooFiles(const char *name, char *tempname) { |
| 1465 | /*// If we're changing the net table file, make a backup first! |
| 1466 | if ((!stricmp (name,TableFilename))) |
| 1467 | { |
| 1468 | cf_CopyFile (BackupTableFilename,TableFilename); |
| 1469 | cf_CopyFile (BackupLockFilename,TableLockFilename); |
| 1470 | }*/ |
| 1471 | int num_tries = 0; |
| 1472 | while (!ddio_DeleteFile(name) && num_tries < MAX_TRIES) { |
| 1473 | Sleep(100); |
| 1474 | num_tries++; |
| 1475 | } |
| 1476 | if (num_tries >= MAX_TRIES) { |
| 1477 | strcpy(ErrorString, "MANAGE:There was a problem deleting the table file."); |
| 1478 | ASSERT(0); // GET JASON IMMEDIATELY |
| 1479 | Int3(); |
| 1480 | return (0); |
| 1481 | } |
| 1482 | num_tries = 0; |
| 1483 | while ((rename(tempname, name)) && num_tries <= MAX_TRIES) { |
| 1484 | Sleep(100); |
| 1485 | num_tries++; |
| 1486 | } |
| 1487 | if (num_tries >= MAX_TRIES) { |
| 1488 | strcpy(ErrorString, "MANAGE:There was a problem renaming the temp file."); |
| 1489 | ASSERT(0); // Get JASON IMMEDIATELY |
| 1490 | Int3(); |
| 1491 | return (0); |
| 1492 | } |
| 1493 | |
| 1494 | return 1; |
| 1495 | } |
| 1496 | |
| 1497 | void mng_TransferPages() { |
| 1498 | CFILE *infile, *outfile; |
| 1499 | int pagetype; |
| 1500 | int num_tracklocks = 0; |
| 1501 | mprintf(0, "Transferring pages, please wait...\n"); |
| 1502 | if (!mng_MakeLocker()) |
| 1503 | return; |
| 1504 | infile = cfopen(TableFilename, "rb"); |
| 1505 | |
| 1506 | if (!infile) { |
| 1507 | mprintf(0, "Couldn't open table file to transfer!\n"); |
| 1508 | Int3(); |
| 1509 | return; |
| 1510 | } |
| 1511 | mngs_track_lock *local_tracklocks = (mngs_track_lock *)mem_malloc(sizeof(*local_tracklocks) * 5000); |
| 1512 | // Do textures |
| 1513 | int done = 0; |
| 1514 | while (!done) { |
| 1515 | if (cfeof(infile)) { |
| 1516 | done = 1; |
| 1517 | continue; |
| 1518 | } |
| 1519 | pagetype = cf_ReadByte(infile); |
| 1520 | int len = cf_ReadInt(infile); |
| 1521 | switch (pagetype) { |
| 1522 | case PAGETYPE_TEXTURE: |
| 1523 | mng_ReadNewTexturePage(infile, &texpage); |
| 1524 | strcpy(local_tracklocks[num_tracklocks].name, texpage.tex_struct.name); |
| 1525 | local_tracklocks[num_tracklocks].pagetype = PAGETYPE_TEXTURE; |
| 1526 | num_tracklocks++; |
| 1527 | |
| 1528 | break; |
| 1529 | case PAGETYPE_SOUND: |
| 1530 | mng_ReadNewSoundPage(infile, &soundpage); |
| 1531 | strcpy(local_tracklocks[num_tracklocks].name, soundpage.sound_struct.name); |
| 1532 | local_tracklocks[num_tracklocks].pagetype = PAGETYPE_SOUND; |
| 1533 | num_tracklocks++; |
| 1534 | break; |
| 1535 | case PAGETYPE_WEAPON: |
| 1536 | mng_ReadNewWeaponPage(infile, &weaponpage); |
| 1537 | strcpy(local_tracklocks[num_tracklocks].name, weaponpage.weapon_struct.name); |
| 1538 | local_tracklocks[num_tracklocks].pagetype = PAGETYPE_WEAPON; |
| 1539 | num_tracklocks++; |
| 1540 | break; |
| 1541 | case PAGETYPE_GENERIC: |
| 1542 | mng_ReadNewGenericPage(infile, &genericpage); |
| 1543 | strcpy(local_tracklocks[num_tracklocks].name, genericpage.objinfo_struct.name); |
| 1544 | local_tracklocks[num_tracklocks].pagetype = PAGETYPE_GENERIC; |
| 1545 | num_tracklocks++; |
| 1546 | break; |
| 1547 | case PAGETYPE_DOOR: |
| 1548 | mng_ReadNewDoorPage(infile, &doorpage); |
| 1549 | strcpy(local_tracklocks[num_tracklocks].name, doorpage.door_struct.name); |
| 1550 | local_tracklocks[num_tracklocks].pagetype = PAGETYPE_DOOR; |
| 1551 | num_tracklocks++; |
| 1552 | break; |
| 1553 | case PAGETYPE_SHIP: |
| 1554 | mng_ReadNewShipPage(infile, &shippage); |
| 1555 | strcpy(local_tracklocks[num_tracklocks].name, shippage.ship_struct.name); |
| 1556 | local_tracklocks[num_tracklocks].pagetype = PAGETYPE_SHIP; |
| 1557 | num_tracklocks++; |
| 1558 | break; |
| 1559 | case PAGETYPE_GAMEFILE: |
| 1560 | mng_ReadNewGamefilePage(infile, &gamefilepage); |
| 1561 | strcpy(local_tracklocks[num_tracklocks].name, gamefilepage.gamefile_struct.name); |
| 1562 | local_tracklocks[num_tracklocks].pagetype = PAGETYPE_GAMEFILE; |
| 1563 | num_tracklocks++; |
| 1564 | break; |
| 1565 | case PAGETYPE_MEGACELL: |
| 1566 | Int3(); // huh? |
| 1567 | break; |
| 1568 | default: |
| 1569 | break; |
| 1570 | } |
| 1571 | } |
| 1572 | cfclose(infile); |
| 1573 | // Now go through and filter out all unused lock files |
| 1574 | infile = (CFILE *)cfopen(TableLockFilename, "rb"); |
| 1575 | if (!infile) { |
| 1576 | strcpy(ErrorString, "Couldn't open Table lock file!"); |
| 1577 | goto done; |
| 1578 | } |
| 1579 | outfile = (CFILE *)cfopen(TempTableLockFilename, "wb"); |
| 1580 | if (!outfile) { |
| 1581 | cfclose(infile); |
| 1582 | strcpy(ErrorString, "Couldn't open temporary table lock file!"); |
| 1583 | goto done; |
| 1584 | } |
| 1585 | done = 0; |
| 1586 | mngs_Pagelock temp_pl; |
| 1587 | while (!done) { |
| 1588 | if (cfeof(infile)) { |
| 1589 | done = 1; |
| 1590 | continue; |
| 1591 | } |
| 1592 | if (mng_ReadPagelock(infile, &temp_pl)) { |
| 1593 | int found = -1; |
| 1594 | for (int i = 0; i < num_tracklocks && found == -1; i++) { |
| 1595 | if (local_tracklocks[i].pagetype == temp_pl.pagetype && !stricmp(local_tracklocks[i].name, temp_pl.name)) { |
| 1596 | found = i; |
| 1597 | } |
| 1598 | } |
| 1599 | if (found != -1) |
| 1600 | mng_WritePagelock(outfile, &temp_pl); |
| 1601 | else { |
| 1602 | mprintf(0, "Found unused lock file %s\n", temp_pl.name); |
| 1603 | } |
| 1604 | |
| 1605 | } else |
| 1606 | done = 1; |
| 1607 | } |
| 1608 | cfclose(infile); |
| 1609 | cfclose(outfile); |
| 1610 | |
| 1611 | if (remove(TableLockFilename)) { |
| 1612 | snprintf(ErrorString, sizeof(ErrorString), "There was a problem deleting the temp file - errno %d", errno); |
| 1613 | goto done; |
| 1614 | } |
| 1615 | if (rename(TempTableLockFilename, TableLockFilename.u8string().c_str())) { |
| 1616 | snprintf(ErrorString, sizeof(ErrorString), "There was a problem renaming the temp file - errno %d", errno); |
| 1617 | |
| 1618 | goto done; |
| 1619 | } |
| 1620 | mng_EraseLocker(); |
| 1621 | mprintf(0, "Done transferring pages...good luck!\n"); |
| 1622 | done:; |
| 1623 | mem_free(local_tracklocks); |
| 1624 | } |
| 1625 | // #define DELETING_PAGELOCKS 1 |
| 1626 | // #define CLEANING_PAGELOCKS 1 |
| 1627 | // Given a list of names and a pagetype, unlocks the ones already inside the lock file |
| 1628 | int mng_UnlockPagelockSeries(const char *names[], int *pagetypes, int num); |
| 1629 | // Goes through the pagelock table and deletes all duplicate entries |
| 1630 | int mng_DeleteDuplicatePagelocks(); |
| 1631 | void ReorderPagelocks() { |
| 1632 | const char *names[] = {"Lava"}; |
| 1633 | int types[] = {PAGETYPE_SOUND}; |
| 1634 | if (!mng_MakeLocker()) |
| 1635 | return; |
| 1636 | mng_UnlockPagelockSeries(names, types, 1); |
| 1637 | |
| 1638 | // mng_DeleteDuplicatePagelocks (); |
| 1639 | |
| 1640 | mng_EraseLocker(); |
| 1641 | } |
| 1642 | // THIS IS A SPECIAL FUNCTION THAT YOU SHOULD ONLY USE IF YOU KNOW WHAT YOU ARE |
| 1643 | // DOING...it simply reorders the table file so that the "primitives" are first. |
| 1644 | // This helps in the load time of a table file. |
| 1645 | void ReorderPages(int local) { |
| 1646 | CFILE *infile, *outfile; |
| 1647 | uint8_t pagetype; |
| 1648 | int done = 0; |
| 1649 | int len; |
| 1650 | #ifdef CLEANING_PAGELOCKS |
| 1651 | mng_TransferPages(); |
| 1652 | return; |
| 1653 | #endif |
| 1654 | #ifdef DELETING_PAGELOCKS |
| 1655 | ReorderPagelocks(); |
| 1656 | return; |
| 1657 | #endif |
| 1658 | mprintf(0, "Reordering pages, please wait...\n"); |
| 1659 | if (local) |
| 1660 | infile = cfopen(LocalTableFilename, "rb"); |
| 1661 | else { |
| 1662 | if (!mng_MakeLocker()) |
| 1663 | return; |
| 1664 | infile = cfopen(TableFilename, "rb"); |
| 1665 | } |
| 1666 | if (!infile) { |
| 1667 | mprintf(0, "Couldn't open table file to reorder!\n"); |
| 1668 | Int3(); |
| 1669 | return; |
| 1670 | } |
| 1671 | if (local) |
| 1672 | outfile = cfopen(LocalTempTableFilename, "wb"); |
| 1673 | else |
| 1674 | outfile = cfopen(TempTableFilename, "wb"); |
| 1675 | if (!outfile) { |
| 1676 | mprintf(0, "Couldn't open temp table file to reorder!\n"); |
| 1677 | cfclose(infile); |
| 1678 | Int3(); |
| 1679 | return; |
| 1680 | } |
| 1681 | // Do textures |
| 1682 | while (!done) { |
| 1683 | if (cfeof(infile)) { |
| 1684 | done = 1; |
| 1685 | continue; |
| 1686 | } |
| 1687 | pagetype = cf_ReadByte(infile); |
| 1688 | len = cf_ReadInt(infile); |
| 1689 | // If not a door page, just read it in and ignore it |
| 1690 | if (pagetype != PAGETYPE_TEXTURE) { |
| 1691 | cfseek(infile, len - 4, SEEK_CUR); |
| 1692 | continue; |
| 1693 | } |
| 1694 | mng_ReadNewTexturePage(infile, &texpage); |
| 1695 | mng_WriteNewTexturePage(outfile, &texpage); |
| 1696 | } |
| 1697 | // Do sounds |
| 1698 | done = 0; |
| 1699 | cfseek(infile, 0, SEEK_SET); |
| 1700 | while (!done) { |
| 1701 | if (cfeof(infile)) { |
| 1702 | done = 1; |
| 1703 | continue; |
| 1704 | } |
| 1705 | pagetype = cf_ReadByte(infile); |
| 1706 | len = cf_ReadInt(infile); |
| 1707 | // If not a door page, just read it in and ignore it |
| 1708 | if (pagetype != PAGETYPE_SOUND) { |
| 1709 | cfseek(infile, len - 4, SEEK_CUR); |
| 1710 | continue; |
| 1711 | } |
| 1712 | mng_ReadNewSoundPage(infile, &soundpage); |
| 1713 | mng_WriteNewSoundPage(outfile, &soundpage); |
| 1714 | } |
| 1715 | // Do weapons |
| 1716 | done = 0; |
| 1717 | cfseek(infile, 0, SEEK_SET); |
| 1718 | while (!done) { |
| 1719 | if (cfeof(infile)) { |
| 1720 | done = 1; |
| 1721 | continue; |
| 1722 | } |
| 1723 | pagetype = cf_ReadByte(infile); |
| 1724 | len = cf_ReadInt(infile); |
| 1725 | // If not a door page, just read it in and ignore it |
| 1726 | if (pagetype != PAGETYPE_WEAPON) { |
| 1727 | cfseek(infile, len - 4, SEEK_CUR); |
| 1728 | continue; |
| 1729 | } |
| 1730 | mng_ReadNewWeaponPage(infile, &weaponpage); |
| 1731 | // Ignore counter measure weapons |
| 1732 | if (!(weaponpage.weapon_struct.flags & WF_SPAWNS_ROBOT)) |
| 1733 | mng_WriteNewWeaponPage(outfile, &weaponpage); |
| 1734 | } |
| 1735 | // Do powerup generics |
| 1736 | done = 0; |
| 1737 | cfseek(infile, 0, SEEK_SET); |
| 1738 | while (!done) { |
| 1739 | if (cfeof(infile)) { |
| 1740 | done = 1; |
| 1741 | continue; |
| 1742 | } |
| 1743 | pagetype = cf_ReadByte(infile); |
| 1744 | len = cf_ReadInt(infile); |
| 1745 | // If not a door page, just read it in and ignore it |
| 1746 | if (pagetype != PAGETYPE_GENERIC) { |
| 1747 | cfseek(infile, len - 4, SEEK_CUR); |
| 1748 | continue; |
| 1749 | } |
| 1750 | mng_ReadNewGenericPage(infile, &genericpage); |
| 1751 | if (genericpage.objinfo_struct.type == OBJ_POWERUP) |
| 1752 | mng_WriteNewGenericPage(outfile, &genericpage); |
| 1753 | if (genericpage.objinfo_struct.description != NULL) { |
| 1754 | mem_free(genericpage.objinfo_struct.description); |
| 1755 | genericpage.objinfo_struct.description = NULL; |
| 1756 | } |
| 1757 | } |
| 1758 | // Do standard generics |
| 1759 | done = 0; |
| 1760 | cfseek(infile, 0, SEEK_SET); |
| 1761 | while (!done) { |
| 1762 | if (cfeof(infile)) { |
| 1763 | done = 1; |
| 1764 | continue; |
| 1765 | } |
| 1766 | pagetype = cf_ReadByte(infile); |
| 1767 | len = cf_ReadInt(infile); |
| 1768 | // If not a door page, just read it in and ignore it |
| 1769 | if (pagetype != PAGETYPE_GENERIC) { |
| 1770 | cfseek(infile, len - 4, SEEK_CUR); |
| 1771 | continue; |
| 1772 | } |
| 1773 | mng_ReadNewGenericPage(infile, &genericpage); |
| 1774 | if (genericpage.objinfo_struct.type != OBJ_POWERUP) |
| 1775 | mng_WriteNewGenericPage(outfile, &genericpage); |
| 1776 | if (genericpage.objinfo_struct.description != NULL) { |
| 1777 | mem_free(genericpage.objinfo_struct.description); |
| 1778 | genericpage.objinfo_struct.description = NULL; |
| 1779 | } |
| 1780 | } |
| 1781 | // Do countermeasure weapons |
| 1782 | done = 0; |
| 1783 | cfseek(infile, 0, SEEK_SET); |
| 1784 | while (!done) { |
| 1785 | if (cfeof(infile)) { |
| 1786 | done = 1; |
| 1787 | continue; |
| 1788 | } |
| 1789 | pagetype = cf_ReadByte(infile); |
| 1790 | len = cf_ReadInt(infile); |
| 1791 | // If not a door page, just read it in and ignore it |
| 1792 | if (pagetype != PAGETYPE_WEAPON) { |
| 1793 | cfseek(infile, len - 4, SEEK_CUR); |
| 1794 | continue; |
| 1795 | } |
| 1796 | mng_ReadNewWeaponPage(infile, &weaponpage); |
| 1797 | if ((weaponpage.weapon_struct.flags & WF_SPAWNS_ROBOT)) |
| 1798 | mng_WriteNewWeaponPage(outfile, &weaponpage); |
| 1799 | } |
| 1800 | |
| 1801 | // Do doors |
| 1802 | done = 0; |
| 1803 | cfseek(infile, 0, SEEK_SET); |
| 1804 | while (!done) { |
| 1805 | if (cfeof(infile)) { |
| 1806 | done = 1; |
| 1807 | continue; |
| 1808 | } |
| 1809 | pagetype = cf_ReadByte(infile); |
| 1810 | len = cf_ReadInt(infile); |
| 1811 | // If not a door page, just read it in and ignore it |
| 1812 | if (pagetype != PAGETYPE_DOOR) { |
| 1813 | cfseek(infile, len - 4, SEEK_CUR); |
| 1814 | continue; |
| 1815 | } |
| 1816 | mng_ReadNewDoorPage(infile, &doorpage); |
| 1817 | mng_WriteNewDoorPage(outfile, &doorpage); |
| 1818 | } |
| 1819 | // Do player ships |
| 1820 | done = 0; |
| 1821 | cfseek(infile, 0, SEEK_SET); |
| 1822 | while (!done) { |
| 1823 | if (cfeof(infile)) { |
| 1824 | done = 1; |
| 1825 | continue; |
| 1826 | } |
| 1827 | pagetype = cf_ReadByte(infile); |
| 1828 | len = cf_ReadInt(infile); |
| 1829 | // If not a door page, just read it in and ignore it |
| 1830 | if (pagetype != PAGETYPE_SHIP) { |
| 1831 | cfseek(infile, len - 4, SEEK_CUR); |
| 1832 | continue; |
| 1833 | } |
| 1834 | mng_ReadNewShipPage(infile, &shippage); |
| 1835 | mng_WriteNewShipPage(outfile, &shippage); |
| 1836 | } |
| 1837 | // Do gamefiles |
| 1838 | done = 0; |
| 1839 | cfseek(infile, 0, SEEK_SET); |
| 1840 | while (!done) { |
| 1841 | if (cfeof(infile)) { |
| 1842 | done = 1; |
| 1843 | continue; |
| 1844 | } |
| 1845 | pagetype = cf_ReadByte(infile); |
| 1846 | len = cf_ReadInt(infile); |
| 1847 | // If not a door page, just read it in and ignore it |
| 1848 | if (pagetype != PAGETYPE_GAMEFILE) { |
| 1849 | cfseek(infile, len - 4, SEEK_CUR); |
| 1850 | continue; |
| 1851 | } |
| 1852 | mng_ReadNewGamefilePage(infile, &gamefilepage); |
| 1853 | mng_WriteNewGamefilePage(outfile, &gamefilepage); |
| 1854 | } |
| 1855 | // Do megacells |
| 1856 | done = 0; |
| 1857 | cfseek(infile, 0, SEEK_SET); |
| 1858 | while (!done) { |
| 1859 | if (cfeof(infile)) { |
| 1860 | done = 1; |
| 1861 | continue; |
| 1862 | } |
| 1863 | pagetype = cf_ReadByte(infile); |
| 1864 | len = cf_ReadInt(infile); |
| 1865 | // If not a door page, just read it in and ignore it |
| 1866 | if (pagetype != PAGETYPE_MEGACELL) { |
| 1867 | cfseek(infile, len - 4, SEEK_CUR); |
| 1868 | continue; |
| 1869 | } |
| 1870 | mng_ReadNewMegacellPage(infile, &megacellpage); |
| 1871 | mng_WriteNewMegacellPage(outfile, &megacellpage); |
| 1872 | } |
| 1873 | |
| 1874 | cfclose(infile); |
| 1875 | cfclose(outfile); |
| 1876 | if (local) { |
| 1877 | SwitcherooFiles(LocalTableFilename, LocalTempTableFilename); |
| 1878 | } else { |
| 1879 | SwitcherooFiles(TableFilename, TempTableFilename); |
| 1880 | } |
| 1881 | mng_EraseLocker(); |
| 1882 | } |
| 1883 | // Returns true if the passed in pagelock is in the LockList, else false |
| 1884 | bool InLockList(mngs_Pagelock *pl) { |
| 1885 | if (Starting_editor) { |
| 1886 | for (int i = 0; i < Num_locklist; i++) { |
| 1887 | if (LockList[i].pagetype == pl->pagetype) { |
| 1888 | if (!stricmp(LockList[i].name, pl->name)) |
| 1889 | return true; |
| 1890 | } |
| 1891 | } |
| 1892 | } else { |
| 1893 | if ((mng_CheckIfPageOwned(pl, TableUser)) > 0) // DAJ -1FIX |
| 1894 | return true; |
| 1895 | } |
| 1896 | return false; |
| 1897 | } |
| 1898 | // Given a filename, returns the type of primitive it is |
| 1899 | int GetPrimType(const std::filesystem::path &name) { |
| 1900 | int primtype; |
| 1901 | std::filesystem::path ext = name.extension(); |
| 1902 | if (!stricmp(".oof", ext.u8string().c_str())) |
| 1903 | primtype = PRIMTYPE_OOF; |
| 1904 | else if (!stricmp(".ogf", ext.u8string().c_str())) |
| 1905 | primtype = PRIMTYPE_OGF; |
| 1906 | else if (!stricmp(".oaf", ext.u8string().c_str())) |
| 1907 | primtype = PRIMTYPE_OAF; |
| 1908 | else if (!stricmp(".wav", ext.u8string().c_str())) |
| 1909 | primtype = PRIMTYPE_WAV; |
| 1910 | else |
| 1911 | primtype = PRIMTYPE_FILE; |
| 1912 | return primtype; |
| 1913 | } |
| 1914 | |
| 1915 | #if defined(WIN32) |
| 1916 | // Builds a list of old files in a path |
| 1917 | void BuildOldFilesForDirectory(const std::filesystem::path& path, FILETIME threshold) { |
| 1918 | HANDLE filehandle; |
| 1919 | WIN32_FIND_DATA filedata; |
| 1920 | std::filesystem::path newpath = path / "*.*"; |
| 1921 | filehandle = FindFirstFile(newpath.u8string().c_str(), &filedata); |
| 1922 | bool go_ahead = true; |
| 1923 | if (filehandle == INVALID_HANDLE_VALUE) |
| 1924 | go_ahead = false; |
| 1925 | while (go_ahead) { |
| 1926 | bool add_it = false; |
| 1927 | |
| 1928 | // if this file is newer than the last time we updated, add it to the list |
| 1929 | |
| 1930 | if (filedata.ftLastWriteTime.dwHighDateTime > threshold.dwHighDateTime) |
| 1931 | add_it = true; |
| 1932 | if (filedata.ftLastWriteTime.dwHighDateTime == threshold.dwHighDateTime) { |
| 1933 | if (filedata.ftLastWriteTime.dwLowDateTime > threshold.dwLowDateTime) |
| 1934 | add_it = true; |
| 1935 | } |
| 1936 | if (filedata.ftCreationTime.dwHighDateTime > threshold.dwHighDateTime) |
| 1937 | add_it = true; |
| 1938 | if (filedata.ftCreationTime.dwHighDateTime == threshold.dwHighDateTime) { |
| 1939 | if (filedata.ftCreationTime.dwLowDateTime > threshold.dwLowDateTime) |
| 1940 | add_it = true; |
| 1941 | } |
| 1942 | // Add it to the list! |
| 1943 | if (add_it) { |
| 1944 | int primtype = GetPrimType(filedata.cFileName); |
| 1945 | OldFiles[Num_old_files].type = primtype; |
| 1946 | strcpy(OldFiles[Num_old_files].name, filedata.cFileName); |
| 1947 | Num_old_files++; |
| 1948 | } |
| 1949 | go_ahead = (FindNextFile(filehandle, &filedata) != 0); |
| 1950 | } |
| 1951 | if (filehandle != INVALID_HANDLE_VALUE) |
| 1952 | FindClose(filehandle); |
| 1953 | } |
| 1954 | |
| 1955 | // Builds a list of old files so we know which ones to update |
| 1956 | // Searches through all our netdirectories for old files |
| 1957 | void BuildOldFileList(FILETIME threshold) { |
| 1958 | char str[_MAX_PATH]; |
| 1959 | mprintf(0, "Building old files list!\n"); |
| 1960 | BuildOldFilesForDirectory(NetModelsDir, threshold); |
| 1961 | BuildOldFilesForDirectory(NetSoundsDir, threshold); |
| 1962 | BuildOldFilesForDirectory(NetMiscDir, threshold); |
| 1963 | BuildOldFilesForDirectory(ManageGraphicsDir, threshold); |
| 1964 | BuildOldFilesForDirectory(NetArtDir, threshold); |
| 1965 | BuildOldFilesForDirectory(NetMusicDir, threshold); |
| 1966 | BuildOldFilesForDirectory(NetVoiceDir, threshold); |
| 1967 | ddio_MakePath(str, NetD3Dir, "data", "levels", NULL); |
| 1968 | BuildOldFilesForDirectory(str, threshold); |
| 1969 | ddio_MakePath(str, NetD3Dir, "data", "briefings", NULL); |
| 1970 | BuildOldFilesForDirectory(str, threshold); |
| 1971 | ddio_MakePath(str, NetD3Dir, "data", "scripts", NULL); |
| 1972 | BuildOldFilesForDirectory(str, threshold); |
| 1973 | mprintf(0, "Found %d old files.\n", Num_old_files); |
| 1974 | } |
| 1975 | #endif |
| 1976 | |
| 1977 | // Returns true if the passed in primitive is old (ie needs to be updated from the network) |
| 1978 | bool IsPrimitiveOld(char *name) { |
| 1979 | int primtype = GetPrimType(name); |
| 1980 | for (int i = 0; i < Num_old_files; i++) { |
| 1981 | if (OldFiles[i].type == primtype && !stricmp(OldFiles[i].name, name)) |
| 1982 | return true; |
| 1983 | } |
| 1984 | |
| 1985 | return false; |
| 1986 | } |
| 1987 | |
| 1988 | // Updates a primitive if needed |
| 1989 | // Localname = local version of the primname (with path) |
| 1990 | // Netname = Network version of the primname (with path) |
| 1991 | void UpdatePrimitive(const std::filesystem::path &localname, const std::filesystem::path &netname, char *primname, |
| 1992 | int pagetype, char *pagename) { |
| 1993 | bool update = false; |
| 1994 | if (Starting_editor && !Use_old_update_method) { |
| 1995 | if (IsPrimitiveOld(primname)) |
| 1996 | update = true; |
| 1997 | } else { |
| 1998 | if (!cfexist(localname) || (cfexist(netname) && cf_Diff(localname, netname))) |
| 1999 | update = true; |
| 2000 | } |
| 2001 | |
| 2002 | if (update) { |
| 2003 | mngs_Pagelock temp_pl; |
| 2004 | temp_pl.pagetype = pagetype; |
| 2005 | strcpy(temp_pl.name, pagename); |
| 2006 | if (!InLockList(&temp_pl)) { |
| 2007 | mprintf(0, "Making a local copy of %s for next time.\n", primname); |
| 2008 | if (!cf_CopyFile(localname, netname, 1)) { |
| 2009 | Int3(); // get Jason |
| 2010 | return; |
| 2011 | } |
| 2012 | } |
| 2013 | } |
| 2014 | } |
| 2015 | // Writes a chunk header. Writes chunk id & placeholder length. Returns chunk start pos |
| 2016 | int StartManagePage(CFILE *ofile, uint8_t pagetype) { |
| 2017 | int chunk_start_pos; |
| 2018 | // Write pagetype |
| 2019 | cf_WriteByte(ofile, pagetype); |
| 2020 | chunk_start_pos = cftell(ofile); |
| 2021 | cf_WriteInt(ofile, 0); // placeholder for chunk len |
| 2022 | |
| 2023 | return chunk_start_pos; |
| 2024 | } |
| 2025 | // Fill in page length when done writing |
| 2026 | void EndManagePage(CFILE *ofile, int chunk_start_pos) { |
| 2027 | int save_pos = cftell(ofile); |
| 2028 | int len = save_pos - chunk_start_pos; |
| 2029 | // seek back to len field and fill in value |
| 2030 | cfseek(ofile, chunk_start_pos, SEEK_SET); |
| 2031 | cf_WriteInt(ofile, len); // write chunk length |
| 2032 | // go back to end of file |
| 2033 | cfseek(ofile, save_pos, SEEK_SET); |
| 2034 | } |
| 2035 | // Assigns a page to its appropriate structure and writes it out |
| 2036 | void mng_AssignAndWritePage(int handle, int pagetype, CFILE *outfile) { |
| 2037 | switch (pagetype) { |
| 2038 | case PAGETYPE_TEXTURE: |
| 2039 | mng_AssignTextureToTexPage(handle, &texpage); |
| 2040 | mng_WriteNewTexturePage(outfile, &texpage); |
| 2041 | break; |
| 2042 | case PAGETYPE_SOUND: |
| 2043 | mng_AssignSoundToSoundPage(handle, &soundpage); |
| 2044 | mng_WriteNewSoundPage(outfile, &soundpage); |
| 2045 | break; |
| 2046 | case PAGETYPE_WEAPON: |
| 2047 | mng_AssignWeaponToWeaponPage(handle, &weaponpage); |
| 2048 | mng_WriteNewWeaponPage(outfile, &weaponpage); |
| 2049 | break; |
| 2050 | case PAGETYPE_GENERIC: |
| 2051 | mng_AssignObjInfoToGenericPage(handle, &genericpage); |
| 2052 | mng_WriteNewGenericPage(outfile, &genericpage); |
| 2053 | break; |
| 2054 | case PAGETYPE_DOOR: |
| 2055 | mng_AssignDoorToDoorPage(handle, &doorpage); |
| 2056 | mng_WriteNewDoorPage(outfile, &doorpage); |
| 2057 | break; |
| 2058 | case PAGETYPE_MEGACELL: |
| 2059 | mng_AssignMegacellToMegacellPage(handle, &megacellpage); |
| 2060 | mng_WriteNewMegacellPage(outfile, &megacellpage); |
| 2061 | break; |
| 2062 | case PAGETYPE_SHIP: |
| 2063 | mng_AssignShipToShipPage(handle, &shippage); |
| 2064 | mng_WriteNewShipPage(outfile, &shippage); |
| 2065 | break; |
| 2066 | case PAGETYPE_GAMEFILE: |
| 2067 | mng_AssignGamefileToGamefilePage(handle, &gamefilepage); |
| 2068 | mng_WriteNewGamefilePage(outfile, &gamefilepage); |
| 2069 | break; |
| 2070 | default: |
| 2071 | break; |
| 2072 | } |
| 2073 | } |
| 2074 | #define COPYBUFFER_SIZE 200000 |
| 2075 | // Given a texture handle, searches the table file and replaces the texture with the same name |
| 2076 | // If local=1, then does it to the users local copy |
| 2077 | // Returns 0 on error, else 1 if all is good |
| 2078 | int mng_ReplacePage(char *srcname, char *destname, int handle, int dest_pagetype, int local) { |
| 2079 | CFILE *infile, *outfile; |
| 2080 | uint8_t pagetype, replaced = 0; |
| 2081 | int done = 0, len; |
| 2082 | mprintf(0, "Replacing '%s' with '%s' (%s).\n", srcname, destname, local ? "locally" : "to network"); |
| 2083 | |
| 2084 | if (local) |
| 2085 | infile = cfopen(LocalTableFilename, "rb"); |
| 2086 | else |
| 2087 | infile = cfopen(TableFilename, "rb"); |
| 2088 | if (!infile) { |
| 2089 | mprintf(0, "Couldn't open table file to replace page %s!\n", srcname); |
| 2090 | Int3(); |
| 2091 | return 0; |
| 2092 | } |
| 2093 | if (local) |
| 2094 | outfile = cfopen(LocalTempTableFilename, "wb"); |
| 2095 | else |
| 2096 | outfile = cfopen(TempTableFilename, "wb"); |
| 2097 | if (!outfile) { |
| 2098 | mprintf(0, "Couldn't open temp table file to replace page %s!\n", srcname); |
| 2099 | cfclose(infile); |
| 2100 | Int3(); |
| 2101 | return 0; |
| 2102 | } |
| 2103 | // Allocate memory for copying |
| 2104 | uint8_t *copybuffer = (uint8_t *)mem_malloc(COPYBUFFER_SIZE); |
| 2105 | if (!copybuffer) { |
| 2106 | mprintf(0, "Couldn't allocate memory to replace page %s!\n", srcname); |
| 2107 | cfclose(infile); |
| 2108 | cfclose(outfile); |
| 2109 | Int3(); |
| 2110 | return 0; |
| 2111 | } |
| 2112 | while (!done) { |
| 2113 | if (cfeof(infile)) { |
| 2114 | done = 1; |
| 2115 | continue; |
| 2116 | } |
| 2117 | pagetype = cf_ReadByte(infile); |
| 2118 | len = cf_ReadInt(infile); |
| 2119 | // If not a texture page, just read it in and write it right back out |
| 2120 | if (pagetype != dest_pagetype) { |
| 2121 | ASSERT(len < COPYBUFFER_SIZE); |
| 2122 | |
| 2123 | cf_ReadBytes(copybuffer, len - 4, infile); |
| 2124 | cf_WriteByte(outfile, pagetype); |
| 2125 | cf_WriteInt(outfile, len); |
| 2126 | if (len - 4 > 0) |
| 2127 | cf_WriteBytes(copybuffer, len - 4, outfile); |
| 2128 | |
| 2129 | continue; |
| 2130 | } |
| 2131 | switch (pagetype) { |
| 2132 | case PAGETYPE_TEXTURE: { |
| 2133 | mng_ReadNewTexturePage(infile, &texpage); |
| 2134 | if (!stricmp(srcname, texpage.tex_struct.name)) { |
| 2135 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2136 | replaced = 1; |
| 2137 | } else { |
| 2138 | mng_WriteNewTexturePage(outfile, &texpage); |
| 2139 | } |
| 2140 | break; |
| 2141 | } |
| 2142 | case PAGETYPE_SOUND: { |
| 2143 | mng_ReadNewSoundPage(infile, &soundpage); |
| 2144 | if (!stricmp(srcname, soundpage.sound_struct.name)) { |
| 2145 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2146 | replaced = 1; |
| 2147 | } else { |
| 2148 | mng_WriteNewSoundPage(outfile, &soundpage); |
| 2149 | } |
| 2150 | break; |
| 2151 | } |
| 2152 | case PAGETYPE_WEAPON: { |
| 2153 | mng_ReadNewWeaponPage(infile, &weaponpage); |
| 2154 | if (!stricmp(srcname, weaponpage.weapon_struct.name)) { |
| 2155 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2156 | replaced = 1; |
| 2157 | } else { |
| 2158 | mng_WriteNewWeaponPage(outfile, &weaponpage); |
| 2159 | } |
| 2160 | break; |
| 2161 | } |
| 2162 | case PAGETYPE_GENERIC: { |
| 2163 | mng_ReadNewGenericPage(infile, &genericpage); |
| 2164 | if (!stricmp(srcname, genericpage.objinfo_struct.name)) { |
| 2165 | // This is the page we want to replace, so write the new one out. |
| 2166 | if (genericpage.objinfo_struct.description != NULL) { |
| 2167 | mem_free(genericpage.objinfo_struct.description); |
| 2168 | genericpage.objinfo_struct.description = NULL; |
| 2169 | } |
| 2170 | genericpage.objinfo_struct.icon_name[0] = '\0'; |
| 2171 | |
| 2172 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2173 | replaced = 1; |
| 2174 | } else { |
| 2175 | mng_WriteNewGenericPage(outfile, &genericpage); |
| 2176 | } |
| 2177 | break; |
| 2178 | } |
| 2179 | case PAGETYPE_DOOR: { |
| 2180 | mng_ReadNewDoorPage(infile, &doorpage); |
| 2181 | if (!stricmp(srcname, doorpage.door_struct.name)) { |
| 2182 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2183 | replaced = 1; |
| 2184 | } else { |
| 2185 | mng_WriteNewDoorPage(outfile, &doorpage); |
| 2186 | } |
| 2187 | break; |
| 2188 | } |
| 2189 | case PAGETYPE_MEGACELL: { |
| 2190 | mng_ReadNewMegacellPage(infile, &megacellpage); |
| 2191 | if (!stricmp(srcname, megacellpage.megacell_struct.name)) { |
| 2192 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2193 | replaced = 1; |
| 2194 | } else { |
| 2195 | mng_WriteNewMegacellPage(outfile, &megacellpage); |
| 2196 | } |
| 2197 | break; |
| 2198 | } |
| 2199 | case PAGETYPE_SHIP: { |
| 2200 | mng_ReadNewShipPage(infile, &shippage); |
| 2201 | if (!stricmp(srcname, shippage.ship_struct.name)) { |
| 2202 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2203 | replaced = 1; |
| 2204 | } else { |
| 2205 | mng_WriteNewShipPage(outfile, &shippage); |
| 2206 | } |
| 2207 | break; |
| 2208 | } |
| 2209 | case PAGETYPE_GAMEFILE: { |
| 2210 | mng_ReadNewGamefilePage(infile, &gamefilepage); |
| 2211 | if (!stricmp(srcname, gamefilepage.gamefile_struct.name)) { |
| 2212 | mng_AssignAndWritePage(handle, pagetype, outfile); |
| 2213 | replaced = 1; |
| 2214 | } else { |
| 2215 | mng_WriteNewGamefilePage(outfile, &gamefilepage); |
| 2216 | } |
| 2217 | break; |
| 2218 | } |
| 2219 | default: |
| 2220 | break; |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | if (!replaced) { |
| 2225 | // This is a new page, so append it to the end of file. |
| 2226 | mng_AssignAndWritePage(handle, dest_pagetype, outfile); |
| 2227 | } |
| 2228 | if (replaced) |
| 2229 | mprintf(0, "Page replaced.\n"); |
| 2230 | else |
| 2231 | mprintf(0, "New page added.\n"); |
| 2232 | cfclose(infile); |
| 2233 | cfclose(outfile); |
| 2234 | mem_free(copybuffer); |
| 2235 | if (local) { |
| 2236 | if (!SwitcherooFiles(LocalTableFilename, LocalTempTableFilename)) |
| 2237 | return 0; |
| 2238 | } else { |
| 2239 | if (!SwitcherooFiles(TableFilename, TempTableFilename)) |
| 2240 | return 0; |
| 2241 | } |
| 2242 | return 1; // successful! |
| 2243 | } |
| 2244 | // Given a texture name, finds it in the table file and deletes it |
| 2245 | // If local is 1, deletes from the local table file |
| 2246 | int mng_DeletePage(char *name, int dest_pagetype, int local) { |
| 2247 | CFILE *infile, *outfile; |
| 2248 | uint8_t pagetype, replaced = 0; |
| 2249 | int done = 0; |
| 2250 | int deleted = 0; |
| 2251 | |
| 2252 | mprintf(0, "Deleting %s (%s).\n", name, local ? "locally" : "on network"); |
| 2253 | |
| 2254 | if (local) |
| 2255 | infile = cfopen(LocalTableFilename, "rb"); |
| 2256 | else |
| 2257 | infile = cfopen(TableFilename, "rb"); |
| 2258 | if (!infile) { |
| 2259 | mprintf(0, "Couldn't open table file to delete page!\n"); |
| 2260 | Int3(); |
| 2261 | return 0; |
| 2262 | } |
| 2263 | if (local) |
| 2264 | outfile = cfopen(LocalTempTableFilename, "wb"); |
| 2265 | else |
| 2266 | outfile = cfopen(TempTableFilename, "wb"); |
| 2267 | if (!outfile) { |
| 2268 | mprintf(0, "Couldn't open temp table file to delete page!\n"); |
| 2269 | cfclose(infile); |
| 2270 | Int3(); |
| 2271 | return 0; |
| 2272 | } |
| 2273 | // Allocate memory for copying |
| 2274 | uint8_t *copybuffer = (uint8_t *)mem_malloc(COPYBUFFER_SIZE); |
| 2275 | if (!copybuffer) { |
| 2276 | mprintf(0, "Couldn't allocate memory to delete page!\n"); |
| 2277 | cfclose(infile); |
| 2278 | cfclose(outfile); |
| 2279 | Int3(); |
| 2280 | return 0; |
| 2281 | } |
| 2282 | while (!done) { |
| 2283 | if (cfeof(infile)) { |
| 2284 | done = 1; |
| 2285 | continue; |
| 2286 | } |
| 2287 | pagetype = cf_ReadByte(infile); |
| 2288 | int len = cf_ReadInt(infile); |
| 2289 | if (pagetype != dest_pagetype) { |
| 2290 | ASSERT(len < COPYBUFFER_SIZE); |
| 2291 | |
| 2292 | cf_ReadBytes(copybuffer, len - 4, infile); |
| 2293 | cf_WriteByte(outfile, pagetype); |
| 2294 | cf_WriteInt(outfile, len); |
| 2295 | if (len - 4 > 0) |
| 2296 | cf_WriteBytes(copybuffer, len - 4, outfile); |
| 2297 | |
| 2298 | continue; |
| 2299 | } |
| 2300 | switch (pagetype) { |
| 2301 | |
| 2302 | case PAGETYPE_TEXTURE: { |
| 2303 | mng_ReadNewTexturePage(infile, &texpage); |
| 2304 | if (stricmp(name, texpage.tex_struct.name)) |
| 2305 | mng_WriteNewTexturePage(outfile, &texpage); |
| 2306 | else |
| 2307 | deleted = 1; // Don't write out the one we want to delete |
| 2308 | break; |
| 2309 | } |
| 2310 | case PAGETYPE_DOOR: { |
| 2311 | mng_ReadNewDoorPage(infile, &doorpage); |
| 2312 | if (stricmp(name, doorpage.door_struct.name)) |
| 2313 | mng_WriteNewDoorPage(outfile, &doorpage); |
| 2314 | else |
| 2315 | deleted = 1; // Don't write out the one we want to delete |
| 2316 | break; |
| 2317 | } |
| 2318 | case PAGETYPE_GENERIC: { |
| 2319 | mng_ReadNewGenericPage(infile, &genericpage); |
| 2320 | if (stricmp(name, genericpage.objinfo_struct.name)) |
| 2321 | mng_WriteNewGenericPage(outfile, &genericpage); |
| 2322 | else |
| 2323 | deleted = 1; // Don't write out the one we want to delete |
| 2324 | if (genericpage.objinfo_struct.description != NULL) { |
| 2325 | mem_free(genericpage.objinfo_struct.description); |
| 2326 | genericpage.objinfo_struct.description = NULL; |
| 2327 | } |
| 2328 | genericpage.objinfo_struct.icon_name[0] = '\0'; |
| 2329 | break; |
| 2330 | } |
| 2331 | case PAGETYPE_SOUND: { |
| 2332 | mng_ReadNewSoundPage(infile, &soundpage); |
| 2333 | if (stricmp(name, soundpage.sound_struct.name)) |
| 2334 | mng_WriteNewSoundPage(outfile, &soundpage); |
| 2335 | else |
| 2336 | deleted = 1; // Don't write out the one we want to delete |
| 2337 | break; |
| 2338 | } |
| 2339 | case PAGETYPE_SHIP: { |
| 2340 | mng_ReadNewShipPage(infile, &shippage); |
| 2341 | if (stricmp(name, shippage.ship_struct.name)) |
| 2342 | mng_WriteNewShipPage(outfile, &shippage); |
| 2343 | else |
| 2344 | deleted = 1; // Don't write out the one we want to delete |
| 2345 | break; |
| 2346 | } |
| 2347 | case PAGETYPE_WEAPON: { |
| 2348 | mng_ReadNewWeaponPage(infile, &weaponpage); |
| 2349 | if (stricmp(name, weaponpage.weapon_struct.name)) |
| 2350 | mng_WriteNewWeaponPage(outfile, &weaponpage); |
| 2351 | else |
| 2352 | deleted = 1; // Don't write out the one we want to delete |
| 2353 | break; |
| 2354 | } |
| 2355 | case PAGETYPE_MEGACELL: { |
| 2356 | mng_ReadNewMegacellPage(infile, &megacellpage); |
| 2357 | if (stricmp(name, megacellpage.megacell_struct.name)) |
| 2358 | mng_WriteNewMegacellPage(outfile, &megacellpage); |
| 2359 | else |
| 2360 | deleted = 1; // Don't write out the one we want to delete |
| 2361 | break; |
| 2362 | } |
| 2363 | case PAGETYPE_GAMEFILE: { |
| 2364 | mng_ReadNewGamefilePage(infile, &gamefilepage); |
| 2365 | if (stricmp(name, gamefilepage.gamefile_struct.name)) |
| 2366 | mng_WriteNewGamefilePage(outfile, &gamefilepage); |
| 2367 | else |
| 2368 | deleted = 1; // Don't write out the one we want to delete |
| 2369 | break; |
| 2370 | } |
| 2371 | default: |
| 2372 | break; |
| 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | if (!local) { |
| 2377 | // It's gotta be there if on the network |
| 2378 | ASSERT(deleted == 1); |
| 2379 | } else { |
| 2380 | if (!deleted) { |
| 2381 | mprintf(0, "Not found locally?!\n"); |
| 2382 | } |
| 2383 | } |
| 2384 | cfclose(infile); |
| 2385 | cfclose(outfile); |
| 2386 | mem_free(copybuffer); |
| 2387 | // Now, remove our table file and rename the temp file to be the table file |
| 2388 | if (local) { |
| 2389 | if (!SwitcherooFiles(LocalTableFilename, LocalTempTableFilename)) |
| 2390 | return 0; |
| 2391 | } else { |
| 2392 | if (!SwitcherooFiles(TableFilename, TempTableFilename)) |
| 2393 | return 0; |
| 2394 | } |
| 2395 | return 1; // successful! |
| 2396 | } |
| 2397 | // Reads in a physics chunk from an open file |
| 2398 | void mng_ReadPhysicsChunk(physics_info *phys_info, CFILE *infile) { |
| 2399 | phys_info->mass = cf_ReadFloat(infile); |
| 2400 | phys_info->drag = cf_ReadFloat(infile); |
| 2401 | phys_info->full_thrust = cf_ReadFloat(infile); |
| 2402 | phys_info->flags = cf_ReadInt(infile); |
| 2403 | phys_info->rotdrag = cf_ReadFloat(infile); |
| 2404 | phys_info->full_rotthrust = cf_ReadFloat(infile); |
| 2405 | phys_info->num_bounces = cf_ReadInt(infile); |
| 2406 | phys_info->velocity.z = cf_ReadFloat(infile); |
| 2407 | phys_info->rotvel.x = cf_ReadFloat(infile); |
| 2408 | phys_info->rotvel.y = cf_ReadFloat(infile); |
| 2409 | phys_info->rotvel.z = cf_ReadFloat(infile); |
| 2410 | phys_info->wiggle_amplitude = cf_ReadFloat(infile); |
| 2411 | phys_info->wiggles_per_sec = cf_ReadFloat(infile); |
| 2412 | phys_info->coeff_restitution = cf_ReadFloat(infile); |
| 2413 | phys_info->hit_die_dot = cf_ReadFloat(infile); |
| 2414 | phys_info->max_turnroll_rate = cf_ReadFloat(infile); |
| 2415 | phys_info->turnroll_ratio = cf_ReadFloat(infile); |
| 2416 | } |
| 2417 | // Writes out a physics chunk to an open file |
| 2418 | void mng_WritePhysicsChunk(physics_info *phys_info, CFILE *outfile) { |
| 2419 | cf_WriteFloat(outfile, phys_info->mass); |
| 2420 | cf_WriteFloat(outfile, phys_info->drag); |
| 2421 | cf_WriteFloat(outfile, phys_info->full_thrust); |
| 2422 | cf_WriteInt(outfile, phys_info->flags); |
| 2423 | cf_WriteFloat(outfile, phys_info->rotdrag); |
| 2424 | cf_WriteFloat(outfile, phys_info->full_rotthrust); |
| 2425 | cf_WriteInt(outfile, phys_info->num_bounces); |
| 2426 | cf_WriteFloat(outfile, phys_info->velocity.z); |
| 2427 | cf_WriteFloat(outfile, phys_info->rotvel.x); |
| 2428 | cf_WriteFloat(outfile, phys_info->rotvel.y); |
| 2429 | cf_WriteFloat(outfile, phys_info->rotvel.z); |
| 2430 | cf_WriteFloat(outfile, phys_info->wiggle_amplitude); |
| 2431 | cf_WriteFloat(outfile, phys_info->wiggles_per_sec); |
| 2432 | cf_WriteFloat(outfile, phys_info->coeff_restitution); |
| 2433 | cf_WriteFloat(outfile, phys_info->hit_die_dot); |
| 2434 | cf_WriteFloat(outfile, phys_info->max_turnroll_rate); |
| 2435 | cf_WriteFloat(outfile, phys_info->turnroll_ratio); |
| 2436 | } |
| 2437 | // Writes out weapon battery info |
| 2438 | void mng_WriteWeaponBatteryChunk(otype_wb_info *static_wb, CFILE *outfile) { |
| 2439 | int j; |
| 2440 | cf_WriteFloat(outfile, static_wb->energy_usage); |
| 2441 | cf_WriteFloat(outfile, static_wb->ammo_usage); |
| 2442 | for (j = 0; j < MAX_WB_GUNPOINTS; j++) { |
| 2443 | cf_WriteShort(outfile, static_wb->gp_weapon_index[j]); |
| 2444 | } |
| 2445 | |
| 2446 | for (j = 0; j < MAX_WB_FIRING_MASKS; j++) { |
| 2447 | cf_WriteByte(outfile, static_wb->gp_fire_masks[j]); |
| 2448 | cf_WriteFloat(outfile, static_wb->gp_fire_wait[j]); |
| 2449 | cf_WriteFloat(outfile, static_wb->anim_time[j]); |
| 2450 | cf_WriteFloat(outfile, static_wb->anim_start_frame[j]); |
| 2451 | cf_WriteFloat(outfile, static_wb->anim_fire_frame[j]); |
| 2452 | cf_WriteFloat(outfile, static_wb->anim_end_frame[j]); |
| 2453 | } |
| 2454 | cf_WriteByte(outfile, static_wb->num_masks); |
| 2455 | cf_WriteShort(outfile, static_wb->aiming_gp_index); |
| 2456 | cf_WriteByte(outfile, static_wb->aiming_flags); |
| 2457 | cf_WriteFloat(outfile, static_wb->aiming_3d_dot); |
| 2458 | cf_WriteFloat(outfile, static_wb->aiming_3d_dist); |
| 2459 | cf_WriteFloat(outfile, static_wb->aiming_XZ_dot); |
| 2460 | cf_WriteShort(outfile, static_wb->flags); |
| 2461 | cf_WriteByte(outfile, static_wb->gp_quad_fire_mask); |
| 2462 | } |
| 2463 | // Reads in weapon battery info |
| 2464 | void mng_ReadWeaponBatteryChunk(otype_wb_info *static_wb, CFILE *infile, int version) { |
| 2465 | int j; |
| 2466 | |
| 2467 | static_wb->energy_usage = cf_ReadFloat(infile); |
| 2468 | static_wb->ammo_usage = cf_ReadFloat(infile); |
| 2469 | |
| 2470 | for (j = 0; j < MAX_WB_GUNPOINTS; j++) { |
| 2471 | static_wb->gp_weapon_index[j] = cf_ReadShort(infile); |
| 2472 | } |
| 2473 | |
| 2474 | for (j = 0; j < MAX_WB_FIRING_MASKS; j++) { |
| 2475 | static_wb->gp_fire_masks[j] = cf_ReadByte(infile); |
| 2476 | static_wb->gp_fire_wait[j] = cf_ReadFloat(infile); |
| 2477 | static_wb->anim_time[j] = cf_ReadFloat(infile); |
| 2478 | static_wb->anim_start_frame[j] = cf_ReadFloat(infile); |
| 2479 | static_wb->anim_fire_frame[j] = cf_ReadFloat(infile); |
| 2480 | static_wb->anim_end_frame[j] = cf_ReadFloat(infile); |
| 2481 | } |
| 2482 | static_wb->num_masks = cf_ReadByte(infile); |
| 2483 | static_wb->aiming_gp_index = cf_ReadShort(infile); |
| 2484 | static_wb->aiming_flags = cf_ReadByte(infile); |
| 2485 | static_wb->aiming_3d_dot = cf_ReadFloat(infile); |
| 2486 | static_wb->aiming_3d_dist = cf_ReadFloat(infile); |
| 2487 | static_wb->aiming_XZ_dot = cf_ReadFloat(infile); |
| 2488 | if (version >= 2) |
| 2489 | static_wb->flags = cf_ReadShort(infile); |
| 2490 | else |
| 2491 | static_wb->flags = cf_ReadByte(infile); |
| 2492 | static_wb->gp_quad_fire_mask = cf_ReadByte(infile); |
| 2493 | } |
| 2494 | // Writes a lighting chunk in from an open file |
| 2495 | void mng_WriteLightingChunk(light_info *lighting_info, CFILE *outfile) { |
| 2496 | cf_WriteFloat(outfile, lighting_info->light_distance); |
| 2497 | cf_WriteFloat(outfile, lighting_info->red_light1); |
| 2498 | cf_WriteFloat(outfile, lighting_info->green_light1); |
| 2499 | cf_WriteFloat(outfile, lighting_info->blue_light1); |
| 2500 | cf_WriteFloat(outfile, lighting_info->time_interval); |
| 2501 | cf_WriteFloat(outfile, lighting_info->flicker_distance); |
| 2502 | cf_WriteFloat(outfile, lighting_info->directional_dot); |
| 2503 | cf_WriteFloat(outfile, lighting_info->red_light2); |
| 2504 | cf_WriteFloat(outfile, lighting_info->green_light2); |
| 2505 | cf_WriteFloat(outfile, lighting_info->blue_light2); |
| 2506 | cf_WriteInt(outfile, lighting_info->flags); |
| 2507 | cf_WriteInt(outfile, lighting_info->timebits); |
| 2508 | cf_WriteByte(outfile, lighting_info->angle); |
| 2509 | cf_WriteByte(outfile, lighting_info->lighting_render_type); |
| 2510 | } |
| 2511 | // Reads a lighting chunk in from an open file |
| 2512 | void mng_ReadLightingChunk(light_info *lighting_info, CFILE *infile) { |
| 2513 | lighting_info->light_distance = cf_ReadFloat(infile); |
| 2514 | lighting_info->red_light1 = cf_ReadFloat(infile); |
| 2515 | lighting_info->green_light1 = cf_ReadFloat(infile); |
| 2516 | lighting_info->blue_light1 = cf_ReadFloat(infile); |
| 2517 | lighting_info->time_interval = cf_ReadFloat(infile); |
| 2518 | lighting_info->flicker_distance = cf_ReadFloat(infile); |
| 2519 | lighting_info->directional_dot = cf_ReadFloat(infile); |
| 2520 | lighting_info->red_light2 = cf_ReadFloat(infile); |
| 2521 | lighting_info->green_light2 = cf_ReadFloat(infile); |
| 2522 | lighting_info->blue_light2 = cf_ReadFloat(infile); |
| 2523 | lighting_info->flags = cf_ReadInt(infile); |
| 2524 | lighting_info->timebits = cf_ReadInt(infile); |
| 2525 | lighting_info->angle = cf_ReadByte(infile); |
| 2526 | lighting_info->lighting_render_type = cf_ReadByte(infile); |
| 2527 | } |
| 2528 | |
| 2529 | // Record keeping |
| 2530 | AddOnTablefile AddOnDataTables[MAX_ADDON_TABLES]; |
| 2531 | |
| 2532 | // the number of addon tables currently in memory |
| 2533 | int Num_addon_tables = 0; |
| 2534 | |
| 2535 | // if not -1, then it is the addon table we are working with |
| 2536 | int Loading_addon_table = -1; |
| 2537 | |
| 2538 | //---------------------- |
| 2539 | // Add-on data routines |
| 2540 | //---------------------- |
| 2541 | // Frees all the primitives associated with an page |
| 2542 | void mng_FreePagetypePrimitives(int pagetype, char *name, int freetype) { |
| 2543 | int n; |
| 2544 | switch (pagetype) { |
| 2545 | case PAGETYPE_TEXTURE: |
| 2546 | n = FindTextureName(name); |
| 2547 | ASSERT(n >= 0); |
| 2548 | if (GameTextures[n].flags & TF_ANIMATED) |
| 2549 | FreeVClip(GameTextures[n].bm_handle); |
| 2550 | else |
| 2551 | bm_FreeBitmap(GameTextures[n].bm_handle); |
| 2552 | if (GameTextures[n].flags & TF_DESTROYABLE && GameTextures[n].destroy_handle >= 0 && |
| 2553 | GameTextures[n].destroy_handle != n) { |
| 2554 | int oldn = n; |
| 2555 | n = GameTextures[n].destroy_handle; |
| 2556 | if (GameTextures[n].flags & TF_ANIMATED) |
| 2557 | FreeVClip(GameTextures[n].bm_handle); |
| 2558 | else |
| 2559 | bm_FreeBitmap(GameTextures[n].bm_handle); |
| 2560 | n = oldn; |
| 2561 | } |
| 2562 | if (freetype) |
| 2563 | FreeTexture(n); |
| 2564 | |
| 2565 | break; |
| 2566 | case PAGETYPE_SOUND: |
| 2567 | n = FindSoundName(name); |
| 2568 | ASSERT(n >= 0); |
| 2569 | FreeSoundFile(Sounds[n].sample_index); |
| 2570 | if (freetype) |
| 2571 | FreeSound(n); |
| 2572 | break; |
| 2573 | case PAGETYPE_WEAPON: |
| 2574 | n = FindWeaponName(name); |
| 2575 | ASSERT(n >= 0); |
| 2576 | // Free weapon images |
| 2577 | if (Weapons[n].flags & WF_HUD_ANIMATED) |
| 2578 | FreeVClip(Weapons[n].hud_image_handle); |
| 2579 | else |
| 2580 | bm_FreeBitmap(Weapons[n].hud_image_handle); |
| 2581 | if (Weapons[n].fire_image_handle != -1) { |
| 2582 | if (Weapons[n].flags & WF_IMAGE_BITMAP) |
| 2583 | bm_FreeBitmap(Weapons[n].fire_image_handle); |
| 2584 | else if (Weapons[n].flags & WF_IMAGE_VCLIP) |
| 2585 | FreeVClip(Weapons[n].fire_image_handle); |
| 2586 | else |
| 2587 | FreePolyModel(Weapons[n].fire_image_handle); |
| 2588 | } |
| 2589 | if (freetype) |
| 2590 | FreeWeapon(n); |
| 2591 | break; |
| 2592 | case PAGETYPE_SHIP: |
| 2593 | n = FindShipName(name); |
| 2594 | ASSERT(n >= 0); |
| 2595 | FreePolyModel(Ships[n].model_handle); |
| 2596 | if (Ships[n].dying_model_handle != -1) |
| 2597 | FreePolyModel(Ships[n].dying_model_handle); |
| 2598 | if (Ships[n].med_render_handle != -1) |
| 2599 | FreePolyModel(Ships[n].med_render_handle); |
| 2600 | if (Ships[n].lo_render_handle != -1) |
| 2601 | FreePolyModel(Ships[n].lo_render_handle); |
| 2602 | if (freetype) |
| 2603 | FreeShip(n); |
| 2604 | break; |
| 2605 | case PAGETYPE_GENERIC: |
| 2606 | n = FindObjectIDName(name); |
| 2607 | ASSERT(n >= 0); |
| 2608 | |
| 2609 | FreePolyModel(Object_info[n].render_handle); |
| 2610 | if (Object_info[n].med_render_handle != -1) |
| 2611 | FreePolyModel(Object_info[n].med_render_handle); |
| 2612 | if (Object_info[n].lo_render_handle != -1) |
| 2613 | FreePolyModel(Object_info[n].lo_render_handle); |
| 2614 | if (freetype) |
| 2615 | FreeObjectID(n); |
| 2616 | break; |
| 2617 | case PAGETYPE_DOOR: |
| 2618 | n = FindDoorName(name); |
| 2619 | ASSERT(n >= 0); |
| 2620 | FreePolyModel(Doors[n].model_handle); |
| 2621 | if (freetype) |
| 2622 | FreeDoor(n); |
| 2623 | break; |
| 2624 | default: |
| 2625 | break; |
| 2626 | } |
| 2627 | } |
| 2628 | // Takes our addon pages and frees/restores our data to the appropriate pages |
| 2629 | void mng_PopAddonPages() { |
| 2630 | int i, n, ok; |
| 2631 | |
| 2632 | ASSERT(Num_addon_tables > 0); |
| 2633 | if (Num_addon_tables <= 0) |
| 2634 | return; // no addon pages to pop off |
| 2635 | |
| 2636 | Num_addon_tables--; |
| 2637 | Loading_locals = 0; |
| 2638 | AddOnTablefile *addondata = &AddOnDataTables[Num_addon_tables]; |
| 2639 | |
| 2640 | for (i = 0; i < addondata->Num_addon_tracklocks; i++) { |
| 2641 | mprintf(0, "Freeing addon page %s [%s].\n", addondata->Addon_tracklocks[i].name, addondata->AddOnTableFilename); |
| 2642 | |
| 2643 | // set the Loading_addon_table to the appropriate value... |
| 2644 | // it depends on if we are overlaying from a previous tablefile |
| 2645 | // or this isn't an overlay at all |
| 2646 | // overlay = 0 (not an overlay of anything) |
| 2647 | // overlay = 1 (overlay of main tablefile) |
| 2648 | // overlay > 1 (overlay of addon table [overlay-2]) |
| 2649 | if (addondata->Addon_tracklocks[i].overlay > 1) { |
| 2650 | // this is an overlay of another table file |
| 2651 | Loading_addon_table = addondata->Addon_tracklocks[i].overlay - 2; |
| 2652 | } else { |
| 2653 | // this is an overlay of the main table file |
| 2654 | // or not an overlay at all |
| 2655 | Loading_addon_table = -1; |
| 2656 | } |
| 2657 | |
| 2658 | if (addondata->Addon_tracklocks[i].overlay > 0) { |
| 2659 | // Free this data, then read the old stuff back in |
| 2660 | mng_FreePagetypePrimitives(addondata->Addon_tracklocks[i].pagetype, addondata->Addon_tracklocks[i].name, 0); |
| 2661 | char *name = addondata->Addon_tracklocks[i].name; |
| 2662 | switch (addondata->Addon_tracklocks[i].pagetype) { |
| 2663 | case PAGETYPE_TEXTURE: { |
| 2664 | n = FindTextureName(name); |
| 2665 | ASSERT(n >= 0); |
| 2666 | ok = mng_FindSpecificTexPage(name, &texpage, addondata->Addon_tracklocks[i].stack_filepos); |
| 2667 | if (ok) { |
| 2668 | ok = mng_AssignTexPageToTexture(&texpage, n); |
| 2669 | if (!ok) |
| 2670 | Error("Error 1 restoring page %s from addon data!", name); |
| 2671 | } else |
| 2672 | Error("Error 2 restoring page %s from addon data!", name); |
| 2673 | break; |
| 2674 | } |
| 2675 | case PAGETYPE_SOUND: { |
| 2676 | n = FindSoundName(name); |
| 2677 | ASSERT(n >= 0); |
| 2678 | ok = mng_FindSpecificSoundPage(name, &soundpage, addondata->Addon_tracklocks[i].stack_filepos); |
| 2679 | if (ok) { |
| 2680 | ok = mng_AssignSoundPageToSound(&soundpage, n); |
| 2681 | if (!ok) |
| 2682 | Error("Error 1 restoring page %s from addon data!", name); |
| 2683 | } else |
| 2684 | Error("Error 2 restoring page %s from addon data!", name); |
| 2685 | break; |
| 2686 | } |
| 2687 | case PAGETYPE_DOOR: { |
| 2688 | n = FindDoorName(name); |
| 2689 | ASSERT(n >= 0); |
| 2690 | ok = mng_FindSpecificDoorPage(name, &doorpage, addondata->Addon_tracklocks[i].stack_filepos); |
| 2691 | if (ok) { |
| 2692 | ok = mng_AssignDoorPageToDoor(&doorpage, n); |
| 2693 | if (!ok) |
| 2694 | Error("Error 1 restoring page %s from addon data!", name); |
| 2695 | } else |
| 2696 | Error("Error 2 restoring page %s from addon data!", name); |
| 2697 | break; |
| 2698 | } |
| 2699 | case PAGETYPE_GENERIC: { |
| 2700 | n = FindObjectIDName(name); |
| 2701 | ASSERT(n >= 0); |
| 2702 | ok = mng_FindSpecificGenericPage(name, &genericpage, addondata->Addon_tracklocks[i].stack_filepos); |
| 2703 | if (ok) { |
| 2704 | ok = mng_AssignGenericPageToObjInfo(&genericpage, n); |
| 2705 | if (!ok) |
| 2706 | Error("Error 1 restoring page %s from addon data!", name); |
| 2707 | } else |
| 2708 | Error("Error 2 restoring page %s from addon data!", name); |
| 2709 | break; |
| 2710 | } |
| 2711 | case PAGETYPE_SHIP: { |
| 2712 | n = FindShipName(name); |
| 2713 | ASSERT(n >= 0); |
| 2714 | ok = mng_FindSpecificShipPage(name, &shippage, addondata->Addon_tracklocks[i].stack_filepos); |
| 2715 | if (ok) { |
| 2716 | ok = mng_AssignShipPageToShip(&shippage, n); |
| 2717 | if (!ok) |
| 2718 | Error("Error 1 restoring page %s from addon data!", name); |
| 2719 | } else |
| 2720 | Error("Error 2 restoring page %s from addon data!", name); |
| 2721 | break; |
| 2722 | } |
| 2723 | case PAGETYPE_WEAPON: { |
| 2724 | n = FindWeaponName(name); |
| 2725 | ASSERT(n >= 0); |
| 2726 | ok = mng_FindSpecificWeaponPage(name, &weaponpage, addondata->Addon_tracklocks[i].stack_filepos); |
| 2727 | if (ok) { |
| 2728 | ok = mng_AssignWeaponPageToWeapon(&weaponpage, n); |
| 2729 | if (!ok) |
| 2730 | Error("Error 1 restoring page %s from addon data!", name); |
| 2731 | } else |
| 2732 | Error("Error 2 restoring page %s from addon data!", name); |
| 2733 | break; |
| 2734 | } |
| 2735 | case PAGETYPE_GAMEFILE: |
| 2736 | // I don't think there's anything we need to do here |
| 2737 | break; |
| 2738 | default: |
| 2739 | Int3(); // bad type in list? Get Jason |
| 2740 | break; |
| 2741 | } |
| 2742 | |
| 2743 | } else { |
| 2744 | // Not overlay, just free this data |
| 2745 | mng_FreePagetypePrimitives(addondata->Addon_tracklocks[i].pagetype, addondata->Addon_tracklocks[i].name, 1); |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | Loading_addon_table = -1; |
| 2750 | } |
| 2751 | // Simply sets no addon data to be loaded |
| 2752 | void mng_ClearAddonTables() { |
| 2753 | int count = Num_addon_tables; |
| 2754 | while (count > 0) { |
| 2755 | ASSERT(count == Num_addon_tables); |
| 2756 | |
| 2757 | if (AddOnDataTables[count - 1].Addon_tracklocks) { |
| 2758 | mng_PopAddonPages(); |
| 2759 | mem_free(AddOnDataTables[count - 1].Addon_tracklocks); |
| 2760 | AddOnDataTables[count - 1].Addon_tracklocks = NULL; |
| 2761 | AddOnDataTables[count - 1].Num_addon_tracklocks = 0; |
| 2762 | } |
| 2763 | |
| 2764 | count--; |
| 2765 | } |
| 2766 | } |
| 2767 | // Push the given table file as an addon table file |
| 2768 | // returns true on success |
| 2769 | bool mng_SetAddonTable(char *name) { |
| 2770 | ASSERT(Num_addon_tables < MAX_ADDON_TABLES); |
| 2771 | if (Num_addon_tables >= MAX_ADDON_TABLES) |
| 2772 | return false; |
| 2773 | |
| 2774 | // make sure the table file exists! |
| 2775 | if (!cfexist(name)) |
| 2776 | return false; |
| 2777 | |
| 2778 | strcpy(AddOnDataTables[Num_addon_tables].AddOnTableFilename, name); |
| 2779 | AddOnDataTables[Num_addon_tables].Addon_tracklocks = |
| 2780 | (mngs_track_lock *)mem_malloc(MAX_ADDON_TRACKLOCKS * sizeof(mngs_track_lock)); |
| 2781 | AddOnDataTables[Num_addon_tables].Num_addon_tracklocks = 0; |
| 2782 | ASSERT(AddOnDataTables[Num_addon_tables].Addon_tracklocks); |
| 2783 | memset(AddOnDataTables[Num_addon_tables].Addon_tracklocks, 0, MAX_ADDON_TRACKLOCKS * sizeof(mngs_track_lock)); |
| 2784 | |
| 2785 | Num_addon_tables++; |
| 2786 | return true; |
| 2787 | } |
| 2788 | |
| 2789 | // Pushes an addon pack onto the stack so we can keep track of it |
| 2790 | void mng_PushAddonPage(int pagetype, char *name, int overlay) { |
| 2791 | ASSERT(Loading_addon_table >= 0 && Loading_addon_table < MAX_ADDON_TABLES); |
| 2792 | |
| 2793 | AddOnTablefile *addon = &AddOnDataTables[Loading_addon_table]; |
| 2794 | |
| 2795 | ASSERT(addon->Num_addon_tracklocks < MAX_ADDON_TRACKLOCKS); |
| 2796 | |
| 2797 | // First check to see if this is a redundant page |
| 2798 | for (int i = 0; i < addon->Num_addon_tracklocks; i++) { |
| 2799 | if (addon->Addon_tracklocks[i].used && addon->Addon_tracklocks[i].pagetype == pagetype) { |
| 2800 | if (!stricmp(addon->Addon_tracklocks[i].name, name)) { |
| 2801 | Int3(); |
| 2802 | Error("Redundant addon page '%s' loaded...", name); |
| 2803 | return; |
| 2804 | } |
| 2805 | } |
| 2806 | } |
| 2807 | mprintf(0, "Adding addon page %s [%s] to list.\n", name, addon->AddOnTableFilename); |
| 2808 | addon->Addon_tracklocks[addon->Num_addon_tracklocks].used = 1; |
| 2809 | addon->Addon_tracklocks[addon->Num_addon_tracklocks].pagetype = pagetype; |
| 2810 | addon->Addon_tracklocks[addon->Num_addon_tracklocks].overlay = overlay; |
| 2811 | addon->Addon_tracklocks[addon->Num_addon_tracklocks].stack_filepos = 0; |
| 2812 | strcpy(addon->Addon_tracklocks[addon->Num_addon_tracklocks].name, name); |
| 2813 | addon->Num_addon_tracklocks++; |
| 2814 | } |
| 2815 | |
| 2816 | // Compiles the addon pages. By looking at all the addon pages (after they have been |
| 2817 | // loaded) and does some compiling and saving of information to speed up addon page |
| 2818 | // freeing |
| 2819 | void mng_CompileAddonPages(void) { |
| 2820 | if (Num_addon_tables <= 0) |
| 2821 | return; // no addon pages to pop off |
| 2822 | |
| 2823 | int curr_tablefile, i, tf, pagetype, len, next_pos, page_pos; |
| 2824 | CFILE *file; |
| 2825 | char pagename[256]; |
| 2826 | bool found_page; |
| 2827 | |
| 2828 | for (curr_tablefile = 1; curr_tablefile <= Num_addon_tables; curr_tablefile++) { |
| 2829 | // find all the pages that are loaded from this tablefile |
| 2830 | // overlay = 0 (not from any tablefile) |
| 2831 | // overlay = 1 (from main tablefile) |
| 2832 | // overlay > 1 (from addontable[overlay-2] |
| 2833 | if (curr_tablefile == 1) { |
| 2834 | file = cfopen(TableFilename, "rb"); |
| 2835 | mprintf(0, "Compiling addon pages of %s\n", TableFilename); |
| 2836 | } else { |
| 2837 | file = cfopen(AddOnDataTables[curr_tablefile - 2].AddOnTableFilename, "rb"); |
| 2838 | mprintf(0, "Compiling addon pages of %s\n", AddOnDataTables[curr_tablefile - 2].AddOnTableFilename); |
| 2839 | } |
| 2840 | ASSERT(file != NULL); |
| 2841 | |
| 2842 | // start reading the pages from this tablefile |
| 2843 | // as we come across each page, check to see if it was |
| 2844 | // ever overlayed. |
| 2845 | while (!cfeof(file)) { |
| 2846 | // Read in a pagetype. If its a page we recognize, load it |
| 2847 | page_pos = cftell(file); |
| 2848 | pagetype = cf_ReadByte(file); |
| 2849 | len = cf_ReadInt(file); |
| 2850 | next_pos = (page_pos + 1) + len; |
| 2851 | |
| 2852 | // get the name of the page |
| 2853 | switch (pagetype) { |
| 2854 | case PAGETYPE_TEXTURE: |
| 2855 | cf_ReadShort(file); // version |
| 2856 | cf_ReadString(pagename, PAGENAME_LEN, file); |
| 2857 | break; |
| 2858 | case PAGETYPE_DOOR: |
| 2859 | cf_ReadShort(file); // version |
| 2860 | cf_ReadString(pagename, PAGENAME_LEN, file); |
| 2861 | break; |
| 2862 | case PAGETYPE_GENERIC: |
| 2863 | cf_ReadShort(file); // version |
| 2864 | cf_ReadByte(file); // type |
| 2865 | cf_ReadString(pagename, PAGENAME_LEN, file); |
| 2866 | break; |
| 2867 | case PAGETYPE_GAMEFILE: |
| 2868 | cf_ReadShort(file); // version |
| 2869 | cf_ReadString(pagename, PAGENAME_LEN, file); |
| 2870 | break; |
| 2871 | case PAGETYPE_SOUND: |
| 2872 | cf_ReadShort(file); // version |
| 2873 | cf_ReadString(pagename, PAGENAME_LEN, file); |
| 2874 | break; |
| 2875 | case PAGETYPE_SHIP: |
| 2876 | cf_ReadShort(file); // version |
| 2877 | cf_ReadString(pagename, PAGENAME_LEN, file); |
| 2878 | break; |
| 2879 | case PAGETYPE_WEAPON: |
| 2880 | cf_ReadShort(file); // version |
| 2881 | cf_ReadString(pagename, PAGENAME_LEN, file); |
| 2882 | break; |
| 2883 | case PAGETYPE_UNKNOWN: |
| 2884 | continue; |
| 2885 | break; |
| 2886 | default: |
| 2887 | Int3(); // Unrecognized pagetype, possible corrupt data following |
| 2888 | break; |
| 2889 | } |
| 2890 | |
| 2891 | // now look for all the places where this page is overlayed |
| 2892 | found_page = false; |
| 2893 | for (tf = 0; tf < Num_addon_tables; tf++) { |
| 2894 | for (i = 0; i < AddOnDataTables[tf].Num_addon_tracklocks; i++) { |
| 2895 | if (!AddOnDataTables[tf].Addon_tracklocks[i].used) |
| 2896 | continue; |
| 2897 | if (AddOnDataTables[tf].Addon_tracklocks[i].overlay != curr_tablefile) |
| 2898 | continue; |
| 2899 | if (AddOnDataTables[tf].Addon_tracklocks[i].pagetype != pagetype) |
| 2900 | continue; |
| 2901 | if (stricmp(pagename, AddOnDataTables[tf].Addon_tracklocks[i].name)) |
| 2902 | continue; |
| 2903 | |
| 2904 | // this is it! |
| 2905 | mprintf(0, "***Compiling: %s[%s] to %d\n", AddOnDataTables[tf].Addon_tracklocks[i].name, |
| 2906 | (curr_tablefile == 1) ? TableFilename : AddOnDataTables[curr_tablefile - 2].AddOnTableFilename, |
| 2907 | page_pos); |
| 2908 | ASSERT(AddOnDataTables[tf].Addon_tracklocks[i].stack_filepos == 0); |
| 2909 | AddOnDataTables[tf].Addon_tracklocks[i].stack_filepos = page_pos; |
| 2910 | found_page = true; |
| 2911 | break; |
| 2912 | } |
| 2913 | |
| 2914 | if (found_page) |
| 2915 | break; |
| 2916 | } |
| 2917 | |
| 2918 | // move on to the next page |
| 2919 | cfseek(file, next_pos, SEEK_SET); |
| 2920 | } |
| 2921 | |
| 2922 | // done with this tablefile |
| 2923 | cfclose(file); |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | // Error checking variables |
| 2928 | bool Loading_addon = false; |
| 2929 | int Data_error_count = 0; |
| 2930 | FILE *Data_error_file = NULL; |
| 2931 | char *Addon_filename; |
| 2932 | |
| 2933 | // Loads and allocs all pages found locally |
| 2934 | void mng_LoadAddonPages() { |
| 2935 | CFILE *infile; |
| 2936 | uint8_t pagetype; |
| 2937 | int len; |
| 2938 | |
| 2939 | // Set flag & Clear error count |
| 2940 | Loading_addon = true; |
| 2941 | Data_error_count = 0; |
| 2942 | |
| 2943 | if (Num_addon_tables == 0) |
| 2944 | return; // No addons to load |
| 2945 | |
| 2946 | Loading_locals = 0; |
| 2947 | |
| 2948 | int c; |
| 2949 | AddOnTablefile *addon; |
| 2950 | |
| 2951 | for (c = 0; c < Num_addon_tables; c++) { |
| 2952 | addon = &AddOnDataTables[c]; |
| 2953 | |
| 2954 | mprintf(0, "------------------------------------\nLoading addon pages for %s....\n", addon->AddOnTableFilename); |
| 2955 | Addon_filename = addon->AddOnTableFilename; |
| 2956 | infile = cfopen(addon->AddOnTableFilename, "rb"); |
| 2957 | if (!infile) { |
| 2958 | mprintf(0, "Couldn't addon table file (%s) to read pages!\n", addon->AddOnTableFilename); |
| 2959 | return; |
| 2960 | } |
| 2961 | Loading_addon_table = c; |
| 2962 | while (!cfeof(infile)) { |
| 2963 | // Read in a pagetype. If its a page we recognize, load it |
| 2964 | pagetype = cf_ReadByte(infile); |
| 2965 | len = cf_ReadInt(infile); |
| 2966 | |
| 2967 | mprintf(0, "."); |
| 2968 | switch (pagetype) { |
| 2969 | case PAGETYPE_TEXTURE: |
| 2970 | mng_LoadLocalTexturePage(infile); |
| 2971 | break; |
| 2972 | case PAGETYPE_POWERUP: |
| 2973 | case PAGETYPE_ROBOT: |
| 2974 | Error("Your local table file is invalid. You must update from the network."); |
| 2975 | break; |
| 2976 | case PAGETYPE_DOOR: |
| 2977 | mng_LoadLocalDoorPage(infile); |
| 2978 | break; |
| 2979 | case PAGETYPE_GENERIC: |
| 2980 | mng_LoadLocalGenericPage(infile); |
| 2981 | break; |
| 2982 | case PAGETYPE_GAMEFILE: |
| 2983 | mng_LoadLocalGamefilePage(infile); |
| 2984 | break; |
| 2985 | case PAGETYPE_SOUND: |
| 2986 | mng_LoadLocalSoundPage(infile); |
| 2987 | break; |
| 2988 | case PAGETYPE_SHIP: |
| 2989 | mng_LoadLocalShipPage(infile); |
| 2990 | break; |
| 2991 | case PAGETYPE_WEAPON: |
| 2992 | mng_LoadLocalWeaponPage(infile); |
| 2993 | break; |
| 2994 | case PAGETYPE_MEGACELL: |
| 2995 | mng_LoadLocalMegacellPage(infile); |
| 2996 | break; |
| 2997 | case PAGETYPE_UNKNOWN: |
| 2998 | break; |
| 2999 | default: |
| 3000 | Int3(); // Unrecognized pagetype, possible corrupt data following |
| 3001 | break; |
| 3002 | } |
| 3003 | } |
| 3004 | mprintf(0, "------------------------------------\n"); |
| 3005 | cfclose(infile); |
| 3006 | } |
| 3007 | |
| 3008 | Loading_locals = 0; |
| 3009 | Loading_addon_table = -1; |
| 3010 | |
| 3011 | mng_CompileAddonPages(); |
| 3012 | |
| 3013 | // Close error file |
| 3014 | if (Data_error_file != NULL) { |
| 3015 | fprintf(Data_error_file, "\nTotal errors: %d", Data_error_count); |
| 3016 | fclose(Data_error_file); |
| 3017 | } |
| 3018 | |
| 3019 | // Clear flag |
| 3020 | Loading_addon = false; |
| 3021 | } |
| 3022 | /* |
| 3023 | #define MAX_256s 200 |
| 3024 | int Num_256s=0; |
| 3025 | char Texture256Names[MAX_256s][80]; |
| 3026 | void Read256TextureNames () |
| 3027 | { |
| 3028 | int n=FindArg ("-File256"); |
| 3029 | if (!n) |
| 3030 | return; |
| 3031 | CFILE *infile; |
| 3032 | infile=(CFILE *)cfopen (GameArgs[n+1],"rt"); |
| 3033 | if (!infile) |
| 3034 | { |
| 3035 | mprintf(0,"Couldn't open 256 file!\n"); |
| 3036 | return; |
| 3037 | } |
| 3038 | |
| 3039 | char curline[200]; |
| 3040 | int done=0; |
| 3041 | while (!done) |
| 3042 | { |
| 3043 | if (cfeof(infile)) |
| 3044 | { |
| 3045 | done=1; |
| 3046 | continue; |
| 3047 | } |
| 3048 | |
| 3049 | // Read a line and parse it |
| 3050 | cf_ReadString (curline,200,infile); |
| 3051 | if (curline[0]==';' || curline[1]==';' || curline[0]==' ' || curline[1]==' ') |
| 3052 | continue; |
| 3053 | if (!(isalnum(curline[0]))) |
| 3054 | continue; |
| 3055 | strcpy (Texture256Names[Num_256s],curline); |
| 3056 | Num_256s++; |
| 3057 | } |
| 3058 | cfclose (infile); |
| 3059 | }*/ |
| 3060 | |
| 3061 | void DataError(const char *fmt, ...) { |
| 3062 | // Got a data error! |
| 3063 | // Int3(); |
| 3064 | |
| 3065 | // Ignore this if not loading add-on data |
| 3066 | if (!Loading_addon) |
| 3067 | return; |
| 3068 | |
| 3069 | // Increment error count |
| 3070 | Data_error_count++; |
| 3071 | |
| 3072 | // Write to file if switch specified |
| 3073 | if (FindArg("-datacheck")) { |
| 3074 | static char last_filename[_MAX_PATH]; |
| 3075 | std::va_list arglist; |
| 3076 | char buf[1024]; |
| 3077 | |
| 3078 | va_start(arglist, fmt); |
| 3079 | std::vsnprintf(buf, sizeof(buf), fmt, arglist); |
| 3080 | va_end(arglist); |
| 3081 | |
| 3082 | // Open file if not already open |
| 3083 | if (Data_error_file == NULL) { |
| 3084 | Data_error_file = fopen("datacheck.out", "wt"); |
| 3085 | |
| 3086 | if (Data_error_file == NULL) |
| 3087 | return; |
| 3088 | |
| 3089 | last_filename[0] = 0; |
| 3090 | } |
| 3091 | |
| 3092 | // If this is a new addon file, print the name |
| 3093 | if (strcmp(last_filename, Addon_filename)) { |
| 3094 | if (last_filename[0]) |
| 3095 | fprintf(Data_error_file, "\n\n"); |
| 3096 | fprintf(Data_error_file, "Errors in addon file <%s>:\n\n", Addon_filename); |
| 3097 | strcpy(last_filename, Addon_filename); |
| 3098 | } |
| 3099 | |
| 3100 | // Print the message |
| 3101 | fprintf(Data_error_file, " "); |
| 3102 | fprintf(Data_error_file, "%s", buf); |
| 3103 | } |
| 3104 | } |