From 50ebfd2b2558782776c3c2d27791299831bca05c Mon Sep 17 00:00:00 2001 From: INeedGames Date: Sat, 6 Jan 2024 15:14:26 -0600 Subject: [PATCH] fix(gsc): fs_read checks multiple files for includes (#165) Co-authored-by: xensik --- include/xsk/gsc/context.hpp | 2 +- src/gsc/context.cpp | 9 ++++++--- src/tool/main.cpp | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/include/xsk/gsc/context.hpp b/include/xsk/gsc/context.hpp index e1c525ba..4e50b93b 100644 --- a/include/xsk/gsc/context.hpp +++ b/include/xsk/gsc/context.hpp @@ -18,7 +18,7 @@ namespace xsk::gsc class context { public: - using fs_callback = std::function>(std::string const&)>; + using fs_callback = std::function>(context const*, std::string const&)>; context(props props, engine engine, endian endian, system system, u32 str_count); diff --git a/src/gsc/context.cpp b/src/gsc/context.cpp index 974f5f79..a36310c4 100644 --- a/src/gsc/context.cpp +++ b/src/gsc/context.cpp @@ -682,7 +682,7 @@ auto context::load_header(std::string const& name) -> std::tuplefirst, reinterpret_cast(itr->second.data()), itr->second.size() }; } - auto data = fs_callback_(name); + auto data = fs_callback_(this, name); if (data.first.data == nullptr && data.first.size == 0 && !data.second.empty()) { @@ -700,7 +700,7 @@ auto context::load_header(std::string const& name) -> std::tuple bool { try - { + { if (includes_.contains(name)) { return false; @@ -711,7 +711,10 @@ auto context::load_include(std::string const& name) -> bool if (include_cache_.contains(name)) return true; - auto file = fs_callback_(name); + auto filename = name; + filename += (instance_ == gsc::instance::server) ? ".gsc" : ".csc"; + + auto file = fs_callback_(this, filename); if ((file.first.data == nullptr || file.first.size == 0) && file.second.empty()) throw std::runtime_error("empty file"); diff --git a/src/tool/main.cpp b/src/tool/main.cpp index cf69d2c3..5f9d6725 100644 --- a/src/tool/main.cpp +++ b/src/tool/main.cpp @@ -432,16 +432,44 @@ auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> result std::unordered_map> files; -auto fs_read(std::string const& name) -> std::pair> +auto fs_read(context const* ctx, std::string const& name) -> std::pair> { - auto data = utils::file::read(fs::path{ name }); + auto path = fs::path{ name }; + + if (!utils::file::exists(path)) + { + path.replace_extension(""); + + auto id = ctx->token_id(path.string()); + if (id > 0) + { + path = fs::path{ std::to_string(id) + ".gscbin" }; + } + + if (!utils::file::exists(path)) + { + path = fs::path{ path.string() + ".gscbin" }; + } + + if (!utils::file::exists(path)) + { + path = fs::path{ path.string() + ".gsh" }; + } + + if (!utils::file::exists(path)) + { + path = fs::path{ path.string() + ".gsc" }; + } + } + + auto data = utils::file::read(path); - if(name.ends_with(".gscbin") || (!name.ends_with(".gsh") && !name.ends_with(".gsc"))) + if (path.extension().string() == ".gscbin" || (path.extension().string() != ".gsh" && path.extension().string() != ".gsc")) { asset s; s.deserialize(data); auto stk = utils::zlib::decompress(s.buffer, s.len); - auto res = files.insert({ name, std::move(s.bytecode) }); + auto res = files.insert({ path.filename().string(), std::move(s.bytecode)}); if (res.second) {