From 4dc912af3ea6bc0920edce63ec6aa6a8c0675e8a Mon Sep 17 00:00:00 2001 From: TheApadayo Date: Wed, 16 Jan 2019 23:52:42 -0500 Subject: [PATCH 1/5] [Menus] Start working on loading custom menus Currently doesn't work to override menus so I'm going to have to rewrite some stuff --- .../Modules/AssetInterfaces/IMenuList.cpp | 10 ++ .../Modules/AssetInterfaces/IMenuList.hpp | 2 +- .../Modules/AssetInterfaces/ImenuDef_t.cpp | 16 +++ .../Modules/AssetInterfaces/ImenuDef_t.hpp | 4 +- src/Components/Modules/Menus.cpp | 124 +++++++++++++++++- src/Components/Modules/Menus.hpp | 8 +- src/Components/Modules/ZoneBuilder.cpp | 4 + 7 files changed, 160 insertions(+), 8 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IMenuList.cpp b/src/Components/Modules/AssetInterfaces/IMenuList.cpp index a66ddee3..f8a25f96 100644 --- a/src/Components/Modules/AssetInterfaces/IMenuList.cpp +++ b/src/Components/Modules/AssetInterfaces/IMenuList.cpp @@ -2,6 +2,16 @@ namespace Assets { + void IMenuList::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) + { + header->menuList = Components::Menus::LoadCustomMenuList(name, builder->getAllocator()); + + for (int i = 0; i < header->menuList->menuCount; i++) + { + ImenuDef_t::LoadedMenus[header->menuList->menus[i]->window.name] = header->menuList->menus[i]; + } + + } void IMenuList::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) { Game::MenuList *asset = header.menuList; diff --git a/src/Components/Modules/AssetInterfaces/IMenuList.hpp b/src/Components/Modules/AssetInterfaces/IMenuList.hpp index 005fa820..fd0a72f9 100644 --- a/src/Components/Modules/AssetInterfaces/IMenuList.hpp +++ b/src/Components/Modules/AssetInterfaces/IMenuList.hpp @@ -9,6 +9,6 @@ namespace Assets virtual void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; virtual void mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; - // virtual void load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder) override; + virtual void load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) override; }; } diff --git a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp index e9bd1eb3..2aa142f4 100644 --- a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp +++ b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp @@ -2,6 +2,22 @@ namespace Assets { + + std::unordered_map ImenuDef_t::LoadedMenus; + + void ImenuDef_t::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) + { + // search menus loaded by a menufile + for (auto i = ImenuDef_t::LoadedMenus.begin(); i != ImenuDef_t::LoadedMenus.end(); ++i) + { + if (i->first == name) { + header->menu = i->second; + return; + } + } + } + + void ImenuDef_t::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) { Game::menuDef_t *asset = header.menu; diff --git a/src/Components/Modules/AssetInterfaces/ImenuDef_t.hpp b/src/Components/Modules/AssetInterfaces/ImenuDef_t.hpp index e62b15ea..b9e38cc4 100644 --- a/src/Components/Modules/AssetInterfaces/ImenuDef_t.hpp +++ b/src/Components/Modules/AssetInterfaces/ImenuDef_t.hpp @@ -9,7 +9,9 @@ namespace Assets virtual void save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; virtual void mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) override; - // virtual void load(Game::XAssetHeader* header, std::string name, Components::ZoneBuilder::Zone* builder) override; + virtual void load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) override; + + static std::unordered_map LoadedMenus; private: template void save_windowDef_t(Game::windowDef_t* asset, T* dest, Components::ZoneBuilder::Zone* builder) diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index cfd06bb3..9b9ff7e4 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -173,6 +173,69 @@ namespace Components return menu; } + Game::MenuList* Menus::LoadCustomMenuList(const std::string& menu, Utils::Memory::Allocator* allocator) + { + std::vector> menus; + FileSystem::File menuFile(menu); + + if (!menuFile.exists()) return nullptr; + + Game::pc_token_t token; + int handle = Menus::LoadMenuSource(menu, menuFile.getBuffer()); + + if (Menus::IsValidSourceHandle(handle)) + { + while (true) + { + ZeroMemory(&token, sizeof(token)); + + if (!Game::PC_ReadTokenHandle(handle, &token) || token.string[0] == '}') + { + break; + } + + if (!_stricmp(token.string, "loadmenu")) + { + Game::PC_ReadTokenHandle(handle, &token); + + Utils::Merge(&menus, Menus::LoadMenu(Utils::String::VA("ui_mp\\%s.menu", token.string))); + } + + if (!_stricmp(token.string, "menudef")) + { + Game::menuDef_t* menudef = Menus::ParseMenu(handle); + if (menudef) menus.push_back({ true, menudef }); // Custom menu + } + } + + Menus::FreeMenuSource(handle); + } + + if (menus.empty()) return nullptr; + + // Allocate new menu list + Game::MenuList* list = allocator->allocate(); + if (!list) return nullptr; + + list->menus = allocator->allocateArray(menus.size()); + if (!list->menus) + { + allocator->free(list); + return nullptr; + } + + list->name = allocator->duplicateString(menu); + list->menuCount = menus.size(); + + // Copy new menus + for (unsigned int i = 0; i < menus.size(); ++i) + { + list->menus[i] = menus[i].second; + } + + return list; + } + std::vector> Menus::LoadMenu(const std::string& menu) { std::vector> menus; @@ -580,12 +643,12 @@ namespace Components Menus::MenuList.clear(); } - Game::XAssetHeader Menus::MenuLoad(Game::XAssetType /*type*/, const std::string& filename) + Game::XAssetHeader Menus::MenuFindHook(Game::XAssetType /*type*/, const std::string& filename) { return { Game::Menus_FindByName(Game::uiContext, filename.data()) }; } - Game::XAssetHeader Menus::MenuFileLoad(Game::XAssetType type, const std::string& filename) + Game::XAssetHeader Menus::MenuListFindHook(Game::XAssetType type, const std::string& filename) { Game::XAssetHeader header = { nullptr }; @@ -694,6 +757,54 @@ namespace Components Menus::CustomMenus.push_back(menu); } + void Menus::RegisterCustomMenusHook() + { + Game::UiContext* uiInfoArray = (Game::UiContext*)0x62E2858; + Game::MenuList list; + + Utils::Hook::Call(0x401700)(); // call original load functions + Game::XAssetHeader header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/iw4x.txt"); + if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) + { + // Utils::Hook::Call(0x401700)(uiInfoArray, header.menuList, 1); // add loaded menus + std::memcpy(&list, header.data, sizeof(Game::MenuList)); + + for (int i = 0; i < uiInfoArray->menuCount; i++) + { + for (int j = 0; j < list.menuCount; j++) + { + if (!list.menus[j]) continue; // skip already used entries + if (!stricmp(list.menus[j]->window.name, uiInfoArray->Menus[i]->window.name)) + { + uiInfoArray->Menus[i] = list.menus[j]; // overwrite UiContext pointer + list.menus[j] = nullptr; // clear entries that already exist so we don't add them later + } + } + } + + for (int i = 0; i < list.menuCount; i++) + { + if (list.menus[i]) + { + uiInfoArray->Menus[uiInfoArray->menuCount++] = list.menus[i]; + } + } + } + + for (int i = 0; i < uiInfoArray->menuCount; i++) + { + OutputDebugStringA(Utils::String::VA("%s\n", uiInfoArray->Menus[i]->window.name)); + } + + /* + header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/mod.txt"); + if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) + { + Utils::Hook::Call(0x401700)(uiInfoArray, header.menuList, 1); // add loaded menus + } + */ + } + Menus::Menus() { if (Dedicated::IsEnabled()) return; @@ -702,12 +813,15 @@ namespace Components Menus::FreeEverything(); // Intercept asset finding - AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENU, Menus::MenuLoad); - AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENULIST, Menus::MenuFileLoad); + AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENU, Menus::MenuFindHook); + AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENULIST, Menus::MenuListFindHook); // Don't open connect menu //Utils::Hook::Nop(0x428E48, 5); + // register custom menufiles if they exist + Utils::Hook(0x4A58C3, Menus::RegisterCustomMenusHook, HOOK_CALL).install()->quick(); + // Use the connect menu open call to update server motds Utils::Hook(0x428E48, []() { @@ -779,6 +893,7 @@ namespace Components }); // Define custom menus here + /* Menus::Add("ui_mp/changelog.menu"); Menus::Add("ui_mp/theater_menu.menu"); Menus::Add("ui_mp/pc_options_multi.menu"); @@ -793,6 +908,7 @@ namespace Components Menus::Add("ui_mp/pc_store.menu"); Menus::Add("ui_mp/iw4x_credits.menu"); Menus::Add("ui_mp/resetclass.menu"); + */ } Menus::~Menus() diff --git a/src/Components/Modules/Menus.hpp b/src/Components/Modules/Menus.hpp index efc051e2..151b5e0a 100644 --- a/src/Components/Modules/Menus.hpp +++ b/src/Components/Modules/Menus.hpp @@ -15,13 +15,15 @@ namespace Components static void Add(const std::string& menu); + static Game::MenuList* Menus::LoadCustomMenuList(const std::string& menu, Utils::Memory::Allocator* allocator); + private: static std::unordered_map MenuList; static std::unordered_map MenuListList; static std::vector CustomMenus; - static Game::XAssetHeader MenuLoad(Game::XAssetType type, const std::string& filename); - static Game::XAssetHeader MenuFileLoad(Game::XAssetType type, const std::string& filename); + static Game::XAssetHeader MenuFindHook(Game::XAssetType type, const std::string& filename); + static Game::XAssetHeader MenuListFindHook(Game::XAssetType type, const std::string& filename); static Game::MenuList* LoadMenuList(Game::MenuList* menuList); static Game::MenuList* LoadScriptMenu(const char* menu); @@ -53,6 +55,8 @@ namespace Components static void RemoveMenuFromContext(Game::UiContext *dc, Game::menuDef_t *menu); + static void RegisterCustomMenusHook(); + // Ugly! static int KeywordHash(char* key); }; diff --git a/src/Components/Modules/ZoneBuilder.cpp b/src/Components/Modules/ZoneBuilder.cpp index dd07177c..ece47840 100644 --- a/src/Components/Modules/ZoneBuilder.cpp +++ b/src/Components/Modules/ZoneBuilder.cpp @@ -871,6 +871,10 @@ namespace Components // defaults need to load before we do this Utils::Hook::Call(0x4E1F30)(); // G_SetupWeaponDef + Utils::Hook::Call(0x4454C0)(); // Item_SetupKeywordHash (for loading menus) + Utils::Hook::Call(0x501BC0)(); // Menu_SetupKeywordHash (for loading menus) + Utils::Hook::Call(0x4A1280)(); // something related to uiInfoArray + Utils::Hook::Call(0x464A90)(GetCommandLineA()); // Com_ParseCommandLine Utils::Hook::Call(0x60C3D0)(); // Com_AddStartupCommands From 8080700432e505dd5399b95d42b8d9a1846269a1 Mon Sep 17 00:00:00 2001 From: TheApadayo Date: Thu, 17 Jan 2019 16:33:26 -0500 Subject: [PATCH 2/5] [Menus] Rewrite how menus are loaded and used to support custom menu lists --- .../Modules/AssetInterfaces/IMenuList.cpp | 2 +- src/Components/Modules/Menus.cpp | 580 ++++-------------- src/Components/Modules/Menus.hpp | 67 +- src/Game/Functions.cpp | 1 + src/Game/Functions.hpp | 3 + 5 files changed, 162 insertions(+), 491 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IMenuList.cpp b/src/Components/Modules/AssetInterfaces/IMenuList.cpp index f8a25f96..9f04195f 100644 --- a/src/Components/Modules/AssetInterfaces/IMenuList.cpp +++ b/src/Components/Modules/AssetInterfaces/IMenuList.cpp @@ -4,7 +4,7 @@ namespace Assets { void IMenuList::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) { - header->menuList = Components::Menus::LoadCustomMenuList(name, builder->getAllocator()); + header->menuList = Components::Menus::LoadMenuList(name); for (int i = 0; i < header->menuList->menuCount; i++) { diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index 9b9ff7e4..48a332bf 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -2,9 +2,9 @@ namespace Components { - std::vector Menus::CustomMenus; - std::unordered_map Menus::MenuList; - std::unordered_map Menus::MenuListList; + std::unordered_map Menus::DiskMenuList; + std::unordered_map Menus::DiskMenuListList; + std::unordered_map> Menus::UiContextMenus; int Menus::ReserveSourceHandle() { @@ -166,76 +166,9 @@ namespace Components } } - Menus::OverrideMenu(menu); - Menus::RemoveMenu(menu->window.name); - Menus::MenuList[menu->window.name] = menu; - return menu; } - Game::MenuList* Menus::LoadCustomMenuList(const std::string& menu, Utils::Memory::Allocator* allocator) - { - std::vector> menus; - FileSystem::File menuFile(menu); - - if (!menuFile.exists()) return nullptr; - - Game::pc_token_t token; - int handle = Menus::LoadMenuSource(menu, menuFile.getBuffer()); - - if (Menus::IsValidSourceHandle(handle)) - { - while (true) - { - ZeroMemory(&token, sizeof(token)); - - if (!Game::PC_ReadTokenHandle(handle, &token) || token.string[0] == '}') - { - break; - } - - if (!_stricmp(token.string, "loadmenu")) - { - Game::PC_ReadTokenHandle(handle, &token); - - Utils::Merge(&menus, Menus::LoadMenu(Utils::String::VA("ui_mp\\%s.menu", token.string))); - } - - if (!_stricmp(token.string, "menudef")) - { - Game::menuDef_t* menudef = Menus::ParseMenu(handle); - if (menudef) menus.push_back({ true, menudef }); // Custom menu - } - } - - Menus::FreeMenuSource(handle); - } - - if (menus.empty()) return nullptr; - - // Allocate new menu list - Game::MenuList* list = allocator->allocate(); - if (!list) return nullptr; - - list->menus = allocator->allocateArray(menus.size()); - if (!list->menus) - { - allocator->free(list); - return nullptr; - } - - list->name = allocator->duplicateString(menu); - list->menuCount = menus.size(); - - // Copy new menus - for (unsigned int i = 0; i < menus.size(); ++i) - { - list->menus[i] = menus[i].second; - } - - return list; - } - std::vector> Menus::LoadMenu(const std::string& menu) { std::vector> menus; @@ -278,33 +211,12 @@ namespace Components return menus; } - std::vector> Menus::LoadMenu(Game::menuDef_t* menudef) - { - std::vector> menus = Menus::LoadMenu(Utils::String::VA("ui_mp\\%s.menu", menudef->window.name)); - - if (menus.empty()) - { -// // Try loading the original menu, if we can't load our custom one -// Game::menuDef_t* originalMenu = AssetHandler::FindOriginalAsset(Game::XAssetType::ASSET_TYPE_MENU, menudef->window.name).menu; -// -// if (originalMenu) -// { -// menus.push_back({ false, originalMenu }); -// } -// else -// { - menus.push_back({ false, menudef }); // Native menu -// } - } - - return menus; - } - - Game::MenuList* Menus::LoadScriptMenu(const char* menu) + // Can be used to load scriptmenus or menulists + Game::MenuList* Menus::LoadMenuList(const std::string& name) { Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); - std::vector> menus = Menus::LoadMenu(menu); + std::vector> menus = Menus::LoadMenu(name); if (menus.empty()) return nullptr; // Allocate new menu list @@ -318,7 +230,7 @@ namespace Components return nullptr; } - newList->name = allocator->duplicateString(menu); + newList->name = allocator->duplicateString(name); newList->menuCount = menus.size(); // Copy new menus @@ -327,116 +239,6 @@ namespace Components newList->menus[i] = menus[i].second; } - Menus::RemoveMenuList(newList->name); - Menus::MenuListList[newList->name] = newList; - - return newList; - } - - void Menus::SafeMergeMenus(std::vector>* menus, std::vector> newMenus) - { - // Check if we overwrote a menu - for (auto i = menus->begin(); i != menus->end();) - { - // Try to find the native menu - bool found = !i->first; // Only if custom menu, try to find it - - // If there is none, try to find a custom menu - if (!found) - { - for (auto& entry : Menus::MenuList) - { - if (i->second == entry.second) - { - found = true; - break; - } - } - } - - // Remove the menu if it has been deallocated (not found) - if (!found) - { - i = menus->erase(i); - continue; - } - - bool increment = true; - - // Remove the menu if it has been loaded twice - for (auto& newMenu : newMenus) - { - if (i->second->window.name == std::string(newMenu.second->window.name)) - { - Menus::RemoveMenu(i->second); - - i = menus->erase(i); - increment = false; - break; - } - } - - if(increment) ++i; - } - - Utils::Merge(menus, newMenus); - } - - Game::MenuList* Menus::LoadMenuList(Game::MenuList* menuList) - { - Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); - - std::vector> menus; - - for (int i = 0; i < menuList->menuCount; ++i) - { - if (!menuList->menus[i]) continue; - Menus::SafeMergeMenus(&menus, Menus::LoadMenu(menuList->menus[i])); - } - - // Load custom menus - if (menuList->name == "ui_mp/code.txt"s) // Should be menus, but code is loaded ingame - { - for (auto menu : Menus::CustomMenus) - { - bool hasMenu = false; - for (auto &loadedMenu : menus) - { - if (loadedMenu.second->window.name == menu) - { - hasMenu = true; - break; - } - } - - if (!hasMenu) Menus::SafeMergeMenus(&menus, Menus::LoadMenu(menu)); - } - } - - // Allocate new menu list - Game::MenuList* newList = allocator->allocate(); - if (!newList) return menuList; - - size_t size = menus.size(); - newList->menus = allocator->allocateArray(size); - if (!newList->menus) - { - allocator->free(newList); - return menuList; - } - - newList->name = allocator->duplicateString(menuList->name); - newList->menuCount = size; - - // Copy new menus - for (unsigned int i = 0; i < menus.size(); ++i) - { - newList->menus[i] = menus[i].second; - } - - Menus::RemoveMenuList(newList->name); - Menus::MenuListList[newList->name] = newList; - return newList; } @@ -483,31 +285,22 @@ namespace Components Game::sourceFiles[handle] = nullptr; } - void Menus::FreeMenu(Game::menuDef_t* menudef) + void Menus::FreeDiskMenu(Game::menuDef_t* menudef) { Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); // Do i need to free expressions and strings? // Or does the game take care of it? // Seems like it does... - if (menudef->items) { - // Seems like this is obsolete as well, - // as the game handles the memory - - //for (int i = 0; i < menudef->itemCount; ++i) - //{ - // Game::Menu_FreeItemMemory(menudef->items[i]); - //} - allocator->free(menudef->items); } allocator->free(menudef); } - void Menus::FreeMenuList(Game::MenuList* menuList) + void Menus::FreeDiskMenuList(Game::MenuList* menuList) { if (!menuList) return; Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); @@ -528,123 +321,27 @@ namespace Components allocator->free(menuList); } - void Menus::RemoveMenu(const std::string& menu) - { - auto i = Menus::MenuList.find(menu); - if (i != Menus::MenuList.end()) - { - if (i->second) Menus::FreeMenu(i->second); - i = Menus::MenuList.erase(i); - } - } - - void Menus::RemoveMenu(Game::menuDef_t* menudef) - { - for (auto i = Menus::MenuList.begin(); i != Menus::MenuList.end();) - { - if (i->second == menudef) - { - Menus::FreeMenu(menudef); - i = Menus::MenuList.erase(i); - } - else - { - ++i; - } - } - } - - void Menus::RemoveMenuList(const std::string& menuList) - { - auto i = Menus::MenuListList.find(menuList); - if (i != Menus::MenuListList.end()) - { - if (i->second) - { - for (auto j = 0; j < i->second->menuCount; ++j) - { - Menus::RemoveMenu(i->second->menus[j]); - } - - Menus::FreeMenuList(i->second); - } - - i = Menus::MenuListList.erase(i); - } - } - - // This is actually a really important function - // It checks if we have already loaded the menu we passed and replaces its instances in memory - // Due to deallocating the old menu, the game might crash on not being able to handle its old instance - // So we need to override it in our menu lists and the game's ui context - // EDIT: We might also remove the old instances inside RemoveMenu - // EDIT2: Removing old instances without having a menu to replace them with might leave a nullptr - // EDIT3: Wouldn't it be better to check if the new menu we're trying to load has already been loaded and not was not deallocated and return that one instead of loading a new one? - void Menus::OverrideMenu(Game::menuDef_t *menu) - { - if (!menu || !menu->window.name) return; - std::string name = menu->window.name; - - // Find the old menu - auto i = Menus::MenuList.find(name); - if (i != Menus::MenuList.end()) - { - // We have found it, *yay* - Game::menuDef_t* oldMenu = i->second; - - // Replace every old instance with our new one in the ui context - for (int j = 0; j < Game::uiContext->menuCount; ++j) - { - if (Game::uiContext->Menus[j] == oldMenu) - { - Game::uiContext->Menus[j] = menu; - } - } - - // Replace every old instance with our new one in our menu lists - for (auto j = Menus::MenuListList.begin(); j != Menus::MenuListList.end(); ++j) - { - Game::MenuList* list = j->second; - - if (list && list->menus) - { - for (int k = 0; k < list->menuCount; ++k) - { - if (list->menus[k] == oldMenu) - { - list->menus[k] = menu; - } - } - } - } - } - } - - void Menus::RemoveMenuList(Game::MenuList* menuList) - { - if (!menuList || !menuList->name) return; - Menus::RemoveMenuList(menuList->name); - } - void Menus::FreeEverything() { - for (auto i = Menus::MenuListList.begin(); i != Menus::MenuListList.end(); ++i) + for (auto i = Menus::DiskMenuListList.begin(); i != Menus::DiskMenuListList.end(); ++i) { - Menus::FreeMenuList(i->second); + Menus::FreeDiskMenuList(i->second); } - Menus::MenuListList.clear(); + Menus::DiskMenuListList.clear(); - for (auto i = Menus::MenuList.begin(); i != Menus::MenuList.end(); ++i) + for (auto i = Menus::DiskMenuList.begin(); i != Menus::DiskMenuList.end(); ++i) { - Menus::FreeMenu(i->second); + Menus::FreeDiskMenu(i->second); } - Menus::MenuList.clear(); + Menus::DiskMenuList.clear(); + + Menus::UiContextMenus.clear(); } Game::XAssetHeader Menus::MenuFindHook(Game::XAssetType /*type*/, const std::string& filename) - { + { return { Game::Menus_FindByName(Game::uiContext, filename.data()) }; } @@ -652,56 +349,14 @@ namespace Components { Game::XAssetHeader header = { nullptr }; - // Free the last menulist and ui context, as we have to rebuild it with the new menus - if (Menus::MenuListList.find(filename) != Menus::MenuListList.end()) + // if menulist or scriptmenu exists on the disk, then load it + if (FileSystem::File(filename).exists()) { - Game::MenuList* list = Menus::MenuListList[filename]; - - for (int i = 0; list && list->menus && i < list->menuCount; ++i) - { - Menus::RemoveMenuFromContext(Game::uiContext, list->menus[i]); - } - - Menus::RemoveMenuList(filename); + header.menuList = Menus::LoadMenuList(filename.data()); + if (header.menuList) return header; } - if (Utils::String::EndsWith(filename, ".menu")) - { - if (FileSystem::File(filename).exists()) - { - header.menuList = Menus::LoadScriptMenu(filename.data()); - if (header.menuList) return header; - } - } - - Game::MenuList* menuList = Game::DB_FindXAssetHeader(type, filename.data()).menuList; - header.menuList = menuList; - - if (menuList && reinterpret_cast(menuList) != 0xDDDDDDDD) - { - // Parse scriptmenus! - if ((menuList->menuCount > 0 && menuList->menus[0] && menuList->menus[0]->window.name == "default_menu"s)) - { - if (FileSystem::File(filename).exists()) - { - header.menuList = Menus::LoadScriptMenu(filename.data()); - - // Reset, if we didn't find scriptmenus - if (!header.menuList) - { - header.menuList = menuList; - } - } - } - else - { - header.menuList = Menus::LoadMenuList(menuList); - } - } - else - { - header.menuList = nullptr; - } + // we don't need to modify any base assets here return header; } @@ -716,7 +371,7 @@ namespace Components if (originalConnect == menu) // Check if we draw the original loadscreen { - if (Menus::MenuList.find("connect") != Menus::MenuList.end()) // Check if we have a custom loadscreen, to prevent drawing the original one on top + if (Menus::DiskMenuList.find("connect") != Menus::DiskMenuList.end()) // Check if we have a custom loadscreen, to prevent drawing the original one on top { return false; } @@ -727,82 +382,108 @@ namespace Components return Game::Menu_IsVisible(dc, menu); } - void Menus::RemoveMenuFromContext(Game::UiContext *dc, Game::menuDef_t *menu) - { - // Search menu in context - int i = 0; - for (; i < dc->menuCount; ++i) - { - if (dc->Menus[i] == menu) - { - break; - } - } - - // Remove from stack - if (i < dc->menuCount) - { - for (; i < dc->menuCount - 1; ++i) - { - dc->Menus[i] = dc->Menus[i + 1]; - } - - // Clear last menu - dc->Menus[--dc->menuCount] = nullptr; - } - } - - void Menus::Add(const std::string& menu) - { - Menus::CustomMenus.push_back(menu); - } - - void Menus::RegisterCustomMenusHook() + void Menus::AddMenuToContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu) { - Game::UiContext* uiInfoArray = (Game::UiContext*)0x62E2858; - Game::MenuList list; + if(ctx->menuCount < MAX_MENUS_IN_CONTEXT) + { + ctx->Menus[ctx->menuCount++] = menu; + } + Menus::UiContextMenus[menu->window.name] = { priority, menu }; + } + + std::pair Menus::FindMenuInContext(Game::UiContext* ctx, const std::string& name) + { + auto entry = Menus::UiContextMenus.find(name); + if (entry == Menus::UiContextMenus.end()) return { 0, nullptr }; + return entry->second; + } + + // overwrite entry in uiContext, update it in our list, and free it if it was a disk menu + void Menus::ReplaceMenuInContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu) + { + for (int i = 0; i < ctx->menuCount; i++) + { + Game::menuDef_t* cur = ctx->Menus[i]; + if (!_stricmp(cur->window.name, menu->window.name)) + { + // check if it was a disk menu and free it if it was + if (Menus::DiskMenuList.find(cur->window.name) != Menus::DiskMenuList.end()) + { + Menus::FreeDiskMenu(cur); + } + + // replace entry in context + ctx->Menus[i] = menu; + break; + } + } + + // update our list + Menus::UiContextMenus[menu->window.name] = { priority, menu }; + } + + // enforce priority on what menus get loaded + // builtin < iw4x < mod + void Menus::AddMenuListToContext(Game::UiContext* ctx, Game::MenuList* list, int close) + { + int insertPriority = Menus::MenuContextPriority::PRIORITY_BUILTIN; + + if (!strncmp(list->name, "ui_mp/iw4x.txt", 13)) insertPriority = Menus::MenuContextPriority::PRIORITY_IW4X; + if (!strncmp(list->name, "ui_mp/mod.txt", 12)) insertPriority = Menus::MenuContextPriority::PRIORITY_MOD; + + for (int i = 0; i < list->menuCount; i++) + { + Game::menuDef_t* cur = list->menus[i]; + + // check if menu already exists in context and replace if priority is higher + std::pair ctxEntry = Menus::FindMenuInContext(ctx, cur->window.name); + + if (ctxEntry.second) // if menu ptr is null then it wasnt found + { + if (insertPriority > ctxEntry.first) // compare priorities to see if we should replace + { + Menus::ReplaceMenuInContext(ctx, insertPriority, cur); + } + } + else // otherwise just insert + { + Menus::AddMenuToContext(ctx, insertPriority, cur); + } + + if (close) + { + Game::Menus_CloseRequest(ctx, cur); + } + } + } + + void Menus::RegisterMenuLists() + { Utils::Hook::Call(0x401700)(); // call original load functions + + // attempt to load iw4x menus Game::XAssetHeader header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/iw4x.txt"); if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) { - // Utils::Hook::Call(0x401700)(uiInfoArray, header.menuList, 1); // add loaded menus - std::memcpy(&list, header.data, sizeof(Game::MenuList)); - - for (int i = 0; i < uiInfoArray->menuCount; i++) - { - for (int j = 0; j < list.menuCount; j++) - { - if (!list.menus[j]) continue; // skip already used entries - if (!stricmp(list.menus[j]->window.name, uiInfoArray->Menus[i]->window.name)) - { - uiInfoArray->Menus[i] = list.menus[j]; // overwrite UiContext pointer - list.menus[j] = nullptr; // clear entries that already exist so we don't add them later - } - } - } - - for (int i = 0; i < list.menuCount; i++) - { - if (list.menus[i]) - { - uiInfoArray->Menus[uiInfoArray->menuCount++] = list.menus[i]; - } - } + Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1); } - for (int i = 0; i < uiInfoArray->menuCount; i++) - { - OutputDebugStringA(Utils::String::VA("%s\n", uiInfoArray->Menus[i]->window.name)); - } - - /* + // attempt to load mod menus header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/mod.txt"); if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) { - Utils::Hook::Call(0x401700)(uiInfoArray, header.menuList, 1); // add loaded menus + Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1); } - */ + } + + void Menus::ResetContextHook(int a1) + { + // reset our lists + Menus::FreeEverything(); + + // continue with initialization + Utils::Hook::Call(0x4A57D0)(a1); } Menus::Menus() @@ -820,7 +501,13 @@ namespace Components //Utils::Hook::Nop(0x428E48, 5); // register custom menufiles if they exist - Utils::Hook(0x4A58C3, Menus::RegisterCustomMenusHook, HOOK_CALL).install()->quick(); + Utils::Hook(0x4A58C3, Menus::RegisterMenuLists, HOOK_CALL).install()->quick(); + + // take control of menus in uiContext + Utils::Hook(0x4533C0, Menus::AddMenuListToContext, HOOK_JUMP).install()->quick(); + + // reset our list on UiContext reset + Utils::Hook(0x4B5422, Menus::ResetContextHook, HOOK_CALL).install()->quick(); // Use the connect menu open call to update server motds Utils::Hook(0x428E48, []() @@ -865,7 +552,7 @@ namespace Components // Close all menus Game::Menus_CloseAll(Game::uiContext); - // Free custom menus + // Free custom menus and reset uiContext list Menus::FreeEverything(); // Only disconnect if in-game, context is updated automatically! @@ -875,8 +562,8 @@ namespace Components } else { - // Reinitialize ui context - Utils::Hook::Call(0x401700)(); + // re-register all menus + Menus::RegisterMenuLists(); // Reopen main menu Game::Menus_OpenByName(Game::uiContext, "main_text"); @@ -891,29 +578,10 @@ namespace Components { Command::Execute("openmenu quickmessage"); }); - - // Define custom menus here - /* - Menus::Add("ui_mp/changelog.menu"); - Menus::Add("ui_mp/theater_menu.menu"); - Menus::Add("ui_mp/pc_options_multi.menu"); - Menus::Add("ui_mp/pc_options_game.menu"); - Menus::Add("ui_mp/stats_reset.menu"); - Menus::Add("ui_mp/stats_unlock.menu"); - Menus::Add("ui_mp/security_increase_popmenu.menu"); - Menus::Add("ui_mp/mod_download_popmenu.menu"); - Menus::Add("ui_mp/popup_friends.menu"); - Menus::Add("ui_mp/menu_first_launch.menu"); - Menus::Add("ui_mp/startup_messages.menu"); - Menus::Add("ui_mp/pc_store.menu"); - Menus::Add("ui_mp/iw4x_credits.menu"); - Menus::Add("ui_mp/resetclass.menu"); - */ } Menus::~Menus() { - Menus::CustomMenus.clear(); Menus::FreeEverything(); } } diff --git a/src/Components/Modules/Menus.hpp b/src/Components/Modules/Menus.hpp index 151b5e0a..df7e9390 100644 --- a/src/Components/Modules/Menus.hpp +++ b/src/Components/Modules/Menus.hpp @@ -1,6 +1,7 @@ #pragma once #define MAX_SOURCEFILES 64 +#define MAX_MENUS_IN_CONTEXT 640 #undef LoadMenu namespace Components @@ -12,50 +13,48 @@ namespace Components ~Menus(); static void FreeEverything(); + static void RegisterMenuLists(); - static void Add(const std::string& menu); - - static Game::MenuList* Menus::LoadCustomMenuList(const std::string& menu, Utils::Memory::Allocator* allocator); + // used to load assets for zonebuilder + static Game::MenuList* LoadMenuList(const std::string& file); + static std::vector> LoadMenu(const std::string& file); private: - static std::unordered_map MenuList; - static std::unordered_map MenuListList; - static std::vector CustomMenus; + static std::unordered_map DiskMenuList; + static std::unordered_map DiskMenuListList; + static std::unordered_map> Menus::UiContextMenus; - static Game::XAssetHeader MenuFindHook(Game::XAssetType type, const std::string& filename); - static Game::XAssetHeader MenuListFindHook(Game::XAssetType type, const std::string& filename); - - static Game::MenuList* LoadMenuList(Game::MenuList* menuList); - static Game::MenuList* LoadScriptMenu(const char* menu); - static std::vector> LoadMenu(Game::menuDef_t* menudef); - static std::vector> LoadMenu(const std::string& file); - static void SafeMergeMenus(std::vector>* menus, std::vector> newMenus); - - static Game::script_t* LoadMenuScript(const std::string& name, const std::string& buffer); - static int LoadMenuSource(const std::string& name, const std::string& buffer); - - static int ReserveSourceHandle(); - static bool IsValidSourceHandle(int handle); - - static Game::menuDef_t* ParseMenu(int handle); + // Loading + static int ReserveSourceHandle(); + static bool IsValidSourceHandle(int handle); + static Game::menuDef_t* ParseMenu(int handle); + static Game::script_t* LoadMenuScript(const std::string& name, const std::string& buffer); + static int LoadMenuSource(const std::string& name, const std::string& buffer); + // Freeing static void FreeMenuSource(int handle); + static void FreeDiskMenuList(Game::MenuList* menuList); + static void FreeDiskMenu(Game::menuDef_t* menudef); - static void FreeMenuList(Game::MenuList* menuList); - static void FreeMenu(Game::menuDef_t* menudef); - - static void RemoveMenu(const std::string& menu); - static void RemoveMenu(Game::menuDef_t* menudef); - static void RemoveMenuList(const std::string& menuList); - static void RemoveMenuList(Game::MenuList* menuList); - - static void OverrideMenu(Game::menuDef_t *menu); - + // Etc. static bool IsMenuVisible(Game::UiContext *dc, Game::menuDef_t *menu); - static void RemoveMenuFromContext(Game::UiContext *dc, Game::menuDef_t *menu); + // Intercept Asset Find Calls + static Game::XAssetHeader MenuFindHook(Game::XAssetType type, const std::string& filename); + static Game::XAssetHeader MenuListFindHook(Game::XAssetType type, const std::string& filename); - static void RegisterCustomMenusHook(); + // Manage menus in uiContext + enum MenuContextPriority + { + PRIORITY_BUILTIN = 0, + PRIORITY_IW4X = 1, + PRIORITY_MOD = 2, + }; + static std::pair FindMenuInContext(Game::UiContext* ctx, const std::string& name); + static void AddMenuToContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu); + static void ReplaceMenuInContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu); + static void AddMenuListToContext(Game::UiContext* ctx, Game::MenuList* list, int close); + static void ResetContextHook(int a1); // Ugly! static int KeywordHash(char* key); diff --git a/src/Game/Functions.cpp b/src/Game/Functions.cpp index 7628c469..701f1412 100644 --- a/src/Game/Functions.cpp +++ b/src/Game/Functions.cpp @@ -148,6 +148,7 @@ namespace Game Load_snd_alias_list_nameArray_t Load_snd_alias_list_nameArray = Load_snd_alias_list_nameArray_t(0x4499F0); Menus_CloseAll_t Menus_CloseAll = Menus_CloseAll_t(0x4BA5B0); + Menus_CloseRequest_t Menus_CloseRequest = Menus_CloseRequest_t(0x430D50); Menus_OpenByName_t Menus_OpenByName = Menus_OpenByName_t(0x4CCE60); Menus_FindByName_t Menus_FindByName = Menus_FindByName_t(0x487240); Menu_IsVisible_t Menu_IsVisible = Menu_IsVisible_t(0x4D77D0); diff --git a/src/Game/Functions.hpp b/src/Game/Functions.hpp index 2350586a..aa4f1bf8 100644 --- a/src/Game/Functions.hpp +++ b/src/Game/Functions.hpp @@ -373,6 +373,9 @@ namespace Game typedef void(__cdecl * Menus_CloseAll_t)(UiContext *dc); extern Menus_CloseAll_t Menus_CloseAll; + typedef void(__cdecl * Menus_CloseRequest_t)(UiContext *dc, menuDef_t* menu); + extern Menus_CloseRequest_t Menus_CloseRequest; + typedef int(__cdecl * Menus_OpenByName_t)(UiContext *dc, const char *p); extern Menus_OpenByName_t Menus_OpenByName; From ad5a58462fbe8e08a831b90f39e994d8b98119a5 Mon Sep 17 00:00:00 2001 From: TheApadayo Date: Thu, 17 Jan 2019 18:39:01 -0500 Subject: [PATCH 3/5] [Menus] Properly load menus Overridden menus are just loaded in iw4x_code_post_gfx_mp and new ones are added to ui_mp/iw4x.txt --- .../Modules/AssetInterfaces/IMenuList.cpp | 2 +- .../Modules/AssetInterfaces/ImenuDef_t.cpp | 10 +++- src/Components/Modules/Menus.cpp | 60 +++++++++++++------ 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IMenuList.cpp b/src/Components/Modules/AssetInterfaces/IMenuList.cpp index 9f04195f..48a77268 100644 --- a/src/Components/Modules/AssetInterfaces/IMenuList.cpp +++ b/src/Components/Modules/AssetInterfaces/IMenuList.cpp @@ -2,7 +2,7 @@ namespace Assets { - void IMenuList::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) + void IMenuList::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/) { header->menuList = Components::Menus::LoadMenuList(name); diff --git a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp index 2aa142f4..6ba6c80a 100644 --- a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp +++ b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp @@ -5,7 +5,7 @@ namespace Assets std::unordered_map ImenuDef_t::LoadedMenus; - void ImenuDef_t::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) + void ImenuDef_t::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/) { // search menus loaded by a menufile for (auto i = ImenuDef_t::LoadedMenus.begin(); i != ImenuDef_t::LoadedMenus.end(); ++i) @@ -15,6 +15,14 @@ namespace Assets return; } } + + // load from disk + auto menus = Components::Menus::LoadMenu(Utils::String::VA("ui_mp/%s.menu", name.data())); + + if (menus.size() == 0) return; + if (menus.size() > 1) Components::Logger::Print("Menu '%s' on disk has more than one menudef in it. Only saving the first one\n", name.data()); + + header->menu = menus[0].second; } diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index 48a332bf..57e2ff06 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -345,7 +345,7 @@ namespace Components return { Game::Menus_FindByName(Game::uiContext, filename.data()) }; } - Game::XAssetHeader Menus::MenuListFindHook(Game::XAssetType type, const std::string& filename) + Game::XAssetHeader Menus::MenuListFindHook(Game::XAssetType /*type*/, const std::string& filename) { Game::XAssetHeader header = { nullptr }; @@ -392,7 +392,7 @@ namespace Components Menus::UiContextMenus[menu->window.name] = { priority, menu }; } - std::pair Menus::FindMenuInContext(Game::UiContext* ctx, const std::string& name) + std::pair Menus::FindMenuInContext(Game::UiContext* /*ctx*/, const std::string& name) { auto entry = Menus::UiContextMenus.find(name); if (entry == Menus::UiContextMenus.end()) return { 0, nullptr }; @@ -438,10 +438,9 @@ namespace Components // check if menu already exists in context and replace if priority is higher std::pair ctxEntry = Menus::FindMenuInContext(ctx, cur->window.name); - if (ctxEntry.second) // if menu ptr is null then it wasnt found { - if (insertPriority > ctxEntry.first) // compare priorities to see if we should replace + if (insertPriority >= ctxEntry.first) // compare priorities to see if we should replace { Menus::ReplaceMenuInContext(ctx, insertPriority, cur); } @@ -460,21 +459,31 @@ namespace Components void Menus::RegisterMenuLists() { - Utils::Hook::Call(0x401700)(); // call original load functions + Utils::Hook::Call(0x401700)(); // reset ui context - // attempt to load iw4x menus - Game::XAssetHeader header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/iw4x.txt"); - if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) + // we can't call DB_FindXAssetHeader here because it blocks the rest of loading waiting on those 2 menulsits + // TODO: Figure out a better way to trigger the custom menulist loading because if you skip the intro the + // custom menus won't have loaded until a few seconds later. All overridden menus are already + // loaded so it isn't a black screen but it wont show the first time intro, credits, etc. + // as soon as this loads those start to work again + // if we just trigger this here it blocks the intro from showing because of the FindXAssetHeader calls + // that are waiting for zones to finish loading + Scheduler::OnReady([]() { - Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1); - } + // attempt to load iw4x menus + Game::XAssetHeader header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/iw4x.txt"); + if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) + { + Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1); + } - // attempt to load mod menus - header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/mod.txt"); - if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) - { - Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1); - } + // attempt to load mod menus + header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/mod.txt"); + if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name))) + { + Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1); + } + }, true); } void Menus::ResetContextHook(int a1) @@ -501,7 +510,7 @@ namespace Components //Utils::Hook::Nop(0x428E48, 5); // register custom menufiles if they exist - Utils::Hook(0x4A58C3, Menus::RegisterMenuLists, HOOK_CALL).install()->quick(); + Utils::Hook(0x4A58C3, Menus::RegisterMenuLists, HOOK_CALL).install()->quick(); // take control of menus in uiContext Utils::Hook(0x4533C0, Menus::AddMenuListToContext, HOOK_JUMP).install()->quick(); @@ -509,6 +518,20 @@ namespace Components // reset our list on UiContext reset Utils::Hook(0x4B5422, Menus::ResetContextHook, HOOK_CALL).install()->quick(); + // grab custom lists as they are loaded otherwise DB takes up to 20 seconds to load intro + /* + AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool*) + { + if (type != Game::XAssetType::ASSET_TYPE_MENULIST) return; + + if (name == "ui_mp/iw4x.txt" || name == "ui_mp/mod.txt") + { + Menus::AddMenuListToContext(Game::uiContext, asset.menuList, 1); + } + }); + + */ + // Use the connect menu open call to update server motds Utils::Hook(0x428E48, []() { @@ -562,8 +585,7 @@ namespace Components } else { - // re-register all menus - Menus::RegisterMenuLists(); + Menus::RegisterMenuLists(); // register custom menus // Reopen main menu Game::Menus_OpenByName(Game::uiContext, "main_text"); From 831291bdfe3c2e11a40c0a0e1037f170ced3abe5 Mon Sep 17 00:00:00 2001 From: TheApadayo Date: Sat, 19 Jan 2019 11:28:36 -0500 Subject: [PATCH 4/5] [ImenuDef_t] Add logging to menus and fix a bug in clearing the array pointer for float expression entries --- .../Modules/AssetInterfaces/ImenuDef_t.cpp | 88 ++++++++++++++++++- src/Utils/Stream.cpp | 8 +- 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp index 6ba6c80a..6a64b529 100644 --- a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp +++ b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp @@ -61,6 +61,10 @@ namespace Assets AssertSize(Game::ExpressionSupportingData, 24); Utils::Stream* buffer = builder->getBuffer(); +#ifdef WRITE_LOGS + buffer->enterStruct("ExpressionSupportingData"); +#endif + buffer->align(Utils::Stream::ALIGN_4); Game::ExpressionSupportingData *dest = buffer->dest(); @@ -131,13 +135,21 @@ namespace Assets } } } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } void ImenuDef_t::save_Statement_s(Game::Statement_s* asset, Components::ZoneBuilder::Zone* builder) { AssertSize(Game::Statement_s, 24); + AssertSize(Game::expressionEntry, 12); Utils::Stream* buffer = builder->getBuffer(); +#ifdef WRITE_LOGS + buffer->enterStruct("Statement_s"); +#endif + // Write header data Game::Statement_s *dest = buffer->dest(); buffer->save(asset); @@ -145,6 +157,9 @@ namespace Assets // Write statement entries if (asset->entries) { +#ifdef WRITE_LOGS + buffer->enterStruct("statement entries"); +#endif buffer->align(Utils::Stream::ALIGN_4); // Write entries @@ -154,6 +169,9 @@ namespace Assets // Loop through entries for (int i = 0; i < asset->numEntries; ++i) { +#ifdef WRITE_LOGS + buffer->enterStruct("entry"); +#endif if (asset->entries[i].type) { switch (asset->entries[i].data.operand.dataType) @@ -183,13 +201,23 @@ namespace Assets break; } } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } + if (asset->supportingData) { this->save_ExpressionSupportingData(asset->supportingData, builder); Utils::Stream::ClearPointer(&dest->supportingData); } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } void ImenuDef_t::save_MenuEventHandlerSet(Game::MenuEventHandlerSet* asset, Components::ZoneBuilder::Zone* builder) @@ -197,6 +225,10 @@ namespace Assets AssertSize(Game::MenuEventHandlerSet, 8); Utils::Stream* buffer = builder->getBuffer(); +#ifdef WRITE_LOGS + buffer->enterStruct("MenuEventHandlerSet"); +#endif + // Write header data Game::MenuEventHandlerSet *destset = buffer->dest(); buffer->save(asset); @@ -215,6 +247,9 @@ namespace Assets if (asset->eventHandlers[i]) { buffer->align(Utils::Stream::ALIGN_4); +#ifdef WRITE_LOGS + buffer->enterStruct("MenuEventHandler"); +#endif // Write menu event handler Game::MenuEventHandler *dest = buffer->dest(); @@ -302,11 +337,17 @@ namespace Assets } break; } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } } Utils::Stream::ClearPointer(&destset->eventHandlers); } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } void ImenuDef_t::save_ItemKeyHandler(Game::ItemKeyHandler* asset, Components::ZoneBuilder::Zone* builder) @@ -314,6 +355,10 @@ namespace Assets AssertSize(Game::ItemKeyHandler, 12); Utils::Stream* buffer = builder->getBuffer(); +#ifdef WRITE_LOGS + buffer->enterStruct("ItemKeyHandler"); +#endif + while (asset) { // Write header @@ -337,6 +382,9 @@ namespace Assets // Next key handler asset = asset->next; } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } #define EVENTHANDLERSET(__indice) \ @@ -364,6 +412,10 @@ namespace Assets Utils::Stream* buffer = builder->getBuffer(); +#ifdef WRITE_LOGS + buffer->enterStruct("itemDefData_t"); +#endif + // feeder if (type == 6) { @@ -445,6 +497,10 @@ namespace Assets } Utils::Stream::ClearPointer(&dest->typeData.data); + +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } void ImenuDef_t::save_itemDef_s(Game::itemDef_s *asset, Components::ZoneBuilder::Zone* builder) @@ -454,6 +510,15 @@ namespace Assets Utils::Stream* buffer = builder->getBuffer(); Game::itemDef_s* dest = buffer->dest(); +#ifdef WRITE_LOGS + if (asset->window.name) + buffer->enterStruct(Utils::String::VA("itemDef_s: name = '%s'", asset->window.name)); + else if (asset->window.background) + buffer->enterStruct(Utils::String::VA("itemDef_s: bg = '%s'", asset->window.background->info.name)); + else + buffer->enterStruct("itemDef_s"); +#endif + buffer->save(asset); // window data @@ -482,6 +547,7 @@ namespace Assets buffer->saveString(asset->dvar); Utils::Stream::ClearPointer(&dest->dvar); } + if (asset->dvarTest) { buffer->saveString(asset->dvarTest); @@ -502,6 +568,7 @@ namespace Assets buffer->saveString(asset->enableDvar); Utils::Stream::ClearPointer(&dest->enableDvar); } + if (asset->localVar) { buffer->saveString(asset->localVar); @@ -524,6 +591,9 @@ namespace Assets if (asset->floatExpressions) { buffer->align(Utils::Stream::ALIGN_4); +#ifdef WRITE_LOGS + buffer->enterStruct("floatExpressions"); +#endif Game::ItemFloatExpression* destExp = buffer->dest(); buffer->saveArray(asset->floatExpressions, asset->floatExpressionCount); @@ -532,10 +602,14 @@ namespace Assets { buffer->align(Utils::Stream::ALIGN_4); this->save_Statement_s(asset->floatExpressions[i].expression, builder); - Utils::Stream::ClearPointer(&destExp->expression); + Utils::Stream::ClearPointer(&destExp[i].expression); } Utils::Stream::ClearPointer(&dest->floatExpressions); + +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } // Statements @@ -543,16 +617,23 @@ namespace Assets STATEMENT(disabledExp); STATEMENT(textExp); STATEMENT(materialExp); + +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif } void ImenuDef_t::save(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) { AssertSize(Game::menuDef_t, 400); +#ifdef WRITE_LOGS + buffer->enterStruct("ImenuDef_t"); +#endif + Utils::Stream* buffer = builder->getBuffer(); Game::menuDef_t* asset = header.menu; Game::menuDef_t* dest = buffer->dest(); - buffer->save(asset); buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL); @@ -627,6 +708,9 @@ namespace Assets } } } +#ifdef WRITE_LOGS + buffer->leaveStruct(); +#endif buffer->popBlock(); } diff --git a/src/Utils/Stream.cpp b/src/Utils/Stream.cpp index 2cda0998..92049f59 100644 --- a/src/Utils/Stream.cpp +++ b/src/Utils/Stream.cpp @@ -289,8 +289,8 @@ namespace Utils } #ifdef WRITE_LOGS - std::string data = fmt::sprintf("%*s%d\n", this->structLevel, "", size); - if (stream == Game::XFILE_BLOCK_RUNTIME) data = fmt::sprintf("%*s(%d)\n", this->structLevel, "", size); + std::string data = Utils::String::VA("%*s%d\n", this->structLevel, "", size); + if (stream == Game::XFILE_BLOCK_RUNTIME) data = Utils::String::VA("%*s(%d)\n", this->structLevel, "", size); Utils::IO::WriteFile("userraw/logs/zb_writes.log", data, true); #endif } @@ -379,7 +379,7 @@ namespace Utils { if (this->structLevel >= 0) { - Utils::IO::WriteFile("userraw/logs/zb_writes.log", fmt::sprintf("%*s%s\n", this->structLevel++, "", structName), true); + Utils::IO::WriteFile("userraw/logs/zb_writes.log", Utils::String::VA("%*s%s\n", this->structLevel++, "", structName), true); } } @@ -391,7 +391,7 @@ namespace Utils return; } - Utils::IO::WriteFile("userraw/logs/zb_writes.log", fmt::sprintf("%*s-----\n", this->structLevel, ""), true); + Utils::IO::WriteFile("userraw/logs/zb_writes.log", Utils::String::VA("%*s-----\n", this->structLevel, ""), true); } #endif } From 128ef8016b0163e074bde143959752de4f0e0216 Mon Sep 17 00:00:00 2001 From: TheApadayo Date: Tue, 22 Jan 2019 12:06:07 -0500 Subject: [PATCH 5/5] [Menus] Reduce complexity in the system for loading menus (+1 squashed commits) --- .../Modules/AssetInterfaces/IMenuList.cpp | 28 ++- .../Modules/AssetInterfaces/ImenuDef_t.cpp | 11 +- src/Components/Modules/Menus.cpp | 221 +++++------------- src/Components/Modules/Menus.hpp | 19 +- 4 files changed, 85 insertions(+), 194 deletions(-) diff --git a/src/Components/Modules/AssetInterfaces/IMenuList.cpp b/src/Components/Modules/AssetInterfaces/IMenuList.cpp index 48a77268..e18cb961 100644 --- a/src/Components/Modules/AssetInterfaces/IMenuList.cpp +++ b/src/Components/Modules/AssetInterfaces/IMenuList.cpp @@ -2,15 +2,35 @@ namespace Assets { - void IMenuList::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/) + void IMenuList::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder) { - header->menuList = Components::Menus::LoadMenuList(name); + Utils::Memory::Allocator* allocator = builder->getAllocator(); - for (int i = 0; i < header->menuList->menuCount; i++) + // actually gets the whole list + std::vector menus = Components::Menus::LoadMenu(name); + if (menus.empty()) return; + + // Allocate new menu list + Game::MenuList* newList = allocator->allocate(); + if (!newList) return; + + newList->menus = allocator->allocateArray(menus.size()); + if (!newList->menus) { - ImenuDef_t::LoadedMenus[header->menuList->menus[i]->window.name] = header->menuList->menus[i]; + allocator->free(newList); + return; } + newList->name = allocator->duplicateString(name); + newList->menuCount = menus.size(); + + // Copy new menus + for (unsigned int i = 0; i < menus.size(); ++i) + { + newList->menus[i] = menus[i]; + } + + header->menuList = newList; } void IMenuList::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder) { diff --git a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp index 6a64b529..9477f908 100644 --- a/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp +++ b/src/Components/Modules/AssetInterfaces/ImenuDef_t.cpp @@ -7,22 +7,13 @@ namespace Assets void ImenuDef_t::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/) { - // search menus loaded by a menufile - for (auto i = ImenuDef_t::LoadedMenus.begin(); i != ImenuDef_t::LoadedMenus.end(); ++i) - { - if (i->first == name) { - header->menu = i->second; - return; - } - } - // load from disk auto menus = Components::Menus::LoadMenu(Utils::String::VA("ui_mp/%s.menu", name.data())); if (menus.size() == 0) return; if (menus.size() > 1) Components::Logger::Print("Menu '%s' on disk has more than one menudef in it. Only saving the first one\n", name.data()); - header->menu = menus[0].second; + header->menu = menus[0]; } diff --git a/src/Components/Modules/Menus.cpp b/src/Components/Modules/Menus.cpp index 57e2ff06..b7a419d0 100644 --- a/src/Components/Modules/Menus.cpp +++ b/src/Components/Modules/Menus.cpp @@ -3,8 +3,6 @@ namespace Components { std::unordered_map Menus::DiskMenuList; - std::unordered_map Menus::DiskMenuListList; - std::unordered_map> Menus::UiContextMenus; int Menus::ReserveSourceHandle() { @@ -169,9 +167,9 @@ namespace Components return menu; } - std::vector> Menus::LoadMenu(const std::string& menu) + std::vector Menus::LoadMenu(const std::string& menu) { - std::vector> menus; + std::vector menus; FileSystem::File menuFile(menu); if (menuFile.exists()) @@ -200,7 +198,7 @@ namespace Components if (!_stricmp(token.string, "menudef")) { Game::menuDef_t* menudef = Menus::ParseMenu(handle); - if (menudef) menus.push_back({ true, menudef }); // Custom menu + if (menudef) menus.push_back(menudef); // Custom menu } } @@ -208,40 +206,15 @@ namespace Components } } + // store loaded menus to be freed later + for (auto it = menus.begin(); it != menus.end(); ++it) + { + Menus::DiskMenuList[(*it)->window.name] = *it; + } + return menus; } - // Can be used to load scriptmenus or menulists - Game::MenuList* Menus::LoadMenuList(const std::string& name) - { - Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); - - std::vector> menus = Menus::LoadMenu(name); - if (menus.empty()) return nullptr; - - // Allocate new menu list - Game::MenuList* newList = allocator->allocate(); - if (!newList) return nullptr; - - newList->menus = allocator->allocateArray(menus.size()); - if (!newList->menus) - { - allocator->free(newList); - return nullptr; - } - - newList->name = allocator->duplicateString(name); - newList->menuCount = menus.size(); - - // Copy new menus - for (unsigned int i = 0; i < menus.size(); ++i) - { - newList->menus[i] = menus[i].second; - } - - return newList; - } - void Menus::FreeMenuSource(int handle) { Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); @@ -300,65 +273,14 @@ namespace Components allocator->free(menudef); } - void Menus::FreeDiskMenuList(Game::MenuList* menuList) - { - if (!menuList) return; - Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator(); - - // Keep our compiler happy - Game::MenuList list = { menuList->name, menuList->menuCount, menuList->menus }; - - if (list.name) - { - allocator->free(list.name); - } - - if (list.menus) - { - allocator->free(list.menus); - } - - allocator->free(menuList); - } - void Menus::FreeEverything() { - for (auto i = Menus::DiskMenuListList.begin(); i != Menus::DiskMenuListList.end(); ++i) - { - Menus::FreeDiskMenuList(i->second); - } - - Menus::DiskMenuListList.clear(); - for (auto i = Menus::DiskMenuList.begin(); i != Menus::DiskMenuList.end(); ++i) { Menus::FreeDiskMenu(i->second); } Menus::DiskMenuList.clear(); - - Menus::UiContextMenus.clear(); - } - - Game::XAssetHeader Menus::MenuFindHook(Game::XAssetType /*type*/, const std::string& filename) - { - return { Game::Menus_FindByName(Game::uiContext, filename.data()) }; - } - - Game::XAssetHeader Menus::MenuListFindHook(Game::XAssetType /*type*/, const std::string& filename) - { - Game::XAssetHeader header = { nullptr }; - - // if menulist or scriptmenu exists on the disk, then load it - if (FileSystem::File(filename).exists()) - { - header.menuList = Menus::LoadMenuList(filename.data()); - if (header.menuList) return header; - } - - // we don't need to modify any base assets here - - return header; } bool Menus::IsMenuVisible(Game::UiContext *dc, Game::menuDef_t *menu) @@ -382,72 +304,55 @@ namespace Components return Game::Menu_IsVisible(dc, menu); } - void Menus::AddMenuToContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu) - { - if(ctx->menuCount < MAX_MENUS_IN_CONTEXT) - { - ctx->Menus[ctx->menuCount++] = menu; - } - - Menus::UiContextMenus[menu->window.name] = { priority, menu }; - } - - std::pair Menus::FindMenuInContext(Game::UiContext* /*ctx*/, const std::string& name) - { - auto entry = Menus::UiContextMenus.find(name); - if (entry == Menus::UiContextMenus.end()) return { 0, nullptr }; - return entry->second; - } - - // overwrite entry in uiContext, update it in our list, and free it if it was a disk menu - void Menus::ReplaceMenuInContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu) - { - for (int i = 0; i < ctx->menuCount; i++) - { - Game::menuDef_t* cur = ctx->Menus[i]; - if (!_stricmp(cur->window.name, menu->window.name)) - { - // check if it was a disk menu and free it if it was - if (Menus::DiskMenuList.find(cur->window.name) != Menus::DiskMenuList.end()) - { - Menus::FreeDiskMenu(cur); - } - - // replace entry in context - ctx->Menus[i] = menu; - break; - } - } - - // update our list - Menus::UiContextMenus[menu->window.name] = { priority, menu }; - } - - // enforce priority on what menus get loaded - // builtin < iw4x < mod void Menus::AddMenuListToContext(Game::UiContext* ctx, Game::MenuList* list, int close) { - int insertPriority = Menus::MenuContextPriority::PRIORITY_BUILTIN; + // scriptmenu + if (std::string(list->name).find(".menu") != std::string::npos) + { + auto menus = Menus::LoadMenu(list->name); - if (!strncmp(list->name, "ui_mp/iw4x.txt", 13)) insertPriority = Menus::MenuContextPriority::PRIORITY_IW4X; - if (!strncmp(list->name, "ui_mp/mod.txt", 12)) insertPriority = Menus::MenuContextPriority::PRIORITY_MOD; + if (menus.size()) + { + Logger::Print("Overriding menu '%s'\n", list->name); + for (auto it = menus.begin(); it != menus.end(); ++it) + { + if (ctx->menuCount < MAX_MENUS_IN_CONTEXT) + { + ctx->Menus[ctx->menuCount++] = *it; + } + + if (close) + { + Game::Menus_CloseRequest(ctx, *it); + } + } + + return; // don't add original menus + } + } + for (int i = 0; i < list->menuCount; i++) { Game::menuDef_t* cur = list->menus[i]; - // check if menu already exists in context and replace if priority is higher - std::pair ctxEntry = Menus::FindMenuInContext(ctx, cur->window.name); - if (ctxEntry.second) // if menu ptr is null then it wasnt found + if (cur->window.name == reinterpret_cast(0xDDDDDDDD)) { - if (insertPriority >= ctxEntry.first) // compare priorities to see if we should replace - { - Menus::ReplaceMenuInContext(ctx, insertPriority, cur); - } + DebugBreak(); } - else // otherwise just insert + + auto menus = Menus::LoadMenu(Utils::String::VA("ui_mp/%s.menu", cur->window.name)); + + if (menus.size()) { - Menus::AddMenuToContext(ctx, insertPriority, cur); + Logger::Print("Overriding menu '%s'\n", cur->window.name); + if (menus.size() > 1) Logger::Print("Disk menu has more than one definition Using only the first one.\n"); + cur = menus[0]; // replace menu in context with loaded one + } + + if (ctx->menuCount < MAX_MENUS_IN_CONTEXT) + { + ctx->Menus[ctx->menuCount++] = cur; } if (close) @@ -468,7 +373,8 @@ namespace Components // as soon as this loads those start to work again // if we just trigger this here it blocks the intro from showing because of the FindXAssetHeader calls // that are waiting for zones to finish loading - Scheduler::OnReady([]() + + auto loadCustomMenus = []() { // attempt to load iw4x menus Game::XAssetHeader header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/iw4x.txt"); @@ -483,7 +389,16 @@ namespace Components { Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1); } - }, true); + }; + + if (!FastFiles::Ready()) + { + Scheduler::OnReady(loadCustomMenus, true); + } + else + { + loadCustomMenus(); + } } void Menus::ResetContextHook(int a1) @@ -502,10 +417,6 @@ namespace Components // Ensure everything is zero'ed Menus::FreeEverything(); - // Intercept asset finding - AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENU, Menus::MenuFindHook); - AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENULIST, Menus::MenuListFindHook); - // Don't open connect menu //Utils::Hook::Nop(0x428E48, 5); @@ -518,20 +429,6 @@ namespace Components // reset our list on UiContext reset Utils::Hook(0x4B5422, Menus::ResetContextHook, HOOK_CALL).install()->quick(); - // grab custom lists as they are loaded otherwise DB takes up to 20 seconds to load intro - /* - AssetHandler::OnLoad([](Game::XAssetType type, Game::XAssetHeader asset, std::string name, bool*) - { - if (type != Game::XAssetType::ASSET_TYPE_MENULIST) return; - - if (name == "ui_mp/iw4x.txt" || name == "ui_mp/mod.txt") - { - Menus::AddMenuListToContext(Game::uiContext, asset.menuList, 1); - } - }); - - */ - // Use the connect menu open call to update server motds Utils::Hook(0x428E48, []() { diff --git a/src/Components/Modules/Menus.hpp b/src/Components/Modules/Menus.hpp index df7e9390..9a204b7a 100644 --- a/src/Components/Modules/Menus.hpp +++ b/src/Components/Modules/Menus.hpp @@ -16,13 +16,10 @@ namespace Components static void RegisterMenuLists(); // used to load assets for zonebuilder - static Game::MenuList* LoadMenuList(const std::string& file); - static std::vector> LoadMenu(const std::string& file); + static std::vector LoadMenu(const std::string& file); private: static std::unordered_map DiskMenuList; - static std::unordered_map DiskMenuListList; - static std::unordered_map> Menus::UiContextMenus; // Loading static int ReserveSourceHandle(); @@ -33,26 +30,12 @@ namespace Components // Freeing static void FreeMenuSource(int handle); - static void FreeDiskMenuList(Game::MenuList* menuList); static void FreeDiskMenu(Game::menuDef_t* menudef); // Etc. static bool IsMenuVisible(Game::UiContext *dc, Game::menuDef_t *menu); - // Intercept Asset Find Calls - static Game::XAssetHeader MenuFindHook(Game::XAssetType type, const std::string& filename); - static Game::XAssetHeader MenuListFindHook(Game::XAssetType type, const std::string& filename); - // Manage menus in uiContext - enum MenuContextPriority - { - PRIORITY_BUILTIN = 0, - PRIORITY_IW4X = 1, - PRIORITY_MOD = 2, - }; - static std::pair FindMenuInContext(Game::UiContext* ctx, const std::string& name); - static void AddMenuToContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu); - static void ReplaceMenuInContext(Game::UiContext* ctx, int priority, Game::menuDef_t* menu); static void AddMenuListToContext(Game::UiContext* ctx, Game::MenuList* list, int close); static void ResetContextHook(int a1);