From 4cd77a019281ca166ff35008e86b4af13c6808aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20W=2E=20Urba=C5=84czyk?= Date: Fri, 1 Jun 2012 15:59:26 +0000 Subject: [PATCH] Naive solution for endianess issues. Seems to allow loading a big-endian save on little-endian build. Fixed compile issues with CEmptyAI. Removed unnecessary methods from its. --- AI/EmptyAI/CEmptyAI.cpp | 24 +++++++++++++++--------- AI/EmptyAI/CEmptyAI.h | 15 ++++----------- lib/Connection.cpp | 17 ++++++++++++++--- lib/Connection.h | 9 ++++++++- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/AI/EmptyAI/CEmptyAI.cpp b/AI/EmptyAI/CEmptyAI.cpp index 5991ccd3f..390b50d8e 100644 --- a/AI/EmptyAI/CEmptyAI.cpp +++ b/AI/EmptyAI/CEmptyAI.cpp @@ -12,16 +12,22 @@ void CEmptyAI::yourTurn() { cb->endTurn(); } -void CEmptyAI::heroKilled(const CGHeroInstance *) -{ -} -void CEmptyAI::heroCreated(const CGHeroInstance *) -{ -} -void CEmptyAI::heroMoved(const TryMoveHero& TMH) -{ -} void CEmptyAI::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector &skills, boost::function &callback) { callback(rand()%skills.size()); } + +void CEmptyAI::commanderGotLevel(const CCommanderInstance * commander, std::vector skills, boost::function &callback) +{ + callback(0); +} + +void CEmptyAI::showBlockingDialog(const std::string &text, const std::vector &components, ui32 askID, const int soundID, bool selection, bool cancel) +{ + cb->selectionMade(0, askID); +} + +void CEmptyAI::showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function &onEnd) +{ + onEnd(); +} \ No newline at end of file diff --git a/AI/EmptyAI/CEmptyAI.h b/AI/EmptyAI/CEmptyAI.h index a10289fbd..2e830ee16 100644 --- a/AI/EmptyAI/CEmptyAI.h +++ b/AI/EmptyAI/CEmptyAI.h @@ -12,17 +12,10 @@ class CEmptyAI : public CGlobalAI public: void init(CCallback * CB) override; void yourTurn() override; - void heroKilled(const CGHeroInstance *) override; - void heroCreated(const CGHeroInstance *) override; - void heroMoved(const TryMoveHero&) override; - void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) override {}; - void showSelDialog(std::string text, std::vector & components, int askID) override {}; - void tileRevealed(const boost::unordered_set &pos) override {}; - void tileHidden(const boost::unordered_set &pos) override {}; - void showBlockingDialog(const std::string &text, const std::vector &components, ui32 askID, int soundID, bool selection, bool cancel) override {}; - void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function &onEnd) override {}; - void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector &skills, boost::function &callback) override; - void commanderGotLevel (const CCommanderInstance * commander, std::vector skills, boost::function &callback) override {}; //TODO + void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector &skills, boost::function &callback) override; + void commanderGotLevel (const CCommanderInstance * commander, std::vector skills, boost::function &callback) override; + void showBlockingDialog(const std::string &text, const std::vector &components, ui32 askID, const int soundID, bool selection, bool cancel) override; + void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function &onEnd) override; }; #define NAME "EmptyAI 0.1" diff --git a/lib/Connection.cpp b/lib/Connection.cpp index 151fb7f45..40e22291d 100644 --- a/lib/Connection.cpp +++ b/lib/Connection.cpp @@ -302,7 +302,8 @@ CLoadFile::~CLoadFile() int CLoadFile::read( const void * data, unsigned size ) { - sfile->read((char *)data,size); + char *bytePtr = (char *)data; + sfile->read(bytePtr, size); return size; } @@ -335,8 +336,18 @@ void CLoadFile::openNextFile(const std::string &fname, int minimalVersion) } if(myVersion > version) { - tlog1 << "Error: Too new file format! (file " << fname << " )\n"; - sfile.release(); + auto versionptr = (char*)&myVersion; + std::reverse(versionptr, versionptr + 4); + if(myVersion == version) + { + reverseEndianess = true; + tlog3 << fname << " seems to have different endianess!\n"; + } + else + { + tlog1 << "Error: Too new file format! (file " << fname << " )\n"; + sfile.release(); + } } } } diff --git a/lib/Connection.h b/lib/Connection.h index 9523f3ad4..6c64b22f9 100644 --- a/lib/Connection.h +++ b/lib/Connection.h @@ -687,6 +687,7 @@ public: bool saving; std::map loaders; // typeID => CPointerSaver ui32 myVersion; + bool reverseEndianess; //if source has different endianess than us, we reverse bytes std::map loadedPointers; bool smartPointerSerialization; @@ -696,6 +697,7 @@ public: saving = false; myVersion = version; smartPointerSerialization = true; + reverseEndianess = false; } ~CISer() @@ -758,7 +760,12 @@ public: template void loadPrimitive(T &data) { - this->This()->read(&data,sizeof(data)); + char * dataPtr = (char*)&data; + unsigned length = sizeof(data); + + this->This()->read(dataPtr,length); + if(reverseEndianess) + std::reverse(dataPtr, dataPtr + length); } template