Skip to content

Commit eaddd34

Browse files
committed
Resolve reletive paths relative to the launch script
This means that require "thing" now works
1 parent 66a5210 commit eaddd34

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lua/wrap/stdio.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "engine/file.hpp"
66

7+
extern std::string base_path;
8+
79
// wrap stdio funcs around blit:: funcs
810
wrap_FILE *wrap_stdin = nullptr;
911
wrap_FILE *wrap_stdout = nullptr;
@@ -44,8 +46,14 @@ wrap_FILE *wrap_fopen(const char *filename, const char *mode)
4446
blit_mode |= blit::OpenMode::read;
4547
}
4648

49+
std::string filename_str = filename;
50+
51+
// adjust relative filenames to root
52+
if(filename[0] == '.' && filename[1] == '/')
53+
filename_str = base_path + (filename + 1);
54+
4755
auto ret = new wrap_FILE;
48-
ret->file.open(filename, blit_mode);
56+
ret->file.open(filename_str, blit_mode);
4957
ret->offset = 0;
5058

5159
ret->getc_buf_len = ret->getc_buf_off = 0;

main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ lua_State *L;
88
bool has_update = true;
99
bool has_render = true;
1010

11+
std::string base_path;
12+
1113
void init() {
1214
set_screen_mode(ScreenMode::lores);
1315
screen.pen = Pen(0, 0, 0, 255);
@@ -20,9 +22,18 @@ void init() {
2022
lua_blit_update_state(L);
2123

2224
auto launchPath = blit::get_launch_path();
25+
2326
if(!launchPath) {
2427
launchPath = "main.lua";
2528
}
29+
30+
31+
// set base path from dir of main script
32+
auto pos = std::string_view(launchPath).find_last_of('/');
33+
if(pos != std::string_view::npos)
34+
base_path = std::string_view(launchPath).substr(0, pos);
35+
36+
// load the script
2637
auto err = luaL_loadfile(L, launchPath);
2738

2839
if(err) {

0 commit comments

Comments
 (0)