Skip to content

Commit

Permalink
Enable QuickJS REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmbernardi committed Dec 25, 2024
1 parent 624ca55 commit 4ccccf0
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 120 deletions.
12 changes: 6 additions & 6 deletions src/openrct2/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace OpenRCT2
std::unique_ptr<DiscordService> _discordService;
#endif
StdInOutConsole _stdInOutConsole;
#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
ScriptEngine _scriptEngine;
#endif
#ifndef DISABLE_NETWORK
Expand Down Expand Up @@ -182,7 +182,7 @@ namespace OpenRCT2
, _scenarioRepository(CreateScenarioRepository(_env))
, _replayManager(CreateReplayManager())
, _gameStateSnapshots(CreateGameStateSnapshots())
#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
, _scriptEngine(_stdInOutConsole, *env)
#endif
#ifndef DISABLE_NETWORK
Expand All @@ -202,7 +202,7 @@ namespace OpenRCT2
// NOTE: We must shutdown all systems here before Instance is set back to null.
// If objects use GetContext() in their destructor things won't go well.

#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
_scriptEngine.StopUnloadRegisterAllPlugins();
#endif

Expand Down Expand Up @@ -239,7 +239,7 @@ namespace OpenRCT2
return _uiContext;
}

#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
Scripting::ScriptEngine& GetScriptEngine() override
{
return _scriptEngine;
Expand Down Expand Up @@ -584,7 +584,7 @@ namespace OpenRCT2

void InitialiseScriptEngine()
{
#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
OpenProgress(STR_LOADING_PLUGIN_ENGINE);
_scriptEngine.Initialise();
_uiContext->InitialiseScriptExtensions();
Expand Down Expand Up @@ -1399,7 +1399,7 @@ namespace OpenRCT2
#endif

ChatUpdate();
#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
if (GetActiveScene() != GetPreloaderScene())
{
_scriptEngine.Tick();
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace OpenRCT2
virtual Localisation::LocalisationService& GetLocalisationService() = 0;
virtual IObjectManager& GetObjectManager() = 0;
virtual IObjectRepository& GetObjectRepository() = 0;
#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
virtual Scripting::ScriptEngine& GetScriptEngine() = 0;
#endif
virtual ITrackDesignRepository* GetTrackDesignRepository() = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/actions/CustomAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#pragma once

#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR

#include "GameAction.h"

Expand Down
2 changes: 1 addition & 1 deletion src/openrct2/command_line/RootCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ static void PrintVersion()
Console::WriteLine();
Console::WriteFormat("Network version: %s", NetworkGetVersion().c_str());
Console::WriteLine();
#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
Console::WriteFormat("Plugin API version: %d", OpenRCT2::Scripting::OPENRCT2_PLUGIN_API_VERSION);
Console::WriteLine();
#else
Expand Down
4 changes: 2 additions & 2 deletions src/openrct2/interface/StdInOutConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void StdInOutConsole::Start()

std::future<void> StdInOutConsole::Eval(const std::string& s)
{
#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR
auto& scriptEngine = OpenRCT2::GetContext()->GetScriptEngine();
return scriptEngine.Eval(s);
#else
Expand All @@ -82,7 +82,7 @@ std::future<void> StdInOutConsole::Eval(const std::string& s)

void StdInOutConsole::ProcessEvalQueue()
{
#ifndef ENABLE_SCRIPTING
#ifndef ENABLE_SCRIPTING_REFACTOR
while (_evalQueue.size() > 0)
{
auto item = std::move(_evalQueue.front());
Expand Down
10 changes: 7 additions & 3 deletions src/openrct2/scripting/HookEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/

#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR

#include "HookEngine.h"

Expand Down Expand Up @@ -53,7 +53,7 @@ HookEngine::HookEngine(ScriptEngine& scriptEngine)
}
}

uint32_t HookEngine::Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function)
uint32_t HookEngine::Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, JSValue function)
{
auto& hookList = GetHookList(type);
auto cookie = _nextCookie++;
Expand Down Expand Up @@ -118,7 +118,7 @@ void HookEngine::Call(HOOK_TYPE type, bool isGameStateMutable)
}
}

void HookEngine::Call(HOOK_TYPE type, const DukValue& arg, bool isGameStateMutable)
void HookEngine::Call(HOOK_TYPE type, const JSValue arg, bool isGameStateMutable)
{
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
Expand All @@ -130,6 +130,9 @@ void HookEngine::Call(HOOK_TYPE type, const DukValue& arg, bool isGameStateMutab
void HookEngine::Call(
HOOK_TYPE type, const std::initializer_list<std::pair<std::string_view, std::any>>& args, bool isGameStateMutable)
{
//TODO (mber)
throw std::runtime_error("HookEngine::Call() not implemented");
/*
auto& hookList = GetHookList(type);
for (auto& hook : hookList.Hooks)
{
Expand Down Expand Up @@ -160,6 +163,7 @@ void HookEngine::Call(
dukArgs.push_back(DukValue::take_from_stack(ctx));
_scriptEngine.ExecutePluginCall(hook.Owner, hook.Function, dukArgs, isGameStateMutable);
}
*/
}

HookList& HookEngine::GetHookList(HOOK_TYPE type)
Expand Down
12 changes: 6 additions & 6 deletions src/openrct2/scripting/HookEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

#pragma once

#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR

#include "Duktape.hpp"
#include <quickjs.h>

#include <any>
#include <memory>
Expand Down Expand Up @@ -53,10 +53,10 @@ namespace OpenRCT2::Scripting
{
uint32_t Cookie;
std::shared_ptr<Plugin> Owner;
DukValue Function;
JSValue Function;

Hook() = default;
Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const DukValue& function)
Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const JSValue function)
: Cookie(cookie)
, Owner(owner)
, Function(function)
Expand Down Expand Up @@ -84,14 +84,14 @@ namespace OpenRCT2::Scripting
public:
HookEngine(ScriptEngine& scriptEngine);
HookEngine(const HookEngine&) = delete;
uint32_t Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function);
uint32_t Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, JSValue function);
void Unsubscribe(HOOK_TYPE type, uint32_t cookie);
void UnsubscribeAll(std::shared_ptr<const Plugin> owner);
void UnsubscribeAll();
bool HasSubscriptions(HOOK_TYPE type) const;
bool IsValidHookForPlugin(HOOK_TYPE type, Plugin& plugin) const;
void Call(HOOK_TYPE type, bool isGameStateMutable);
void Call(HOOK_TYPE type, const DukValue& arg, bool isGameStateMutable);
void Call(HOOK_TYPE type, JSValue arg, bool isGameStateMutable);
void Call(
HOOK_TYPE type, const std::initializer_list<std::pair<std::string_view, std::any>>& args, bool isGameStateMutable);

Expand Down
28 changes: 22 additions & 6 deletions src/openrct2/scripting/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/

#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR

#include "Plugin.h"

#include "../Diagnostic.h"
#include "../OpenRCT2.h"
#include "../core/File.h"
#include "Duktape.hpp"
#include "ScriptEngine.h"

#include <fstream>
#include <memory>

using namespace OpenRCT2::Scripting;

Plugin::Plugin(duk_context* context, std::string_view path)
Plugin::Plugin(JSContext* context, std::string_view path)
: _context(context)
, _path(path)
{
Expand All @@ -35,6 +34,9 @@ void Plugin::SetCode(std::string_view code)

void Plugin::Load()
{
// TODO (mber) plugins
throw std::runtime_error("Plugin::Load() not implemented");
/*
if (!_path.empty())
{
LoadCodeFromFile();
Expand Down Expand Up @@ -73,10 +75,12 @@ void Plugin::Load()
_metadata = GetMetadata(DukValue::take_from_stack(_context));
_hasLoaded = true;
*/
}

void Plugin::Start()
{
/*
if (!_hasLoaded)
{
throw std::runtime_error("Plugin has not been loaded.");
Expand All @@ -99,6 +103,7 @@ void Plugin::Start()
throw std::runtime_error("[" + _metadata.Name + "] " + val);
}
duk_pop(_context);
*/
}

void Plugin::StopBegin()
Expand All @@ -114,10 +119,14 @@ void Plugin::StopEnd()

void Plugin::ThrowIfStopping() const
{
// TODO (mber) find out how to do something similar in quickjs.
throw std::runtime_error("ThrowIfStopping() not implemented");
/*
if (IsStopping())
{
duk_error(_context, DUK_ERR_ERROR, "Plugin is stopping.");
}
*/
}

void Plugin::Unload()
Expand All @@ -139,15 +148,18 @@ void Plugin::LoadCodeFromFile()
_code = File::ReadAllText(_path);
}

static std::string TryGetString(const DukValue& value, const std::string& message)
/*
static std::string TryGetString(const JSValue value, const std::string& message)
{
if (value.type() != DukValue::Type::STRING)
throw std::runtime_error(message);
return value.as_string();
}
*/

PluginMetadata Plugin::GetMetadata(const DukValue& dukMetadata)
PluginMetadata Plugin::GetMetadata(const JSValue dukMetadata)
{
/*
PluginMetadata metadata;
if (dukMetadata.type() == DukValue::Type::OBJECT)
{
Expand Down Expand Up @@ -191,6 +203,8 @@ PluginMetadata Plugin::GetMetadata(const DukValue& dukMetadata)
metadata.Main = dukMetadata["main"];
}
return metadata;
*/
return PluginMetadata{};
}

PluginType Plugin::ParsePluginType(std::string_view type)
Expand All @@ -204,10 +218,12 @@ PluginType Plugin::ParsePluginType(std::string_view type)
throw std::invalid_argument("Unknown plugin type.");
}

void Plugin::CheckForLicence(const DukValue& dukLicence, std::string_view pluginName)
void Plugin::CheckForLicence(const JSValue dukLicence, std::string_view pluginName)
{
/*
if (dukLicence.type() != DukValue::Type::STRING || dukLicence.as_string().empty())
LOG_ERROR("Plugin %s does not specify a licence", std::string(pluginName).c_str());
*/
}

int32_t Plugin::GetTargetAPIVersion() const
Expand Down
16 changes: 8 additions & 8 deletions src/openrct2/scripting/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

#pragma once

#ifdef ENABLE_SCRIPTING
#ifdef ENABLE_SCRIPTING_REFACTOR

#include "Duktape.hpp"
#include <quickjs.h>

#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
Expand Down Expand Up @@ -49,13 +49,13 @@ namespace OpenRCT2::Scripting
PluginType Type{};
int32_t MinApiVersion{};
std::optional<int32_t> TargetApiVersion{};
DukValue Main;
JSValue Main;
};

class Plugin
{
private:
duk_context* _context{};
JSContext* _context{};
std::string _path;
PluginMetadata _metadata{};
std::string _code;
Expand Down Expand Up @@ -102,7 +102,7 @@ namespace OpenRCT2::Scripting
int32_t GetTargetAPIVersion() const;

Plugin() = default;
Plugin(duk_context* context, std::string_view path);
Plugin(JSContext* context, std::string_view path);
Plugin(const Plugin&) = delete;
Plugin(Plugin&&) = delete;

Expand All @@ -120,9 +120,9 @@ namespace OpenRCT2::Scripting
private:
void LoadCodeFromFile();

static PluginMetadata GetMetadata(const DukValue& dukMetadata);
static PluginMetadata GetMetadata(JSValue dukMetadata);
static PluginType ParsePluginType(std::string_view type);
static void CheckForLicence(const DukValue& dukLicence, std::string_view pluginName);
static void CheckForLicence(JSValue dukLicence, std::string_view pluginName);
};
} // namespace OpenRCT2::Scripting

Expand Down
Loading

0 comments on commit 4ccccf0

Please sign in to comment.