1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-20 14:23:08 +01:00

format new files

Since many of the changes were made before the clang-format changes went live, the new source files used a different format still. This PR applies the new style to all the new source files.
This commit is contained in:
Hielke Morsink
2018-07-31 17:59:48 +02:00
committed by Ted John
parent c37d7d859e
commit e8bdb95f13
24 changed files with 409 additions and 343 deletions

View File

@@ -7,19 +7,20 @@
* OpenRCT2 is licensed under the GNU General Public License version 3. * OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/ *****************************************************************************/
#include "../interface/Dropdown.h"
#include "ScUi.hpp"
#include "ScWindow.hpp"
#include <limits> #include <limits>
#include <optional> #include <openrct2-ui/interface/Widget.h>
#include <string> #include <openrct2-ui/windows/Window.h>
#include <vector>
#include <openrct2/drawing/Drawing.h> #include <openrct2/drawing/Drawing.h>
#include <openrct2/localisation/Localisation.h> #include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/StringIds.h> #include <openrct2/localisation/StringIds.h>
#include <openrct2/scripting/Plugin.h> #include <openrct2/scripting/Plugin.h>
#include <openrct2-ui/interface/Widget.h> #include <optional>
#include <openrct2-ui/windows/Window.h> #include <string>
#include "../interface/Dropdown.h" #include <vector>
#include "ScUi.hpp"
#include "ScWindow.hpp"
using namespace OpenRCT2; using namespace OpenRCT2;
using namespace OpenRCT2::Scripting; using namespace OpenRCT2::Scripting;
@@ -50,9 +51,7 @@ namespace OpenRCT2::Ui::Windows
static void window_custom_invalidate(rct_window* w); static void window_custom_invalidate(rct_window* w);
static void window_custom_paint(rct_window* w, rct_drawpixelinfo* dpi); static void window_custom_paint(rct_window* w, rct_drawpixelinfo* dpi);
static rct_window_event_list window_custom_events = static rct_window_event_list window_custom_events = { window_custom_close,
{
window_custom_close,
window_custom_mouseup, window_custom_mouseup,
window_custom_resize, window_custom_resize,
window_custom_mousedown, window_custom_mousedown,
@@ -79,8 +78,7 @@ namespace OpenRCT2::Ui::Windows
nullptr, nullptr,
window_custom_invalidate, window_custom_invalidate,
window_custom_paint, window_custom_paint,
nullptr nullptr };
};
struct CustomWidgetDesc struct CustomWidgetDesc
{ {
@@ -165,8 +163,9 @@ namespace OpenRCT2::Ui::Windows
if (desc["widgets"].is_array()) if (desc["widgets"].is_array())
{ {
auto dukWidgets = desc["widgets"].as_array(); auto dukWidgets = desc["widgets"].as_array();
std::transform(dukWidgets.begin(), dukWidgets.end(), std::back_inserter(result.Widgets), std::transform(dukWidgets.begin(), dukWidgets.end(), std::back_inserter(result.Widgets), [](const DukValue& w) {
[](const DukValue& w) { return CustomWidgetDesc::FromDukValue(w); }); return CustomWidgetDesc::FromDukValue(w);
});
} }
return std::move(result); return std::move(result);
@@ -174,9 +173,7 @@ namespace OpenRCT2::Ui::Windows
static std::optional<int32_t> GetOptionalInt(DukValue input) static std::optional<int32_t> GetOptionalInt(DukValue input)
{ {
return input.type() == DukValue::Type::NUMBER ? return input.type() == DukValue::Type::NUMBER ? std::make_optional(input.as_int()) : std::nullopt;
std::make_optional(input.as_int()) :
std::nullopt;
} }
}; };
@@ -193,14 +190,11 @@ namespace OpenRCT2::Ui::Windows
std::vector<size_t> WidgetIndexMap; std::vector<size_t> WidgetIndexMap;
CustomWindowInfo( CustomWindowInfo(
rct_windowclass cls, rct_windowclass cls, rct_windownumber number, std::shared_ptr<Plugin> owner, const CustomWindowDesc& desc)
rct_windownumber number, : _class(cls)
std::shared_ptr<Plugin> owner, , _number(number)
const CustomWindowDesc& desc) , Owner(owner)
: _class(cls), , Desc(desc)
_number(number),
Owner(owner),
Desc(desc)
{ {
} }
@@ -226,7 +220,8 @@ namespace OpenRCT2::Ui::Windows
static rct_windownumber GetNewWindowNumber(); static rct_windownumber GetNewWindowNumber();
static void RefreshWidgets(rct_window* w); static void RefreshWidgets(rct_window* w);
static void InvokeEventHandler(std::shared_ptr<Plugin> owner, const DukValue& dukHandler); static void InvokeEventHandler(std::shared_ptr<Plugin> owner, const DukValue& dukHandler);
static void InvokeEventHandler(std::shared_ptr<Plugin> owner, const DukValue& dukHandler, const std::vector<DukValue>& args); static void InvokeEventHandler(
std::shared_ptr<Plugin> owner, const DukValue& dukHandler, const std::vector<DukValue>& args);
rct_window* window_custom_open(std::shared_ptr<Plugin> owner, DukValue dukDesc) rct_window* window_custom_open(std::shared_ptr<Plugin> owner, DukValue dukDesc)
{ {
@@ -242,22 +237,11 @@ namespace OpenRCT2::Ui::Windows
if (desc.X && desc.Y) if (desc.X && desc.Y)
{ {
window = window_create( window = window_create(
desc.X.value(), desc.X.value(), desc.Y.value(), desc.Width, desc.Height, &window_custom_events, WC_CUSTOM, windowFlags);
desc.Y.value(),
desc.Width,
desc.Height,
&window_custom_events,
WC_CUSTOM,
windowFlags);
} }
else else
{ {
window = window_create_auto_pos( window = window_create_auto_pos(desc.Width, desc.Height, &window_custom_events, WC_CUSTOM, windowFlags);
desc.Width,
desc.Height,
&window_custom_events,
WC_CUSTOM,
windowFlags);
} }
window->number = GetNewWindowNumber(); window->number = GetNewWindowNumber();
@@ -286,7 +270,8 @@ namespace OpenRCT2::Ui::Windows
static void window_custom_mouseup(rct_window* w, rct_widgetindex widgetIndex) static void window_custom_mouseup(rct_window* w, rct_widgetindex widgetIndex)
{ {
switch (widgetIndex) { switch (widgetIndex)
{
case WIDX_CLOSE: case WIDX_CLOSE:
window_close(w); window_close(w);
break; break;
@@ -341,14 +326,8 @@ namespace OpenRCT2::Ui::Windows
set_format_arg_on((uint8_t*)&gDropdownItemsArgs[i], 0, const char*, items[i].c_str()); set_format_arg_on((uint8_t*)&gDropdownItemsArgs[i], 0, const char*, items[i].c_str());
} }
window_dropdown_show_text_custom_width( window_dropdown_show_text_custom_width(
w->x + widget->left, w->x + widget->left, w->y + widget->top, widget->bottom - widget->top + 1, w->colours[widget->colour], 0,
w->y + widget->top, DROPDOWN_FLAG_STAY_OPEN, numItems, widget->right - widget->left - 3);
widget->bottom - widget->top + 1,
w->colours[widget->colour],
0,
DROPDOWN_FLAG_STAY_OPEN,
numItems,
widget->right - widget->left - 3);
} }
} }
} }
@@ -521,4 +500,4 @@ namespace OpenRCT2::Ui::Windows
} }
} }
} }
} } // namespace OpenRCT2::Ui::Windows

View File

@@ -9,13 +9,14 @@
#pragma once #pragma once
#include "../Context.h"
#include "../common.h"
#include "ScWindow.hpp"
#include <dukglue/dukglue.h> #include <dukglue/dukglue.h>
#include <memory> #include <memory>
#include <string>
#include <openrct2/scripting/ScriptEngine.h> #include <openrct2/scripting/ScriptEngine.h>
#include "../common.h" #include <string>
#include "../Context.h"
#include "ScWindow.hpp"
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
@@ -33,14 +34,21 @@ namespace OpenRCT2::Scripting
{ {
private: private:
ScriptEngine& _scriptEngine; ScriptEngine& _scriptEngine;
public: public:
ScUi(ScriptEngine& scriptEngine) ScUi(ScriptEngine& scriptEngine)
: _scriptEngine(scriptEngine) : _scriptEngine(scriptEngine)
{ {
} }
int32_t width_get() { return context_get_width(); } int32_t width_get()
int32_t height_get() { return context_get_height(); } {
return context_get_width();
}
int32_t height_get()
{
return context_get_height();
}
int32_t windows_get() int32_t windows_get()
{ {
return static_cast<int32_t>(g_window_list.size()); return static_cast<int32_t>(g_window_list.size());
@@ -114,4 +122,4 @@ namespace OpenRCT2::Scripting
return WC_NULL; return WC_NULL;
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -9,11 +9,12 @@
#pragma once #pragma once
#include <dukglue/dukglue.h>
#include "../common.h" #include "../common.h"
#include "../interface/Window.h" #include "../interface/Window.h"
#include "../interface/Window_internal.h" #include "../interface/Window_internal.h"
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
class ScWindow class ScWindow
@@ -29,25 +30,37 @@ namespace OpenRCT2::Scripting
} }
ScWindow(rct_windowclass c, rct_windownumber n) ScWindow(rct_windowclass c, rct_windownumber n)
: _class(c), : _class(c)
_number(n) , _number(n)
{ {
} }
int32_t x_get() { return GetWindow()->x; } int32_t x_get()
{
return GetWindow()->x;
}
void x_set(int32_t value) void x_set(int32_t value)
{ {
auto w = GetWindow(); auto w = GetWindow();
window_set_position(w, value, w->y); window_set_position(w, value, w->y);
} }
int32_t y_get() { return GetWindow()->y; } int32_t y_get()
{
return GetWindow()->y;
}
void y_set(int32_t value) void y_set(int32_t value)
{ {
auto w = GetWindow(); auto w = GetWindow();
window_set_position(w, w->x, value); window_set_position(w, w->x, value);
} }
int32_t width_get() { return GetWindow()->width; } int32_t width_get()
int32_t height_get() { return GetWindow()->height; } {
return GetWindow()->width;
}
int32_t height_get()
{
return GetWindow()->height;
}
bool isSticky_get() bool isSticky_get()
{ {
auto flags = GetWindow()->flags; auto flags = GetWindow()->flags;
@@ -69,4 +82,4 @@ namespace OpenRCT2::Scripting
return window_find_by_number(_class, _number); return window_find_by_number(_class, _number);
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -7,11 +7,13 @@
* OpenRCT2 is licensed under the GNU General Public License version 3. * OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/ *****************************************************************************/
#include <openrct2/scripting/ScriptEngine.h>
#include "UiExtensions.h" #include "UiExtensions.h"
#include "ScUi.hpp" #include "ScUi.hpp"
#include "ScWindow.hpp" #include "ScWindow.hpp"
#include <openrct2/scripting/ScriptEngine.h>
using namespace OpenRCT2::Scripting; using namespace OpenRCT2::Scripting;
void UiScriptExtensions::Extend(ScriptEngine& scriptEngine) void UiScriptExtensions::Extend(ScriptEngine& scriptEngine)

View File

@@ -18,4 +18,4 @@ namespace OpenRCT2::Scripting
public: public:
static void Extend(ScriptEngine& scriptEngine); static void Extend(ScriptEngine& scriptEngine);
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -8,8 +8,8 @@
*****************************************************************************/ *****************************************************************************/
#include <array> #include <array>
#include <stdexcept>
#include <experimental/filesystem> #include <experimental/filesystem>
#include <stdexcept>
#ifdef _WIN32 #ifdef _WIN32
# define WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN
@@ -61,9 +61,9 @@ void FileWatcher::FileDescriptor::Close()
} }
FileWatcher::WatchDescriptor::WatchDescriptor(int fd, const std::string& path) FileWatcher::WatchDescriptor::WatchDescriptor(int fd, const std::string& path)
: Fd(fd), : Fd(fd)
Wd(inotify_add_watch(fd, path.c_str(), IN_CLOSE_WRITE)), , Wd(inotify_add_watch(fd, path.c_str(), IN_CLOSE_WRITE))
Path(path) , Path(path)
{ {
if (Wd >= 0) if (Wd >= 0)
{ {
@@ -87,7 +87,9 @@ FileWatcher::FileWatcher(const std::string &directoryPath)
{ {
#ifdef _WIN32 #ifdef _WIN32
_path = directoryPath; _path = directoryPath;
_directoryHandle = CreateFileA(directoryPath.c_str(), FILE_LIST_DIRECTORY, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); _directoryHandle = CreateFileA(
directoryPath.c_str(), FILE_LIST_DIRECTORY, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS,
nullptr);
if (_directoryHandle == INVALID_HANDLE_VALUE) if (_directoryHandle == INVALID_HANDLE_VALUE)
{ {
throw std::runtime_error("Unable to open directory '" + directoryPath + "'"); throw std::runtime_error("Unable to open directory '" + directoryPath + "'");
@@ -123,7 +125,9 @@ void FileWatcher::WatchDirectory()
#ifdef _WIN32 #ifdef _WIN32
std::array<char, 1024> eventData; std::array<char, 1024> eventData;
DWORD bytesReturned; DWORD bytesReturned;
while (ReadDirectoryChangesW(_directoryHandle, eventData.data(), (DWORD)eventData.size(), TRUE, FILE_NOTIFY_CHANGE_LAST_WRITE, &bytesReturned, nullptr, nullptr)) while (ReadDirectoryChangesW(
_directoryHandle, eventData.data(), (DWORD)eventData.size(), TRUE, FILE_NOTIFY_CHANGE_LAST_WRITE, &bytesReturned,
nullptr, nullptr))
{ {
auto onFileChanged = OnFileChanged; auto onFileChanged = OnFileChanged;
if (onFileChanged) if (onFileChanged)
@@ -139,8 +143,7 @@ void FileWatcher::WatchDirectory()
auto fileName = String::ToUtf8(fileNameW); auto fileName = String::ToUtf8(fileNameW);
auto path = fs::path(_path) / fs::path(fileName); auto path = fs::path(_path) / fs::path(fileName);
onFileChanged(path.u8string()); onFileChanged(path.u8string());
} } while (notifyInfo->NextEntryOffset != 0);
while (notifyInfo->NextEntryOffset != 0);
} }
} }
#else #else
@@ -165,11 +168,9 @@ void FileWatcher::WatchDirectory()
// Find watch descriptor // Find watch descriptor
int wd = e->wd; int wd = e->wd;
auto findResult = std::find_if(_watchDescs.begin(), _watchDescs.end(), auto findResult = std::find_if(
[wd](const WatchDescriptor& watchDesc) _watchDescs.begin(), _watchDescs.end(),
{ [wd](const WatchDescriptor& watchDesc) { return wd == watchDesc.Wd; });
return wd == watchDesc.Wd;
});
if (findResult != _watchDescs.end()) if (findResult != _watchDescs.end())
{ {
auto directory = findResult->Path; auto directory = findResult->Path;

View File

@@ -7,13 +7,14 @@
* OpenRCT2 is licensed under the GNU General Public License version 3. * OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/ *****************************************************************************/
#include <linenoise.hpp>
#include "../Context.h" #include "../Context.h"
#include "../OpenRCT2.h" #include "../OpenRCT2.h"
#include "../platform/Platform2.h" #include "../platform/Platform2.h"
#include "../scripting/ScriptEngine.h" #include "../scripting/ScriptEngine.h"
#include "InteractiveConsole.h" #include "InteractiveConsole.h"
#include <linenoise.hpp>
using namespace OpenRCT2; using namespace OpenRCT2;
void StdInOutConsole::Start() void StdInOutConsole::Start()

View File

@@ -69,7 +69,7 @@ namespace WIDGET_FLAGS
{ {
const WidgetFlags TEXT_IS_STRING = 1 << 0; const WidgetFlags TEXT_IS_STRING = 1 << 0;
const WidgetFlags IS_ENABLED = 1 << 1; const WidgetFlags IS_ENABLED = 1 << 1;
} } // namespace WIDGET_FLAGS
/** /**
* Widget structure * Widget structure

View File

@@ -7,10 +7,12 @@
* OpenRCT2 is licensed under the GNU General Public License version 3. * OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/ *****************************************************************************/
#include <unordered_map>
#include "HookEngine.h" #include "HookEngine.h"
#include "ScriptEngine.h" #include "ScriptEngine.h"
#include <unordered_map>
using namespace OpenRCT2::Scripting; using namespace OpenRCT2::Scripting;
HOOK_TYPE OpenRCT2::Scripting::GetHookType(const std::string& name) HOOK_TYPE OpenRCT2::Scripting::GetHookType(const std::string& name)
@@ -20,13 +22,11 @@ HOOK_TYPE OpenRCT2::Scripting::GetHookType(const std::string &name)
{ "interval.day", HOOK_TYPE::INTERVAL_DAY }, { "interval.day", HOOK_TYPE::INTERVAL_DAY },
}); });
auto result = LookupTable.find(name); auto result = LookupTable.find(name);
return (result != LookupTable.end()) ? return (result != LookupTable.end()) ? result->second : HOOK_TYPE::UNDEFINED;
result->second :
HOOK_TYPE::UNDEFINED;
} }
HookEngine::HookEngine(ScriptExecutionInfo& execInfo) : HookEngine::HookEngine(ScriptExecutionInfo& execInfo)
_execInfo(execInfo) : _execInfo(execInfo)
{ {
_hookMap.resize(NUM_HOOK_TYPES); _hookMap.resize(NUM_HOOK_TYPES);
for (size_t i = 0; i < NUM_HOOK_TYPES; i++) for (size_t i = 0; i < NUM_HOOK_TYPES; i++)

View File

@@ -10,10 +10,11 @@
#pragma once #pragma once
#include "../common.h" #include "../common.h"
#include <dukglue/dukglue.h>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
@@ -38,9 +39,9 @@ namespace OpenRCT2::Scripting
Hook(); Hook();
Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const DukValue& function) Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const DukValue& function)
: Cookie(cookie), : Cookie(cookie)
Owner(owner), , Owner(owner)
Function(function) , Function(function)
{ {
} }
}; };
@@ -50,11 +51,13 @@ namespace OpenRCT2::Scripting
HOOK_TYPE Type; HOOK_TYPE Type;
std::vector<Hook> Hooks; std::vector<Hook> Hooks;
HookList() {} HookList()
{
}
HookList(const HookList&) = delete; HookList(const HookList&) = delete;
HookList(HookList&& src) HookList(HookList&& src)
: Type(std::move(src.Type)), : Type(std::move(src.Type))
Hooks(std::move(src.Hooks)) , Hooks(std::move(src.Hooks))
{ {
} }
}; };
@@ -78,4 +81,4 @@ namespace OpenRCT2::Scripting
private: private:
HookList& GetHookList(HOOK_TYPE type); HookList& GetHookList(HOOK_TYPE type);
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -8,9 +8,10 @@
*****************************************************************************/ *****************************************************************************/
#include "Plugin.h" #include "Plugin.h"
#include <algorithm>
#include <dukglue/dukglue.h> #include <dukglue/dukglue.h>
#include <duktape.h> #include <duktape.h>
#include <algorithm>
#include <fstream> #include <fstream>
#include <memory> #include <memory>
@@ -26,8 +27,8 @@
using namespace OpenRCT2::Scripting; using namespace OpenRCT2::Scripting;
Plugin::Plugin(duk_context* context, const std::string& path) Plugin::Plugin(duk_context* context, const std::string& path)
: _context(context), : _context(context)
_path(path) , _path(path)
{ {
} }
@@ -42,9 +43,7 @@ void Plugin::Load()
fs.seekg(0, std::ios::end); fs.seekg(0, std::ios::end);
code.reserve(fs.tellg()); code.reserve(fs.tellg());
fs.seekg(0, std::ios::beg); fs.seekg(0, std::ios::beg);
code.assign( code.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
std::istreambuf_iterator<char>(fs),
std::istreambuf_iterator<char>());
} }
} }
// Wrap the script in a function and pass the global objects as variables // Wrap the script in a function and pass the global objects as variables
@@ -98,11 +97,9 @@ PluginMetadata Plugin::GetMetadata(const DukValue& dukMetadata)
if (dukAuthors.is_array()) if (dukAuthors.is_array())
{ {
auto elements = dukAuthors.as_array(); auto elements = dukAuthors.as_array();
std::transform( std::transform(elements.begin(), elements.end(), std::back_inserter(metadata.Authors), [](const DukValue& v) {
elements.begin(), return v.as_string();
elements.end(), });
std::back_inserter(metadata.Authors),
[](const DukValue& v) { return v.as_string(); });
} }
else else
{ {

View File

@@ -9,10 +9,10 @@
#pragma once #pragma once
#include <dukglue/dukglue.h>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
@@ -32,9 +32,14 @@ namespace OpenRCT2::Scripting
PluginMetadata _metadata; PluginMetadata _metadata;
public: public:
std::string GetPath() const { return _path; }; std::string GetPath() const
{
return _path;
};
Plugin() { } Plugin()
{
}
Plugin(duk_context* context, const std::string& path); Plugin(duk_context* context, const std::string& path);
Plugin(const Plugin&) = delete; Plugin(const Plugin&) = delete;
Plugin(Plugin&&) = delete; Plugin(Plugin&&) = delete;
@@ -46,4 +51,4 @@ namespace OpenRCT2::Scripting
private: private:
static PluginMetadata GetMetadata(const DukValue& dukMetadata); static PluginMetadata GetMetadata(const DukValue& dukMetadata);
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -9,9 +9,8 @@
#pragma once #pragma once
#include <dukglue/dukglue.h>
#include "../interface/InteractiveConsole.h"
#include "../core/Math.hpp" #include "../core/Math.hpp"
#include "../interface/InteractiveConsole.h"
#include <dukglue/dukglue.h> #include <dukglue/dukglue.h>
@@ -38,9 +37,15 @@ namespace OpenRCT2::Scripting
std::string str; std::string str;
switch (val.type()) switch (val.type())
{ {
case DukValue::Type::UNDEFINED: str = "undefined"; break; case DukValue::Type::UNDEFINED:
case DukValue::Type::NULLREF: str = "null"; break; str = "undefined";
case DukValue::Type::BOOLEAN: str = val.as_bool() ? "true" : "false"; break; break;
case DukValue::Type::NULLREF:
str = "null";
break;
case DukValue::Type::BOOLEAN:
str = val.as_bool() ? "true" : "false";
break;
case DukValue::Type::NUMBER: case DukValue::Type::NUMBER:
{ {
const auto d = val.as_double(); const auto d = val.as_double();
@@ -55,11 +60,20 @@ namespace OpenRCT2::Scripting
} }
break; break;
} }
case DukValue::Type::STRING: str = val.as_string(); break; case DukValue::Type::STRING:
case DukValue::Type::OBJECT: str = "{}"; break; str = val.as_string();
case DukValue::Type::BUFFER: str = "buffer"; break; break;
case DukValue::Type::POINTER: str = "pointer"; break; case DukValue::Type::OBJECT:
case DukValue::Type::LIGHTFUNC: break; str = "{}";
break;
case DukValue::Type::BUFFER:
str = "buffer";
break;
case DukValue::Type::POINTER:
str = "pointer";
break;
case DukValue::Type::LIGHTFUNC:
break;
} }
_console.WriteLine(str); _console.WriteLine(str);

View File

@@ -9,11 +9,12 @@
#pragma once #pragma once
#include <dukglue/dukglue.h>
#include "HookEngine.h" #include "HookEngine.h"
#include "ScDisposable.hpp" #include "ScDisposable.hpp"
#include "ScriptEngine.h" #include "ScriptEngine.h"
#include <cstdio> #include <cstdio>
#include <dukglue/dukglue.h>
#include <memory> #include <memory>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
@@ -26,8 +27,8 @@ namespace OpenRCT2::Scripting
public: public:
ScContext(ScriptExecutionInfo& execInfo, HookEngine& hookEngine) ScContext(ScriptExecutionInfo& execInfo, HookEngine& hookEngine)
: _execInfo(execInfo), : _execInfo(execInfo)
_hookEngine(hookEngine) , _hookEngine(hookEngine)
{ {
} }
@@ -55,11 +56,7 @@ namespace OpenRCT2::Scripting
} }
auto cookie = _hookEngine.Subscribe(hookType, owner, callback); auto cookie = _hookEngine.Subscribe(hookType, owner, callback);
return std::make_shared<ScDisposable>( return std::make_shared<ScDisposable>([this, hookType, cookie]() { _hookEngine.Unsubscribe(hookType, cookie); });
[this, hookType, cookie]()
{
_hookEngine.Unsubscribe(hookType, cookie);
});
} }
static void Register(duk_context* ctx) static void Register(duk_context* ctx)
@@ -68,4 +65,4 @@ namespace OpenRCT2::Scripting
dukglue_register_method(ctx, &ScContext::subscribe, "subscribe"); dukglue_register_method(ctx, &ScContext::subscribe, "subscribe");
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -20,8 +20,8 @@ namespace OpenRCT2::Scripting
std::function<void()> _onDispose; std::function<void()> _onDispose;
public: public:
ScDisposable(std::function<void()> onDispose) : ScDisposable(std::function<void()> onDispose)
_onDispose(onDispose) : _onDispose(onDispose)
{ {
} }
@@ -38,4 +38,4 @@ namespace OpenRCT2::Scripting
dukglue_register_method(ctx, &ScDisposable::dispose, "dispose"); dukglue_register_method(ctx, &ScDisposable::dispose, "dispose");
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -9,12 +9,13 @@
#pragma once #pragma once
#include <dukglue/dukglue.h>
#include "../common.h" #include "../common.h"
#include "../ride/Ride.h" #include "../ride/Ride.h"
#include "../world/Map.h" #include "../world/Map.h"
#include "ScTile.hpp"
#include "ScThing.hpp" #include "ScThing.hpp"
#include "ScTile.hpp"
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
@@ -78,4 +79,4 @@ namespace OpenRCT2::Scripting
dukglue_register_method(ctx, &ScMap::getThing, "getThing"); dukglue_register_method(ctx, &ScMap::getThing, "getThing");
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -9,21 +9,25 @@
#pragma once #pragma once
#include <algorithm>
#include <dukglue/dukglue.h>
#include "../common.h"
#include "../Context.h" #include "../Context.h"
#include "../common.h"
#include "../management/Finance.h" #include "../management/Finance.h"
#include "../management/NewsItem.h" #include "../management/NewsItem.h"
#include "../windows/Intent.h" #include "../windows/Intent.h"
#include "../world/Park.h" #include "../world/Park.h"
#include <algorithm>
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
class ScPark class ScPark
{ {
public: public:
money32 cash_get() { return gCash; } money32 cash_get()
{
return gCash;
}
void cash_set(money32 value) void cash_set(money32 value)
{ {
gCash = value; gCash = value;
@@ -31,7 +35,10 @@ namespace OpenRCT2::Scripting
context_broadcast_intent(&intent); context_broadcast_intent(&intent);
} }
int32_t rating_get() { return gParkRating; } int32_t rating_get()
{
return gParkRating;
}
void rating_set(int32_t value) void rating_set(int32_t value)
{ {
gParkRating = std::min(std::max(0, value), 999); gParkRating = std::min(std::max(0, value), 999);
@@ -39,7 +46,10 @@ namespace OpenRCT2::Scripting
context_broadcast_intent(&intent); context_broadcast_intent(&intent);
} }
money32 bankLoan_get() { return gBankLoan; } money32 bankLoan_get()
{
return gBankLoan;
}
void bankLoan_set(money32 value) void bankLoan_set(money32 value)
{ {
gBankLoan = value; gBankLoan = value;
@@ -47,7 +57,10 @@ namespace OpenRCT2::Scripting
context_broadcast_intent(&intent); context_broadcast_intent(&intent);
} }
money32 maxBankLoan_get() { return gMaxBankLoan; } money32 maxBankLoan_get()
{
return gMaxBankLoan;
}
void maxBankLoan_set(money32 value) void maxBankLoan_set(money32 value)
{ {
gMaxBankLoan = value; gMaxBankLoan = value;
@@ -89,15 +102,7 @@ namespace OpenRCT2::Scripting
private: private:
uint8_t GetParkMessageType(const std::string& key) uint8_t GetParkMessageType(const std::string& key)
{ {
static auto keys = { static auto keys = { "attraction", "peep_on_attraction", "peep", "money", "blank", "research", "guests", "award",
"attraction",
"peep_on_attraction",
"peep",
"money",
"blank",
"research",
"guests",
"award",
"chart" }; "chart" };
uint8_t i = 0; uint8_t i = 0;
for (const auto& k : keys) for (const auto& k : keys)
@@ -111,4 +116,4 @@ namespace OpenRCT2::Scripting
return NEWS_ITEM_BLANK; return NEWS_ITEM_BLANK;
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -9,10 +9,11 @@
#pragma once #pragma once
#include <dukglue/dukglue.h>
#include "../common.h" #include "../common.h"
#include "../world/Sprite.h" #include "../world/Sprite.h"
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
class ScThing class ScThing
@@ -35,14 +36,35 @@ namespace OpenRCT2::Scripting
return "unknown"; return "unknown";
} }
int32_t x_get() { return _sprite->unknown.x; } int32_t x_get()
int32_t y_get() { return _sprite->unknown.y; } {
int32_t z_get() { return _sprite->unknown.z; } return _sprite->unknown.x;
}
int32_t y_get()
{
return _sprite->unknown.y;
}
int32_t z_get()
{
return _sprite->unknown.z;
}
uint8_t tshirtColour_get() { return _sprite->peep.tshirt_colour; } uint8_t tshirtColour_get()
void tshirtColour_set(uint8_t value) { _sprite->peep.tshirt_colour = value; } {
uint8_t trousersColour_get() { return _sprite->peep.trousers_colour; } return _sprite->peep.tshirt_colour;
void trousersColour_set(uint8_t value) { _sprite->peep.trousers_colour = value; } }
void tshirtColour_set(uint8_t value)
{
_sprite->peep.tshirt_colour = value;
}
uint8_t trousersColour_get()
{
return _sprite->peep.trousers_colour;
}
void trousersColour_set(uint8_t value)
{
_sprite->peep.trousers_colour = value;
}
static void Register(duk_context* ctx) static void Register(duk_context* ctx)
{ {
@@ -54,4 +76,4 @@ namespace OpenRCT2::Scripting
dukglue_register_property(ctx, &ScThing::trousersColour_get, &ScThing::trousersColour_set, "trousersColour"); dukglue_register_property(ctx, &ScThing::trousersColour_get, &ScThing::trousersColour_set, "trousersColour");
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -9,10 +9,11 @@
#pragma once #pragma once
#include <dukglue/dukglue.h>
#include "../common.h" #include "../common.h"
#include "../world/Sprite.h" #include "../world/Sprite.h"
#include <cstdio> #include <cstdio>
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting namespace OpenRCT2::Scripting
{ {
@@ -31,21 +32,30 @@ namespace OpenRCT2::Scripting
{ {
switch (_element->GetType()) switch (_element->GetType())
{ {
case TILE_ELEMENT_TYPE_SURFACE: return "surface"; case TILE_ELEMENT_TYPE_SURFACE:
case TILE_ELEMENT_TYPE_PATH: return "footpath"; return "surface";
case TILE_ELEMENT_TYPE_TRACK: return "track"; case TILE_ELEMENT_TYPE_PATH:
case TILE_ELEMENT_TYPE_SMALL_SCENERY: return "small-scenery"; return "footpath";
case TILE_ELEMENT_TYPE_ENTRANCE: return "entrance"; case TILE_ELEMENT_TYPE_TRACK:
case TILE_ELEMENT_TYPE_WALL: return "wall"; return "track";
case TILE_ELEMENT_TYPE_LARGE_SCENERY: return "large-scenery"; case TILE_ELEMENT_TYPE_SMALL_SCENERY:
case TILE_ELEMENT_TYPE_BANNER: return "banner"; return "small-scenery";
case TILE_ELEMENT_TYPE_CORRUPT: return "openrct2-corrupt"; case TILE_ELEMENT_TYPE_ENTRANCE:
default: return "unknown"; return "entrance";
case TILE_ELEMENT_TYPE_WALL:
return "wall";
case TILE_ELEMENT_TYPE_LARGE_SCENERY:
return "large-scenery";
case TILE_ELEMENT_TYPE_BANNER:
return "banner";
case TILE_ELEMENT_TYPE_CORRUPT:
return "openrct2-corrupt";
default:
return "unknown";
} }
} }
template<typename T> template<typename T> T* getAs()
T* getAs()
{ {
return static_cast<T*>(this); return static_cast<T*>(this);
} }
@@ -85,8 +95,7 @@ namespace OpenRCT2::Scripting
} }
static void Register(duk_context* ctx); static void Register(duk_context* ctx);
template<typename T> template<typename T> static void RegisterSharedProperties(duk_context* ctx);
static void RegisterSharedProperties(duk_context* ctx);
}; };
class ScSurfaceElement final : public ScTileElement class ScSurfaceElement final : public ScTileElement
@@ -190,28 +199,21 @@ namespace OpenRCT2::Scripting
ScBannerElement ::Register(ctx); ScBannerElement ::Register(ctx);
} }
template<typename T> template<typename T> void ScTileElement::RegisterSharedProperties(duk_context* ctx)
void ScTileElement::RegisterSharedProperties(duk_context* ctx)
{ {
// When you register a member function of a parent class, it belongs to the parent and cannot be accessed by its // When you register a member function of a parent class, it belongs to the parent and cannot be accessed by its
// children. To get around this issue, we need to force the function pointer types to be of the class we want them to be // children. To get around this issue, we need to force the function pointer types to be of the class we want them to be
// available for. This needs to be done for every child. // available for. This needs to be done for every child.
dukglue_register_property(ctx, static_cast<std::string (T::*)() const>(&ScTileElement::type_get), nullptr, "type"); dukglue_register_property(ctx, static_cast<std::string (T::*)() const>(&ScTileElement::type_get), nullptr, "type");
dukglue_register_property( dukglue_register_property(
ctx, ctx, static_cast<bool (T::*)() const>(&ScTileElement::broken_get),
static_cast<bool (T::*)() const>(&ScTileElement::broken_get), static_cast<void (T::*)(bool)>(&ScTileElement::broken_set), "broken");
static_cast<void (T::*)(bool)>(&ScTileElement::broken_set),
"broken");
dukglue_register_property( dukglue_register_property(
ctx, ctx, static_cast<uint8_t (T::*)() const>(&ScTileElement::baseHeight_get),
static_cast<uint8_t (T::*)() const>(&ScTileElement::baseHeight_get), static_cast<void (T::*)(uint8_t)>(&ScTileElement::baseHeight_set), "baseHeight");
static_cast<void (T::*)(uint8_t)>(&ScTileElement::baseHeight_set),
"baseHeight");
dukglue_register_property( dukglue_register_property(
ctx, ctx, static_cast<uint8_t (T::*)() const>(&ScTileElement::clearanceHeight_get),
static_cast<uint8_t (T::*)() const>(&ScTileElement::clearanceHeight_get), static_cast<void (T::*)(uint8_t)>(&ScTileElement::clearanceHeight_set), "clearanceHeight");
static_cast<void (T::*)(uint8_t)>(&ScTileElement::clearanceHeight_set),
"clearanceHeight");
} }
class ScTile class ScTile
@@ -231,16 +233,19 @@ namespace OpenRCT2::Scripting
do do
{ {
_count++; _count++;
} } while (!(element++)->IsLastForTile());
while (!(element++)->IsLastForTile());
} }
} }
size_t numElements_get() { return _count; } size_t numElements_get()
{
return _count;
}
std::shared_ptr<ScTileElement> getElement(size_t index) std::shared_ptr<ScTileElement> getElement(size_t index)
{ {
if (index >= _count) return nullptr; if (index >= _count)
return nullptr;
return std::make_shared<ScTileElement>(&_first[index]); return std::make_shared<ScTileElement>(&_first[index]);
} }
@@ -263,4 +268,4 @@ namespace OpenRCT2::Scripting
dukglue_register_method(ctx, &ScTile::getElement, "getElement"); dukglue_register_method(ctx, &ScTile::getElement, "getElement");
} }
}; };
} } // namespace OpenRCT2::Scripting

View File

@@ -8,23 +8,24 @@
*****************************************************************************/ *****************************************************************************/
#include "ScriptEngine.h" #include "ScriptEngine.h"
#include "../PlatformEnvironment.h"
#include "../core/FileScanner.h" #include "../core/FileScanner.h"
#include "../core/Path.hpp" #include "../core/Path.hpp"
#include "../interface/InteractiveConsole.h" #include "../interface/InteractiveConsole.h"
#include "../platform/Platform2.h" #include "../platform/Platform2.h"
#include "../PlatformEnvironment.h"
#include <dukglue/dukglue.h>
#include <duktape.h>
#include <iostream>
#include <stdexcept>
#include "ScConsole.hpp" #include "ScConsole.hpp"
#include "ScContext.hpp" #include "ScContext.hpp"
#include "ScDisposable.hpp" #include "ScDisposable.hpp"
#include "ScMap.hpp" #include "ScMap.hpp"
#include "ScPark.hpp" #include "ScPark.hpp"
#include "ScTile.hpp"
#include "ScThing.hpp" #include "ScThing.hpp"
#include "ScTile.hpp"
#include <dukglue/dukglue.h>
#include <duktape.h>
#include <iostream>
#include <stdexcept>
using namespace OpenRCT2; using namespace OpenRCT2;
using namespace OpenRCT2::Scripting; using namespace OpenRCT2::Scripting;
@@ -45,10 +46,10 @@ DukContext::~DukContext()
duk_destroy_heap(_context); duk_destroy_heap(_context);
} }
ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env) : ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env)
_console(console), : _console(console)
_env(env), , _env(env)
_hookEngine(_execInfo) , _hookEngine(_execInfo)
{ {
} }
@@ -98,9 +99,7 @@ void ScriptEngine::LoadPlugins()
{ {
// Enable hot reloading // Enable hot reloading
_pluginFileWatcher = std::make_unique<FileWatcher>(base); _pluginFileWatcher = std::make_unique<FileWatcher>(base);
_pluginFileWatcher->OnFileChanged = _pluginFileWatcher->OnFileChanged = [this](const std::string& path) {
[this](const std::string &path)
{
std::lock_guard<std::mutex> guard(_changedPluginFilesMutex); std::lock_guard<std::mutex> guard(_changedPluginFilesMutex);
_changedPluginFiles.push_back(path); _changedPluginFiles.push_back(path);
}; };
@@ -118,9 +117,7 @@ void ScriptEngine::AutoReloadPlugins()
std::lock_guard<std::mutex> guard(_changedPluginFilesMutex); std::lock_guard<std::mutex> guard(_changedPluginFilesMutex);
for (auto& path : _changedPluginFiles) for (auto& path : _changedPluginFiles)
{ {
auto findResult = std::find_if(_plugins.begin(), _plugins.end(), auto findResult = std::find_if(_plugins.begin(), _plugins.end(), [&path](const std::shared_ptr<Plugin>& plugin) {
[&path](const std::shared_ptr<Plugin>& plugin)
{
return Path::Equals(path, plugin->GetPath()); return Path::Equals(path, plugin->GetPath());
}); });
if (findResult != _plugins.end()) if (findResult != _plugins.end())

View File

@@ -13,6 +13,7 @@
#include "../core/FileWatcher.h" #include "../core/FileWatcher.h"
#include "HookEngine.h" #include "HookEngine.h"
#include "Plugin.h" #include "Plugin.h"
#include <future> #include <future>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
@@ -47,8 +48,8 @@ namespace OpenRCT2::Scripting
public: public:
PluginScope(ScriptExecutionInfo& execInfo, std::shared_ptr<Plugin> plugin) PluginScope(ScriptExecutionInfo& execInfo, std::shared_ptr<Plugin> plugin)
: _execInfo(execInfo), : _execInfo(execInfo)
_plugin(plugin) , _plugin(plugin)
{ {
_execInfo._plugin = plugin; _execInfo._plugin = plugin;
} }
@@ -59,7 +60,10 @@ namespace OpenRCT2::Scripting
} }
}; };
std::shared_ptr<Plugin> GetCurrentPlugin() { return _plugin; } std::shared_ptr<Plugin> GetCurrentPlugin()
{
return _plugin;
}
}; };
class DukContext class DukContext
@@ -76,7 +80,10 @@ namespace OpenRCT2::Scripting
} }
~DukContext(); ~DukContext();
operator duk_context*() { return _context; } operator duk_context*()
{
return _context;
}
}; };
class ScriptEngine class ScriptEngine
@@ -100,9 +107,18 @@ namespace OpenRCT2::Scripting
ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env); ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env);
ScriptEngine(ScriptEngine&) = delete; ScriptEngine(ScriptEngine&) = delete;
duk_context * GetContext() { return _context; } duk_context* GetContext()
HookEngine& GetHookEngine() { return _hookEngine; } {
ScriptExecutionInfo& GetExecInfo() { return _execInfo; } return _context;
}
HookEngine& GetHookEngine()
{
return _hookEngine;
}
ScriptExecutionInfo& GetExecInfo()
{
return _execInfo;
}
void Update(); void Update();
std::future<void> Eval(const std::string& s); std::future<void> Eval(const std::string& s);
@@ -113,4 +129,4 @@ namespace OpenRCT2::Scripting
void StartPlugins(); void StartPlugins();
void AutoReloadPlugins(); void AutoReloadPlugins();
}; };
} } // namespace OpenRCT2::Scripting