Skip to content

Commit

Permalink
Impl menu extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ArKaNeMaN committed Jan 4, 2025
1 parent a6a0e16 commit 64ae722
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
],
"Title": "Привет, {m:playerName}!",
"Extensions": [
"PlayerPlaceholders"
{
"Extension": "Test",
"Prefix": "JopA"
}
// "PlayerPlaceholders"
],
"Items": [
{
Expand Down
37 changes: 34 additions & 3 deletions amxmodx/scripting/EMS/API/MenuExtension.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,38 @@
#include "EMS/Objects/MenuExtension"

API_MenuExtension_Init() {
// TODO
// register_native("MenuExtension_Register", "@_MenuExtension_Register");
// register_native("MenuExtension_AddParams", "@_MenuExtension_AddParams");
register_native("EMS_MenuExtension_Register", "@_MenuExtension_Register");
register_native("EMS_MenuExtension_AddParams", "@_MenuExtension_AddParams");
register_native("EMS_MenuExtension_RegisterEventHandler", "@_MenuExtension_RegisterEventHandler");
}

T_MenuExtension:@_MenuExtension_Register() {
enum {Arg_Key = 1};

new extensionKey[EMS_EXTENSION_KEY_MAX_LEN];
get_string(Arg_Key, extensionKey, charsmax(extensionKey));

return MenuExtension_Construct(extensionKey);
}

@_MenuExtension_AddParams(const pluginIndex, const paramsCount) {
enum {Arg_Extension = 1, Arg_Params};

new T_MenuExtension:extension = T_MenuExtension:get_param(Arg_Extension);
new Array:newParams = ParamsController_Param_ListFromNativeParams(Arg_Params, paramsCount);

MenuExtension_AddParams(extension, newParams);

ArrayDestroy(newParams);
}

@_MenuExtension_RegisterEventHandler(const pluginIndex) {
enum {Arg_Extension = 1, Arg_Event = 2, Arg_Callback};

new T_MenuExtension:extension = T_MenuExtension:get_param(Arg_Extension);
new E_MenuEvent:event = E_MenuEvent:get_param(Arg_Event);
new callback[EMS_EVENT_HANDLER_MAX_LEN];
get_string(Arg_Callback, callback, charsmax(callback));

MenuExtesions_SetEventHandler(extension, event, Menu_MakeEventHandler(event, pluginIndex, callback));
}
83 changes: 9 additions & 74 deletions amxmodx/scripting/EMS/DefaultObjects.inc
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,28 @@
#include <ems>
#include "EMS/Objects/MenuController"

#include "EMS/DefaultObjects/Controllers/Default"
#include "EMS/DefaultObjects/Controllers/Players"
#include "EMS/DefaultObjects/Extensions/Test"

static T_MenuController:DefaultControllerHandler = Invalid_MenuController;
static T_MenuController:PlayersControllerHandler = Invalid_MenuController;
static T_MenuExtension:TestExtensionHandler = Invalid_MenuExtension;

DefaultObjects_Register() {
if (DefaultControllerHandler == Invalid_MenuController) {
DefaultControllerHandler = EMS_MenuController_Register(EMS_DEFAULT_CONTROLLER_KEY, "@DefaultObjects_MenuController_Default");
EMS_MenuController_AddParams(DefaultControllerHandler,
"Items", EMS_MENU_ITEMS_PARAM_TYPE, true
);
DefaultControllerHandler = DefaultObjects_DefaultController_Register();
}

if (PlayersControllerHandler == Invalid_MenuController) {
PlayersControllerHandler = EMS_MenuController_Register(EMS_PLAYERS_CONTROLLER_KEY, "@DefaultObjects_MenuController_Players");
EMS_MenuController_AddParams(PlayersControllerHandler,
// TODO: title template
"Title", "String", false,
"Command", "String", true,
"ExcludeDead", "Boolean", false
);
PlayersControllerHandler = DefaultObjects_PlayersController_Register();
}
}

@DefaultObjects_MenuController_Players(const playerIndex, const menu, const Trie:params) {
static titleTemplate[EMS_MENU_TITLE_MAX_LEN], title[EMS_MENU_TITLE_MAX_LEN];
TrieGetString(params, "Title", titleTemplate, charsmax(titleTemplate));

if (titleTemplate[0] == EOS) {
copy(title, charsmax(title), "{i:playerName}");
}

static commandTemplate[EMS_MENU_ITEM_COMMAND_MAX_LEN], command[EMS_MENU_ITEM_COMMAND_MAX_LEN];
TrieGetString(params, "Command", commandTemplate, charsmax(commandTemplate));

log_amx("Title tpl: %s, Command tpl: %s", titleTemplate, commandTemplate);


new bool:excludeDead = false;
TrieGetCell(params, "ExcludeDead", excludeDead);

for (new i = 1; i <= MAX_PLAYERS; ++i) {
if (!is_user_connected(i)) {
continue;
}

if (excludeDead && !is_user_alive(i)) {
continue;
}

copy(title, charsmax(title), titleTemplate);
copy(command, charsmax(command), commandTemplate);

static name[32];
get_user_name(i, name, charsmax(name));
static index[11];
num_to_str(i, index, charsmax(index));

// TODO: мб сделать что-то типа компиляции шаблонов
replace_all(title, charsmax(title), "{i:playerIndex}", index);
replace_all(title, charsmax(title), "{i:playerName}", name);

replace_all(command, charsmax(command), "{i:playerIndex}", index);
replace_all(command, charsmax(command), "{i:playerName}", name);

log_amx("Title: %s, Command: %s", title, command);

// TODO: юзать прослойку с эмитом ивентов
menu_additem(menu, title, command);
if (TestExtensionHandler == Invalid_MenuExtension) {
TestExtensionHandler = DefaultObjects_TestExtension_Register();
}
}

T_MenuController:DefaultObjects_GetDefaultController() {
return DefaultControllerHandler;
}


@DefaultObjects_MenuController_Default(const playerIndex, const menu, const Trie:params) {
new Array:items = Invalid_Array;
if (!TrieGetCell(params, "Items", items) || items == Invalid_Array) {
abort(AMX_ERR_PARAMS, "Failed to get items array.");
return;
}

for (new i = 0, ii = ArraySize(items); i < ii; ++i) {
new menuItemData[S_Param_MenuItem];
ArrayGetArray(items, i, menuItemData);

// TODO: юзать прослойку с эмитом ивентов
menu_additem(menu, menuItemData[Param_MenuItem_Title], menuItemData[Param_MenuItem_Command]);
}
}
32 changes: 32 additions & 0 deletions amxmodx/scripting/EMS/DefaultObjects/Controllers/Default.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#if defined _ems_src__default_objects__controllers__default_included
#endinput
#endif
#define _ems_src__default_objects__controllers__default_included

#include <amxmodx>
#include <ems>

T_MenuController:DefaultObjects_DefaultController_Register() {
new T_MenuController:handler = EMS_MenuController_Register(EMS_DEFAULT_CONTROLLER_KEY, "@DefaultObjects_DefaultController_Callback");
EMS_MenuController_AddParams(handler,
"Items", EMS_MENU_ITEMS_PARAM_TYPE, true
);

return handler;
}

@DefaultObjects_DefaultController_Callback(const playerIndex, const menu, const Trie:params) {
new Array:items = Invalid_Array;
if (!TrieGetCell(params, "Items", items) || items == Invalid_Array) {
abort(AMX_ERR_PARAMS, "Failed to get items array.");
return;
}

for (new i = 0, ii = ArraySize(items); i < ii; ++i) {
new menuItemData[S_Param_MenuItem];
ArrayGetArray(items, i, menuItemData);

// TODO: юзать прослойку с эмитом ивентов
menu_additem(menu, menuItemData[Param_MenuItem_Title], menuItemData[Param_MenuItem_Command]);
}
}
63 changes: 63 additions & 0 deletions amxmodx/scripting/EMS/DefaultObjects/Controllers/Players.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#if defined _ems_src__default_objects__controllers__players_included
#endinput
#endif
#define _ems_src__default_objects__controllers__players_included

#include <amxmodx>
#include <ems>

T_MenuController:DefaultObjects_PlayersController_Register() {
new T_MenuController:handler = EMS_MenuController_Register(EMS_PLAYERS_CONTROLLER_KEY, "@DefaultObjects_PlayersController_Callback");
EMS_MenuController_AddParams(handler,
// TODO: title template
"Title", "String", false,
"Command", "String", true,
"ExcludeDead", "Boolean", false
);

return handler;
}

@DefaultObjects_PlayersController_Callback(const playerIndex, const menu, const Trie:params) {
static titleTemplate[EMS_MENU_TITLE_MAX_LEN], title[EMS_MENU_TITLE_MAX_LEN];
TrieGetString(params, "Title", titleTemplate, charsmax(titleTemplate));

if (titleTemplate[0] == EOS) {
copy(title, charsmax(title), "{i:playerName}");
}

static commandTemplate[EMS_MENU_ITEM_COMMAND_MAX_LEN], command[EMS_MENU_ITEM_COMMAND_MAX_LEN];
TrieGetString(params, "Command", commandTemplate, charsmax(commandTemplate));


new bool:excludeDead = false;
TrieGetCell(params, "ExcludeDead", excludeDead);

for (new i = 1; i <= MAX_PLAYERS; ++i) {
if (!is_user_connected(i)) {
continue;
}

if (excludeDead && !is_user_alive(i)) {
continue;
}

copy(title, charsmax(title), titleTemplate);
copy(command, charsmax(command), commandTemplate);

static name[32];
get_user_name(i, name, charsmax(name));
static index[11];
num_to_str(i, index, charsmax(index));

// TODO: мб сделать что-то типа компиляции шаблонов
replace_all(title, charsmax(title), "{i:playerIndex}", index);
replace_all(title, charsmax(title), "{i:playerName}", name);

replace_all(command, charsmax(command), "{i:playerIndex}", index);
replace_all(command, charsmax(command), "{i:playerName}", name);

// TODO: юзать прослойку с эмитом ивентов
menu_additem(menu, title, command);
}
}
47 changes: 47 additions & 0 deletions amxmodx/scripting/EMS/DefaultObjects/Extensions/Test.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#if defined _ems_src__default_objects__extensions__test_included
#endinput
#endif
#define _ems_src__default_objects__extensions__test_included

#include <amxmodx>
#include <ems>
#include <ParamsController>

T_MenuExtension:DefaultObjects_TestExtension_Register() {
new T_MenuExtension:handler = EMS_MenuExtension_Register("Test");
EMS_MenuExtension_AddParams(handler,
"Prefix", "ShortString", false
);
EMS_MenuExtension_RegisterEventHandler(handler, MenuEvent_MakeTitle, "@DefaultObjects_TestExtension_MakeTitle");
EMS_MenuExtension_RegisterEventHandler(handler, MenuEvent_Create, "@DefaultObjects_TestExtension_Create");
EMS_MenuExtension_RegisterEventHandler(handler, MenuEvent_MakeItem, "@DefaultObjects_TestExtension_MakeItem");

return handler;
}

@DefaultObjects_TestExtension_MakeTitle(title[EMS_MENU_TITLE_MAX_LEN], const Trie:params) {
new prefix[PARAM_SHORT_STRING_MAX_LEN];
TrieGetString(params, "Prefix", prefix, charsmax(prefix));
if (prefix[0] == EOS) {
copy(prefix, charsmax(prefix), "Test");
}

// Надо бы прокидывать длину буфера в колбек
format(title, charsmax(title), "[%s] %s", prefix, title);
}

@DefaultObjects_TestExtension_Create(const menu, const Trie:params) {
// Хз чё тут делать)
// TODO: Придумать
}

@DefaultObjects_TestExtension_MakeItem(title[EMS_MENU_ITEM_TITLE_MAX_LEN], command[EMS_MENU_ITEM_COMMAND_MAX_LEN], const Trie:params) {
new prefix[PARAM_SHORT_STRING_MAX_LEN];
TrieGetString(params, "Prefix", prefix, charsmax(prefix));
if (prefix[0] == EOS) {
copy(prefix, charsmax(prefix), "Test");
}

format(title, charsmax(title), "[%s] %s", prefix, title);
format(command, charsmax(command), "say ^"%s^";%s", prefix, command);
}
10 changes: 5 additions & 5 deletions amxmodx/scripting/EMS/Objects/Menu.inc
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,22 @@ Menu_MakeEventHandler(const E_MenuEvent:event, const pluginIndex, const callback
switch (event) {
case MenuEvent_MakeTitle:
// (title[], const Trie:params)
fwd = CreateOneForward(pluginIndex, callback, FP_STRING, FP_CELL);
fwd = CreateOneForward(pluginIndex, callback, FP_ARRAY, FP_CELL);
// fwd = CreateOneForward(pluginIndex, callback, FP_STRING, FP_CELL);
case MenuEvent_Create:
// (const menu, const Trie:params)
fwd = CreateOneForward(pluginIndex, callback, FP_CELL, FP_CELL);
case MenuEvent_Create:
case MenuEvent_MakeItem:
// (title[], command[], const Trie:params)
fwd = CreateOneForward(pluginIndex, callback, FP_STRING, FP_STRING, FP_CELL);
fwd = CreateOneForward(pluginIndex, callback, FP_ARRAY, FP_ARRAY, FP_CELL);
// fwd = CreateOneForward(pluginIndex, callback, FP_STRING, FP_STRING, FP_CELL);
default: {
abort(AMX_ERR_PARAMS, "Invalid menu event (%d).", event);
return;
}
}

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

return fwd;
Expand Down
Loading

0 comments on commit 64ae722

Please sign in to comment.