[Menus] Rewrite how menus are loaded and used to support custom menu lists
This commit is contained in:
parent
4dc912af3e
commit
8080700432
@ -4,7 +4,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::LoadCustomMenuList(name, builder->getAllocator());
|
header->menuList = Components::Menus::LoadMenuList(name);
|
||||||
|
|
||||||
for (int i = 0; i < header->menuList->menuCount; i++)
|
for (int i = 0; i < header->menuList->menuCount; i++)
|
||||||
{
|
{
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace Components
|
namespace Components
|
||||||
{
|
{
|
||||||
std::vector<std::string> Menus::CustomMenus;
|
std::unordered_map<std::string, Game::menuDef_t*> Menus::DiskMenuList;
|
||||||
std::unordered_map<std::string, Game::menuDef_t*> Menus::MenuList;
|
std::unordered_map<std::string, Game::MenuList*> Menus::DiskMenuListList;
|
||||||
std::unordered_map<std::string, Game::MenuList*> Menus::MenuListList;
|
std::unordered_map<std::string, std::pair<int, Game::menuDef_t*>> Menus::UiContextMenus;
|
||||||
|
|
||||||
int Menus::ReserveSourceHandle()
|
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;
|
return menu;
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::MenuList* Menus::LoadCustomMenuList(const std::string& menu, Utils::Memory::Allocator* allocator)
|
|
||||||
{
|
|
||||||
std::vector<std::pair<bool, Game::menuDef_t*>> 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<Game::MenuList>();
|
|
||||||
if (!list) return nullptr;
|
|
||||||
|
|
||||||
list->menus = allocator->allocateArray<Game::menuDef_t*>(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<std::pair<bool, Game::menuDef_t*>> Menus::LoadMenu(const std::string& menu)
|
std::vector<std::pair<bool, Game::menuDef_t*>> Menus::LoadMenu(const std::string& menu)
|
||||||
{
|
{
|
||||||
std::vector<std::pair<bool, Game::menuDef_t*>> menus;
|
std::vector<std::pair<bool, Game::menuDef_t*>> menus;
|
||||||
@ -278,33 +211,12 @@ namespace Components
|
|||||||
return menus;
|
return menus;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::pair<bool, Game::menuDef_t*>> Menus::LoadMenu(Game::menuDef_t* menudef)
|
// Can be used to load scriptmenus or menulists
|
||||||
{
|
Game::MenuList* Menus::LoadMenuList(const std::string& name)
|
||||||
std::vector<std::pair<bool, Game::menuDef_t*>> 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)
|
|
||||||
{
|
{
|
||||||
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
|
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
|
||||||
|
|
||||||
std::vector<std::pair<bool, Game::menuDef_t*>> menus = Menus::LoadMenu(menu);
|
std::vector<std::pair<bool, Game::menuDef_t*>> menus = Menus::LoadMenu(name);
|
||||||
if (menus.empty()) return nullptr;
|
if (menus.empty()) return nullptr;
|
||||||
|
|
||||||
// Allocate new menu list
|
// Allocate new menu list
|
||||||
@ -318,7 +230,7 @@ namespace Components
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
newList->name = allocator->duplicateString(menu);
|
newList->name = allocator->duplicateString(name);
|
||||||
newList->menuCount = menus.size();
|
newList->menuCount = menus.size();
|
||||||
|
|
||||||
// Copy new menus
|
// Copy new menus
|
||||||
@ -327,116 +239,6 @@ namespace Components
|
|||||||
newList->menus[i] = menus[i].second;
|
newList->menus[i] = menus[i].second;
|
||||||
}
|
}
|
||||||
|
|
||||||
Menus::RemoveMenuList(newList->name);
|
|
||||||
Menus::MenuListList[newList->name] = newList;
|
|
||||||
|
|
||||||
return newList;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Menus::SafeMergeMenus(std::vector<std::pair<bool, Game::menuDef_t*>>* menus, std::vector<std::pair<bool, Game::menuDef_t*>> 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<std::pair<bool, Game::menuDef_t*>> 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<Game::MenuList>();
|
|
||||||
if (!newList) return menuList;
|
|
||||||
|
|
||||||
size_t size = menus.size();
|
|
||||||
newList->menus = allocator->allocateArray<Game::menuDef_t*>(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;
|
return newList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -483,31 +285,22 @@ namespace Components
|
|||||||
Game::sourceFiles[handle] = nullptr;
|
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();
|
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
|
||||||
|
|
||||||
// Do i need to free expressions and strings?
|
// Do i need to free expressions and strings?
|
||||||
// Or does the game take care of it?
|
// Or does the game take care of it?
|
||||||
// Seems like it does...
|
// Seems like it does...
|
||||||
|
|
||||||
if (menudef->items)
|
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->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
allocator->free(menudef);
|
allocator->free(menudef);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menus::FreeMenuList(Game::MenuList* menuList)
|
void Menus::FreeDiskMenuList(Game::MenuList* menuList)
|
||||||
{
|
{
|
||||||
if (!menuList) return;
|
if (!menuList) return;
|
||||||
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
|
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
|
||||||
@ -528,123 +321,27 @@ namespace Components
|
|||||||
allocator->free(menuList);
|
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()
|
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)
|
Game::XAssetHeader Menus::MenuFindHook(Game::XAssetType /*type*/, const std::string& filename)
|
||||||
{
|
{
|
||||||
return { Game::Menus_FindByName(Game::uiContext, filename.data()) };
|
return { Game::Menus_FindByName(Game::uiContext, filename.data()) };
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -652,56 +349,14 @@ namespace Components
|
|||||||
{
|
{
|
||||||
Game::XAssetHeader header = { nullptr };
|
Game::XAssetHeader header = { nullptr };
|
||||||
|
|
||||||
// Free the last menulist and ui context, as we have to rebuild it with the new menus
|
// if menulist or scriptmenu exists on the disk, then load it
|
||||||
if (Menus::MenuListList.find(filename) != Menus::MenuListList.end())
|
if (FileSystem::File(filename).exists())
|
||||||
{
|
{
|
||||||
Game::MenuList* list = Menus::MenuListList[filename];
|
header.menuList = Menus::LoadMenuList(filename.data());
|
||||||
|
if (header.menuList) return header;
|
||||||
for (int i = 0; list && list->menus && i < list->menuCount; ++i)
|
|
||||||
{
|
|
||||||
Menus::RemoveMenuFromContext(Game::uiContext, list->menus[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Menus::RemoveMenuList(filename);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Utils::String::EndsWith(filename, ".menu"))
|
// we don't need to modify any base assets here
|
||||||
{
|
|
||||||
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<DWORD>(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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return header;
|
return header;
|
||||||
}
|
}
|
||||||
@ -716,7 +371,7 @@ namespace Components
|
|||||||
|
|
||||||
if (originalConnect == menu) // Check if we draw the original loadscreen
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
@ -727,82 +382,108 @@ namespace Components
|
|||||||
return Game::Menu_IsVisible(dc, menu);
|
return Game::Menu_IsVisible(dc, menu);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menus::RemoveMenuFromContext(Game::UiContext *dc, Game::menuDef_t *menu)
|
void Menus::AddMenuToContext(Game::UiContext* ctx, int priority, 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()
|
|
||||||
{
|
{
|
||||||
Game::UiContext* uiInfoArray = (Game::UiContext*)0x62E2858;
|
if(ctx->menuCount < MAX_MENUS_IN_CONTEXT)
|
||||||
Game::MenuList list;
|
{
|
||||||
|
ctx->Menus[ctx->menuCount++] = menu;
|
||||||
|
}
|
||||||
|
|
||||||
|
Menus::UiContextMenus[menu->window.name] = { priority, menu };
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<int, Game::menuDef_t*> 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<int, Game::menuDef_t*> 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<void()>(0x401700)(); // call original load functions
|
Utils::Hook::Call<void()>(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");
|
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)))
|
if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name)))
|
||||||
{
|
{
|
||||||
// Utils::Hook::Call<void(void*, Game::MenuList*, int)>(0x401700)(uiInfoArray, header.menuList, 1); // add loaded menus
|
Menus::AddMenuListToContext(Game::uiContext, header.menuList, 1);
|
||||||
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++)
|
// attempt to load mod menus
|
||||||
{
|
|
||||||
OutputDebugStringA(Utils::String::VA("%s\n", uiInfoArray->Menus[i]->window.name));
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
header = Game::DB_FindXAssetHeader(Game::XAssetType::ASSET_TYPE_MENULIST, "ui_mp/mod.txt");
|
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)))
|
if (header.data && !(header.menuList->menuCount == 1 && !_stricmp("default_menu", header.menuList->menus[0]->window.name)))
|
||||||
{
|
{
|
||||||
Utils::Hook::Call<void(void*, Game::MenuList*, int)>(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<void(int)>(0x4A57D0)(a1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Menus::Menus()
|
Menus::Menus()
|
||||||
@ -820,7 +501,13 @@ namespace Components
|
|||||||
//Utils::Hook::Nop(0x428E48, 5);
|
//Utils::Hook::Nop(0x428E48, 5);
|
||||||
|
|
||||||
// register custom menufiles if they exist
|
// 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
|
// Use the connect menu open call to update server motds
|
||||||
Utils::Hook(0x428E48, []()
|
Utils::Hook(0x428E48, []()
|
||||||
@ -865,7 +552,7 @@ namespace Components
|
|||||||
// Close all menus
|
// Close all menus
|
||||||
Game::Menus_CloseAll(Game::uiContext);
|
Game::Menus_CloseAll(Game::uiContext);
|
||||||
|
|
||||||
// Free custom menus
|
// Free custom menus and reset uiContext list
|
||||||
Menus::FreeEverything();
|
Menus::FreeEverything();
|
||||||
|
|
||||||
// Only disconnect if in-game, context is updated automatically!
|
// Only disconnect if in-game, context is updated automatically!
|
||||||
@ -875,8 +562,8 @@ namespace Components
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Reinitialize ui context
|
// re-register all menus
|
||||||
Utils::Hook::Call<void()>(0x401700)();
|
Menus::RegisterMenuLists();
|
||||||
|
|
||||||
// Reopen main menu
|
// Reopen main menu
|
||||||
Game::Menus_OpenByName(Game::uiContext, "main_text");
|
Game::Menus_OpenByName(Game::uiContext, "main_text");
|
||||||
@ -891,29 +578,10 @@ namespace Components
|
|||||||
{
|
{
|
||||||
Command::Execute("openmenu quickmessage");
|
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::~Menus()
|
||||||
{
|
{
|
||||||
Menus::CustomMenus.clear();
|
|
||||||
Menus::FreeEverything();
|
Menus::FreeEverything();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define MAX_SOURCEFILES 64
|
#define MAX_SOURCEFILES 64
|
||||||
|
#define MAX_MENUS_IN_CONTEXT 640
|
||||||
#undef LoadMenu
|
#undef LoadMenu
|
||||||
|
|
||||||
namespace Components
|
namespace Components
|
||||||
@ -12,50 +13,48 @@ namespace Components
|
|||||||
~Menus();
|
~Menus();
|
||||||
|
|
||||||
static void FreeEverything();
|
static void FreeEverything();
|
||||||
|
static void RegisterMenuLists();
|
||||||
|
|
||||||
static void Add(const std::string& menu);
|
// used to load assets for zonebuilder
|
||||||
|
static Game::MenuList* LoadMenuList(const std::string& file);
|
||||||
static Game::MenuList* Menus::LoadCustomMenuList(const std::string& menu, Utils::Memory::Allocator* allocator);
|
static std::vector<std::pair<bool, Game::menuDef_t*>> LoadMenu(const std::string& file);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::unordered_map<std::string, Game::menuDef_t*> MenuList;
|
static std::unordered_map<std::string, Game::menuDef_t*> DiskMenuList;
|
||||||
static std::unordered_map<std::string, Game::MenuList*> MenuListList;
|
static std::unordered_map<std::string, Game::MenuList*> DiskMenuListList;
|
||||||
static std::vector<std::string> CustomMenus;
|
static std::unordered_map<std::string, std::pair<int, Game::menuDef_t*>> Menus::UiContextMenus;
|
||||||
|
|
||||||
static Game::XAssetHeader MenuFindHook(Game::XAssetType type, const std::string& filename);
|
// Loading
|
||||||
static Game::XAssetHeader MenuListFindHook(Game::XAssetType type, const std::string& filename);
|
static int ReserveSourceHandle();
|
||||||
|
static bool IsValidSourceHandle(int handle);
|
||||||
static Game::MenuList* LoadMenuList(Game::MenuList* menuList);
|
static Game::menuDef_t* ParseMenu(int handle);
|
||||||
static Game::MenuList* LoadScriptMenu(const char* menu);
|
static Game::script_t* LoadMenuScript(const std::string& name, const std::string& buffer);
|
||||||
static std::vector<std::pair<bool, Game::menuDef_t*>> LoadMenu(Game::menuDef_t* menudef);
|
static int LoadMenuSource(const std::string& name, const std::string& buffer);
|
||||||
static std::vector<std::pair<bool, Game::menuDef_t*>> LoadMenu(const std::string& file);
|
|
||||||
static void SafeMergeMenus(std::vector<std::pair<bool, Game::menuDef_t*>>* menus, std::vector<std::pair<bool, Game::menuDef_t*>> 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);
|
|
||||||
|
|
||||||
|
// Freeing
|
||||||
static void FreeMenuSource(int handle);
|
static void FreeMenuSource(int handle);
|
||||||
|
static void FreeDiskMenuList(Game::MenuList* menuList);
|
||||||
|
static void FreeDiskMenu(Game::menuDef_t* menudef);
|
||||||
|
|
||||||
static void FreeMenuList(Game::MenuList* menuList);
|
// Etc.
|
||||||
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);
|
|
||||||
|
|
||||||
static bool IsMenuVisible(Game::UiContext *dc, Game::menuDef_t *menu);
|
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<int, Game::menuDef_t*> 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!
|
// Ugly!
|
||||||
static int KeywordHash(char* key);
|
static int KeywordHash(char* key);
|
||||||
|
@ -148,6 +148,7 @@ namespace Game
|
|||||||
Load_snd_alias_list_nameArray_t Load_snd_alias_list_nameArray = Load_snd_alias_list_nameArray_t(0x4499F0);
|
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_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_OpenByName_t Menus_OpenByName = Menus_OpenByName_t(0x4CCE60);
|
||||||
Menus_FindByName_t Menus_FindByName = Menus_FindByName_t(0x487240);
|
Menus_FindByName_t Menus_FindByName = Menus_FindByName_t(0x487240);
|
||||||
Menu_IsVisible_t Menu_IsVisible = Menu_IsVisible_t(0x4D77D0);
|
Menu_IsVisible_t Menu_IsVisible = Menu_IsVisible_t(0x4D77D0);
|
||||||
|
@ -373,6 +373,9 @@ namespace Game
|
|||||||
typedef void(__cdecl * Menus_CloseAll_t)(UiContext *dc);
|
typedef void(__cdecl * Menus_CloseAll_t)(UiContext *dc);
|
||||||
extern Menus_CloseAll_t Menus_CloseAll;
|
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);
|
typedef int(__cdecl * Menus_OpenByName_t)(UiContext *dc, const char *p);
|
||||||
extern Menus_OpenByName_t Menus_OpenByName;
|
extern Menus_OpenByName_t Menus_OpenByName;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user