]> git.lizzy.rs Git - dragonblocks-bedrock.git/blob - src/mods.cpp
Upload Files
[dragonblocks-bedrock.git] / src / mods.cpp
1 #include <cstring>
2 #include <cstdio>
3 #include <cerrno>
4 #include <string>
5 #include <iostream>
6 #include "node.h"
7 #include "mods.h"
8 #include "game.h"
9 #include "util.h"
10 #include "graphics.h"
11
12 using namespace std;
13
14 lua_State *Mods::lua_vm;
15
16 void Mods::init(){
17         Game::log("Initializing Mods");
18         lua_vm = luaL_newstate();
19         luaL_openlibs(lua_vm);
20         if(! check_lua(luaL_dofile(lua_vm, "builtin/init.lua"))){
21                 Game::log("Failed to load Builtin: File 'builtin/init.lua' does not exist or contains an error", ERROR);
22                 exit(EXIT_FAILURE);
23                 return;
24         }
25 }
26 void Mods::nodedef(){
27         Game::log("Initializing Nodes");
28         lua_getglobal(lua_vm, "dragonblocks");
29         if(! lua_istable(lua_vm, -1)){
30                 Game::log("Failed to load Builtin: 'dragonblocks' does not exist or is not a table", ERROR);
31                 exit(EXIT_FAILURE);
32                 return;
33         }
34         lua_pushstring(lua_vm, "registered_nodes");
35         lua_gettable(lua_vm, -2);
36         if(!lua_istable(lua_vm,-1)){
37                 Game::log("Failed to load Builtin: 'dragonblocks.registered_nodes' does not exist or is not a table", ERROR);
38                 exit(EXIT_FAILURE);
39                 return;
40         }
41         lua_pushnil(lua_vm);
42         for(int i = 1;; i++){
43                 lua_pop(lua_vm, 1);
44                 lua_pushnumber(lua_vm, i);
45                 lua_gettable(lua_vm, -2);
46                 if(!lua_istable(lua_vm, -1))
47                         break;
48                 
49                 
50                 lua_pushstring(lua_vm, "name");
51                 lua_gettable(lua_vm, -2);
52                 if(!lua_isstring(lua_vm,-1))
53                         continue;
54                 string name = lua_tostring(lua_vm,-1);
55                 lua_pop(lua_vm, 1);
56                 
57                 lua_pushstring(lua_vm, "texture");
58                 lua_gettable(lua_vm, -2);
59                 if(!lua_isstring(lua_vm,-1))
60                         continue;
61                 string texture = lua_tostring(lua_vm,-1);
62                 lua_pop(lua_vm, 1);
63                 
64                 lua_pushstring(lua_vm, "hidden");
65                 lua_gettable(lua_vm, -2);
66                 if(!lua_isboolean(lua_vm,-1))
67                         continue;
68                 bool hidden = lua_toboolean(lua_vm,-1);
69                 lua_pop(lua_vm, 1);
70                 
71                 lua_pushstring(lua_vm, "stable");
72                 lua_gettable(lua_vm, -2);
73                 if(!lua_isboolean(lua_vm,-1))
74                         continue;
75                 bool stable = lua_toboolean(lua_vm,-1);
76                 lua_pop(lua_vm, 1);
77                 
78                 lua_pushstring(lua_vm, "translucent");
79                 lua_gettable(lua_vm, -2);
80                 if(!lua_isboolean(lua_vm,-1))
81                         continue;
82                 bool translucent = lua_toboolean(lua_vm,-1);
83                 lua_pop(lua_vm, 1);
84                 
85                 new Node(name, texture, hidden, stable, translucent);
86                 Game::log("Registered Node " + name, INFO);
87         }
88 }
89 bool Mods::check_lua(int code){
90         if(code == LUA_OK)
91                 return true;
92         else{
93                 error(lua_tostring(lua_vm, -1));
94                 return false;
95         }
96 }
97 void Mods::error(string text){
98         Game::log("\e[34mlua: \e[0m" + text, ERROR);
99 }