Error handling in lua code

This commit is contained in:
Dane Johnson 2021-01-30 15:34:27 -06:00
parent aec78299ac
commit abbd5f4f98
8 changed files with 35 additions and 29 deletions

View File

@ -1,5 +1,7 @@
#include "Material.h"
#include <string>
Texture::Texture() {}
Texture Texture::FromFile(const char *filename) {
@ -18,7 +20,7 @@ Texture Texture::FromFile(const char *filename) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
Util::Die("Error loading texture file: ", filename);
throw std::string() + "Error loading texture file: " + filename;
}
stbi_image_free(data);

View File

@ -96,7 +96,7 @@ int Mesh::GetNumSubmeshes() {
Material Mesh::GetMaterial(int submesh) {
if (submesh >= GetNumSubmeshes()) {
Util::Die("Submesh index out of range");
throw "Submesh index out of range";
}
return submeshes[submesh]->material;
}

View File

@ -26,6 +26,7 @@
#define MESH_H
#include <vector>
#include <exception>
#include "types.h"
#include "Spatial.h"

View File

@ -33,10 +33,10 @@ NodeList::NodeList(bool isPrefabList) {
void NodeList::Append(Node *node) {
if (this->isPrefabList and not node->isPrefab) {
Util::Die("Attempt to add instanced node to prefab list!");
throw "Attempt to add instanced node to prefab list!";
}
if (node->isPrefab and not this->isPrefabList) {
Util::Die("Attempt to add prefab node to instanced list!");
throw "Attempt to add prefab node to instanced list!";
}
push_back(node);
}
@ -83,7 +83,7 @@ void Node::QueueFree() {
void Node::DoFree() {
if (this != root) {
Util::Die("Tried to call DoFree from non-root node");
throw "Tried to call DoFree from non-root node";
}
freeList->FreeList();
}
@ -102,7 +102,7 @@ Node* Node::Duplicate() {
Node* Node::Instance() {
if (not isPrefab) {
Util::Die("Attempt to instance an instanced node!");
throw "Attempt to instance an instanced node!";
}
Node* instance = Duplicate();
instance->isPrefab = false;

View File

@ -1,4 +1,7 @@
#include "Lua.h"
#include <string>
#include "../Util.h"
#ifdef LUA_SCRIPTING
@ -14,18 +17,14 @@ void Lua::Initialize() {
// Initialize Lua
luaL_openlibs(L);
luaopen_couch(L);
lua_atpanic(L, &LuaExceptionHandler);
err = luaL_loadfile(L, "main.lua");
if (err == LUA_OK) {
err = lua_pcall(L, 0, 0, 0);
if (err != LUA_OK) {
Error();
}
lua_call(L, 0, 0);
if (HasHook("init")) {
lua_getglobal(L, "init");
err = lua_pcall(L, 0, 0, 0);
if (err != LUA_OK) {
Error();
}
lua_call(L, 0, 0);
}
} else if (err == LUA_ERRFILE) {
Util::Die("Could not find main.lua.");
@ -53,10 +52,7 @@ void Lua::Update(double delta) {
if (HasHook("update")) {
lua_getglobal(L, "update");
lua_pushnumber(L, delta);
int err = lua_pcall(L, 1, 0, 0);
if (err != LUA_OK) {
Error();
}
lua_call(L, 1, 0);
}
#endif // LUA_SCRIPTING
}
@ -88,16 +84,12 @@ bool Lua::HasHook(const char *name) {
#ifdef LUA_SCRIPTING
void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods) {
// lua_State *L = (lua_State*) glfwGetWindowUserPointer(window);
lua_getglobal(L, "onkey");
lua_pushinteger(L, key);
lua_pushinteger(L, code);
lua_pushinteger(L, action);
lua_pushinteger(L, mods);
int err = lua_pcall(L, 4, 0, 0);
if (err != LUA_OK) {
Error();
}
lua_call(L, 4, 0);
}
void Lua::LuaMousePositionHandler(Window *window, double xpos, double ypos, double relx, double rely) {
@ -107,10 +99,18 @@ void Lua::LuaMousePositionHandler(Window *window, double xpos, double ypos, doub
lua_pushnumber(L, ypos);
lua_pushnumber(L, relx);
lua_pushnumber(L, rely);
int err = lua_pcall(L, 4, 0, 1);
if (err != LUA_OK) {
Error();
}
lua_call(L, 4, 0);
}
int Lua::LuaExceptionHandler(lua_State *L1) {
std::string err;
err += lua_tostring(L1, -1);
err += "\n";
luaL_traceback(L1, L1, NULL, 1);
err += lua_tostring(L, -1);
Util::Die(err);
// Should never get here
return 1;
}
#endif // LUA_SCRIPTING

View File

@ -27,6 +27,7 @@ private:
static lua_State *L;
static void LuaKeyHandler(Window *window, int key, int code, int action, int mods);
static void LuaMousePositionHandler(Window *window, double xpos, double ypos, double xrel, double yrel);
static int LuaExceptionHandler(lua_State *L1);
#endif // LUA_SCRIPTING
};

View File

@ -1,5 +1,7 @@
#include "Skybox.h"
#include <string>
float vertices[] = {
// positions
-1.0f, 1.0f, -1.0f,
@ -91,7 +93,7 @@ Skybox *Skybox::FromFiles(const char *right, const char* left, const char* top,
for (int i = 0; i < 6; i++) {
data = stbi_load(files[i], &width, &height, &nrChannels, 3);
if (!data) {
Util::Die("Could not load skybox image ", files[i]);
throw std::string() + "Could not load skybox image " + files[i];
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);

View File

@ -79,7 +79,7 @@ Yeah sure let's do physics
## Great Refactor
Things are starting to smell, here's what I need to do
- [ ] Replace all public attributes with accessors (on classes)
- [ ] Seperate prefabs from instances
- [X] Seperate prefabs from instances
- [ ] Combine related files (shaders)
- [ ] Create a testing suite
- [ ] Have exceptions thrown to Lua if generated from a user program