Skip to content

Commit

Permalink
Fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ArKaNeMaN committed Dec 14, 2024
1 parent c564c73 commit b1eada5
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .build-config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PACKAGE_NAME=CustomWeaponsAPI
PACKAGE_NAME=ExtendedMenuSystem
PACKAGE_README_USE=1

PACKAGE_COMPILED_PLUGINS_USE=1
Expand Down
8 changes: 2 additions & 6 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ on:
types: [published]

env:
PACKAGE_NAME: CustomWeaponsAPI
STORE_READMY: 1
PACKAGE_NAME: ExtendedMenuSystem
PARAMS_CONRTOLLER_TAG: 1.0.0-b4

jobs:
Expand Down Expand Up @@ -118,10 +117,7 @@ jobs:
run: |
mkdir -p publish/${{ env.PACKAGE_NAME }}/addons
mv amxmodx/ publish/${{ env.PACKAGE_NAME }}/addons
if [ ! -z "${STORE_READMY}" ]; then
mv README.md publish/
fi
mv README.md publish/
- name: Deploy artifact
uses: actions/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions amxmodx/scripting/EMS/API/MenuController.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ T_MenuController:@_MenuController_Register(const pluginIndex) {
get_string(Arg_Key, controllerKey, charsmax(controllerKey));
get_string(Arg_Callback, controllerCallback, charsmax(controllerCallback));

return MenuController_Register(controllerKey, MenuController_MakeCallback(pluginIndex, controllerCallback));
return MenuController_Construct(controllerKey, MenuController_MakeCallback(pluginIndex, controllerCallback));
}

@_MenuController_AddParams(const pluginIndex, const paramsCount) {
enum {Arg_Controller = 1, Arg_Params};

new T_MenuController:controller = get_param(Arg_Controller);
new T_MenuController:controller = T_MenuController:get_param(Arg_Controller);
new Array:newParams = ParamsController_Param_ListFromNativeParams(Arg_Params, paramsCount);

MenuController_AddParams(controller, newParams);
Expand Down
9 changes: 9 additions & 0 deletions amxmodx/scripting/EMS/API/MenuExtension.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <amxmodx>
#include <ems>
#include <ParamsController>
#include "EMS/Objects/MenuExtension"

API_MenuExtension_Init() {
// register_native("MenuExtension_Register", "@_MenuExtension_Register");
// register_native("MenuExtension_AddParams", "@_MenuExtension_AddParams");
}
15 changes: 8 additions & 7 deletions amxmodx/scripting/EMS/Objects/Menu.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "EMS/Objects/MenuExtensionInstance"
#include "EMS/Objects/MenuController"
#include "EMS/Objects/MenuExtension"
#include "EMS/Utils"

enum T_Menu { Invalid_Menu = INVALID_HANDLE }

Expand Down Expand Up @@ -88,7 +89,7 @@ Menu_CallMakeItem(const menuObject[], title[], command[]) {
}
}

Menu_LoadFromFolder(const folderPath[]) {
Menu_LoadFromFolder(folderPath[]) {
new filePath[PLATFORM_MAX_PATH], fileHandler, FileType:fileType;
fileHandler = open_dir(folderPath, filePath, charsmax(filePath), fileType);
if (!fileHandler) {
Expand Down Expand Up @@ -121,7 +122,7 @@ Menu_LoadFromFolder(const folderPath[]) {

Menu_LoadFromFile(const filePath[]) {
new JSON:menuJson = json_parse(filePath, true, true);
Menu_LoadFromJson(menuJson);
new menuIndex = Menu_LoadFromJson(menuJson);
json_free(menuJson);

return menuIndex;
Expand All @@ -130,27 +131,27 @@ Menu_LoadFromFile(const filePath[]) {
Menu_LoadFromJson(const JSON:menuJson) {
if (!json_is_object(menuJson)) {
abort(AMX_ERR_PARAMS, "menuJson is not a json object.");
return;
return INVALID_HANDLE;
}

new menu[S_Menu];

json_object_get_string(menuJson, "Command", menu[Menu_Command], charsmax(menu[Menu_Command]));
if (menu[Menu_Command][0] == EOS) {
abort(AMX_ERR_PARAMS, "`Command` field in menu object is empty.");
return;
return INVALID_HANDLE;
}

JsonObject_ReadMultilineString(menuJson, "Title", menu[Menu_Title], charsmax(menu[Menu_Title]));
menu[Menu_ExtensionInstances] = JsonObject_GetMenuExtensionInstances(menuJson, "Extensions");

if (json_object_has_value(menuJson, "Items", JSONArray)) {
JsonObject_GetDefaultMenuControllerInstance(menuJson, "Items", menu[Menu_ControllerInstance]);
menu[Menu_ControllerInstance] = JsonObject_iGetDefaultMenuControllerInstance(menuJson, "Items");
} else if (json_object_has_value(menuJson, "Controller")) {
JsonObject_GetMenuControllerInstance(menuJson, "Controller", menu[Menu_ControllerInstance]);
menu[Menu_ControllerInstance] = JsonObject_iGetMenuControllerInstance(menuJson, "Controller");
} else {
abort(AMX_ERR_PARAMS, "Menu object has no `Items` or `Controller` fields.");
return;
return INVALID_HANDLE;
}

new menuIndex = ArrayPushArray(Menus, menu);
Expand Down
75 changes: 53 additions & 22 deletions amxmodx/scripting/EMS/Objects/MenuController.inc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ MenuController_Init() {
ParamsController_Init();
}

MenuController_Use(const T_MenuController:controller, const playerIndex, const menu, const Trie:params) {
new controllerObject[S_MenuController];
MenuController_Get(controller, controllerObject);

// TODO: Вместо создания пунктов напрямую, использовать какую-нить обёртку
ExecuteForward(controllerObject[MenuController_Callback], _, playerIndex, menu, params);
}

T_MenuController:MenuController_Construct(const controllerKey[], const callback) {
new controllerObject[S_MenuController];

copy(controllerObject[MenuController_Key], charsmax(controllerObject[MenuController_Key]), controllerKey);
controllerObject[MenuController_Callback] = callback;
controllerObject[MenuController_Params] = ArrayCreate(1, 1);

return T_MenuController:ArrayMapPushArray(MenuControllers, controllerObject, controllerObject[MenuController_Key]);
}

MenuController_AddParams(const T_MenuController:controller, const Array:newParams) {
new controllerObject[S_MenuController];
MenuController_Get(controller, controllerObject);

for (new i = 0, ii = ArraySize(newParams); i < ii; ++i) {
ArrayPushCell(controllerObject[MenuController_Params], ArrayGetCell(newParams, i));
}
}

T_MenuController:MenuController_Find(const controllerKey[], const bool:orFail = false) {
if (!ArrayMapKeyExists(MenuControllers, controllerKey)) {
if (orFail) {
Expand All @@ -33,30 +60,43 @@ T_MenuController:MenuController_Find(const controllerKey[], const bool:orFail =
return Invalid_MenuController;
}

return ArrayMapGetIndex(MenuControllers, controllerKey);
return T_MenuController:ArrayMapGetIndex(MenuControllers, controllerKey);
}

MenuController_Get(const T_MenuController:controller, controllerObject[S_MenuController]) {
ArrayMapGetArray(MenuControllers, controller, controllerObject);
}

T_MenuController:MenuController_Construct(const controllerKey[], const callback) {
new controllerObject[S_MenuController];

copy(controllerObject[MenuController_Key], charsmax(controllerObject[MenuController_Key]), controllerKey);
controllerObject[MenuController_Callback] = callback;
controllerObject[MenuController_Params] = ArrayCreate(1, 1);

return ArrayMapPushArray(MenuControllers, controllerObject, controllerObject[MenuController_Key]);
}
Trie:MenuController_ReadParams(const T_MenuController:controller, const JSON:paramsJson) {
if (paramsJson == Invalid_JSON) {
abort(AMX_ERR_PARAMS, "paramsJson is an invalid json handler (%d).", paramsJson);
return Invalid_Trie;
}

MenuController_AddParams(const T_MenuController:controller, const Array:newParams) {
new controllerObject[S_MenuController];
MenuController_Get(controller, controllerObject);

for (new i =, ii = ArraySize(newParams); i < ii; ++i) {
ArrayPushCell(controllerObject[MenuController_Params], ArrayGetCell(newParams, i));
new Trie:params = TrieCreate();
if (controllerObject[MenuController_Params] != Invalid_Array) {
new E_ParamsReadErrorType:errType = ParamsReadError_None;
new errParamName[PARAM_KEY_MAX_LEN];

ParamsController_Param_ReadList(
controllerObject[MenuController_Params], paramsJson, params,
.iErrType = errType,
.sErrParamName = errParamName,
.iErrParamNameLen = charsmax(errParamName)
);

switch (errType) {
case ParamsReadError_RequiredParamNotPresented:
abort(AMX_ERR_PARAMS, "Controller '%s' parameter '%s' is required but not presented.", controllerObject[MenuController_Key], errParamName);
case ParamsReadError_ParamValueIsInvalid:
abort(AMX_ERR_PARAMS, "Controller '%s' parameter '%s' has invalid value.", controllerObject[MenuController_Key], errParamName);
}
}

return params;
}

MenuController_MakeCallback(const pluginIndex, const callback[]) {
Expand All @@ -65,16 +105,7 @@ MenuController_MakeCallback(const pluginIndex, const callback[]) {

if (fwd == INVALID_HANDLE) {
abort(AMX_ERR_PARAMS, "Failed to create forward.");
return;
}

return fwd;
}

MenuController_Use(const T_MenuController:controller, const playerIndex, const menu, const Trie:params) {
new controllerObject[S_MenuController];
MenuController_Get(controller, controllerObject);

// TODO: Вместо создания пунктов напрямую, использовать какую-нить обёртку
ExecuteForward(controllerObject[MenuController_Callback], _, playerIndex, menu, params);
}
48 changes: 29 additions & 19 deletions amxmodx/scripting/EMS/Objects/MenuControllerInstance.inc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if defined _ems_src__objects_menu_controller_included
#if defined _ems_src__objects_menu_controller_instance_included
#endinput
#endif
#define _ems_src__objects_menu_controller_included
#define _ems_src__objects_menu_controller_instance_included

#include <amxmodx>
#include <json>
Expand All @@ -27,14 +27,13 @@ MenuControllerInstance_Make(const T_MenuController:controller, const Trie:params
abort(AMX_ERR_PARAMS, "controller is an invalid menu controller handler (%d).", controller);
return instance;
}
instance[MenuControllerInstance_MenuController] = controller;

if (params == Invalid_Trie) {
abort(AMX_ERR_PARAMS, "params is an invalid trie handler (%d).", params);
return instance;
instance[MenuControllerInstance_Params] = TrieCreate();
} else {
instance[MenuControllerInstance_Params] = params;
}

instance[MenuControllerInstance_MenuController] = controller;
instance[MenuControllerInstance_Params] = params;

return instance;
}
Expand All @@ -53,33 +52,44 @@ MenuControllerInstance_MakeFromJson(const JSON:controllerJson, instance[S_MenuCo
new controllerKey[EMS_CONTROLLER_KEY_MAX_LEN];
json_get_string(controllerJson, controllerKey, charsmax(controllerKey));

new T_MenuController:controller = MenuController_Find(controllerKey, .orFail = true);

new JSON:fakeParamsJson = json_init_object();
new Trie:params = MenuController_ReadParams(controller, fakeParams);
json_free(fakeParamsJson);

instance = MenuControllerInstance_Make(controller, params);
instance = MenuControllerInstance_Make(
.controller = MenuController_Find(controllerKey, .orFail = true),
.params = Invalid_Trie
);
} else if (json_is_object(controllerJson)) {
new controllerKey[EMS_CONTROLLER_KEY_MAX_LEN];
json_get_string(controllerJson, "Controller", controllerKey, EMS_CONTROLLER_KEY_MAX_LEN);
json_object_get_string(controllerJson, "Controller", controllerKey, EMS_CONTROLLER_KEY_MAX_LEN);

new T_MenuController:controller = MenuController_Find(controllerKey, .orFail = true);

instance = MenuControllerInstance_Make(controller, MenuController_ReadParams(controller, controllerJson));
instance = MenuControllerInstance_Make(
.controller = controller,
.params = MenuController_ReadParams(controller, controllerJson)
);
} else {
abort(AMX_ERR_PARAMS, "controllerJson is not a json object or string.");
}
}

JsonObject_GetMenuControllerInstance(const JSON:jsonObject, const key[], instance[S_MenuControllerInstance], const bool:dotNot = false) {
new JSON:controllerJson = json_object_get_value(jsonObject, key, dotNot);
instance = MenuControllerInstance_MakeFromJson(controllerJson);
json_free(controllersJson);
MenuControllerInstance_MakeFromJson(controllerJson, instance);
json_free(controllerJson);
}

JsonObject_iGetMenuControllerInstance(const JSON:jsonObject, const key[], const bool:dotNot = false) {
new instance[S_MenuControllerInstance];
JsonObject_GetMenuControllerInstance(jsonObject, key, instance, dotNot);
return instance;
}

JsonObject_GetDefaultMenuControllerInstance(const JSON:jsonObject, const itemsKey[], instance[S_MenuControllerInstance], const bool:dotNot = false) {
new JSON:itemsJson = json_object_get_value(jsonObject, itemsKey, dotNot);
MenuControllerInstance_MakeDefaultFromItemsJson(itemsJson, instance);
json_free(itemsJson);
}

JsonObject_iGetDefaultMenuControllerInstance(const JSON:jsonObject, const itemsKey[], const bool:dotNot = false) {
new instance[S_MenuControllerInstance];
JsonObject_GetDefaultMenuControllerInstance(jsonObject, itemsKey, instance, dotNot);
return instance;
}
34 changes: 33 additions & 1 deletion amxmodx/scripting/EMS/Objects/MenuExtension.inc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,38 @@ MenuExtension_Get(const T_MenuExtension:extension, extensionObject[S_MenuExtensi
ArrayMapGetArray(MenuExtensions, extension, extensionObject);
}

Trie:MenuExtension_ReadParams(const T_MenuExtension:extension, const JSON:paramsJson) {
if (paramsJson == Invalid_JSON) {
abort(AMX_ERR_PARAMS, "paramsJson is an invalid json handler (%d).", paramsJson);
return Invalid_Trie;
}

new extensionObject[S_MenuExtension];
MenuExtension_Get(extension, extensionObject);

new Trie:params = TrieCreate();
if (extensionObject[MenuExtension_Params] != Invalid_Array) {
new E_ParamsReadErrorType:errType = ParamsReadError_None;
new errParamName[PARAM_KEY_MAX_LEN];

ParamsController_Param_ReadList(
extensionObject[MenuExtension_Params], paramsJson, params,
.iErrType = errType,
.sErrParamName = errParamName,
.iErrParamNameLen = charsmax(errParamName)
);

switch (errType) {
case ParamsReadError_RequiredParamNotPresented:
abort(AMX_ERR_PARAMS, "Extension '%s' parameter '%s' is required but not presented.", extensionObject[MenuExtension_Key], errParamName);
case ParamsReadError_ParamValueIsInvalid:
abort(AMX_ERR_PARAMS, "Extension '%s' parameter '%s' has invalid value.", extensionObject[MenuExtension_Key], errParamName);
}
}

return params;
}

T_MenuExtension:MenuExtension_Find(const extensionKey[], const bool:orFail = false) {
if (!ArrayMapKeyExists(MenuExtensions, extensionKey)) {
if (orFail) {
Expand All @@ -37,7 +69,7 @@ T_MenuExtension:MenuExtension_Find(const extensionKey[], const bool:orFail = fal
return Invalid_MenuExtension;
}

return ArrayMapGetIndex(MenuExtensions, extensionKey);
return T_MenuExtension:ArrayMapGetIndex(MenuExtensions, extensionKey);
}

MenuExtension_CallMakeTitle(const T_MenuExtension:extension, title[], const Trie:params) {
Expand Down
Loading

0 comments on commit b1eada5

Please sign in to comment.