diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index 7b4962d138..62174b2d6d 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -781,6 +781,8 @@ 9DF65BAE250C4307AE5794DB /* ScStaff.hpp in Headers */ = {isa = PBXBuildFile; fileRef = C30AB6C640464647ABE90EC4 /* ScStaff.hpp */; }; 8E6484F6C43644F4B644D361 /* ScLitter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 36C67B84754D4FEFA3678AA9 /* ScLitter.cpp */; }; 359328A3A65D49578912CA40 /* ScLitter.hpp in Headers */ = {isa = PBXBuildFile; fileRef = C30388F4F6D34DF59EF7072C /* ScLitter.hpp */; }; + 149D1ACFD5FA42938364C843 /* ScPlayerGroup.cpp in Sources */ = {isa = PBXBuildFile; fileRef = DA49170D35604E6A986D09D9 /* ScPlayerGroup.cpp */; }; + 97C222A2A4F543E9AEC3912B /* ScPlayerGroup.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 887EBCE57E4A48C88191346E /* ScPlayerGroup.hpp */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -1864,6 +1866,8 @@ C30AB6C640464647ABE90EC4 /* ScStaff.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ScStaff.hpp; path = src/openrct2/scripting/bindings/entity/ScStaff.hpp; sourceTree = SOURCE_ROOT; }; 36C67B84754D4FEFA3678AA9 /* ScLitter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScLitter.cpp; path = src/openrct2/scripting/bindings/entity/ScLitter.cpp; sourceTree = SOURCE_ROOT; }; C30388F4F6D34DF59EF7072C /* ScLitter.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ScLitter.hpp; path = src/openrct2/scripting/bindings/entity/ScLitter.hpp; sourceTree = SOURCE_ROOT; }; + DA49170D35604E6A986D09D9 /* ScPlayerGroup.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ScPlayerGroup.cpp; path = src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp; sourceTree = SOURCE_ROOT; }; + 887EBCE57E4A48C88191346E /* ScPlayerGroup.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = ScPlayerGroup.hpp; path = src/openrct2/scripting/bindings/network/ScPlayerGroup.hpp; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -3407,6 +3411,8 @@ children = ( BDEB01D4A8144819A82E3CB4 /* ScNetwork.hpp */, D4DD54E62DAE4E4889E26DF6 /* ScSocket.hpp */, + DA49170D35604E6A986D09D9 /* ScPlayerGroup.cpp */, + 887EBCE57E4A48C88191346E /* ScPlayerGroup.hpp */, ); name = network; sourceTree = ""; @@ -3588,6 +3594,7 @@ D7B575F07C4D4E42887C72AA /* ScPeep.hpp in Headers */, 9DF65BAE250C4307AE5794DB /* ScStaff.hpp in Headers */, 359328A3A65D49578912CA40 /* ScLitter.hpp in Headers */, + 97C222A2A4F543E9AEC3912B /* ScPlayerGroup.hpp in Headers */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -4397,6 +4404,7 @@ BC39D70B9A2249E4B14E99FA /* ScGuest.cpp in Sources */, 7A06F880718F49CA9FB71F52 /* ScStaff.cpp in Sources */, 8E6484F6C43644F4B644D361 /* ScLitter.cpp in Sources */, + 149D1ACFD5FA42938364C843 /* ScPlayerGroup.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 6e68b44c43..e4579359cc 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -410,6 +410,7 @@ + @@ -858,6 +859,7 @@ + diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index bc3fe0ee34..519b031a12 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -34,6 +34,7 @@ # include "bindings/game/ScContext.hpp" # include "bindings/game/ScDisposable.hpp" # include "bindings/network/ScNetwork.hpp" +# include "bindings/network/ScPlayerGroup.hpp" # include "bindings/network/ScSocket.hpp" # include "bindings/object/ScObject.hpp" # include "bindings/ride/ScRide.hpp" diff --git a/src/openrct2/scripting/bindings/network/ScNetwork.hpp b/src/openrct2/scripting/bindings/network/ScNetwork.hpp index 3b1cf27184..15073ba207 100644 --- a/src/openrct2/scripting/bindings/network/ScNetwork.hpp +++ b/src/openrct2/scripting/bindings/network/ScNetwork.hpp @@ -19,140 +19,10 @@ # include "../../../network/network.h" # include "../../Duktape.hpp" # include "../network/ScSocket.hpp" +# include "ScPlayerGroup.hpp" namespace OpenRCT2::Scripting { - class ScPlayerGroup - { - private: - int32_t _id; - - public: - ScPlayerGroup(int32_t id) - : _id(id) - { - } - - int32_t id_get() - { - return _id; - } - - std::string name_get() const - { -# ifndef DISABLE_NETWORK - auto index = network_get_group_index(_id); - if (index == -1) - return {}; - return network_get_group_name(index); -# else - return {}; -# endif - } - void name_set(std::string value) - { -# ifndef DISABLE_NETWORK - auto action = NetworkModifyGroupAction(ModifyGroupType::SetName, _id, value); - GameActions::Execute(&action); -# endif - } - - std::vector permissions_get() const - { -# ifndef DISABLE_NETWORK - auto index = network_get_group_index(_id); - if (index == -1) - return {}; - - // Create array of permissions - std::vector result; - auto permissionIndex = 0; - for (const auto& action : NetworkActions::Actions) - { - if (network_can_perform_action(index, static_cast(permissionIndex))) - { - result.push_back(TransformPermissionKeyToJS(action.PermissionName)); - } - permissionIndex++; - } - return result; -# else - return {}; -# endif - } - void permissions_set(std::vector value) - { -# ifndef DISABLE_NETWORK - auto groupIndex = network_get_group_index(_id); - if (groupIndex == -1) - return; - - // First clear all permissions - auto networkAction = NetworkModifyGroupAction( - ModifyGroupType::SetPermissions, _id, "", 0, PermissionState::ClearAll); - GameActions::Execute(&networkAction); - - std::vector enabledPermissions; - enabledPermissions.resize(NetworkActions::Actions.size()); - for (const auto& p : value) - { - auto permissionName = TransformPermissionKeyToInternal(p); - - auto permissionIndex = 0; - for (const auto& action : NetworkActions::Actions) - { - if (action.PermissionName == permissionName) - { - enabledPermissions[permissionIndex] = true; - } - permissionIndex++; - } - } - - for (size_t i = 0; i < enabledPermissions.size(); i++) - { - auto toggle - = (enabledPermissions[i] - != (network_can_perform_action(groupIndex, static_cast(i)) != 0)); - if (toggle) - { - auto networkAction2 = NetworkModifyGroupAction( - ModifyGroupType::SetPermissions, _id, "", static_cast(i), PermissionState::Toggle); - GameActions::Execute(&networkAction2); - } - } -# endif - } - - static void Register(duk_context* ctx) - { - dukglue_register_property(ctx, &ScPlayerGroup::id_get, nullptr, "id"); - dukglue_register_property(ctx, &ScPlayerGroup::name_get, &ScPlayerGroup::name_set, "name"); - dukglue_register_property(ctx, &ScPlayerGroup::permissions_get, &ScPlayerGroup::permissions_set, "permissions"); - } - - private: - static std::string TransformPermissionKeyToJS(const std::string& s) - { - auto result = s.substr(sizeof("PERMISSION_") - 1); - for (auto& c : result) - { - c = std::tolower(c); - } - return result; - } - - static std::string TransformPermissionKeyToInternal(const std::string& s) - { - auto result = "PERMISSION_" + s; - for (auto& c : result) - { - c = std::toupper(c); - } - return result; - } - }; - class ScPlayer { private: diff --git a/src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp b/src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp new file mode 100644 index 0000000000..9c6f5a399d --- /dev/null +++ b/src/openrct2/scripting/bindings/network/ScPlayerGroup.cpp @@ -0,0 +1,150 @@ +/***************************************************************************** + * Copyright (c) 2014-2021 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#ifdef ENABLE_SCRIPTING + +# include "ScPlayerGroup.hpp" + +# include "../../../Context.h" +# include "../../../actions/NetworkModifyGroupAction.h" +# include "../../../actions/PlayerSetGroupAction.h" +# include "../../../network/NetworkAction.h" +# include "../../../network/network.h" +# include "../../Duktape.hpp" + +namespace OpenRCT2::Scripting +{ + ScPlayerGroup::ScPlayerGroup(int32_t id) + : _id(id) + { + } + + int32_t ScPlayerGroup::id_get() + { + return _id; + } + + std::string ScPlayerGroup::name_get() const + { +# ifndef DISABLE_NETWORK + auto index = network_get_group_index(_id); + if (index == -1) + return {}; + return network_get_group_name(index); +# else + return {}; +# endif + } + + void ScPlayerGroup::name_set(std::string value) + { +# ifndef DISABLE_NETWORK + auto action = NetworkModifyGroupAction(ModifyGroupType::SetName, _id, value); + GameActions::Execute(&action); +# endif + } + + static std::string TransformPermissionKeyToJS(const std::string& s) + { + auto result = s.substr(sizeof("PERMISSION_") - 1); + for (auto& c : result) + { + c = std::tolower(c); + } + return result; + } + + std::vector ScPlayerGroup::permissions_get() const + { +# ifndef DISABLE_NETWORK + auto index = network_get_group_index(_id); + if (index == -1) + return {}; + + // Create array of permissions + std::vector result; + auto permissionIndex = 0; + for (const auto& action : NetworkActions::Actions) + { + if (network_can_perform_action(index, static_cast(permissionIndex))) + { + result.push_back(TransformPermissionKeyToJS(action.PermissionName)); + } + permissionIndex++; + } + return result; +# else + return {}; +# endif + } + + static std::string TransformPermissionKeyToInternal(const std::string& s) + { + auto result = "PERMISSION_" + s; + for (auto& c : result) + { + c = std::toupper(c); + } + return result; + } + + void ScPlayerGroup::permissions_set(std::vector value) + { +# ifndef DISABLE_NETWORK + auto groupIndex = network_get_group_index(_id); + if (groupIndex == -1) + return; + + // First clear all permissions + auto networkAction = NetworkModifyGroupAction(ModifyGroupType::SetPermissions, _id, "", 0, PermissionState::ClearAll); + GameActions::Execute(&networkAction); + + std::vector enabledPermissions; + enabledPermissions.resize(NetworkActions::Actions.size()); + for (const auto& p : value) + { + auto permissionName = TransformPermissionKeyToInternal(p); + + auto permissionIndex = 0; + for (const auto& action : NetworkActions::Actions) + { + if (action.PermissionName == permissionName) + { + enabledPermissions[permissionIndex] = true; + } + permissionIndex++; + } + } + + for (size_t i = 0; i < enabledPermissions.size(); i++) + { + auto toggle + = (enabledPermissions[i] != (network_can_perform_action(groupIndex, static_cast(i)) != 0)); + if (toggle) + { + auto networkAction2 = NetworkModifyGroupAction( + ModifyGroupType::SetPermissions, _id, "", static_cast(i), PermissionState::Toggle); + GameActions::Execute(&networkAction2); + } + } +# endif + } + + void ScPlayerGroup::Register(duk_context* ctx) + { + dukglue_register_property(ctx, &ScPlayerGroup::id_get, nullptr, "id"); + dukglue_register_property(ctx, &ScPlayerGroup::name_get, &ScPlayerGroup::name_set, "name"); + dukglue_register_property(ctx, &ScPlayerGroup::permissions_get, &ScPlayerGroup::permissions_set, "permissions"); + } + +} // namespace OpenRCT2::Scripting + +#endif diff --git a/src/openrct2/scripting/bindings/network/ScPlayerGroup.hpp b/src/openrct2/scripting/bindings/network/ScPlayerGroup.hpp new file mode 100644 index 0000000000..757fbda24a --- /dev/null +++ b/src/openrct2/scripting/bindings/network/ScPlayerGroup.hpp @@ -0,0 +1,41 @@ +/***************************************************************************** + * Copyright (c) 2014-2021 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#ifdef ENABLE_SCRIPTING + +# include "../../Duktape.hpp" + +# include +# include + +namespace OpenRCT2::Scripting +{ + class ScPlayerGroup + { + int32_t _id; + + public: + ScPlayerGroup(int32_t id); + + int32_t id_get(); + + std::string name_get() const; + void name_set(std::string value); + + std::vector permissions_get() const; + void permissions_set(std::vector value); + + static void Register(duk_context* ctx); + }; + +} // namespace OpenRCT2::Scripting + +#endif