twn_util: final cleaning up, introducton of powerful file_read()
This commit is contained in:
@ -29,10 +29,6 @@ void game_tick(void) {
|
||||
ctx.debug = !ctx.debug;
|
||||
}
|
||||
|
||||
if (input_action_just_pressed("debug_dump_atlases")) {
|
||||
textures_dump_atlases();
|
||||
}
|
||||
|
||||
state->scene->tick(state);
|
||||
|
||||
/* there's a scene switch pending, we can do it now that the tick is done */
|
||||
|
@ -30,10 +30,6 @@ void game_tick(void) {
|
||||
ctx.debug = !ctx.debug;
|
||||
}
|
||||
|
||||
if (input_action_just_pressed("debug_dump_atlases")) {
|
||||
textures_dump_atlases();
|
||||
}
|
||||
|
||||
state->scene->tick(state);
|
||||
|
||||
/* there's a scene switch pending, we can do it now that the tick is done */
|
||||
|
@ -109,6 +109,12 @@ static float height_at(SceneIngame *scn, Vec2 position);
|
||||
static Vec3 normal_at(SceneIngame *scn, Vec2 position);
|
||||
|
||||
|
||||
static inline float clampf(float f, float min, float max) {
|
||||
const float t = f < min ? min : f;
|
||||
return t > max ? max : t;
|
||||
}
|
||||
|
||||
|
||||
static void draw_vehicle(SceneIngame *scn) {
|
||||
for (size_t i = 0; i < 12; ++i)
|
||||
draw_line_3d(vbp[vbs[i][0]], vbp[vbs[i][1]], 1, (Color){255, 255, 255, 255});
|
||||
|
@ -112,6 +112,9 @@ for procedure, procedure_desc in api["procedures"].items():
|
||||
binding += " lua_pushnumber(L, (float)(%s(%s)));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
elif procedure_desc["return"] == "char *":
|
||||
binding += " lua_pushstring(L, %s(%s));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
elif procedure_desc["return"] == "String":
|
||||
binding += " String result = %s(%s);\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
binding += " lua_pushlstring(L, result.data, (int)result.length);\n"
|
||||
elif type(procedure_desc["return"]) is dict or procedure_desc["return"] in api["types"]:
|
||||
type_desc = procedure_desc["return"] if type(procedure_desc["return"]) is dict else api["types"][procedure_desc["return"]]
|
||||
binding += " %s result = %s(%s);\n" % (type_desc["c_type"], procedure, ", ".join(param["name"] for param in procedure_desc["params"]))
|
||||
|
@ -26,7 +26,7 @@ static int physfs_loader(lua_State *L) {
|
||||
|
||||
static const char *name_breaker = NULL;
|
||||
if (name_breaker && SDL_strcmp(name, name_breaker) == 0) {
|
||||
log_critical("Recursive load on itself from lua module (%s)", name_breaker);
|
||||
log_string(name_breaker, "Recursive load on itself from lua module");
|
||||
return 0;
|
||||
} name_breaker = name;
|
||||
|
||||
@ -40,26 +40,26 @@ static int physfs_loader(lua_State *L) {
|
||||
SDL_asprintf(&final_path, "/scripts/%s.lua", path_copy);
|
||||
SDL_free(path_copy);
|
||||
|
||||
if (!file_exists(final_path)) {
|
||||
if (SDL_strcmp(file_read(final_path, ":exists").data, "yes") == 0) {
|
||||
char *error_message = NULL;
|
||||
SDL_asprintf(&error_message, "could not find module %s in filesystem", name);
|
||||
lua_pushstring(L, error_message);
|
||||
free(error_message);
|
||||
SDL_free(error_message);
|
||||
|
||||
free(final_path);
|
||||
SDL_free(final_path);
|
||||
return 1;
|
||||
}
|
||||
|
||||
unsigned char *buf = NULL;
|
||||
int64_t buf_size = file_to_bytes(final_path, &buf);
|
||||
free(final_path);
|
||||
char *final_path_binary = NULL;
|
||||
SDL_asprintf(&final_path_binary, "%s%s", final_path, ":binary");
|
||||
|
||||
String file = file_read(final_path, ":binary");
|
||||
SDL_free(final_path);
|
||||
|
||||
/* TODO: use reader interface for streaming instead */
|
||||
int const result = luaL_loadbuffer(L, (char *)buf, buf_size, name);
|
||||
free(buf);
|
||||
|
||||
int const result = luaL_loadbuffer(L, file.data, (size_t)file.length, name);
|
||||
if (result != LUA_OK)
|
||||
log_critical("%s", lua_tostring(L, -1));
|
||||
log_string(lua_tostring(L, -1), NULL);
|
||||
|
||||
return result == LUA_OK;
|
||||
}
|
||||
@ -110,7 +110,7 @@ static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
||||
|
||||
static void exchange_lua_states(lua_State *from, lua_State *to, int level, int index) {
|
||||
if (level >= UDATA_NESTING_LIMIT) {
|
||||
log_critical("ctx.udata nesting limit is reached (%u)", UDATA_NESTING_LIMIT);
|
||||
log_string("ctx.udata nesting limit is reached", NULL);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -143,7 +143,7 @@ static void exchange_lua_states(lua_State *from, lua_State *to, int level, int i
|
||||
break;
|
||||
default:
|
||||
/* TODO: provide a path and type of it for better diagnostic */
|
||||
log_warn("Unserializable udata found and is ignored");
|
||||
log_string("Unserializable udata found and is ignored", NULL);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -167,7 +167,6 @@ void game_tick(void) {
|
||||
SDL_assert(!lua_isnoneornil(state->L, -1));
|
||||
SDL_assert(!lua_isnoneornil(state->L, -2));
|
||||
if (!lua_isnoneornil(state->L, -1)) {
|
||||
log_info("Exchanging lua states...");
|
||||
lua_newtable(new_state);
|
||||
exchange_lua_states(state->L, new_state, 0, -1);
|
||||
lua_setfield(new_state, -2, "udata");
|
||||
@ -216,24 +215,21 @@ void game_tick(void) {
|
||||
bindgen_load_twn(state->L);
|
||||
|
||||
/* now finally get to running the code */
|
||||
unsigned char *game_buf = NULL;
|
||||
size_t game_buf_size = file_to_bytes("/scripts/game.lua", &game_buf);
|
||||
String file = file_read("/scripts/game.lua", ":binary");
|
||||
/* TODO: use reader interface for streaming instead */
|
||||
if (luaL_loadbuffer(state->L, (char *)game_buf, game_buf_size, "game.lua") == LUA_OK) {
|
||||
if (luaL_loadbuffer(state->L, file.data, (size_t)file.length, "game.lua") == LUA_OK) {
|
||||
if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) {
|
||||
log_critical("%s", lua_tostring(state->L, -1));
|
||||
log_string(luaL_tolstring(state->L, -1, NULL), "Error executing /scripts/game.lua entry");
|
||||
lua_pop(state->L, 1);
|
||||
} else
|
||||
state->loaded_successfully = true;
|
||||
} else {
|
||||
/* got some sort of error, it should be pushed on top of the stack */
|
||||
SDL_assert(lua_isstring(state->L, -1));
|
||||
log_critical("Error loading /scripts/game.lua entry: %s", luaL_tolstring(state->L, -1, NULL));
|
||||
log_string(luaL_tolstring(state->L, -1, NULL), "Error loading /scripts/game.lua entry");
|
||||
lua_pop(state->L, 1);
|
||||
}
|
||||
|
||||
SDL_free(game_buf);
|
||||
|
||||
/* from this point we have access to everything defined in lua */
|
||||
}
|
||||
|
||||
@ -251,7 +247,7 @@ void game_tick(void) {
|
||||
|
||||
lua_getglobal(state->L, "game_tick");
|
||||
if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) {
|
||||
log_critical("%s", lua_tostring(state->L, -1));
|
||||
log_string(luaL_tolstring(state->L, -1, NULL), "Error executing game_tick()");
|
||||
lua_pop(state->L, 1);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user