mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2026-01-15 11:03:00 +01:00
Add console API using dukglue
This commit is contained in:
32
src/openrct2/scripting/ScConsole.hpp
Normal file
32
src/openrct2/scripting/ScConsole.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <dukglue/dukglue.h>
|
||||
#include "../interface/Console.h"
|
||||
|
||||
namespace OpenRCT2::Scripting
|
||||
{
|
||||
class ScConsole
|
||||
{
|
||||
private:
|
||||
InteractiveConsole& _console;
|
||||
public:
|
||||
ScConsole(InteractiveConsole& console) :
|
||||
_console(console)
|
||||
{
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
_console.Clear();
|
||||
}
|
||||
|
||||
void log(const std::string &s)
|
||||
{
|
||||
_console.WriteLine(s);
|
||||
}
|
||||
|
||||
static void Register(duk_context * ctx)
|
||||
{
|
||||
dukglue_register_method(ctx, &ScConsole::clear, "clear");
|
||||
dukglue_register_method(ctx, &ScConsole::log, "log");
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -9,9 +9,13 @@
|
||||
|
||||
#include "ScriptEngine.h"
|
||||
#include "../interface/InteractiveConsole.h"
|
||||
#include <dukglue/dukglue.h>
|
||||
#include <duktape.h>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ScConsole.hpp"
|
||||
|
||||
using namespace OpenRCT2;
|
||||
using namespace OpenRCT2::Scripting;
|
||||
|
||||
@@ -21,11 +25,6 @@ ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& en
|
||||
_console(console),
|
||||
_env(env)
|
||||
{
|
||||
_context = duk_create_heap_default();
|
||||
if (_context == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Unable to initialise duktape context.");
|
||||
}
|
||||
}
|
||||
|
||||
ScriptEngine::~ScriptEngine()
|
||||
@@ -33,8 +32,28 @@ ScriptEngine::~ScriptEngine()
|
||||
duk_destroy_heap(_context);
|
||||
}
|
||||
|
||||
void ScriptEngine::Initialise()
|
||||
{
|
||||
_context = duk_create_heap_default();
|
||||
if (_context == nullptr)
|
||||
{
|
||||
throw std::runtime_error("Unable to initialise duktape context.");
|
||||
}
|
||||
|
||||
auto ctx = _context;
|
||||
ScConsole::Register(ctx);
|
||||
|
||||
auto scConsole = std::make_shared<ScConsole>(_console);
|
||||
dukglue_register_global(ctx, scConsole, "console");
|
||||
}
|
||||
|
||||
void ScriptEngine::Update()
|
||||
{
|
||||
if (!_initialised)
|
||||
{
|
||||
Initialise();
|
||||
_initialised = true;
|
||||
}
|
||||
while (_evalQueue.size() > 0)
|
||||
{
|
||||
auto item = std::move(_evalQueue.front());
|
||||
|
||||
@@ -31,7 +31,8 @@ namespace OpenRCT2::Scripting
|
||||
private:
|
||||
InteractiveConsole& _console;
|
||||
IPlatformEnvironment& _env;
|
||||
duk_context * _context;
|
||||
bool _initialised{};
|
||||
duk_context * _context{};
|
||||
std::queue<std::tuple<std::promise<void>, std::string>> _evalQueue;
|
||||
|
||||
public:
|
||||
@@ -39,6 +40,7 @@ namespace OpenRCT2::Scripting
|
||||
ScriptEngine(ScriptEngine&&) = delete;
|
||||
~ScriptEngine();
|
||||
|
||||
void Initialise();
|
||||
void Update();
|
||||
std::future<void> Eval(const std::string &s);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user