From: <av...@us...> - 2009-12-20 03:08:08
|
Revision: 3454 http://sc2.svn.sourceforge.net/sc2/?rev=3454&view=rev Author: avolkov Date: 2009-12-20 03:07:53 +0000 (Sun, 20 Dec 2009) Log Message: ----------- Fix signed value read/writes in savegames and groups Modified Paths: -------------- trunk/sc2/src/uqm/grpinfo.c trunk/sc2/src/uqm/load.c trunk/sc2/src/uqm/save.c trunk/sc2/src/uqm/state.h Modified: trunk/sc2/src/uqm/grpinfo.c =================================================================== --- trunk/sc2/src/uqm/grpinfo.c 2009-12-20 02:18:01 UTC (rev 3453) +++ trunk/sc2/src/uqm/grpinfo.c 2009-12-20 03:07:53 UTC (rev 3454) @@ -153,8 +153,8 @@ GroupPtr->dest_loc = LONIBBLE (tmpb); GroupPtr->orbit_pos = HINIBBLE (tmpb); sread_8 (fp, &GroupPtr->group_id); /* was max_energy */ - sread_16 (fp, &GroupPtr->loc.x); - sread_16 (fp, &GroupPtr->loc.y); + sread_16s(fp, &GroupPtr->loc.x); + sread_16s(fp, &GroupPtr->loc.y); } static void Modified: trunk/sc2/src/uqm/load.c =================================================================== --- trunk/sc2/src/uqm/load.c 2009-12-20 02:18:01 UTC (rev 3453) +++ trunk/sc2/src/uqm/load.c 2009-12-20 03:07:53 UTC (rev 3454) @@ -59,6 +59,19 @@ } static inline COUNT +cread_16s (DECODE_REF fh, SWORD *v) +{ + UWORD t; + COUNT ret; + // value was converted to unsigned when saved + ret = cread_16 (fh, &t); + // unsigned to signed conversion + if (v) + *v = t; + return ret; +} + +static inline COUNT cread_32 (DECODE_REF fh, DWORD *v) { DWORD t; @@ -68,6 +81,19 @@ } static inline COUNT +cread_32s (DECODE_REF fh, SDWORD *v) +{ + DWORD t; + COUNT ret; + // value was converted to unsigned when saved + ret = cread_32 (fh, &t); + // unsigned to signed conversion + if (v) + *v = t; + return ret; +} + +static inline COUNT cread_ptr (DECODE_REF fh) { DWORD t; @@ -109,6 +135,19 @@ } static inline COUNT +read_32s (void *fp, SDWORD *v) +{ + DWORD t; + COUNT ret; + // value was converted to unsigned when saved + ret = read_32 (fp, &t); + // unsigned to signed conversion + if (v) + *v = t; + return ret; +} + +static inline COUNT read_ptr (void *fp) { DWORD t; @@ -123,6 +162,13 @@ } static inline COUNT +read_str (void *fp, char *str, COUNT count) +{ + // no type conversion needed for strings + return read_a8 (fp, (BYTE *)str, count); +} + +static inline COUNT read_a16 (void *fp, UWORD *ar, COUNT count) { assert (ar != NULL); @@ -219,17 +265,17 @@ FleetPtr->max_crew = tmpb; cread_8 (fh, &FleetPtr->growth); cread_8 (fh, &FleetPtr->max_energy); - cread_16 (fh, &FleetPtr->loc.x); - cread_16 (fh, &FleetPtr->loc.y); + cread_16s(fh, &FleetPtr->loc.x); + cread_16s(fh, &FleetPtr->loc.y); cread_16 (fh, &FleetPtr->actual_strength); cread_16 (fh, &FleetPtr->known_strength); - cread_16 (fh, &FleetPtr->known_loc.x); - cread_16 (fh, &FleetPtr->known_loc.y); + cread_16s(fh, &FleetPtr->known_loc.x); + cread_16s(fh, &FleetPtr->known_loc.y); cread_8 (fh, &FleetPtr->growth_err_term); cread_8 (fh, &FleetPtr->func_index); - cread_16 (fh, &FleetPtr->dest_loc.x); - cread_16 (fh, &FleetPtr->dest_loc.y); + cread_16s(fh, &FleetPtr->dest_loc.x); + cread_16s(fh, &FleetPtr->dest_loc.y); cread_16 (fh, NULL); /* alignment padding */ UnlockFleetInfo (pQueue, hStarShip); @@ -268,8 +314,8 @@ GroupPtr->dest_loc = LONIBBLE (tmpb); GroupPtr->orbit_pos = HINIBBLE (tmpb); cread_8 (fh, &GroupPtr->group_id); /* was max_energy */ - cread_16 (fh, &GroupPtr->loc.x); - cread_16 (fh, &GroupPtr->loc.y); + cread_16s(fh, &GroupPtr->loc.x); + cread_16s(fh, &GroupPtr->loc.y); UnlockIpGroup (pQueue, hGroup); } @@ -286,13 +332,13 @@ EncounterPtr->succ = 0; cread_ptr (fh); /* useless ptr; HELEMENT hElement */ EncounterPtr->hElement = 0; - cread_16 (fh, &EncounterPtr->transition_state); - cread_16 (fh, &EncounterPtr->origin.x); - cread_16 (fh, &EncounterPtr->origin.y); + cread_16s (fh, &EncounterPtr->transition_state); + cread_16s (fh, &EncounterPtr->origin.x); + cread_16s (fh, &EncounterPtr->origin.y); cread_16 (fh, &EncounterPtr->radius); // STAR_DESC fields - cread_16 (fh, &EncounterPtr->SD.star_pt.x); - cread_16 (fh, &EncounterPtr->SD.star_pt.y); + cread_16s (fh, &EncounterPtr->SD.star_pt.x); + cread_16s (fh, &EncounterPtr->SD.star_pt.y); cread_8 (fh, &EncounterPtr->SD.Type); cread_8 (fh, &EncounterPtr->SD.Index); cread_16 (fh, NULL); /* alignment padding */ @@ -321,8 +367,8 @@ } // Load the stuff after the BRIEF_SHIP_INFO array - cread_32 (fh, &EncounterPtr->log_x); - cread_32 (fh, &EncounterPtr->log_y); + cread_32s (fh, &EncounterPtr->log_x); + cread_32s (fh, &EncounterPtr->log_y); } static void @@ -367,8 +413,8 @@ cread_8 (fh, &ClockPtr->day_index); cread_8 (fh, &ClockPtr->month_index); cread_16 (fh, &ClockPtr->year_index); - cread_16 (fh, &ClockPtr->tick_count); - cread_16 (fh, &ClockPtr->day_in_ticks); + cread_16s (fh, &ClockPtr->tick_count); + cread_16s (fh, &ClockPtr->day_in_ticks); cread_ptr (fh); /* not loading ptr; Semaphore clock_sem */ cread_ptr (fh); /* not loading ptr; Task clock_task */ cread_32 (fh, NULL); /* not loading; DWORD TimeCounter */ @@ -393,27 +439,27 @@ cread_16 (fh, NULL); /* CLOCK_STATE alignment padding */ LoadClockState (&GSPtr->GameClock, fh); - cread_16 (fh, &GSPtr->autopilot.x); - cread_16 (fh, &GSPtr->autopilot.y); - cread_16 (fh, &GSPtr->ip_location.x); - cread_16 (fh, &GSPtr->ip_location.y); + cread_16s (fh, &GSPtr->autopilot.x); + cread_16s (fh, &GSPtr->autopilot.y); + cread_16s (fh, &GSPtr->ip_location.x); + cread_16s (fh, &GSPtr->ip_location.y); /* STAMP ShipStamp */ - cread_16 (fh, &GSPtr->ShipStamp.origin.x); - cread_16 (fh, &GSPtr->ShipStamp.origin.y); + cread_16s (fh, &GSPtr->ShipStamp.origin.x); + cread_16s (fh, &GSPtr->ShipStamp.origin.y); cread_16 (fh, &GSPtr->ShipFacing); cread_8 (fh, &GSPtr->ip_planet); cread_8 (fh, &GSPtr->in_orbit); /* VELOCITY_DESC velocity */ cread_16 (fh, &GSPtr->velocity.TravelAngle); - cread_16 (fh, &GSPtr->velocity.vector.width); - cread_16 (fh, &GSPtr->velocity.vector.height); - cread_16 (fh, &GSPtr->velocity.fract.width); - cread_16 (fh, &GSPtr->velocity.fract.height); - cread_16 (fh, &GSPtr->velocity.error.width); - cread_16 (fh, &GSPtr->velocity.error.height); - cread_16 (fh, &GSPtr->velocity.incr.width); - cread_16 (fh, &GSPtr->velocity.incr.height); + cread_16s (fh, &GSPtr->velocity.vector.width); + cread_16s (fh, &GSPtr->velocity.vector.height); + cread_16s (fh, &GSPtr->velocity.fract.width); + cread_16s (fh, &GSPtr->velocity.fract.height); + cread_16s (fh, &GSPtr->velocity.error.width); + cread_16s (fh, &GSPtr->velocity.error.height); + cread_16s (fh, &GSPtr->velocity.incr.width); + cread_16s (fh, &GSPtr->velocity.incr.height); cread_16 (fh, NULL); /* VELOCITY_DESC padding */ cread_32 (fh, &GSPtr->BattleGroupRef); @@ -434,8 +480,8 @@ LoadSisState (SIS_STATE *SSPtr, void *fp) { if ( - read_32 (fp, &SSPtr->log_x) != 1 || - read_32 (fp, &SSPtr->log_y) != 1 || + read_32s (fp, &SSPtr->log_x) != 1 || + read_32s (fp, &SSPtr->log_y) != 1 || read_32 (fp, &SSPtr->ResUnits) != 1 || read_32 (fp, &SSPtr->FuelOnBoard) != 1 || read_16 (fp, &SSPtr->CrewEnlisted) != 1 || @@ -447,9 +493,9 @@ read_8 (fp, &SSPtr->NumLanders) != 1 || read_a16 (fp, SSPtr->ElementAmounts, NUM_ELEMENT_CATEGORIES) != 1 || - read_a8 (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 || - read_a8 (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 || - read_a8 (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 || + read_str (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 || + read_str (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 || + read_str (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 || read_16 (fp, NULL) != 1 /* padding */ ) @@ -487,8 +533,8 @@ static void LoadStarDesc (STAR_DESC *SDPtr, DECODE_REF fh) { - cread_16 (fh, &SDPtr->star_pt.x); - cread_16 (fh, &SDPtr->star_pt.y); + cread_16s(fh, &SDPtr->star_pt.x); + cread_16s(fh, &SDPtr->star_pt.y); cread_8 (fh, &SDPtr->Type); cread_8 (fh, &SDPtr->Index); cread_8 (fh, &SDPtr->Prefix); Modified: trunk/sc2/src/uqm/save.c =================================================================== --- trunk/sc2/src/uqm/save.c 2009-12-20 02:18:01 UTC (rev 3453) +++ trunk/sc2/src/uqm/save.c 2009-12-20 03:07:53 UTC (rev 3454) @@ -105,6 +105,13 @@ } static inline COUNT +write_str (void *fp, const char *str, COUNT count) +{ + // no type conversion needed for strings + return write_a8 (fp, (const BYTE *)str, count); +} + +static inline COUNT write_a16 (void *fp, const UWORD *ar, COUNT count) { for ( ; count > 0; --count, ++ar) @@ -419,9 +426,9 @@ write_8 (fp, SSPtr->NumLanders) != 1 || write_a16 (fp, SSPtr->ElementAmounts, NUM_ELEMENT_CATEGORIES) != 1 || - write_a8 (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 || - write_a8 (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 || - write_a8 (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 || + write_str (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 || + write_str (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 || + write_str (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 || write_16 (fp, 0) != 1 /* padding */ ) Modified: trunk/sc2/src/uqm/state.h =================================================================== --- trunk/sc2/src/uqm/state.h 2009-12-20 02:18:01 UTC (rev 3453) +++ trunk/sc2/src/uqm/state.h 2009-12-20 03:07:53 UTC (rev 3454) @@ -93,6 +93,18 @@ } static inline COUNT +sread_16s (void *fp, SWORD *v) +{ + UWORD t; + COUNT ret; + ret = sread_16 (fp, &t); + // unsigned to signed conversion + if (v) + *v = t; + return ret; +} + +static inline COUNT sread_32 (void *fp, DWORD *v) { DWORD t; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |