Skip to content

Commit

Permalink
Resolve reletive paths relative to the launch script
Browse files Browse the repository at this point in the history
This means that require "thing" now works
  • Loading branch information
Daft-Freak committed Sep 14, 2024
1 parent 66a5210 commit eaddd34
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lua/wrap/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "engine/file.hpp"

extern std::string base_path;

// wrap stdio funcs around blit:: funcs
wrap_FILE *wrap_stdin = nullptr;
wrap_FILE *wrap_stdout = nullptr;
Expand Down Expand Up @@ -44,8 +46,14 @@ wrap_FILE *wrap_fopen(const char *filename, const char *mode)
blit_mode |= blit::OpenMode::read;
}

std::string filename_str = filename;

// adjust relative filenames to root
if(filename[0] == '.' && filename[1] == '/')
filename_str = base_path + (filename + 1);

auto ret = new wrap_FILE;
ret->file.open(filename, blit_mode);
ret->file.open(filename_str, blit_mode);
ret->offset = 0;

ret->getc_buf_len = ret->getc_buf_off = 0;
Expand Down
11 changes: 11 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ lua_State *L;
bool has_update = true;
bool has_render = true;

std::string base_path;

void init() {
set_screen_mode(ScreenMode::lores);
screen.pen = Pen(0, 0, 0, 255);
Expand All @@ -20,9 +22,18 @@ void init() {
lua_blit_update_state(L);

auto launchPath = blit::get_launch_path();

if(!launchPath) {
launchPath = "main.lua";
}


// set base path from dir of main script
auto pos = std::string_view(launchPath).find_last_of('/');
if(pos != std::string_view::npos)
base_path = std::string_view(launchPath).substr(0, pos);

// load the script
auto err = luaL_loadfile(L, launchPath);

if(err) {
Expand Down

0 comments on commit eaddd34

Please sign in to comment.