Atlas-Launcher/source/script/engine/Engine.cpp

91 lines
2.5 KiB
C++

#include "Engine.hpp"
#include <angelscript.h>
#include <format>
#include <stdexcept>
#include <datetime/datetime.h>
#include <scriptany/scriptany.h>
#include <scriptstdstring/scriptstdstring.h>
#include <scriptarray/scriptarray.h>
#include <scriptdictionary/scriptdictionary.h>
#include <scriptgrid/scriptgrid.h>
#include <scripthandle/scripthandle.h>
#include <scripthelper/scripthelper.h>
#include <scriptmath/scriptmath.h>
#include <vfspp/MemoryFileSystem.hpp>
#include <weakref/weakref.h>
namespace atlas::script::engine {
Engine::Engine() {
// create a new AngelScript engine
this->asEngine = asCreateScriptEngine();
if (this->asEngine == nullptr)
throw std::runtime_error("Could not create the AngelScript Engine.");
this->asEngine->SetMessageCallback(asFUNCTION(this->asCallback), nullptr, asCALL_CDECL);
// AngelScript standard library
RegisterScriptArray(this->asEngine, true);
RegisterScriptGrid(this->asEngine);
RegisterStdString(this->asEngine);
RegisterStdStringUtils(this->asEngine);
RegisterScriptDictionary(this->asEngine);
RegisterScriptDateTime(this->asEngine);
RegisterScriptAny(this->asEngine);
RegisterScriptHandle(this->asEngine);
RegisterScriptWeakRef(this->asEngine);
RegisterExceptionRoutines(this->asEngine);
RegisterScriptMath(this->asEngine);
// initialise the file system
this->fileSystem = std::make_unique<vfspp::VirtualFileSystem>();
// TODO(Faraphel): is the memory fs
auto memoryFileSystem = std::make_unique<vfspp::MemoryFileSystem>();
memoryFileSystem->Initialize();
this->fileSystem->AddFileSystem("/tmp", std::move(memoryFileSystem));
}
void Engine::asCallback(const asSMessageInfo *message, void *args) {
std::string type = "UNKNOWN";
std::ostream* output = &std::cout;
// get a representation for the message type
switch (message->type) {
case asMSGTYPE_INFORMATION:
type = "INFO";
output = &std::cout;
case asMSGTYPE_WARNING:
type = "WARN";
output = &std::cerr;
case asMSGTYPE_ERROR:
type = "ERROR";
output = &std::cerr;
}
// display the message
*output << std::format(
"{} ({}, {}) [{}] {}",
message->section,
message->row,
message->col,
type,
message->message
) << std::endl;
}
Engine::~Engine() {
// release the engine
this->asEngine->ShutDownAndRelease();
}
asIScriptEngine *Engine::getAsEngine() const {
return this->asEngine;
}
}