MCPcopy Create free account
hub / github.com/black-sliver/PopTracker / LoadScript

Method LoadScript

src/core/scripthost.cpp:184–226  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

182}
183
184bool ScriptHost::LoadScript(const std::string& file)
185{
186 std::string script;
187 printf("Loading \"%s\" ...\n", file.c_str());
188 if (!_pack->ReadFile(file, script)) {
189 // TODO; throw exception?
190 fprintf(stderr, "File not found!\n");
191 return false;
192 }
193
194 const char* buf = script.c_str();
195 size_t len = script.length();
196 if (len>=3 && memcmp(buf, "\xEF\xBB\xBF", 3) == 0) {
197 fprintf(stderr, "WARNING: skipping BOM of %s\n", sanitize_print(file).c_str());
198 buf += 3;
199 len -= 3;
200 }
201 if (luaL_loadbufferx(_L, buf, len, file.c_str(), "t") == LUA_OK) {
202 std::string modname = file;
203 if (strncasecmp(modname.c_str(), "scripts/", 8) == 0)
204 modname = modname.substr(8);
205 if (modname.length() > 4 && strcasecmp(modname.c_str() + modname.length() - 4, ".lua") == 0)
206 modname = modname.substr(0, modname.length() - 4);
207 std::replace(modname.begin(), modname.end(), '/', '.');
208 lua_pushstring(_L, modname.c_str());
209 if (lua_pcall(_L, 1, 1, 0) == LUA_OK) {
210 // if it was executed successfully, pop everything from stack
211 lua_pop(_L, lua_gettop(_L)); // TODO: lua_settop(L, 0); ?
212 } else {
213 // TODO: throw exception?
214 const char* msg = lua_tostring(_L,-1);
215 printf("Error running \"%s\": %s!\n", file.c_str(), msg);
216 lua_pop(_L, lua_gettop(_L)); // TODO: lua_settop(L, 0); ?
217 return false;
218 }
219 } else {
220 fprintf(stderr, "Error loading \"%s\": %s!\n", file.c_str(), lua_tostring(_L,-1));
221 lua_pop(_L, 1); // TODO: verify this is correct
222 return false;
223 }
224
225 return true;
226}
227
228LuaItem* ScriptHost::CreateLuaItem()
229{

Callers 1

loadTrackerMethod · 0.80

Calls 4

sanitize_printFunction · 0.85
ReadFileMethod · 0.80
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected