Change ExecuteScript and ExecuteString to return void and throw a TENScriptException on error. Remove my old comments which didn't really add info. Implement SetSettings.

This commit is contained in:
hispidence 2021-08-03 15:12:24 +01:00
parent efc91a8e34
commit 58a424e52f
4 changed files with 26 additions and 39 deletions

View file

@ -54,7 +54,6 @@ Add a level to the gameflow.
*/ */
m_lua->set_function("SetAudioTracks", &GameFlow::SetAudioTracks, this); m_lua->set_function("SetAudioTracks", &GameFlow::SetAudioTracks, this);
/*** /***
@function SetStrings @function SetStrings
@tparam table table array-style table with strings @tparam table table array-style table with strings
@ -66,6 +65,12 @@ Add a level to the gameflow.
@tparam table table array-style table with TODO EXTRA INFO HERE @tparam table table array-style table with TODO EXTRA INFO HERE
*/ */
m_lua->set_function("SetLanguageNames", &GameFlow::SetLanguageNames, this); m_lua->set_function("SetLanguageNames", &GameFlow::SetLanguageNames, this);
/***
@function SetSettings
@tparam table table array-style table with TODO EXTRA INFO HERE
*/
m_lua->set_function("SetSettings", &GameFlow::SetSettings, this);
} }
GameFlow::~GameFlow() GameFlow::~GameFlow()
@ -86,6 +91,11 @@ void GameFlow::SetStrings(sol::nested<std::unordered_map<std::string, std::vecto
m_translationsMap = std::move(src); m_translationsMap = std::move(src);
} }
void GameFlow::SetSettings(GameScriptSettings const & src)
{
m_settings = src;
}
void GameFlow::AddLevel(GameScriptLevel const& level) void GameFlow::AddLevel(GameScriptLevel const& level)
{ {
Levels.push_back(new GameScriptLevel{ level }); Levels.push_back(new GameScriptLevel{ level });
@ -108,30 +118,13 @@ void GameFlow::SetAudioTracks(sol::as_table_t<std::vector<GameScriptAudioTrack>>
} }
} }
bool GameFlow::LoadGameFlowScript() void GameFlow::LoadGameFlowScript()
{ {
// Load the enums file ExecuteScript("Scripts/Enums.lua");
std::string err; ExecuteScript("Scripts/Tracks.lua");
if (!ExecuteScript("Scripts/Enums.lua", err)) { ExecuteScript("Scripts/Gameflow.lua");
std::cout << err << "\n"; ExecuteScript("Scripts/Strings.lua");
} ExecuteScript("Scripts/Settings.lua");
// Load the new audio tracks file
if (!ExecuteScript("Scripts/Tracks.lua", err)) {
std::cout << err << "\n";
}
// Load the new script file
if (!ExecuteScript("Scripts/Gameflow.lua", err)) {
std::cout << err << "\n";
}
// Populate strings
if (!ExecuteScript("Scripts/Strings.lua", err)) {
std::cout << err << "\n";
}
return true;
} }
char const * GameFlow::GetString(const char* id) char const * GameFlow::GetString(const char* id)

View file

@ -50,10 +50,11 @@ public:
void AddLevel(GameScriptLevel const& level); void AddLevel(GameScriptLevel const& level);
void SetAudioTracks(sol::as_table_t<std::vector<GameScriptAudioTrack>>&& src); void SetAudioTracks(sol::as_table_t<std::vector<GameScriptAudioTrack>>&& src);
bool LoadGameFlowScript(); void LoadGameFlowScript();
char const * GetString(const char* id); char const * GetString(const char* id);
void SetStrings(sol::nested<std::unordered_map<std::string, std::vector<std::string>>> && src); void SetStrings(sol::nested<std::unordered_map<std::string, std::vector<std::string>>> && src);
void SetLanguageNames(sol::as_table_t<std::vector<std::string>> && src); void SetLanguageNames(sol::as_table_t<std::vector<std::string>> && src);
void SetSettings(GameScriptSettings const & src);
GameScriptSettings* GetSettings(); GameScriptSettings* GetSettings();
GameScriptLevel* GetLevel(int id); GameScriptLevel* GetLevel(int id);
void SetHorizon(bool horizon, bool colAddHorizon); void SetHorizon(bool horizon, bool colAddHorizon);

View file

@ -6,27 +6,20 @@ LuaHandler::LuaHandler(sol::state* lua) {
m_lua = lua; m_lua = lua;
} }
bool LuaHandler::ExecuteScript(std::string const& luaFilename, std::string & message) { void LuaHandler::ExecuteScript(std::string const& luaFilename) {
auto result = m_lua->safe_script_file(luaFilename); auto result = m_lua->safe_script_file(luaFilename, sol::script_pass_on_error);
//auto result = m_lua->safe_script_file(luaFilename, sol::environment(m_lua->lua_state(), sol::create, m_lua->globals()), sol::script_pass_on_error);
if (!result.valid()) if (!result.valid())
{ {
sol::error error = result; sol::error error = result;
message = error.what(); throw TENScriptException{ error.what() };
return false;
} }
return true;
} }
bool LuaHandler::ExecuteString(std::string const & command, std::string& message) { void LuaHandler::ExecuteString(std::string const & command) {
auto result = m_lua->safe_script(command, sol::environment(m_lua->lua_state(), sol::create, m_lua->globals()), sol::script_pass_on_error); auto result = m_lua->safe_script(command, sol::environment(m_lua->lua_state(), sol::create, m_lua->globals()), sol::script_pass_on_error);
if (!result.valid()) if (!result.valid())
{ {
sol::error error = result; sol::error error = result;
message = error.what(); throw TENScriptException{ error.what() };
return false;
} }
return true;
} }

View file

@ -10,6 +10,6 @@ public:
LuaHandler(LuaHandler const &) = delete; LuaHandler(LuaHandler const &) = delete;
LuaHandler& operator=(LuaHandler const &) = delete; LuaHandler& operator=(LuaHandler const &) = delete;
bool ExecuteScript(const std::string & luaFilename, std::string & message); void ExecuteScript(const std::string & luaFilename);
bool ExecuteString(const std::string& command, std::string& message); void ExecuteString(const std::string & command);
}; };