Atlas-Launcher/source/main.cpp

66 lines
1.8 KiB
C++

#include <iostream>
#include <memory>
#include <vfspp/VirtualFileSystem.hpp>
#include <vfspp/MemoryFileSystem.hpp>
#include <angelscript.h>
#include <scriptbuilder/scriptbuilder.h>
#include "script/engine/Engine.hpp"
#include "script/module/debug/load.hpp"
int main(int argc, char* argv[]) {
int error;
// engine
const auto atlasEngine = std::make_unique<atlas::script::engine::Engine>();
auto* asEngine = atlasEngine->getAsEngine();
// functions
atlas::script::module::debug::load(atlasEngine.get());
// script
CScriptBuilder builder;
error = builder.StartNewModule(asEngine, "script");
if (error < 0)
throw std::runtime_error("Could not start a new module.");
error = builder.AddSectionFromMemory("script", R"(
void main() {
array<int> data(256);
atlas::debug::log("main", "hello world !");
}
)");
if (error < 0)
throw std::runtime_error("Could not add a section to the module.");
error = builder.BuildModule();
if (error < 0)
throw std::runtime_error("Could not build the module.");
// execution
asIScriptModule *module = asEngine->GetModule("script");
if (module == nullptr)
throw std::runtime_error("Could not get the module.");
asIScriptFunction *function = module->GetFunctionByDecl("void main()");
if (function == nullptr)
throw std::runtime_error("Could not get the main function.");
asIScriptContext *context = asEngine->CreateContext();
if (context == nullptr)
throw std::runtime_error("Could not create the context.");
error = context->Prepare(function);
if (error < 0)
throw std::runtime_error("Could not register the function in the context.");
error = context->Execute();
// clean up
context->Release();
return error;
}