- File operations work on "data/" folder or ".jrf" file

- [NEW] fopenres() works on .jrf resources, fopen on normal files
This commit is contained in:
2022-10-11 17:58:03 +02:00
parent a74ca5033c
commit b282bd05d5
8 changed files with 185 additions and 24 deletions

13
lua.cpp
View File

@@ -1,6 +1,7 @@
#include "lua.h"
#include "lua/lua.hpp"
#include "mini.h"
#include "jfile.h"
extern "C" {
static int cpp_newsurf(lua_State *L) {
@@ -568,6 +569,11 @@ extern "C" {
fopen(str, mode);
return 0;
}
static int cpp_fopenres(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
fopenres(str);
return 0;
}
static int cpp_fclose(lua_State *L) {
fclose();
return 0;
@@ -740,6 +746,7 @@ void push_lua_funcs() {
lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen");
lua_pushcfunction(L,cpp_fopen); lua_setglobal(L, "fopen");
lua_pushcfunction(L,cpp_fopenres); lua_setglobal(L, "fopenres");
lua_pushcfunction(L,cpp_fclose); lua_setglobal(L, "fclose");
lua_pushcfunction(L,cpp_feof); lua_setglobal(L, "feof");
lua_pushcfunction(L,cpp_fwritei); lua_setglobal(L, "fwritei");
@@ -884,12 +891,16 @@ void lua_init(char* filenames) {
if (*pointer!=0) *(pointer++)=0;
// Load and execute file
if (luaL_loadfile(L, file_start)) { // "game.lua"
//if (luaL_loadfile(L, file_start)) { // "game.lua"
int size;
char* buffer = file_getfilebuffer(file_start, size);
if (luaL_loadbuffer(L, buffer, size, file_start)) {
debug("error loading game");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
free(buffer);
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug(lua_tostring(L, -1));