| 1 | #include "Script.h" | 
|---|
| 2 |  | 
|---|
| 3 |  | 
|---|
| 4 | Script::Script() | 
|---|
| 5 | { | 
|---|
| 6 |   returnCount = argumentCount = 0; | 
|---|
| 7 |  | 
|---|
| 8 |   luaState = lua_open(); | 
|---|
| 9 |  | 
|---|
| 10 |   luaopen_base(luaState); | 
|---|
| 11 |   luaopen_table(luaState); | 
|---|
| 12 |   luaopen_io(luaState); | 
|---|
| 13 |   luaopen_string(luaState); | 
|---|
| 14 |   luaopen_math(luaState); | 
|---|
| 15 |   luaopen_debug(luaState); | 
|---|
| 16 |  | 
|---|
| 17 | } | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | Script::~Script() | 
|---|
| 21 | { | 
|---|
| 22 |   lua_setgcthreshold(luaState, 0);  // collected garbage | 
|---|
| 23 |   lua_close(luaState); | 
|---|
| 24 | } | 
|---|
| 25 |  | 
|---|
| 26 |  | 
|---|
| 27 | bool Script::loadFile(std::string& filename) | 
|---|
| 28 |  { | 
|---|
| 29 |  | 
|---|
| 30 |    if(currentFile.length() != 0) | 
|---|
| 31 |      printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str()); | 
|---|
| 32 |  | 
|---|
| 33 |    int error = luaL_loadfile (luaState, filename.c_str()); | 
|---|
| 34 |  | 
|---|
| 35 |    if(error == 0) | 
|---|
| 36 |    { | 
|---|
| 37 |      error = lua_pcall(luaState, 0, 0, 0); | 
|---|
| 38 |  | 
|---|
| 39 |      if(error == 0) | 
|---|
| 40 |      { | 
|---|
| 41 |       currentFile = filename; | 
|---|
| 42 |       return true; | 
|---|
| 43 |      } | 
|---|
| 44 |      else | 
|---|
| 45 |      { | 
|---|
| 46 |       reportError(error); | 
|---|
| 47 |      } | 
|---|
| 48 |  | 
|---|
| 49 |    } | 
|---|
| 50 |    else | 
|---|
| 51 |    { | 
|---|
| 52 |      reportError(error); | 
|---|
| 53 |    } | 
|---|
| 54 |  | 
|---|
| 55 |    return false; | 
|---|
| 56 |  } | 
|---|
| 57 |  | 
|---|
| 58 |  bool Script::executeFile() | 
|---|
| 59 |  { | 
|---|
| 60 |    if(currentFile.length() != 0) | 
|---|
| 61 |    { | 
|---|
| 62 |     int error = lua_pcall(luaState, 0, 0, 0); | 
|---|
| 63 |     if( error == 0) | 
|---|
| 64 |       return true; | 
|---|
| 65 |      else | 
|---|
| 66 |       { | 
|---|
| 67 |        reportError(error); | 
|---|
| 68 |        return false; | 
|---|
| 69 |       } | 
|---|
| 70 |    } | 
|---|
| 71 |    return false; | 
|---|
| 72 |  } | 
|---|
| 73 |  | 
|---|
| 74 |  bool Script::selectFunction(std::string& functionName, int retCount) | 
|---|
| 75 |  { | 
|---|
| 76 |    lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); | 
|---|
| 77 |    lua_gettable(luaState, LUA_GLOBALSINDEX); | 
|---|
| 78 |  | 
|---|
| 79 |    if(lua_isfunction( luaState , -1)) | 
|---|
| 80 |    { | 
|---|
| 81 |      returnCount = retCount; | 
|---|
| 82 |      argumentCount = 0; | 
|---|
| 83 |      currentFunction = functionName; | 
|---|
| 84 |      return true; | 
|---|
| 85 |    } | 
|---|
| 86 |    else | 
|---|
| 87 |     return false; | 
|---|
| 88 |  | 
|---|
| 89 |  } | 
|---|
| 90 |  | 
|---|
| 91 |  //return number of returned values | 
|---|
| 92 |  bool Script::executeFunction() | 
|---|
| 93 |  { | 
|---|
| 94 |    if(currentFunction.length() != 0 ) | 
|---|
| 95 |    { | 
|---|
| 96 |     int error = lua_pcall(luaState, argumentCount, returnCount,0); | 
|---|
| 97 |     if(error != 0) | 
|---|
| 98 |     { | 
|---|
| 99 |      reportError(error); | 
|---|
| 100 |      return false; | 
|---|
| 101 |     } | 
|---|
| 102 |     else | 
|---|
| 103 |     { | 
|---|
| 104 |       currentFunction.assign("");//a function gets unusable after beeing called for the first time | 
|---|
| 105 |       returnCount = argumentCount = 0; | 
|---|
| 106 |       return true; | 
|---|
| 107 |     } | 
|---|
| 108 |    } | 
|---|
| 109 |    else | 
|---|
| 110 |      printf("Error: no function selected.\n"); | 
|---|
| 111 |  } | 
|---|
| 112 |  | 
|---|
| 113 |  | 
|---|
| 114 |  //overload this function to add different types | 
|---|
| 115 |  bool Script::pushParam(int param, std::string& toFunction) | 
|---|
| 116 |  { | 
|---|
| 117 |    if(currentFunction.compare(toFunction) == 0) | 
|---|
| 118 |    { | 
|---|
| 119 |      lua_pushnumber(luaState, (lua_Number) param); | 
|---|
| 120 |      argumentCount++; | 
|---|
| 121 |     return true; | 
|---|
| 122 |    } | 
|---|
| 123 |    else | 
|---|
| 124 |    { | 
|---|
| 125 |     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); | 
|---|
| 126 |     return false; | 
|---|
| 127 |    } | 
|---|
| 128 |  | 
|---|
| 129 |  } | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 |  bool Script::pushParam(float param, std::string& toFunction) | 
|---|
| 133 |  { | 
|---|
| 134 |    if(currentFunction.compare(toFunction) == 0) | 
|---|
| 135 |    { | 
|---|
| 136 |      lua_pushnumber(luaState,(lua_Number) param); | 
|---|
| 137 |      argumentCount++; | 
|---|
| 138 |      return true; | 
|---|
| 139 |    } | 
|---|
| 140 |    else | 
|---|
| 141 |    { | 
|---|
| 142 |      printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); | 
|---|
| 143 |      return false; | 
|---|
| 144 |    } | 
|---|
| 145 |  | 
|---|
| 146 |  } | 
|---|
| 147 |  | 
|---|
| 148 |  bool Script::pushParam(double param, std::string& toFunction) | 
|---|
| 149 |  { | 
|---|
| 150 |    if(currentFunction.compare(toFunction) == 0) | 
|---|
| 151 |    { | 
|---|
| 152 |      lua_pushnumber(luaState,(lua_Number) param); | 
|---|
| 153 |      argumentCount++; | 
|---|
| 154 |      return true; | 
|---|
| 155 |    } | 
|---|
| 156 |    else | 
|---|
| 157 |    { | 
|---|
| 158 |      printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); | 
|---|
| 159 |      return false; | 
|---|
| 160 |    } | 
|---|
| 161 |  | 
|---|
| 162 |  } | 
|---|
| 163 |  | 
|---|
| 164 |  int Script::getReturnedInt() | 
|---|
| 165 |  { | 
|---|
| 166 |    if(returnCount > 0) | 
|---|
| 167 |    { | 
|---|
| 168 |      printf("int found"); | 
|---|
| 169 |      if(lua_isnumber(luaState, 1)) | 
|---|
| 170 |      { | 
|---|
| 171 |        printf("int found"); | 
|---|
| 172 |        int returnValue = (int)lua_tonumber(luaState, 1); | 
|---|
| 173 |        returnCount--; | 
|---|
| 174 |      } | 
|---|
| 175 |    } | 
|---|
| 176 |  } | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|
| 179 |  int Script::reportError(int error) | 
|---|
| 180 |  { | 
|---|
| 181 |  const char *msg = lua_tostring(luaState, -1); | 
|---|
| 182 |  if (msg == NULL) msg = "(error with no message)"; | 
|---|
| 183 |  fprintf(stderr, "ERROR: %s\n", msg); | 
|---|
| 184 |  lua_pop(luaState, 1); | 
|---|
| 185 |  return error; | 
|---|
| 186 |  } | 
|---|
| 187 |  | 
|---|