diff --git a/src/io_util.c b/src/io_util.c index f8bb780..6ef3810 100644 --- a/src/io_util.c +++ b/src/io_util.c @@ -3,13 +3,34 @@ #include "io_util.h" #include "util.h" +static void +file_error(const char *path) +{ + PHYSFS_ErrorCode code = PHYSFS_getLastErrorCode(); + fatal("Unable to open file %s: (%d) %s", path, code, PHYSFS_getErrorByCode(code)); +} + SDL_RWops * io_load_rwops(const char *path) { - if (!PHYSFS_exists(path)) { - PHYSFS_ErrorCode code = PHYSFS_getLastErrorCode(); - fatal("Unable to open file %s: (%d) %s", path, code, PHYSFS_getErrorByCode(code)); - } + if (!PHYSFS_exists(path)) + file_error(path); return PHYSFSRWOPS_openRead(path); } + +void +io_load_lua_buffer(char **dest, unsigned long *len, const char *filepath) +{ + if (!PHYSFS_exists(filepath)) + file_error(filepath); + + PHYSFS_File *file = PHYSFS_openRead(filepath); + PHYSFS_sint64 size = (unsigned long) PHYSFS_fileLength(file); + char *buffer = ec_malloc(sizeof(char) * (unsigned long) size); + PHYSFS_read(file, buffer, 1, (PHYSFS_uint32) size); + PHYSFS_close(file); + + *len = (unsigned long) size; + *dest = buffer; +} \ No newline at end of file diff --git a/src/io_util.h b/src/io_util.h index 92de846..a4401c0 100644 --- a/src/io_util.h +++ b/src/io_util.h @@ -24,4 +24,7 @@ SDL_RWops * io_load_rwops(const char *path); +void +io_load_lua_buffer(char **dest, unsigned long *len, const char *filepath); + #endif // IO_UTIL_H_ \ No newline at end of file diff --git a/src/main.c b/src/main.c index 1f3a82d..a488662 100644 --- a/src/main.c +++ b/src/main.c @@ -630,6 +630,7 @@ int main(int argc, char *argv[]) #else PHYSFS_mount("assets", NULL, 0); #endif // DEBUG + PHYSFS_mount("data", NULL, 0); if (!init()) return 1; diff --git a/src/map_lua.c b/src/map_lua.c index aacc3b0..96b6cee 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -309,14 +309,20 @@ l_add_monster(lua_State *L) } static Map* -generate_map(unsigned int level, char *file, SDL_Renderer *renderer) +generate_map(unsigned int level, const char *file, SDL_Renderer *renderer) { int status, result; info("Running lua map script: %s", file); lua_State *L = load_lua_state(); - status = luaL_loadfile(L, file); + + char *buffer = NULL; + long unsigned int len; + io_load_lua_buffer(&buffer, &len, file); + status = luaL_loadbuffer(L, buffer, len, file); + free(buffer); + if (status) { fatal("Couldn't load file: %s\n", lua_tostring(L, -1)); } @@ -372,13 +378,13 @@ generate_map(unsigned int level, char *file, SDL_Renderer *renderer) Map* map_lua_generator_single_room__run(unsigned int level, SDL_Renderer *renderer) { - char file[] = "data/menumapgen.lua"; + char file[] = "menumapgen.lua"; return generate_map(level, file, renderer); } Map* map_lua_generator_run(unsigned int level, SDL_Renderer *renderer) { - char file[] = "data/mapgen.lua"; + char file[] = "mapgen.lua"; return generate_map(level, file, renderer); } diff --git a/src/skillbar.c b/src/skillbar.c index d8928fb..55cf24c 100644 --- a/src/skillbar.c +++ b/src/skillbar.c @@ -35,12 +35,12 @@ load_texture(SkillBar *bar, const char *path, SDL_Renderer *renderer) ht_set(bar->textures, path, t); for (unsigned int i = 0; i < 4; ++i) { - char buffer[1]; + char buffer[4]; Sprite *s = sprite_create(); s->pos = (Position) { i * 32 + 20, 20 }; s->fixed = true; sprite_load_text_texture(s, "GUI/SDS_8x8.ttf", 0, 8); - m_sprintf(buffer, 1, "%u", i+1); + m_sprintf(buffer, 4, "%u", i+1); texture_load_from_text(s->textures[0], buffer, c_yellow, renderer); linkedlist_append(&bar->sprites, s); }