Merge pull request #170 from diamante0018/insanely-fast-crit-sections

Fix DB_EnumXAssetEntries
This commit is contained in:
Dss0 2022-03-02 17:42:31 +01:00 committed by GitHub
commit 1216f10b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 26 deletions

View File

@ -199,7 +199,7 @@ namespace Assets
replacementFound = true;
}
}
}, false, false);
}, false);
if (!replacementFound)
{
@ -236,7 +236,7 @@ namespace Assets
replacementFound = true;
}
}
}, false, false);
}, false);
}
if (!replacementFound && asset->techniqueSet)
@ -250,28 +250,31 @@ namespace Assets
if (iw4TechSet)
{
Game::DB_EnumXAssetEntries(Game::XAssetType::ASSET_TYPE_MATERIAL, [asset, iw4TechSet](Game::XAssetEntry* entry)
{
if (!replacementFound)
{
if (!replacementFound)
Game::XAssetHeader header = entry->asset.header;
if (header.material->techniqueSet == iw4TechSet->asset.header.techniqueSet)
{
Game::XAssetHeader header = entry->asset.header;
Components::Logger::Print("Material %s with techset %s has been mapped to %s (last chance!), taking the sort key of material %s\n",
asset->info.name, asset->techniqueSet->name,
header.material->techniqueSet->name, header.material->info.name);
if (header.material->techniqueSet == iw4TechSet->asset.header.techniqueSet)
{
Components::Logger::Print("Material %s with techset %s has been mapped to %s (last chance!), taking the sort key of material %s\n", asset->info.name, asset->techniqueSet->name, header.material->techniqueSet->name, header.material->info.name);
asset->info.sortKey = header.material->info.sortKey;
asset->techniqueSet = iw4TechSet->asset.header.techniqueSet;
asset->info.sortKey = header.material->info.sortKey;
asset->techniqueSet = iw4TechSet->asset.header.techniqueSet;
// this is terrible!
asset->stateBitsCount = header.material->stateBitsCount;
asset->stateBitsTable = header.material->stateBitsTable;
std::memcpy(asset->stateBitsEntry, header.material->stateBitsEntry, 48);
asset->constantCount = header.material->constantCount;
asset->constantTable = header.material->constantTable;
// this is terrible!
asset->stateBitsCount = header.material->stateBitsCount;
asset->stateBitsTable = header.material->stateBitsTable;
std::memcpy(asset->stateBitsEntry, header.material->stateBitsEntry, 48);
asset->constantCount = header.material->constantCount;
asset->constantTable = header.material->constantTable;
replacementFound = true;
}
replacementFound = true;
}
}, false, false);
}
}, false);
if (!replacementFound)
{

View File

@ -981,7 +981,7 @@ namespace Components
replacementFound = true;
}
}
}, false, false);
}, false);
if (replacementFound) return ret;
return "";

View File

@ -339,6 +339,9 @@ namespace Game
Sys_SuspendOtherThreads_t Sys_SuspendOtherThreads = Sys_SuspendOtherThreads_t(0x45A190);
Sys_ListFiles_t Sys_ListFiles = Sys_ListFiles_t(0x45A660);
Sys_Milliseconds_t Sys_Milliseconds = Sys_Milliseconds_t(0x42A660);
Sys_LockWrite_t Sys_LockWrite = Sys_LockWrite_t(0x435880);
Sys_TempPriorityAtLeastNormalBegin_t Sys_TempPriorityAtLeastNormalBegin = Sys_TempPriorityAtLeastNormalBegin_t(0x478680);
Sys_TempPriorityEnd_t Sys_TempPriorityEnd = Sys_TempPriorityEnd_t(0x4DCF00);
TeleportPlayer_t TeleportPlayer = TeleportPlayer_t(0x496850);
@ -503,8 +506,22 @@ namespace Game
GraphFloat* aaInputGraph = reinterpret_cast<GraphFloat*>(0x7A2FC0);
FastCriticalSection* db_hashCritSect = reinterpret_cast<FastCriticalSection*>(0x16B8A54);
vec3_t* CorrectSolidDeltas = reinterpret_cast<vec3_t*>(0x739BB8); // Count 26
void Sys_LockRead(FastCriticalSection* critSect)
{
InterlockedIncrement(&critSect->readCount);
while (critSect->writeCount) std::this_thread::sleep_for(1ms);
}
void Sys_UnlockRead(FastCriticalSection* critSect)
{
assert(critSect->readCount > 0);
InterlockedDecrement(&critSect->readCount);
}
XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize)
{
int elSize = DB_GetXAssetSizeHandlers[type]();
@ -619,12 +636,9 @@ namespace Game
return false;
}
void DB_EnumXAssetEntries(XAssetType type, std::function<void(XAssetEntry*)> callback, bool overrides, bool lock)
void DB_EnumXAssetEntries(XAssetType type, std::function<void(XAssetEntry*)> callback, bool overrides)
{
volatile long* lockVar = reinterpret_cast<volatile long*>(0x16B8A54);
if (lock) InterlockedIncrement(lockVar);
while (lock && *reinterpret_cast<volatile long*>(0x16B8A58)) std::this_thread::sleep_for(1ms);
Sys_LockRead(db_hashCritSect);
const auto pool = Components::Maps::GetAssetEntryPool();
for(auto hash = 0; hash < 37000; hash++)
@ -653,7 +667,7 @@ namespace Game
}
}
if(lock) InterlockedDecrement(lockVar);
Sys_UnlockRead(db_hashCritSect);
}
// this cant be MessageBox because windows.h has a define that converts it to MessageBoxW. which is just stupid

View File

@ -813,6 +813,15 @@ namespace Game
typedef int(__cdecl * Sys_Milliseconds_t)();
extern Sys_Milliseconds_t Sys_Milliseconds;
typedef void(__cdecl * Sys_LockWrite_t)(FastCriticalSection* critSect);
extern Sys_LockWrite_t Sys_LockWrite;
typedef void(__cdecl * Sys_TempPriorityAtLeastNormalBegin_t)(TempPriority*);
extern Sys_TempPriorityAtLeastNormalBegin_t Sys_TempPriorityAtLeastNormalBegin;
typedef void(__cdecl * Sys_TempPriorityEnd_t)(TempPriority*);
extern Sys_TempPriorityEnd_t Sys_TempPriorityEnd;
typedef void(__cdecl * TeleportPlayer_t)(gentity_t* entity, float* pos, float* orientation);
extern TeleportPlayer_t TeleportPlayer;
@ -1040,6 +1049,11 @@ namespace Game
extern vec3_t* CorrectSolidDeltas;
extern FastCriticalSection* db_hashCritSect;
void Sys_LockRead(FastCriticalSection* critSect);
void Sys_UnlockRead(FastCriticalSection* critSect);
XAssetHeader ReallocateAssetPool(XAssetType type, unsigned int newSize);
void Menu_FreeItemMemory(Game::itemDef_s* item);
void Menu_SetNextCursorItem(Game::UiContext* ctx, Game::menuDef_t* currentMenu, int unk = 1);
@ -1053,7 +1067,7 @@ namespace Game
XAssetType DB_GetXAssetNameType(const char* name);
int DB_GetZoneIndex(const std::string& name);
bool DB_IsZoneLoaded(const char* zone);
void DB_EnumXAssetEntries(XAssetType type, std::function<void(XAssetEntry*)> callback, bool overrides, bool lock);
void DB_EnumXAssetEntries(XAssetType type, std::function<void(XAssetEntry*)> callback, bool overrides);
XAssetHeader DB_FindXAssetDefaultHeaderInternal(XAssetType type);
XAssetEntry* DB_FindXAssetEntry(XAssetType type, const char* name);

View File

@ -7174,6 +7174,19 @@ namespace Game
PM_EFF_STANCE_COUNT = 4
};
struct TempPriority
{
void* threadHandle;
int oldPriority;
};
struct FastCriticalSection
{
volatile long readCount;
volatile long writeCount;
TempPriority tempPriority;
};
#pragma endregion
#ifndef IDA