static int debugPrint(lua_State *L) { const char *msg = lua_tostring(L, 1); PolycodeDebugEvent *event = new PolycodeDebugEvent(); if(msg) event->errorString = string(msg); else event->errorString = string("<invalid string>"); Logger::log(">> %s\n", event->errorString.c_str()); PolycodePlayer *player = (PolycodePlayer*)CoreServices::getInstance()->getCore()->getUserPointer(); player->dispatchEvent(event, PolycodeDebugEvent::EVENT_PRINT); return 0; }
int MyLoader(lua_State* pState) { std::string module = lua_tostring(pState, 1); module += ".lua"; std::string defaultPath = "API/"; defaultPath.append(module); const char* fullPath = module.c_str(); // Logger::log("Loading custom class: %s\n", module.c_str()); OSFILE *inFile = OSBasics::open(module, "r"); if(!inFile) { inFile = OSBasics::open(defaultPath, "r"); } if(inFile) { OSBasics::seek(inFile, 0, SEEK_END); long progsize = OSBasics::tell(inFile); OSBasics::seek(inFile, 0, SEEK_SET); char *buffer = (char*)malloc(progsize+1); memset(buffer, 0, progsize+1); OSBasics::read(buffer, progsize, 1, inFile); PolycodePlayer *player = (PolycodePlayer*)CoreServices::getInstance()->getCore()->getUserPointer(); player->report(pState, luaL_loadbuffer(pState, (const char*)buffer, progsize, fullPath)); //free(buffer); OSBasics::close(inFile); } else { std::string err = "\n\tError - Could could not find "; err += module; err += "."; lua_pushstring(pState, err.c_str()); } return 1; }
static int customError(lua_State *L) { PolycodePlayer *player = (PolycodePlayer*)CoreServices::getInstance()->getCore()->getUserPointer(); player->crashed = true; std::vector<BackTraceEntry> backTrace; lua_Debug entry; int depth = 0; while (lua_getstack(L, depth, &entry)) { lua_getinfo(L, "Sln", &entry); std::vector<String> bits = String(entry.short_src).split("\""); if(bits.size() > 1) { String fileName = bits[1]; if(fileName != "class.lua") { BackTraceEntry trace; trace.lineNumber = entry.currentline; trace.fileName = fileName; backTrace.push_back(trace); printf(">>>> In file: %s on line %d\n", fileName.c_str(), trace.lineNumber); //backTrace += "In file: " + fileName + " on line " + String::IntToString(entry.currentline)+"\n"; } } depth++; } // horrible hack to determine the filenames of things bool stringThatIsTheMainFileSet = false; String stringThatIsTheMainFile; if(backTrace.size() == 0) { BackTraceEntry trace; trace.lineNumber = 0; trace.fileName = player->fullPath; backTrace.push_back(trace); } else { stringThatIsTheMainFileSet = true; stringThatIsTheMainFile = backTrace[backTrace.size()-1].fileName; backTrace[backTrace.size()-1].fileName = player->fullPath; } if(stringThatIsTheMainFileSet) { for(int i=0; i < backTrace.size(); i++) { if(backTrace[i].fileName == stringThatIsTheMainFile) { backTrace[i].fileName = player->fullPath; } } } const char *msg = lua_tostring(L, -1); if (msg == NULL) msg = "(error with no message)"; lua_pop(L, 1); String errorString; std::vector<String> info = String(msg).split(":"); if(info.size() > 2) { errorString = info[2]; } else { errorString = msg; } PolycodeDebugEvent *event = new PolycodeDebugEvent(); event->errorString = errorString; event->backTrace = backTrace; event->fileName = backTrace[0].fileName; event->lineNumber = backTrace[0].lineNumber; player->dispatchEvent(event, PolycodeDebugEvent::EVENT_ERROR); return 0; }