Skip to content

Commit

Permalink
fix(gsc): fs_read checks multiple files for includes
Browse files Browse the repository at this point in the history
  • Loading branch information
ineed bots committed Dec 26, 2023
1 parent cbfcce1 commit 388fd63
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion include/xsk/gsc/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace xsk::gsc
class context
{
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>>(std::string const&, context&)>;

context(props props, engine engine, endian endian, system system, u32 str_count);

Expand Down
4 changes: 2 additions & 2 deletions src/gsc/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
}

auto data = fs_callback_(name);
auto data = fs_callback_(name, *this);

if (data.first.data == nullptr && data.first.size == 0 && !data.second.empty())
{
Expand Down Expand Up @@ -711,7 +711,7 @@ auto context::load_include(std::string const& name) -> bool
if (include_cache_.contains(name))
return true;

auto file = fs_callback_(name);
auto file = fs_callback_(name, *this);

if ((file.first.data == nullptr || file.first.size == 0) && file.second.empty())
throw std::runtime_error("empty file");
Expand Down
34 changes: 30 additions & 4 deletions src/tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,42 @@ auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> void

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(std::string const& name, context& ctx) -> 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.has_extension())
{
auto id = ctx.token_id(name);
if (id > 0)
{
path = fs::path{ std::to_string(id) + ".gscbin" };
}

if (!utils::file::exists(path))
{
path = fs::path{ name + ".gscbin" };
}

if (!utils::file::exists(path))
{
path = fs::path{ name + ".gsh" };
}

if (!utils::file::exists(path))
{
path = fs::path{ name + ".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)
{
Expand Down

0 comments on commit 388fd63

Please sign in to comment.