Read lua through physfs

This commit is contained in:
Linus Probert 2018-02-23 19:32:01 +01:00
parent 3ef45df8cb
commit 052b03189e
5 changed files with 41 additions and 10 deletions

View File

@ -3,13 +3,34 @@
#include "io_util.h"
#include "util.h"
SDL_RWops *
io_load_rwops(const char *path)
static void
file_error(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));
}
SDL_RWops *
io_load_rwops(const char *path)
{
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;
}

View File

@ -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_

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}