| 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 "stdafx.h" |
| 20 | #include "resource.h" |
| 21 | #include "TableFileEdit.h" |
| 22 | |
| 23 | #include "PsTypes.h" |
| 24 | // #include "descent.h" |
| 25 | #include "TableManage.h" |
| 26 | #include "manage.h" |
| 27 | // #include "pserror.h" |
| 28 | // #include "gametexture.h" |
| 29 | // #include "texpage.h" |
| 30 | // #include "doorpage.h" |
| 31 | // #include "soundpage.h" |
| 32 | // #include "megapage.h" |
| 33 | // #include "shippage.h" |
| 34 | // #include "weaponpage.h" |
| 35 | // #include "gamefilepage.h" |
| 36 | #include "mono.h" |
| 37 | #include "genericpage.h" |
| 38 | // #include "object.h" |
| 39 | // #include "ddio.h" |
| 40 | #include "cfile.h" |
| 41 | // #include "AppDatabase.h" |
| 42 | #include "mem.h" |
| 43 | |
| 44 | //////////////////////////////////////////////////////////////// |
| 45 | // GenericPageNode Class Members |
| 46 | //////////////////////////////////////////////////////////////// |
| 47 | GenericPageNode::GenericPageNode() { |
| 48 | prev = NULL; |
| 49 | next = NULL; |
| 50 | page_id = 0; |
| 51 | } |
| 52 | |
| 53 | GenericPageNode::~GenericPageNode() { |
| 54 | // Free up the object description memory |
| 55 | if (genericpage.objinfo_struct.description != NULL) { |
| 56 | mem_free(genericpage.objinfo_struct.description); |
| 57 | genericpage.objinfo_struct.description = NULL; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bool GenericPageNode::operator<(const GenericPageNode &node) { |
| 62 | return (stricmp(genericpage.objinfo_struct.name, node.genericpage.objinfo_struct.name) < 0); |
| 63 | } |
| 64 | |
| 65 | //////////////////////////////////////////////////////////////// |
| 66 | // GenericPageList Class Members |
| 67 | //////////////////////////////////////////////////////////////// |
| 68 | GenericPageList::GenericPageList() { |
| 69 | m_head = NULL; |
| 70 | m_tail = NULL; |
| 71 | m_size = 0; |
| 72 | m_TableFilename = ""; |
| 73 | m_TableLoaded = FALSE; |
| 74 | m_TableModified = FALSE; |
| 75 | m_SelectedNode = NULL; |
| 76 | m_SelectedIndex = 0; |
| 77 | } |
| 78 | |
| 79 | GenericPageList::~GenericPageList() { ClearList(); } |
| 80 | |
| 81 | void GenericPageList::ClearList(void) { |
| 82 | GenericPageNode *temp, *node; |
| 83 | |
| 84 | node = m_head; |
| 85 | while (node != NULL) { |
| 86 | temp = node; |
| 87 | node = node->next; |
| 88 | delete temp; |
| 89 | } |
| 90 | m_size = 0; |
| 91 | m_head = NULL; |
| 92 | m_tail = NULL; |
| 93 | m_TableFilename = ""; |
| 94 | m_TableLoaded = FALSE; |
| 95 | m_TableModified = FALSE; |
| 96 | m_SelectedNode = NULL; |
| 97 | m_SelectedIndex = 0; |
| 98 | } |
| 99 | |
| 100 | // Adds the node to the list in ascending alphabetical order |
| 101 | // according to the page names of the nodes |
| 102 | bool GenericPageList::AddToList(GenericPageNode *new_node) { |
| 103 | GenericPageNode *node; |
| 104 | |
| 105 | if (new_node == NULL) |
| 106 | return FALSE; |
| 107 | |
| 108 | // Handle empty list case |
| 109 | if (m_head == NULL) { |
| 110 | m_head = new_node; |
| 111 | m_tail = new_node; |
| 112 | new_node->next = NULL; |
| 113 | new_node->prev = NULL; |
| 114 | |
| 115 | m_size++; |
| 116 | return TRUE; |
| 117 | } |
| 118 | |
| 119 | // Handle (new node < list head) case |
| 120 | if ((*new_node) < (*m_head)) { |
| 121 | new_node->next = m_head; |
| 122 | new_node->prev = NULL; |
| 123 | m_head->prev = new_node; |
| 124 | m_head = new_node; |
| 125 | |
| 126 | m_size++; |
| 127 | return TRUE; |
| 128 | } |
| 129 | |
| 130 | // Search through list until end of list or until the new node is less |
| 131 | // than the next node, then add the new node after the current node |
| 132 | node = m_head; |
| 133 | while (node->next != NULL && (*(node->next)) < (*new_node)) |
| 134 | node = node->next; |
| 135 | |
| 136 | new_node->next = node->next; |
| 137 | new_node->prev = node; |
| 138 | |
| 139 | if (node->next == NULL) |
| 140 | m_tail = new_node; |
| 141 | else |
| 142 | node->next->prev = new_node; |
| 143 | |
| 144 | node->next = new_node; |
| 145 | |
| 146 | m_size++; |
| 147 | |
| 148 | return TRUE; |
| 149 | } |
| 150 | |
| 151 | bool GenericPageList::LoadTable(char *table_filename) { |
| 152 | CFILE *infile; |
| 153 | uint8_t pagetype; |
| 154 | int done; |
| 155 | uint32_t page_id; |
| 156 | |
| 157 | GenericPageNode *generic_page_node; |
| 158 | |
| 159 | infile = cfopen(table_filename, "rb"); |
| 160 | |
| 161 | if (!infile) { |
| 162 | // mprintf(0,"Couldn't open table file to reorder!\n"); |
| 163 | // Int3(); |
| 164 | return FALSE; |
| 165 | } |
| 166 | |
| 167 | CWaitCursor wc; |
| 168 | |
| 169 | // Wipeout the list first |
| 170 | ClearList(); |
| 171 | |
| 172 | done = 0; |
| 173 | page_id = 0; |
| 174 | |
| 175 | while (!done) { |
| 176 | if (cfeof(infile)) { |
| 177 | done = 1; |
| 178 | continue; |
| 179 | } |
| 180 | pagetype = cf_ReadByte(infile); |
| 181 | |
| 182 | // If not a generic page, just read it in and ignore it |
| 183 | if (pagetype != PAGETYPE_GENERIC) { |
| 184 | mng_ReadDummyPage(infile, pagetype); |
| 185 | page_id++; |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | generic_page_node = new GenericPageNode; |
| 190 | if (generic_page_node == NULL) { |
| 191 | return FALSE; |
| 192 | } |
| 193 | mng_ReadNewGenericPage(infile, &(generic_page_node->genericpage)); |
| 194 | generic_page_node->page_id = page_id; |
| 195 | AddToList(generic_page_node); |
| 196 | |
| 197 | page_id++; |
| 198 | } |
| 199 | |
| 200 | cfclose(infile); |
| 201 | |
| 202 | // Set Loaded tabel data |
| 203 | m_TableFilename = table_filename; |
| 204 | m_TableLoaded = TRUE; |
| 205 | |
| 206 | m_TableModified = FALSE; |
| 207 | |
| 208 | return TRUE; |
| 209 | } |
| 210 | |
| 211 | bool GenericPageList::SaveTable(char *table_filename) { |
| 212 | CFILE *infile, *outfile; |
| 213 | uint8_t pagetype, replaced = 0; |
| 214 | int done = 0; |
| 215 | uint32_t page_id; |
| 216 | GenericPageNode *new_generic_page; |
| 217 | mngs_generic_page genericpage; |
| 218 | |
| 219 | CWaitCursor wc; |
| 220 | |
| 221 | // If no table is loaded, get outta here |
| 222 | if (!m_TableLoaded) |
| 223 | return FALSE; |
| 224 | |
| 225 | // First make sure we can open the table file and the temp table file |
| 226 | infile = cfopen(m_TableFilename.GetBuffer(0), "rb"); |
| 227 | if (!infile) { |
| 228 | mprintf(0, "Couldn't open table file to replace generic!\n"); |
| 229 | return FALSE; |
| 230 | } |
| 231 | |
| 232 | outfile = cfopen(TEMP_TABLE_FILENAME, "wb"); |
| 233 | if (!outfile) { |
| 234 | mprintf(0, "Couldn't open temp table file to replace generic!\n"); |
| 235 | cfclose(infile); |
| 236 | return FALSE; |
| 237 | } |
| 238 | |
| 239 | // Clear out the generic page |
| 240 | memset(&genericpage, 0, sizeof(mngs_generic_page)); |
| 241 | |
| 242 | // Read through the entire pagefile and replace the page we want replaced |
| 243 | // If we can't find the one we want, we simply append it to the end of the file |
| 244 | page_id = 0; |
| 245 | while (!done) { |
| 246 | if (cfeof(infile)) { |
| 247 | done = 1; |
| 248 | continue; |
| 249 | } |
| 250 | pagetype = cf_ReadByte(infile); |
| 251 | |
| 252 | // If not a generic page, just read it in and write it right back out |
| 253 | if (pagetype != PAGETYPE_GENERIC) { |
| 254 | mng_ReadWriteDummyPage(infile, outfile, pagetype); |
| 255 | page_id++; |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | mng_ReadNewGenericPage(infile, &genericpage); |
| 260 | |
| 261 | // Look for the generic page with the current page position in list |
| 262 | new_generic_page = FindNode(page_id); |
| 263 | if (new_generic_page == NULL) { |
| 264 | // There is a page incompatibility!!!! |
| 265 | // so, just write out the one that was read instead |
| 266 | mng_WriteNewGenericPage(outfile, &genericpage); |
| 267 | OutrageMessageBox("File incompatibility Error!", "Error!"); |
| 268 | } else { |
| 269 | // Found the page for this position in the list, |
| 270 | // So write out the new generic page |
| 271 | mng_WriteNewGenericPage(outfile, &(new_generic_page->genericpage)); |
| 272 | } |
| 273 | |
| 274 | // Free up the description memory for the temp page |
| 275 | if (genericpage.objinfo_struct.description != NULL) { |
| 276 | mem_free(genericpage.objinfo_struct.description); |
| 277 | genericpage.objinfo_struct.description = NULL; |
| 278 | } |
| 279 | |
| 280 | page_id++; |
| 281 | } |
| 282 | |
| 283 | cfclose(infile); |
| 284 | cfclose(outfile); |
| 285 | |
| 286 | if (table_filename == NULL) |
| 287 | // if no filename specified, just save it to the currently loaded file |
| 288 | table_filename = m_TableFilename.GetBuffer(0); |
| 289 | else { |
| 290 | // We need to create this one just so it can get deleted alright by |
| 291 | // Jason's Switcheroo function |
| 292 | outfile = cfopen(table_filename, "wb"); |
| 293 | if (!outfile) |
| 294 | return FALSE; |
| 295 | cfclose(outfile); |
| 296 | } |
| 297 | |
| 298 | // Now, perform the switcheroo by deleting the real table file and renaming the |
| 299 | // temp table file to be the new "real" table file. |
| 300 | if (!SwitcherooFiles(table_filename, TEMP_TABLE_FILENAME)) |
| 301 | return FALSE; |
| 302 | |
| 303 | m_TableModified = FALSE; |
| 304 | m_TableFilename = table_filename; |
| 305 | |
| 306 | return TRUE; // successful! |
| 307 | } |
| 308 | |
| 309 | bool GenericPageList::FillFromList(CComboBox *list) { |
| 310 | GenericPageNode *node; |
| 311 | |
| 312 | list->ResetContent(); |
| 313 | |
| 314 | node = m_head; |
| 315 | while (node != NULL) { |
| 316 | list->InsertString(-1, node->genericpage.objinfo_struct.name); |
| 317 | node = node->next; |
| 318 | } |
| 319 | |
| 320 | // Select the start of the list |
| 321 | SelectNode(0); |
| 322 | |
| 323 | return TRUE; |
| 324 | } |
| 325 | |
| 326 | void GenericPageList::SelectNext(void) { |
| 327 | if (m_SelectedNode == NULL) |
| 328 | return; |
| 329 | |
| 330 | if (m_SelectedNode->next == NULL) { |
| 331 | m_SelectedNode = m_head; |
| 332 | m_SelectedIndex = 0; |
| 333 | } else { |
| 334 | m_SelectedNode = m_SelectedNode->next; |
| 335 | m_SelectedIndex++; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void GenericPageList::SelectPrev(void) { |
| 340 | if (m_SelectedNode == NULL) |
| 341 | return; |
| 342 | |
| 343 | if (m_SelectedNode->prev == NULL) { |
| 344 | m_SelectedNode = m_tail; |
| 345 | m_SelectedIndex = (m_size - 1); |
| 346 | } else { |
| 347 | m_SelectedNode = m_SelectedNode->prev; |
| 348 | m_SelectedIndex--; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void GenericPageList::SelectNode(uint32_t list_index) { |
| 353 | uint32_t j; |
| 354 | GenericPageNode *node; |
| 355 | |
| 356 | if (list_index < 0 || list_index > (m_size - 1)) |
| 357 | return; |
| 358 | |
| 359 | node = m_head; |
| 360 | for (j = 0; j < list_index; j++) |
| 361 | if (node != NULL) |
| 362 | node = node->next; |
| 363 | |
| 364 | m_SelectedNode = node; |
| 365 | m_SelectedIndex = list_index; |
| 366 | } |
| 367 | |
| 368 | void GenericPageList::DisplaySelected(CComboBox *list, CEdit *description) { |
| 369 | char *text; |
| 370 | |
| 371 | if (m_SelectedNode == NULL) |
| 372 | return; |
| 373 | |
| 374 | list->SetCurSel(m_SelectedIndex); |
| 375 | |
| 376 | text = m_SelectedNode->genericpage.objinfo_struct.description; |
| 377 | if (text == NULL) |
| 378 | description->SetWindowText(NO_DESCRIPTION_STRING); |
| 379 | else |
| 380 | description->SetWindowText(text); |
| 381 | } |
| 382 | |
| 383 | void GenericPageList::SaveSelected(CEdit *description) { |
| 384 | CString new_descrip; |
| 385 | char *new_text; |
| 386 | char *old_text; |
| 387 | |
| 388 | if (m_SelectedNode == NULL) |
| 389 | return; |
| 390 | |
| 391 | // Get pointer to old text |
| 392 | old_text = m_SelectedNode->genericpage.objinfo_struct.description; |
| 393 | |
| 394 | // Get Copy of new Description text |
| 395 | description->GetWindowText(new_descrip); |
| 396 | new_text = (char *)mem_malloc(strlen(new_descrip.GetBuffer(0)) + 1); |
| 397 | ASSERT(new_text); // out of memory! |
| 398 | sprintf(new_text, "%s", new_descrip.GetBuffer(0)); |
| 399 | |
| 400 | // Check if new description is empty, if it is, make it NULL |
| 401 | if (strlen(new_text) == 0 || stricmp(new_text, NO_DESCRIPTION_STRING) == 0) { |
| 402 | mem_free(new_text); |
| 403 | new_text = NULL; |
| 404 | } |
| 405 | |
| 406 | // If both new and old are empty, get outta here |
| 407 | if (old_text == NULL && new_text == NULL) { |
| 408 | mem_free(new_text); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | // If both have descriptions and they match, get outta here |
| 413 | if (old_text != NULL && new_text != NULL) { |
| 414 | if (strcmp(old_text, new_text) == 0) { |
| 415 | mem_free(new_text); |
| 416 | return; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // Free up the old text memory, and assign it to the new text |
| 421 | if (old_text != NULL) |
| 422 | mem_free(old_text); |
| 423 | |
| 424 | m_SelectedNode->genericpage.objinfo_struct.description = new_text; |
| 425 | m_TableModified = TRUE; |
| 426 | |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | GenericPageNode *GenericPageList::FindNode(uint32_t page_id) { |
| 431 | GenericPageNode *node, *found_node; |
| 432 | |
| 433 | found_node = NULL; |
| 434 | node = m_head; |
| 435 | while (node != NULL) { |
| 436 | if (node->page_id == page_id) { |
| 437 | found_node = node; |
| 438 | break; |
| 439 | } |
| 440 | node = node->next; |
| 441 | } |
| 442 | |
| 443 | return (found_node); |
| 444 | } |
| 445 | |
| 446 | int GenericPageList::ModifiedPrompt(CDialog *wnd) { |
| 447 | // If current file has been modified, check with user first... |
| 448 | if (!m_TableModified) |
| 449 | return (IDYES); |
| 450 | if (wnd->MessageBox("Changes to table file will be lost. Proceed?", "Table File Has Been Modified", MB_YESNO) == |
| 451 | IDYES) |
| 452 | return (IDYES); |
| 453 | return (IDNO); |
| 454 | } |
| 455 | |
| 456 | void GenericPageList::SetTitleString(CDialog *wnd) { |
| 457 | char title[1024]; |
| 458 | char loaded_file[1024]; |
| 459 | |
| 460 | if (m_TableLoaded) |
| 461 | sprintf(loaded_file, " - [%s%s]", m_TableFilename.GetBuffer(0), (m_TableModified) ? "*" : ""); |
| 462 | else |
| 463 | sprintf(loaded_file, ""); |
| 464 | |
| 465 | sprintf(title, "%s%s", TITLE_NAME, loaded_file); |
| 466 | wnd->SetWindowText(title); |
| 467 | } |