diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index 17b3170e9..886e73fa9 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -1284,37 +1284,8 @@ local function errorf(level, fmt, ...) error(errmsg, level+1) end -local ERRLEV = 5 - -local function readintostr(fn) - -- XXX: this is pretty much the same as the code in L_RunOnce() - - local fd = ffiC.kopen4loadfrommod(fn, 0) -- TODO: g_loadFromGroupOnly - if (fd < 0) then - return nil - end - - local sz = ffiC.kfilelength(fd) - if (sz == 0) then - ffiC.kclose(fd) - return "" - end - - if (sz < 0) then - ffi.kclose(fd) - error("INTERNAL ERROR: kfilelength() returned negative length", ERRLEV) - end - - local str = ffi.new("char [?]", sz) - local readlen = ffiC.kread(fd, str, sz) - - ffiC.kclose(fd); fd=-1 - - if (readlen ~= sz) then - errorf(ERRLEV, "INTERNAL ERROR: couldn't read \"%s\" wholly", fn) - end - - return ffi.string(str, sz) +local function readintostr_mod(fn) + return defs_c.readintostr(fn, ffiC.kopen4loadfrommod) end @@ -1333,6 +1304,8 @@ local required_module_mt = { -- * never messes with the global environment, it only returns the module. -- * allows passing varargs beyond the name to the module. local function our_require(modname, ...) + local ERRLEV = 5 + -- Check module name is valid first. -- TODO: restrict valid names? if (type(modname) ~= "string") then @@ -1380,7 +1353,7 @@ local function our_require(modname, ...) end local modfn = modname .. ".lua" - local str = readintostr(modfn) + local str = readintostr_mod(modfn) if (str == nil) then errorf(ERRLEV-1, "Couldn't open file \"%s\"", modfn) end @@ -1683,7 +1656,7 @@ local concode --- Compile CONs do - read_into_string = readintostr -- for lunacon + read_into_string = readintostr_mod -- for lunacon local lunacon = require("lunacon") local confn = { ffi.string(ffiC.G_ConFile()) } diff --git a/polymer/eduke32/source/lunatic/defs_common.lua b/polymer/eduke32/source/lunatic/defs_common.lua index e200a6dae..e85da543e 100644 --- a/polymer/eduke32/source/lunatic/defs_common.lua +++ b/polymer/eduke32/source/lunatic/defs_common.lua @@ -248,9 +248,10 @@ local vec3_ct = ffi.typeof("vec3_t") -- will be metatype'd in geom.lua: if (not _LUNATIC_AUX) then require("geom") - local hitdata_ct = ffi.typeof("hitdata_t") end +local hitdata_ct = ffi.typeof("hitdata_t") + decl[[ const int32_t engine_main_arrays_are_static; const int32_t engine_v8; @@ -325,9 +326,54 @@ const int32_t totalclock; int32_t randomseed; // DEPRECATED const int32_t xdim, ydim; const int32_t windowx1, windowy1, windowx2, windowy2; + +int32_t kopen4load(const char *filename, char searchfirst); +int32_t kfilelength(int32_t handle); +void kclose(int32_t handle); +int32_t kread(int32_t handle, void *buffer, int32_t leng); +int32_t klseek(int32_t handle, int32_t offset, int32_t whence); ]] +function readintostr(fn, kopen4load_func) + -- XXX: this is pretty much the same as the code in L_RunOnce() + + if (kopen4load_func == nil) then + kopen4load_func = ffiC.kopen4load + end + + -- TODO: for game, g_loadFromGroupOnly? + local fd = kopen4load_func(fn, 0) + if (fd < 0) then + return nil + end + + local sz = ffiC.kfilelength(fd) + if (sz == 0) then + ffiC.kclose(fd) + return "" + end + + if (sz < 0) then + ffi.kclose(fd) + error("INTERNAL ERROR: kfilelength() returned negative length") + end + + local str = ffi.new("char [?]", sz) + local readlen = ffiC.kread(fd, str, sz) + + ffiC.kclose(fd); fd=-1 + + if (readlen ~= sz) then + error("INTERNAL ERROR: couldn't read \""..fn.."\" wholly") + end + + return ffi.string(str, sz) +end + if (_LUNATIC_AUX) then + -- XXX: The global doesn't show up in 'engine_maptext'. + -- I guess I still haven't fully grokked globals in Lua. + string.readintostr = readintostr require "engine_maptext" return end @@ -382,12 +428,6 @@ void rotatesprite_(int32_t sx, int32_t sy, int32_t z, int16_t a, int16_t picnum, int32_t cx1, int32_t cy1, int32_t cx2, int32_t cy2); void setaspect(int32_t daxrange, int32_t daaspect); - -int32_t kopen4load(const char *filename, char searchfirst); -int32_t kfilelength(int32_t handle); -void kclose(int32_t handle); -int32_t kread(int32_t handle, void *buffer, int32_t leng); -int32_t klseek(int32_t handle, int32_t offset, int32_t whence); ]] -- misc. functions