| 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 | #ifndef MANAGE_H |
| 20 | #define MANAGE_H |
| 21 | |
| 22 | #include <cstdio> |
| 23 | #include <filesystem> |
| 24 | |
| 25 | #include "cfile.h" |
| 26 | #include "bitmap.h" |
| 27 | #include "manage_external.h" |
| 28 | |
| 29 | #if defined(POSIX) |
| 30 | #include "linux_fix.h" //for strnicmp,etc. |
| 31 | #endif |
| 32 | |
| 33 | #define LOCAL_TABLE "Table.loc" |
| 34 | #define TEMP_LOCAL_TABLE "Tablr.loc" |
| 35 | |
| 36 | #define NET_TABLE "Table.gam" |
| 37 | #define TEMP_NET_TABLE "Tablr.tmp" |
| 38 | |
| 39 | // Notes: Pagelocks are for keeping track of what pages are locked by which users |
| 40 | // Tracklocks are for keeping track of what pages the local user is working on or has locked |
| 41 | |
| 42 | #define PAGELOCK_NAME_LEN 30 |
| 43 | #define TABLE_NAME_LEN _MAX_PATH |
| 44 | #define INFO_STRING_LEN 100 |
| 45 | #define MAX_PAGELOCKS 1000 |
| 46 | #define MAX_TRACKLOCKS 5000 |
| 47 | |
| 48 | #define PAGETYPE_UNKNOWN 0 |
| 49 | #define PAGETYPE_TEXTURE 1 |
| 50 | #define PAGETYPE_WEAPON 2 |
| 51 | #define PAGETYPE_ROBOT 3 |
| 52 | #define PAGETYPE_POWERUP 4 |
| 53 | #define PAGETYPE_DOOR 5 |
| 54 | #define PAGETYPE_SHIP 6 |
| 55 | #define PAGETYPE_SOUND 7 |
| 56 | #define PAGETYPE_MEGACELL 8 |
| 57 | #define PAGETYPE_GAMEFILE 9 |
| 58 | #define PAGETYPE_GENERIC 10 |
| 59 | |
| 60 | struct mngs_Pagelock { |
| 61 | uint8_t pagetype; // of type PAGETYPE above |
| 62 | char name[PAGENAME_LEN]; |
| 63 | char holder[PAGENAME_LEN]; |
| 64 | }; |
| 65 | |
| 66 | struct mngs_track_lock { |
| 67 | uint8_t used; |
| 68 | uint8_t overlay; |
| 69 | uint8_t pagetype; |
| 70 | uint8_t __pad; |
| 71 | int stack_filepos; // file position of this page in the tablefile (the value we are |
| 72 | // pushing, for addon tables) |
| 73 | char name[PAGENAME_LEN]; |
| 74 | }; |
| 75 | |
| 76 | // For addon data |
| 77 | #define MAX_ADDON_TRACKLOCKS 1000 |
| 78 | #define MAX_ADDON_TABLES 2 |
| 79 | |
| 80 | struct AddOnTablefile { |
| 81 | char AddOnTableFilename[TABLE_NAME_LEN]; |
| 82 | int Num_addon_tracklocks; |
| 83 | mngs_track_lock *Addon_tracklocks; |
| 84 | }; |
| 85 | extern AddOnTablefile AddOnDataTables[MAX_ADDON_TABLES]; |
| 86 | extern int Num_addon_tables; |
| 87 | |
| 88 | // Takes our addon pages and frees/restores our data to the appropriate pages |
| 89 | void mng_PopAddonPages(); |
| 90 | |
| 91 | // Simply sets no addon data to be loaded |
| 92 | void mng_ClearAddonTables(); |
| 93 | |
| 94 | // Push the given table file as an addon table file |
| 95 | // returns true on success |
| 96 | bool mng_SetAddonTable(char *name); |
| 97 | |
| 98 | // Pushes an addon pack onto the stack so we can keep track of it |
| 99 | void mng_PushAddonPage(int pagetype, char *name, int overlay); |
| 100 | |
| 101 | // Loads and allocs all pages found locally |
| 102 | void mng_LoadAddonPages(); |
| 103 | |
| 104 | // signifies whether or not the network is up |
| 105 | extern int Network_up, Stand_alone; |
| 106 | |
| 107 | // Starting editor? |
| 108 | extern int Starting_editor, Loading_locals, Loading_addon_table; |
| 109 | |
| 110 | extern char LocalD3Dir[]; |
| 111 | extern char NetD3Dir[]; |
| 112 | extern char TableFilename[]; |
| 113 | extern std::filesystem::path TableLockFilename; |
| 114 | extern char LocalTableFilename[]; |
| 115 | extern char LocalTempTableFilename[]; |
| 116 | extern char LocalLevelsDir[]; |
| 117 | extern std::filesystem::path ManageGraphicsDir; |
| 118 | extern std::filesystem::path LocalManageGraphicsDir; |
| 119 | extern std::filesystem::path LocalModelsDir; |
| 120 | extern std::filesystem::path NetModelsDir; |
| 121 | extern std::filesystem::path LocalSoundsDir; |
| 122 | extern std::filesystem::path NetSoundsDir; |
| 123 | extern std::filesystem::path LocalRoomsDir; |
| 124 | extern std::filesystem::path NetRoomsDir; |
| 125 | extern std::filesystem::path LocalTableDir; |
| 126 | extern std::filesystem::path NetMiscDir; |
| 127 | extern std::filesystem::path LocalMiscDir; |
| 128 | extern std::filesystem::path NetMusicDir; |
| 129 | extern std::filesystem::path LocalMusicDir; |
| 130 | extern char NetScriptDir[]; |
| 131 | extern char LocalScriptDir[]; |
| 132 | extern std::filesystem::path NetArtDir; |
| 133 | extern std::filesystem::path LocalArtDir; |
| 134 | |
| 135 | extern char LocalCustomGraphicsDir[]; |
| 136 | extern char LocalCustomSoundsDir[]; |
| 137 | |
| 138 | extern char TempTableFilename[]; |
| 139 | extern char TempTableLockFilename[]; |
| 140 | extern char ErrorString[INFO_STRING_LEN]; |
| 141 | extern char InfoString[INFO_STRING_LEN]; |
| 142 | extern char TableUser[]; |
| 143 | extern std::filesystem::path LockerFile; |
| 144 | extern std::filesystem::path VersionFile; |
| 145 | extern mngs_Pagelock GlobalPagelocks[]; |
| 146 | extern mngs_track_lock GlobalTrackLocks[]; |
| 147 | |
| 148 | int mng_InitTableFiles(); |
| 149 | |
| 150 | // Loads our tables |
| 151 | int mng_LoadTableFiles(int show_progress); |
| 152 | |
| 153 | int mng_InitLocalTables(); |
| 154 | int mng_InitNetTables(); |
| 155 | |
| 156 | // Checks to see if there is a table file...if not, create one with a dummy page |
| 157 | void mng_CheckToCreateLocalTables(); |
| 158 | void mng_CheckToCreateNetTables(); |
| 159 | |
| 160 | // Creates directories if needed |
| 161 | void mng_InitLocalDirectories(); |
| 162 | void mng_InitNetDirectories(); |
| 163 | |
| 164 | void mng_ReadDummyPage(CFILE *infile, uint8_t pagetype); |
| 165 | void mng_ReadWriteDummyPage(CFILE *infile, CFILE *outfile, uint8_t pagetype); |
| 166 | |
| 167 | // Function for writing out "undefined" page...useful for placeholding |
| 168 | void mng_WriteUnknownPage(CFILE *outfile); |
| 169 | |
| 170 | // Lock functions |
| 171 | //----------------------------------------------- |
| 172 | |
| 173 | // If table lock file not found, create a dummy one |
| 174 | void mng_InitPagelocks(); |
| 175 | |
| 176 | // Writes a pagelock to an open file |
| 177 | void mng_WritePagelock(CFILE *fp, mngs_Pagelock *pl); |
| 178 | |
| 179 | // Reads a pagelock from the fp file. Returns 1 if successfully read, 0 if eof was encountered. |
| 180 | int mng_ReadPagelock(CFILE *, mngs_Pagelock *); |
| 181 | |
| 182 | // Given a page name, checks to see if it is locked. |
| 183 | // Returns 1 if locked, 0 if not. |
| 184 | int mng_CheckIfPageLocked(mngs_Pagelock *); |
| 185 | |
| 186 | // Given a page name, checks to see if it is locked by owner. |
| 187 | // Returns 1 if locked, 0 if not. |
| 188 | int mng_CheckIfPageOwned(mngs_Pagelock *, char *owner); |
| 189 | |
| 190 | // Given a pagelock, replaces the one already inside the lock file, or if not present, adds it to |
| 191 | // the lock file. Returns 0 on error, or 1 if successful. |
| 192 | int mng_ReplacePagelock(char *, mngs_Pagelock *); |
| 193 | |
| 194 | int mng_GetListOfLocks(mngs_Pagelock *pl, int max, char *who); |
| 195 | |
| 196 | // Given a name and a pagetype, deletes the one already inside the lock file |
| 197 | int mng_DeletePagelock(char *name, int pagetype); |
| 198 | |
| 199 | // Call this before any chokepoint functions are executed. |
| 200 | // Locks the whole table system for our exclusive use |
| 201 | // Returns 0 if can't lock |
| 202 | int mng_MakeLocker(); |
| 203 | |
| 204 | // Given a list of names and a pagetype, deletes the ones already inside the lock file |
| 205 | int mng_DeletePagelockSeries(char *names[], int num, int pagetype); |
| 206 | |
| 207 | // Simply erases the Lockerfile |
| 208 | void mng_EraseLocker(); |
| 209 | |
| 210 | // Open the net table file and read in all pages |
| 211 | int mng_LoadNetPages(int show_progress); |
| 212 | int mng_LoadLocalPages(); |
| 213 | |
| 214 | //--------------------------------------------------------------- |
| 215 | |
| 216 | // Clear out tracklocks |
| 217 | void mng_InitTrackLocks(); |
| 218 | |
| 219 | // Given a name, returns the index of the tracklock with that name |
| 220 | // -1 indicates that it wasn't found |
| 221 | int mng_FindTrackLock(char *name, int pagetype); |
| 222 | |
| 223 | // Searches through global array of tracklocks and returns first free one |
| 224 | // returns -1 if none free |
| 225 | int mng_AllocTrackLock(char *name, int pagetype); |
| 226 | |
| 227 | // Frees a tracklock |
| 228 | void mng_FreeTrackLock(int n); |
| 229 | |
| 230 | //---------------------------------------------------------------- |
| 231 | |
| 232 | // Displays all the locks of "name" |
| 233 | void mng_DisplayLockList(char *name); |
| 234 | |
| 235 | // Renames a page on the network |
| 236 | int mng_RenamePage(char *oldname, char *newname, int pagetype); |
| 237 | |
| 238 | // Removes a file, then renames another file to be the removed file. Get it? |
| 239 | // Returns 1 on success, else 0 on fail |
| 240 | int SwitcherooFiles(const char *name, char *tempname); |
| 241 | |
| 242 | // Returns true if the passed in pagelock is in the LockList, else false |
| 243 | bool InLockList(mngs_Pagelock *pl); |
| 244 | |
| 245 | // Takes a pagelock and sets its holder name to UNLOCKED |
| 246 | void mng_OverrideToUnlocked(mngs_Pagelock *temp_pl); |
| 247 | |
| 248 | // Returns true if the passed in primitive is old (ie needs to be updated from the network) |
| 249 | bool IsPrimitiveOld(char *name); |
| 250 | |
| 251 | // Updates a primitive if needed |
| 252 | // Localname = local version of the primname (with path) |
| 253 | // Netname = Network version of the primname (with path) |
| 254 | void UpdatePrimitive(const std::filesystem::path &localname, const std::filesystem::path &netname, char *primname, |
| 255 | int pagetype, char *pagename); |
| 256 | |
| 257 | // Writes a chunk header. Writes chunk id & placeholder length. Returns chunk start pos |
| 258 | int StartManagePage(CFILE *ofile, uint8_t pagetype); |
| 259 | |
| 260 | // Fill in page length when done writing |
| 261 | void EndManagePage(CFILE *ofile, int chunk_start_pos); |
| 262 | |
| 263 | struct physics_info; |
| 264 | struct otype_wb_info; |
| 265 | |
| 266 | // Reads a physics chunk in from the table file |
| 267 | void mng_ReadPhysicsChunk(physics_info *phys_info, CFILE *infile); |
| 268 | void mng_WritePhysicsChunk(physics_info *phys_info, CFILE *outfile); |
| 269 | |
| 270 | // Writes out weapon battery info |
| 271 | void mng_WriteWeaponBatteryChunk(otype_wb_info *static_wb, CFILE *outfile); |
| 272 | |
| 273 | // Reads in weapon battery info |
| 274 | void mng_ReadWeaponBatteryChunk(otype_wb_info *static_wb, CFILE *infile, int version); |
| 275 | |
| 276 | // Given a texture handle, searches the table file and replaces the texture with the same name |
| 277 | // If local=1, then does it to the users local copy |
| 278 | // Returns 0 on error, else 1 if all is good |
| 279 | int mng_ReplacePage(char *srcname, char *destname, int handle, int dest_pagetype, int local); |
| 280 | |
| 281 | // Given a texture name, finds it in the table file and deletes it |
| 282 | // If local is 1, deletes from the local table file |
| 283 | int mng_DeletePage(char *name, int dest_pagetype, int local); |
| 284 | |
| 285 | void mng_FreePagetypePrimitives(int pagetype, char *name, int freetype); |
| 286 | |
| 287 | extern int Old_table_method; |
| 288 | |
| 289 | // Error reporting |
| 290 | void DataError(const char *fmt, ...); |
| 291 | |
| 292 | #endif |