Skip to content

Commit

Permalink
Fix some float-serialisation bugs on Windows
Browse files Browse the repository at this point in the history
That whole "Windows doesn't support hex-encoded floats" thing is such a
 pain in the nonportable orifice.
  • Loading branch information
ec429 committed Dec 6, 2019
1 parent ee88fb8 commit ed35412
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
10 changes: 5 additions & 5 deletions gensave.py
Expand Up @@ -96,13 +96,13 @@ def float_to_hex(value):
if windows and ':' in line:
to_conv = {'Confid':(0,),
'Morale':(0,),
'IClass':(1,),
'Targets init':(0,1,2,3),
'Targ':(1,2,3,4)}
'IClass':(0,),
'Targ':(0,1,2,3)}
tag, values = line.rstrip('\n').split(':', 1)
starttag, _, resttag = tag.partition(' ')
values = values.split(',')
if tag in to_conv:
for pos in to_conv[tag]:
if starttag in to_conv:
for pos in to_conv[starttag]:
values[pos] = float_to_hex(values[pos])
line = ':'.join((tag, ','.join(values))) + '\n'
sys.stdout.write(line)
13 changes: 9 additions & 4 deletions saving.c
Expand Up @@ -669,8 +669,11 @@ int loadgame(const char *fn, game *state)
}
else if(strcmp(tag, "Targets init")==0)
{
double dmg,flk,heat,flam;
f=sscanf(dat, FLOAT","FLOAT","FLOAT","FLOAT"\n", CAST_FLOAT_PTR &dmg, CAST_FLOAT_PTR &flk, CAST_FLOAT_PTR &heat, CAST_FLOAT_PTR &flam);
/* Only user of this tag is qstart.sav, and that doesn't need fractions.
* Using integers here allows us to avoid awkwardness with the Windows compat.
*/
unsigned int dmg,flk,heat,flam;
f=sscanf(dat, "%u,%u,%u,%u\n", &dmg, &flk, &heat, &flam);
if(f!=4)
{
fprintf(stderr, "1 Too few arguments to tag \"%s\"\n", tag);
Expand Down Expand Up @@ -1024,8 +1027,10 @@ int savegame(const char *fn, game state)
fprintf(fs, "Site %u:%u\n", i, flags);
}
fprintf(fs, "Targets:%u\n", ntargs);
for(unsigned int i=0;i<ntargs;i++)
fprintf(fs, "Targ %u:"FLOAT","FLOAT","FLOAT","FLOAT"\n", i, CAST_FLOAT state.dmg[i], CAST_FLOAT (targs[i].flak?state.flk[i]*100.0/(double)targs[i].flak:0), CAST_FLOAT state.heat[i], CAST_FLOAT state.flam[i]);
for(unsigned int i=0;i<ntargs;i++) {
double flk = (targs[i].flak?state.flk[i]*100.0/(double)targs[i].flak:0);
fprintf(fs, "Targ %u:"FLOAT","FLOAT","FLOAT","FLOAT"\n", i, CAST_FLOAT state.dmg[i], CAST_FLOAT flk, CAST_FLOAT state.heat[i], CAST_FLOAT state.flam[i]);
}
fprintf(fs, "Weather state:"FLOAT","FLOAT"\n", CAST_FLOAT state.weather.push, CAST_FLOAT state.weather.slant);
for(unsigned int x=0;x<256;x++)
{
Expand Down
2 changes: 1 addition & 1 deletion saving.h
Expand Up @@ -12,7 +12,7 @@
#ifdef WINDOWS /* Because of this little bugger, savegames from Windows won't work on Linux/Unix and vice-versa */
#define FLOAT "%I64x"
#define CAST_FLOAT_PTR (unsigned long long *)
#define CAST_FLOAT (unsigned long long)
#define CAST_FLOAT *(unsigned long long *)&
#else
#define FLOAT "%la"
#define CAST_FLOAT_PTR
Expand Down

0 comments on commit ed35412

Please sign in to comment.