From 876daf0827f1ad8b8f2b5229cab7e278a821e315 Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Mon, 7 Oct 2013 00:39:06 -0700 Subject: [PATCH] Remove padding and unused data from savegame format. --- sc2/src/uqm/load.c | 95 +++------------------------------- sc2/src/uqm/save.c | 126 ++++----------------------------------------- 2 files changed, 17 insertions(+), 204 deletions(-) diff --git a/sc2/src/uqm/load.c b/sc2/src/uqm/load.c index 081ce9526..d7788c8e0 100644 --- a/sc2/src/uqm/load.c +++ b/sc2/src/uqm/load.c @@ -86,13 +86,6 @@ read_32s (void *fp, SDWORD *v) return ret; } -static inline size_t -read_ptr (void *fp) -{ - DWORD t; - return read_32 (fp, &t); /* ptrs are useless in saves */ -} - static inline size_t read_a8 (void *fp, BYTE *ar, COUNT count) { @@ -167,21 +160,13 @@ LoadShipQueue (void *fh, QUEUE *pQueue) FragPtr = LockShipFrag (pQueue, hStarShip); // Read SHIP_FRAGMENT elements - read_16 (fh, NULL); /* unused: was which_side */ read_8 (fh, &FragPtr->captains_name_index); - read_8 (fh, NULL); /* padding */ - read_16 (fh, NULL); /* unused: was ship_flags */ read_8 (fh, &FragPtr->race_id); read_8 (fh, &FragPtr->index); - // XXX: reading crew as BYTE to maintain savegame compatibility - read_8 (fh, &tmpb); - FragPtr->crew_level = tmpb; - read_8 (fh, &tmpb); - FragPtr->max_crew = tmpb; + read_16 (fh, &FragPtr->crew_level); + read_16 (fh, &FragPtr->max_crew); read_8 (fh, &FragPtr->energy_level); read_8 (fh, &FragPtr->max_energy); - read_16 (fh, NULL); /* unused; was loc.x */ - read_16 (fh, NULL); /* unused; was loc.y */ UnlockShipFrag (pQueue, hStarShip); } @@ -227,7 +212,6 @@ LoadRaceQueue (void *fh, QUEUE *pQueue) read_8 (fh, &FleetPtr->func_index); read_16s(fh, &FleetPtr->dest_loc.x); read_16s(fh, &FleetPtr->dest_loc.y); - read_16 (fh, NULL); /* alignment padding */ UnlockFleetInfo (pQueue, hStarShip); } @@ -246,21 +230,15 @@ LoadGroupQueue (void *fh, QUEUE *pQueue) IP_GROUP *GroupPtr; BYTE tmpb; - read_16 (fh, NULL); /* unused; was race_id */ - hGroup = BuildGroup (pQueue, 0); GroupPtr = LockIpGroup (pQueue, hGroup); - read_16 (fh, NULL); /* unused; was which_side */ - read_8 (fh, NULL); /* unused; was captains_name_index */ - read_8 (fh, NULL); /* padding; for savegame compat */ read_16 (fh, &GroupPtr->group_counter); read_8 (fh, &GroupPtr->race_id); read_8 (fh, &tmpb); /* was var2 */ GroupPtr->sys_loc = LONIBBLE (tmpb); GroupPtr->task = HINIBBLE (tmpb); read_8 (fh, &GroupPtr->in_system); /* was crew_level */ - read_8 (fh, NULL); /* unused; was max_crew */ read_8 (fh, &tmpb); /* was energy_level */ GroupPtr->dest_loc = LONIBBLE (tmpb); GroupPtr->orbit_pos = HINIBBLE (tmpb); @@ -278,11 +256,8 @@ LoadEncounter (ENCOUNTER *EncounterPtr, void *fh) COUNT i; BYTE tmpb; - read_ptr (fh); /* useless ptr; HENCOUNTER pred */ EncounterPtr->pred = 0; - read_ptr (fh); /* useless ptr; HENCOUNTER succ */ EncounterPtr->succ = 0; - read_ptr (fh); /* useless ptr; HELEMENT hElement */ EncounterPtr->hElement = 0; read_16s (fh, &EncounterPtr->transition_state); read_16s (fh, &EncounterPtr->origin.x); @@ -292,31 +267,18 @@ LoadEncounter (ENCOUNTER *EncounterPtr, void *fh) read_16s (fh, &EncounterPtr->loc_pt.x); read_16s (fh, &EncounterPtr->loc_pt.y); read_8 (fh, &EncounterPtr->race_id); - read_8 (fh, &tmpb); - EncounterPtr->num_ships = tmpb & ENCOUNTER_SHIPS_MASK; - EncounterPtr->flags = tmpb & ENCOUNTER_FLAGS_MASK; - read_16 (fh, NULL); /* alignment padding */ + read_8 (fh, &EncounterPtr->num_ships); + read_8 (fh, &EncounterPtr->flags); // Load each entry in the BRIEF_SHIP_INFO array for (i = 0; i < MAX_HYPER_SHIPS; i++) { BRIEF_SHIP_INFO *ShipInfo = &EncounterPtr->ShipList[i]; - read_16 (fh, NULL); /* useless; was SHIP_INFO.ship_flags */ read_8 (fh, &ShipInfo->race_id); - read_8 (fh, NULL); /* useless; was SHIP_INFO.var2 */ - // XXX: reading crew as BYTE to maintain savegame compatibility - read_8 (fh, &tmpb); - ShipInfo->crew_level = tmpb; - read_8 (fh, &tmpb); - ShipInfo->max_crew = tmpb; - read_8 (fh, NULL); /* useless; was SHIP_INFO.energy_level */ + read_16 (fh, &ShipInfo->crew_level); + read_16 (fh, &ShipInfo->max_crew); read_8 (fh, &ShipInfo->max_energy); - read_16 (fh, NULL); /* useless; was SHIP_INFO.loc.x */ - read_16 (fh, NULL); /* useless; was SHIP_INFO.loc.y */ - read_32 (fh, NULL); /* useless val; STRING race_strings */ - read_ptr (fh); /* useless ptr; FRAME icons */ - read_ptr (fh); /* useless ptr; FRAME melee_icon */ } // Load the stuff after the BRIEF_SHIP_INFO array @@ -327,37 +289,12 @@ LoadEncounter (ENCOUNTER *EncounterPtr, void *fh) static void LoadEvent (EVENT *EventPtr, void *fh) { - read_ptr (fh); /* useless ptr; HEVENT pred */ EventPtr->pred = 0; - read_ptr (fh); /* useless ptr; HEVENT succ */ EventPtr->succ = 0; read_8 (fh, &EventPtr->day_index); read_8 (fh, &EventPtr->month_index); read_16 (fh, &EventPtr->year_index); read_8 (fh, &EventPtr->func_index); - read_8 (fh, NULL); /* padding */ - read_16 (fh, NULL); /* padding */ -} - -static void -DummyLoadQueue (QUEUE *QueuePtr, void *fh) -{ - /* QUEUE should never actually be loaded since it contains - * purely internal representation and the lists - * involved are actually loaded separately */ - (void)QueuePtr; /* silence compiler */ - - /* QUEUE format with QUEUE_TABLE defined -- UQM default */ - read_ptr (fh); /* HLINK head */ - read_ptr (fh); /* HLINK tail */ - read_ptr (fh); /* BYTE* pq_tab */ - read_ptr (fh); /* HLINK free_list */ - read_16 (fh, NULL); /* MEM_HANDLE hq_tab */ - read_16 (fh, NULL); /* COUNT object_size */ - read_8 (fh, NULL); /* BYTE num_objects */ - - read_8 (fh, NULL); /* padding */ - read_16 (fh, NULL); /* padding */ } static void @@ -368,28 +305,18 @@ LoadClockState (CLOCK_STATE *ClockPtr, void *fh) read_16 (fh, &ClockPtr->year_index); read_16s (fh, &ClockPtr->tick_count); read_16s (fh, &ClockPtr->day_in_ticks); - read_ptr (fh); /* not loading ptr; Semaphore clock_sem */ - read_ptr (fh); /* not loading ptr; Task clock_task */ - read_32 (fh, NULL); /* not loading; DWORD TimeCounter */ - - DummyLoadQueue (&ClockPtr->event_q, fh); } static void LoadGameState (GAME_STATE *GSPtr, void *fh) { - BYTE dummy8; - - read_8 (fh, &dummy8); /* obsolete */ read_8 (fh, &GSPtr->glob_flags); read_8 (fh, &GSPtr->CrewCost); read_8 (fh, &GSPtr->FuelCost); read_a8 (fh, GSPtr->ModuleCost, NUM_MODULES); read_a8 (fh, GSPtr->ElementWorth, NUM_ELEMENT_CATEGORIES); - read_ptr (fh); /* not loading ptr; PRIMITIVE *DisplayArray */ read_16 (fh, &GSPtr->CurrentActivity); - read_16 (fh, NULL); /* CLOCK_STATE alignment padding */ LoadClockState (&GSPtr->GameClock, fh); read_16s (fh, &GSPtr->autopilot.x); @@ -413,20 +340,10 @@ LoadGameState (GAME_STATE *GSPtr, void *fh) read_16s (fh, &GSPtr->velocity.error.height); read_16s (fh, &GSPtr->velocity.incr.width); read_16s (fh, &GSPtr->velocity.incr.height); - read_16 (fh, NULL); /* VELOCITY_DESC padding */ read_32 (fh, &GSPtr->BattleGroupRef); - DummyLoadQueue (&GSPtr->avail_race_q, fh); - DummyLoadQueue (&GSPtr->npc_built_ship_q, fh); - // Not loading ip_group_q, was not there originally - DummyLoadQueue (&GSPtr->encounter_q, fh); - DummyLoadQueue (&GSPtr->built_ship_q, fh); - read_a8 (fh, GSPtr->GameState, sizeof (GSPtr->GameState)); - - assert (sizeof (GSPtr->GameState) % 4 == 3); - read_8 (fh, NULL); /* GAME_STATE alignment padding */ } static BOOLEAN diff --git a/sc2/src/uqm/save.c b/sc2/src/uqm/save.c index bfaa140f5..8b2791a17 100644 --- a/sc2/src/uqm/save.c +++ b/sc2/src/uqm/save.c @@ -84,12 +84,6 @@ cwrite_32 (SAVEBUF *fh, DWORD v) return 0; } -static inline COUNT -cwrite_ptr (SAVEBUF *fh) -{ - return cwrite_32 (fh, 0); /* ptrs are useless in saves */ -} - static inline COUNT cwrite_a8 (SAVEBUF *fh, const BYTE *ar, COUNT count) { @@ -118,12 +112,6 @@ write_32 (void *fp, DWORD v) return WriteResFile (&v, 4, 1, fp); } -static inline size_t -write_ptr (void *fp) -{ - return write_32 (fp, 0); /* ptrs are useless in saves */ -} - static inline size_t write_a8 (void *fp, const BYTE *ar, COUNT count) { @@ -183,19 +171,13 @@ SaveShipQueue (SAVEBUF *fh, QUEUE *pQueue) cwrite_16 (fh, Index); // Write SHIP_FRAGMENT elements - cwrite_16 (fh, 0); /* unused; was which_side */ cwrite_8 (fh, FragPtr->captains_name_index); - cwrite_8 (fh, 0); /* padding */ - cwrite_16 (fh, 0); /* unused: was ship_flags */ cwrite_8 (fh, FragPtr->race_id); cwrite_8 (fh, FragPtr->index); - // XXX: writing crew as BYTE to maintain savegame compatibility - cwrite_8 (fh, FragPtr->crew_level); - cwrite_8 (fh, FragPtr->max_crew); + cwrite_16 (fh, FragPtr->crew_level); + cwrite_16 (fh, FragPtr->max_crew); cwrite_8 (fh, FragPtr->energy_level); cwrite_8 (fh, FragPtr->max_energy); - cwrite_16 (fh, 0); /* unused; was loc.x */ - cwrite_16 (fh, 0); /* unused; was loc.y */ UnlockShipFrag (pQueue, hStarShip); hStarShip = hNextShip; @@ -245,7 +227,6 @@ SaveRaceQueue (SAVEBUF *fh, QUEUE *pQueue) cwrite_8 (fh, FleetPtr->func_index); cwrite_16 (fh, FleetPtr->dest_loc.x); cwrite_16 (fh, FleetPtr->dest_loc.y); - cwrite_16 (fh, 0); /* alignment padding */ UnlockFleetInfo (pQueue, hFleet); hFleet = hNextFleet; @@ -267,18 +248,12 @@ SaveGroupQueue (SAVEBUF *fh, QUEUE *pQueue) GroupPtr = LockIpGroup (pQueue, hGroup); hNextGroup = _GetSuccLink (GroupPtr); - cwrite_16 (fh, GroupPtr->race_id); /* unused; for old versions */ - - cwrite_16 (fh, 0); /* unused; was which_side */ - cwrite_8 (fh, 0); /* unused; was captains_name_index */ - cwrite_8 (fh, 0); /* padding; for savegame compat */ cwrite_16 (fh, GroupPtr->group_counter); cwrite_8 (fh, GroupPtr->race_id); assert (GroupPtr->sys_loc < 0x10 && GroupPtr->task < 0x10); cwrite_8 (fh, MAKE_BYTE (GroupPtr->sys_loc, GroupPtr->task)); /* was var2 */ cwrite_8 (fh, GroupPtr->in_system); /* was crew_level */ - cwrite_8 (fh, 0); /* unused; was max_crew */ assert (GroupPtr->dest_loc < 0x10 && GroupPtr->orbit_pos < 0x10); cwrite_8 (fh, MAKE_BYTE (GroupPtr->dest_loc, GroupPtr->orbit_pos)); /* was energy_level */ @@ -295,9 +270,6 @@ SaveEncounter (const ENCOUNTER *EncounterPtr, SAVEBUF *fh) { COUNT i; - cwrite_ptr (fh); /* useless ptr; HENCOUNTER pred */ - cwrite_ptr (fh); /* useless ptr; HENCOUNTER succ */ - cwrite_ptr (fh); /* useless ptr; HELEMENT hElement */ cwrite_16 (fh, EncounterPtr->transition_state); cwrite_16 (fh, EncounterPtr->origin.x); cwrite_16 (fh, EncounterPtr->origin.y); @@ -306,29 +278,18 @@ SaveEncounter (const ENCOUNTER *EncounterPtr, SAVEBUF *fh) cwrite_16 (fh, EncounterPtr->loc_pt.x); cwrite_16 (fh, EncounterPtr->loc_pt.y); cwrite_8 (fh, EncounterPtr->race_id); - // XXX: writing combined fields to maintain savegame compatibility - cwrite_8 (fh, (EncounterPtr->num_ships & ENCOUNTER_SHIPS_MASK) - | (EncounterPtr->flags & ENCOUNTER_FLAGS_MASK)); - cwrite_16 (fh, 0); /* alignment padding */ + cwrite_8 (fh, EncounterPtr->num_ships); + cwrite_8 (fh, EncounterPtr->flags); // Save each entry in the BRIEF_SHIP_INFO array for (i = 0; i < MAX_HYPER_SHIPS; i++) { const BRIEF_SHIP_INFO *ShipInfo = &EncounterPtr->ShipList[i]; - cwrite_16 (fh, 0); /* useless; was SHIP_INFO.ship_flags */ cwrite_8 (fh, ShipInfo->race_id); - cwrite_8 (fh, 0); /* useless; was SHIP_INFO.var2 */ - // XXX: writing crew as BYTE to maintain savegame compatibility - cwrite_8 (fh, ShipInfo->crew_level); - cwrite_8 (fh, ShipInfo->max_crew); - cwrite_8 (fh, 0); /* useless; was SHIP_INFO.energy_level */ + cwrite_16 (fh, ShipInfo->crew_level); + cwrite_16 (fh, ShipInfo->max_crew); cwrite_8 (fh, ShipInfo->max_energy); - cwrite_16 (fh, 0); /* useless; was SHIP_INFO.loc.x */ - cwrite_16 (fh, 0); /* useless; was SHIP_INFO.loc.y */ - cwrite_32 (fh, 0); /* useless val; STRING race_strings */ - cwrite_ptr (fh); /* useless ptr; FRAME icons */ - cwrite_ptr (fh); /* useless ptr; FRAME melee_icon */ } // Save the stuff after the BRIEF_SHIP_INFO array @@ -339,35 +300,10 @@ SaveEncounter (const ENCOUNTER *EncounterPtr, SAVEBUF *fh) static void SaveEvent (const EVENT *EventPtr, SAVEBUF *fh) { - cwrite_ptr (fh); /* useless ptr; HEVENT pred */ - cwrite_ptr (fh); /* useless ptr; HEVENT succ */ cwrite_8 (fh, EventPtr->day_index); cwrite_8 (fh, EventPtr->month_index); cwrite_16 (fh, EventPtr->year_index); cwrite_8 (fh, EventPtr->func_index); - cwrite_8 (fh, 0); /* padding */ - cwrite_16 (fh, 0); /* padding */ -} - -static void -DummySaveQueue (const QUEUE *QueuePtr, SAVEBUF *fh) -{ - /* QUEUE should never actually be saved since it contains - * purely internal representation and the lists - * involved are actually saved separately */ - (void)QueuePtr; /* silence compiler */ - - /* QUEUE format with QUEUE_TABLE defined -- UQM default */ - cwrite_ptr (fh); /* HLINK head */ - cwrite_ptr (fh); /* HLINK tail */ - cwrite_ptr (fh); /* BYTE* pq_tab */ - cwrite_ptr (fh); /* HLINK free_list */ - cwrite_16 (fh, 0); /* MEM_HANDLE hq_tab */ - cwrite_16 (fh, 0); /* COUNT object_size */ - cwrite_8 (fh, 0); /* BYTE num_objects */ - - cwrite_8 (fh, 0); /* padding */ - cwrite_16 (fh, 0); /* padding */ } static void @@ -378,26 +314,18 @@ SaveClockState (const CLOCK_STATE *ClockPtr, SAVEBUF *fh) cwrite_16 (fh, ClockPtr->year_index); cwrite_16 (fh, ClockPtr->tick_count); cwrite_16 (fh, ClockPtr->day_in_ticks); - cwrite_ptr (fh); /* useless ptr; Semaphore clock_sem */ - cwrite_ptr (fh); /* useless ptr; Task clock_task */ - cwrite_32 (fh, 0); /* useless value; DWORD TimeCounter */ - - DummySaveQueue (&ClockPtr->event_q, fh); } static void SaveGameState (const GAME_STATE *GSPtr, SAVEBUF *fh) { - cwrite_8 (fh, 0); /* obsolete; BYTE cur_state */ cwrite_8 (fh, GSPtr->glob_flags); cwrite_8 (fh, GSPtr->CrewCost); cwrite_8 (fh, GSPtr->FuelCost); cwrite_a8 (fh, GSPtr->ModuleCost, NUM_MODULES); cwrite_a8 (fh, GSPtr->ElementWorth, NUM_ELEMENT_CATEGORIES); - cwrite_ptr (fh); /* useless ptr; PRIMITIVE *DisplayArray */ cwrite_16 (fh, GSPtr->CurrentActivity); - cwrite_16 (fh, 0); /* CLOCK_STATE alignment padding */ SaveClockState (&GSPtr->GameClock, fh); cwrite_16 (fh, GSPtr->autopilot.x); @@ -421,20 +349,10 @@ SaveGameState (const GAME_STATE *GSPtr, SAVEBUF *fh) cwrite_16 (fh, GSPtr->velocity.error.height); cwrite_16 (fh, GSPtr->velocity.incr.width); cwrite_16 (fh, GSPtr->velocity.incr.height); - cwrite_16 (fh, 0); /* VELOCITY_DESC padding */ cwrite_32 (fh, GSPtr->BattleGroupRef); - DummySaveQueue (&GSPtr->avail_race_q, fh); - DummySaveQueue (&GSPtr->npc_built_ship_q, fh); - // Not saving ip_group_q, was not there originally - DummySaveQueue (&GSPtr->encounter_q, fh); - DummySaveQueue (&GSPtr->built_ship_q, fh); - cwrite_a8 (fh, GSPtr->GameState, sizeof (GSPtr->GameState)); - - assert (sizeof (GSPtr->GameState) % 4 == 3); - cwrite_8 (fh, 0); /* GAME_STATE alignment padding */ } static BOOLEAN @@ -469,7 +387,7 @@ SaveSummary (const SUMMARY_DESC *SummPtr, void *fp) if ( write_32 (fp, SAVE_MAGIC) != 1 || write_32 (fp, SUMMARY_MAGIC) != 1 || - write_32 (fp, 161 + strlen(SummPtr->SaveName)) != 1 + write_32 (fp, 160 + strlen(SummPtr->SaveName)) != 1 ) return FALSE; if (!SaveSisState (&SummPtr->SS, fp)) @@ -487,7 +405,7 @@ SaveSummary (const SUMMARY_DESC *SummPtr, void *fp) write_8 (fp, SummPtr->NumDevices) != 1 || write_a8 (fp, SummPtr->ShipList, MAX_BUILT_SHIPS) != 1 || write_a8 (fp, SummPtr->DeviceList, MAX_EXCLUSIVE_DEVICES) != 1 || - write_a8 (fp, SummPtr->SaveName, strlen(SummPtr->SaveName)+1) != 1 + write_a8 (fp, SummPtr->SaveName, strlen(SummPtr->SaveName)) != 1 ) return FALSE; else @@ -667,31 +585,16 @@ SaveFlagshipState (void) BOOLEAN SaveGame (COUNT which_game, SUMMARY_DESC *SummPtr, const char *name) { - BOOLEAN success, made_room; + BOOLEAN success; void *out_fp, *h; SAVEBUF fh_backing, *fh; success = TRUE; - made_room = FALSE; fh = &fh_backing; -RetrySave: h = HMalloc (128 * 1024); if (h == 0) { - if (success) - { - success = FALSE; - made_room = TRUE; - HFree (h); - - FreeSC2Data (); - log_add (log_Debug, "Insufficient room for save buffers" - " -- RETRYING"); - goto RetrySave; - } - else - log_add (log_Debug, "Insufficient room for save buffers" - " -- GIVING UP!"); + log_add (log_Debug, "Insufficient room for save buffers"); } else { @@ -742,7 +645,6 @@ RetrySave: // from group state files. But the original code did, // and so will we until we can prove we do not need to. SaveGroupQueue (fh, &GLOBAL (ip_group_q)); - //SaveEmptyQueue (fh); else // XXX: empty queue write-out is only needed to maintain // the savegame compatibility @@ -802,7 +704,6 @@ RetrySave: { flen = LengthStateFile (fp); // Write the uncompressed size. - printf ("Star Info: %u bytes\n", flen); cwrite_32 (fh, flen); while (flen) { @@ -823,7 +724,6 @@ RetrySave: { flen = LengthStateFile (fp); // Write the uncompressed size. - printf ("Defined Group Info: %u bytes\n", flen); cwrite_32 (fh, flen); while (flen) { @@ -844,7 +744,6 @@ RetrySave: { flen = LengthStateFile (fp); // Write the uncompressed size. - printf ("Random Group Info: %u bytes\n", flen); cwrite_32 (fh, flen); while (flen) { @@ -867,7 +766,7 @@ RetrySave: // Write the memory file to the actual savegame file. sprintf (file, "uqmsave.%02u", which_game); log_add (log_Debug, "'%s' is %u bytes long", file, - flen + 181 + strlen(SummPtr->SaveName)); + flen + 180 + strlen(SummPtr->SaveName)); if (flen && (out_fp = res_OpenResFile (saveDir, file, "wb"))) { PrepareSummary (SummPtr, name); @@ -894,8 +793,5 @@ RetrySave: HFree (h); - if (made_room) - LoadSC2Data (); - return (success); }