1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-19 22:02:57 +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

@@ -362,7 +362,7 @@ static void widget_text_centred(rct_drawpixelinfo* dpi, rct_window* w, rct_widge
t = w->windowPos.y + widget->top;
auto stringId = widget->text;
void * formatArgs = gCommonFormatArgs;
void* formatArgs = gCommonFormatArgs;
if (widget->flags & WIDGET_FLAGS::TEXT_IS_STRING)
{
stringId = STR_STRING;
@@ -407,7 +407,7 @@ static void widget_text(rct_drawpixelinfo* dpi, rct_window* w, rct_widgetindex w
t = w->windowPos.y + widget->top;
auto stringId = widget->text;
void * formatArgs = gCommonFormatArgs;
void* formatArgs = gCommonFormatArgs;
if (widget->flags & WIDGET_FLAGS::TEXT_IS_STRING)
{
stringId = STR_STRING;

View File

@@ -7,19 +7,20 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../interface/Dropdown.h"
#include "ScUi.hpp"
#include "ScWindow.hpp"
#include <limits>
#include <optional>
#include <string>
#include <vector>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/localisation/Localisation.h>
#include <openrct2/localisation/StringIds.h>
#include <openrct2/scripting/Plugin.h>
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include "../interface/Dropdown.h"
#include "ScUi.hpp"
#include "ScWindow.hpp"
#include <optional>
#include <string>
#include <vector>
using namespace OpenRCT2;
using namespace OpenRCT2::Scripting;
@@ -42,17 +43,15 @@ namespace OpenRCT2::Ui::Windows
{ WWT_RESIZE, 1, 0, 0, 14, 0, 0xFFFFFFFF, STR_NONE }, // content panel
};
static void window_custom_close(rct_window * w);
static void window_custom_mouseup(rct_window * w, rct_widgetindex widgetIndex);
static void window_custom_mousedown(rct_window * w, rct_widgetindex widgetIndex, rct_widget * widget);
static void window_custom_resize(rct_window * w);
static void window_custom_dropdown(rct_window * w, rct_widgetindex widgetIndex, int32_t dropdownIndex);
static void window_custom_invalidate(rct_window * w);
static void window_custom_paint(rct_window * w, rct_drawpixelinfo * dpi);
static void window_custom_close(rct_window* w);
static void window_custom_mouseup(rct_window* w, rct_widgetindex widgetIndex);
static void window_custom_mousedown(rct_window* w, rct_widgetindex widgetIndex, rct_widget* widget);
static void window_custom_resize(rct_window* w);
static void window_custom_dropdown(rct_window* w, rct_widgetindex widgetIndex, int32_t dropdownIndex);
static void window_custom_invalidate(rct_window* w);
static void window_custom_paint(rct_window* w, rct_drawpixelinfo* dpi);
static rct_window_event_list window_custom_events =
{
window_custom_close,
static rct_window_event_list window_custom_events = { window_custom_close,
window_custom_mouseup,
window_custom_resize,
window_custom_mousedown,
@@ -79,8 +78,7 @@ namespace OpenRCT2::Ui::Windows
nullptr,
window_custom_invalidate,
window_custom_paint,
nullptr
};
nullptr };
struct CustomWidgetDesc
{
@@ -165,8 +163,9 @@ namespace OpenRCT2::Ui::Windows
if (desc["widgets"].is_array())
{
auto dukWidgets = desc["widgets"].as_array();
std::transform(dukWidgets.begin(), dukWidgets.end(), std::back_inserter(result.Widgets),
[](const DukValue& w) { return CustomWidgetDesc::FromDukValue(w); });
std::transform(dukWidgets.begin(), dukWidgets.end(), std::back_inserter(result.Widgets), [](const DukValue& w) {
return CustomWidgetDesc::FromDukValue(w);
});
}
return std::move(result);
@@ -174,9 +173,7 @@ namespace OpenRCT2::Ui::Windows
static std::optional<int32_t> GetOptionalInt(DukValue input)
{
return input.type() == DukValue::Type::NUMBER ?
std::make_optional(input.as_int()) :
std::nullopt;
return input.type() == DukValue::Type::NUMBER ? std::make_optional(input.as_int()) : std::nullopt;
}
};
@@ -193,20 +190,17 @@ namespace OpenRCT2::Ui::Windows
std::vector<size_t> WidgetIndexMap;
CustomWindowInfo(
rct_windowclass cls,
rct_windownumber number,
std::shared_ptr<Plugin> owner,
const CustomWindowDesc& desc)
: _class(cls),
_number(number),
Owner(owner),
Desc(desc)
rct_windowclass cls, rct_windownumber number, std::shared_ptr<Plugin> owner, const CustomWindowDesc& desc)
: _class(cls)
, _number(number)
, Owner(owner)
, Desc(desc)
{
}
CustomWindowInfo(const CustomWindowInfo&) = delete;
const CustomWidgetDesc * GetCustomWidgetDesc(size_t widgetIndex) const
const CustomWidgetDesc* GetCustomWidgetDesc(size_t widgetIndex) const
{
if (widgetIndex < WidgetIndexMap.size())
{
@@ -222,13 +216,14 @@ namespace OpenRCT2::Ui::Windows
static rct_windownumber _nextWindowNumber;
static CustomWindowInfo& GetInfo(rct_window * w);
static CustomWindowInfo& GetInfo(rct_window* w);
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, 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)
{
auto desc = CustomWindowDesc::FromDukValue(dukDesc);
@@ -238,26 +233,15 @@ namespace OpenRCT2::Ui::Windows
windowFlags |= WF_RESIZABLE;
}
rct_window * window;
rct_window* window;
if (desc.X && desc.Y)
{
window = window_create(
desc.X.value(),
desc.Y.value(),
desc.Width,
desc.Height,
&window_custom_events,
WC_CUSTOM,
windowFlags);
desc.X.value(), desc.Y.value(), desc.Width, desc.Height, &window_custom_events, WC_CUSTOM, windowFlags);
}
else
{
window = window_create_auto_pos(
desc.Width,
desc.Height,
&window_custom_events,
WC_CUSTOM,
windowFlags);
window = window_create_auto_pos(desc.Width, desc.Height, &window_custom_events, WC_CUSTOM, windowFlags);
}
window->number = GetNewWindowNumber();
@@ -278,15 +262,16 @@ namespace OpenRCT2::Ui::Windows
return window;
}
static void window_custom_close(rct_window * w)
static void window_custom_close(rct_window* w)
{
delete static_cast<CustomWindowInfo *>(w->custom_info);
delete static_cast<CustomWindowInfo*>(w->custom_info);
w->custom_info = nullptr;
}
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:
window_close(w);
break;
@@ -306,7 +291,7 @@ namespace OpenRCT2::Ui::Windows
}
}
static void window_custom_resize(rct_window * w)
static void window_custom_resize(rct_window* w)
{
const auto& desc = GetInfo(w).Desc;
if (desc.IsResizable())
@@ -324,7 +309,7 @@ namespace OpenRCT2::Ui::Windows
}
}
static void window_custom_mousedown(rct_window * w, rct_widgetindex widgetIndex, rct_widget * widget)
static void window_custom_mousedown(rct_window* w, rct_widgetindex widgetIndex, rct_widget* widget)
{
const auto& info = GetInfo(w);
const auto widgetDesc = info.GetCustomWidgetDesc(widgetIndex);
@@ -338,22 +323,16 @@ namespace OpenRCT2::Ui::Windows
for (size_t i = 0; i < numItems; i++)
{
gDropdownItemsFormat[i] = STR_STRING;
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(
w->x + widget->left,
w->y + widget->top,
widget->bottom - widget->top + 1,
w->colours[widget->colour],
0,
DROPDOWN_FLAG_STAY_OPEN,
numItems,
widget->right - widget->left - 3);
w->x + widget->left, w->y + widget->top, widget->bottom - widget->top + 1, w->colours[widget->colour], 0,
DROPDOWN_FLAG_STAY_OPEN, numItems, widget->right - widget->left - 3);
}
}
}
static void window_custom_dropdown(rct_window * w, rct_widgetindex widgetIndex, int32_t dropdownIndex)
static void window_custom_dropdown(rct_window* w, rct_widgetindex widgetIndex, int32_t dropdownIndex)
{
if (dropdownIndex == -1)
return;
@@ -379,7 +358,7 @@ namespace OpenRCT2::Ui::Windows
}
}
static void window_custom_invalidate(rct_window * w)
static void window_custom_invalidate(rct_window* w)
{
w->widgets[WIDX_BACKGROUND].right = w->width - 1;
w->widgets[WIDX_BACKGROUND].bottom = w->height - 1;
@@ -390,17 +369,17 @@ namespace OpenRCT2::Ui::Windows
w->widgets[WIDX_CONTENT_PANEL].bottom = w->height - 1;
const auto& desc = GetInfo(w).Desc;
set_format_arg(0, void *, desc.Title.c_str());
set_format_arg(0, void*, desc.Title.c_str());
}
static void window_custom_paint(rct_window * w, rct_drawpixelinfo * dpi)
static void window_custom_paint(rct_window* w, rct_drawpixelinfo* dpi)
{
window_draw_widgets(w, dpi);
}
static CustomWindowInfo& GetInfo(rct_window * w)
static CustomWindowInfo& GetInfo(rct_window* w)
{
return *(static_cast<CustomWindowInfo *>(w->custom_info));
return *(static_cast<CustomWindowInfo*>(w->custom_info));
}
static rct_windownumber GetNewWindowNumber()
@@ -413,7 +392,7 @@ namespace OpenRCT2::Ui::Windows
return result;
}
static void CreateWidget(std::vector<rct_widget>& widgetList, const CustomWidgetDesc &desc)
static void CreateWidget(std::vector<rct_widget>& widgetList, const CustomWidgetDesc& desc)
{
rct_widget widget{};
widget.colour = 1;
@@ -456,7 +435,7 @@ namespace OpenRCT2::Ui::Windows
}
}
static void RefreshWidgets(rct_window * w)
static void RefreshWidgets(rct_window* w)
{
auto& info = GetInfo(w);
auto& widgets = info.Widgets;
@@ -521,4 +500,4 @@ namespace OpenRCT2::Ui::Windows
}
}
}
}
} // namespace OpenRCT2::Ui::Windows

View File

@@ -9,13 +9,14 @@
#pragma once
#include "../Context.h"
#include "../common.h"
#include "ScWindow.hpp"
#include <dukglue/dukglue.h>
#include <memory>
#include <string>
#include <openrct2/scripting/ScriptEngine.h>
#include "../common.h"
#include "../Context.h"
#include "ScWindow.hpp"
#include <string>
namespace OpenRCT2::Scripting
{
@@ -24,7 +25,7 @@ namespace OpenRCT2::Scripting
namespace OpenRCT2::Ui::Windows
{
rct_window * window_custom_open(std::shared_ptr<OpenRCT2::Scripting::Plugin> owner, DukValue dukDesc);
rct_window* window_custom_open(std::shared_ptr<OpenRCT2::Scripting::Plugin> owner, DukValue dukDesc);
}
namespace OpenRCT2::Scripting
@@ -33,14 +34,21 @@ namespace OpenRCT2::Scripting
{
private:
ScriptEngine& _scriptEngine;
public:
ScUi(ScriptEngine& scriptEngine)
: _scriptEngine(scriptEngine)
{
}
int32_t width_get() { return context_get_width(); }
int32_t height_get() { return context_get_height(); }
int32_t width_get()
{
return context_get_width();
}
int32_t height_get()
{
return context_get_height();
}
int32_t windows_get()
{
return static_cast<int32_t>(g_window_list.size());
@@ -97,7 +105,7 @@ namespace OpenRCT2::Scripting
return nullptr;
}
static void Register(duk_context * ctx)
static void Register(duk_context* ctx)
{
dukglue_register_property(ctx, &ScUi::height_get, nullptr, "height");
dukglue_register_property(ctx, &ScUi::width_get, nullptr, "width");
@@ -114,4 +122,4 @@ namespace OpenRCT2::Scripting
return WC_NULL;
}
};
}
} // namespace OpenRCT2::Scripting

View File

@@ -9,11 +9,12 @@
#pragma once
#include <dukglue/dukglue.h>
#include "../common.h"
#include "../interface/Window.h"
#include "../interface/Window_internal.h"
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting
{
class ScWindow
@@ -23,38 +24,50 @@ namespace OpenRCT2::Scripting
rct_windownumber _number;
public:
ScWindow(rct_window * w)
ScWindow(rct_window* w)
: ScWindow(w->classification, w->number)
{
}
ScWindow(rct_windowclass c, rct_windownumber n)
: _class(c),
_number(n)
: _class(c)
, _number(n)
{
}
int32_t x_get() { return GetWindow()->x; }
int32_t x_get()
{
return GetWindow()->x;
}
void x_set(int32_t value)
{
auto w = GetWindow();
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)
{
auto w = GetWindow();
window_set_position(w, w->x, value);
}
int32_t width_get() { return GetWindow()->width; }
int32_t height_get() { return GetWindow()->height; }
int32_t width_get()
{
return GetWindow()->width;
}
int32_t height_get()
{
return GetWindow()->height;
}
bool isSticky_get()
{
auto flags = GetWindow()->flags;
return (flags & (WF_STICK_TO_BACK | WF_STICK_TO_FRONT)) != 0;
}
static void Register(duk_context * ctx)
static void Register(duk_context* ctx)
{
dukglue_register_property(ctx, &ScWindow::x_get, &ScWindow::x_set, "x");
dukglue_register_property(ctx, &ScWindow::y_get, &ScWindow::y_set, "y");
@@ -64,9 +77,9 @@ namespace OpenRCT2::Scripting
}
private:
rct_window * GetWindow() const
rct_window* GetWindow() const
{
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.
*****************************************************************************/
#include <openrct2/scripting/ScriptEngine.h>
#include "UiExtensions.h"
#include "ScUi.hpp"
#include "ScWindow.hpp"
#include <openrct2/scripting/ScriptEngine.h>
using namespace OpenRCT2::Scripting;
void UiScriptExtensions::Extend(ScriptEngine& scriptEngine)

View File

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

View File

@@ -8,17 +8,17 @@
*****************************************************************************/
#include <array>
#include <stdexcept>
#include <experimental/filesystem>
#include <stdexcept>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
#else
#include <fcntl.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <unistd.h>
# include <fcntl.h>
# include <sys/inotify.h>
# include <sys/types.h>
# include <unistd.h>
#endif
#include "../core/String.hpp"
@@ -61,9 +61,9 @@ void FileWatcher::FileDescriptor::Close()
}
FileWatcher::WatchDescriptor::WatchDescriptor(int fd, const std::string& path)
: Fd(fd),
Wd(inotify_add_watch(fd, path.c_str(), IN_CLOSE_WRITE)),
Path(path)
: Fd(fd)
, Wd(inotify_add_watch(fd, path.c_str(), IN_CLOSE_WRITE))
, Path(path)
{
if (Wd >= 0)
{
@@ -83,11 +83,13 @@ FileWatcher::WatchDescriptor::~WatchDescriptor()
}
#endif
FileWatcher::FileWatcher(const std::string &directoryPath)
FileWatcher::FileWatcher(const std::string& directoryPath)
{
#ifdef _WIN32
_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)
{
throw std::runtime_error("Unable to open directory '" + directoryPath + "'");
@@ -123,12 +125,14 @@ void FileWatcher::WatchDirectory()
#ifdef _WIN32
std::array<char, 1024> eventData;
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;
if (onFileChanged)
{
FILE_NOTIFY_INFORMATION * notifyInfo;
FILE_NOTIFY_INFORMATION* notifyInfo;
size_t offset = 0;
do
{
@@ -139,8 +143,7 @@ void FileWatcher::WatchDirectory()
auto fileName = String::ToUtf8(fileNameW);
auto path = fs::path(_path) / fs::path(fileName);
onFileChanged(path.u8string());
}
while (notifyInfo->NextEntryOffset != 0);
} while (notifyInfo->NextEntryOffset != 0);
}
}
#else
@@ -165,11 +168,9 @@ void FileWatcher::WatchDirectory()
// Find watch descriptor
int wd = e->wd;
auto findResult = std::find_if(_watchDescs.begin(), _watchDescs.end(),
[wd](const WatchDescriptor& watchDesc)
{
return wd == watchDesc.Wd;
});
auto findResult = std::find_if(
_watchDescs.begin(), _watchDescs.end(),
[wd](const WatchDescriptor& watchDesc) { return wd == watchDesc.Wd; });
if (findResult != _watchDescs.end())
{
auto directory = findResult->Path;

View File

@@ -15,7 +15,7 @@
#include <vector>
#ifdef _WIN32
typedef void * HANDLE;
typedef void* HANDLE;
#endif
/**
@@ -53,9 +53,9 @@ private:
#endif
public:
std::function<void(const std::string &path)> OnFileChanged;
std::function<void(const std::string& path)> OnFileChanged;
FileWatcher(const std::string &directoryPath);
FileWatcher(const std::string& directoryPath);
~FileWatcher();
private:

View File

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

View File

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

View File

@@ -55,7 +55,7 @@ struct rct_window
scenery_variables scenery;
track_list_variables track_list;
error_variables error;
void * custom_info;
void* custom_info;
};
int16_t page;
union

View File

@@ -7,26 +7,26 @@
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include <unordered_map>
#include "HookEngine.h"
#include "ScriptEngine.h"
#include <unordered_map>
using namespace OpenRCT2::Scripting;
HOOK_TYPE OpenRCT2::Scripting::GetHookType(const std::string &name)
HOOK_TYPE OpenRCT2::Scripting::GetHookType(const std::string& name)
{
static const std::unordered_map<std::string, HOOK_TYPE> LookupTable({
{ "interval.tick", HOOK_TYPE::INTERVAL_TICK },
{ "interval.day", HOOK_TYPE::INTERVAL_DAY },
});
auto result = LookupTable.find(name);
return (result != LookupTable.end()) ?
result->second :
HOOK_TYPE::UNDEFINED;
return (result != LookupTable.end()) ? result->second : HOOK_TYPE::UNDEFINED;
}
HookEngine::HookEngine(ScriptExecutionInfo& execInfo) :
_execInfo(execInfo)
HookEngine::HookEngine(ScriptExecutionInfo& execInfo)
: _execInfo(execInfo)
{
_hookMap.resize(NUM_HOOK_TYPES);
for (size_t i = 0; i < NUM_HOOK_TYPES; i++)
@@ -35,7 +35,7 @@ HookEngine::HookEngine(ScriptExecutionInfo& execInfo) :
}
}
uint32_t HookEngine::Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue &function)
uint32_t HookEngine::Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function)
{
auto& hookList = GetHookList(type);
auto cookie = _nextCookie++;

View File

@@ -10,10 +10,11 @@
#pragma once
#include "../common.h"
#include <dukglue/dukglue.h>
#include <memory>
#include <string>
#include <vector>
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting
{
@@ -28,7 +29,7 @@ namespace OpenRCT2::Scripting
UNDEFINED = -1,
};
constexpr size_t NUM_HOOK_TYPES = static_cast<size_t>(HOOK_TYPE::COUNT);
HOOK_TYPE GetHookType(const std::string &name);
HOOK_TYPE GetHookType(const std::string& name);
struct Hook
{
@@ -37,10 +38,10 @@ namespace OpenRCT2::Scripting
DukValue Function;
Hook();
Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const DukValue &function)
: Cookie(cookie),
Owner(owner),
Function(function)
Hook(uint32_t cookie, std::shared_ptr<Plugin> owner, const DukValue& function)
: Cookie(cookie)
, Owner(owner)
, Function(function)
{
}
};
@@ -50,11 +51,13 @@ namespace OpenRCT2::Scripting
HOOK_TYPE Type;
std::vector<Hook> Hooks;
HookList() {}
HookList()
{
}
HookList(const HookList&) = delete;
HookList(HookList&& src)
: Type(std::move(src.Type)),
Hooks(std::move(src.Hooks))
: Type(std::move(src.Type))
, Hooks(std::move(src.Hooks))
{
}
};
@@ -70,7 +73,7 @@ namespace OpenRCT2::Scripting
public:
HookEngine(ScriptExecutionInfo& execInfo);
HookEngine(const HookEngine&) = delete;
uint32_t Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue &function);
uint32_t Subscribe(HOOK_TYPE type, std::shared_ptr<Plugin> owner, const DukValue& function);
void Unsubscribe(HOOK_TYPE type, uint32_t cookie);
void UnsubscribeAll(std::shared_ptr<const Plugin> owner);
void Call(HOOK_TYPE type);
@@ -78,4 +81,4 @@ namespace OpenRCT2::Scripting
private:
HookList& GetHookList(HOOK_TYPE type);
};
}
} // namespace OpenRCT2::Scripting

View File

@@ -8,26 +8,27 @@
*****************************************************************************/
#include "Plugin.h"
#include <algorithm>
#include <dukglue/dukglue.h>
#include <duktape.h>
#include <algorithm>
#include <fstream>
#include <memory>
#ifdef _WIN32
#else
#include <fcntl.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <unistd.h>
# include <fcntl.h>
# include <sys/inotify.h>
# include <sys/types.h>
# include <unistd.h>
#endif
using namespace OpenRCT2::Scripting;
Plugin::Plugin(duk_context * context, const std::string &path)
: _context(context),
_path(path)
Plugin::Plugin(duk_context* context, const std::string& path)
: _context(context)
, _path(path)
{
}
@@ -42,9 +43,7 @@ void Plugin::Load()
fs.seekg(0, std::ios::end);
code.reserve(fs.tellg());
fs.seekg(0, std::ios::beg);
code.assign(
std::istreambuf_iterator<char>(fs),
std::istreambuf_iterator<char>());
code.assign(std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());
}
}
// 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())
{
auto elements = dukAuthors.as_array();
std::transform(
elements.begin(),
elements.end(),
std::back_inserter(metadata.Authors),
[](const DukValue& v) { return v.as_string(); });
std::transform(elements.begin(), elements.end(), std::back_inserter(metadata.Authors), [](const DukValue& v) {
return v.as_string();
});
}
else
{

View File

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

View File

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

View File

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

View File

@@ -20,8 +20,8 @@ namespace OpenRCT2::Scripting
std::function<void()> _onDispose;
public:
ScDisposable(std::function<void()> onDispose) :
_onDispose(onDispose)
ScDisposable(std::function<void()> onDispose)
: _onDispose(onDispose)
{
}
@@ -33,9 +33,9 @@ namespace OpenRCT2::Scripting
}
}
static void Register(duk_context * ctx)
static void Register(duk_context* ctx)
{
dukglue_register_method(ctx, &ScDisposable::dispose, "dispose");
}
};
}
} // namespace OpenRCT2::Scripting

View File

@@ -9,22 +9,23 @@
#pragma once
#include <dukglue/dukglue.h>
#include "../common.h"
#include "../ride/Ride.h"
#include "../world/Map.h"
#include "ScTile.hpp"
#include "ScThing.hpp"
#include "ScTile.hpp"
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting
{
class ScMap
{
private:
duk_context * _context;
duk_context* _context;
public:
ScMap(duk_context * ctx)
ScMap(duk_context* ctx)
: _context(ctx)
{
}
@@ -69,7 +70,7 @@ namespace OpenRCT2::Scripting
return nullptr;
}
static void Register(duk_context * ctx)
static void Register(duk_context* ctx)
{
dukglue_register_property(ctx, &ScMap::size_get, nullptr, "size");
dukglue_register_property(ctx, &ScMap::rides_get, nullptr, "rides");
@@ -78,4 +79,4 @@ namespace OpenRCT2::Scripting
dukglue_register_method(ctx, &ScMap::getThing, "getThing");
}
};
}
} // namespace OpenRCT2::Scripting

View File

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

View File

@@ -9,19 +9,20 @@
#pragma once
#include <dukglue/dukglue.h>
#include "../common.h"
#include "../world/Sprite.h"
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting
{
class ScThing
{
private:
rct_sprite * _sprite;
rct_sprite* _sprite;
public:
ScThing(rct_sprite * sprite)
ScThing(rct_sprite* sprite)
: _sprite(sprite)
{
}
@@ -35,16 +36,37 @@ namespace OpenRCT2::Scripting
return "unknown";
}
int32_t x_get() { return _sprite->unknown.x; }
int32_t y_get() { return _sprite->unknown.y; }
int32_t z_get() { return _sprite->unknown.z; }
int32_t x_get()
{
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; }
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; }
uint8_t tshirtColour_get()
{
return _sprite->peep.tshirt_colour;
}
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)
{
dukglue_register_property(ctx, &ScThing::type_get, nullptr, "type");
dukglue_register_property(ctx, &ScThing::x_get, nullptr, "x");
@@ -54,4 +76,4 @@ namespace OpenRCT2::Scripting
dukglue_register_property(ctx, &ScThing::trousersColour_get, &ScThing::trousersColour_set, "trousersColour");
}
};
}
} // namespace OpenRCT2::Scripting

View File

@@ -9,20 +9,21 @@
#pragma once
#include <dukglue/dukglue.h>
#include "../common.h"
#include "../world/Sprite.h"
#include <cstdio>
#include <dukglue/dukglue.h>
namespace OpenRCT2::Scripting
{
class ScTileElement
{
protected:
rct_tile_element * _element;
rct_tile_element* _element;
public:
ScTileElement(rct_tile_element * element)
ScTileElement(rct_tile_element* element)
: _element(element)
{
}
@@ -31,21 +32,30 @@ namespace OpenRCT2::Scripting
{
switch (_element->GetType())
{
case TILE_ELEMENT_TYPE_SURFACE: return "surface";
case TILE_ELEMENT_TYPE_PATH: return "footpath";
case TILE_ELEMENT_TYPE_TRACK: return "track";
case TILE_ELEMENT_TYPE_SMALL_SCENERY: return "small-scenery";
case TILE_ELEMENT_TYPE_ENTRANCE: 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";
case TILE_ELEMENT_TYPE_SURFACE:
return "surface";
case TILE_ELEMENT_TYPE_PATH:
return "footpath";
case TILE_ELEMENT_TYPE_TRACK:
return "track";
case TILE_ELEMENT_TYPE_SMALL_SCENERY:
return "small-scenery";
case TILE_ELEMENT_TYPE_ENTRANCE:
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>
T* getAs()
template<typename T> T* getAs()
{
return static_cast<T*>(this);
}
@@ -85,8 +95,7 @@ namespace OpenRCT2::Scripting
}
static void Register(duk_context* ctx);
template<typename T>
static void RegisterSharedProperties(duk_context* ctx);
template<typename T> static void RegisterSharedProperties(duk_context* ctx);
};
class ScSurfaceElement final : public ScTileElement
@@ -190,38 +199,31 @@ namespace OpenRCT2::Scripting
ScBannerElement ::Register(ctx);
}
template<typename T>
void ScTileElement::RegisterSharedProperties(duk_context* ctx)
template<typename T> 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
// 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.
dukglue_register_property(ctx, static_cast<std::string (T::*)() const>(&ScTileElement::type_get), nullptr, "type");
dukglue_register_property(
ctx,
static_cast<bool (T::*)() const>(&ScTileElement::broken_get),
static_cast<void (T::*)(bool)>(&ScTileElement::broken_set),
"broken");
ctx, static_cast<bool (T::*)() const>(&ScTileElement::broken_get),
static_cast<void (T::*)(bool)>(&ScTileElement::broken_set), "broken");
dukglue_register_property(
ctx,
static_cast<uint8_t (T::*)() const>(&ScTileElement::baseHeight_get),
static_cast<void (T::*)(uint8_t)>(&ScTileElement::baseHeight_set),
"baseHeight");
ctx, static_cast<uint8_t (T::*)() const>(&ScTileElement::baseHeight_get),
static_cast<void (T::*)(uint8_t)>(&ScTileElement::baseHeight_set), "baseHeight");
dukglue_register_property(
ctx,
static_cast<uint8_t (T::*)() const>(&ScTileElement::clearanceHeight_get),
static_cast<void (T::*)(uint8_t)>(&ScTileElement::clearanceHeight_set),
"clearanceHeight");
ctx, static_cast<uint8_t (T::*)() const>(&ScTileElement::clearanceHeight_get),
static_cast<void (T::*)(uint8_t)>(&ScTileElement::clearanceHeight_set), "clearanceHeight");
}
class ScTile
{
private:
rct_tile_element * _first;
rct_tile_element* _first;
size_t _count = 0;
public:
ScTile(rct_tile_element * first)
ScTile(rct_tile_element* first)
: _first(first)
{
_count = 0;
@@ -231,16 +233,19 @@ namespace OpenRCT2::Scripting
do
{
_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)
{
if (index >= _count) return nullptr;
if (index >= _count)
return nullptr;
return std::make_shared<ScTileElement>(&_first[index]);
}
@@ -256,11 +261,11 @@ namespace OpenRCT2::Scripting
// return elements;
// }
static void Register(duk_context * ctx)
static void Register(duk_context* ctx)
{
// dukglue_register_property(ctx, &ScTile::elements_get, nullptr, "elements");
dukglue_register_property(ctx, &ScTile::numElements_get, nullptr, "numElements");
dukglue_register_method(ctx, &ScTile::getElement, "getElement");
}
};
}
} // namespace OpenRCT2::Scripting

View File

@@ -8,28 +8,29 @@
*****************************************************************************/
#include "ScriptEngine.h"
#include "../PlatformEnvironment.h"
#include "../core/FileScanner.h"
#include "../core/Path.hpp"
#include "../interface/InteractiveConsole.h"
#include "../platform/Platform2.h"
#include "../PlatformEnvironment.h"
#include <dukglue/dukglue.h>
#include <duktape.h>
#include <iostream>
#include <stdexcept>
#include "ScConsole.hpp"
#include "ScContext.hpp"
#include "ScDisposable.hpp"
#include "ScMap.hpp"
#include "ScPark.hpp"
#include "ScTile.hpp"
#include "ScThing.hpp"
#include "ScTile.hpp"
#include <dukglue/dukglue.h>
#include <duktape.h>
#include <iostream>
#include <stdexcept>
using namespace OpenRCT2;
using namespace OpenRCT2::Scripting;
static std::string Stringify(duk_context * ctx, duk_idx_t idx);
static std::string Stringify(duk_context* ctx, duk_idx_t idx);
DukContext::DukContext()
{
@@ -45,10 +46,10 @@ DukContext::~DukContext()
duk_destroy_heap(_context);
}
ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env) :
_console(console),
_env(env),
_hookEngine(_execInfo)
ScriptEngine::ScriptEngine(InteractiveConsole& console, IPlatformEnvironment& env)
: _console(console)
, _env(env)
, _hookEngine(_execInfo)
{
}
@@ -88,7 +89,7 @@ void ScriptEngine::LoadPlugins()
plugin->Load();
_plugins.push_back(std::move(plugin));
}
catch (const std::exception &e)
catch (const std::exception& e)
{
_console.WriteLineError(e.what());
}
@@ -98,9 +99,7 @@ void ScriptEngine::LoadPlugins()
{
// Enable hot reloading
_pluginFileWatcher = std::make_unique<FileWatcher>(base);
_pluginFileWatcher->OnFileChanged =
[this](const std::string &path)
{
_pluginFileWatcher->OnFileChanged = [this](const std::string& path) {
std::lock_guard<std::mutex> guard(_changedPluginFilesMutex);
_changedPluginFiles.push_back(path);
};
@@ -118,9 +117,7 @@ void ScriptEngine::AutoReloadPlugins()
std::lock_guard<std::mutex> guard(_changedPluginFilesMutex);
for (auto& path : _changedPluginFiles)
{
auto findResult = std::find_if(_plugins.begin(), _plugins.end(),
[&path](const std::shared_ptr<Plugin>& plugin)
{
auto findResult = std::find_if(_plugins.begin(), _plugins.end(), [&path](const std::shared_ptr<Plugin>& plugin) {
return Path::Equals(path, plugin->GetPath());
});
if (findResult != _plugins.end())
@@ -134,7 +131,7 @@ void ScriptEngine::AutoReloadPlugins()
plugin->Load();
plugin->Start();
}
catch (const std::exception &e)
catch (const std::exception& e)
{
_console.WriteLineError(e.what());
}
@@ -153,7 +150,7 @@ void ScriptEngine::StartPlugins()
{
plugin->Start();
}
catch (const std::exception &e)
catch (const std::exception& e)
{
_console.WriteLineError(e.what());
}
@@ -196,7 +193,7 @@ void ScriptEngine::Update()
}
}
std::future<void> ScriptEngine::Eval(const std::string &s)
std::future<void> ScriptEngine::Eval(const std::string& s)
{
std::promise<void> barrier;
auto future = barrier.get_future();
@@ -204,7 +201,7 @@ std::future<void> ScriptEngine::Eval(const std::string &s)
return future;
}
static std::string Stringify(duk_context * ctx, duk_idx_t idx)
static std::string Stringify(duk_context* ctx, duk_idx_t idx)
{
auto type = duk_get_type(ctx, idx);
if (type == DUK_TYPE_OBJECT && !duk_is_function(ctx, idx))

View File

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