From 421530c3ea6bcc2d4cdce7b09973637a43c784d3 Mon Sep 17 00:00:00 2001 From: Michael Bernardi Date: Mon, 2 Mar 2026 00:48:18 +1100 Subject: [PATCH] Use class prototypes for all script types. --- .../scripting/ScGraphicsContext.hpp | 29 ++++---- src/openrct2-ui/scripting/ScImageManager.hpp | 12 +-- src/openrct2-ui/scripting/ScTileSelection.hpp | 12 +-- src/openrct2-ui/scripting/ScTitleSequence.hpp | 38 +++++----- src/openrct2-ui/scripting/ScUi.hpp | 68 ++++++++--------- src/openrct2-ui/scripting/ScViewport.hpp | 12 +-- src/openrct2-ui/scripting/ScWidget.hpp | 32 ++++---- src/openrct2-ui/scripting/ScWindow.cpp | 25 ++++++- src/openrct2-ui/scripting/ScWindow.h | 22 ------ src/openrct2/scripting/ScriptEngine.cpp | 74 ++++++++++++++----- src/openrct2/scripting/ScriptEngine.h | 36 ++++----- .../scripting/bindings/entity/ScEntity.cpp | 14 ++-- .../scripting/bindings/entity/ScGuest.cpp | 12 +-- .../scripting/bindings/entity/ScStaff.cpp | 12 +-- .../scripting/bindings/game/ScCheats.hpp | 12 +-- .../bindings/game/ScConfiguration.hpp | 19 +++-- .../scripting/bindings/game/ScConsole.hpp | 18 +++-- .../scripting/bindings/game/ScContext.hpp | 12 +-- .../scripting/bindings/game/ScDisposable.hpp | 11 ++- .../scripting/bindings/game/ScPlugin.hpp | 11 ++- .../scripting/bindings/game/ScProfiler.hpp | 13 ++-- .../scripting/bindings/network/ScNetwork.cpp | 8 +- .../scripting/bindings/network/ScNetwork.hpp | 5 +- .../scripting/bindings/network/ScPlayer.cpp | 12 +-- .../bindings/network/ScPlayerGroup.cpp | 12 +-- .../scripting/bindings/network/ScSocket.hpp | 40 +++++----- .../bindings/object/ScInstalledObject.hpp | 12 +-- .../scripting/bindings/object/ScObject.hpp | 36 ++++----- .../bindings/object/ScObjectManager.cpp | 12 +-- .../scripting/bindings/ride/ScRide.cpp | 12 +-- .../scripting/bindings/ride/ScRideStation.cpp | 12 +-- .../bindings/ride/ScTrackIterator.cpp | 12 +-- .../bindings/ride/ScTrackSegment.cpp | 12 +-- .../scripting/bindings/world/ScAward.cpp | 10 ++- .../scripting/bindings/world/ScAward.hpp | 7 -- .../scripting/bindings/world/ScDate.hpp | 13 ++-- .../scripting/bindings/world/ScMap.cpp | 12 +-- .../scripting/bindings/world/ScPark.cpp | 12 +-- .../bindings/world/ScParkMessage.cpp | 13 ++-- .../bindings/world/ScParkMessage.hpp | 2 +- .../scripting/bindings/world/ScResearch.cpp | 12 +-- .../scripting/bindings/world/ScScenario.hpp | 26 +++---- .../scripting/bindings/world/ScTile.cpp | 12 +-- .../bindings/world/ScTileElement.cpp | 12 +-- .../scripting/bindings/world/ScWeather.hpp | 28 ++++--- 45 files changed, 436 insertions(+), 402 deletions(-) diff --git a/src/openrct2-ui/scripting/ScGraphicsContext.hpp b/src/openrct2-ui/scripting/ScGraphicsContext.hpp index 3dc77b9f73..61ff36115f 100644 --- a/src/openrct2-ui/scripting/ScGraphicsContext.hpp +++ b/src/openrct2-ui/scripting/ScGraphicsContext.hpp @@ -42,18 +42,6 @@ namespace OpenRCT2::Scripting public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "GraphicsContext", Finalize); - } - - static void Finalize(JSRuntime* rt, JSValue thisVal) - { - GraphicsData* data = gScGraphicsContext.GetOpaque(thisVal); - if (data) - delete data; - } - - JSValue New(JSContext* ctx, const Drawing::RenderTarget& rt) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("colour", ScGraphicsContext::colour_get, ScGraphicsContext::colour_set), @@ -79,7 +67,19 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("text", 3, ScGraphicsContext::text), JS_CFUNC_DEF("well", 4, ScGraphicsContext::well), }; - return MakeWithOpaque(ctx, funcs, new GraphicsData{ rt }); + RegisterBase(ctx, "GraphicsContext", Finalize, funcs); + } + + static void Finalize(JSRuntime* rt, JSValue thisVal) + { + GraphicsData* data = gScGraphicsContext.GetOpaque(thisVal); + if (data) + delete data; + } + + JSValue New(JSContext* ctx, const Drawing::RenderTarget& rt) + { + return MakeWithOpaque(ctx, new GraphicsData{ rt }); } private: @@ -212,7 +212,8 @@ namespace OpenRCT2::Scripting GraphicsData* data = gScGraphicsContext.GetOpaque(thisVal); Drawing::Rectangle::fillInset( - data->_rt, { x, y, x + width - 1, y + height - 1 }, { static_cast(data->_colour.value_or(0)) }); + data->_rt, { x, y, x + width - 1, y + height - 1 }, + { static_cast(data->_colour.value_or(0)) }); return JS_UNDEFINED; } diff --git a/src/openrct2-ui/scripting/ScImageManager.hpp b/src/openrct2-ui/scripting/ScImageManager.hpp index 580eda46c5..26faf32d6f 100644 --- a/src/openrct2-ui/scripting/ScImageManager.hpp +++ b/src/openrct2-ui/scripting/ScImageManager.hpp @@ -26,11 +26,6 @@ namespace OpenRCT2::Scripting { public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "ImageManager"); - } - - JSValue New(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CFUNC_DEF("getPredefinedRange", 1, ScImageManager::getPredefinedRange), @@ -42,7 +37,12 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("setPixelData", 2, ScImageManager::setPixelData), JS_CFUNC_DEF("draw", 3, ScImageManager::draw) }; - return MakeWithOpaque(ctx, funcs, nullptr); + RegisterBase(ctx, "ImageManager", nullptr, funcs); + } + + JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); } private: diff --git a/src/openrct2-ui/scripting/ScTileSelection.hpp b/src/openrct2-ui/scripting/ScTileSelection.hpp index 98e05dbdab..6d8bb9c9a3 100644 --- a/src/openrct2-ui/scripting/ScTileSelection.hpp +++ b/src/openrct2-ui/scripting/ScTileSelection.hpp @@ -22,17 +22,17 @@ namespace OpenRCT2::Scripting { public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "TileSelection"); - } - - JSValue New(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("range", ScTileSelection::range_get, ScTileSelection::range_set), JS_CGETSET_DEF("tiles", ScTileSelection::tiles_get, ScTileSelection::tiles_set), }; - return MakeWithOpaque(ctx, funcs, nullptr); + RegisterBase(ctx, "TileSelection", nullptr, funcs); + } + + JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); } static JSValue range_get(JSContext* ctx, JSValue thisVal) diff --git a/src/openrct2-ui/scripting/ScTitleSequence.hpp b/src/openrct2-ui/scripting/ScTitleSequence.hpp index a66998f8a4..202f824410 100644 --- a/src/openrct2-ui/scripting/ScTitleSequence.hpp +++ b/src/openrct2-ui/scripting/ScTitleSequence.hpp @@ -322,19 +322,18 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx, std::string_view path, std::string_view fileName) + { + return MakeWithOpaque(ctx, new OpaqueData{ std::string(path), std::string(fileName) }); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("fileName", ScTitleSequencePark::fileName_get, ScTitleSequencePark::fileName_set), JS_CFUNC_DEF("delete", 0, ScTitleSequencePark::delete_), JS_CFUNC_DEF("load", 0, ScTitleSequencePark::load), }; - - return MakeWithOpaque(ctx, funcs, new OpaqueData{ std::string(path), std::string(fileName) }); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "TitleSequencePark", Finalize); + RegisterBase(ctx, "TitleSequencePark", Finalize, funcs); } private: @@ -615,6 +614,11 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx, std::string_view path) + { + return MakeWithOpaque(ctx, new OpaqueData{ std::string(path) }); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("name", ScTitleSequence::name_get, ScTitleSequence::name_set), @@ -634,13 +638,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("seek", 1, ScTitleSequence::seek), JS_CFUNC_DEF("stop", 0, ScTitleSequence::stop), }; - - return MakeWithOpaque(ctx, funcs, new OpaqueData{ std::string(path) }); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "TitleSequence", Finalize); + RegisterBase(ctx, "TitleSequence", Finalize, funcs); } private: @@ -712,16 +710,16 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx) { - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("titleSequences", ScTitleSequenceManager::titleSequences_get, nullptr), - JS_CFUNC_DEF("create", 1, ScTitleSequenceManager::create), - }; - return MakeWithOpaque(ctx, funcs, nullptr); + return MakeWithOpaque(ctx, nullptr); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "TitleSequenceManager"); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("titleSequences", ScTitleSequenceManager::titleSequences_get, nullptr), + JS_CFUNC_DEF("create", 1, ScTitleSequenceManager::create), + }; + RegisterBase(ctx, "TitleSequenceManager", nullptr, funcs); } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2-ui/scripting/ScUi.hpp b/src/openrct2-ui/scripting/ScUi.hpp index f53fbc2fa2..2bfecb3abc 100644 --- a/src/openrct2-ui/scripting/ScUi.hpp +++ b/src/openrct2-ui/scripting/ScUi.hpp @@ -89,21 +89,20 @@ namespace OpenRCT2::Scripting return JS_UNDEFINED; } - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("id", ScTool::id_get, nullptr), - JS_CGETSET_DEF("cursor", ScTool::cursor_get, nullptr), - JS_CFUNC_DEF("cancel", 0, ScTool::cancel), - }; - public: JSValue New(JSContext* ctx) { - return MakeWithOpaque(ctx, funcs, nullptr); + return MakeWithOpaque(ctx, nullptr); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Tool"); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("id", ScTool::id_get, nullptr), + JS_CGETSET_DEF("cursor", ScTool::cursor_get, nullptr), + JS_CFUNC_DEF("cancel", 0, ScTool::cancel), + }; + RegisterBase(ctx, "Tool", nullptr, funcs); } }; @@ -379,7 +378,6 @@ namespace OpenRCT2::Scripting auto& execInfo = scriptEngine->GetExecInfo(); auto owner = execInfo.GetCurrentPlugin(); std::string text = JSToStdString(ctx, argv[0]); - assert(owner->GetContext() == ctx); CustomMenuItems.emplace_back(owner, CustomToolbarMenuItemKind::Standard, text, JSCallback(ctx, argv[1])); std::ranges::sort(CustomMenuItems, [](auto&& a, auto&& b) { return a.Text < b.Text; }); @@ -445,38 +443,42 @@ namespace OpenRCT2::Scripting return JS_UNDEFINED; } - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("height", ScUi::height_get, nullptr), - JS_CGETSET_DEF("width", ScUi::width_get, nullptr), - JS_CGETSET_DEF("windows", ScUi::windows_get, nullptr), - JS_CGETSET_DEF("mainViewport", ScUi::mainViewport_get, nullptr), - JS_CGETSET_DEF("tileSelection", ScUi::tileSelection_get, nullptr), - JS_CGETSET_DEF("tool", ScUi::tool_get, nullptr), - JS_CGETSET_DEF("imageManager", ScUi::imageManager_get, nullptr), - - JS_CFUNC_DEF("openWindow", 1, ScUi::openWindow), - JS_CFUNC_DEF("closeWindows", 2, ScUi::closeWindows), - JS_CFUNC_DEF("closeAllWindows", 0, ScUi::closeAllWindows), - JS_CFUNC_DEF("getWindow", 1, ScUi::getWindow), - JS_CFUNC_DEF("showError", 2, ScUi::showError), - JS_CFUNC_DEF("showTextInput", 1, ScUi::showTextInput), - JS_CFUNC_DEF("showFileBrowse", 1, ScUi::showFileBrowse), - JS_CFUNC_DEF("showScenarioSelect", 1, ScUi::showScenarioSelect), - JS_CFUNC_DEF("activateTool", 1, ScUi::activateTool), - JS_CFUNC_DEF("registerMenuItem", 2, ScUi::registerMenuItem), - JS_CFUNC_DEF("registerToolboxMenuItem", 2, ScUi::registerToolboxMenuItem), - JS_CFUNC_DEF("registerShortcut", 1, ScUi::registerShortcut), - }; + static void Finalize(JSRuntime* rt, JSValue thisVal) + { + // Do nothing as we don't need to free the script engine. + } public: JSValue New(JSContext* ctx, ScriptEngine* scriptEngine) { - return MakeWithOpaque(ctx, funcs, scriptEngine); + return MakeWithOpaque(ctx, scriptEngine); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Ui"); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("height", ScUi::height_get, nullptr), + JS_CGETSET_DEF("width", ScUi::width_get, nullptr), + JS_CGETSET_DEF("windows", ScUi::windows_get, nullptr), + JS_CGETSET_DEF("mainViewport", ScUi::mainViewport_get, nullptr), + JS_CGETSET_DEF("tileSelection", ScUi::tileSelection_get, nullptr), + JS_CGETSET_DEF("tool", ScUi::tool_get, nullptr), + JS_CGETSET_DEF("imageManager", ScUi::imageManager_get, nullptr), + + JS_CFUNC_DEF("openWindow", 1, ScUi::openWindow), + JS_CFUNC_DEF("closeWindows", 2, ScUi::closeWindows), + JS_CFUNC_DEF("closeAllWindows", 0, ScUi::closeAllWindows), + JS_CFUNC_DEF("getWindow", 1, ScUi::getWindow), + JS_CFUNC_DEF("showError", 2, ScUi::showError), + JS_CFUNC_DEF("showTextInput", 1, ScUi::showTextInput), + JS_CFUNC_DEF("showFileBrowse", 1, ScUi::showFileBrowse), + JS_CFUNC_DEF("showScenarioSelect", 1, ScUi::showScenarioSelect), + JS_CFUNC_DEF("activateTool", 1, ScUi::activateTool), + JS_CFUNC_DEF("registerMenuItem", 2, ScUi::registerMenuItem), + JS_CFUNC_DEF("registerToolboxMenuItem", 2, ScUi::registerToolboxMenuItem), + JS_CFUNC_DEF("registerShortcut", 1, ScUi::registerShortcut), + }; + RegisterBase(ctx, "Ui", Finalize, funcs); } private: diff --git a/src/openrct2-ui/scripting/ScViewport.hpp b/src/openrct2-ui/scripting/ScViewport.hpp index a51c9d9726..d826ea4097 100644 --- a/src/openrct2-ui/scripting/ScViewport.hpp +++ b/src/openrct2-ui/scripting/ScViewport.hpp @@ -251,6 +251,11 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx, WindowClass wClass, WindowNumber wNum = 0) + { + return MakeWithOpaque(ctx, new OpaqueWindowData{ wClass, wNum }); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("left", ScViewport::left_get, ScViewport::left_set), @@ -264,12 +269,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("moveTo", 1, ScViewport::moveTo), JS_CFUNC_DEF("scrollTo", 1, ScViewport::scrollTo), }; - return MakeWithOpaque(ctx, funcs, new OpaqueWindowData{ wClass, wNum }); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Viewport", Finalize); + RegisterBase(ctx, "Viewport", Finalize, funcs); } static void Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2-ui/scripting/ScWidget.hpp b/src/openrct2-ui/scripting/ScWidget.hpp index e29fd36679..149b9a7ce1 100644 --- a/src/openrct2-ui/scripting/ScWidget.hpp +++ b/src/openrct2-ui/scripting/ScWidget.hpp @@ -440,26 +440,24 @@ namespace OpenRCT2::Scripting return JS_UNDEFINED; } - private: - static constexpr JSCFunctionListEntry _funcs[] = { - JS_CGETSET_DEF("window", ScWidget::window_get, nullptr), - JS_CGETSET_DEF("name", ScWidget::name_get, ScWidget::name_set), - JS_CGETSET_DEF("type", ScWidget::type_get, nullptr), - JS_CGETSET_DEF("x", ScWidget::x_get, ScWidget::x_set), - JS_CGETSET_DEF("y", ScWidget::y_get, ScWidget::y_set), - JS_CGETSET_DEF("width", ScWidget::width_get, ScWidget::width_set), - JS_CGETSET_DEF("height", ScWidget::height_get, &ScWidget::height_set), - JS_CGETSET_DEF("tooltip", ScWidget::tooltip_get, &ScWidget::tooltip_set), - JS_CGETSET_DEF("isDisabled", ScWidget::isDisabled_get, &ScWidget::isDisabled_set), - JS_CGETSET_DEF("isVisible", ScWidget::isVisible_get, &ScWidget::isVisible_set), - }; - public: JSValue New(JSContext* ctx, WindowBase* w, WidgetIndex widgetIndex); void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Widget", Finalize); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("window", ScWidget::window_get, nullptr), + JS_CGETSET_DEF("name", ScWidget::name_get, ScWidget::name_set), + JS_CGETSET_DEF("type", ScWidget::type_get, nullptr), + JS_CGETSET_DEF("x", ScWidget::x_get, ScWidget::x_set), + JS_CGETSET_DEF("y", ScWidget::y_get, ScWidget::y_set), + JS_CGETSET_DEF("width", ScWidget::width_get, ScWidget::width_set), + JS_CGETSET_DEF("height", ScWidget::height_get, &ScWidget::height_set), + JS_CGETSET_DEF("tooltip", ScWidget::tooltip_get, &ScWidget::tooltip_set), + JS_CGETSET_DEF("isDisabled", ScWidget::isDisabled_get, &ScWidget::isDisabled_set), + JS_CGETSET_DEF("isVisible", ScWidget::isVisible_get, &ScWidget::isVisible_set), + }; + RegisterBase(ctx, "Widget", Finalize, funcs); } private: @@ -1133,7 +1131,9 @@ namespace OpenRCT2::Scripting inline JSValue ScWidget::New(JSContext* ctx, WindowBase* w, WidgetIndex widgetIndex) { - JSValue newObj = MakeWithOpaque(ctx, _funcs, new WidgetData{ w->classification, w->number, widgetIndex }); + JSValue newObj = MakeWithOpaque(ctx, new WidgetData{ w->classification, w->number, widgetIndex }); + // TODO: Adding these functions like this in New() is probably slower than creating a proto for each type in Register() + // and using that. switch (w->widgets[widgetIndex].type) { case WidgetType::button: diff --git a/src/openrct2-ui/scripting/ScWindow.cpp b/src/openrct2-ui/scripting/ScWindow.cpp index 71587969c4..306cad4625 100644 --- a/src/openrct2-ui/scripting/ScWindow.cpp +++ b/src/openrct2-ui/scripting/ScWindow.cpp @@ -378,12 +378,33 @@ namespace OpenRCT2::Scripting JSValue ScWindow::New(JSContext* ctx, WindowClass wClass, WindowNumber wNum) { - return MakeWithOpaque(ctx, funcs, new OpaqueWindowData{ wClass, wNum }); + return MakeWithOpaque(ctx, new OpaqueWindowData{ wClass, wNum }); } void ScWindow::Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Window", Finalize); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("classification", ScWindow::classification_get, nullptr), + JS_CGETSET_DEF("number", ScWindow::number_get, nullptr), + JS_CGETSET_DEF("x", ScWindow::x_get, ScWindow::x_set), + JS_CGETSET_DEF("y", ScWindow::y_get, ScWindow::y_set), + JS_CGETSET_DEF("width", ScWindow::width_get, ScWindow::width_set), + JS_CGETSET_DEF("height", ScWindow::height_get, ScWindow::height_set), + JS_CGETSET_DEF("minWidth", ScWindow::minWidth_get, ScWindow::minWidth_set), + JS_CGETSET_DEF("maxWidth", ScWindow::maxWidth_get, ScWindow::maxWidth_set), + JS_CGETSET_DEF("minHeight", ScWindow::minHeight_get, ScWindow::minHeight_set), + JS_CGETSET_DEF("maxHeight", ScWindow::maxHeight_get, ScWindow::maxHeight_set), + JS_CGETSET_DEF("isSticky", ScWindow::isSticky_get, nullptr), + JS_CGETSET_DEF("widgets", ScWindow::widgets_get, nullptr), + JS_CGETSET_DEF("colours", ScWindow::colours_get, ScWindow::colours_set), + JS_CGETSET_DEF("title", ScWindow::title_get, ScWindow::title_set), + JS_CGETSET_DEF("tabIndex", ScWindow::tabIndex_get, ScWindow::tabIndex_set), + + JS_CFUNC_DEF("close", 0, ScWindow::close), + JS_CFUNC_DEF("findWidget", 1, ScWindow::findWidget), + JS_CFUNC_DEF("bringToFront", 0, ScWindow::bringToFront) + }; + RegisterBase(ctx, "Window", Finalize, funcs); } void ScWindow::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2-ui/scripting/ScWindow.h b/src/openrct2-ui/scripting/ScWindow.h index 6fcf4cb964..6f05805198 100644 --- a/src/openrct2-ui/scripting/ScWindow.h +++ b/src/openrct2-ui/scripting/ScWindow.h @@ -76,28 +76,6 @@ namespace OpenRCT2::Scripting void Register(JSContext* ctx); - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("classification", ScWindow::classification_get, nullptr), - JS_CGETSET_DEF("number", ScWindow::number_get, nullptr), - JS_CGETSET_DEF("x", ScWindow::x_get, ScWindow::x_set), - JS_CGETSET_DEF("y", ScWindow::y_get, ScWindow::y_set), - JS_CGETSET_DEF("width", ScWindow::width_get, ScWindow::width_set), - JS_CGETSET_DEF("height", ScWindow::height_get, ScWindow::height_set), - JS_CGETSET_DEF("minWidth", ScWindow::minWidth_get, ScWindow::minWidth_set), - JS_CGETSET_DEF("maxWidth", ScWindow::maxWidth_get, ScWindow::maxWidth_set), - JS_CGETSET_DEF("minHeight", ScWindow::minHeight_get, ScWindow::minHeight_set), - JS_CGETSET_DEF("maxHeight", ScWindow::maxHeight_get, ScWindow::maxHeight_set), - JS_CGETSET_DEF("isSticky", ScWindow::isSticky_get, nullptr), - JS_CGETSET_DEF("widgets", ScWindow::widgets_get, nullptr), - JS_CGETSET_DEF("colours", ScWindow::colours_get, ScWindow::colours_set), - JS_CGETSET_DEF("title", ScWindow::title_get, ScWindow::title_set), - JS_CGETSET_DEF("tabIndex", ScWindow::tabIndex_get, ScWindow::tabIndex_set), - - JS_CFUNC_DEF("close", 0, ScWindow::close), - JS_CFUNC_DEF("findWidget", 1, ScWindow::findWidget), - JS_CFUNC_DEF("bringToFront", 0, ScWindow::bringToFront) - }; - private: static void Finalize(JSRuntime* rt, JSValue thisVal); diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index f436307697..59f8165c1a 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -28,8 +28,8 @@ #include "../core/Path.hpp" #include "../interface/InteractiveConsole.h" #include "../platform/Platform.h" - #include "../ride/ted/PitchAndRoll.h" #include "../profiling/Profiling.h" + #include "../ride/ted/PitchAndRoll.h" #include "bindings/entity/ScBalloon.hpp" #include "bindings/entity/ScEntity.hpp" #include "bindings/entity/ScGuest.hpp" @@ -231,19 +231,56 @@ private: void StringifyObject(const JSValue val, bool canStartWithNewLine, int32_t nestLevel) { - JSPropertyEnum* props; - uint32_t propsLen; - JS_GetOwnPropertyNames(_context, &props, &propsLen, val, JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK | JS_GPN_PRIVATE_MASK); + std::vector propsVec; - if (propsLen == 0) + // To correctly get all properties of an object, we must walk up the stack of prototypes until we get to the Object + // prototype. This is because JS_GetOwnPropertyNames only returns the "own" properties, not inherited ones. + JSValue blankValue = JS_NewObject(_context); + JSValue objProto = JS_GetPrototype(_context, blankValue); + JS_FreeValue(_context, blankValue); + JSValue propsObj = JS_DupValue(_context, val); + while (true) + { + JSValue nextPropsObj = JS_GetPrototype(_context, propsObj); + if (JS_IsNull(nextPropsObj)) + { + // The prototype of the Object prototype is null. + JS_FreeValue(_context, nextPropsObj); + break; + } + + JSPropertyEnum* props; + uint32_t propsLen; + if (JS_GetOwnPropertyNames( + _context, &props, &propsLen, propsObj, JS_GPN_STRING_MASK | JS_GPN_SYMBOL_MASK | JS_GPN_PRIVATE_MASK) + == -1) + { + JS_FreeValue(_context, nextPropsObj); + break; + } + propsVec.reserve(propsLen); + for (uint32_t i = 0; i < propsLen; i++) + { + JS_DupAtom(_context, props[i].atom); + propsVec.push_back(props[i].atom); + } + JS_FreePropertyEnum(_context, props, propsLen); + + JS_FreeValue(_context, propsObj); + propsObj = nextPropsObj; + } + JS_FreeValue(_context, propsObj); + JS_FreeValue(_context, objProto); + + if (propsVec.size() == 0) { _ss << "{}"; } - else if (propsLen == 1) + else if (propsVec.size() == 1) { _ss << "{ "; - const char* key = JS_AtomToCString(_context, props[0].atom); + const char* key = JS_AtomToCString(_context, propsVec[0]); if (key) { _ss << key << ": "; @@ -255,7 +292,7 @@ private: _ss << "?: "; } - JSValue prop = JS_GetProperty(_context, val, props[0].atom); + JSValue prop = JS_GetProperty(_context, val, propsVec[0]); Stringify(prop, true, nestLevel + 1); JS_FreeValue(_context, prop); @@ -272,14 +309,14 @@ private: _ss << "{ "; PushIndent(2); - for (uint32_t i = 0; i < propsLen; i++) + for (uint32_t i = 0; i < propsVec.size(); i++) { if (i != 0) { _ss << ","; LineFeed(); } - const char* key = JS_AtomToCString(_context, props[i].atom); + const char* key = JS_AtomToCString(_context, propsVec[i]); if (key) { _ss << key << ": "; @@ -291,7 +328,7 @@ private: _ss << "?: "; } - JSValue prop = JS_GetProperty(_context, val, props[i].atom); + JSValue prop = JS_GetProperty(_context, val, propsVec[i]); Stringify(prop, true, nestLevel + 1); JS_FreeValue(_context, prop); } @@ -305,7 +342,10 @@ private: } } - JS_FreePropertyEnum(_context, props, propsLen); + for (size_t i = 0; i < propsVec.size(); i++) + { + JS_FreeAtom(_context, propsVec[i]); + } } void StringifyFunction(const JSValue val) @@ -637,16 +677,16 @@ ScriptEngine::~ScriptEngine() JS_FreeContext(_replContext); _replContext = nullptr; } - if (_runtime) - { - JS_FreeRuntime(_runtime); - _runtime = nullptr; - } for (const ExtensionCallbacks& ext : _extensions) { ext.unregister(); } UnregisterClasses(); + if (_runtime) + { + JS_FreeRuntime(_runtime); + _runtime = nullptr; + } } class ConstantBuilder diff --git a/src/openrct2/scripting/ScriptEngine.h b/src/openrct2/scripting/ScriptEngine.h index bee5128035..1aea8d15b5 100644 --- a/src/openrct2/scripting/ScriptEngine.h +++ b/src/openrct2/scripting/ScriptEngine.h @@ -314,26 +314,30 @@ namespace OpenRCT2::Scripting class ScBase { private: + JSValue proto = JS_NULL; + JSContext* protoCtx = nullptr; JSClassID classId = JS_INVALID_CLASS_ID; + bool hasFinalizer = false; public: void Unregister() { classId = JS_INVALID_CLASS_ID; + if (protoCtx != nullptr) + JS_FreeValue(protoCtx, proto); } protected: - [[nodiscard]] JSValue MakeWithOpaque(JSContext* ctx, std::span classFuncs, void* opaque) + [[nodiscard]] JSValue MakeWithOpaque(JSContext* ctx, void* opaque) const { + // If you fail this assert you probably need a finalizer to free the opaque ptr. + assert(opaque == nullptr || hasFinalizer); + // TODO: the class proto could be set once per context rather than here on every object creation. + JS_SetClassProto(ctx, classId, JS_DupValue(ctx, proto)); JSValue obj = JS_NewObjectClass(ctx, classId); if (JS_IsException(obj)) throw std::runtime_error("Failed to create new object for class."); JS_SetOpaque(obj, opaque); - - // Note: Usually one would set a class prototype rather than setting the functions as properties on every creation. - // However, that causes the attached functions to not be "own properties" which make them a little less - // visible to the user, and also does not match the previous behaviour with the DukTape engine. - JS_SetPropertyFunctionList(ctx, obj, classFuncs.data(), static_cast(classFuncs.size())); return obj; } @@ -343,9 +347,11 @@ namespace OpenRCT2::Scripting return static_cast(JS_GetOpaque(obj, classId)); } - void RegisterBase(JSContext* ctx, const JSClassDef& classDef) + void RegisterBase( + JSContext* ctx, const char* className, JSClassFinalizer* finalizer, + std::span classFuncs) { - Guard::Assert(classId == JS_INVALID_CLASS_ID); + assert(classId == JS_INVALID_CLASS_ID); // Note: Technically JS_NewClassID is meant to be called once during the lifetime of the program // whereas JS_NewClass is meant to be called for each runtime. // If we ever have any more runtimes, this flow would be wrong. @@ -353,18 +359,14 @@ namespace OpenRCT2::Scripting // calling callbacks. JSRuntime* rt = JS_GetRuntime(ctx); JS_NewClassID(rt, &classId); + JSClassDef classDef = { className, finalizer, nullptr, nullptr, nullptr }; JS_NewClass(rt, classId, &classDef); - JS_SetClassProto(ctx, classId, JS_NewObject(ctx)); - } - void RegisterBaseStr(JSContext* ctx, const char* className) - { - RegisterBase(ctx, { className, nullptr, nullptr, nullptr, nullptr }); - } + proto = JS_NewObject(ctx); + protoCtx = ctx; + JS_SetPropertyFunctionList(ctx, proto, classFuncs.data(), static_cast(classFuncs.size())); - void RegisterBaseStr(JSContext* ctx, const char* className, JSClassFinalizer* finalizer) - { - RegisterBase(ctx, { className, finalizer, nullptr, nullptr, nullptr }); + hasFinalizer = finalizer != nullptr; } }; diff --git a/src/openrct2/scripting/bindings/entity/ScEntity.cpp b/src/openrct2/scripting/bindings/entity/ScEntity.cpp index 2b90003cc1..1a9a96e334 100644 --- a/src/openrct2/scripting/bindings/entity/ScEntity.cpp +++ b/src/openrct2/scripting/bindings/entity/ScEntity.cpp @@ -204,12 +204,7 @@ namespace OpenRCT2::Scripting JSValue ScEntity::NewInstance(JSContext* ctx, EntityId entityId) { - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("id", &ScEntity::id_get, nullptr), JS_CGETSET_DEF("type", &ScEntity::type_get, nullptr), - JS_CGETSET_DEF("x", &ScEntity::x_get, &ScEntity::x_set), JS_CGETSET_DEF("y", &ScEntity::y_get, &ScEntity::y_set), - JS_CGETSET_DEF("z", &ScEntity::z_get, &ScEntity::z_set), JS_CFUNC_DEF("remove", 0, &ScEntity::remove) - }; - return MakeWithOpaque(ctx, funcs, new OpaqueEntityData{ entityId }); + return MakeWithOpaque(ctx, new OpaqueEntityData{ entityId }); } // this one exists as a hack to make a template work in ScMap @@ -220,7 +215,12 @@ namespace OpenRCT2::Scripting void ScEntity::Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Entity", Finalize); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("id", &ScEntity::id_get, nullptr), JS_CGETSET_DEF("type", &ScEntity::type_get, nullptr), + JS_CGETSET_DEF("x", &ScEntity::x_get, &ScEntity::x_set), JS_CGETSET_DEF("y", &ScEntity::y_get, &ScEntity::y_set), + JS_CGETSET_DEF("z", &ScEntity::z_get, &ScEntity::z_set), JS_CFUNC_DEF("remove", 0, &ScEntity::remove) + }; + RegisterBase(ctx, "Entity", Finalize, funcs); } void ScEntity::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/entity/ScGuest.cpp b/src/openrct2/scripting/bindings/entity/ScGuest.cpp index a16de88e93..d30275c3f0 100644 --- a/src/openrct2/scripting/bindings/entity/ScGuest.cpp +++ b/src/openrct2/scripting/bindings/entity/ScGuest.cpp @@ -1054,6 +1054,11 @@ namespace OpenRCT2::Scripting }; JSValue ScThought::New(JSContext* ctx, PeepThought thought) + { + return MakeWithOpaque(ctx, new OpaqueThoughtData{ thought }); + } + + void ScThought::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("type", &ScThought::type_get, nullptr), @@ -1062,12 +1067,7 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("freshTimeout", &ScThought::freshTimeout_get, nullptr), JS_CFUNC_DEF("toString", 0, &ScThought::toString), }; - return MakeWithOpaque(ctx, funcs, new OpaqueThoughtData{ thought }); - } - - void ScThought::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Thought", Finalize); + RegisterBase(ctx, "Thought", Finalize, funcs); } void ScThought::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/entity/ScStaff.cpp b/src/openrct2/scripting/bindings/entity/ScStaff.cpp index 5f5fdd1331..e317a99024 100644 --- a/src/openrct2/scripting/bindings/entity/ScStaff.cpp +++ b/src/openrct2/scripting/bindings/entity/ScStaff.cpp @@ -579,6 +579,11 @@ namespace OpenRCT2::Scripting }; JSValue ScPatrolArea::New(JSContext* ctx, EntityId staffId) + { + return MakeWithOpaque(ctx, new OpaquePatrolAreaData{ staffId }); + } + + void ScPatrolArea::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("tiles", &ScPatrolArea::tiles_get, &ScPatrolArea::tiles_set), @@ -587,12 +592,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("remove", 1, &ScPatrolArea::remove), JS_CFUNC_DEF("contains", 1, &ScPatrolArea::contains), }; - return MakeWithOpaque(ctx, funcs, new OpaquePatrolAreaData{ staffId }); - } - - void ScPatrolArea::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "PatrolArea", Finalize); + RegisterBase(ctx, "PatrolArea", Finalize, funcs); } void ScPatrolArea::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/game/ScCheats.hpp b/src/openrct2/scripting/bindings/game/ScCheats.hpp index 64bddd6bd1..02c01adb53 100644 --- a/src/openrct2/scripting/bindings/game/ScCheats.hpp +++ b/src/openrct2/scripting/bindings/game/ScCheats.hpp @@ -25,6 +25,11 @@ namespace OpenRCT2::Scripting { public: JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF( @@ -77,12 +82,7 @@ namespace OpenRCT2::Scripting &ScCheats::showVehiclesFromOtherTrackTypes_set), JS_CGETSET_DEF("makeAllDestructible", &ScCheats::makeAllDestructible_get, &ScCheats::makeAllDestructible_set) }; - return MakeWithOpaque(ctx, funcs, nullptr); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Cheats"); + RegisterBase(ctx, "Cheats", nullptr, funcs); } private: diff --git a/src/openrct2/scripting/bindings/game/ScConfiguration.hpp b/src/openrct2/scripting/bindings/game/ScConfiguration.hpp index c7ac78a4a0..272434ce33 100644 --- a/src/openrct2/scripting/bindings/game/ScConfiguration.hpp +++ b/src/openrct2/scripting/bindings/game/ScConfiguration.hpp @@ -341,29 +341,28 @@ namespace OpenRCT2::Scripting return JS_NewBool(ctx, retval); } - static constexpr JSCFunctionListEntry funcs[] = { - JS_CFUNC_DEF("getAll", 1, ScConfiguration::getAll), - JS_CFUNC_DEF("get", 2, ScConfiguration::get), - JS_CFUNC_DEF("set", 2, ScConfiguration::set), - JS_CFUNC_DEF("has", 1, ScConfiguration::has), - }; - public: // context.configuration JSValue New(JSContext* ctx) { - return MakeWithOpaque(ctx, funcs, new ConfigurationData{ ScConfigurationKind::User, {} }); + return MakeWithOpaque(ctx, new ConfigurationData{ ScConfigurationKind::User, {} }); } // context.sharedStorage / context.getParkStorage JSValue New(JSContext* ctx, ScConfigurationKind kind, std::string_view pluginName = {}) { - return MakeWithOpaque(ctx, funcs, new ConfigurationData{ kind, std::string(pluginName) }); + return MakeWithOpaque(ctx, new ConfigurationData{ kind, std::string(pluginName) }); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Configuration", Finalize); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CFUNC_DEF("getAll", 1, ScConfiguration::getAll), + JS_CFUNC_DEF("get", 2, ScConfiguration::get), + JS_CFUNC_DEF("set", 2, ScConfiguration::set), + JS_CFUNC_DEF("has", 1, ScConfiguration::has), + }; + RegisterBase(ctx, "Configuration", Finalize, funcs); } static void Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/game/ScConsole.hpp b/src/openrct2/scripting/bindings/game/ScConsole.hpp index 267b0a171e..262dfbbc0a 100644 --- a/src/openrct2/scripting/bindings/game/ScConsole.hpp +++ b/src/openrct2/scripting/bindings/game/ScConsole.hpp @@ -55,21 +55,25 @@ namespace OpenRCT2::Scripting return JS_UNDEFINED; } - static constexpr JSCFunctionListEntry funcs[] = { - JS_CFUNC_DEF("clear", 0, clear), - JS_CFUNC_DEF("log", 0, log), - JS_CFUNC_DEF("executeLegacy", 1, executeLegacy), - }; + static void Finalize(JSRuntime* ctx, JSValue thisVal) + { + // Do nothing as we don't need to free the console object. + } public: JSValue New(JSContext* ctx, InteractiveConsole& console) { - return MakeWithOpaque(ctx, funcs, &console); + return MakeWithOpaque(ctx, &console); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Console"); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CFUNC_DEF("clear", 0, clear), + JS_CFUNC_DEF("log", 0, log), + JS_CFUNC_DEF("executeLegacy", 1, executeLegacy), + }; + RegisterBase(ctx, "Console", Finalize, funcs); } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/game/ScContext.hpp b/src/openrct2/scripting/bindings/game/ScContext.hpp index 37f36a80ca..8c01c1a6c2 100644 --- a/src/openrct2/scripting/bindings/game/ScContext.hpp +++ b/src/openrct2/scripting/bindings/game/ScContext.hpp @@ -420,11 +420,6 @@ namespace OpenRCT2::Scripting public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Context"); - } - - JSValue New(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("apiVersion", ScContext::apiVersion_get, nullptr), @@ -450,7 +445,12 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("clearTimeout", 1, ScContext::clearTimeout), JS_CFUNC_DEF("getIcon", 1, ScContext::getIcon), }; - return MakeWithOpaque(ctx, funcs, nullptr); + RegisterBase(ctx, "Context", nullptr, funcs); + } + + JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/game/ScDisposable.hpp b/src/openrct2/scripting/bindings/game/ScDisposable.hpp index ab1cef7b19..8d1e00ed26 100644 --- a/src/openrct2/scripting/bindings/game/ScDisposable.hpp +++ b/src/openrct2/scripting/bindings/game/ScDisposable.hpp @@ -35,16 +35,15 @@ namespace OpenRCT2::Scripting JSValue New(JSContext* ctx, const std::function& f) { - static constexpr JSCFunctionListEntry funcs[] = { - JS_CFUNC_DEF("dispose", 0, ScDisposable::dispose), - }; - - return MakeWithOpaque(ctx, funcs, f ? new OpaqueType(f) : nullptr); + return MakeWithOpaque(ctx, f ? new OpaqueType(f) : nullptr); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Disposable", Finalize); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CFUNC_DEF("dispose", 0, ScDisposable::dispose), + }; + RegisterBase(ctx, "Disposable", Finalize, funcs); } static void Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/game/ScPlugin.hpp b/src/openrct2/scripting/bindings/game/ScPlugin.hpp index c05c6e07d3..56ef8d2d70 100644 --- a/src/openrct2/scripting/bindings/game/ScPlugin.hpp +++ b/src/openrct2/scripting/bindings/game/ScPlugin.hpp @@ -68,16 +68,15 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx) { - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("plugins", ScPlugin::plugins_get, nullptr), - }; - - return MakeWithOpaque(ctx, funcs, nullptr); + return MakeWithOpaque(ctx, nullptr); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "PluginManager"); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("plugins", ScPlugin::plugins_get, nullptr), + }; + RegisterBase(ctx, "PluginManager", nullptr, funcs); } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/game/ScProfiler.hpp b/src/openrct2/scripting/bindings/game/ScProfiler.hpp index c4f336fdc8..ae31847324 100644 --- a/src/openrct2/scripting/bindings/game/ScProfiler.hpp +++ b/src/openrct2/scripting/bindings/game/ScProfiler.hpp @@ -83,6 +83,11 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CFUNC_DEF("getData", 0, ScProfiler::getData), @@ -91,13 +96,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("reset", 0, ScProfiler::reset), JS_CGETSET_DEF("enabled", ScProfiler::enabled_get, nullptr), }; - - return MakeWithOpaque(ctx, funcs, nullptr); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Profiler"); + RegisterBase(ctx, "Profiler", nullptr, funcs); } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/network/ScNetwork.cpp b/src/openrct2/scripting/bindings/network/ScNetwork.cpp index c84a88ac72..4b95df3653 100644 --- a/src/openrct2/scripting/bindings/network/ScNetwork.cpp +++ b/src/openrct2/scripting/bindings/network/ScNetwork.cpp @@ -322,6 +322,11 @@ namespace OpenRCT2::Scripting #endif JSValue ScNetwork::New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } + + void ScNetwork::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("mode", ScNetwork::mode_get, nullptr), @@ -343,8 +348,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("createListener", 0, ScNetwork::createListener), JS_CFUNC_DEF("createSocket", 0, ScNetwork::createSocket) }; - - return MakeWithOpaque(ctx, funcs, nullptr); + RegisterBase(ctx, "Network", nullptr, funcs); } } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/network/ScNetwork.hpp b/src/openrct2/scripting/bindings/network/ScNetwork.hpp index 236ef49684..e22eb19332 100644 --- a/src/openrct2/scripting/bindings/network/ScNetwork.hpp +++ b/src/openrct2/scripting/bindings/network/ScNetwork.hpp @@ -55,10 +55,7 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx); - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Network"); - } + void Register(JSContext* ctx); }; extern ScNetwork gScNetwork; diff --git a/src/openrct2/scripting/bindings/network/ScPlayer.cpp b/src/openrct2/scripting/bindings/network/ScPlayer.cpp index b5bc13fd8e..d4da8762ce 100644 --- a/src/openrct2/scripting/bindings/network/ScPlayer.cpp +++ b/src/openrct2/scripting/bindings/network/ScPlayer.cpp @@ -118,6 +118,11 @@ namespace OpenRCT2::Scripting } JSValue ScPlayer::New(JSContext* ctx, int32_t id) + { + return MakeWithOpaque(ctx, new OpaquePlayerData{ id }); + } + + void ScPlayer::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("id", ScPlayer::id_get, nullptr), @@ -129,12 +134,7 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("ipAddress", ScPlayer::ipAddress_get, nullptr), JS_CGETSET_DEF("publicKeyHash", ScPlayer::publicKeyHash_get, nullptr), }; - return MakeWithOpaque(ctx, funcs, new OpaquePlayerData{ id }); - } - - void ScPlayer::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Player", Finalize); + RegisterBase(ctx, "Player", Finalize, funcs); } void ScPlayer::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp b/src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp index 9dbf39239d..68c88bd5e2 100644 --- a/src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp +++ b/src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp @@ -154,18 +154,18 @@ namespace OpenRCT2::Scripting } JSValue ScPlayerGroup::New(JSContext* ctx, int32_t id) + { + return MakeWithOpaque(ctx, new OpaquePlayerGroupData{ id }); + } + + void ScPlayerGroup::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("id", ScPlayerGroup::id_get, nullptr), JS_CGETSET_DEF("name", ScPlayerGroup::name_get, ScPlayerGroup::name_set), JS_CGETSET_DEF("permissions", ScPlayerGroup::permissions_get, ScPlayerGroup::permissions_set), }; - return MakeWithOpaque(ctx, funcs, new OpaquePlayerGroupData{ id }); - } - - void ScPlayerGroup::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "PlayerGroup", Finalize); + RegisterBase(ctx, "PlayerGroup", Finalize, funcs); } void ScPlayerGroup::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/network/ScSocket.hpp b/src/openrct2/scripting/bindings/network/ScSocket.hpp index 17f65014ef..3474fbd01e 100644 --- a/src/openrct2/scripting/bindings/network/ScSocket.hpp +++ b/src/openrct2/scripting/bindings/network/ScSocket.hpp @@ -405,20 +405,13 @@ namespace OpenRCT2::Scripting } public: - static constexpr JSCFunctionListEntry funcs[] = { - JS_CFUNC_DEF("destroy", 1, ScSocket::destroy), JS_CFUNC_DEF("setNoDelay", 1, ScSocket::setNoDelay), - JS_CFUNC_DEF("connect", 3, ScSocket::connect), JS_CFUNC_DEF("end", 1, ScSocket::end), - JS_CFUNC_DEF("write", 1, ScSocket::write), JS_CFUNC_DEF("on", 2, ScSocket::on), - JS_CFUNC_DEF("off", 2, ScSocket::off), - }; - JSValue New(JSContext* ctx, const std::shared_ptr& plugin) { // unique ptr is used to avoid static analyzer false positives. auto data = std::make_unique(); data->_plugin = plugin; GetContext()->GetScriptEngine().AddSocket(data.get()); - return MakeWithOpaque(ctx, funcs, data.release()); + return MakeWithOpaque(ctx, data.release()); } JSValue New(JSContext* ctx, const std::shared_ptr& plugin, std::unique_ptr&& socket) @@ -427,12 +420,18 @@ namespace OpenRCT2::Scripting data->_plugin = plugin; data->_socket = std::move(socket); GetContext()->GetScriptEngine().AddSocket(data.get()); - return MakeWithOpaque(ctx, funcs, data.release()); + return MakeWithOpaque(ctx, data.release()); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Socket", Finalize); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CFUNC_DEF("destroy", 1, ScSocket::destroy), JS_CFUNC_DEF("setNoDelay", 1, ScSocket::setNoDelay), + JS_CFUNC_DEF("connect", 3, ScSocket::connect), JS_CFUNC_DEF("end", 1, ScSocket::end), + JS_CFUNC_DEF("write", 1, ScSocket::write), JS_CFUNC_DEF("on", 2, ScSocket::on), + JS_CFUNC_DEF("off", 2, ScSocket::off), + }; + RegisterBase(ctx, "Socket", Finalize, funcs); } static void Finalize(JSRuntime* rt, JSValue thisVal) @@ -631,6 +630,15 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx, const std::shared_ptr& plugin) + { + // unique ptr is used to avoid static analyzer false positives. + auto data = std::make_unique(); + data->_plugin = plugin; + GetContext()->GetScriptEngine().AddSocket(data.get()); + return MakeWithOpaque(ctx, data.release()); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("listening", ScListener::listening_get, nullptr), @@ -639,17 +647,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("on", 2, ScListener::on), JS_CFUNC_DEF("off", 2, ScListener::off), }; - - // unique ptr is used to avoid static analyzer false positives. - auto data = std::make_unique(); - data->_plugin = plugin; - GetContext()->GetScriptEngine().AddSocket(data.get()); - return MakeWithOpaque(ctx, funcs, data.release()); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Listener", Finalize); + RegisterBase(ctx, "Listener", Finalize, funcs); } static void Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/object/ScInstalledObject.hpp b/src/openrct2/scripting/bindings/object/ScInstalledObject.hpp index 8f560af3aa..6fa521cee9 100644 --- a/src/openrct2/scripting/bindings/object/ScInstalledObject.hpp +++ b/src/openrct2/scripting/bindings/object/ScInstalledObject.hpp @@ -46,11 +46,6 @@ namespace OpenRCT2::Scripting public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "InstalledObject", Finalize); - } - - JSValue New(JSContext* ctx, size_t index) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("path", ScInstalledObject::path_get, nullptr), @@ -62,7 +57,12 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("authors", ScInstalledObject::authors_get, nullptr), JS_CGETSET_DEF("name", ScInstalledObject::name_get, nullptr), }; - return MakeWithOpaque(ctx, funcs, new InstalledObjectData{ index }); + RegisterBase(ctx, "InstalledObject", Finalize, funcs); + } + + JSValue New(JSContext* ctx, size_t index) + { + return MakeWithOpaque(ctx, new InstalledObjectData{ index }); } private: diff --git a/src/openrct2/scripting/bindings/object/ScObject.hpp b/src/openrct2/scripting/bindings/object/ScObject.hpp index 29b0f424bc..6a73890d7c 100644 --- a/src/openrct2/scripting/bindings/object/ScObject.hpp +++ b/src/openrct2/scripting/bindings/object/ScObject.hpp @@ -39,11 +39,6 @@ namespace OpenRCT2::Scripting public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Object", Finalize); - } - - JSValue NewInstance(JSContext* ctx, ObjectType type, int32_t index) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("installedObject", ScObject::installedObject_get, nullptr), @@ -55,7 +50,12 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("baseImageId", ScObject::baseImageId_get, nullptr), JS_CGETSET_DEF("numImages", ScObject::numImages_get, nullptr), }; - return MakeWithOpaque(ctx, funcs, new ObjectData{ type, index }); + RegisterBase(ctx, "Object", Finalize, funcs); + } + + JSValue NewInstance(JSContext* ctx, ObjectType type, int32_t index) + { + return MakeWithOpaque(ctx, new ObjectData{ type, index }); } static JSValue New(JSContext* ctx, ObjectType type, int32_t index) @@ -176,11 +176,6 @@ namespace OpenRCT2::Scripting public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "RideObjectVehicle", Finalize); - } - - JSValue New(JSContext* ctx, ObjectEntryIndex objectIndex, size_t vehicleIndex) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("rotationFrameMask", ScRideObjectVehicle::rotationFrameMask_get, nullptr), @@ -212,7 +207,12 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("drawOrder", ScRideObjectVehicle::drawOrder_get, nullptr), JS_CGETSET_DEF("numVerticalFramesOverride", ScRideObjectVehicle::numVerticalFramesOverride_get, nullptr), }; - return MakeWithOpaque(ctx, funcs, new VehicleData{ objectIndex, vehicleIndex }); + RegisterBase(ctx, "RideObjectVehicle", Finalize, funcs); + } + + JSValue New(JSContext* ctx, ObjectEntryIndex objectIndex, size_t vehicleIndex) + { + return MakeWithOpaque(ctx, new VehicleData{ objectIndex, vehicleIndex }); } private: @@ -755,11 +755,6 @@ namespace OpenRCT2::Scripting public: void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "LargeSceneryObjectTile", Finalize); - } - - JSValue New(JSContext* ctx, const LargeSceneryTile& tile) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("offset", ScLargeSceneryObjectTile::offset_get, nullptr), @@ -769,7 +764,12 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("corners", ScLargeSceneryObjectTile::corners_get, nullptr), JS_CGETSET_DEF("walls", ScLargeSceneryObjectTile::walls_get, nullptr), }; - return MakeWithOpaque(ctx, funcs, new TileData{ tile }); + RegisterBase(ctx, "LargeSceneryObjectTile", Finalize, funcs); + } + + JSValue New(JSContext* ctx, const LargeSceneryTile& tile) + { + return MakeWithOpaque(ctx, new TileData{ tile }); } private: diff --git a/src/openrct2/scripting/bindings/object/ScObjectManager.cpp b/src/openrct2/scripting/bindings/object/ScObjectManager.cpp index 3f64ef7696..7c344a5ec4 100644 --- a/src/openrct2/scripting/bindings/object/ScObjectManager.cpp +++ b/src/openrct2/scripting/bindings/object/ScObjectManager.cpp @@ -21,11 +21,6 @@ using namespace OpenRCT2; using namespace OpenRCT2::Scripting; void ScObjectManager::Register(JSContext* ctx) -{ - RegisterBaseStr(ctx, "ObjectManager"); -} - -JSValue ScObjectManager::New(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("installedObjects", ScObjectManager::installedObjects_get, nullptr), @@ -35,7 +30,12 @@ JSValue ScObjectManager::New(JSContext* ctx) JS_CFUNC_DEF("getObject", 2, ScObjectManager::getObject), JS_CFUNC_DEF("getAllObjects", 1, ScObjectManager::getAllObjects), }; - return MakeWithOpaque(ctx, funcs, nullptr); + RegisterBase(ctx, "ObjectManager", nullptr, funcs); +} + +JSValue ScObjectManager::New(JSContext* ctx) +{ + return MakeWithOpaque(ctx, nullptr); } JSValue ScObjectManager::installedObjects_get(JSContext* ctx, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/ride/ScRide.cpp b/src/openrct2/scripting/bindings/ride/ScRide.cpp index 9d3e630d59..4b6dc7a2b0 100644 --- a/src/openrct2/scripting/bindings/ride/ScRide.cpp +++ b/src/openrct2/scripting/bindings/ride/ScRide.cpp @@ -34,11 +34,6 @@ namespace OpenRCT2::Scripting }); void ScRide::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Ride", Finalize); - } - - JSValue ScRide::New(JSContext* ctx, RideId rideId) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("id", ScRide::id_get, nullptr), @@ -90,7 +85,12 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("setBreakdown", 1, ScRide::setBreakdown), JS_CFUNC_DEF("fixBreakdown", 0, ScRide::fixBreakdown), }; - return MakeWithOpaque(ctx, funcs, new RideData{ rideId }); + RegisterBase(ctx, "Ride", Finalize, funcs); + } + + JSValue ScRide::New(JSContext* ctx, RideId rideId) + { + return MakeWithOpaque(ctx, new RideData{ rideId }); } void ScRide::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/ride/ScRideStation.cpp b/src/openrct2/scripting/bindings/ride/ScRideStation.cpp index 7212f3f3b2..e99838701d 100644 --- a/src/openrct2/scripting/bindings/ride/ScRideStation.cpp +++ b/src/openrct2/scripting/bindings/ride/ScRideStation.cpp @@ -18,11 +18,6 @@ namespace OpenRCT2::Scripting { void ScRideStation::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "RideStation", Finalize); - } - - JSValue ScRideStation::New(JSContext* ctx, RideId rideId, StationIndex stationIndex) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("start", ScRideStation::start_get, ScRideStation::start_set), @@ -30,7 +25,12 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("entrance", ScRideStation::entrance_get, ScRideStation::entrance_set), JS_CGETSET_DEF("exit", ScRideStation::exit_get, ScRideStation::exit_set), }; - return MakeWithOpaque(ctx, funcs, new RideStationData{ rideId, stationIndex }); + RegisterBase(ctx, "RideStation", Finalize, funcs); + } + + JSValue ScRideStation::New(JSContext* ctx, RideId rideId, StationIndex stationIndex) + { + return MakeWithOpaque(ctx, new RideStationData{ rideId, stationIndex }); } void ScRideStation::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/ride/ScTrackIterator.cpp b/src/openrct2/scripting/bindings/ride/ScTrackIterator.cpp index 40eaceab96..b74dc589e7 100644 --- a/src/openrct2/scripting/bindings/ride/ScTrackIterator.cpp +++ b/src/openrct2/scripting/bindings/ride/ScTrackIterator.cpp @@ -39,11 +39,6 @@ JSValue ScTrackIterator::FromElement(JSContext* ctx, const CoordsXY& position, i } void ScTrackIterator::Register(JSContext* ctx) -{ - RegisterBaseStr(ctx, "TrackIterator", Finalize); -} - -JSValue ScTrackIterator::New(JSContext* ctx, const CoordsXYZD& position, OpenRCT2::TrackElemType type) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("position", ScTrackIterator::position_get, nullptr), @@ -53,7 +48,12 @@ JSValue ScTrackIterator::New(JSContext* ctx, const CoordsXYZD& position, OpenRCT JS_CFUNC_DEF("previous", 0, ScTrackIterator::previous), JS_CFUNC_DEF("next", 0, ScTrackIterator::next), }; - return MakeWithOpaque(ctx, funcs, new TrackIteratorData{ position, type }); + RegisterBase(ctx, "TrackIterator", Finalize, funcs); +} + +JSValue ScTrackIterator::New(JSContext* ctx, const CoordsXYZD& position, OpenRCT2::TrackElemType type) +{ + return MakeWithOpaque(ctx, new TrackIteratorData{ position, type }); } void ScTrackIterator::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp index 18c195c608..8d49943bca 100644 --- a/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp +++ b/src/openrct2/scripting/bindings/ride/ScTrackSegment.cpp @@ -35,11 +35,6 @@ static JSValue VehicleInfoToJSValue(JSContext* ctx, const VehicleInfo& value) } void ScTrackSegment::Register(JSContext* ctx) -{ - RegisterBaseStr(ctx, "TrackSegment", Finalize); -} - -JSValue ScTrackSegment::New(JSContext* ctx, OpenRCT2::TrackElemType type) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("type", ScTrackSegment::type_get, nullptr), @@ -79,7 +74,12 @@ JSValue ScTrackSegment::New(JSContext* ctx, OpenRCT2::TrackElemType type) JS_CFUNC_DEF("getSubpositionLength", 2, ScTrackSegment::getSubpositionLength), JS_CFUNC_DEF("getSubpositions", 2, ScTrackSegment::getSubpositions), }; - return MakeWithOpaque(ctx, funcs, new TrackSegmentData{ type }); + RegisterBase(ctx, "TrackSegment", Finalize, funcs); +} + +JSValue ScTrackSegment::New(JSContext* ctx, OpenRCT2::TrackElemType type) +{ + return MakeWithOpaque(ctx, new TrackSegmentData{ type }); } void ScTrackSegment::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/world/ScAward.cpp b/src/openrct2/scripting/bindings/world/ScAward.cpp index d9fabc03ff..f70afe6a98 100644 --- a/src/openrct2/scripting/bindings/world/ScAward.cpp +++ b/src/openrct2/scripting/bindings/world/ScAward.cpp @@ -28,12 +28,18 @@ namespace OpenRCT2::Scripting JSValue ScAward::New(JSContext* ctx, size_t index) { - return MakeWithOpaque(ctx, funcs, new OpaqueAwardData{ index }); + return MakeWithOpaque(ctx, new OpaqueAwardData{ index }); } void ScAward::Register(JSContext* ctx) { - RegisterBaseStr(ctx, "Award", Finalize); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("type", &ScAward::type_get, nullptr), JS_CGETSET_DEF("text", &ScAward::text_get, nullptr), + JS_CGETSET_DEF("positive", &ScAward::positive_get, nullptr), + JS_CGETSET_DEF("imageId", &ScAward::imageId_get, nullptr), + JS_CGETSET_DEF("monthsRemaining", &ScAward::monthsRemaining_get, nullptr) + }; + RegisterBase(ctx, "Award", Finalize, funcs); } void ScAward::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/world/ScAward.hpp b/src/openrct2/scripting/bindings/world/ScAward.hpp index 4118805683..f9977435bb 100644 --- a/src/openrct2/scripting/bindings/world/ScAward.hpp +++ b/src/openrct2/scripting/bindings/world/ScAward.hpp @@ -78,13 +78,6 @@ namespace OpenRCT2::Scripting static JSValue monthsRemaining_get(JSContext* ctx, JSValue thisVal); static JSValue positive_get(JSContext* ctx, JSValue thisVal); static JSValue imageId_get(JSContext* ctx, JSValue thisVal); - - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("type", &ScAward::type_get, nullptr), JS_CGETSET_DEF("text", &ScAward::text_get, nullptr), - JS_CGETSET_DEF("positive", &ScAward::positive_get, nullptr), - JS_CGETSET_DEF("imageId", &ScAward::imageId_get, nullptr), - JS_CGETSET_DEF("monthsRemaining", &ScAward::monthsRemaining_get, nullptr) - }; }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/world/ScDate.hpp b/src/openrct2/scripting/bindings/world/ScDate.hpp index 122474f8b6..a258e3fd9b 100644 --- a/src/openrct2/scripting/bindings/world/ScDate.hpp +++ b/src/openrct2/scripting/bindings/world/ScDate.hpp @@ -79,6 +79,11 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("monthsElapsed", ScDate::monthsElapsed_get, ScDate::monthsElapsed_set), @@ -89,13 +94,7 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("month", ScDate::month_get, nullptr), JS_CGETSET_DEF("year", ScDate::year_get, nullptr), }; - - return MakeWithOpaque(ctx, funcs, nullptr); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Date"); + RegisterBase(ctx, "Date", nullptr, funcs); } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/world/ScMap.cpp b/src/openrct2/scripting/bindings/world/ScMap.cpp index dfef620e1f..111fe72766 100644 --- a/src/openrct2/scripting/bindings/world/ScMap.cpp +++ b/src/openrct2/scripting/bindings/world/ScMap.cpp @@ -450,11 +450,6 @@ namespace OpenRCT2::Scripting } void ScMap::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Map"); - } - - JSValue ScMap::New(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("size", ScMap::size_get, nullptr), @@ -469,7 +464,12 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("createEntity", 2, ScMap::createEntity), JS_CFUNC_DEF("getTrackIterator", 2, ScMap::getTrackIterator), }; - return MakeWithOpaque(ctx, funcs, nullptr); + RegisterBase(ctx, "Map", nullptr, funcs); + } + + JSValue ScMap::New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); } JSValue ScMap::GetEntityAsDukValue(JSContext* ctx, const EntityBase* sprite) diff --git a/src/openrct2/scripting/bindings/world/ScPark.cpp b/src/openrct2/scripting/bindings/world/ScPark.cpp index eafede98c0..82f7dc28dd 100644 --- a/src/openrct2/scripting/bindings/world/ScPark.cpp +++ b/src/openrct2/scripting/bindings/world/ScPark.cpp @@ -500,6 +500,11 @@ namespace OpenRCT2::Scripting } JSValue ScPark::New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } // namespace OpenRCT2::Scripting + + void ScPark::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("cash", ScPark::cash_get, ScPark::cash_set), @@ -536,12 +541,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("clearAwards", 0, ScPark::clearAwards), JS_CFUNC_DEF("grantAward", 1, ScPark::grantAward), }; - return MakeWithOpaque(ctx, funcs, nullptr); - } // namespace OpenRCT2::Scripting - - void ScPark::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Park"); + RegisterBase(ctx, "Park", nullptr, funcs); } } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/world/ScParkMessage.cpp b/src/openrct2/scripting/bindings/world/ScParkMessage.cpp index 20494f5618..7e0cf94992 100644 --- a/src/openrct2/scripting/bindings/world/ScParkMessage.cpp +++ b/src/openrct2/scripting/bindings/world/ScParkMessage.cpp @@ -186,6 +186,11 @@ namespace OpenRCT2::Scripting } JSValue ScParkMessage::New(JSContext* ctx, size_t index) + { + return MakeWithOpaque(ctx, new OpaqueParkMessageData{ index }); + } + + void ScParkMessage::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("isArchived", ScParkMessage::isArchived_get, nullptr), @@ -197,13 +202,7 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("text", ScParkMessage::text_get, ScParkMessage::text_set), JS_CFUNC_DEF("remove", 0, ScParkMessage::remove), }; - - return MakeWithOpaque(ctx, funcs, new OpaqueParkMessageData{ index }); - } - - void ScParkMessage::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "ParkMessage"); + RegisterBase(ctx, "ParkMessage", Finalize, funcs); } void ScParkMessage::Finalize(JSRuntime*, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/world/ScParkMessage.hpp b/src/openrct2/scripting/bindings/world/ScParkMessage.hpp index 8d7185fd30..da8068a3a9 100644 --- a/src/openrct2/scripting/bindings/world/ScParkMessage.hpp +++ b/src/openrct2/scripting/bindings/world/ScParkMessage.hpp @@ -94,7 +94,7 @@ namespace OpenRCT2::Scripting void Register(JSContext* ctx); private: - void Finalize(JSRuntime* rt, JSValue thisVal); + static void Finalize(JSRuntime* rt, JSValue thisVal); }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/world/ScResearch.cpp b/src/openrct2/scripting/bindings/world/ScResearch.cpp index a4c3f7428a..2f634d6d0e 100644 --- a/src/openrct2/scripting/bindings/world/ScResearch.cpp +++ b/src/openrct2/scripting/bindings/world/ScResearch.cpp @@ -262,11 +262,6 @@ namespace OpenRCT2::Scripting } void ScResearch::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Research"); - } - - JSValue ScResearch::New(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("funding", ScResearch::funding_get, ScResearch::funding_set), @@ -281,7 +276,12 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("uninventedItems", ScResearch::uninventedItems_get, ScResearch::uninventedItems_set), JS_CFUNC_DEF("isObjectResearched", 2, ScResearch::isObjectResearched), }; - return MakeWithOpaque(ctx, funcs, nullptr); + RegisterBase(ctx, "Research", nullptr, funcs); + } + + JSValue ScResearch::New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); } std::vector ScResearch::ConvertResearchList(JSContext* ctx, JSValue value) diff --git a/src/openrct2/scripting/bindings/world/ScScenario.hpp b/src/openrct2/scripting/bindings/world/ScScenario.hpp index 729087d8de..58f5ef2ff2 100644 --- a/src/openrct2/scripting/bindings/world/ScScenario.hpp +++ b/src/openrct2/scripting/bindings/world/ScScenario.hpp @@ -199,6 +199,11 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("type", ScScenarioObjective::type_get, ScScenarioObjective::type_set), @@ -208,13 +213,7 @@ namespace OpenRCT2::Scripting JS_CGETSET_DEF("monthlyIncome", ScScenarioObjective::monthlyIncome_get, ScScenarioObjective::monthlyIncome_set), JS_CGETSET_DEF("parkValue", ScScenarioObjective::parkValue_get, ScScenarioObjective::parkValue_set), }; - - return MakeWithOpaque(ctx, funcs, nullptr); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "ScenarioObjective"); + RegisterBase(ctx, "ScenarioObjective", nullptr, funcs); } }; @@ -348,6 +347,11 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("name", ScScenario::name_get, ScScenario::name_set), @@ -362,13 +366,7 @@ namespace OpenRCT2::Scripting "completedCompanyValue", ScScenario::completedCompanyValue_get, ScScenario::completedCompanyValue_set), JS_CGETSET_DEF("companyValueRecord", ScScenario::companyValueRecord_get, ScScenario::companyValueRecord_set), }; - - return MakeWithOpaque(ctx, funcs, nullptr); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Scenario"); + RegisterBase(ctx, "Scenario", nullptr, funcs); } }; } // namespace OpenRCT2::Scripting diff --git a/src/openrct2/scripting/bindings/world/ScTile.cpp b/src/openrct2/scripting/bindings/world/ScTile.cpp index 9802eb92bc..7e23b2fd5f 100644 --- a/src/openrct2/scripting/bindings/world/ScTile.cpp +++ b/src/openrct2/scripting/bindings/world/ScTile.cpp @@ -238,6 +238,11 @@ namespace OpenRCT2::Scripting } JSValue ScTile::New(JSContext* ctx, CoordsXY& coords) + { + return MakeWithOpaque(ctx, new OpaqueTileData{ coords }); + } + + void ScTile::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("x", ScTile::x_get, nullptr), JS_CGETSET_DEF("y", ScTile::y_get, nullptr), @@ -247,12 +252,7 @@ namespace OpenRCT2::Scripting JS_CFUNC_DEF("getElement", 1, ScTile::getElement), JS_CFUNC_DEF("insertElement", 1, ScTile::insertElement), JS_CFUNC_DEF("removeElement", 0, ScTile::removeElement) }; - return MakeWithOpaque(ctx, funcs, new OpaqueTileData{ coords }); - } - - void ScTile::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Tile", Finalize); + RegisterBase(ctx, "Tile", Finalize, funcs); } void ScTile::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/world/ScTileElement.cpp b/src/openrct2/scripting/bindings/world/ScTileElement.cpp index c19e731009..56fa74df48 100644 --- a/src/openrct2/scripting/bindings/world/ScTileElement.cpp +++ b/src/openrct2/scripting/bindings/world/ScTileElement.cpp @@ -2414,6 +2414,11 @@ namespace OpenRCT2::Scripting } JSValue ScTileElement::New(JSContext* ctx, TileElement* element, CoordsXY& coords) + { + return MakeWithOpaque(ctx, new OpaqueTileElementData{ element, coords }); + } + + void ScTileElement::Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { // All @@ -2507,12 +2512,7 @@ namespace OpenRCT2::Scripting // Banner only JS_CGETSET_DEF("isNoEntry", ScTileElement::isNoEntry_get, ScTileElement::isNoEntry_set) }; - return MakeWithOpaque(ctx, funcs, new OpaqueTileElementData{ element, coords }); - } - - void ScTileElement::Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "TileElement", Finalize); + RegisterBase(ctx, "TileElement", Finalize, funcs); } void ScTileElement::Finalize(JSRuntime* rt, JSValue thisVal) diff --git a/src/openrct2/scripting/bindings/world/ScWeather.hpp b/src/openrct2/scripting/bindings/world/ScWeather.hpp index ed08c4ddce..77eee6b117 100644 --- a/src/openrct2/scripting/bindings/world/ScWeather.hpp +++ b/src/openrct2/scripting/bindings/world/ScWeather.hpp @@ -45,20 +45,19 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx, const std::string& weather, int8_t temperature) { - static constexpr JSCFunctionListEntry funcs[] = { - JS_CGETSET_DEF("weather", ScWeatherState::weather_get, nullptr), - JS_CGETSET_DEF("temperature", ScWeatherState::temperature_get, nullptr), - }; - - return MakeWithOpaque(ctx, funcs, new OpaqueWeatherStateData{ weather, temperature }); + return MakeWithOpaque(ctx, new OpaqueWeatherStateData{ weather, temperature }); } void Register(JSContext* ctx) { - RegisterBaseStr(ctx, "WeatherState"); + static constexpr JSCFunctionListEntry funcs[] = { + JS_CGETSET_DEF("weather", ScWeatherState::weather_get, nullptr), + JS_CGETSET_DEF("temperature", ScWeatherState::temperature_get, nullptr), + }; + RegisterBase(ctx, "WeatherState", Finalize, funcs); } - void Finalize(JSRuntime*, JSValue thisVal) + static void Finalize(JSRuntime*, JSValue thisVal) { OpaqueWeatherStateData* data = gScWeatherState.GetOpaque(thisVal); if (data) @@ -125,19 +124,18 @@ namespace OpenRCT2::Scripting public: JSValue New(JSContext* ctx) + { + return MakeWithOpaque(ctx, nullptr); + } + + void Register(JSContext* ctx) { static constexpr JSCFunctionListEntry funcs[] = { JS_CGETSET_DEF("type", ScWeather::type_get, nullptr), JS_CGETSET_DEF("current", ScWeather::current_get, nullptr), JS_CGETSET_DEF("future", ScWeather::future_get, nullptr), }; - - return MakeWithOpaque(ctx, funcs, nullptr); - } - - void Register(JSContext* ctx) - { - RegisterBaseStr(ctx, "Weather"); + RegisterBase(ctx, "Weather", nullptr, funcs); } };