mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 08:05:45 +00:00
Only load menu files once
This commit is contained in:
parent
1e3946974c
commit
b5475e30f0
@ -0,0 +1,17 @@
|
||||
#include "AssetLoaderMenuDef.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "ObjLoading.h"
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "Pool/GlobalAssetPool.h"
|
||||
|
||||
using namespace IW4;
|
||||
|
||||
void* AssetLoaderMenuDef::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
|
||||
{
|
||||
auto* menu = memory->Create<menuDef_t>();
|
||||
memset(menu, 0, sizeof(menuDef_t));
|
||||
menu->window.name = memory->Dup(assetName.c_str());
|
||||
return menu;
|
||||
}
|
@ -2,7 +2,6 @@
|
||||
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "AssetLoading/BasicAssetLoader.h"
|
||||
#include "AssetLoading/IAssetLoadingManager.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
namespace IW4
|
||||
@ -11,7 +10,5 @@ namespace IW4
|
||||
{
|
||||
public:
|
||||
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
||||
_NODISCARD bool CanLoadFromRaw() const override;
|
||||
bool LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override;
|
||||
};
|
||||
}
|
||||
|
@ -12,6 +12,102 @@
|
||||
|
||||
using namespace IW4;
|
||||
|
||||
namespace IW4
|
||||
{
|
||||
class MenuLoader
|
||||
{
|
||||
public:
|
||||
static bool ProcessParsedResults(const std::string& fileName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, menu::ParsingResult* parsingResult,
|
||||
menu::MenuAssetZoneState* zoneState, MenuConversionZoneState* conversionState, std::vector<menuDef_t*>& menus, std::vector<XAssetInfoGeneric*>& menuListDependencies)
|
||||
{
|
||||
const auto menuCount = parsingResult->m_menus.size();
|
||||
const auto functionCount = parsingResult->m_functions.size();
|
||||
const auto menuLoadCount = parsingResult->m_menus_to_load.size();
|
||||
auto totalItemCount = 0u;
|
||||
for (const auto& menu : parsingResult->m_menus)
|
||||
totalItemCount += menu->m_items.size();
|
||||
|
||||
std::cout << "Successfully read menu file \"" << fileName << "\" (" << menuLoadCount << " loads, " << menuCount << " menus, " << functionCount << " functions, " << totalItemCount << " items)\n";
|
||||
|
||||
// Add all functions to the zone state to make them available for all menus to be converted
|
||||
for (auto& function : parsingResult->m_functions)
|
||||
zoneState->AddFunction(std::move(function));
|
||||
|
||||
// Prepare a list of all menus of this file
|
||||
std::vector<menuDef_t*> allMenusOfFile;
|
||||
allMenusOfFile.reserve(parsingResult->m_menus.size());
|
||||
|
||||
// Convert all menus and add them as assets
|
||||
for (auto& menu : parsingResult->m_menus)
|
||||
{
|
||||
MenuConverter converter(ObjLoading::Configuration.MenuNoOptimization, searchPath, memory, manager);
|
||||
auto* menuAsset = converter.ConvertMenu(*menu);
|
||||
if (menuAsset == nullptr)
|
||||
{
|
||||
std::cout << "Failed to convert menu file \"" << menu->m_name << "\"\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
menus.push_back(menuAsset);
|
||||
allMenusOfFile.push_back(menuAsset);
|
||||
auto* menuAssetInfo = manager->AddAsset(ASSET_TYPE_MENU, menu->m_name, menuAsset, std::move(converter.GetDependencies()), std::vector<scr_string_t>());
|
||||
|
||||
if (menuAssetInfo)
|
||||
menuListDependencies.push_back(menuAssetInfo);
|
||||
|
||||
zoneState->AddMenu(std::move(menu));
|
||||
}
|
||||
|
||||
// Register this file with all loaded menus
|
||||
conversionState->AddLoadedFile(fileName, std::move(allMenusOfFile));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static MenuList* CreateMenuListAsset(const std::string& assetName, MemoryManager* memory, const std::vector<menuDef_t*>& menus)
|
||||
{
|
||||
auto* menuListAsset = memory->Create<MenuList>();
|
||||
menuListAsset->name = memory->Dup(assetName.c_str());
|
||||
menuListAsset->menuCount = static_cast<int>(menus.size());
|
||||
|
||||
if (menuListAsset->menuCount > 0)
|
||||
{
|
||||
menuListAsset->menus = static_cast<menuDef_t**>(memory->Alloc(sizeof(uintptr_t) * menuListAsset->menuCount));
|
||||
for (auto i = 0; i < menuListAsset->menuCount; i++)
|
||||
menuListAsset->menus[i] = menus[i];
|
||||
}
|
||||
else
|
||||
menuListAsset->menus = nullptr;
|
||||
|
||||
return menuListAsset;
|
||||
}
|
||||
|
||||
static std::unique_ptr<menu::ParsingResult> ParseMenuFile(const std::string& menuFileName, ISearchPath* searchPath, const menu::MenuAssetZoneState* zoneState)
|
||||
{
|
||||
const auto file = searchPath->Open(menuFileName);
|
||||
if (!file.IsOpen())
|
||||
{
|
||||
std::cerr << "Failed to open menu file \"" << menuFileName << "\"\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
menu::MenuFileReader reader(*file.m_stream, menuFileName, menu::FeatureLevel::IW4, [searchPath](const std::string& filename, const std::string& sourceFile) -> std::unique_ptr<std::istream>
|
||||
{
|
||||
auto foundFileToInclude = searchPath->Open(filename);
|
||||
if (!foundFileToInclude.IsOpen() || !foundFileToInclude.m_stream)
|
||||
return nullptr;
|
||||
|
||||
return std::move(foundFileToInclude.m_stream);
|
||||
});
|
||||
|
||||
reader.IncludeZoneState(zoneState);
|
||||
reader.SetPermissiveMode(ObjLoading::Configuration.MenuPermissiveParsing);
|
||||
|
||||
return reader.ReadMenuFile();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void* AssetLoaderMenuList::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
|
||||
{
|
||||
auto* menuList = memory->Create<MenuList>();
|
||||
@ -25,130 +121,45 @@ bool AssetLoaderMenuList::CanLoadFromRaw() const
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AssetLoaderMenuList::ShouldLoadMenuFile(const std::string& menuFilePath, menu::MenuAssetZoneState* zoneState)
|
||||
{
|
||||
const auto alreadyLoadedFile = zoneState->m_loaded_files.find(menuFilePath);
|
||||
if (alreadyLoadedFile == zoneState->m_loaded_files.end())
|
||||
{
|
||||
zoneState->AddLoadedFile(menuFilePath);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void AssetLoaderMenuList::AddMenuFilesToLoadToQueue(std::deque<std::string>& queue, const menu::ParsingResult* parsingResult, menu::MenuAssetZoneState* zoneState)
|
||||
{
|
||||
for(const auto& menuFileToLoad : parsingResult->m_menus_to_load)
|
||||
{
|
||||
if(ShouldLoadMenuFile(menuFileToLoad, zoneState))
|
||||
{
|
||||
queue.push_back(menuFileToLoad);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool AssetLoaderMenuList::ProcessParsedResults(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, menu::ParsingResult* parsingResult,
|
||||
menu::MenuAssetZoneState* zoneState, std::vector<menuDef_t*>& menus, std::vector<XAssetInfoGeneric*>& menuListDependencies)
|
||||
{
|
||||
const auto menuCount = parsingResult->m_menus.size();
|
||||
const auto functionCount = parsingResult->m_functions.size();
|
||||
const auto menuLoadCount = parsingResult->m_menus_to_load.size();
|
||||
auto totalItemCount = 0u;
|
||||
for (const auto& menu : parsingResult->m_menus)
|
||||
totalItemCount += menu->m_items.size();
|
||||
|
||||
std::cout << "Successfully read menu file \"" << assetName << "\" (" << menuLoadCount << " loads, " << menuCount << " menus, " << functionCount << " functions, " << totalItemCount << " items)\n";
|
||||
|
||||
for (auto& function : parsingResult->m_functions)
|
||||
zoneState->AddFunction(std::move(function));
|
||||
|
||||
for (auto& menu : parsingResult->m_menus)
|
||||
{
|
||||
MenuConverter converter(ObjLoading::Configuration.MenuNoOptimization, searchPath, memory, manager);
|
||||
auto* menuAsset = converter.ConvertMenu(*menu);
|
||||
if(menuAsset == nullptr)
|
||||
{
|
||||
std::cout << "Failed to convert menu \"" << menu->m_name << "\"\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
menus.push_back(menuAsset);
|
||||
auto* menuAssetInfo = manager->AddAsset(ASSET_TYPE_MENU, menu->m_name, menuAsset, std::move(converter.GetDependencies()), std::vector<scr_string_t>());
|
||||
|
||||
if (menuAssetInfo)
|
||||
menuListDependencies.push_back(menuAssetInfo);
|
||||
|
||||
zoneState->AddMenu(std::move(menu));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
MenuList* AssetLoaderMenuList::CreateMenuListAsset(const std::string& assetName, MemoryManager* memory, const std::vector<menuDef_t*>& menus)
|
||||
{
|
||||
auto* menuListAsset = memory->Create<MenuList>();
|
||||
menuListAsset->name = memory->Dup(assetName.c_str());
|
||||
menuListAsset->menuCount = static_cast<int>(menus.size());
|
||||
|
||||
if (menuListAsset->menuCount > 0)
|
||||
{
|
||||
menuListAsset->menus = static_cast<menuDef_t**>(memory->Alloc(sizeof(uintptr_t) * menuListAsset->menuCount));
|
||||
for(auto i = 0; i < menuListAsset->menuCount; i++)
|
||||
menuListAsset->menus[i] = menus[i];
|
||||
}
|
||||
else
|
||||
menuListAsset->menus = nullptr;
|
||||
|
||||
return menuListAsset;
|
||||
}
|
||||
|
||||
bool AssetLoaderMenuList::LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
|
||||
{
|
||||
std::deque<std::string> menuFileQueue;
|
||||
std::vector<menuDef_t*> menus;
|
||||
std::vector<XAssetInfoGeneric*> menuListDependencies;
|
||||
|
||||
auto* zoneState = manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<menu::MenuAssetZoneState>();
|
||||
menuFileQueue.push_back(assetName);
|
||||
auto* conversionState = manager->GetAssetLoadingContext()->GetZoneAssetLoaderState<MenuConversionZoneState>();
|
||||
|
||||
while (!menuFileQueue.empty())
|
||||
const auto menuListResult = MenuLoader::ParseMenuFile(assetName, searchPath, zoneState);
|
||||
if (menuListResult)
|
||||
MenuLoader::ProcessParsedResults(assetName, searchPath, memory, manager, menuListResult.get(), zoneState, conversionState, menus, menuListDependencies);
|
||||
else
|
||||
std::cerr << "Could not read menu list file \"" << assetName << "\"\n";
|
||||
|
||||
for(const auto& menuFileToLoad : menuListResult->m_menus_to_load)
|
||||
{
|
||||
const auto& nextMenuFile = menuFileQueue.front();
|
||||
const auto file = searchPath->Open(nextMenuFile);
|
||||
if (!file.IsOpen())
|
||||
const auto alreadyLoadedMenuFile = conversionState->m_menus_by_filename.find(menuFileToLoad);
|
||||
if(alreadyLoadedMenuFile != conversionState->m_menus_by_filename.end())
|
||||
{
|
||||
std::cout << "Failed to open menu file \"" << nextMenuFile << "\"\n";
|
||||
return false;
|
||||
std::cout << "Already loaded \"" << menuFileToLoad << "\", skipping\n";
|
||||
for (auto* menu : alreadyLoadedMenuFile->second)
|
||||
menus.push_back(menu);
|
||||
continue;
|
||||
}
|
||||
|
||||
menu::MenuFileReader reader(*file.m_stream, nextMenuFile, menu::FeatureLevel::IW4, [searchPath](const std::string& filename, const std::string& sourceFile) -> std::unique_ptr<std::istream>
|
||||
{
|
||||
auto foundFileToInclude = searchPath->Open(filename);
|
||||
if (!foundFileToInclude.IsOpen() || !foundFileToInclude.m_stream)
|
||||
return nullptr;
|
||||
|
||||
return std::move(foundFileToInclude.m_stream);
|
||||
});
|
||||
|
||||
reader.IncludeZoneState(zoneState);
|
||||
reader.SetPermissiveMode(ObjLoading::Configuration.MenuPermissiveParsing);
|
||||
|
||||
const auto menuFileResult = reader.ReadMenuFile();
|
||||
const auto menuFileResult = MenuLoader::ParseMenuFile(menuFileToLoad, searchPath, zoneState);
|
||||
if (menuFileResult)
|
||||
{
|
||||
ProcessParsedResults(nextMenuFile, searchPath, memory, manager, menuFileResult.get(), zoneState, menus, menuListDependencies);
|
||||
AddMenuFilesToLoadToQueue(menuFileQueue, menuFileResult.get(), zoneState);
|
||||
MenuLoader::ProcessParsedResults(menuFileToLoad, searchPath, memory, manager, menuFileResult.get(), zoneState, conversionState, menus, menuListDependencies);
|
||||
if (!menuFileResult->m_menus_to_load.empty())
|
||||
std::cout << "WARNING: Menu file has menus to load even though it is not a menu list, ignoring: \"" << menuFileToLoad << "\"\n";
|
||||
}
|
||||
else
|
||||
std::cout << "Could not read menu file \"" << nextMenuFile << "\"\n";
|
||||
|
||||
menuFileQueue.pop_front();
|
||||
std::cerr << "Could not read menu file \"" << menuFileToLoad << "\"\n";
|
||||
}
|
||||
|
||||
auto* menuListAsset = CreateMenuListAsset(assetName, memory, menus);
|
||||
auto* menuListAsset = MenuLoader::CreateMenuListAsset(assetName, memory, menus);
|
||||
|
||||
if(menuListAsset)
|
||||
if (menuListAsset)
|
||||
manager->AddAsset(ASSET_TYPE_MENULIST, assetName, menuListAsset);
|
||||
|
||||
return true;
|
||||
|
@ -1,23 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <deque>
|
||||
|
||||
#include "Game/IW4/IW4.h"
|
||||
#include "AssetLoading/BasicAssetLoader.h"
|
||||
#include "AssetLoading/IAssetLoadingManager.h"
|
||||
#include "Parsing/Menu/MenuAssetZoneState.h"
|
||||
#include "Parsing/Menu/Domain/MenuParsingResult.h"
|
||||
#include "SearchPath/ISearchPath.h"
|
||||
|
||||
namespace IW4
|
||||
{
|
||||
class AssetLoaderMenuList final : public BasicAssetLoader<ASSET_TYPE_MENULIST, MenuList>
|
||||
{
|
||||
static bool ShouldLoadMenuFile(const std::string& menuFilePath, menu::MenuAssetZoneState* zoneState);
|
||||
static void AddMenuFilesToLoadToQueue(std::deque<std::string>& queue, const menu::ParsingResult* parsingResult, menu::MenuAssetZoneState* zoneState);
|
||||
static bool ProcessParsedResults(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, menu::ParsingResult* parsingResult, menu::MenuAssetZoneState* zoneState, std::vector<menuDef_t*>& menus, std::vector<XAssetInfoGeneric*>& menuListDependencies);
|
||||
static MenuList* CreateMenuListAsset(const std::string& assetName, MemoryManager* memory, const std::vector<menuDef_t*>& menus);
|
||||
|
||||
public:
|
||||
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
|
||||
_NODISCARD bool CanLoadFromRaw() const override;
|
||||
|
@ -73,6 +73,11 @@ const char* MenuConversionZoneState::AddString(const std::string& str)
|
||||
return strDuped;
|
||||
}
|
||||
|
||||
void MenuConversionZoneState::AddLoadedFile(std::string loadedFileName, std::vector<menuDef_t*> menusOfFile)
|
||||
{
|
||||
m_menus_by_filename.emplace(std::make_pair(std::move(loadedFileName), std::move(menusOfFile)));
|
||||
}
|
||||
|
||||
void MenuConversionZoneState::FinalizeSupportingData() const
|
||||
{
|
||||
auto* memory = m_zone->GetMemory();
|
||||
|
@ -20,6 +20,7 @@ namespace IW4
|
||||
std::map<std::string, const char*> m_strings_by_value;
|
||||
|
||||
public:
|
||||
std::map<std::string, std::vector<menuDef_t*>> m_menus_by_filename;
|
||||
ExpressionSupportingData* m_supporting_data;
|
||||
|
||||
MenuConversionZoneState();
|
||||
@ -31,6 +32,8 @@ namespace IW4
|
||||
size_t AddStaticDvar(const std::string& dvarName);
|
||||
const char* AddString(const std::string& str);
|
||||
|
||||
void AddLoadedFile(std::string loadedFileName, std::vector<menuDef_t*> menusOfFile);
|
||||
|
||||
void FinalizeSupportingData() const;
|
||||
};
|
||||
}
|
||||
|
@ -2,11 +2,6 @@
|
||||
|
||||
using namespace menu;
|
||||
|
||||
void MenuAssetZoneState::AddLoadedFile(std::string loadedFileName)
|
||||
{
|
||||
m_loaded_files.emplace(std::move(loadedFileName));
|
||||
}
|
||||
|
||||
void MenuAssetZoneState::AddFunction(std::unique_ptr<CommonFunctionDef> function)
|
||||
{
|
||||
m_functions_by_name.emplace(std::make_pair(function->m_name, function.get()));
|
||||
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include "AssetLoading/IZoneAssetLoaderState.h"
|
||||
@ -12,7 +11,6 @@ namespace menu
|
||||
class MenuAssetZoneState final : public IZoneAssetLoaderState
|
||||
{
|
||||
public:
|
||||
std::set<std::string> m_loaded_files;
|
||||
std::vector<std::unique_ptr<CommonFunctionDef>> m_functions;
|
||||
std::vector<std::unique_ptr<CommonMenuDef>> m_menus;
|
||||
|
||||
@ -20,7 +18,6 @@ namespace menu
|
||||
|
||||
MenuAssetZoneState() = default;
|
||||
|
||||
void AddLoadedFile(std::string loadedFileName);
|
||||
void AddFunction(std::unique_ptr<CommonFunctionDef> function);
|
||||
void AddMenu(std::unique_ptr<CommonMenuDef> menu);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user