From 6575eace2b08fa86e581bc5a139b3245bbd6d04f Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Fri, 3 Jan 2014 01:13:05 +0000
Subject: [PATCH] [Change] Balanced the Lua conf stack.

---
 src/conf.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/src/conf.c b/src/conf.c
index 526ecef..7a9031c 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -19,30 +19,30 @@
   lua_getglobal(L,n); \
   if(lua_isnumber(L, -1)) { \
   i = (int)lua_tonumber(L, -1); \
+  } \
   lua_remove(L, -1); \
-  }
 
 #define conf_loadFloat(n,f) \
   lua_getglobal(L,n); \
   if(lua_isnumber(L, -1)) { \
   f = (double)lua_tonumber(L, -1); \
+  } \
   lua_remove(L,-1);\
-  }
 
 #define conf_loadBool(n,b) \
   lua_getglobal(L,n); \
   if(lua_isnumber(L, -1)) \
   if((int)lua_tonumber(L, -1) == 1) { \
   b = 1; \
-  lua_remove(L, -1); \
   } \
+  lua_remove(L, -1); \
 
 #define conf_loadString(n,s) \
   lua_getglobal(L,n); \
   if(lua_isstring(L, -1)) { \
   s = strdup((char*)lua_tostring(L, -1)); \
+  } \
   lua_remove(L, -1); \
-  }
 
 /* Some crap from main. */
 extern int nosound;
@@ -164,25 +164,30 @@ int conf_loadConfig(const char* file) {
         lua_gettable(L, -2);
         if(lua_isstring(L, -1))
           str = (char*)lua_tostring(L, -1);
+        lua_remove(L, -1);
 
         /* Get the key. */
         lua_pushstring(L, "key");
-        lua_gettable(L, -3);
+        lua_gettable(L, -2);
         if(lua_isnumber(L, -1))
           key = (int)lua_tonumber(L, -1);
+        lua_remove(L, -1);
 
         /* Is it reversed? Only used for axis. */
         lua_pushstring(L, "reverse");
-        lua_gettable(L, -4);
+        lua_gettable(L, -2);
         if(lua_isnumber(L, -1))
           reverse = 1;
+        lua_remove(L, -1);
 
         lua_pushstring(L, "mod");
-        lua_gettable(L, -4);
+        lua_gettable(L, -2);
         if(lua_isstring(L, -1))
           mod = (char*)lua_tostring(L, -1);
+        lua_remove(L, -1);
 
-        if(key != -1 && str != NULL) {
+
+        if((key != -1) && (str != NULL)) {
           /* Then the keybind is valid. Get the type. */
           if(strcmp(str, "null")==0)            type = KEYBIND_NULL;
           else if(strcmp(str, "keyboard")==0)   type = KEYBIND_KEYBOARD;
@@ -216,10 +221,8 @@ int conf_loadConfig(const char* file) {
 
         /* Clean up after table crap. */
         lua_remove(L,-1);
-        lua_remove(L,-1);
-        lua_remove(L,-1);
-        lua_remove(L,-1);
       }
+      lua_remove(L, -1);
     }
   } else {
     /* Failed to load the config file.. */