From 567772d535dd8fb2f5095a5582e3731aae4617e5 Mon Sep 17 00:00:00 2001 From: Mikael Agren Date: Mon, 30 Jul 2018 13:56:36 +0200 Subject: [PATCH] Do not rely on atof() for version detection Removes atof() in the AGI version detection. atof() is locale dependent. If the user has a locale were the decimal point is something other than '.' the version detection will fail. On Linux this can be tested by running: LC_NUMERIC="sv_SE.UTF-8" AGIStudio A 2.917 game will then be detected as a 2.0 game. --- src/agicommands.cpp | 20 ++++++++++---------- src/agicommands.h | 2 +- src/game.cpp | 24 ++++++++++++------------ src/game.h | 4 ++-- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/agicommands.cpp b/src/agicommands.cpp index dbd9a0c..9aae03d 100644 --- a/src/agicommands.cpp +++ b/src/agicommands.cpp @@ -240,28 +240,28 @@ CommandStruct AGICommand[182] = { int NumAGICommands = 181; // not counting return() -void CorrectCommands(double VerNum) +void CorrectCommands(long VerNum) { - if (VerNum <= 2.089) { + if (VerNum <= 2089000) { AGICommand[134].NumArgs = 0; //quit } - if (VerNum < 2.400) { + if (VerNum < 2400000) { AGICommand[151].NumArgs = 3; // print.at AGICommand[152].NumArgs = 3; // print.at.v } - if (VerNum <= 3.002086) + if (VerNum <= 3002086) AGICommand[176].NumArgs = 1; - if (VerNum <= 2.089) + if (VerNum <= 2089000) NumAGICommands = 155; - else if (VerNum <= 2.272) + else if (VerNum <= 2272000) NumAGICommands = 161; - else if (VerNum <= 2.440) + else if (VerNum <= 2440000) NumAGICommands = 169; - else if (VerNum <= 2.917) + else if (VerNum <= 2917000) NumAGICommands = 173; - else if (VerNum <= 2.936) + else if (VerNum <= 2936000) NumAGICommands = 175; - else if (VerNum <= 3.002086) + else if (VerNum <= 3002086) NumAGICommands = 177; else NumAGICommands = 181; diff --git a/src/agicommands.h b/src/agicommands.h index ccc1e82..e62df61 100644 --- a/src/agicommands.h +++ b/src/agicommands.h @@ -48,6 +48,6 @@ extern CommandStruct TestCommand[19]; //tests for different flags, etc extern CommandStruct AGICommand[182]; //all the other -extern void CorrectCommands(double VerNum); +extern void CorrectCommands(long VerNum); #endif diff --git a/src/game.cpp b/src/game.cpp index 0ed5db6..0d10a3a 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -215,7 +215,7 @@ int Game::open(std::string name) if (!ErrorOccured) { AGIVersionNumber = GetAGIVersionNumber(); - //printf("AGIVersion = %f\n",AGIVersionNumber); + //printf("AGIVersion = %lu\n",AGIVersionNumber); CorrectCommands(AGIVersionNumber); isOpen = true; make_source_dir(); @@ -388,7 +388,7 @@ int Game::newgame(std::string name) isOpen = true; isV3 = false; ID = ""; - AGIVersionNumber = 2.917; + AGIVersionNumber = 2917000; for (int _i = 0; _i <= 3; _i++) { for (j = 0; j <= 255; j++) ResourceInfo[_i][j].Exists = false; @@ -478,16 +478,15 @@ std::string Game::FindAGIV3GameID(const char *name) } //******************************************************* -double Game::GetAGIVersionNumber(void) +long Game::GetAGIVersionNumber(void) { - double VerNum; int ResPos, x; byte VerLen; bool ISVerNum; - char VerNumText[16]; + long VerNum; std::string InFileName = dir + "/agidata.ovl"; char VersionNumBuffer[] = "A_CDE_GHI"; - double ret = 2.917; + int ret = 2917000; // This is what we use if we can't find the version number. // Version 2.917 is the most common interpreter and // the one that all the "new" AGI games should be based on. @@ -532,14 +531,15 @@ double Game::GetAGIVersionNumber(void) } else ISVerNum = false; if (VerLen > 0) { + VerNum = (VersionNumBuffer[0] - '0') * 1000000; + VerNum += (VersionNumBuffer[2] - '0') * 100000; + VerNum += (VersionNumBuffer[3] - '0') * 10000; + VerNum += (VersionNumBuffer[4] - '0') * 1000; if (VersionNumBuffer[5] == '.') { - strcpy(VerNumText, VersionNumBuffer); - strcpy(VerNumText + 5, VersionNumBuffer + 6); // remove second . - } else { - strncpy(VerNumText, VersionNumBuffer, VerLen); - VerNumText[VerLen] = 0; + VerNum += (VersionNumBuffer[6] - '0') * 100; + VerNum += (VersionNumBuffer[7] - '0') * 10; + VerNum += (VersionNumBuffer[8] - '0'); } - VerNum = atof(VerNumText); if (VerNum != 0) { ret = VerNum; break; diff --git a/src/game.h b/src/game.h index a324004..11d0d6d 100644 --- a/src/game.h +++ b/src/game.h @@ -82,9 +82,9 @@ class Game std::string templatedir; //template game directory std::string helpdir; //help directory private: - double AGIVersionNumber; + long AGIVersionNumber; std::string FindAGIV3GameID(const char *name); - double GetAGIVersionNumber(void); + long GetAGIVersionNumber(void); int ReadV3Resource(char ResourceType, int ResourceID); FILE *OpenPatchVol(int PatchVol, int *filesize); FILE *OpenDirUpdate(int *dirsize, int ResType);