Skip to content

Commit

Permalink
[Script] Instrumented scripting features for profiling purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
Razakhel committed Mar 21, 2024
1 parent 719247a commit 3ffe42b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/RaZ/Script/LuaEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
#define SOL_PRINT_ERRORS 0
#include "sol/sol.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

LuaEnvironment::LuaEnvironment()
: m_environment{ std::make_unique<sol::environment>(LuaWrapper::getState(), sol::create, LuaWrapper::getState().globals()) } {}

bool LuaEnvironment::execute(const std::string& code) const {
ZoneScopedN("LuaEnvironment::execute");

if (code.empty())
return false;

Expand All @@ -32,6 +36,8 @@ bool LuaEnvironment::execute(const std::string& code) const {
}

bool LuaEnvironment::executeFromFile(const FilePath& filePath) const {
ZoneScopedN("LuaEnvironment::executeFromFile");

if (filePath.isEmpty())
return false;

Expand Down
10 changes: 10 additions & 0 deletions src/RaZ/Script/LuaScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define SOL_SAFE_GETTER 0 // Allowing implicit conversion to bool
#include "sol/sol.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

LuaScript::LuaScript(const std::string& code) {
Expand All @@ -20,6 +22,8 @@ LuaScript::LuaScript(const std::string& code) {
}

void LuaScript::loadCode(const std::string& code) {
ZoneScopedN("LuaScript::loadCode");

Logger::debug("[LuaScript] Loading code...");

const sol::object owningEntity = m_environment.get("this");
Expand All @@ -41,18 +45,24 @@ void LuaScript::loadCode(const std::string& code) {
}

void LuaScript::loadCodeFromFile(const FilePath& filePath) {
ZoneScopedN("LuaScript::loadCodeFromFile");

Logger::debug("[LuaScript] Loading code from file ('" + filePath + "')...");
loadCode(FileUtils::readFileToString(filePath));
Logger::debug("[LuaScript] Loaded code from file");
}

bool LuaScript::update(const FrameTimeInfo& timeInfo) const {
ZoneScopedN("LuaScript::update");

const sol::unsafe_function updateFunc = m_environment.get("update");
const sol::unsafe_function_result updateRes = updateFunc(timeInfo);
return (updateRes.get_type() == sol::type::none || updateRes);
}

void LuaScript::setup() const {
ZoneScopedN("LuaScript::setup");

if (!m_environment.exists("setup"))
return;

Expand Down
8 changes: 8 additions & 0 deletions src/RaZ/Script/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
#define SOL_PRINT_ERRORS 0
#include "sol/sol.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

void LuaWrapper::registerTypes() {
[[maybe_unused]] static const bool _ = [] () {
ZoneScopedN("LuaWrapper::registerTypes");

Logger::debug("[LuaWrapper] Registering types...");

registerAnimationTypes();
Expand Down Expand Up @@ -50,6 +54,8 @@ void LuaWrapper::registerTypes() {
}

bool LuaWrapper::execute(const std::string& code) {
ZoneScopedN("LuaWrapper::execute");

if (code.empty())
return false;

Expand All @@ -68,6 +74,8 @@ bool LuaWrapper::execute(const std::string& code) {
}

bool LuaWrapper::executeFromFile(const FilePath& filePath) {
ZoneScopedN("LuaWrapper::executeFromFile");

if (filePath.isEmpty())
return false;

Expand Down
4 changes: 4 additions & 0 deletions src/RaZ/Script/ScriptSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include "RaZ/Script/LuaScript.hpp"
#include "RaZ/Script/ScriptSystem.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

ScriptSystem::ScriptSystem() {
registerComponents<LuaScript>();
}

bool ScriptSystem::update(const FrameTimeInfo& timeInfo) {
ZoneScopedN("ScriptSystem::update");

bool res = true;

for (const Entity* entity : m_entities) {
Expand Down

0 comments on commit 3ffe42b

Please sign in to comment.