Skip to content

Commit

Permalink
Add materials limit extender
Browse files Browse the repository at this point in the history
  • Loading branch information
2010kohtep committed Jun 10, 2020
1 parent b3a7d3f commit edbbd13
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 1 deletion.
65 changes: 64 additions & 1 deletion StdPatch/src/StdPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void LoadConfig()
if (FileExists(szBuf))
{
g_nMAXSTUDIOVERTS_NEW = GetPrivateProfileIntA("Main", "MaxStudioVerts", (1024 * 512), szBuf);
g_nMAXMATERIALSCOUNT_NEW = GetPrivateProfileIntA("Main", "MaxMaterials", 127, szBuf);
g_nBUFFERSIZE_NEW = GetPrivateProfileIntA("Main", "BufferSize", (1024 * 1024 * 32), szBuf);
g_nMAXFLEXCONTROLLER_NEW = GetPrivateProfileIntA("Main", "FlexControllerSize", 400, szBuf);
}
Expand All @@ -30,9 +31,16 @@ void LoadConfig()
DbgTrace("Warning! 'StdPatch.ini' configuration file could not be found, creating one with default values for patching.\n");

WritePrivateProfileStringA("Main", "MaxStudioVerts", "524288", szBuf);
WritePrivateProfileStringA("Main", "MaxMaterials", "127", szBuf);
WritePrivateProfileStringA("Main", "BufferSize", "33554432", szBuf);
WritePrivateProfileStringA("Main", "FlexControllerSize", "400", szBuf);
}

if (g_nMAXMATERIALSCOUNT_NEW > 127)
{
DbgTrace("Warning! Materials limit can't be more than 127, but received %d. Setting to 127...\n", g_nMAXMATERIALSCOUNT_NEW);
g_nMAXMATERIALSCOUNT_NEW = 127;
}
}

bool IsSFM()
Expand All @@ -55,11 +63,58 @@ VOID WINAPI hkOutputDebugStringA(LPCSTR lpOutputString)
void Hook_OutputDebugStringA()
{
orgOutputDebugStringA = decltype(orgOutputDebugStringA)(gKernelDll->HookExport("OutputDebugStringA", hkOutputDebugStringA));
}

bool Find_MaterialsList()
{
ISearchPattern *pattern;

pattern = gStudioExe->CreatePattern(g_pMaterialsList);
{
pattern->FindAnsiString("face %d references NULL texture %d\n", kPatternFlagsStringRef);
pattern->FindCall(0, true, true);
pattern->FindUInt16(0x868D);
pattern->Transpose(2);
pattern->Dereference();
}

if (!g_pMaterialsList)
return false;

pattern = gStudioExe->CreatePattern(g_pMaterialsListCheck);
{
pattern->FindAnsiString("Too many materials used, max %d\n", kPatternFlagsStringRef);
}

if (!g_pMaterialsListCheck)
return false;

g_nMAXMATERIALSCOUNT_DEF = *(uint8_t *)Transpose(g_pMaterialsListCheck, -2);

return true;
}

bool Patch_MaterialsList()
{
if (!g_pMaterialsList || !g_pMaterialsListCheck)
return false;

g_MaterialsList.SetLength(g_nMAXMATERIALSCOUNT_NEW);

if (gStudioExe->HookRefAddr(g_pMaterialsList, g_MaterialsList.GetData(), 0x00) == 0)
{
g_MaterialsList.SetLength(0);
return false;
}

WritePrimitive<uint8_t>(g_pMaterialsListCheck, g_nMAXMATERIALSCOUNT_NEW, -2);
WritePrimitive<uint8_t>(g_pMaterialsListCheck, g_nMAXMATERIALSCOUNT_NEW, -6);
return true;
}

void PatchStudioMdl()
{
DbgTrace("StudioMdl Patcher 2.2.0 is started.\n");
DbgTrace("StudioMdl Patcher 2.3.0 is started.\n");
DbgTrace("Code by Alexander B. (2010kohtep) special for RED_EYE.\n");

FindModules();
Expand Down Expand Up @@ -123,6 +178,14 @@ void PatchStudioMdl()
if (!Patch_SanityCheckVertexBoneLODFlags())
FailedToFind("SanityCheckVertexBoneLODFlags()");

if (!Find_MaterialsList())
FailedToPatch("MaterialsList");
else
{
if (!Patch_MaterialsList())
FailedToHook("MaterialsList");
}

DbgTrace("\n");
}

Expand Down
6 changes: 6 additions & 0 deletions StdPatch/src/main/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ IModule *gKernelDll;
int g_nMAXSTUDIOVERTS_NEW = 0x00F00000; // def - $80000
int g_nBUFFERSIZE_NEW = 0x02000000; // def - $2000000
int g_nMAXFLEXCONTROLLER_NEW = 0x00000400; // def - $80
int g_nMAXMATERIALSCOUNT_NEW = 0x0000007F; // def - $20

int g_nMAXSTUDIOVERTS_DEF;
int g_nBUFFERSIZE_DEF;
int g_nMAXFLEXCONTROLLER_DEF;
int g_nMAXMATERIALSCOUNT_DEF;

CArrayHelper<TFlexController> g_FlexControllerNew;
CArrayHelper<TVUnify *> g_VerticesPtrsNew;
CArrayHelper<TVUnify> g_VerticesDataNew;
CArrayHelper<TWeightList> g_WeightList;
CArrayHelper<TMaterialInfo> g_MaterialsList;

void *g_pBUFFERSIZE;
void *g_pMAXSTUDIOVERTS;
void *g_pMAXFLEXCONTROLLER;
void *g_pMAXMATERIALSCOUNT;

TAddToVlist g_pfnAddToVlist;
TIsInt24 g_pfnIsInt24;

void *g_pVList;
void *g_pMaterialsList;
void *g_pMaterialsListCheck;
void *g_pFlexController;
6 changes: 6 additions & 0 deletions StdPatch/src/main/Global.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@ extern IModule *gKernelDll;
extern int g_nMAXSTUDIOVERTS_NEW;
extern int g_nBUFFERSIZE_NEW;
extern int g_nMAXFLEXCONTROLLER_NEW;
extern int g_nMAXMATERIALSCOUNT_NEW;

extern int g_nMAXSTUDIOVERTS_DEF;
extern int g_nBUFFERSIZE_DEF;
extern int g_nMAXFLEXCONTROLLER_DEF;
extern int g_nMAXMATERIALSCOUNT_DEF;

extern CArrayHelper<TFlexController> g_FlexControllerNew;
extern CArrayHelper<TVUnify *> g_VerticesPtrsNew;
extern CArrayHelper<TVUnify> g_VerticesDataNew;
extern CArrayHelper<TWeightList> g_WeightList;
extern CArrayHelper<TMaterialInfo> g_MaterialsList;

extern void *g_pBUFFERSIZE;
extern void *g_pMAXSTUDIOVERTS;
extern void *g_pMAXFLEXCONTROLLER;
extern void *g_pMAXMATERIALSCOUNT;

using TAddToVlist = void *(__cdecl *)(int a1, int a2, int a3, int a4);
using TIsInt24 = bool(__cdecl *)(int nValue);
Expand All @@ -32,4 +36,6 @@ extern TAddToVlist g_pfnAddToVlist;
extern TIsInt24 g_pfnIsInt24;

extern void *g_pVList;
extern void *g_pMaterialsList;
extern void *g_pMaterialsListCheck;
extern void *g_pFlexController;
5 changes: 5 additions & 0 deletions StdPatch/src/main/SDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ struct TotalMeshStats_t
int m_TotalTopology;
int m_TotalBoneStateChanges;
int m_TotalMaterialReplacements;
};

struct TMaterialInfo
{
unsigned char dummy[288];
};

0 comments on commit edbbd13

Please sign in to comment.