feat: improve logging output, add #include support to AngelScript

This commit is contained in:
2025-11-06 14:37:35 +13:00
parent 9d8c3bd605
commit a337b9002f
8 changed files with 146 additions and 28 deletions
+71 -13
View File
@@ -1,8 +1,10 @@
#include "ScriptEngine.h"
#include "ScriptBindings.h"
#include "scriptstdstring.h"
#include "scriptbuilder.h"
#include <fstream>
#include <sstream>
#include <cstring>
#include <assert.h>
#include "log/log.h"
@@ -49,23 +51,24 @@ void ScriptEngine::Shutdown() {
bool ScriptEngine::CompileScript(const std::string& filename) {
if (!engine) return false;
std::string code = ReadFile(filename);
if (code.empty()) {
log_error("Failed to read script file: %s", filename.c_str());
// Try to compile in a temporary module first using CScriptBuilder
CScriptBuilder tempBuilder;
tempBuilder.SetIncludeCallback(IncludeCallback, nullptr);
int r = tempBuilder.StartNewModule(engine, "temp");
if (r < 0) {
log_error("Failed to start new temp module");
return false;
}
// Try to compile in a temporary module first
asIScriptModule* tempMod = engine->GetModule("temp", asGM_ALWAYS_CREATE);
int r = tempMod->AddScriptSection(filename.c_str(), code.c_str());
r = tempBuilder.AddSectionFromFile(filename.c_str());
if (r < 0) {
log_error("Failed to add script section for: %s", filename.c_str());
log_error("Failed to add script section from file: %s", filename.c_str());
engine->DiscardModule("temp");
return false;
}
r = tempMod->Build();
r = tempBuilder.BuildModule();
if (r < 0) {
log_error("Failed to build script: %s", filename.c_str());
engine->DiscardModule("temp");
@@ -78,11 +81,16 @@ bool ScriptEngine::CompileScript(const std::string& filename) {
engine->DiscardModule("main");
}
// Create new main module with the working code
currentModule = engine->GetModule("main", asGM_ALWAYS_CREATE);
r = currentModule->AddScriptSection(filename.c_str(), code.c_str());
// Create new main module with the working code using CScriptBuilder
CScriptBuilder mainBuilder;
mainBuilder.SetIncludeCallback(IncludeCallback, nullptr);
r = mainBuilder.StartNewModule(engine, "main");
if (r >= 0) {
r = currentModule->Build();
r = mainBuilder.AddSectionFromFile(filename.c_str());
}
if (r >= 0) {
r = mainBuilder.BuildModule();
}
// Clean up temp module
@@ -95,6 +103,9 @@ bool ScriptEngine::CompileScript(const std::string& filename) {
return false;
}
// Get the newly created module
currentModule = engine->GetModule("main");
// Cache new functions
updateFunc = currentModule->GetFunctionByName("Update");
drawFunc = currentModule->GetFunctionByName("Draw");
@@ -152,6 +163,53 @@ void ScriptEngine::MessageCallback(const asSMessageInfo* msg, void*) {
}
}
int ScriptEngine::IncludeCallback(const char* include, const char* from, CScriptBuilder* builder, void* userParam) {
// Extract just the filename from the from path for cleaner logging
const char* fromFile = from;
if (from) {
const char* lastSlash = strrchr(from, '/');
const char* lastBackslash = strrchr(from, '\\');
const char* lastSep = (lastSlash > lastBackslash) ? lastSlash : lastBackslash;
if (lastSep) {
fromFile = lastSep + 1;
}
}
// Log the include request with clean filenames
log_info("Including: %s (from: %s)", include, fromFile ? fromFile : "main");
// Build the full path relative to the including file's directory
std::string fullPath;
if (from && strlen(from) > 0) {
// Extract directory from the 'from' path
std::string fromPath(from);
size_t lastSlash = fromPath.find_last_of("/\\");
if (lastSlash != std::string::npos) {
// Get the directory part
std::string directory = fromPath.substr(0, lastSlash + 1);
fullPath = directory + include;
} else {
// No directory in from path, use include as-is
fullPath = include;
}
} else {
fullPath = include;
}
// Try to add the included file
int r = builder->AddSectionFromFile(fullPath.c_str());
if (r < 0) {
log_error("Failed to include file: %s (tried path: %s)", include, fullPath.c_str());
} else {
log_info("Successfully included: %s", include);
}
return r;
}
std::string ScriptEngine::ReadFile(const std::string& filename) {
std::ifstream file(filename);
if (!file) return "";