2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2025-06-30 08:11:49 +00:00

Add load argument to unlinker to load zones before trying to unlink specified zones

this allows to make sure certain ipaks are loaded before dumping
This commit is contained in:
Jan
2021-03-27 16:45:30 +01:00
parent 7d4029b21f
commit 4f995751ce
10 changed files with 158 additions and 81 deletions

View File

@ -36,6 +36,8 @@ class Unlinker::Impl
SearchPathFilesystem* m_last_zone_search_path;
std::set<std::string> m_absolute_search_paths;
std::vector<std::unique_ptr<Zone>> m_loaded_zones;
_NODISCARD bool ShouldLoadObj() const
{
return m_args.m_task != UnlinkerArgs::ProcessingTask::LIST;
@ -102,7 +104,7 @@ class Unlinker::Impl
LoadSearchPath(m_last_zone_search_path);
}
for(auto* iwd : IWD::Repository)
for (auto* iwd : IWD::Repository)
{
searchPathsForZone.IncludeSearchPath(iwd);
}
@ -174,7 +176,7 @@ class Unlinker::Impl
}
}
if(!result)
if (!result)
{
printf("Failed to find writer for zone definition file of zone \"%s\".\n", zone->m_name.c_str());
}
@ -228,7 +230,7 @@ class Unlinker::Impl
context.m_zone = zone;
context.m_base_path = outputFolderPath;
if(m_args.m_use_gdt)
if (m_args.m_use_gdt)
{
if (!OpenGdtFile(zone, zoneDefinitionFileFolder, gdtStream))
return false;
@ -240,7 +242,7 @@ class Unlinker::Impl
ObjWriting::DumpZone(context);
if(m_args.m_use_gdt)
if (m_args.m_use_gdt)
{
context.m_gdt->EndStream();
gdtStream.close();
@ -250,6 +252,101 @@ class Unlinker::Impl
return true;
}
bool LoadZones()
{
for (const auto& zonePath : m_args.m_zones_to_load)
{
if (!fs::is_regular_file(zonePath))
{
printf("Could not find file \"%s\".\n", zonePath.c_str());
continue;
}
auto absoluteZoneDirectory = absolute(std::filesystem::path(zonePath).remove_filename()).string();
auto searchPathsForZone = GetSearchPathsForZone(absoluteZoneDirectory);
searchPathsForZone.IncludeSearchPath(&m_search_paths);
auto zone = ZoneLoading::LoadZone(zonePath);
if (zone == nullptr)
{
printf("Failed to load zone \"%s\".\n", zonePath.c_str());
return false;
}
if (m_args.m_verbose)
{
printf("Loaded zone \"%s\"\n", zone->m_name.c_str());
}
if (ShouldLoadObj())
{
ObjLoading::LoadReferencedContainersForZone(&searchPathsForZone, zone.get());
ObjLoading::LoadObjDataForZone(&searchPathsForZone, zone.get());
}
m_loaded_zones.emplace_back(std::move(zone));
}
return true;
}
void UnloadZones()
{
if (ShouldLoadObj())
{
for (auto& loadedZone : m_loaded_zones)
{
ObjLoading::UnloadContainersOfZone(loadedZone.get());
}
}
m_loaded_zones.clear();
}
bool UnlinkZones()
{
for (const auto& zonePath : m_args.m_zones_to_unlink)
{
if (!fs::is_regular_file(zonePath))
{
printf("Could not find file \"%s\".\n", zonePath.c_str());
continue;
}
auto absoluteZoneDirectory = absolute(std::filesystem::path(zonePath).remove_filename()).string();
auto searchPathsForZone = GetSearchPathsForZone(absoluteZoneDirectory);
searchPathsForZone.IncludeSearchPath(&m_search_paths);
auto zone = ZoneLoading::LoadZone(zonePath);
if (zone == nullptr)
{
printf("Failed to load zone \"%s\".\n", zonePath.c_str());
return false;
}
if (m_args.m_verbose)
{
printf("Loaded zone \"%s\"\n", zone->m_name.c_str());
}
if (ShouldLoadObj())
{
ObjLoading::LoadReferencedContainersForZone(&searchPathsForZone, zone.get());
ObjLoading::LoadObjDataForZone(&searchPathsForZone, zone.get());
}
if (!HandleZone(zone.get()))
return false;
if (ShouldLoadObj())
ObjLoading::UnloadContainersOfZone(zone.get());
}
return true;
}
public:
Impl()
{
@ -267,51 +364,13 @@ public:
if (!BuildSearchPaths())
return false;
for (const auto& zonePath : m_args.m_zones_to_load)
{
if (!fs::is_regular_file(zonePath))
{
printf("Could not find file \"%s\".\n", zonePath.c_str());
continue;
}
if (!LoadZones())
return false;
auto absoluteZoneDirectory = absolute(std::filesystem::path(zonePath).remove_filename()).string();
const auto result = UnlinkZones();
auto searchPathsForZone = GetSearchPathsForZone(absoluteZoneDirectory);
searchPathsForZone.IncludeSearchPath(&m_search_paths);
auto* zone = ZoneLoading::LoadZone(zonePath);
if (zone == nullptr)
{
printf("Failed to load zone \"%s\".\n", zonePath.c_str());
return false;
}
if (m_args.m_verbose)
{
printf("Loaded zone \"%s\"\n", zone->m_name.c_str());
}
if (ShouldLoadObj())
{
ObjLoading::LoadReferencedContainersForZone(&searchPathsForZone, zone);
ObjLoading::LoadObjDataForZone(&searchPathsForZone, zone);
}
if (!HandleZone(zone))
{
return false;
}
if (ShouldLoadObj())
{
ObjLoading::UnloadContainersOfZone(zone);
}
delete zone;
}
return true;
UnloadZones();
return result;
}
};

View File

@ -29,9 +29,17 @@ const CommandLineOption* const OPTION_MINIMAL_ZONE_FILE =
.WithDescription("Minimizes the size of the zone file output by only including assets that are not a dependency of another asset.")
.Build();
const CommandLineOption* const OPTION_LIST =
const CommandLineOption* const OPTION_LOAD =
CommandLineOption::Builder::Create()
.WithShortName("l")
.WithLongName("load")
.WithDescription("Loads an existing zone before trying to unlink any zone.")
.WithParameter("zonePath")
.Reusable()
.Build();
const CommandLineOption* const OPTION_LIST =
CommandLineOption::Builder::Create()
.WithLongName("list")
.WithDescription("Lists the contents of a zone instead of writing them to the disk.")
.Build();
@ -69,6 +77,7 @@ const CommandLineOption* const COMMAND_LINE_OPTIONS[]
OPTION_HELP,
OPTION_VERBOSE,
OPTION_MINIMAL_ZONE_FILE,
OPTION_LOAD,
OPTION_LIST,
OPTION_OUTPUT_FOLDER,
OPTION_SEARCH_PATH,
@ -146,8 +155,8 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv)
return false;
}
m_zones_to_load = m_argument_parser.GetArguments();
const size_t zoneCount = m_zones_to_load.size();
m_zones_to_unlink = m_argument_parser.GetArguments();
const size_t zoneCount = m_zones_to_unlink.size();
if (zoneCount < 1)
{
// No zones to load specified...
@ -162,7 +171,11 @@ bool UnlinkerArgs::ParseArgs(const int argc, const char** argv)
// -min; --minimal-zone
m_minimal_zone_def = m_argument_parser.IsOptionSpecified(OPTION_MINIMAL_ZONE_FILE);
// -l; --list
// -l; --load
if (m_argument_parser.IsOptionSpecified(OPTION_LOAD))
m_zones_to_load = m_argument_parser.GetParametersForOption(OPTION_LOAD);
// --list
if (m_argument_parser.IsOptionSpecified(OPTION_LIST))
m_task = ProcessingTask::LIST;

View File

@ -31,6 +31,7 @@ public:
};
std::vector<std::string> m_zones_to_load;
std::vector<std::string> m_zones_to_unlink;
std::set<std::string> m_user_search_paths;
ProcessingTask m_task;