Merge branch 'feature/custom-menulists' into 'develop'

Fix menus in Fastfiles and allow custom menu definitions via fastfile
This commit is contained in:
TheApadayo 2019-01-25 00:58:37 +01:00
commit a41e3fd012
10 changed files with 291 additions and 463 deletions

View File

@ -2,6 +2,36 @@
namespace Assets
{
void IMenuList::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* builder)
{
Utils::Memory::Allocator* allocator = builder->getAllocator();
// actually gets the whole list
std::vector<Game::menuDef_t*> menus = Components::Menus::LoadMenu(name);
if (menus.empty()) return;
// Allocate new menu list
Game::MenuList* newList = allocator->allocate<Game::MenuList>();
if (!newList) return;
newList->menus = allocator->allocateArray<Game::menuDef_t*>(menus.size());
if (!newList->menus)
{
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)
{
Game::MenuList *asset = header.menuList;

View File

@ -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;
};
}

View File

@ -2,6 +2,21 @@
namespace Assets
{
std::unordered_map<std::string, Game::menuDef_t*> ImenuDef_t::LoadedMenus;
void ImenuDef_t::load(Game::XAssetHeader* header, const std::string& name, Components::ZoneBuilder::Zone* /*builder*/)
{
// 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];
}
void ImenuDef_t::mark(Game::XAssetHeader header, Components::ZoneBuilder::Zone* builder)
{
Game::menuDef_t *asset = header.menu;
@ -37,6 +52,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<Game::ExpressionSupportingData>();
@ -107,13 +126,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<Game::Statement_s>();
buffer->save(asset);
@ -121,6 +148,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
@ -130,6 +160,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)
@ -159,13 +192,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)
@ -173,6 +216,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<Game::MenuEventHandlerSet>();
buffer->save(asset);
@ -191,6 +238,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<Game::MenuEventHandler>();
@ -278,11 +328,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)
@ -290,6 +346,10 @@ namespace Assets
AssertSize(Game::ItemKeyHandler, 12);
Utils::Stream* buffer = builder->getBuffer();
#ifdef WRITE_LOGS
buffer->enterStruct("ItemKeyHandler");
#endif
while (asset)
{
// Write header
@ -313,6 +373,9 @@ namespace Assets
// Next key handler
asset = asset->next;
}
#ifdef WRITE_LOGS
buffer->leaveStruct();
#endif
}
#define EVENTHANDLERSET(__indice) \
@ -340,6 +403,10 @@ namespace Assets
Utils::Stream* buffer = builder->getBuffer();
#ifdef WRITE_LOGS
buffer->enterStruct("itemDefData_t");
#endif
// feeder
if (type == 6)
{
@ -421,6 +488,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)
@ -430,6 +501,15 @@ namespace Assets
Utils::Stream* buffer = builder->getBuffer();
Game::itemDef_s* dest = buffer->dest<Game::itemDef_s>();
#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
@ -458,6 +538,7 @@ namespace Assets
buffer->saveString(asset->dvar);
Utils::Stream::ClearPointer(&dest->dvar);
}
if (asset->dvarTest)
{
buffer->saveString(asset->dvarTest);
@ -478,6 +559,7 @@ namespace Assets
buffer->saveString(asset->enableDvar);
Utils::Stream::ClearPointer(&dest->enableDvar);
}
if (asset->localVar)
{
buffer->saveString(asset->localVar);
@ -500,6 +582,9 @@ namespace Assets
if (asset->floatExpressions)
{
buffer->align(Utils::Stream::ALIGN_4);
#ifdef WRITE_LOGS
buffer->enterStruct("floatExpressions");
#endif
Game::ItemFloatExpression* destExp = buffer->dest<Game::ItemFloatExpression>();
buffer->saveArray(asset->floatExpressions, asset->floatExpressionCount);
@ -508,10 +593,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
@ -519,16 +608,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<Game::menuDef_t>();
buffer->save(asset);
buffer->pushBlock(Game::XFILE_BLOCK_VIRTUAL);
@ -603,6 +699,9 @@ namespace Assets
}
}
}
#ifdef WRITE_LOGS
buffer->leaveStruct();
#endif
buffer->popBlock();
}

View File

@ -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<std::string, Game::menuDef_t*> LoadedMenus;
private:
template <typename T> void save_windowDef_t(Game::windowDef_t* asset, T* dest, Components::ZoneBuilder::Zone* builder)

View File

@ -2,9 +2,7 @@
namespace Components
{
std::vector<std::string> Menus::CustomMenus;
std::unordered_map<std::string, Game::menuDef_t*> Menus::MenuList;
std::unordered_map<std::string, Game::MenuList*> Menus::MenuListList;
std::unordered_map<std::string, Game::menuDef_t*> Menus::DiskMenuList;
int Menus::ReserveSourceHandle()
{
@ -166,16 +164,12 @@ namespace Components
}
}
Menus::OverrideMenu(menu);
Menus::RemoveMenu(menu->window.name);
Menus::MenuList[menu->window.name] = menu;
return menu;
}
std::vector<std::pair<bool, Game::menuDef_t*>> Menus::LoadMenu(const std::string& menu)
std::vector<Game::menuDef_t*> Menus::LoadMenu(const std::string& menu)
{
std::vector<std::pair<bool, Game::menuDef_t*>> menus;
std::vector<Game::menuDef_t*> menus;
FileSystem::File menuFile(menu);
if (menuFile.exists())
@ -204,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
}
}
@ -212,171 +206,15 @@ namespace Components
}
}
return menus;
}
std::vector<std::pair<bool, Game::menuDef_t*>> Menus::LoadMenu(Game::menuDef_t* menudef)
// store loaded menus to be freed later
for (auto it = menus.begin(); it != menus.end(); ++it)
{
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
// }
Menus::DiskMenuList[(*it)->window.name] = *it;
}
return menus;
}
Game::MenuList* Menus::LoadScriptMenu(const char* menu)
{
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
std::vector<std::pair<bool, Game::menuDef_t*>> menus = Menus::LoadMenu(menu);
if (menus.empty()) return nullptr;
// Allocate new menu list
Game::MenuList* newList = allocator->allocate<Game::MenuList>();
if (!newList) return nullptr;
newList->menus = allocator->allocateArray<Game::menuDef_t*>(menus.size());
if (!newList->menus)
{
allocator->free(newList);
return nullptr;
}
newList->name = allocator->duplicateString(menu);
newList->menuCount = menus.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;
}
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;
}
void Menus::FreeMenuSource(int handle)
{
Utils::Memory::Allocator* allocator = Utils::Memory::GetAllocator();
@ -420,227 +258,29 @@ 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)
{
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::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::DiskMenuList.begin(); i != Menus::DiskMenuList.end(); ++i)
{
Menus::FreeMenuList(i->second);
Menus::FreeDiskMenu(i->second);
}
Menus::MenuListList.clear();
for (auto i = Menus::MenuList.begin(); i != Menus::MenuList.end(); ++i)
{
Menus::FreeMenu(i->second);
}
Menus::MenuList.clear();
}
Game::XAssetHeader Menus::MenuLoad(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 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())
{
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);
}
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<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;
Menus::DiskMenuList.clear();
}
bool Menus::IsMenuVisible(Game::UiContext *dc, Game::menuDef_t *menu)
@ -653,7 +293,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;
}
@ -664,34 +304,110 @@ namespace Components
return Game::Menu_IsVisible(dc, menu);
}
void Menus::RemoveMenuFromContext(Game::UiContext *dc, Game::menuDef_t *menu)
void Menus::AddMenuListToContext(Game::UiContext* ctx, Game::MenuList* list, int close)
{
// Search menu in context
int i = 0;
for (; i < dc->menuCount; ++i)
// scriptmenu
if (std::string(list->name).find(".menu") != std::string::npos)
{
if (dc->Menus[i] == menu)
auto menus = Menus::LoadMenu(list->name);
if (menus.size())
{
break;
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);
}
}
// 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;
return; // don't add original menus
}
}
void Menus::Add(const std::string& menu)
for (int i = 0; i < list->menuCount; i++)
{
Menus::CustomMenus.push_back(menu);
Game::menuDef_t* cur = list->menus[i];
if (cur->window.name == reinterpret_cast<const char*>(0xDDDDDDDD))
{
DebugBreak();
}
auto menus = Menus::LoadMenu(Utils::String::VA("ui_mp/%s.menu", cur->window.name));
if (menus.size())
{
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)
{
Game::Menus_CloseRequest(ctx, cur);
}
}
}
void Menus::RegisterMenuLists()
{
Utils::Hook::Call<void()>(0x401700)(); // reset ui context
// 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
auto loadCustomMenus = []()
{
// 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);
}
};
if (!FastFiles::Ready())
{
Scheduler::OnReady(loadCustomMenus, true);
}
else
{
loadCustomMenus();
}
}
void Menus::ResetContextHook(int a1)
{
// reset our lists
Menus::FreeEverything();
// continue with initialization
Utils::Hook::Call<void(int)>(0x4A57D0)(a1);
}
Menus::Menus()
@ -701,13 +417,18 @@ namespace Components
// Ensure everything is zero'ed
Menus::FreeEverything();
// Intercept asset finding
AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENU, Menus::MenuLoad);
AssetHandler::OnFind(Game::XAssetType::ASSET_TYPE_MENULIST, Menus::MenuFileLoad);
// Don't open connect menu
//Utils::Hook::Nop(0x428E48, 5);
// register custom menufiles if they exist
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, []()
{
@ -751,7 +472,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!
@ -761,8 +482,7 @@ namespace Components
}
else
{
// Reinitialize ui context
Utils::Hook::Call<void()>(0x401700)();
Menus::RegisterMenuLists(); // register custom menus
// Reopen main menu
Game::Menus_OpenByName(Game::uiContext, "main_text");
@ -777,27 +497,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();
}
}

View File

@ -1,6 +1,7 @@
#pragma once
#define MAX_SOURCEFILES 64
#define MAX_MENUS_IN_CONTEXT 640
#undef LoadMenu
namespace Components
@ -12,46 +13,31 @@ namespace Components
~Menus();
static void FreeEverything();
static void RegisterMenuLists();
static void Add(const std::string& menu);
// used to load assets for zonebuilder
static std::vector<Game::menuDef_t*> LoadMenu(const std::string& file);
private:
static std::unordered_map<std::string, Game::menuDef_t*> MenuList;
static std::unordered_map<std::string, Game::MenuList*> MenuListList;
static std::vector<std::string> 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::MenuList* LoadMenuList(Game::MenuList* menuList);
static Game::MenuList* LoadScriptMenu(const char* menu);
static std::vector<std::pair<bool, Game::menuDef_t*>> LoadMenu(Game::menuDef_t* menudef);
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 std::unordered_map<std::string, Game::menuDef_t*> DiskMenuList;
// 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);
static int ReserveSourceHandle();
static bool IsValidSourceHandle(int handle);
static Game::menuDef_t* ParseMenu(int handle);
// Freeing
static void FreeMenuSource(int handle);
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);
// Manage menus in uiContext
static void AddMenuListToContext(Game::UiContext* ctx, Game::MenuList* list, int close);
static void ResetContextHook(int a1);
// Ugly!
static int KeywordHash(char* key);

View File

@ -871,6 +871,10 @@ namespace Components
// defaults need to load before we do this
Utils::Hook::Call<void()>(0x4E1F30)(); // G_SetupWeaponDef
Utils::Hook::Call<void()>(0x4454C0)(); // Item_SetupKeywordHash (for loading menus)
Utils::Hook::Call<void()>(0x501BC0)(); // Menu_SetupKeywordHash (for loading menus)
Utils::Hook::Call<void()>(0x4A1280)(); // something related to uiInfoArray
Utils::Hook::Call<void(const char*)>(0x464A90)(GetCommandLineA()); // Com_ParseCommandLine
Utils::Hook::Call<void()>(0x60C3D0)(); // Com_AddStartupCommands

View File

@ -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);

View File

@ -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;

View File

@ -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
}