/** * Load game state */ int Game_Load(const char* name) { FILE *f; char *ch, local; local = 1; for(ch=(char*)name;*ch;ch++) { if((*ch == '\\') || (*ch == '/')) { local = 0; break; } } if(local) { char token[512]; snprintf(token, 512, "save/%s", name); f = fopen(token, "rb"); if(f == NULL) { Sys_extWarn("Can not read file \"%s\"", token); return 0; } fclose(f); Engine_LuaClearTasks(); try { engine_lua.doFile(token); } catch(lua::RuntimeError& error) { Sys_DebugLog(LUA_LOG_FILENAME, "%s", error.what()); } catch(lua::LoadError& error) { Sys_DebugLog(LUA_LOG_FILENAME, "%s", error.what()); } } else { f = fopen(name, "rb"); if(f == NULL) { Sys_extWarn("Can not read file \"%s\"", name); return 0; } fclose(f); Engine_LuaClearTasks(); try { engine_lua.doFile(name); } catch(lua::RuntimeError& error) { Sys_DebugLog(LUA_LOG_FILENAME, "%s", error.what()); } catch(lua::LoadError& error) { Sys_DebugLog(LUA_LOG_FILENAME, "%s", error.what()); } } return 1; }
int main() { Lua::State state = Lua::State::create(); state.AddObject<std::string>(); state.loadstring( "x = stdstring.create()\n"\ "x:append('hi guy')\n"\ "y = stdstring.create()\n"\ "y:append(x:at(3))\n"\ "y:display()" ); state.call(); return 0; }
void Component :: RegMeta(Lua::State& st) { Lua::Var tmp = st["Component"]; if(tmp != Lua::TAB) { tmp = st.new_tab("Component", Lua::META); tmp.reg("__index", getting); tmp.reg("__newindex", setting); } }
void LuaScriptEditorView::CheckSyntax() { CStringA text; GetText(text); int indicatorNumber = INDIC_CONTAINER; AnnotationClearAll(); Lua::State state; std::vector<CString> errorMessages; if (state.CheckSyntax(CString(text), errorMessages)) { IndicSetStyle(indicatorNumber, INDIC_HIDDEN); AnnotationSetVisible(ANNOTATION_HIDDEN); return; } IndicSetStyle(indicatorNumber, INDIC_SQUIGGLE); IndicSetFore(indicatorNumber, RGB(255, 0, 0)); // red SetIndicatorCurrent(indicatorNumber); for (size_t index = 0, maxIndex = errorMessages.size(); index < maxIndex; index++) { CString errorMessage = errorMessages[0]; int pos = errorMessage.Find(_T("]:")); int pos2 = errorMessage.Find(_T(':'), pos + 2); int lineNumber = _ttoi(errorMessage.Mid(pos + 2, pos2 - (pos + 2))); CString error = errorMessage.Mid(pos2 + 1).Trim(); SetIndicatorValue(index); int textStart = static_cast<int>(PositionFromLine(lineNumber - 1)); int textEnd = GetLineEndPosition(lineNumber - 1); IndicatorFillRange(textStart, textEnd - textStart); AnnotationSetText(lineNumber - 1, CStringA(error).GetString()); AnnotationSetVisible(ANNOTATION_BOXED); } }
/// prints values as output debug string std::vector<Lua::Value> Print(Lua::State& state, const std::vector<Lua::Value>& vecParams) { lua_State* L = state.GetState(); lua_concat(L, vecParams.size()); CString cszText = lua_tostring(L, -1); ATLTRACE(_T("%s\n"), cszText.GetString()); if (m_fnOutputDebugString != nullptr) m_fnOutputDebugString(cszText); return std::vector<Lua::Value>(); }
void Game_RegisterLuaFunctions(lua::State& state) { state.set("debuginfo", lua_debuginfo); state.set("mlook", lua_mlook); state.set("freelook", lua_freelook); state.set("noclip", lua_noclip); state.set("cam_distance", lua_cam_distance); state.set("timescale", lua_timescale); }
Foo(lua::State& state) { state.set("Foo_setA", [this](int value) { a = value; } ); state.set("Foo_setB", std::function<void(int)>(std::bind(&Foo::setB, this, _1)) ); }
bool AnimationList::Load(std::string filename) { // Restore instance to it's original state. this->Cleanup(); // Setup the cleanup scope guard. bool success = false; SCOPE_GUARD_IF(!success, this->Cleanup()); // Get the resource manager. System::ResourceManager* resourceManager = this->GetResourceManager(); if(resourceManager == nullptr) { Log() << LogLoadError(filename) << "Resource manager isn't bound to this instance."; return false; } // Load sprite sheet file. Lua::State lua; if(!lua.Load(filename)) { Log() << LogLoadError(filename) << "Couldn't load the file."; return false; } // Get the global table. lua_getglobal(lua, "AnimationList"); if(!lua_istable(lua, -1)) { Log() << LogLoadError(filename) << "Table \"AnimationList\" is missing or invalid."; return false; } // Load the sprite sheet. lua_getfield(lua, -1, "SpriteSheet"); if(!lua_isstring(lua, -1)) { Log() << LogLoadError(filename) << "Field \"AnimationList.SpriteSheet\" is missing or invalid."; return false; } auto spriteSheet = resourceManager->Load<SpriteSheet>(lua_tostring(lua, -1)); lua_pop(lua, 1); // Save sprite sheet texture. m_texture = spriteSheet->GetTexture(); // Read the animation table. lua_getfield(lua, -1, "Animations"); if(!lua_istable(lua, -1)) { Log() << LogLoadError(filename) << "Field \"AnimationList.Animations\" is missing or invalid."; return false; } // Iterate over the animation table. for(lua_pushnil(lua); lua_next(lua, -2); lua_pop(lua, 1)) { // Check if the key is a string. if(!lua_isstring(lua, -2)) { Log() << LogLoadError(filename) << "One of \"AnimationList.Animations\" keys is not a string."; return false; } // Check if the value is a table. if(!lua_istable(lua, -1)) { Log() << LogLoadError(filename) << "One of \"AnimationList.Animations\" values is not a table."; return false; } // Read animation frames. std::vector<Frame> frames; for(lua_pushnil(lua); lua_next(lua, -2); lua_pop(lua, 1)) { Frame frame; // Check if the value is a table. if(!lua_istable(lua, -1)) { Log() << LogLoadError(filename) << "One of \"AnimationList.Animations.Frames\" values is not a table."; return false; } // Read frame sprite. lua_getfield(lua, -1, "Sprite"); if(!lua_isstring(lua, -1)) { Log() << LogLoadError(filename) << "Field \"Animation.Sprite\" is missing or invalid."; return false; } frame.rectangle = spriteSheet->GetSprite(lua_tostring(lua, -1)); lua_pop(lua, 1); // Read frame offset. lua_getfield(lua, -1, "Offset"); if(lua_istable(lua, -1)) { for(int i = 0; i < 2; ++i) { lua_pushinteger(lua, i + 1); lua_gettable(lua, -2); frame.offset[i] = (float)lua_tonumber(lua, -1); lua_pop(lua, 1); } } lua_pop(lua, 1); // Read frame duration. lua_getfield(lua, -1, "Duration"); if(!lua_isnumber(lua, -1)) { Log() << LogLoadError(filename) << "Field \"Animation.Duration\" is missing or invalid."; return false; } frame.duration = (float)lua_tonumber(lua, -1); lua_pop(lua, 1); // Add animation frame. frames.push_back(frame); } // Add animation. if(!this->AddAnimation(lua_tostring(lua, -2), frames)) { Log() << LogLoadError(filename) << "Couldn't add an animation."; return false; } } lua_pop(lua, 1); // Success! Log() << "Loaded an animation list from \"" << filename << "\" file."; return success = true; }