| 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 | |
| 19 | #include <cstring> |
| 20 | #include <filesystem> |
| 21 | |
| 22 | #include "cfile.h" |
| 23 | #include "manage.h" |
| 24 | #include "mono.h" |
| 25 | #include "pserror.h" |
| 26 | #include "ddio.h" |
| 27 | #include "gamefile.h" |
| 28 | #include "gamefilepage.h" |
| 29 | #include "args.h" |
| 30 | |
| 31 | // gamefilepage commands that are read/written |
| 32 | // A command is followed by a byte count describing how many bytes |
| 33 | // are in the data for the command |
| 34 | #define GAMEFILEPAGE_COMMAND_NAME 1 |
| 35 | #define GAMEFILEPAGE_COMMAND_DIR_NAME 2 |
| 36 | #define GAMEFILEPAGE_COMMAND_END 3 |
| 37 | |
| 38 | #define GAMEFILE_VERSION 2 |
| 39 | |
| 40 | extern char *TablefileNameOverride; |
| 41 | |
| 42 | // Given an open file pointer and a gamefile_page struct, writes that gamefile page out |
| 43 | void mng_WriteGamefilePage(CFILE *outfile, mngs_gamefile_page *gamefilepage) { |
| 44 | ASSERT(outfile != NULL); |
| 45 | ASSERT(gamefilepage != NULL); |
| 46 | |
| 47 | cf_WriteByte(outfile, PAGETYPE_GAMEFILE); |
| 48 | |
| 49 | cf_WriteByte(outfile, GAMEFILEPAGE_COMMAND_NAME); // write out gamefile name |
| 50 | cf_WriteByte(outfile, strlen(gamefilepage->gamefile_struct.name) + 1); |
| 51 | cf_WriteString(outfile, gamefilepage->gamefile_struct.name); |
| 52 | |
| 53 | cf_WriteByte(outfile, GAMEFILEPAGE_COMMAND_DIR_NAME); // write out gamefile name |
| 54 | cf_WriteByte(outfile, strlen(gamefilepage->gamefile_struct.dir_name) + 1); |
| 55 | cf_WriteString(outfile, gamefilepage->gamefile_struct.dir_name); |
| 56 | |
| 57 | cf_WriteByte(outfile, GAMEFILEPAGE_COMMAND_END); // we're all done |
| 58 | cf_WriteByte(outfile, 0); |
| 59 | } |
| 60 | // Given an open file pointer and a gamefile_page struct, writes that gamefile page out |
| 61 | void mng_WriteNewGamefilePage(CFILE *outfile, mngs_gamefile_page *gamefilepage) { |
| 62 | ASSERT(outfile != NULL); |
| 63 | ASSERT(gamefilepage != NULL); |
| 64 | |
| 65 | int offset = StartManagePage(outfile, PAGETYPE_GAMEFILE); |
| 66 | |
| 67 | cf_WriteShort(outfile, GAMEFILE_VERSION); |
| 68 | |
| 69 | cf_WriteString(outfile, gamefilepage->gamefile_struct.name); |
| 70 | cf_WriteString(outfile, gamefilepage->gamefile_struct.dir_name); |
| 71 | |
| 72 | EndManagePage(outfile, offset); |
| 73 | } |
| 74 | |
| 75 | // Reads a gamefile page from an open file. Returns 0 on error. |
| 76 | int mng_ReadNewGamefilePage(CFILE *infile, mngs_gamefile_page *gamefilepage) { |
| 77 | ASSERT(infile != NULL); |
| 78 | memset(gamefilepage, 0, sizeof(mngs_gamefile_page)); |
| 79 | |
| 80 | int version = cf_ReadShort(infile); |
| 81 | |
| 82 | cf_ReadString(gamefilepage->gamefile_struct.name, PAGENAME_LEN, infile); |
| 83 | cf_ReadString(gamefilepage->gamefile_struct.dir_name, PAGENAME_LEN, infile); |
| 84 | |
| 85 | // This is a valid new page |
| 86 | gamefilepage->gamefile_struct.used = 1; |
| 87 | |
| 88 | return 1; // successfully read |
| 89 | } |
| 90 | |
| 91 | // Reads a gamefile page from an open file. Returns 0 on error. |
| 92 | int mng_ReadGamefilePage(CFILE *infile, mngs_gamefile_page *gamefilepage) { |
| 93 | int done = 0; |
| 94 | char command; |
| 95 | uint16_t len; |
| 96 | int i; |
| 97 | |
| 98 | if (!Old_table_method) |
| 99 | return mng_ReadNewGamefilePage(infile, gamefilepage); |
| 100 | |
| 101 | ASSERT(infile != NULL); |
| 102 | memset(gamefilepage, 0, sizeof(mngs_gamefile_page)); |
| 103 | |
| 104 | while (!done) { |
| 105 | // Read in command byte then read in the length of that commands data |
| 106 | |
| 107 | command = cf_ReadByte(infile); |
| 108 | len = cf_ReadByte(infile); |
| 109 | |
| 110 | switch (command) { |
| 111 | case GAMEFILEPAGE_COMMAND_END: |
| 112 | done = 1; |
| 113 | break; |
| 114 | case GAMEFILEPAGE_COMMAND_NAME: // the name of the gamefile model |
| 115 | cf_ReadString(gamefilepage->gamefile_struct.name, len + 1, infile); |
| 116 | break; |
| 117 | case GAMEFILEPAGE_COMMAND_DIR_NAME: |
| 118 | cf_ReadString(gamefilepage->gamefile_struct.dir_name, len + 1, infile); |
| 119 | break; |
| 120 | default: |
| 121 | // Ignore the ones we don't know |
| 122 | for (i = 0; i < len; i++) |
| 123 | cf_ReadByte(infile); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // This is a valid new page |
| 129 | gamefilepage->gamefile_struct.used = 1; |
| 130 | |
| 131 | return 1; // successfully read |
| 132 | } |
| 133 | |
| 134 | // Reads in the gamefile named "name" into gamefilepage struct |
| 135 | // Returns 0 on error or couldn't find, else 1 if all is good |
| 136 | int mng_FindSpecificGamefilePage(char *name, mngs_gamefile_page *gamefilepage, int offset) { |
| 137 | CFILE *infile; |
| 138 | uint8_t pagetype; |
| 139 | int done = 0, found = 0; |
| 140 | std::filesystem::path tablename; |
| 141 | |
| 142 | if (Loading_locals) { |
| 143 | infile = cfopen(LocalTableFilename, "rb"); |
| 144 | } else if (Loading_addon_table != -1) { |
| 145 | infile = cfopen(AddOnDataTables[Loading_addon_table].AddOnTableFilename, "rb"); |
| 146 | } else { |
| 147 | if (Network_up && Starting_editor) { |
| 148 | int farg = FindArg("-filter"); |
| 149 | |
| 150 | if (farg) |
| 151 | tablename = GameArgs[farg + 1]; |
| 152 | else |
| 153 | tablename = LocalTableDir / NET_TABLE; |
| 154 | |
| 155 | infile = cfopen(tablename, "rb"); |
| 156 | } else { |
| 157 | infile = NULL; |
| 158 | if (TablefileNameOverride) { |
| 159 | infile = cfopen(TablefileNameOverride, "rb"); |
| 160 | } |
| 161 | |
| 162 | if (!infile) |
| 163 | infile = cfopen(TableFilename, "rb"); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (!infile) |
| 168 | Error("Cannot open table file."); |
| 169 | |
| 170 | // Read in the entire page file until we find the page we want |
| 171 | while (!done) { |
| 172 | if (cfeof(infile)) { |
| 173 | done = 1; |
| 174 | continue; |
| 175 | } |
| 176 | pagetype = cf_ReadByte(infile); |
| 177 | int len = cf_ReadInt(infile); |
| 178 | |
| 179 | // If not a gamefile page, just read it in and ignore it |
| 180 | if (pagetype != PAGETYPE_GAMEFILE) { |
| 181 | cfseek(infile, len - 4, SEEK_CUR); |
| 182 | continue; |
| 183 | } |
| 184 | |
| 185 | mng_ReadNewGamefilePage(infile, gamefilepage); |
| 186 | |
| 187 | if (!stricmp(name, gamefilepage->gamefile_struct.name)) { |
| 188 | // This is the page we want |
| 189 | found = 1; |
| 190 | done = 1; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | cfclose(infile); |
| 195 | |
| 196 | return found; // successful! |
| 197 | } |
| 198 | |
| 199 | // Given a gamefile page, allocs a gamefile and calls AssignGamefilePageToGamefile to actually |
| 200 | // load models and values. Rturns gamefile handle on success, -1 if fail |
| 201 | int mng_SetAndLoadGamefile(mngs_gamefile_page *gamefilepage) { |
| 202 | int n; |
| 203 | |
| 204 | n = AllocGamefile(); |
| 205 | if (n < 0) |
| 206 | return -1; |
| 207 | if (!mng_AssignGamefilePageToGamefile(gamefilepage, n)) |
| 208 | return -1; |
| 209 | |
| 210 | return n; |
| 211 | } |
| 212 | |
| 213 | // Given a gamefilepage and a gamefile handle, attempts to make gamefile n correspond to |
| 214 | // to the gamefilepage. |
| 215 | // Returns 1 on success, 0 otherwise |
| 216 | int mng_AssignGamefilePageToGamefile(mngs_gamefile_page *gamefilepage, int n) { |
| 217 | gamefile *gamefilepointer = &Gamefiles[n]; |
| 218 | |
| 219 | // copy our values |
| 220 | memcpy(gamefilepointer, &gamefilepage->gamefile_struct, sizeof(gamefile)); |
| 221 | strcpy(gamefilepointer->name, gamefilepage->gamefile_struct.name); |
| 222 | strcpy(gamefilepointer->dir_name, gamefilepage->gamefile_struct.dir_name); |
| 223 | |
| 224 | // First see if our image differs from the one on the net |
| 225 | // If it is, make a copy |
| 226 | // If its a release version, don't do any of this |
| 227 | |
| 228 | #ifndef RELEASE |
| 229 | if (Network_up) { |
| 230 | std::filesystem::path str = |
| 231 | std::filesystem::path(LocalD3Dir) / "data" / gamefilepointer->dir_name / gamefilepointer->name; |
| 232 | std::filesystem::path netstr = |
| 233 | std::filesystem::path(NetD3Dir) / "data" / gamefilepointer->dir_name / gamefilepointer->name; |
| 234 | |
| 235 | UpdatePrimitive(str, netstr, gamefilepointer->name, PAGETYPE_GAMEFILE, gamefilepointer->name); |
| 236 | |
| 237 | if (!Starting_editor) { |
| 238 | if (cf_Diff(str, netstr)) { |
| 239 | ASSERT(1); // Get Jason Immediately...versions between local and net don't match! |
| 240 | |
| 241 | // Do it again so we can trace to see what is wrong |
| 242 | UpdatePrimitive(str, netstr, gamefilepointer->name, PAGETYPE_GAMEFILE, gamefilepointer->name); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | // Copies values from a gamefile into a gamefile_page |
| 252 | void mng_AssignGamefileToGamefilePage(int n, mngs_gamefile_page *gamefilepage) { |
| 253 | gamefile *gamefilepointer = &Gamefiles[n]; |
| 254 | |
| 255 | // Assign the values |
| 256 | memcpy(&gamefilepage->gamefile_struct, gamefilepointer, sizeof(gamefile)); |
| 257 | |
| 258 | strcpy(gamefilepage->gamefile_struct.name, gamefilepointer->name); |
| 259 | strcpy(gamefilepage->gamefile_struct.dir_name, gamefilepointer->dir_name); |
| 260 | } |
| 261 | |
| 262 | // Loads a gamefile found in the net table file. It then allocs a gamefile and |
| 263 | // then calls SetAndLoadGamefile to actually load in any images/models associated |
| 264 | // with it |
| 265 | void mng_LoadNetGamefilePage(CFILE *infile, bool overlay) { |
| 266 | mngs_gamefile_page gamefilepage; |
| 267 | memset(&gamefilepage, 0, sizeof(mngs_gamefile_page)); |
| 268 | |
| 269 | if (mng_ReadNewGamefilePage(infile, &gamefilepage)) { |
| 270 | int n = FindGamefileName(gamefilepage.gamefile_struct.name); |
| 271 | if (n != -1) { |
| 272 | if (overlay) { |
| 273 | mprintf(0, "OVERLAYING GAMEFILE %s\n", gamefilepage.gamefile_struct.name); |
| 274 | mng_FreePagetypePrimitives(PAGETYPE_GAMEFILE, gamefilepage.gamefile_struct.name, 0); |
| 275 | mng_AssignGamefilePageToGamefile(&gamefilepage, n); |
| 276 | } |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | int ret = mng_SetAndLoadGamefile(&gamefilepage); |
| 281 | ASSERT(ret >= 0); |
| 282 | } else |
| 283 | mprintf(0, "Could not load gamefilepage named %s!\n", gamefilepage.gamefile_struct.name); |
| 284 | } |
| 285 | |
| 286 | // Reads a gamefile page from a local table file. It then allocs a gamefile and |
| 287 | // loads any images/models associated with that gamefile |
| 288 | void mng_LoadLocalGamefilePage(CFILE *infile) { |
| 289 | mngs_gamefile_page gamefilepage; |
| 290 | int ok = 0; |
| 291 | memset(&gamefilepage, 0, sizeof(mngs_gamefile_page)); |
| 292 | |
| 293 | if (mng_ReadNewGamefilePage(infile, &gamefilepage)) { |
| 294 | // Check to see if this is a local copy that is supposed |
| 295 | // to go over a network copy (supersede the net copy) |
| 296 | |
| 297 | int i = FindGamefileName(gamefilepage.gamefile_struct.name); |
| 298 | if (i != -1) { |
| 299 | // Make sure we really have this page checked out |
| 300 | mngs_Pagelock pl; |
| 301 | |
| 302 | strcpy(pl.name, gamefilepage.gamefile_struct.name); |
| 303 | pl.pagetype = PAGETYPE_GAMEFILE; |
| 304 | |
| 305 | /*if (Network_up && Stand_alone==0) |
| 306 | { |
| 307 | int locked=mng_CheckIfPageOwned(&pl,TableUser); |
| 308 | if (locked!=1) |
| 309 | Int3(); // Your local vs net copies of the lock file do not match |
| 310 | }*/ |
| 311 | |
| 312 | ok = 1; |
| 313 | bool need_to_load_page = true; |
| 314 | |
| 315 | if (Loading_addon_table != -1) { |
| 316 | AddOnTablefile *addon; |
| 317 | int tidx; |
| 318 | |
| 319 | // see if we really need to load this page |
| 320 | // check to see if we already have loaded this page (because it was |
| 321 | // a dependancy of another) |
| 322 | addon = &AddOnDataTables[Loading_addon_table]; |
| 323 | for (tidx = 0; tidx < addon->Num_addon_tracklocks; tidx++) { |
| 324 | if (addon->Addon_tracklocks[tidx].pagetype == PAGETYPE_GAMEFILE && |
| 325 | !stricmp(addon->Addon_tracklocks[tidx].name, gamefilepage.gamefile_struct.name)) { |
| 326 | // found it!! |
| 327 | mprintf(0, "GamefilePage: %s previously loaded\n", gamefilepage.gamefile_struct.name); |
| 328 | need_to_load_page = false; |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | if (need_to_load_page) { |
| 335 | mng_FreePagetypePrimitives(PAGETYPE_GAMEFILE, gamefilepage.gamefile_struct.name, 0); |
| 336 | mng_AssignGamefilePageToGamefile(&gamefilepage, i); |
| 337 | |
| 338 | // For addon data |
| 339 | if (Loading_addon_table != -1) { |
| 340 | // this is an overlay of some sort..see which we are overlaying |
| 341 | int overlay = 1; |
| 342 | int addidx, tidx; |
| 343 | bool found = false; |
| 344 | for (addidx = Num_addon_tables - 1; addidx >= 0; addidx--) { |
| 345 | if (addidx == Loading_addon_table) |
| 346 | continue; |
| 347 | AddOnTablefile *addon = &AddOnDataTables[addidx]; |
| 348 | |
| 349 | // look for the page in this table file |
| 350 | for (tidx = 0; tidx < addon->Num_addon_tracklocks; tidx++) { |
| 351 | if (addon->Addon_tracklocks[tidx].pagetype == PAGETYPE_GAMEFILE && |
| 352 | !stricmp(addon->Addon_tracklocks[tidx].name, gamefilepage.gamefile_struct.name)) { |
| 353 | // found it!! |
| 354 | found = true; |
| 355 | overlay = addidx + 2; |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if (found) |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | mng_PushAddonPage(PAGETYPE_GAMEFILE, gamefilepage.gamefile_struct.name, overlay); |
| 365 | } |
| 366 | } |
| 367 | } else { |
| 368 | // This is a local gamefile that has never been checked in |
| 369 | if (mng_SetAndLoadGamefile(&gamefilepage) < 0) |
| 370 | ok = 0; |
| 371 | else |
| 372 | ok = 1; |
| 373 | |
| 374 | // For addon data |
| 375 | if (ok && Loading_addon_table != -1) |
| 376 | mng_PushAddonPage(PAGETYPE_GAMEFILE, gamefilepage.gamefile_struct.name, 0); |
| 377 | } |
| 378 | |
| 379 | ASSERT(ok == 1); // There was problem loading a page! |
| 380 | |
| 381 | if (Loading_addon_table == -1) |
| 382 | mng_AllocTrackLock(gamefilepage.gamefile_struct.name, PAGETYPE_GAMEFILE); |
| 383 | |
| 384 | } else |
| 385 | mprintf(0, "Could not load gamefilepage named %s!\n", gamefilepage.gamefile_struct.name); |
| 386 | } |