fix(gsc): fs_read checks multiple files for includes (#165)

Co-authored-by: xensik <xensik@pm.me>
This commit is contained in:
INeedGames 2024-01-06 15:14:26 -06:00 committed by GitHub
parent b41a9f7d54
commit 50ebfd2b25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 8 deletions

View File

@ -18,7 +18,7 @@ namespace xsk::gsc
class context class context
{ {
public: public:
using fs_callback = std::function<std::pair<buffer, std::vector<u8>>(std::string const&)>; using fs_callback = std::function<std::pair<buffer, std::vector<u8>>(context const*, std::string const&)>;
context(props props, engine engine, endian endian, system system, u32 str_count); context(props props, engine engine, endian endian, system system, u32 str_count);

View File

@ -682,7 +682,7 @@ auto context::load_header(std::string const& name) -> std::tuple<std::string con
return { &itr->first, reinterpret_cast<char const*>(itr->second.data()), itr->second.size() }; return { &itr->first, reinterpret_cast<char const*>(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()) 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<std::string con
auto context::load_include(std::string const& name) -> bool auto context::load_include(std::string const& name) -> bool
{ {
try try
{ {
if (includes_.contains(name)) if (includes_.contains(name))
{ {
return false; return false;
@ -711,7 +711,10 @@ auto context::load_include(std::string const& name) -> bool
if (include_cache_.contains(name)) if (include_cache_.contains(name))
return true; 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()) if ((file.first.data == nullptr || file.first.size == 0) && file.second.empty())
throw std::runtime_error("empty file"); throw std::runtime_error("empty file");

View File

@ -432,16 +432,44 @@ auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> result
std::unordered_map<std::string, std::vector<std::uint8_t>> files; std::unordered_map<std::string, std::vector<std::uint8_t>> files;
auto fs_read(std::string const& name) -> std::pair<buffer, std::vector<u8>> auto fs_read(context const* ctx, std::string const& name) -> std::pair<buffer, std::vector<u8>>
{ {
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; asset s;
s.deserialize(data); s.deserialize(data);
auto stk = utils::zlib::decompress(s.buffer, s.len); 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) if (res.second)
{ {