Skip to content

Commit

Permalink
[Script/LuaWrapper] Added debug logs for Lua wrapping classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Razakhel committed Dec 4, 2023
1 parent c73022e commit ba6105c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/RaZ/Script/LuaEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,35 @@ bool LuaEnvironment::execute(const std::string& code) const {
if (code.empty())
return false;

Logger::debug("[LuaEnvironment] Executing code...");

try {
LuaWrapper::getState().script(code, *m_environment);
} catch (const sol::error& err) {
Logger::error("[LuaEnvironment] Error running code: '" + std::string(err.what()) + "'.");
Logger::error("[LuaEnvironment] Error executing code: '" + std::string(err.what()) + "'.");
return false;
}

Logger::debug("[LuaEnvironment] Executed code");

return true;
}

bool LuaEnvironment::executeFromFile(const FilePath& filePath) const {
if (filePath.isEmpty())
return false;

Logger::debug("[LuaEnvironment] Executing code from file ('" + filePath + "')...");

try {
LuaWrapper::getState().script_file(filePath.toUtf8(), *m_environment);
} catch (const sol::error& err) {
Logger::error("[LuaEnvironment] Error running file: '" + std::string(err.what()) + "'.");
Logger::error("[LuaEnvironment] Error executing code from file: '" + std::string(err.what()) + "'.");
return false;
}

Logger::debug("[LuaEnvironment] Executed code from file");

return true;
}

Expand All @@ -46,13 +54,17 @@ bool LuaEnvironment::exists(const char* name) const {
}

void LuaEnvironment::clear() {
Logger::debug("[LuaEnvironment] Clearing environment...");
m_environment->clear();
Logger::debug("[LuaEnvironment] Cleared environment");
}

LuaEnvironment::~LuaEnvironment() = default;

void LuaEnvironment::registerEntity(const Entity& entity, const std::string& name) {
Logger::debug("[LuaEnvironment] Registering entity (ID: " + std::to_string(entity.getId()) + ") as '" + name + "'...");
m_environment->operator[](name) = &entity;
Logger::debug("[LuaEnvironment] Registered entity");
}

sol::reference LuaEnvironment::get(const char* name) const {
Expand Down
16 changes: 16 additions & 0 deletions src/RaZ/Script/LuaScript.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#include "RaZ/Application.hpp"
#include "RaZ/Script/LuaScript.hpp"
#include "RaZ/Script/LuaWrapper.hpp"
#include "RaZ/Utils/FilePath.hpp"
#include "RaZ/Utils/FileUtils.hpp"
#include "RaZ/Utils/Logger.hpp"

#define SOL_SAFE_GETTER 0 // Allowing implicit conversion to bool
#include "sol/sol.hpp"

namespace Raz {

LuaScript::LuaScript(const std::string& code) {
Logger::debug("[LuaScript] Creating script...");

Raz::LuaWrapper::registerTypes();
loadCode(code);

Logger::debug("[LuaScript] Created script");
}

void LuaScript::loadCode(const std::string& code) {
Logger::debug("[LuaScript] Loading code...");

const sol::object owningEntity = m_environment.get("this");

m_environment.clear();
Expand All @@ -28,10 +36,14 @@ void LuaScript::loadCode(const std::string& code) {
m_environment.registerEntity(owningEntity.as<Entity>(), "this");
setup();
}

Logger::debug("[LuaScript] Loaded code");
}

void LuaScript::loadCodeFromFile(const FilePath& filePath) {
Logger::debug("[LuaScript] Loading code from file ('" + filePath + "')...");
loadCode(FileUtils::readFile(filePath));
Logger::debug("[LuaScript] Loaded code from file");
}

bool LuaScript::update(const FrameTimeInfo& timeInfo) const {
Expand All @@ -49,10 +61,14 @@ void LuaScript::setup() const {
if (setupRef.get_type() != sol::type::function)
return;

Logger::debug("[LuaScript] Running script setup...");

const sol::unsafe_function setupFunc = setupRef;

if (!setupFunc().valid())
throw std::runtime_error("Error: The Lua script failed to be setup");

Logger::debug("[LuaScript] Ran script setup");
}

} // namespace Raz
20 changes: 18 additions & 2 deletions src/RaZ/Script/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Raz {

void LuaWrapper::registerTypes() {
[[maybe_unused]] static const bool _ = [] () {
Logger::debug("[LuaWrapper] Registering types...");

registerAnimationTypes();
#if defined(RAZ_USE_AUDIO)
registerAudioTypes();
Expand Down Expand Up @@ -39,6 +41,8 @@ void LuaWrapper::registerTypes() {
registerVectorTypes();
registerWindowTypes();

Logger::debug("[LuaWrapper] Registered types");

return true;
}();
}
Expand All @@ -47,35 +51,47 @@ bool LuaWrapper::execute(const std::string& code) {
if (code.empty())
return false;

Logger::debug("[LuaWrapper] Executing code...");

try {
getState().script(code);
} catch (const sol::error& err) {
Logger::error("[LuaWrapper] Error running code: '" + std::string(err.what()) + "'.");
Logger::error("[LuaWrapper] Error executing code: '" + std::string(err.what()) + "'.");
return false;
}

Logger::debug("[LuaWrapper] Executed code");

return true;
}

bool LuaWrapper::executeFromFile(const FilePath& filePath) {
if (filePath.isEmpty())
return false;

Logger::debug("[LuaWrapper] Executing code from file ('" + filePath + "')...");

try {
getState().script_file(filePath.toUtf8());
} catch (const sol::error& err) {
Logger::error("[LuaWrapper] Error running file: '" + std::string(err.what()) + "'.");
Logger::error("[LuaWrapper] Error executing code from file: '" + std::string(err.what()) + "'.");
return false;
}

Logger::debug("[LuaWrapper] Executed code from file");

return true;
}

sol::state& LuaWrapper::getState() {
static sol::state state = [] () {
Logger::debug("[LuaWrapper] Initializing state...");

sol::state luaState;
luaState.open_libraries(sol::lib::base, sol::lib::math, sol::lib::string);

Logger::debug("[LuaWrapper] Initialized state");

return luaState;
}();

Expand Down

0 comments on commit ba6105c

Please sign in to comment.