From 63cec317b439feca9d287b8ec0a33d10b7b2085e Mon Sep 17 00:00:00 2001
From: TheApadayo <muel96@gmail.com>
Date: Sat, 20 May 2017 14:41:04 -0400
Subject: [PATCH] [ZoneBuilder] Neuter startup more and add support for
 defaults.ff and shaders.ff shaders.ff should probably be re-generated to
 include every single techies including the SP ones. i'll do that later

---
 src/Components/Modules/ZoneBuilder.cpp | 69 ++++++++++++++++++++++++++
 src/Components/Modules/ZoneBuilder.hpp |  3 ++
 2 files changed, 72 insertions(+)

diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp
index 56d91699..777aa04f 100644
--- a/src/Components/Modules/ZoneBuilder.cpp
+++ b/src/Components/Modules/ZoneBuilder.cpp
@@ -791,6 +791,69 @@ namespace Components
 		}
 	}
 
+	
+	Game::XZoneInfo baseZones_old [] = { { "code_pre_gfx_mp", 0, 0 },
+		{ "localized_code_pre_gfx_mp", 0, 0 },
+		{ "code_post_gfx_mp", 0, 0 },
+		{ "localized_code_post_gfx_mp", 0, 0 },
+		{ "common_mp", 0, 0 }
+	};
+	
+
+	Game::XZoneInfo baseZones [] = {
+		{ "defaults", 0, 0 },
+		{ "shaders", 0, 0 },
+		{ "common_mp", 0, 0 }
+	};
+
+	void ZoneBuilder::EndInit()
+	{
+		// do other init stuff
+		DWORD R_RegisterDvars = 0x5196C0;
+		DWORD NET_Init = 0x491860;
+		DWORD SND_InitDriver = 0x4F5090;
+		DWORD SND_Init = 0x46A630;
+		DWORD G_SetupWeaponDef = 0x4E1F30;
+		__asm
+		{
+			call R_RegisterDvars
+			call NET_Init
+			call SND_InitDriver
+			call SND_Init
+		}
+
+		// now load default assets and shaders
+		if (FastFiles::Exists("defaults") && FastFiles::Exists("shaders"))
+		{
+			Game::DB_LoadXAssets(baseZones, 3, 0);
+		}
+		else
+		{
+			Logger::Print("Warning: Missing new init zones (defaults.ff & shaders.ff). You will need to load fastfiles to manually obtain techsets.\n");
+			Game::DB_LoadXAssets(baseZones_old, 5, 0);
+		}
+
+		Logger::Print("Waiting for fastiles to load...");
+		while (!Game::Sys_IsDatabaseReady()) std::this_thread::sleep_for(100ms);
+		Logger::Print("Done!\n");
+
+		// defaults need to load before we do this
+		__asm
+		{
+			call G_SetupWeaponDef
+		}
+	}
+
+	void __declspec(naked) ZoneBuilder::EndInitStub()
+	{
+		__asm
+		{
+			add esp, 0x14
+			call ZoneBuilder::EndInit
+			retn
+		}
+	}
+
 	ZoneBuilder::ZoneBuilder()
 	{
 		// ReSharper disable CppStaticAssertFailure
@@ -853,6 +916,12 @@ namespace Components
 			Utils::Hook::Set<DWORD>(0x64A029, 0x38400000); // 900 MiB
 			Utils::Hook::Set<DWORD>(0x64A057, 0x38400000);
 
+			// Further neuter Com_Init
+			Utils::Hook(0x60BBCE, ZoneBuilder::EndInitStub, HOOK_JUMP).install()->quick();
+
+			// dont run Live_Frame
+			Utils::Hook::Nop(0x48A0AF, 5);
+
 			AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader /*asset*/, std::string name, bool* /*restrict*/)
 			{
 				if (!ZoneBuilder::TraceZone.empty() && ZoneBuilder::TraceZone == FastFiles::Current())
diff --git a/src/Components/Modules/ZoneBuilder.hpp b/src/Components/Modules/ZoneBuilder.hpp
index a59edf35..69137abd 100644
--- a/src/Components/Modules/ZoneBuilder.hpp
+++ b/src/Components/Modules/ZoneBuilder.hpp
@@ -114,5 +114,8 @@ namespace Components
 		static Utils::Memory::Allocator MemAllocator;
 		static int StoreTexture(Game::GfxImageLoadDef **loadDef, Game::GfxImage *image);
 		static void ReleaseTexture(Game::XAssetHeader header);
+
+		static void EndInitStub();
+		static void EndInit();
 	};
 }