Skip to content

Commit

Permalink
hash lookup traits & ancillaries
Browse files Browse the repository at this point in the history
  • Loading branch information
FynnTW committed Oct 8, 2024
1 parent d635e80 commit 75cdd9a
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 62 deletions.
4 changes: 4 additions & 0 deletions M2TWEOP Code/M2TWEOP library/dataOffsets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void dataOffsets::initDataOffsets(int gameVer)
offsets.eventManager = 0x16F0BC8;
offsets.uiStratUiV2 = 0x2CB62FC;
offsets.currentRegion = 0x18FD970;
offsets.traitDb = 0x1666F90;
offsets.ancillaryDb = 0x1666F40;


offsets.fortVtbl = 0x13362F4;
Expand Down Expand Up @@ -145,6 +147,8 @@ void dataOffsets::initDataOffsets(int gameVer)
offsets.perfectSpy = 0x016A7CC4;

offsets.portraitDatabase = 0x018671D0;
offsets.traitDb = 0x161F0B8;
offsets.ancillaryDb = 0x161F068;

offsets.cultureDatabase = 0x0161F0F0;
offsets.loadGameHandler = 0x02C6DC60;
Expand Down
2 changes: 2 additions & 0 deletions M2TWEOP Code/M2TWEOP library/dataOffsets.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class dataOffsets
DWORD edbDataStart = NULL;
DWORD religionDatabase = NULL;
DWORD portraitDatabase = NULL;
DWORD traitDb = NULL;
DWORD ancillaryDb = NULL;
DWORD cultureDatabase = NULL;
DWORD loadGameHandler = NULL;
DWORD selectedEra = NULL;
Expand Down
17 changes: 17 additions & 0 deletions M2TWEOP Code/M2TWEOP library/luaPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,5 +1017,22 @@ void luaPlugin::fillHashMapsNonCampaign()
buildingLevelLines.insert_or_assign(std::string(level->name), building->buildingID);
}
}

const auto traitsDb = characterRecordHelpers::getTraitDb();
const int traitNum = traitsDb->traitsNum;
for (int i = 0; i < traitNum; i++)
{
const auto trait = &traitsDb->traits[i];
traits.insert_or_assign(std::string(trait->name), i);
}

const auto ancDb = characterRecordHelpers::getAncillaryDb();
const int ancNum = ancDb->ancillariesNum;
for (int i = 0; i < ancNum; i++)
{
const auto anc = &ancDb->ancillaries[i];
ancillaries.insert_or_assign(std::string(anc->ancName), i);
}

hashNonCampaignLoaded = true;
}
4 changes: 4 additions & 0 deletions M2TWEOP Code/M2TWEOP library/luaPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ class luaPlugin
};
std::unordered_map<std::string, int> buildingLevels = {
};
std::unordered_map<std::string, int> traits = {
};
std::unordered_map<std::string, int> ancillaries = {
};
sol::function* onCampaignMapLoaded = nullptr;
sol::function* onRemoveFromUnitQueue = nullptr;
sol::function* onReadGameDbsAtStart = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion M2TWEOP Code/M2TWEOP library/patchesForGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ void __stdcall patchesForGame::onGameInit()
cultures::eopPortraitDb::createEopPortraitDb();
discordManager::menuLoaded();
gameEvents::onGameInit();
const auto edb = eopBuildings::getEdb();
*reinterpret_cast<bool*>(dataOffsets::offsets.bugReport) = true;
}

void __stdcall patchesForGame::onUnloadCampaign()
Expand Down
97 changes: 36 additions & 61 deletions M2TWEOP Code/M2TWEOP library/types/characterRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "cultures.h"
#include "faction.h"
#include "gameHelpers.h"
#include "luaPlugin.h"
#include "stratModelsChange.h"
#include "techFuncs.h"
#include "unit.h"
Expand Down Expand Up @@ -191,13 +192,18 @@ namespace characterRecordHelpers
{
if (ancName.empty())
return nullptr;
return GAME_FUNC(ancillary*(__cdecl*)(const char*), findAncillary)(ancName.c_str());
if (plugData::data.luaAll.ancillaries.empty())
plugData::data.luaAll.fillHashMapsNonCampaign();
const auto ancDb = getAncillaryDb();
if (const auto ancIndex = plugData::data.luaAll.ancillaries.find(ancName); ancIndex != plugData::data.luaAll.ancillaries.end())
return &ancDb->ancillaries[ancIndex->second];
return nullptr;
}

int addAncillaryName(characterRecord* character, const std::string& ancName)
{
ancillary* anc = findAncillary(ancName);
if (anc == nullptr)
if (!anc)
return 0;
return addAncillary(character, anc);
}
Expand Down Expand Up @@ -331,77 +337,46 @@ namespace characterRecordHelpers
return &level->effects[index];
}

traitDb* getTraitDb()
{
return reinterpret_cast<traitDb*>(dataOffsets::offsets.traitDb);
}

ancillaryDb* getAncillaryDb()
{
return reinterpret_cast<ancillaryDb*>(dataOffsets::offsets.ancillaryDb);
}

traitEntry* findTrait(const std::string& name)
{
const auto hashed = reinterpret_cast<stringWithHash*>(gameStringHelpers::createHashedString(name.c_str()));
return GAME_FUNC(traitEntry*(__cdecl*)(const char*, int), getTrait)(hashed->name, hashed->hash);
if (plugData::data.luaAll.traits.empty())
plugData::data.luaAll.fillHashMapsNonCampaign();
const auto traitsDb = getTraitDb();
if (const auto traitIndex = plugData::data.luaAll.traits.find(name); traitIndex != plugData::data.luaAll.traits.end())
return &traitsDb->traits[traitIndex->second];
return nullptr;
}

void addTrait(characterRecord* character, const char* traitName, int traitLevel)
{
DWORD adrFunc = codes::offsets.getTrait;
char** cryptS = gameStringHelpers::createHashedString(traitName);
void* resTrait = nullptr;
auto oneArg = cryptS[0];
auto nextArg = cryptS[1];
_asm
{
push nextArg
push oneArg

mov eax, adrFunc
call eax

mov resTrait, eax
add esp, 0x8
}
if (resTrait == nullptr)
const auto traitsDb = getTraitDb();
traitEntry* trait = nullptr;
if (const auto traitIndex = plugData::data.luaAll.traits.find(traitName); traitIndex != plugData::data.luaAll.traits.end())
trait = &traitsDb->traits[traitIndex->second];
if (!trait)
return;

//set trait
adrFunc = codes::offsets.addTrait;
_asm
{
push 1
push traitLevel
push resTrait
mov ecx, character
mov eax, adrFunc
call eax
}
GAME_FUNC(void(__thiscall*)(characterRecord*, traitEntry*, int, bool), addTrait)(character, trait, traitLevel, true);
}

void removeTrait(characterRecord* character, const char* traitName)
{
DWORD adrFunc = codes::offsets.getTrait;
char** cryptS = gameStringHelpers::createHashedString(traitName);
void* resTrait = nullptr;
auto oneArg = cryptS[0];
auto nextArg = cryptS[1];
_asm
{
push nextArg
push oneArg

mov eax, adrFunc
call eax

mov resTrait, eax
add esp, 0x8
}
if (resTrait == nullptr)
const auto traitsDb = getTraitDb();
traitEntry* trait = nullptr;
if (const auto traitIndex = plugData::data.luaAll.traits.find(traitName); traitIndex != plugData::data.luaAll.traits.end())
trait = &traitsDb->traits[traitIndex->second];
if (!trait)
return;

//set trait
adrFunc = codes::offsets.removeTrait;
_asm
{
push resTrait
mov ecx, character

mov eax, adrFunc
call eax
}
GAME_FUNC(void(__thiscall*)(characterRecord*, traitEntry*), removeTrait)(character, trait);
}
#pragma endregion Trait helpers

Expand Down
18 changes: 18 additions & 0 deletions M2TWEOP Code/M2TWEOP library/types/characterRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ struct traitEntry { /* char* at 0x4 */
}
};

struct traitDb
{
traitEntry* traits;
int traitsSize;
int traitsNum;
};

struct ancillaryDb
{
public:
struct ancillary *ancillaries; //0x0000
int32_t ancillariesSize; //0x0004
int32_t ancillariesNum; //0x0008
}; //Size: 0x129F0


struct traitContainer {
struct trait* trait;
struct traitContainer* prev;
Expand Down Expand Up @@ -527,6 +543,8 @@ namespace characterRecordHelpers
void namedCharSetLocalizedNicknameForSave(characterRecord* genChar, const char* str);
void setHeir(characterRecord* gen, bool isJustSet);
std::string getEopSetModel();
traitDb* getTraitDb();
ancillaryDb* getAncillaryDb();

int addAncillaryName(characterRecord* character, const std::string& ancName);
int addAncillary(characterRecord* character, ancillary* anc);
Expand Down

0 comments on commit 75cdd9a

Please sign in to comment.