2018-03-17 19:31:06 +00:00
|
|
|
/*****************************************************************************
|
2026-01-05 14:42:59 +01:00
|
|
|
* Copyright (c) 2014-2026 OpenRCT2 developers
|
2018-03-17 19:31:06 +00:00
|
|
|
*
|
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
|
*
|
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2020-04-24 15:58:01 +01:00
|
|
|
#ifdef ENABLE_SCRIPTING
|
2018-07-31 17:59:48 +02:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "ScriptEngine.h"
|
2018-07-31 17:59:48 +02:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
#include "../Context.h"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "../PlatformEnvironment.h"
|
2026-02-09 15:07:42 +01:00
|
|
|
#include "../actions/GameAction.hpp"
|
2026-02-10 08:57:40 +01:00
|
|
|
#include "../actions/general/CustomAction.h"
|
|
|
|
|
#include "../actions/peep/StaffHireNewAction.h"
|
|
|
|
|
#include "../actions/ride/RideCreateAction.h"
|
|
|
|
|
#include "../actions/scenery/BannerPlaceAction.h"
|
|
|
|
|
#include "../actions/scenery/LargeSceneryPlaceAction.h"
|
|
|
|
|
#include "../actions/scenery/WallPlaceAction.h"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "../config/Config.h"
|
2025-09-29 02:05:00 +10:00
|
|
|
#include "../core/Console.hpp"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "../core/EnumMap.hpp"
|
|
|
|
|
#include "../core/File.h"
|
|
|
|
|
#include "../core/FileScanner.h"
|
|
|
|
|
#include "../core/Path.hpp"
|
|
|
|
|
#include "../interface/InteractiveConsole.h"
|
|
|
|
|
#include "../platform/Platform.h"
|
2026-02-28 15:35:16 +01:00
|
|
|
#include "../ride/ted/PitchAndRoll.h"
|
2025-09-29 02:05:00 +10:00
|
|
|
#include "../profiling/Profiling.h"
|
2025-04-19 23:23:02 +02:00
|
|
|
#include "bindings/entity/ScBalloon.hpp"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "bindings/entity/ScEntity.hpp"
|
|
|
|
|
#include "bindings/entity/ScGuest.hpp"
|
|
|
|
|
#include "bindings/entity/ScLitter.hpp"
|
2025-04-19 23:23:02 +02:00
|
|
|
#include "bindings/entity/ScMoneyEffect.hpp"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "bindings/entity/ScParticle.hpp"
|
|
|
|
|
#include "bindings/entity/ScPeep.hpp"
|
|
|
|
|
#include "bindings/entity/ScStaff.hpp"
|
|
|
|
|
#include "bindings/entity/ScVehicle.hpp"
|
|
|
|
|
#include "bindings/game/ScCheats.hpp"
|
|
|
|
|
#include "bindings/game/ScConsole.hpp"
|
|
|
|
|
#include "bindings/game/ScContext.hpp"
|
|
|
|
|
#include "bindings/game/ScDisposable.hpp"
|
|
|
|
|
#include "bindings/game/ScPlugin.hpp"
|
|
|
|
|
#include "bindings/game/ScProfiler.hpp"
|
|
|
|
|
#include "bindings/network/ScNetwork.hpp"
|
|
|
|
|
#include "bindings/network/ScPlayer.hpp"
|
|
|
|
|
#include "bindings/network/ScPlayerGroup.hpp"
|
|
|
|
|
#include "bindings/network/ScSocket.hpp"
|
|
|
|
|
#include "bindings/object/ScInstalledObject.hpp"
|
|
|
|
|
#include "bindings/object/ScObject.hpp"
|
|
|
|
|
#include "bindings/object/ScObjectManager.h"
|
|
|
|
|
#include "bindings/ride/ScRide.hpp"
|
|
|
|
|
#include "bindings/ride/ScRideStation.hpp"
|
2025-07-13 12:04:26 +02:00
|
|
|
#include "bindings/world/ScAward.hpp"
|
2024-11-29 12:04:56 +09:00
|
|
|
#include "bindings/world/ScDate.hpp"
|
|
|
|
|
#include "bindings/world/ScMap.hpp"
|
|
|
|
|
#include "bindings/world/ScPark.hpp"
|
|
|
|
|
#include "bindings/world/ScParkMessage.hpp"
|
|
|
|
|
#include "bindings/world/ScResearch.hpp"
|
|
|
|
|
#include "bindings/world/ScScenario.hpp"
|
|
|
|
|
#include "bindings/world/ScTile.hpp"
|
|
|
|
|
#include "bindings/world/ScTileElement.hpp"
|
2026-03-08 11:00:07 +01:00
|
|
|
#include "bindings/world/ScWeather.hpp"
|
2020-02-23 12:55:48 +00:00
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#include <cassert>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
#include <string>
|
2018-03-17 23:26:55 +00:00
|
|
|
|
2018-03-17 19:31:06 +00:00
|
|
|
using namespace OpenRCT2;
|
|
|
|
|
using namespace OpenRCT2::Scripting;
|
|
|
|
|
|
2026-02-28 18:02:27 +01:00
|
|
|
using OpenRCT2::TrackMetadata::TrackPitch;
|
|
|
|
|
using OpenRCT2::TrackMetadata::TrackRoll;
|
2026-02-28 15:35:16 +01:00
|
|
|
|
2020-02-25 00:43:49 +00:00
|
|
|
struct ExpressionStringifier final
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
std::stringstream _ss;
|
2025-09-29 02:05:00 +10:00
|
|
|
JSContext* _context{};
|
2020-02-25 00:43:49 +00:00
|
|
|
int32_t _indent{};
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
ExpressionStringifier(JSContext* ctx)
|
2020-02-25 00:43:49 +00:00
|
|
|
: _context(ctx)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PushIndent(int32_t c = 1)
|
|
|
|
|
{
|
|
|
|
|
_indent += c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PopIndent(int32_t c = 1)
|
|
|
|
|
{
|
|
|
|
|
_indent -= c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LineFeed()
|
|
|
|
|
{
|
|
|
|
|
_ss << "\n" << std::string(_indent, ' ');
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void Stringify(const JSValue val, bool canStartWithNewLine, int32_t nestLevel)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2021-02-04 00:25:34 +00:00
|
|
|
if (nestLevel >= 8)
|
|
|
|
|
{
|
|
|
|
|
_ss << "[...]";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsUndefined(val))
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_ss << "undefined";
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsNull(val))
|
|
|
|
|
{
|
|
|
|
|
_ss << "null";
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsUninitialized(val))
|
|
|
|
|
{
|
|
|
|
|
_ss << "uninitialized";
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsBool(val))
|
|
|
|
|
{
|
|
|
|
|
_ss << (JS_VALUE_GET_BOOL(val) ? "true" : "false");
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsNumber(val) || JS_IsBigInt(val))
|
|
|
|
|
{
|
|
|
|
|
StringifyNumber(val);
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsString(val))
|
|
|
|
|
{
|
|
|
|
|
StringifyString(val);
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsObject(val))
|
|
|
|
|
{
|
|
|
|
|
if (JS_IsFunction(_context, val))
|
|
|
|
|
{
|
|
|
|
|
StringifyFunction(val);
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsArray(val))
|
|
|
|
|
{
|
|
|
|
|
StringifyArray(val, canStartWithNewLine, nestLevel);
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsError(val))
|
|
|
|
|
{
|
|
|
|
|
StringifyError(val);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StringifyObject(val, canStartWithNewLine, nestLevel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (JS_IsArrayBuffer(val))
|
|
|
|
|
{
|
|
|
|
|
_ss << "[Buffer]";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ss << "[Unknown Value]";
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void StringifyArray(const JSValue val, bool canStartWithNewLine, int32_t nestLevel)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
constexpr int64_t maxItemsToShow = 4;
|
2020-02-25 00:43:49 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
int64_t arrayLen;
|
|
|
|
|
if (JS_GetLength(_context, val, &arrayLen) == -1)
|
|
|
|
|
{
|
|
|
|
|
_ss << "[error printing array]";
|
|
|
|
|
}
|
|
|
|
|
else if (arrayLen == 0)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
|
|
|
|
_ss << "[]";
|
|
|
|
|
}
|
|
|
|
|
else if (arrayLen == 1)
|
|
|
|
|
{
|
|
|
|
|
_ss << "[ ";
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue prop = JS_GetPropertyInt64(_context, val, 0);
|
|
|
|
|
Stringify(prop, false, nestLevel + 1);
|
|
|
|
|
JS_FreeValue(_context, prop);
|
2020-02-25 00:43:49 +00:00
|
|
|
_ss << " ]";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (canStartWithNewLine)
|
|
|
|
|
{
|
|
|
|
|
PushIndent();
|
|
|
|
|
LineFeed();
|
|
|
|
|
}
|
|
|
|
|
_ss << "[ ";
|
|
|
|
|
PushIndent(2);
|
2025-09-29 02:05:00 +10:00
|
|
|
for (int64_t i = 0; i < arrayLen; i++)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
|
|
|
|
if (i != 0)
|
|
|
|
|
{
|
|
|
|
|
_ss << ",";
|
|
|
|
|
LineFeed();
|
|
|
|
|
}
|
|
|
|
|
if (i >= maxItemsToShow)
|
|
|
|
|
{
|
|
|
|
|
auto remainingItemsNotShown = arrayLen - maxItemsToShow;
|
|
|
|
|
if (remainingItemsNotShown == 1)
|
|
|
|
|
{
|
|
|
|
|
_ss << "... 1 more item";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ss << "... " << std::to_string(remainingItemsNotShown) << " more items";
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue prop = JS_GetPropertyInt64(_context, val, i);
|
|
|
|
|
Stringify(prop, false, nestLevel + 1);
|
|
|
|
|
JS_FreeValue(_context, prop);
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
_ss << " ]";
|
|
|
|
|
PopIndent(2);
|
|
|
|
|
if (canStartWithNewLine)
|
|
|
|
|
{
|
|
|
|
|
PopIndent();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void StringifyObject(const JSValue val, bool canStartWithNewLine, int32_t nestLevel)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSPropertyEnum* props;
|
|
|
|
|
uint32_t propsLen;
|
|
|
|
|
JS_GetOwnPropertyNames(_context, &props, &propsLen, val, JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK | JS_GPN_PRIVATE_MASK);
|
|
|
|
|
|
|
|
|
|
if (propsLen == 0)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
|
|
|
|
_ss << "{}";
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else if (propsLen == 1)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
|
|
|
|
_ss << "{ ";
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
const char* key = JS_AtomToCString(_context, props[0].atom);
|
|
|
|
|
if (key)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_ss << key << ": ";
|
|
|
|
|
JS_FreeCString(_context, key);
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// For some reason the key was not a string
|
|
|
|
|
_ss << "?: ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSValue prop = JS_GetProperty(_context, val, props[0].atom);
|
|
|
|
|
Stringify(prop, true, nestLevel + 1);
|
|
|
|
|
JS_FreeValue(_context, prop);
|
2020-02-25 00:43:49 +00:00
|
|
|
|
|
|
|
|
_ss << " }";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (canStartWithNewLine)
|
|
|
|
|
{
|
|
|
|
|
PushIndent();
|
|
|
|
|
LineFeed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ss << "{ ";
|
|
|
|
|
PushIndent(2);
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
for (uint32_t i = 0; i < propsLen; i++)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (i != 0)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
|
|
|
|
_ss << ",";
|
|
|
|
|
LineFeed();
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
const char* key = JS_AtomToCString(_context, props[i].atom);
|
|
|
|
|
if (key)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_ss << key << ": ";
|
|
|
|
|
JS_FreeCString(_context, key);
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// For some reason the key was not a string
|
|
|
|
|
_ss << "?: ";
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
|
|
|
|
|
JSValue prop = JS_GetProperty(_context, val, props[i].atom);
|
|
|
|
|
Stringify(prop, true, nestLevel + 1);
|
|
|
|
|
JS_FreeValue(_context, prop);
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PopIndent(2);
|
|
|
|
|
_ss << " }";
|
|
|
|
|
|
|
|
|
|
if (canStartWithNewLine)
|
|
|
|
|
{
|
|
|
|
|
PopIndent();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
|
|
|
|
|
JS_FreePropertyEnum(_context, props, propsLen);
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void StringifyFunction(const JSValue val)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsConstructor(_context, val))
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_ss << "[Constructor]";
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ss << "[Function]";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void StringifyString(const JSValue val)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
size_t len;
|
|
|
|
|
const char* str = JS_ToCStringLen(_context, &len, val);
|
|
|
|
|
if (str)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_ss << "'" << std::string_view(str, len) << "'";
|
|
|
|
|
JS_FreeCString(_context, str);
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_ss << "[error printing string]";
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void StringifyNumber(const JSValue val)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_VALUE_GET_TAG(val) == JS_TAG_INT)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_ss << std::to_string(JS_VALUE_GET_INT(val));
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else if (JS_TAG_IS_FLOAT64(JS_VALUE_GET_TAG(val)))
|
|
|
|
|
{
|
|
|
|
|
const double d = JS_VALUE_GET_FLOAT64(val);
|
|
|
|
|
const int64_t i = static_cast<int64_t>(d);
|
|
|
|
|
if (AlmostEqual<double>(d, i))
|
|
|
|
|
{
|
|
|
|
|
_ss << std::to_string(i);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ss << std::to_string(d);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (JS_VALUE_GET_TAG(val) == JS_TAG_SHORT_BIG_INT)
|
|
|
|
|
{
|
|
|
|
|
_ss << std::to_string(JS_VALUE_GET_SHORT_BIG_INT(val));
|
|
|
|
|
}
|
|
|
|
|
else if (JS_VALUE_GET_TAG(val) == JS_TAG_BIG_INT)
|
|
|
|
|
{
|
|
|
|
|
const char* str = JS_ToCString(_context, val);
|
|
|
|
|
if (str)
|
|
|
|
|
{
|
|
|
|
|
_ss << str;
|
|
|
|
|
JS_FreeCString(_context, str);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ss << "[BitInt error]";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringifyError(const JSValue val)
|
|
|
|
|
{
|
|
|
|
|
const char* str = JS_ToCString(_context, val);
|
|
|
|
|
if (str)
|
|
|
|
|
{
|
|
|
|
|
_ss << str;
|
|
|
|
|
JS_FreeCString(_context, str);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_ss << "[error]";
|
|
|
|
|
}
|
|
|
|
|
JSValue stackVal = JS_GetPropertyStr(_context, val, "stack");
|
|
|
|
|
if (!JS_IsUndefined(stackVal))
|
|
|
|
|
{
|
|
|
|
|
const char* stackStr = JS_ToCString(_context, stackVal);
|
|
|
|
|
if (stackStr)
|
|
|
|
|
{
|
|
|
|
|
LineFeed();
|
|
|
|
|
std::string_view view(stackStr);
|
|
|
|
|
if (view.ends_with('\n'))
|
|
|
|
|
view = view.substr(0, view.length() - 1);
|
|
|
|
|
_ss << view;
|
|
|
|
|
JS_FreeCString(_context, stackStr);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LineFeed();
|
|
|
|
|
_ss << "[no stack trace]";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
JS_FreeValue(_context, stackVal);
|
2020-02-25 00:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Taken from http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon
|
|
|
|
|
template<class T>
|
2026-01-31 19:59:59 -03:00
|
|
|
static std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type AlmostEqual(T x, T y, int32_t ulp = 20)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
|
|
|
|
// the machine epsilon has to be scaled to the magnitude of the values used
|
|
|
|
|
// and multiplied by the desired precision in ULPs (units in the last place)
|
|
|
|
|
return std::abs(x - y) <= std::numeric_limits<T>::epsilon() * std::abs(x + y) * ulp
|
|
|
|
|
// unless the result is subnormal
|
|
|
|
|
|| std::abs(x - y)
|
|
|
|
|
< (std::numeric_limits<T>::min)(); // TODO: Remove parentheses around min once the macro is removed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
2025-09-29 02:05:00 +10:00
|
|
|
static std::string StringifyExpression(JSContext* ctx, const JSValue val)
|
2020-02-25 00:43:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
ExpressionStringifier instance(ctx);
|
2021-02-04 00:25:34 +00:00
|
|
|
instance.Stringify(val, false, 0);
|
2020-02-25 00:43:49 +00:00
|
|
|
return instance._ss.str();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 17:59:48 +02:00
|
|
|
ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env)
|
|
|
|
|
: _console(console)
|
|
|
|
|
, _env(env)
|
2020-03-07 22:40:45 +00:00
|
|
|
, _hookEngine(*this)
|
2018-03-17 19:31:06 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-17 23:26:55 +00:00
|
|
|
void ScriptEngine::Initialise()
|
|
|
|
|
{
|
2022-02-27 15:48:43 +00:00
|
|
|
if (_initialised)
|
|
|
|
|
throw std::runtime_error("Script engine already initialised.");
|
2025-09-29 02:05:00 +10:00
|
|
|
if (!_runtime)
|
|
|
|
|
{
|
|
|
|
|
_runtime = JS_NewRuntime();
|
|
|
|
|
if (!_runtime)
|
|
|
|
|
throw std::runtime_error("QuickJS: cannot allocate JS runtime\n");
|
2022-02-27 15:48:43 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
#ifndef NDEBUG
|
|
|
|
|
// Dump JS engine memory leaks
|
|
|
|
|
JS_SetDumpFlags(_runtime, JS_DUMP_LEAKS);
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2025-09-29 02:05:00 +10:00
|
|
|
}
|
2018-03-17 23:26:55 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
// Disable maximum stack size limit for the JS engine.
|
|
|
|
|
JS_SetMaxStackSize(_runtime, 0);
|
2018-03-18 16:27:48 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
_replContext = JS_NewContext(_runtime);
|
|
|
|
|
if (!_replContext)
|
|
|
|
|
throw std::runtime_error("QuickJS: cannot allocate REPL JS context\n");
|
|
|
|
|
RegisterClasses(_replContext);
|
|
|
|
|
InitialiseContext(_replContext);
|
2022-05-10 21:54:28 +01:00
|
|
|
|
2020-02-06 23:15:20 +00:00
|
|
|
_initialised = true;
|
2021-03-02 22:55:41 +00:00
|
|
|
_transientPluginsEnabled = false;
|
|
|
|
|
_transientPluginsStarted = false;
|
2020-03-06 20:17:15 +00:00
|
|
|
|
2021-03-02 22:55:41 +00:00
|
|
|
LoadSharedStorage();
|
2022-02-19 16:32:08 +00:00
|
|
|
ClearParkStorage();
|
2018-03-18 16:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSRuntime* ScriptEngine::_runtime = nullptr;
|
|
|
|
|
ScAward Scripting::gScAward;
|
|
|
|
|
ScCheats Scripting::gScCheats;
|
|
|
|
|
ScWeather Scripting::gScWeather;
|
|
|
|
|
ScWeatherState Scripting::gScWeatherState;
|
|
|
|
|
ScConfiguration Scripting::gScConfiguration;
|
|
|
|
|
ScConsole Scripting::gScConsole;
|
|
|
|
|
ScContext Scripting::gScContext;
|
|
|
|
|
ScDate Scripting::gScDate;
|
|
|
|
|
ScDisposable Scripting::gScDisposable;
|
|
|
|
|
ScMap Scripting::gScMap;
|
|
|
|
|
ScNetwork Scripting::gScNetwork;
|
|
|
|
|
ScObjectManager Scripting::gScObjectManager;
|
|
|
|
|
ScInstalledObject Scripting::gScInstalledObject;
|
|
|
|
|
ScLargeSceneryObjectTile Scripting::gScLargeSceneryObjectTile;
|
|
|
|
|
ScObject Scripting::gScObject;
|
|
|
|
|
ScPark Scripting::gScPark;
|
|
|
|
|
ScParkMessage Scripting::gScParkMessage;
|
|
|
|
|
ScPlayer Scripting::gScPlayer;
|
|
|
|
|
ScPlayerGroup Scripting::gScPlayerGroup;
|
|
|
|
|
ScProfiler Scripting::gScProfiler;
|
|
|
|
|
ScResearch Scripting::gScResearch;
|
|
|
|
|
ScRide Scripting::gScRide;
|
|
|
|
|
ScRideStation Scripting::gScRideStation;
|
|
|
|
|
ScRideObjectVehicle Scripting::gScRideObjectVehicle;
|
|
|
|
|
ScTile Scripting::gScTile;
|
|
|
|
|
ScTileElement Scripting::gScTileElement;
|
|
|
|
|
ScTrackIterator Scripting::gScTrackIterator;
|
|
|
|
|
ScTrackSegment Scripting::gScTrackSegment;
|
|
|
|
|
ScEntity Scripting::gScEntity;
|
|
|
|
|
ScThought Scripting::gScThought;
|
|
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
ScSocket Scripting::gScSocket;
|
|
|
|
|
ScListener Scripting::gScListener;
|
|
|
|
|
#endif
|
|
|
|
|
ScScenario Scripting::gScScenario;
|
|
|
|
|
ScScenarioObjective Scripting::gScScenarioObjective;
|
|
|
|
|
ScPatrolArea Scripting::gScPatrolArea;
|
|
|
|
|
ScPlugin Scripting::gScPlugin;
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::RegisterClasses(JSContext* ctx)
|
|
|
|
|
{
|
|
|
|
|
gScAward.Register(ctx);
|
|
|
|
|
gScCheats.Register(ctx);
|
|
|
|
|
gScWeather.Register(ctx);
|
|
|
|
|
gScWeatherState.Register(ctx);
|
|
|
|
|
gScConfiguration.Register(ctx);
|
|
|
|
|
gScConsole.Register(ctx);
|
|
|
|
|
gScContext.Register(ctx);
|
|
|
|
|
gScDate.Register(ctx);
|
|
|
|
|
gScDisposable.Register(ctx);
|
|
|
|
|
gScMap.Register(ctx);
|
|
|
|
|
gScNetwork.Register(ctx);
|
|
|
|
|
gScObjectManager.Register(ctx);
|
|
|
|
|
gScInstalledObject.Register(ctx);
|
|
|
|
|
gScObject.Register(ctx);
|
|
|
|
|
gScLargeSceneryObjectTile.Register(ctx);
|
|
|
|
|
gScPark.Register(ctx);
|
|
|
|
|
gScParkMessage.Register(ctx);
|
|
|
|
|
gScPlayer.Register(ctx);
|
|
|
|
|
gScPlayerGroup.Register(ctx);
|
|
|
|
|
gScProfiler.Register(ctx);
|
|
|
|
|
gScResearch.Register(ctx);
|
|
|
|
|
gScRide.Register(ctx);
|
|
|
|
|
gScRideStation.Register(ctx);
|
|
|
|
|
gScRideObjectVehicle.Register(ctx);
|
|
|
|
|
gScTile.Register(ctx);
|
|
|
|
|
gScTileElement.Register(ctx);
|
|
|
|
|
gScTrackIterator.Register(ctx);
|
|
|
|
|
gScTrackSegment.Register(ctx);
|
|
|
|
|
gScEntity.Register(ctx);
|
|
|
|
|
gScThought.Register(ctx);
|
|
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
gScSocket.Register(ctx);
|
|
|
|
|
gScListener.Register(ctx);
|
|
|
|
|
#endif
|
|
|
|
|
gScScenario.Register(ctx);
|
|
|
|
|
gScScenarioObjective.Register(ctx);
|
|
|
|
|
gScPatrolArea.Register(ctx);
|
|
|
|
|
gScPlugin.Register(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::UnregisterClasses()
|
|
|
|
|
{
|
|
|
|
|
gScAward.Unregister();
|
|
|
|
|
gScCheats.Unregister();
|
|
|
|
|
gScWeather.Unregister();
|
|
|
|
|
gScWeatherState.Unregister();
|
|
|
|
|
gScConfiguration.Unregister();
|
|
|
|
|
gScConsole.Unregister();
|
|
|
|
|
gScContext.Unregister();
|
|
|
|
|
gScDate.Unregister();
|
|
|
|
|
gScDisposable.Unregister();
|
|
|
|
|
gScMap.Unregister();
|
|
|
|
|
gScNetwork.Unregister();
|
|
|
|
|
gScObjectManager.Unregister();
|
|
|
|
|
gScInstalledObject.Unregister();
|
|
|
|
|
gScObject.Unregister();
|
|
|
|
|
gScLargeSceneryObjectTile.Unregister();
|
|
|
|
|
gScPark.Unregister();
|
|
|
|
|
gScParkMessage.Unregister();
|
|
|
|
|
gScPlayer.Unregister();
|
|
|
|
|
gScPlayerGroup.Unregister();
|
|
|
|
|
gScProfiler.Unregister();
|
|
|
|
|
gScResearch.Unregister();
|
|
|
|
|
gScRide.Unregister();
|
|
|
|
|
gScRideStation.Unregister();
|
|
|
|
|
gScRideObjectVehicle.Unregister();
|
|
|
|
|
gScTile.Unregister();
|
|
|
|
|
gScTileElement.Unregister();
|
|
|
|
|
gScTrackIterator.Unregister();
|
|
|
|
|
gScTrackSegment.Unregister();
|
|
|
|
|
gScEntity.Unregister();
|
|
|
|
|
gScThought.Unregister();
|
|
|
|
|
#ifndef DISABLE_NETWORK
|
|
|
|
|
gScSocket.Unregister();
|
|
|
|
|
gScListener.Unregister();
|
|
|
|
|
#endif
|
|
|
|
|
gScScenario.Unregister();
|
|
|
|
|
gScScenarioObjective.Unregister();
|
|
|
|
|
gScPatrolArea.Unregister();
|
|
|
|
|
gScPlugin.Unregister();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSContext* ScriptEngine::CreateContext() const
|
|
|
|
|
{
|
|
|
|
|
JSContext* newCtx = JS_NewContext(_runtime);
|
|
|
|
|
if (!newCtx)
|
|
|
|
|
{
|
|
|
|
|
throw std::runtime_error("QuickJS: cannot allocate JS context\n");
|
|
|
|
|
}
|
|
|
|
|
InitialiseContext(newCtx);
|
|
|
|
|
|
|
|
|
|
for (const auto& ext : _extensions)
|
|
|
|
|
{
|
|
|
|
|
ext.newContext(newCtx);
|
|
|
|
|
}
|
|
|
|
|
return newCtx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::InitialiseContext(JSContext* ctx) const
|
|
|
|
|
{
|
|
|
|
|
JSValue glb = JS_GetGlobalObject(ctx);
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "cheats", gScCheats.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "climate", gScWeather.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "console", gScConsole.New(ctx, _console));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "context", gScContext.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "date", gScDate.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "map", gScMap.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "network", gScNetwork.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "park", gScPark.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "pluginManager", gScPlugin.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "profiler", gScProfiler.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "scenario", gScScenario.New(ctx));
|
|
|
|
|
JS_SetPropertyStr(ctx, glb, "objectManager", gScObjectManager.New(ctx));
|
|
|
|
|
JS_FreeValue(ctx, glb);
|
|
|
|
|
|
|
|
|
|
RegisterConstants(ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScriptEngine::~ScriptEngine()
|
|
|
|
|
{
|
|
|
|
|
if (_replContext)
|
|
|
|
|
{
|
|
|
|
|
JS_FreeValue(_replContext, _sharedStorage);
|
|
|
|
|
JS_FreeValue(_replContext, _parkStorage);
|
|
|
|
|
JS_FreeContext(_replContext);
|
|
|
|
|
_replContext = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (_runtime)
|
|
|
|
|
{
|
|
|
|
|
JS_FreeRuntime(_runtime);
|
|
|
|
|
_runtime = nullptr;
|
|
|
|
|
}
|
|
|
|
|
for (const ExtensionCallbacks& ext : _extensions)
|
|
|
|
|
{
|
|
|
|
|
ext.unregister();
|
|
|
|
|
}
|
|
|
|
|
UnregisterClasses();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 21:54:28 +01:00
|
|
|
class ConstantBuilder
|
|
|
|
|
{
|
|
|
|
|
private:
|
2025-09-29 02:05:00 +10:00
|
|
|
JSContext* _ctx;
|
|
|
|
|
JSValue _obj;
|
2022-05-10 21:54:28 +01:00
|
|
|
|
|
|
|
|
public:
|
2025-09-29 02:05:00 +10:00
|
|
|
ConstantBuilder(JSContext* ctx)
|
2022-05-10 21:54:28 +01:00
|
|
|
: _ctx(ctx)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_obj = JS_GetGlobalObject(_ctx);
|
2022-05-10 21:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConstantBuilder& Namespace(std::string_view ns)
|
|
|
|
|
{
|
|
|
|
|
// Create a new object for namespace
|
|
|
|
|
// Keep a reference to the namespace object
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_FreeValue(_ctx, _obj);
|
|
|
|
|
_obj = JS_NewObject(_ctx);
|
2022-05-10 21:54:28 +01:00
|
|
|
|
|
|
|
|
// Place the namespace object into the global context
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue global = JS_GetGlobalObject(_ctx);
|
|
|
|
|
JS_SetPropertyStr(_ctx, global, std::string(ns).c_str(), JS_DupValue(_ctx, _obj));
|
|
|
|
|
JS_FreeValue(_ctx, global);
|
2022-05-10 21:54:28 +01:00
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConstantBuilder& Constant(std::string_view name, int32_t value)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(_ctx, _obj, std::string(name).c_str(), JS_NewInt32(_ctx, value));
|
2022-05-10 21:54:28 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
|
|
|
|
|
~ConstantBuilder()
|
|
|
|
|
{
|
|
|
|
|
JS_FreeValue(_ctx, _obj);
|
|
|
|
|
}
|
2022-05-10 21:54:28 +01:00
|
|
|
};
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void ScriptEngine::RegisterConstants(JSContext* ctx)
|
2022-05-10 21:54:28 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
ConstantBuilder builder(ctx);
|
2022-05-10 21:54:28 +01:00
|
|
|
builder.Namespace("TrackSlope")
|
2025-12-23 19:53:25 +01:00
|
|
|
.Constant("None", EnumValue(TrackPitch::none))
|
|
|
|
|
.Constant("Up25", EnumValue(TrackPitch::up25))
|
|
|
|
|
.Constant("Up60", EnumValue(TrackPitch::up60))
|
|
|
|
|
.Constant("Down25", EnumValue(TrackPitch::down25))
|
|
|
|
|
.Constant("Down60", EnumValue(TrackPitch::down60))
|
|
|
|
|
.Constant("Up90", EnumValue(TrackPitch::up90))
|
|
|
|
|
.Constant("Down90", EnumValue(TrackPitch::down90));
|
2022-05-10 21:54:28 +01:00
|
|
|
builder.Namespace("TrackBanking")
|
2025-12-23 19:52:05 +01:00
|
|
|
.Constant("None", EnumValue(TrackRoll::none))
|
|
|
|
|
.Constant("BankLeft", EnumValue(TrackRoll::left))
|
|
|
|
|
.Constant("BankRight", EnumValue(TrackRoll::right))
|
|
|
|
|
.Constant("UpsideDown", EnumValue(TrackRoll::upsideDown));
|
2022-05-10 21:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 00:17:13 +00:00
|
|
|
void ScriptEngine::RefreshPlugins()
|
|
|
|
|
{
|
|
|
|
|
// Get a list of removed and added plugin files
|
|
|
|
|
auto pluginFiles = GetPluginFiles();
|
|
|
|
|
std::vector<std::string> plugins;
|
|
|
|
|
std::vector<std::string> removedPlugins;
|
|
|
|
|
std::vector<std::string> addedPlugins;
|
|
|
|
|
for (const auto& plugin : _plugins)
|
|
|
|
|
{
|
2022-03-21 22:56:21 +00:00
|
|
|
if (plugin->HasPath())
|
|
|
|
|
{
|
2022-04-25 16:32:04 +03:00
|
|
|
plugins.emplace_back(plugin->GetPath());
|
2022-03-21 22:56:21 +00:00
|
|
|
}
|
2021-03-02 00:17:13 +00:00
|
|
|
}
|
2022-04-02 23:03:07 +02:00
|
|
|
|
|
|
|
|
// The lists need to be sorted for std::set_difference to work properly
|
|
|
|
|
std::sort(pluginFiles.begin(), pluginFiles.end());
|
|
|
|
|
std::sort(plugins.begin(), plugins.end());
|
|
|
|
|
|
2021-03-02 00:17:13 +00:00
|
|
|
std::set_difference(
|
|
|
|
|
plugins.begin(), plugins.end(), pluginFiles.begin(), pluginFiles.end(), std::back_inserter(removedPlugins));
|
|
|
|
|
std::set_difference(
|
|
|
|
|
pluginFiles.begin(), pluginFiles.end(), plugins.begin(), plugins.end(), std::back_inserter(addedPlugins));
|
|
|
|
|
|
|
|
|
|
// Unregister plugin files that were removed
|
|
|
|
|
for (const auto& plugin : removedPlugins)
|
|
|
|
|
{
|
|
|
|
|
UnregisterPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register plugin files that were added
|
|
|
|
|
for (const auto& plugin : addedPlugins)
|
|
|
|
|
{
|
|
|
|
|
RegisterPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Turn on hot reload if not already enabled
|
2025-10-07 20:14:37 +02:00
|
|
|
if (!_hotReloadingInitialised && Config::Get().plugin.enableHotReloading && Network::GetMode() == Network::Mode::none)
|
2021-03-02 00:17:13 +00:00
|
|
|
{
|
|
|
|
|
SetupHotReloading();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> ScriptEngine::GetPluginFiles() const
|
|
|
|
|
{
|
|
|
|
|
// Scan for .js files in plugin directory
|
|
|
|
|
std::vector<std::string> pluginFiles;
|
2025-03-25 23:39:27 +01:00
|
|
|
auto base = _env.GetDirectoryPath(DirBase::user, DirId::plugins);
|
2021-03-02 00:17:13 +00:00
|
|
|
if (Path::DirectoryExists(base))
|
|
|
|
|
{
|
2021-03-02 22:55:41 +00:00
|
|
|
auto pattern = Path::Combine(base, u8"*.js");
|
|
|
|
|
auto scanner = Path::ScanDirectory(pattern, true);
|
2021-03-02 00:17:13 +00:00
|
|
|
while (scanner->Next())
|
|
|
|
|
{
|
|
|
|
|
auto path = std::string(scanner->GetPath());
|
|
|
|
|
if (ShouldLoadScript(path))
|
|
|
|
|
{
|
|
|
|
|
pluginFiles.push_back(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return pluginFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ScriptEngine::ShouldLoadScript(std::string_view path)
|
|
|
|
|
{
|
|
|
|
|
// A lot of JavaScript is often found in a node_modules directory tree and is most likely unwanted, so ignore it
|
|
|
|
|
return path.find("/node_modules/") == std::string_view::npos && path.find("\\node_modules\\") == std::string_view::npos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::UnregisterPlugin(std::string_view path)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
auto pluginIt = std::find_if(_plugins.begin(), _plugins.end(), [path](const std::shared_ptr<Plugin>& plugin) {
|
|
|
|
|
return plugin->GetPath() == path;
|
|
|
|
|
});
|
|
|
|
|
auto& plugin = *pluginIt;
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
StopPlugin(plugin, true);
|
2021-03-02 00:17:13 +00:00
|
|
|
UnloadPlugin(plugin);
|
|
|
|
|
LogPluginInfo(plugin, "Unregistered");
|
|
|
|
|
|
|
|
|
|
_plugins.erase(pluginIt);
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
_console.WriteLineError(e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::RegisterPlugin(std::string_view path)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto plugin = std::make_shared<Plugin>(path);
|
2021-03-02 00:17:13 +00:00
|
|
|
|
|
|
|
|
// We must load the plugin to get the metadata for it
|
|
|
|
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin, false);
|
|
|
|
|
plugin->Load();
|
|
|
|
|
|
|
|
|
|
// Unload the plugin now, metadata is kept
|
|
|
|
|
plugin->Unload();
|
|
|
|
|
|
|
|
|
|
LogPluginInfo(plugin, "Registered");
|
|
|
|
|
_plugins.push_back(std::move(plugin));
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
_console.WriteLineError(e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::StartIntransientPlugins()
|
|
|
|
|
{
|
2021-03-02 22:55:41 +00:00
|
|
|
LoadSharedStorage();
|
|
|
|
|
|
2021-03-02 00:17:13 +00:00
|
|
|
for (auto& plugin : _plugins)
|
|
|
|
|
{
|
|
|
|
|
if (!plugin->HasStarted() && !plugin->IsTransient())
|
|
|
|
|
{
|
|
|
|
|
LoadPlugin(plugin);
|
|
|
|
|
StartPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-27 15:48:43 +00:00
|
|
|
|
|
|
|
|
_intransientPluginsStarted = true;
|
2021-03-02 00:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::StopUnloadRegisterAllPlugins()
|
|
|
|
|
{
|
|
|
|
|
std::vector<std::string> pluginPaths;
|
|
|
|
|
for (auto& plugin : _plugins)
|
|
|
|
|
{
|
2022-04-25 16:32:04 +03:00
|
|
|
pluginPaths.emplace_back(plugin->GetPath());
|
2021-03-02 00:17:13 +00:00
|
|
|
StopPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
for (auto& plugin : _plugins)
|
|
|
|
|
{
|
|
|
|
|
UnloadPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
for (auto& pluginPath : pluginPaths)
|
|
|
|
|
{
|
|
|
|
|
UnregisterPlugin(pluginPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 22:55:41 +00:00
|
|
|
void ScriptEngine::LoadTransientPlugins()
|
2018-03-18 16:27:48 +00:00
|
|
|
{
|
2022-02-23 02:02:07 +00:00
|
|
|
if (!_initialised)
|
|
|
|
|
{
|
|
|
|
|
Initialise();
|
|
|
|
|
RefreshPlugins();
|
|
|
|
|
}
|
2021-03-02 22:55:41 +00:00
|
|
|
_transientPluginsEnabled = true;
|
2020-02-10 18:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::LoadPlugin(const std::string& path)
|
2020-02-22 22:18:22 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto plugin = std::make_shared<Plugin>(path);
|
2020-02-22 22:18:22 +00:00
|
|
|
LoadPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::LoadPlugin(std::shared_ptr<Plugin>& plugin)
|
2020-02-10 18:44:42 +00:00
|
|
|
{
|
2018-05-23 17:56:42 +01:00
|
|
|
try
|
|
|
|
|
{
|
2021-03-02 00:17:13 +00:00
|
|
|
if (!plugin->IsLoaded())
|
2020-02-10 18:44:42 +00:00
|
|
|
{
|
2021-03-02 00:17:13 +00:00
|
|
|
const auto& metadata = plugin->GetMetadata();
|
2025-01-19 16:59:45 +01:00
|
|
|
if (metadata.MinApiVersion <= kPluginApiVersion)
|
2021-03-02 00:17:13 +00:00
|
|
|
{
|
|
|
|
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin, false);
|
|
|
|
|
plugin->Load();
|
|
|
|
|
LogPluginInfo(plugin, "Loaded");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LogPluginInfo(plugin, "Requires newer API version: v" + std::to_string(metadata.MinApiVersion));
|
|
|
|
|
}
|
2020-02-10 18:44:42 +00:00
|
|
|
}
|
2018-05-23 17:56:42 +01:00
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
2020-02-10 18:44:42 +00:00
|
|
|
_console.WriteLineError(e.what());
|
2018-05-23 17:56:42 +01:00
|
|
|
}
|
2018-03-18 16:27:48 +00:00
|
|
|
}
|
|
|
|
|
|
2021-03-02 00:17:13 +00:00
|
|
|
void ScriptEngine::UnloadPlugin(std::shared_ptr<Plugin>& plugin)
|
|
|
|
|
{
|
2022-02-20 00:23:15 +00:00
|
|
|
if (plugin->IsLoaded())
|
|
|
|
|
{
|
|
|
|
|
plugin->Unload();
|
|
|
|
|
LogPluginInfo(plugin, "Unloaded");
|
2025-09-29 02:05:00 +10:00
|
|
|
// A GC run is required to clean up all JS objects before the game global context is cleaned up on shut down.
|
|
|
|
|
JS_RunGC(_runtime);
|
2022-02-20 00:23:15 +00:00
|
|
|
}
|
2021-03-02 00:17:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::StartPlugin(std::shared_ptr<Plugin> plugin)
|
|
|
|
|
{
|
|
|
|
|
if (!plugin->HasStarted() && ShouldStartPlugin(plugin))
|
|
|
|
|
{
|
|
|
|
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin, false);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LogPluginInfo(plugin, "Started");
|
|
|
|
|
plugin->Start();
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
_console.WriteLineError(e.what());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void ScriptEngine::StopPlugin(std::shared_ptr<Plugin> plugin, bool unregistering)
|
2020-02-11 21:26:05 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
// This is hacky but on the title screen the plugin can register things without having
|
|
|
|
|
// been started. Therefore when unregistering we must make sure to clean those things up
|
|
|
|
|
if (plugin->HasStarted() || unregistering)
|
2020-02-11 21:26:05 +00:00
|
|
|
{
|
2022-02-15 20:26:37 +00:00
|
|
|
plugin->StopBegin();
|
|
|
|
|
|
2021-01-12 06:14:15 +09:00
|
|
|
for (const auto& callback : _pluginStoppedSubscriptions)
|
2020-02-11 21:26:05 +00:00
|
|
|
{
|
|
|
|
|
callback(plugin);
|
|
|
|
|
}
|
2022-02-15 20:26:37 +00:00
|
|
|
RemoveCustomGameActions(plugin);
|
|
|
|
|
RemoveIntervals(plugin);
|
|
|
|
|
RemoveSockets(plugin);
|
|
|
|
|
_hookEngine.UnsubscribeAll(plugin);
|
2020-02-11 21:26:05 +00:00
|
|
|
|
2022-02-15 20:26:37 +00:00
|
|
|
plugin->StopEnd();
|
2025-09-29 02:05:00 +10:00
|
|
|
if (!unregistering)
|
|
|
|
|
LogPluginInfo(plugin, "Stopped");
|
2020-02-11 21:26:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-20 00:23:15 +00:00
|
|
|
void ScriptEngine::ReloadPlugin(std::shared_ptr<Plugin> plugin)
|
|
|
|
|
{
|
|
|
|
|
StopPlugin(plugin);
|
|
|
|
|
{
|
|
|
|
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin, false);
|
|
|
|
|
plugin->Load();
|
|
|
|
|
LogPluginInfo(plugin, "Reloaded");
|
|
|
|
|
}
|
|
|
|
|
StartPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 18:44:42 +00:00
|
|
|
void ScriptEngine::SetupHotReloading()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-03-25 23:39:27 +01:00
|
|
|
auto base = _env.GetDirectoryPath(DirBase::user, DirId::plugins);
|
2021-03-02 00:17:13 +00:00
|
|
|
if (Path::DirectoryExists(base))
|
|
|
|
|
{
|
|
|
|
|
_pluginFileWatcher = std::make_unique<FileWatcher>(base);
|
2022-10-24 02:35:00 +02:00
|
|
|
_pluginFileWatcher->OnFileChanged = [this](u8string_view path) {
|
|
|
|
|
std::lock_guard guard(_changedPluginFilesMutex);
|
2021-03-02 00:17:13 +00:00
|
|
|
_changedPluginFiles.emplace(path);
|
|
|
|
|
};
|
2021-03-02 22:55:41 +00:00
|
|
|
_hotReloadingInitialised = true;
|
2021-03-02 00:17:13 +00:00
|
|
|
}
|
2020-02-10 18:44:42 +00:00
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
2021-02-13 00:15:27 +00:00
|
|
|
Console::Error::WriteLine("Unable to enable hot reloading of plugins: %s", e.what());
|
2020-02-10 18:44:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 22:55:41 +00:00
|
|
|
void ScriptEngine::DoAutoReloadPluginCheck()
|
|
|
|
|
{
|
|
|
|
|
if (_hotReloadingInitialised)
|
|
|
|
|
{
|
|
|
|
|
auto tick = Platform::GetTicks();
|
|
|
|
|
if (tick - _lastHotReloadCheckTick > 1000)
|
|
|
|
|
{
|
|
|
|
|
AutoReloadPlugins();
|
|
|
|
|
_lastHotReloadCheckTick = tick;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-18 17:43:47 +00:00
|
|
|
void ScriptEngine::AutoReloadPlugins()
|
|
|
|
|
{
|
2022-10-24 02:35:00 +02:00
|
|
|
if (!_changedPluginFiles.empty())
|
2018-03-18 17:43:47 +00:00
|
|
|
{
|
2022-10-24 02:35:00 +02:00
|
|
|
std::lock_guard guard(_changedPluginFilesMutex);
|
|
|
|
|
for (const auto& path : _changedPluginFiles)
|
2018-03-18 17:43:47 +00:00
|
|
|
{
|
2018-07-31 17:59:48 +02:00
|
|
|
auto findResult = std::find_if(_plugins.begin(), _plugins.end(), [&path](const std::shared_ptr<Plugin>& plugin) {
|
|
|
|
|
return Path::Equals(path, plugin->GetPath());
|
|
|
|
|
});
|
2018-03-31 00:08:59 +01:00
|
|
|
if (findResult != _plugins.end())
|
2018-03-18 17:43:47 +00:00
|
|
|
{
|
2018-03-31 00:08:59 +01:00
|
|
|
auto& plugin = *findResult;
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-02-20 00:23:15 +00:00
|
|
|
ReloadPlugin(plugin);
|
2018-03-31 00:08:59 +01:00
|
|
|
}
|
2018-07-31 17:59:48 +02:00
|
|
|
catch (const std::exception& e)
|
2018-03-31 00:08:59 +01:00
|
|
|
{
|
|
|
|
|
_console.WriteLineError(e.what());
|
|
|
|
|
}
|
2018-03-18 17:43:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-03-31 00:08:59 +01:00
|
|
|
_changedPluginFiles.clear();
|
2018-03-18 17:43:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 22:55:41 +00:00
|
|
|
void ScriptEngine::UnloadTransientPlugins()
|
2020-02-06 23:15:20 +00:00
|
|
|
{
|
2021-03-02 22:55:41 +00:00
|
|
|
// Stop them all first
|
2020-02-06 23:15:20 +00:00
|
|
|
for (auto& plugin : _plugins)
|
|
|
|
|
{
|
2021-03-02 22:55:41 +00:00
|
|
|
if (plugin->IsTransient())
|
2018-03-18 23:35:58 +00:00
|
|
|
{
|
2022-02-20 00:23:15 +00:00
|
|
|
StopPlugin(plugin);
|
2018-03-18 23:35:58 +00:00
|
|
|
}
|
2018-03-18 16:27:48 +00:00
|
|
|
}
|
2021-03-02 22:55:41 +00:00
|
|
|
|
|
|
|
|
// Now unload them
|
|
|
|
|
for (auto& plugin : _plugins)
|
|
|
|
|
{
|
2022-02-20 00:23:15 +00:00
|
|
|
if (plugin->IsTransient())
|
|
|
|
|
{
|
|
|
|
|
UnloadPlugin(plugin);
|
|
|
|
|
}
|
2021-03-02 22:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_transientPluginsEnabled = false;
|
|
|
|
|
_transientPluginsStarted = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::StartTransientPlugins()
|
|
|
|
|
{
|
|
|
|
|
LoadSharedStorage();
|
|
|
|
|
|
|
|
|
|
// Load transient plugins
|
|
|
|
|
for (auto& plugin : _plugins)
|
|
|
|
|
{
|
|
|
|
|
if (plugin->IsTransient() && !plugin->IsLoaded() && ShouldStartPlugin(plugin))
|
|
|
|
|
{
|
|
|
|
|
LoadPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start transient plugins
|
|
|
|
|
for (auto& plugin : _plugins)
|
|
|
|
|
{
|
|
|
|
|
if (plugin->IsTransient() && plugin->IsLoaded() && !plugin->HasStarted())
|
|
|
|
|
{
|
|
|
|
|
StartPlugin(plugin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_transientPluginsStarted = true;
|
2020-02-06 23:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 22:18:22 +00:00
|
|
|
bool ScriptEngine::ShouldStartPlugin(const std::shared_ptr<Plugin>& plugin)
|
|
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
auto networkMode = Network::GetMode();
|
|
|
|
|
if (networkMode == Network::Mode::client)
|
2020-02-22 22:18:22 +00:00
|
|
|
{
|
|
|
|
|
// Only client plugins and plugins downloaded from server should be started
|
|
|
|
|
const auto& metadata = plugin->GetMetadata();
|
2020-02-24 17:40:36 +00:00
|
|
|
if (metadata.Type == PluginType::Remote && plugin->HasPath())
|
2020-02-22 22:18:22 +00:00
|
|
|
{
|
2020-02-24 17:40:36 +00:00
|
|
|
LogPluginInfo(plugin, "Remote plugin not started");
|
2020-02-22 22:18:22 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-02 17:35:31 +02:00
|
|
|
void ScriptEngine::Tick()
|
2018-03-17 19:31:06 +00:00
|
|
|
{
|
2024-07-09 20:19:01 +02:00
|
|
|
if (!_initialised)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 10:21:20 -08:00
|
|
|
PROFILED_FUNCTION();
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSContext* jobCtx;
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
if (JS_ExecutePendingJob(_runtime, &jobCtx) == 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 15:48:43 +00:00
|
|
|
CheckAndStartPlugins();
|
2021-01-03 15:08:35 +00:00
|
|
|
UpdateIntervals();
|
2020-08-17 03:53:37 +01:00
|
|
|
UpdateSockets();
|
2020-02-06 23:15:20 +00:00
|
|
|
ProcessREPL();
|
2021-03-02 22:55:41 +00:00
|
|
|
DoAutoReloadPluginCheck();
|
2020-02-06 23:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
2022-02-27 15:48:43 +00:00
|
|
|
void ScriptEngine::CheckAndStartPlugins()
|
|
|
|
|
{
|
|
|
|
|
auto startIntransient = !_intransientPluginsStarted;
|
|
|
|
|
auto startTransient = !_transientPluginsStarted && _transientPluginsEnabled;
|
|
|
|
|
|
|
|
|
|
if (startIntransient || startTransient)
|
|
|
|
|
{
|
|
|
|
|
RefreshPlugins();
|
|
|
|
|
}
|
|
|
|
|
if (startIntransient)
|
|
|
|
|
{
|
|
|
|
|
StartIntransientPlugins();
|
|
|
|
|
}
|
|
|
|
|
if (startTransient)
|
|
|
|
|
{
|
|
|
|
|
StartTransientPlugins();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 23:15:20 +00:00
|
|
|
void ScriptEngine::ProcessREPL()
|
|
|
|
|
{
|
2026-02-05 09:14:12 -03:00
|
|
|
while (!_evalQueue.empty())
|
2018-03-17 19:31:06 +00:00
|
|
|
{
|
|
|
|
|
auto item = std::move(_evalQueue.front());
|
|
|
|
|
_evalQueue.pop();
|
|
|
|
|
auto promise = std::move(std::get<0>(item));
|
|
|
|
|
auto command = std::move(std::get<1>(item));
|
2025-09-29 02:05:00 +10:00
|
|
|
|
|
|
|
|
JSValue res = JS_Eval(_replContext, command.c_str(), command.length(), "<repl>", JS_EVAL_TYPE_GLOBAL);
|
|
|
|
|
if (JS_IsException(res))
|
2018-03-17 19:31:06 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue exceptionVal = JS_GetException(_replContext);
|
|
|
|
|
_console.WriteLineError(Stringify(_replContext, exceptionVal));
|
|
|
|
|
JS_FreeValue(_replContext, exceptionVal);
|
2018-03-17 19:31:06 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
else if (!JS_IsUndefined(res))
|
2018-03-17 19:31:06 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_console.WriteLine(Stringify(_replContext, res));
|
2018-03-17 19:31:06 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_FreeValue(_replContext, res);
|
|
|
|
|
|
2018-03-17 19:31:06 +00:00
|
|
|
// Signal the promise so caller can continue
|
|
|
|
|
promise.set_value();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 17:59:48 +02:00
|
|
|
std::future<void> ScriptEngine::Eval(const std::string& s)
|
2018-03-17 19:31:06 +00:00
|
|
|
{
|
|
|
|
|
std::promise<void> barrier;
|
|
|
|
|
auto future = barrier.get_future();
|
|
|
|
|
_evalQueue.emplace(std::move(barrier), s);
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void ScriptEngine::ExecutePluginCall(
|
|
|
|
|
const std::shared_ptr<Plugin>& plugin, const JSValue func, const std::vector<JSValue>& args, bool isGameStateMutable,
|
|
|
|
|
bool keepArgsAlive)
|
2021-01-31 23:23:22 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
ExecutePluginCall(plugin, func, JS_UNDEFINED, args, isGameStateMutable, keepArgsAlive);
|
2021-01-31 23:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue ScriptEngine::ExecutePluginCall(
|
|
|
|
|
const std::shared_ptr<Plugin>& plugin, const JSValue func, const JSValue thisValue, const std::vector<JSValue>& args,
|
|
|
|
|
bool isGameStateMutable, bool keepArgsAlive, bool keepRetValueAlive)
|
2020-02-29 13:18:31 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue ret = JS_UNDEFINED;
|
|
|
|
|
|
|
|
|
|
// Note: the plugin pointer is null when called from the repl, so we assume the repl JSContext in that case.
|
|
|
|
|
JSContext* ctx = plugin ? plugin->GetContext() : _replContext;
|
|
|
|
|
if (JS_IsFunction(ctx, func) && (!plugin || plugin->HasStarted()))
|
2020-02-29 13:18:31 +00:00
|
|
|
{
|
|
|
|
|
ScriptExecutionInfo::PluginScope scope(_execInfo, plugin, isGameStateMutable);
|
2021-09-15 22:22:15 +02:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
// The call can free itself (by closing windows/clearing timers etc.) so we need to dup the values to keep them alive
|
|
|
|
|
JS_DupValue(ctx, thisValue);
|
|
|
|
|
JS_DupValue(ctx, func);
|
|
|
|
|
ret = JS_Call(ctx, func, thisValue, static_cast<int>(args.size()), const_cast<JSValue*>(args.data()));
|
|
|
|
|
JS_FreeValue(ctx, thisValue);
|
|
|
|
|
JS_FreeValue(ctx, func);
|
|
|
|
|
|
|
|
|
|
if (JS_IsException(ret))
|
|
|
|
|
{
|
|
|
|
|
JSValue exceptionVal = JS_GetException(ctx);
|
|
|
|
|
_console.WriteLineError(Stringify(ctx, exceptionVal));
|
|
|
|
|
JS_FreeValue(ctx, exceptionVal);
|
|
|
|
|
}
|
|
|
|
|
if (!keepRetValueAlive)
|
|
|
|
|
{
|
|
|
|
|
JS_FreeValue(ctx, ret);
|
|
|
|
|
ret = JS_UNDEFINED;
|
|
|
|
|
}
|
2020-02-29 13:18:31 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
[[likely]] if (!keepArgsAlive)
|
|
|
|
|
{
|
|
|
|
|
for (const JSValue& arg : args)
|
|
|
|
|
{
|
|
|
|
|
JS_FreeValue(ctx, arg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
2020-02-29 13:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
2022-05-09 21:13:58 +02:00
|
|
|
void ScriptEngine::LogPluginInfo(std::string_view message)
|
|
|
|
|
{
|
|
|
|
|
auto plugin = _execInfo.GetCurrentPlugin();
|
|
|
|
|
LogPluginInfo(plugin, message);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void ScriptEngine::LogPluginInfo(const std::shared_ptr<Plugin>& plugin, std::string_view message)
|
2020-02-06 23:15:20 +00:00
|
|
|
{
|
2021-01-28 19:55:04 +00:00
|
|
|
if (plugin == nullptr)
|
|
|
|
|
{
|
|
|
|
|
_console.WriteLine(std::string(message));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const auto& pluginName = plugin->GetMetadata().Name;
|
|
|
|
|
_console.WriteLine("[" + pluginName + "] " + std::string(message));
|
|
|
|
|
}
|
2020-02-06 23:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void ScriptEngine::AddNetworkPlugin(std::string_view code)
|
2020-02-22 22:18:22 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto plugin = std::make_shared<Plugin>(std::string());
|
2020-02-22 22:18:22 +00:00
|
|
|
plugin->SetCode(code);
|
2022-03-21 22:56:21 +00:00
|
|
|
_plugins.push_back(plugin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::RemoveNetworkPlugins()
|
|
|
|
|
{
|
|
|
|
|
auto it = _plugins.begin();
|
|
|
|
|
while (it != _plugins.end())
|
|
|
|
|
{
|
2023-01-15 21:52:23 +01:00
|
|
|
auto plugin = (*it);
|
|
|
|
|
if (!plugin->HasPath())
|
2022-03-21 22:56:21 +00:00
|
|
|
{
|
2023-01-15 21:52:23 +01:00
|
|
|
StopPlugin(plugin);
|
|
|
|
|
UnloadPlugin(plugin);
|
|
|
|
|
LogPluginInfo(plugin, "Unregistered network plugin");
|
|
|
|
|
|
2022-03-21 22:56:21 +00:00
|
|
|
it = _plugins.erase(it);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-22 22:18:22 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-26 14:48:16 +02:00
|
|
|
GameActions::Result ScriptEngine::QueryOrExecuteCustomGameAction(const GameActions::CustomAction& customAction, bool isExecute)
|
2020-03-01 21:56:49 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSContext* ctx = _replContext;
|
2023-01-15 21:52:23 +01:00
|
|
|
std::string actionz = customAction.GetId();
|
2020-03-02 23:55:18 +00:00
|
|
|
auto kvp = _customActions.find(actionz);
|
|
|
|
|
if (kvp != _customActions.end())
|
2020-03-01 21:56:49 +00:00
|
|
|
{
|
2023-01-15 21:52:23 +01:00
|
|
|
const auto& customActionInfo = kvp->second;
|
2020-03-02 23:55:18 +00:00
|
|
|
|
|
|
|
|
// Deserialise the JSON args
|
2025-09-29 02:05:00 +10:00
|
|
|
const std::string& argsz = customAction.GetJson();
|
2020-03-07 12:37:07 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
auto jsArgs = JS_ParseJSON(ctx, argsz.c_str(), argsz.size(), customActionInfo.Name.c_str());
|
|
|
|
|
if (JS_IsException(jsArgs))
|
2020-03-07 12:37:07 +00:00
|
|
|
{
|
2025-12-12 12:11:49 +01:00
|
|
|
auto res = GameActions::Result();
|
|
|
|
|
res.error = GameActions::Status::invalidParameters;
|
|
|
|
|
res.errorTitle = "Invalid JSON";
|
|
|
|
|
return res;
|
2020-03-07 12:37:07 +00:00
|
|
|
}
|
2020-03-02 23:55:18 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
std::vector<JSValue> pluginCallArgs;
|
2025-01-19 16:59:45 +01:00
|
|
|
if (customActionInfo.Owner->GetTargetAPIVersion() <= kApiVersionCustomActionArgs)
|
2023-01-15 21:52:23 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
pluginCallArgs = { jsArgs };
|
2023-01-15 21:52:23 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue obj = JS_NewObject(ctx);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "action", JSFromStdString(ctx, actionz));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "args", jsArgs);
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "player", JS_NewInt32(ctx, customAction.GetPlayer()));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "type", JS_NewInt32(ctx, EnumValue(customAction.GetType())));
|
2023-01-15 21:52:23 +01:00
|
|
|
|
|
|
|
|
auto flags = customAction.GetActionFlags();
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "isClientOnly", JS_NewBool(ctx, (flags & GameActions::Flags::ClientOnly) != 0));
|
|
|
|
|
pluginCallArgs = { obj };
|
2023-01-15 21:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:55:18 +00:00
|
|
|
// Ready to call plugin handler
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue jsResult;
|
2020-03-02 23:55:18 +00:00
|
|
|
if (!isExecute)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
jsResult = ExecutePluginCall(
|
|
|
|
|
customActionInfo.Owner, customActionInfo.Query.callback, JS_UNDEFINED, pluginCallArgs, false, false, true);
|
2020-03-02 23:55:18 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
jsResult = ExecutePluginCall(
|
|
|
|
|
customActionInfo.Owner, customActionInfo.Execute.callback, JS_UNDEFINED, pluginCallArgs, true, false, true);
|
2020-03-02 23:55:18 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
|
|
|
|
|
GameActions::Result res = JSToGameActionResult(ctx, jsResult);
|
|
|
|
|
JS_FreeValue(ctx, jsResult);
|
|
|
|
|
return res;
|
2020-03-01 21:56:49 +00:00
|
|
|
}
|
2023-06-03 09:38:40 -04:00
|
|
|
|
2025-12-12 12:11:49 +01:00
|
|
|
auto res = GameActions::Result();
|
|
|
|
|
res.error = GameActions::Status::unknown;
|
|
|
|
|
res.errorTitle = "Unknown custom action";
|
|
|
|
|
res.errorMessage = customAction.GetPluginName() + ": " + actionz;
|
|
|
|
|
return res;
|
2020-03-02 23:55:18 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
GameActions::Result ScriptEngine::JSToGameActionResult(JSContext* ctx, JSValue d)
|
2020-03-02 23:55:18 +00:00
|
|
|
{
|
2021-11-23 23:35:08 -08:00
|
|
|
auto result = GameActions::Result();
|
2025-09-29 02:05:00 +10:00
|
|
|
if (JS_IsObject(d))
|
2020-05-01 17:50:11 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
result.error = static_cast<GameActions::Status>(AsOrDefault(ctx, d, "error", int32_t()));
|
|
|
|
|
result.errorTitle = AsOrDefault(ctx, d, "errorTitle", "");
|
|
|
|
|
result.errorMessage = AsOrDefault(ctx, d, "errorMessage", "");
|
|
|
|
|
result.cost = AsOrDefault(ctx, d, "cost", int64_t());
|
|
|
|
|
auto expenditureType = AsOrDefault(ctx, d, "expenditureType", "");
|
2023-06-24 16:20:21 +03:00
|
|
|
if (!expenditureType.empty())
|
2020-05-01 17:50:11 +01:00
|
|
|
{
|
2023-06-24 16:20:21 +03:00
|
|
|
auto expenditure = StringToExpenditureType(expenditureType);
|
2025-08-21 21:03:44 +02:00
|
|
|
if (expenditure != ExpenditureType::count)
|
2023-06-24 16:20:21 +03:00
|
|
|
{
|
2025-12-12 12:11:49 +01:00
|
|
|
result.expenditure = expenditure;
|
2023-06-24 16:20:21 +03:00
|
|
|
}
|
2020-05-01 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-24 16:20:21 +03:00
|
|
|
else
|
|
|
|
|
{
|
2025-12-12 12:11:49 +01:00
|
|
|
result.error = GameActions::Status::unknown;
|
|
|
|
|
result.errorTitle = "Unknown";
|
|
|
|
|
result.errorMessage = "Unknown";
|
2023-06-24 16:20:21 +03:00
|
|
|
}
|
2020-03-02 23:55:18 +00:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-01 17:50:11 +01:00
|
|
|
constexpr static const char* ExpenditureTypes[] = {
|
|
|
|
|
"ride_construction",
|
|
|
|
|
"ride_runningcosts",
|
|
|
|
|
"land_purchase",
|
|
|
|
|
"landscaping",
|
|
|
|
|
"park_entrance_tickets",
|
|
|
|
|
"park_ride_tickets",
|
|
|
|
|
"shop_sales",
|
|
|
|
|
"shop_stock",
|
|
|
|
|
"food_drink_sales",
|
|
|
|
|
"food_drink_stock",
|
|
|
|
|
"wages",
|
|
|
|
|
"marketing",
|
|
|
|
|
"research",
|
|
|
|
|
"interest",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string_view ScriptEngine::ExpenditureTypeToString(ExpenditureType expenditureType)
|
|
|
|
|
{
|
|
|
|
|
auto index = static_cast<size_t>(expenditureType);
|
|
|
|
|
if (index < std::size(ExpenditureTypes))
|
|
|
|
|
{
|
|
|
|
|
return ExpenditureTypes[index];
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
ExpenditureType ScriptEngine::StringToExpenditureType(std::string_view expenditureType)
|
2020-05-01 17:50:11 +01:00
|
|
|
{
|
|
|
|
|
auto it = std::find(std::begin(ExpenditureTypes), std::end(ExpenditureTypes), expenditureType);
|
|
|
|
|
if (it != std::end(ExpenditureTypes))
|
|
|
|
|
{
|
|
|
|
|
return static_cast<ExpenditureType>(std::distance(std::begin(ExpenditureTypes), it));
|
|
|
|
|
}
|
2025-08-21 21:03:44 +02:00
|
|
|
return ExpenditureType::count;
|
2020-05-01 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue ScriptEngine::GameActionResultToJS(
|
|
|
|
|
JSContext* ctx, const GameActions::GameAction& action, const GameActions::Result& result)
|
2020-03-04 20:03:13 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue obj = JS_NewObject(ctx);
|
2020-03-05 18:30:41 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(
|
|
|
|
|
ctx, obj, "error", JS_NewInt32(ctx, static_cast<std::underlying_type_t<GameActions::Status>>(result.error)));
|
2025-12-12 12:11:49 +01:00
|
|
|
if (result.error != GameActions::Status::ok)
|
2020-03-04 20:03:13 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "errorTitle", JSFromStdString(ctx, result.getErrorTitle()));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "errorMessage", JSFromStdString(ctx, result.getErrorMessage()));
|
2020-03-04 20:03:13 +00:00
|
|
|
}
|
2023-01-12 22:14:52 +01:00
|
|
|
|
2025-12-12 12:11:49 +01:00
|
|
|
if (result.cost != kMoney64Undefined)
|
2020-03-04 20:03:13 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "cost", JS_NewInt64(ctx, result.cost));
|
2020-03-04 20:03:13 +00:00
|
|
|
}
|
2025-12-12 12:11:49 +01:00
|
|
|
if (!result.position.IsNull())
|
2020-03-04 20:03:13 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "position", ToJSValue(ctx, result.position));
|
2020-03-04 20:03:13 +00:00
|
|
|
}
|
2025-12-12 12:11:49 +01:00
|
|
|
if (result.expenditure != ExpenditureType::count)
|
2020-05-01 17:50:11 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "expenditureType", JSFromStdString(ctx, ExpenditureTypeToString(result.expenditure)));
|
2020-05-01 17:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
if (result.error == GameActions::Status::ok)
|
2020-03-05 18:30:41 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
// RideCreateAction only
|
|
|
|
|
if (action.GetType() == GameCommand::CreateRide)
|
2020-03-05 18:30:41 +00:00
|
|
|
{
|
2025-12-12 12:11:49 +01:00
|
|
|
const auto rideIndex = result.getData<RideId>();
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "ride", JS_NewInt32(ctx, rideIndex.ToUnderlying()));
|
2020-03-05 18:30:41 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
// StaffHireNewAction only
|
|
|
|
|
else if (action.GetType() == GameCommand::HireNewStaffMember)
|
2020-11-26 14:37:07 -05:00
|
|
|
{
|
2025-12-12 12:11:49 +01:00
|
|
|
const auto actionResult = result.getData<GameActions::StaffHireNewActionResult>();
|
2022-02-12 23:31:06 +02:00
|
|
|
if (!actionResult.StaffEntityId.IsNull())
|
2021-10-04 16:57:39 +03:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "peep", JS_NewInt32(ctx, actionResult.StaffEntityId.ToUnderlying()));
|
2021-10-04 16:57:39 +03:00
|
|
|
}
|
2020-11-26 14:37:07 -05:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
// BannerPlaceAction, LargeSceneryPlaceAction, WallPlaceAction
|
|
|
|
|
auto bannerId = BannerIndex::GetNull();
|
|
|
|
|
switch (action.GetType())
|
|
|
|
|
{
|
|
|
|
|
case GameCommand::PlaceBanner:
|
|
|
|
|
bannerId = result.getData<GameActions::BannerPlaceActionResult>().bannerId;
|
|
|
|
|
break;
|
|
|
|
|
case GameCommand::PlaceLargeScenery:
|
|
|
|
|
bannerId = result.getData<GameActions::LargeSceneryPlaceActionResult>().bannerId;
|
|
|
|
|
break;
|
|
|
|
|
case GameCommand::PlaceWall:
|
|
|
|
|
bannerId = result.getData<GameActions::WallPlaceActionResult>().BannerId;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!bannerId.IsNull())
|
|
|
|
|
{
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "bannerIndex", JS_NewInt32(ctx, bannerId.ToUnderlying()));
|
|
|
|
|
}
|
2024-05-29 22:35:35 +02:00
|
|
|
}
|
2020-03-05 18:30:41 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
return obj;
|
2020-03-04 20:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 23:55:18 +00:00
|
|
|
bool ScriptEngine::RegisterCustomAction(
|
2025-09-29 02:05:00 +10:00
|
|
|
const std::shared_ptr<Plugin>& plugin, std::string_view action, const JSCallback& query, const JSCallback& execute)
|
2020-03-02 23:55:18 +00:00
|
|
|
{
|
|
|
|
|
std::string actionz = std::string(action);
|
|
|
|
|
if (_customActions.find(actionz) != _customActions.end())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
2020-03-01 21:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-30 10:56:57 +01:00
|
|
|
CustomActionInfo customAction;
|
2020-03-04 00:01:17 +00:00
|
|
|
customAction.Owner = plugin;
|
2020-03-02 23:55:18 +00:00
|
|
|
customAction.Name = std::move(actionz);
|
|
|
|
|
customAction.Query = query;
|
|
|
|
|
customAction.Execute = execute;
|
|
|
|
|
_customActions[customAction.Name] = std::move(customAction);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::RemoveCustomGameActions(const std::shared_ptr<Plugin>& plugin)
|
|
|
|
|
{
|
2020-04-24 13:38:27 +01:00
|
|
|
for (auto it = _customActions.begin(); it != _customActions.end();)
|
|
|
|
|
{
|
|
|
|
|
if (it->second.Owner == plugin)
|
|
|
|
|
{
|
|
|
|
|
it = _customActions.erase(it);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-01 21:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
class JSToGameActionParameterVisitor : public GameActions::GameActionParameterVisitor
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
|
|
|
|
private:
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue _jsValue;
|
|
|
|
|
JSContext* _ctx;
|
|
|
|
|
bool _error = false;
|
2020-04-30 10:56:57 +01:00
|
|
|
|
|
|
|
|
public:
|
2025-09-29 02:05:00 +10:00
|
|
|
JSToGameActionParameterVisitor(JSContext* ctx, JSValue jsValue)
|
|
|
|
|
: _jsValue(jsValue)
|
|
|
|
|
, _ctx(ctx)
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void Visit(std::string_view name, bool& param) override
|
2020-05-04 22:10:53 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto val = JSToOptionalBool(_ctx, _jsValue, std::string(name).c_str());
|
|
|
|
|
if (val.has_value())
|
|
|
|
|
param = val.value();
|
|
|
|
|
else
|
|
|
|
|
_error = true;
|
2020-05-04 22:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void Visit(std::string_view name, int32_t& param) override
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto val = JSToOptionalInt(_ctx, _jsValue, std::string(name).c_str());
|
|
|
|
|
if (val.has_value())
|
|
|
|
|
param = val.value();
|
|
|
|
|
else
|
|
|
|
|
_error = true;
|
2020-04-30 10:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void Visit(std::string_view name, std::string& param) override
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
auto val = JSToOptionalStdString(_ctx, _jsValue, std::string(name).c_str());
|
|
|
|
|
if (val.has_value())
|
|
|
|
|
param = val.value();
|
|
|
|
|
else
|
|
|
|
|
_error = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GetErrorFlag() const
|
|
|
|
|
{
|
|
|
|
|
return _error;
|
2020-04-30 10:56:57 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
class JSFromGameActionParameterVisitor : public GameActions::GameActionParameterVisitor
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
|
|
|
|
private:
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue _jsObject;
|
|
|
|
|
JSContext* _ctx;
|
2020-04-30 10:56:57 +01:00
|
|
|
|
|
|
|
|
public:
|
2025-09-29 02:05:00 +10:00
|
|
|
JSFromGameActionParameterVisitor(JSContext* ctx, JSValue jsObject)
|
|
|
|
|
: _jsObject(jsObject)
|
|
|
|
|
, _ctx(ctx)
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void Visit(std::string_view name, bool& param) override
|
2020-05-04 22:10:53 +01:00
|
|
|
{
|
|
|
|
|
std::string szName(name);
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(_ctx, _jsObject, szName.c_str(), JS_NewBool(_ctx, param));
|
2020-05-04 22:10:53 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void Visit(std::string_view name, int32_t& param) override
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
|
|
|
|
std::string szName(name);
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(_ctx, _jsObject, szName.c_str(), JS_NewInt32(_ctx, param));
|
2020-04-30 10:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-07 01:22:22 +09:00
|
|
|
void Visit(std::string_view name, std::string& param) override
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
|
|
|
|
std::string szName(name);
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(_ctx, _jsObject, szName.c_str(), JSFromStdString(_ctx, param));
|
2020-04-30 10:56:57 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-22 20:21:38 +03:00
|
|
|
// clang-format off
|
|
|
|
|
const static EnumMap<GameCommand> ActionNameToType = {
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "balloonpress", GameCommand::BalloonPress },
|
|
|
|
|
{ "bannerplace", GameCommand::PlaceBanner },
|
|
|
|
|
{ "bannerremove", GameCommand::RemoveBanner },
|
|
|
|
|
{ "bannersetcolour", GameCommand::SetBannerColour },
|
|
|
|
|
{ "bannersetname", GameCommand::SetBannerName },
|
|
|
|
|
{ "bannersetstyle", GameCommand::SetBannerStyle },
|
|
|
|
|
{ "clearscenery", GameCommand::ClearScenery },
|
|
|
|
|
{ "footpathplace", GameCommand::PlacePath },
|
2023-01-05 10:39:15 +01:00
|
|
|
{ "footpathlayoutplace", GameCommand::PlacePathLayout },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "footpathremove", GameCommand::RemovePath },
|
|
|
|
|
{ "footpathadditionplace", GameCommand::PlaceFootpathAddition },
|
|
|
|
|
{ "footpathadditionremove", GameCommand::RemoveFootpathAddition },
|
2023-10-04 11:13:40 -04:00
|
|
|
{ "gamesetspeed", GameCommand::SetGameSpeed },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "guestsetflags", GameCommand::GuestSetFlags },
|
|
|
|
|
{ "guestsetname", GameCommand::SetGuestName },
|
|
|
|
|
{ "landbuyrights", GameCommand::BuyLandRights },
|
|
|
|
|
{ "landlower", GameCommand::LowerLand },
|
|
|
|
|
{ "landraise", GameCommand::RaiseLand },
|
|
|
|
|
{ "landsetheight", GameCommand::SetLandHeight },
|
|
|
|
|
{ "landsetrights", GameCommand::SetLandOwnership },
|
2022-12-16 22:38:45 +01:00
|
|
|
{ "landsmooth", GameCommand::EditLandSmooth },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "largesceneryplace", GameCommand::PlaceLargeScenery },
|
|
|
|
|
{ "largesceneryremove", GameCommand::RemoveLargeScenery },
|
2022-12-16 22:38:45 +01:00
|
|
|
{ "largescenerysetcolour", GameCommand::SetLargeSceneryColour },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "loadorquit", GameCommand::LoadOrQuit },
|
2023-01-07 14:36:31 +01:00
|
|
|
{ "mapchangesize", GameCommand::ChangeMapSize },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "mazeplacetrack", GameCommand::PlaceMazeDesign },
|
|
|
|
|
{ "mazesettrack", GameCommand::SetMazeTrack },
|
|
|
|
|
{ "networkmodifygroup", GameCommand::ModifyGroups },
|
2022-12-16 22:38:45 +01:00
|
|
|
{ "parkentranceplace", GameCommand::PlaceParkEntrance },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "parkentranceremove", GameCommand::RemoveParkEntrance },
|
|
|
|
|
{ "parkmarketing", GameCommand::StartMarketingCampaign },
|
|
|
|
|
{ "parksetdate", GameCommand::SetDate },
|
2023-01-02 14:42:28 +01:00
|
|
|
{ "parksetentrancefee", GameCommand::SetParkEntranceFee },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "parksetloan", GameCommand::SetCurrentLoan },
|
|
|
|
|
{ "parksetname", GameCommand::SetParkName },
|
|
|
|
|
{ "parksetparameter", GameCommand::SetParkOpen },
|
|
|
|
|
{ "parksetresearchfunding", GameCommand::SetResearchFunding },
|
|
|
|
|
{ "pausetoggle", GameCommand::TogglePause },
|
|
|
|
|
{ "peeppickup", GameCommand::PickupGuest },
|
2022-12-16 22:38:45 +01:00
|
|
|
{ "peepspawnplace", GameCommand::PlacePeepSpawn },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "playerkick", GameCommand::KickPlayer },
|
|
|
|
|
{ "playersetgroup", GameCommand::SetPlayerGroup },
|
|
|
|
|
{ "ridecreate", GameCommand::CreateRide },
|
|
|
|
|
{ "ridedemolish", GameCommand::DemolishRide },
|
|
|
|
|
{ "rideentranceexitplace", GameCommand::PlaceRideEntranceOrExit },
|
|
|
|
|
{ "rideentranceexitremove", GameCommand::RemoveRideEntranceOrExit },
|
2022-09-21 19:14:43 +02:00
|
|
|
{ "ridefreezerating", GameCommand::FreezeRideRating },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "ridesetappearance", GameCommand::SetRideAppearance },
|
|
|
|
|
{ "ridesetcolourscheme", GameCommand::SetColourScheme },
|
|
|
|
|
{ "ridesetname", GameCommand::SetRideName },
|
|
|
|
|
{ "ridesetprice", GameCommand::SetRidePrice },
|
|
|
|
|
{ "ridesetsetting", GameCommand::SetRideSetting },
|
|
|
|
|
{ "ridesetstatus", GameCommand::SetRideStatus },
|
2022-12-16 22:38:45 +01:00
|
|
|
{ "ridesetvehicle", GameCommand::SetRideVehicles },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "scenariosetsetting", GameCommand::EditScenarioOptions },
|
2023-04-18 13:23:44 +02:00
|
|
|
{ "cheatset", GameCommand::Cheat },
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "signsetname", GameCommand::SetSignName },
|
|
|
|
|
{ "signsetstyle", GameCommand::SetSignStyle },
|
|
|
|
|
{ "smallsceneryplace", GameCommand::PlaceScenery },
|
|
|
|
|
{ "smallsceneryremove", GameCommand::RemoveScenery },
|
2022-12-16 22:38:45 +01:00
|
|
|
{ "smallscenerysetcolour", GameCommand::SetSceneryColour},
|
2020-12-22 23:53:37 +00:00
|
|
|
{ "stafffire", GameCommand::FireStaffMember },
|
|
|
|
|
{ "staffhire", GameCommand::HireNewStaffMember },
|
|
|
|
|
{ "staffsetcolour", GameCommand::SetStaffColour },
|
|
|
|
|
{ "staffsetcostume", GameCommand::SetStaffCostume },
|
|
|
|
|
{ "staffsetname", GameCommand::SetStaffName },
|
|
|
|
|
{ "staffsetorders", GameCommand::SetStaffOrders },
|
|
|
|
|
{ "staffsetpatrolarea", GameCommand::SetStaffPatrol },
|
|
|
|
|
{ "surfacesetstyle", GameCommand::ChangeSurfaceStyle },
|
|
|
|
|
{ "tilemodify", GameCommand::ModifyTile },
|
|
|
|
|
{ "trackdesign", GameCommand::PlaceTrackDesign },
|
|
|
|
|
{ "trackplace", GameCommand::PlaceTrack },
|
|
|
|
|
{ "trackremove", GameCommand::RemoveTrack },
|
|
|
|
|
{ "tracksetbrakespeed", GameCommand::SetBrakesSpeed },
|
|
|
|
|
{ "wallplace", GameCommand::PlaceWall },
|
|
|
|
|
{ "wallremove", GameCommand::RemoveWall },
|
|
|
|
|
{ "wallsetcolour", GameCommand::SetWallColour },
|
|
|
|
|
{ "waterlower", GameCommand::LowerWater },
|
|
|
|
|
{ "waterraise", GameCommand::RaiseWater },
|
|
|
|
|
{ "watersetheight", GameCommand::SetWaterHeight }
|
2020-05-04 23:01:17 +01:00
|
|
|
};
|
2021-07-22 20:21:38 +03:00
|
|
|
// clang-format on
|
2020-05-04 23:01:17 +01:00
|
|
|
|
2020-12-22 23:53:37 +00:00
|
|
|
static std::string GetActionName(GameCommand commandId)
|
2020-05-04 23:01:17 +01:00
|
|
|
{
|
2021-07-22 20:21:38 +03:00
|
|
|
auto it = ActionNameToType.find(commandId);
|
2020-05-04 23:01:17 +01:00
|
|
|
if (it != ActionNameToType.end())
|
|
|
|
|
{
|
2021-07-22 20:21:38 +03:00
|
|
|
return std::string{ it->first };
|
2020-05-04 23:01:17 +01:00
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 11:32:05 +02:00
|
|
|
static std::unique_ptr<GameActions::GameAction> CreateGameActionFromActionId(const std::string& name)
|
2020-05-04 23:01:17 +01:00
|
|
|
{
|
2021-07-22 20:21:38 +03:00
|
|
|
auto result = ActionNameToType.find(name);
|
2020-05-04 23:01:17 +01:00
|
|
|
if (result != ActionNameToType.end())
|
|
|
|
|
{
|
|
|
|
|
return GameActions::Create(result->second);
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-29 11:32:05 +02:00
|
|
|
void ScriptEngine::RunGameActionHooks(const GameActions::GameAction& action, GameActions::Result& result, bool isExecute)
|
2020-03-04 20:03:13 +00:00
|
|
|
{
|
2025-03-26 12:16:47 +01:00
|
|
|
auto hookType = isExecute ? HookType::actionExecute : HookType::actionQuery;
|
2020-03-04 20:03:13 +00:00
|
|
|
if (_hookEngine.HasSubscriptions(hookType))
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSContext* ctx = _replContext;
|
|
|
|
|
JSValue obj = JS_NewObject(ctx);
|
2020-05-04 23:01:17 +01:00
|
|
|
|
|
|
|
|
auto actionId = action.GetType();
|
2020-12-22 23:53:37 +00:00
|
|
|
if (action.GetType() == GameCommand::Custom)
|
2020-05-04 23:01:17 +01:00
|
|
|
{
|
2025-08-26 14:48:16 +02:00
|
|
|
auto customAction = static_cast<const GameActions::CustomAction&>(action);
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "action", JSFromStdString(ctx, customAction.GetId()));
|
2020-05-04 23:01:17 +01:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue jsArgs = JS_ParseJSON(
|
|
|
|
|
ctx, customAction.GetJson().c_str(), customAction.GetJson().length(), customAction.GetName());
|
|
|
|
|
if (!JS_IsException(jsArgs))
|
2020-05-04 23:01:17 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "args", jsArgs);
|
2020-05-04 23:01:17 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "args", JS_NewObject(ctx));
|
2020-05-04 23:01:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
auto actionName = GetActionName(actionId);
|
|
|
|
|
if (!actionName.empty())
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "action", JSFromStdString(ctx, actionName));
|
2020-05-04 23:01:17 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue args = JS_NewObject(ctx);
|
|
|
|
|
JSFromGameActionParameterVisitor visitor(ctx, args);
|
2025-08-29 11:32:05 +02:00
|
|
|
const_cast<GameActions::GameAction&>(action).AcceptParameters(visitor);
|
|
|
|
|
const_cast<GameActions::GameAction&>(action).AcceptFlags(visitor);
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "args", args);
|
2020-05-04 23:01:17 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "player", JS_NewInt32(ctx, action.GetPlayer()));
|
|
|
|
|
JS_SetPropertyStr(ctx, obj, "type", JS_NewInt32(ctx, EnumValue(actionId)));
|
2020-03-04 20:03:13 +00:00
|
|
|
|
|
|
|
|
auto flags = action.GetActionFlags();
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "isClientOnly", JS_NewBool(ctx, (flags & GameActions::Flags::ClientOnly) != 0));
|
2020-03-05 18:30:41 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_SetPropertyStr(ctx, obj, "result", GameActionResultToJS(ctx, action, result));
|
2020-03-04 20:03:13 +00:00
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
_hookEngine.Call(hookType, obj, false, true);
|
2020-03-04 20:03:13 +00:00
|
|
|
|
|
|
|
|
if (!isExecute)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue jsResult = JS_GetPropertyStr(ctx, obj, "result");
|
|
|
|
|
if (JS_IsObject(jsResult))
|
2020-03-04 20:03:13 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
int32_t error = AsOrDefault(ctx, jsResult, "error", int32_t());
|
2020-05-02 11:45:05 +01:00
|
|
|
if (error != 0)
|
|
|
|
|
{
|
2025-12-12 12:11:49 +01:00
|
|
|
result.error = static_cast<GameActions::Status>(error);
|
2025-09-29 02:05:00 +10:00
|
|
|
result.errorTitle = AsOrDefault(ctx, jsResult, "errorTitle", "");
|
|
|
|
|
result.errorMessage = AsOrDefault(ctx, jsResult, "errorMessage", "");
|
2020-05-02 11:45:05 +01:00
|
|
|
}
|
2020-03-04 20:03:13 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_FreeValue(ctx, jsResult);
|
2020-03-04 20:03:13 +00:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_FreeValue(ctx, obj);
|
2020-03-04 20:03:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
std::pair<std::unique_ptr<GameActions::GameAction>, bool> ScriptEngine::CreateGameAction(
|
|
|
|
|
JSContext* ctx, const std::string& actionid, JSValue args, const std::string& pluginName)
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
|
|
|
|
auto action = CreateGameActionFromActionId(actionid);
|
|
|
|
|
if (action != nullptr)
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSToGameActionParameterVisitor visitor(ctx, args);
|
2020-04-30 10:56:57 +01:00
|
|
|
action->AcceptParameters(visitor);
|
2025-09-29 02:05:00 +10:00
|
|
|
|
|
|
|
|
JSValue flags = JS_GetPropertyStr(ctx, args, "flags");
|
|
|
|
|
if (JS_IsNumber(flags))
|
2020-11-06 10:40:15 +01:00
|
|
|
{
|
|
|
|
|
action->AcceptFlags(visitor);
|
|
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_FreeValue(ctx, flags);
|
|
|
|
|
return { std::move(action), visitor.GetErrorFlag() };
|
2020-04-30 10:56:57 +01:00
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
|
|
|
|
// Serialise args to json so that it can be sent
|
2025-09-29 02:05:00 +10:00
|
|
|
std::string json;
|
|
|
|
|
if (JS_IsObject(args))
|
2021-09-15 22:22:15 +02:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue jsonVal = JS_JSONStringify(ctx, args, JS_UNDEFINED, JS_UNDEFINED);
|
|
|
|
|
if (JS_IsString(jsonVal))
|
|
|
|
|
{
|
|
|
|
|
json = JSToStdString(ctx, jsonVal);
|
|
|
|
|
}
|
|
|
|
|
JS_FreeValue(ctx, jsonVal);
|
2021-09-15 22:22:15 +02:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
if (json.empty())
|
2020-04-30 10:56:57 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue emptyObj = JS_NewObject(ctx);
|
|
|
|
|
JSValue jsonVal = JS_JSONStringify(ctx, args, JS_UNDEFINED, JS_UNDEFINED);
|
|
|
|
|
json = JSToStdString(ctx, jsonVal);
|
|
|
|
|
JS_FreeValue(ctx, emptyObj);
|
|
|
|
|
JS_FreeValue(ctx, jsonVal);
|
2020-04-30 10:56:57 +01:00
|
|
|
}
|
2025-08-26 14:48:16 +02:00
|
|
|
auto customAction = std::make_unique<GameActions::CustomAction>(actionid, json, pluginName);
|
2023-01-15 21:52:23 +01:00
|
|
|
|
2025-08-31 16:02:43 +02:00
|
|
|
if (customAction->GetPlayer() == -1 && Network::GetMode() != Network::Mode::none)
|
2023-01-15 21:52:23 +01:00
|
|
|
{
|
2025-08-31 16:02:43 +02:00
|
|
|
customAction->SetPlayer(Network::GetCurrentPlayerId());
|
2023-01-15 21:52:23 +01:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
return { std::move(customAction), false };
|
2020-04-30 10:56:57 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-07 12:37:07 +00:00
|
|
|
void ScriptEngine::InitSharedStorage()
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (_replContext)
|
|
|
|
|
{
|
|
|
|
|
JS_FreeValue(_replContext, _sharedStorage);
|
|
|
|
|
_sharedStorage = JS_NewObject(_replContext);
|
|
|
|
|
}
|
2020-03-07 12:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-06 20:17:15 +00:00
|
|
|
void ScriptEngine::LoadSharedStorage()
|
|
|
|
|
{
|
2020-03-07 12:37:07 +00:00
|
|
|
InitSharedStorage();
|
|
|
|
|
|
2025-03-25 23:57:36 +01:00
|
|
|
auto path = _env.GetFilePath(PathId::pluginStore);
|
2020-03-06 20:17:15 +00:00
|
|
|
try
|
|
|
|
|
{
|
2020-04-22 22:53:32 +01:00
|
|
|
if (File::Exists(path))
|
2020-03-07 12:37:07 +00:00
|
|
|
{
|
2020-04-22 22:53:32 +01:00
|
|
|
auto data = File::ReadAllBytes(path);
|
2025-09-29 02:05:00 +10:00
|
|
|
// quickjs wants a null terminator not counted in the buf_len
|
|
|
|
|
data.push_back(0);
|
|
|
|
|
JSValue result = JS_ParseJSON(
|
|
|
|
|
_replContext, reinterpret_cast<const char*>(data.data()), data.size() - 1, path.c_str());
|
|
|
|
|
if (JS_IsObject(result))
|
2020-04-22 22:53:32 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JS_FreeValue(_replContext, _sharedStorage);
|
|
|
|
|
_sharedStorage = result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine("Unable to load plugin shared storage");
|
2020-04-22 22:53:32 +01:00
|
|
|
}
|
2020-03-07 12:37:07 +00:00
|
|
|
}
|
2020-03-06 20:17:15 +00:00
|
|
|
}
|
|
|
|
|
catch (const std::exception&)
|
|
|
|
|
{
|
2021-02-13 00:15:27 +00:00
|
|
|
Console::Error::WriteLine("Unable to read '%s'", path.c_str());
|
2020-03-06 20:17:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::SaveSharedStorage()
|
|
|
|
|
{
|
2025-03-25 23:57:36 +01:00
|
|
|
auto path = _env.GetFilePath(PathId::pluginStore);
|
2020-03-06 20:17:15 +00:00
|
|
|
try
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
JSValue jsonVal = JS_JSONStringify(_replContext, _sharedStorage, JS_UNDEFINED, JS_UNDEFINED);
|
|
|
|
|
if (JS_IsString(jsonVal))
|
|
|
|
|
{
|
|
|
|
|
// inefficient to copy the whole string out first, but we have to do this to avoid breaking the exception flow
|
|
|
|
|
std::string json = JSToStdString(_replContext, jsonVal);
|
|
|
|
|
JS_FreeValue(_replContext, jsonVal);
|
|
|
|
|
File::WriteAllBytes(path, json.c_str(), json.size());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
JS_FreeValue(_replContext, jsonVal);
|
|
|
|
|
Console::Error::WriteLine("Unable to stringify shared storage JSON");
|
2020-03-06 20:17:15 +00:00
|
|
|
}
|
|
|
|
|
catch (const std::exception&)
|
|
|
|
|
{
|
2021-02-13 00:15:27 +00:00
|
|
|
Console::Error::WriteLine("Unable to write to '%s'", path.c_str());
|
2020-03-06 20:17:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 16:32:08 +00:00
|
|
|
void ScriptEngine::ClearParkStorage()
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (_replContext)
|
|
|
|
|
{
|
|
|
|
|
JS_FreeValue(_replContext, _parkStorage);
|
|
|
|
|
_parkStorage = JS_NewObject(_replContext);
|
|
|
|
|
}
|
2022-02-19 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ScriptEngine::GetParkStorageAsJSON()
|
|
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
std::string retStr{};
|
|
|
|
|
JSValue jsonVal = JS_JSONStringify(_replContext, _parkStorage, JS_UNDEFINED, JS_UNDEFINED);
|
|
|
|
|
if (JS_IsString(jsonVal))
|
|
|
|
|
{
|
|
|
|
|
retStr = JSToStdString(_replContext, jsonVal);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console::Error::WriteLine("Could not stringify park storage");
|
|
|
|
|
}
|
|
|
|
|
JS_FreeValue(_replContext, jsonVal);
|
|
|
|
|
return retStr;
|
2022-02-19 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
void ScriptEngine::SetParkStorageFromJSON(const std::string& value, const std::string& filename)
|
2022-02-19 16:32:08 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (value.empty())
|
2022-02-19 16:32:08 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
ClearParkStorage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
JSValue result = JS_ParseJSON(_replContext, value.c_str(), value.size(), filename.c_str());
|
|
|
|
|
if (JS_IsObject(result))
|
|
|
|
|
{
|
|
|
|
|
JS_FreeValue(_replContext, _parkStorage);
|
|
|
|
|
_parkStorage = result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ClearParkStorage();
|
|
|
|
|
Console::Error::WriteLine("Could not load park storage");
|
2022-02-19 16:32:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 15:08:35 +00:00
|
|
|
IntervalHandle ScriptEngine::AllocateHandle()
|
|
|
|
|
{
|
2024-01-11 19:45:07 +02:00
|
|
|
const auto nextHandle = _nextIntervalHandle;
|
2024-01-06 09:38:25 +02:00
|
|
|
|
2024-01-11 19:45:07 +02:00
|
|
|
// In case of overflow start from 1 again
|
2024-07-15 21:16:30 +02:00
|
|
|
_nextIntervalHandle = std::max(_nextIntervalHandle + 1u, 1u);
|
2024-01-11 19:45:07 +02:00
|
|
|
|
|
|
|
|
return nextHandle;
|
2021-01-03 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
IntervalHandle ScriptEngine::AddInterval(
|
|
|
|
|
const std::shared_ptr<Plugin>& plugin, int32_t delay, bool repeat, const JSCallback& callback)
|
2021-01-03 15:08:35 +00:00
|
|
|
{
|
|
|
|
|
auto handle = AllocateHandle();
|
2024-01-06 09:38:25 +02:00
|
|
|
assert(handle != 0);
|
|
|
|
|
|
|
|
|
|
auto& interval = _intervals[handle];
|
|
|
|
|
interval.Owner = plugin;
|
|
|
|
|
interval.Delay = delay;
|
|
|
|
|
interval.LastTimestamp = _lastIntervalTimestamp;
|
2025-09-29 02:05:00 +10:00
|
|
|
interval.Callback = callback;
|
2024-01-06 09:38:25 +02:00
|
|
|
interval.Repeat = repeat;
|
|
|
|
|
|
2021-01-03 15:08:35 +00:00
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::RemoveInterval(const std::shared_ptr<Plugin>& plugin, IntervalHandle handle)
|
|
|
|
|
{
|
2024-01-06 09:38:25 +02:00
|
|
|
if (handle == 0)
|
|
|
|
|
return;
|
2021-01-03 15:08:35 +00:00
|
|
|
|
2024-01-06 09:38:25 +02:00
|
|
|
auto it = _intervals.find(handle);
|
|
|
|
|
if (it == _intervals.end())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
auto& interval = it->second;
|
|
|
|
|
|
|
|
|
|
// Only allow owner or REPL (nullptr) to remove intervals
|
|
|
|
|
if (plugin == nullptr || interval.Owner == plugin)
|
|
|
|
|
{
|
2024-01-25 17:00:43 +02:00
|
|
|
interval.Deleted = true;
|
2021-01-03 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::UpdateIntervals()
|
|
|
|
|
{
|
2022-02-18 21:57:00 +01:00
|
|
|
uint32_t timestamp = Platform::GetTicks();
|
2021-01-03 15:08:35 +00:00
|
|
|
if (timestamp < _lastIntervalTimestamp)
|
|
|
|
|
{
|
|
|
|
|
// timestamp has wrapped, subtract all intervals by the remaining amount before wrap
|
|
|
|
|
auto delta = static_cast<int64_t>(std::numeric_limits<uint32_t>::max() - _lastIntervalTimestamp);
|
2024-01-06 09:38:25 +02:00
|
|
|
for (auto& entry : _intervals)
|
2021-01-03 15:08:35 +00:00
|
|
|
{
|
2024-01-06 09:38:25 +02:00
|
|
|
auto& interval = entry.second;
|
|
|
|
|
interval.LastTimestamp = -delta;
|
2021-01-03 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_lastIntervalTimestamp = timestamp;
|
|
|
|
|
|
2024-01-25 17:00:43 +02:00
|
|
|
// Erase all intervals marked as deleted.
|
|
|
|
|
for (auto it = _intervals.begin(); it != _intervals.end();)
|
2021-01-03 15:08:35 +00:00
|
|
|
{
|
2024-01-06 09:38:25 +02:00
|
|
|
auto& interval = it->second;
|
|
|
|
|
|
2024-01-25 17:00:43 +02:00
|
|
|
if (interval.Deleted)
|
2024-01-06 09:38:25 +02:00
|
|
|
{
|
2024-01-25 17:00:43 +02:00
|
|
|
it = _intervals.erase(it);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
it++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-06 09:38:25 +02:00
|
|
|
|
2024-01-25 17:00:43 +02:00
|
|
|
// Execute all intervals that are due.
|
|
|
|
|
for (auto it = _intervals.begin(); it != _intervals.end(); it++)
|
|
|
|
|
{
|
|
|
|
|
auto& interval = it->second;
|
|
|
|
|
|
|
|
|
|
if (timestamp < interval.LastTimestamp + interval.Delay)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 17:13:25 +02:00
|
|
|
if (interval.Deleted)
|
|
|
|
|
{
|
|
|
|
|
// There is a chance that in one of the callbacks it deletes another interval.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
ExecutePluginCall(interval.Owner, interval.Callback.callback, {}, false);
|
2024-01-25 17:00:43 +02:00
|
|
|
|
|
|
|
|
interval.LastTimestamp = timestamp;
|
|
|
|
|
if (!interval.Repeat)
|
|
|
|
|
{
|
|
|
|
|
interval.Deleted = true;
|
2021-01-03 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::RemoveIntervals(const std::shared_ptr<Plugin>& plugin)
|
|
|
|
|
{
|
2024-01-06 09:38:25 +02:00
|
|
|
for (auto it = _intervals.begin(); it != _intervals.end();)
|
2021-01-03 15:08:35 +00:00
|
|
|
{
|
2024-01-06 09:38:25 +02:00
|
|
|
auto& interval = it->second;
|
|
|
|
|
|
2021-01-03 15:08:35 +00:00
|
|
|
if (interval.Owner == plugin)
|
|
|
|
|
{
|
2024-01-06 09:38:25 +02:00
|
|
|
it = _intervals.erase(it);
|
2021-01-03 15:08:35 +00:00
|
|
|
}
|
2024-01-11 17:34:32 +02:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
it++;
|
|
|
|
|
}
|
2021-01-03 15:08:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2025-09-29 02:05:00 +10:00
|
|
|
void ScriptEngine::AddSocket(SocketDataBase* data)
|
2020-08-17 03:53:37 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
_sockets.push_back(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScriptEngine::RemoveSocket(SocketDataBase* data)
|
|
|
|
|
{
|
|
|
|
|
// Just remove one
|
|
|
|
|
auto it = std::find(_sockets.begin(), _sockets.end(), data);
|
|
|
|
|
if (it != _sockets.end())
|
|
|
|
|
*it = nullptr;
|
2020-08-17 03:53:37 +01:00
|
|
|
}
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2020-08-17 03:53:37 +01:00
|
|
|
|
|
|
|
|
void ScriptEngine::UpdateSockets()
|
|
|
|
|
{
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2025-09-29 02:05:00 +10:00
|
|
|
// AddSocket and RemoveSocket can be called as a result of the Update
|
|
|
|
|
// Therefore we add to the end and remove by setting to null and cleaning up
|
|
|
|
|
// after the update. We also must use [] here and we remember the original
|
|
|
|
|
// sockets vector size so that we process new ones in the next tick.
|
|
|
|
|
const size_t sz = _sockets.size();
|
|
|
|
|
for (size_t i = 0; i < sz; i++)
|
2020-08-17 03:53:37 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (_sockets[i] != nullptr)
|
|
|
|
|
_sockets[i]->Update();
|
2020-08-17 03:53:37 +01:00
|
|
|
}
|
2025-09-29 02:05:00 +10:00
|
|
|
std::erase(_sockets, nullptr);
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2020-08-17 03:53:37 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-26 01:56:50 +01:00
|
|
|
void ScriptEngine::RemoveSockets(const std::shared_ptr<Plugin>& plugin)
|
|
|
|
|
{
|
2024-11-29 12:04:56 +09:00
|
|
|
#ifndef DISABLE_NETWORK
|
2025-09-29 02:05:00 +10:00
|
|
|
// The ownership model has the javascript engine owning all the socket data.
|
|
|
|
|
// Therefore we rely on the javascript engine to finalise all sockets.
|
|
|
|
|
// We just remove the listeners since they can hold references to the javascript socket objects which will
|
|
|
|
|
// prevent finalisation.
|
|
|
|
|
for (SocketDataBase* socket : _sockets)
|
2020-08-31 20:23:11 +01:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
if (socket != nullptr)
|
|
|
|
|
socket->_eventList.RemoveAllListeners();
|
2020-08-31 20:23:11 +01:00
|
|
|
}
|
2024-11-29 12:04:56 +09:00
|
|
|
#endif
|
2020-08-26 01:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 02:05:00 +10:00
|
|
|
std::string Scripting::Stringify(JSContext* ctx, const JSValue val)
|
2018-03-17 19:31:06 +00:00
|
|
|
{
|
2025-09-29 02:05:00 +10:00
|
|
|
return ExpressionStringifier::StringifyExpression(ctx, val);
|
2020-05-06 20:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 04:38:20 -03:00
|
|
|
bool Scripting::IsGameStateMutable()
|
2020-02-23 02:00:22 +00:00
|
|
|
{
|
2020-02-24 17:43:03 +00:00
|
|
|
// Allow single player to alter game state anywhere
|
2025-08-31 16:02:43 +02:00
|
|
|
if (Network::GetMode() == Network::Mode::none)
|
2020-02-24 17:43:03 +00:00
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2021-09-15 22:22:15 +02:00
|
|
|
|
|
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
|
|
|
|
auto& execInfo = scriptEngine.GetExecInfo();
|
|
|
|
|
return execInfo.IsGameStateMutable();
|
2020-02-23 02:00:22 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 04:38:20 -03:00
|
|
|
int32_t Scripting::GetTargetAPIVersion()
|
2021-08-04 20:58:02 +03:00
|
|
|
{
|
|
|
|
|
auto& scriptEngine = GetContext()->GetScriptEngine();
|
|
|
|
|
auto& execInfo = scriptEngine.GetExecInfo();
|
2021-08-28 17:43:11 +02:00
|
|
|
|
|
|
|
|
// Commands from the in-game console do not have a plug-in set
|
|
|
|
|
auto plugin = execInfo.GetCurrentPlugin();
|
|
|
|
|
if (plugin == nullptr)
|
|
|
|
|
{
|
|
|
|
|
// For in-game console, default to the current API version
|
2025-01-19 16:59:45 +01:00
|
|
|
return kPluginApiVersion;
|
2021-08-28 17:43:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return plugin->GetTargetAPIVersion();
|
2021-08-04 20:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
2020-02-23 12:55:48 +00:00
|
|
|
#endif
|