Fix signed value read/writes in savegames and groups

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3454 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2009-12-20 03:07:53 +00:00
parent 515f2a216c
commit f6959074cb
4 changed files with 108 additions and 43 deletions
+2 -2
View File
@@ -153,8 +153,8 @@ ReadIpGroup (void *fp, IP_GROUP *GroupPtr)
GroupPtr->dest_loc = LONIBBLE (tmpb); GroupPtr->dest_loc = LONIBBLE (tmpb);
GroupPtr->orbit_pos = HINIBBLE (tmpb); GroupPtr->orbit_pos = HINIBBLE (tmpb);
sread_8 (fp, &GroupPtr->group_id); /* was max_energy */ sread_8 (fp, &GroupPtr->group_id); /* was max_energy */
sread_16 (fp, &GroupPtr->loc.x); sread_16s(fp, &GroupPtr->loc.x);
sread_16 (fp, &GroupPtr->loc.y); sread_16s(fp, &GroupPtr->loc.y);
} }
static void static void
+84 -38
View File
@@ -58,6 +58,19 @@ cread_16 (DECODE_REF fh, UWORD *v)
return cread (v, 2, 1, fh); return cread (v, 2, 1, fh);
} }
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 static inline COUNT
cread_32 (DECODE_REF fh, DWORD *v) cread_32 (DECODE_REF fh, DWORD *v)
{ {
@@ -67,6 +80,19 @@ cread_32 (DECODE_REF fh, DWORD *v)
return cread (v, 4, 1, fh); return cread (v, 4, 1, fh);
} }
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 static inline COUNT
cread_ptr (DECODE_REF fh) cread_ptr (DECODE_REF fh)
{ {
@@ -108,6 +134,19 @@ read_32 (void *fp, DWORD *v)
return ReadResFile (v, 4, 1, fp); return ReadResFile (v, 4, 1, fp);
} }
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 static inline COUNT
read_ptr (void *fp) read_ptr (void *fp)
{ {
@@ -122,6 +161,13 @@ read_a8 (void *fp, BYTE *ar, COUNT count)
return ReadResFile (ar, 1, count, fp) == count; return ReadResFile (ar, 1, count, fp) == count;
} }
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 static inline COUNT
read_a16 (void *fp, UWORD *ar, COUNT count) read_a16 (void *fp, UWORD *ar, COUNT count)
{ {
@@ -219,17 +265,17 @@ LoadRaceQueue (DECODE_REF fh, QUEUE *pQueue)
FleetPtr->max_crew = tmpb; FleetPtr->max_crew = tmpb;
cread_8 (fh, &FleetPtr->growth); cread_8 (fh, &FleetPtr->growth);
cread_8 (fh, &FleetPtr->max_energy); cread_8 (fh, &FleetPtr->max_energy);
cread_16 (fh, &FleetPtr->loc.x); cread_16s(fh, &FleetPtr->loc.x);
cread_16 (fh, &FleetPtr->loc.y); cread_16s(fh, &FleetPtr->loc.y);
cread_16 (fh, &FleetPtr->actual_strength); cread_16 (fh, &FleetPtr->actual_strength);
cread_16 (fh, &FleetPtr->known_strength); cread_16 (fh, &FleetPtr->known_strength);
cread_16 (fh, &FleetPtr->known_loc.x); cread_16s(fh, &FleetPtr->known_loc.x);
cread_16 (fh, &FleetPtr->known_loc.y); cread_16s(fh, &FleetPtr->known_loc.y);
cread_8 (fh, &FleetPtr->growth_err_term); cread_8 (fh, &FleetPtr->growth_err_term);
cread_8 (fh, &FleetPtr->func_index); cread_8 (fh, &FleetPtr->func_index);
cread_16 (fh, &FleetPtr->dest_loc.x); cread_16s(fh, &FleetPtr->dest_loc.x);
cread_16 (fh, &FleetPtr->dest_loc.y); cread_16s(fh, &FleetPtr->dest_loc.y);
cread_16 (fh, NULL); /* alignment padding */ cread_16 (fh, NULL); /* alignment padding */
UnlockFleetInfo (pQueue, hStarShip); UnlockFleetInfo (pQueue, hStarShip);
@@ -268,8 +314,8 @@ LoadGroupQueue (DECODE_REF fh, QUEUE *pQueue)
GroupPtr->dest_loc = LONIBBLE (tmpb); GroupPtr->dest_loc = LONIBBLE (tmpb);
GroupPtr->orbit_pos = HINIBBLE (tmpb); GroupPtr->orbit_pos = HINIBBLE (tmpb);
cread_8 (fh, &GroupPtr->group_id); /* was max_energy */ cread_8 (fh, &GroupPtr->group_id); /* was max_energy */
cread_16 (fh, &GroupPtr->loc.x); cread_16s(fh, &GroupPtr->loc.x);
cread_16 (fh, &GroupPtr->loc.y); cread_16s(fh, &GroupPtr->loc.y);
UnlockIpGroup (pQueue, hGroup); UnlockIpGroup (pQueue, hGroup);
} }
@@ -286,13 +332,13 @@ LoadEncounter (ENCOUNTER *EncounterPtr, DECODE_REF fh)
EncounterPtr->succ = 0; EncounterPtr->succ = 0;
cread_ptr (fh); /* useless ptr; HELEMENT hElement */ cread_ptr (fh); /* useless ptr; HELEMENT hElement */
EncounterPtr->hElement = 0; EncounterPtr->hElement = 0;
cread_16 (fh, &EncounterPtr->transition_state); cread_16s (fh, &EncounterPtr->transition_state);
cread_16 (fh, &EncounterPtr->origin.x); cread_16s (fh, &EncounterPtr->origin.x);
cread_16 (fh, &EncounterPtr->origin.y); cread_16s (fh, &EncounterPtr->origin.y);
cread_16 (fh, &EncounterPtr->radius); cread_16 (fh, &EncounterPtr->radius);
// STAR_DESC fields // STAR_DESC fields
cread_16 (fh, &EncounterPtr->SD.star_pt.x); cread_16s (fh, &EncounterPtr->SD.star_pt.x);
cread_16 (fh, &EncounterPtr->SD.star_pt.y); cread_16s (fh, &EncounterPtr->SD.star_pt.y);
cread_8 (fh, &EncounterPtr->SD.Type); cread_8 (fh, &EncounterPtr->SD.Type);
cread_8 (fh, &EncounterPtr->SD.Index); cread_8 (fh, &EncounterPtr->SD.Index);
cread_16 (fh, NULL); /* alignment padding */ cread_16 (fh, NULL); /* alignment padding */
@@ -321,8 +367,8 @@ LoadEncounter (ENCOUNTER *EncounterPtr, DECODE_REF fh)
} }
// Load the stuff after the BRIEF_SHIP_INFO array // Load the stuff after the BRIEF_SHIP_INFO array
cread_32 (fh, &EncounterPtr->log_x); cread_32s (fh, &EncounterPtr->log_x);
cread_32 (fh, &EncounterPtr->log_y); cread_32s (fh, &EncounterPtr->log_y);
} }
static void static void
@@ -367,8 +413,8 @@ LoadClockState (CLOCK_STATE *ClockPtr, DECODE_REF fh)
cread_8 (fh, &ClockPtr->day_index); cread_8 (fh, &ClockPtr->day_index);
cread_8 (fh, &ClockPtr->month_index); cread_8 (fh, &ClockPtr->month_index);
cread_16 (fh, &ClockPtr->year_index); cread_16 (fh, &ClockPtr->year_index);
cread_16 (fh, &ClockPtr->tick_count); cread_16s (fh, &ClockPtr->tick_count);
cread_16 (fh, &ClockPtr->day_in_ticks); cread_16s (fh, &ClockPtr->day_in_ticks);
cread_ptr (fh); /* not loading ptr; Semaphore clock_sem */ cread_ptr (fh); /* not loading ptr; Semaphore clock_sem */
cread_ptr (fh); /* not loading ptr; Task clock_task */ cread_ptr (fh); /* not loading ptr; Task clock_task */
cread_32 (fh, NULL); /* not loading; DWORD TimeCounter */ cread_32 (fh, NULL); /* not loading; DWORD TimeCounter */
@@ -393,27 +439,27 @@ LoadGameState (GAME_STATE *GSPtr, DECODE_REF fh)
cread_16 (fh, NULL); /* CLOCK_STATE alignment padding */ cread_16 (fh, NULL); /* CLOCK_STATE alignment padding */
LoadClockState (&GSPtr->GameClock, fh); LoadClockState (&GSPtr->GameClock, fh);
cread_16 (fh, &GSPtr->autopilot.x); cread_16s (fh, &GSPtr->autopilot.x);
cread_16 (fh, &GSPtr->autopilot.y); cread_16s (fh, &GSPtr->autopilot.y);
cread_16 (fh, &GSPtr->ip_location.x); cread_16s (fh, &GSPtr->ip_location.x);
cread_16 (fh, &GSPtr->ip_location.y); cread_16s (fh, &GSPtr->ip_location.y);
/* STAMP ShipStamp */ /* STAMP ShipStamp */
cread_16 (fh, &GSPtr->ShipStamp.origin.x); cread_16s (fh, &GSPtr->ShipStamp.origin.x);
cread_16 (fh, &GSPtr->ShipStamp.origin.y); cread_16s (fh, &GSPtr->ShipStamp.origin.y);
cread_16 (fh, &GSPtr->ShipFacing); cread_16 (fh, &GSPtr->ShipFacing);
cread_8 (fh, &GSPtr->ip_planet); cread_8 (fh, &GSPtr->ip_planet);
cread_8 (fh, &GSPtr->in_orbit); cread_8 (fh, &GSPtr->in_orbit);
/* VELOCITY_DESC velocity */ /* VELOCITY_DESC velocity */
cread_16 (fh, &GSPtr->velocity.TravelAngle); cread_16 (fh, &GSPtr->velocity.TravelAngle);
cread_16 (fh, &GSPtr->velocity.vector.width); cread_16s (fh, &GSPtr->velocity.vector.width);
cread_16 (fh, &GSPtr->velocity.vector.height); cread_16s (fh, &GSPtr->velocity.vector.height);
cread_16 (fh, &GSPtr->velocity.fract.width); cread_16s (fh, &GSPtr->velocity.fract.width);
cread_16 (fh, &GSPtr->velocity.fract.height); cread_16s (fh, &GSPtr->velocity.fract.height);
cread_16 (fh, &GSPtr->velocity.error.width); cread_16s (fh, &GSPtr->velocity.error.width);
cread_16 (fh, &GSPtr->velocity.error.height); cread_16s (fh, &GSPtr->velocity.error.height);
cread_16 (fh, &GSPtr->velocity.incr.width); cread_16s (fh, &GSPtr->velocity.incr.width);
cread_16 (fh, &GSPtr->velocity.incr.height); cread_16s (fh, &GSPtr->velocity.incr.height);
cread_16 (fh, NULL); /* VELOCITY_DESC padding */ cread_16 (fh, NULL); /* VELOCITY_DESC padding */
cread_32 (fh, &GSPtr->BattleGroupRef); cread_32 (fh, &GSPtr->BattleGroupRef);
@@ -434,8 +480,8 @@ static BOOLEAN
LoadSisState (SIS_STATE *SSPtr, void *fp) LoadSisState (SIS_STATE *SSPtr, void *fp)
{ {
if ( if (
read_32 (fp, &SSPtr->log_x) != 1 || read_32s (fp, &SSPtr->log_x) != 1 ||
read_32 (fp, &SSPtr->log_y) != 1 || read_32s (fp, &SSPtr->log_y) != 1 ||
read_32 (fp, &SSPtr->ResUnits) != 1 || read_32 (fp, &SSPtr->ResUnits) != 1 ||
read_32 (fp, &SSPtr->FuelOnBoard) != 1 || read_32 (fp, &SSPtr->FuelOnBoard) != 1 ||
read_16 (fp, &SSPtr->CrewEnlisted) != 1 || read_16 (fp, &SSPtr->CrewEnlisted) != 1 ||
@@ -447,9 +493,9 @@ LoadSisState (SIS_STATE *SSPtr, void *fp)
read_8 (fp, &SSPtr->NumLanders) != 1 || read_8 (fp, &SSPtr->NumLanders) != 1 ||
read_a16 (fp, SSPtr->ElementAmounts, NUM_ELEMENT_CATEGORIES) != 1 || read_a16 (fp, SSPtr->ElementAmounts, NUM_ELEMENT_CATEGORIES) != 1 ||
read_a8 (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 || read_str (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 ||
read_a8 (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 || read_str (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 ||
read_a8 (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 || read_str (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 ||
read_16 (fp, NULL) != 1 /* padding */ read_16 (fp, NULL) != 1 /* padding */
) )
@@ -487,8 +533,8 @@ LoadSummary (SUMMARY_DESC *SummPtr, void *fp)
static void static void
LoadStarDesc (STAR_DESC *SDPtr, DECODE_REF fh) LoadStarDesc (STAR_DESC *SDPtr, DECODE_REF fh)
{ {
cread_16 (fh, &SDPtr->star_pt.x); cread_16s(fh, &SDPtr->star_pt.x);
cread_16 (fh, &SDPtr->star_pt.y); cread_16s(fh, &SDPtr->star_pt.y);
cread_8 (fh, &SDPtr->Type); cread_8 (fh, &SDPtr->Type);
cread_8 (fh, &SDPtr->Index); cread_8 (fh, &SDPtr->Index);
cread_8 (fh, &SDPtr->Prefix); cread_8 (fh, &SDPtr->Prefix);
+10 -3
View File
@@ -104,6 +104,13 @@ write_a8 (void *fp, const BYTE *ar, COUNT count)
return WriteResFile (ar, 1, count, fp) == count; return WriteResFile (ar, 1, count, fp) == count;
} }
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 static inline COUNT
write_a16 (void *fp, const UWORD *ar, COUNT count) write_a16 (void *fp, const UWORD *ar, COUNT count)
{ {
@@ -419,9 +426,9 @@ SaveSisState (const SIS_STATE *SSPtr, void *fp)
write_8 (fp, SSPtr->NumLanders) != 1 || write_8 (fp, SSPtr->NumLanders) != 1 ||
write_a16 (fp, SSPtr->ElementAmounts, NUM_ELEMENT_CATEGORIES) != 1 || write_a16 (fp, SSPtr->ElementAmounts, NUM_ELEMENT_CATEGORIES) != 1 ||
write_a8 (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 || write_str (fp, SSPtr->ShipName, SIS_NAME_SIZE) != 1 ||
write_a8 (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 || write_str (fp, SSPtr->CommanderName, SIS_NAME_SIZE) != 1 ||
write_a8 (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 || write_str (fp, SSPtr->PlanetName, SIS_NAME_SIZE) != 1 ||
write_16 (fp, 0) != 1 /* padding */ write_16 (fp, 0) != 1 /* padding */
) )
+12
View File
@@ -92,6 +92,18 @@ sread_16 (void *fp, UWORD *v)
return ReadStateFile (v, 2, 1, fp); return ReadStateFile (v, 2, 1, fp);
} }
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 static inline COUNT
sread_32 (void *fp, DWORD *v) sread_32 (void *fp, DWORD *v)
{ {