From 46950125716c05171f25a12d8805811112e90551 Mon Sep 17 00:00:00 2001
From: Amos <48657826+Mauler125@users.noreply.github.com>
Date: Wed, 29 Dec 2021 13:58:03 +0100
Subject: [PATCH] Fix 'C_PropDoor' not spawning properly on the server

---
 r5dev/common/opcodes.cpp | 13 ++++++++++---
 r5dev/common/opcodes.h   |  1 +
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/r5dev/common/opcodes.cpp b/r5dev/common/opcodes.cpp
index 124f2278..a3e9c348 100644
--- a/r5dev/common/opcodes.cpp
+++ b/r5dev/common/opcodes.cpp
@@ -18,19 +18,26 @@ void NoShaderApi()
 	gCShaderSystem__Init.Patch({ 0xC3 });                                                         // FUN --> RET | Return early in 'CShaderSystem::Init' to prevent initialization.
 
 	gCGameServer__SpawnServer.Offset(0x43).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });               // CAL --> NOP | Prevent call to unknown materia;/shader code.
-	//gCGameServer__SpawnServer.Offset(0x48).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });               // TODO: Research 'CIVDebugOverlay'.
+	gCGameServer__SpawnServer.Offset(0x48).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });               // TODO: Research 'CIVDebugOverlay'.
 
 	CStudioRenderContext__LoadMaterials.Offset(0x28).Patch({ 0xE9, 0x80, 0x04, 0x00, 0x00 });     // FUN --> RET | 'CStudioRenderContext::LoadMaterials' is called virtually by the 'RMDL' streaming job.
 
 	// Note: The registers here seems to contains pointers to material data and 'CMaterial' class methods when the shader system is initialized.
 	gCStudioRenderContext__LoadModel.Offset(0x17D).Patch({ 0x90, 0x90, 0x90, 0x90 });             // MOV --> NOP | RAX + RCX are both nullptrs.
-	gCStudioRenderContext__LoadModel.Offset(0x181).Patch({ 0x90, 0x90, 0x90 });                   // MOV --> NOP | RCX is nullptr when trying to move to RAX.
+	gCStudioRenderContext__LoadModel.Offset(0x181).Patch({ 0x90, 0x90, 0x90 });                   // MOV --> NOP | RCX is nullptr when trying to dereference.
 	gCStudioRenderContext__LoadModel.Offset(0x184).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | RAX is nullptr during virtual call resulting in exception 'C0000005'.
 
 	CollisionBSPData_LoadAllLumps.Offset(0x1045).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });         // CAL --> NOP | Prevent call to 'Mod_LoadCubemapSamples()'
 
 	LoadModel.Offset(0x462).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });                              // CAL --> NOP | Prevent call to 'CStudioRenderContext::LoadMaterials'.
-	LoadModel.Offset(0x6FE).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });                              // CAL --> NOP | Unknown material/texture code.
+	//LoadModel.Offset(0x6FE).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });                              // CAL --> NOP | Unknown material/texture code.
+
+	CModelLoader__Sprite_LoadModel.Offset(0x325).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | Virtual call to 'CMaterialSystem::FindMaterialEx' fails as RAX is nullptr.
+	CModelLoader__Sprite_LoadModel.Offset(0x33D).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | Virtual call to 'CMaterialGlue' class method fails as RAX is nullptr.
+	CModelLoader__Sprite_LoadModel.Offset(0x359).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | Virtual call to 'CMaterialGlue' class method fails as RAX is nullptr.
+	CModelLoader__Sprite_LoadModel.Offset(0x374).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | Virtual call to 'CMaterialGlue' class method fails as RAX is nullptr.
+	CModelLoader__Sprite_LoadModel.Offset(0x38D).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | Virtual call to 'ReturnZero' fails as RAX is nullptr.
+	CModelLoader__Sprite_LoadModel.Offset(0x3A4).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }); // CAL --> NOP | Virtual call to 'CMaterialGlue' class method fails as RAX is nullptr.
 
 	// Note: At [14028F3B0 + 0x5C7] RSP seems to contain a block of pointers to data for the static prop rmdl in question. [RSP + 0x70] is a pointer to (what seems to be) shader/material data. The pointer will be NULL without a shader system.
 	p_CalcPropStaticFrustumCulling.Offset(0x5E0).Patch({ 0x90, 0x90, 0x90, 0x90, 0x90 });         // MOV --> NOP | RSP + 0x70 is a nullptr which gets moved to R13, R13 gets used here resulting in exception 'C0000005'.
diff --git a/r5dev/common/opcodes.h b/r5dev/common/opcodes.h
index 8f39767e..2c1039c9 100644
--- a/r5dev/common/opcodes.h
+++ b/r5dev/common/opcodes.h
@@ -91,6 +91,7 @@ namespace
 
 		ADDRESS ParsePropStatic = 0x1402901E0;
 
+		ADDRESS CModelLoader__Sprite_LoadModel = 0x140252F10;
 
 
 		ADDRESS e9 = 0x00000001404066E0;