Skip to content

Commit 4f3c317

Browse files
JoinedSensesHeadline
authored andcommitted
Create GlobalForward & PrivateForward methodmaps (#1004)
1 parent 81dc80f commit 4f3c317

4 files changed

Lines changed: 110 additions & 33 deletions

File tree

core/logic/smn_functions.cpp

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -733,27 +733,36 @@ static cell_t sm_AddFrameAction(IPluginContext *pContext, const cell_t *params)
733733

734734
REGISTER_NATIVES(functionNatives)
735735
{
736-
{"GetFunctionByName", sm_GetFunctionByName},
737-
{"CreateGlobalForward", sm_CreateGlobalForward},
738-
{"CreateForward", sm_CreateForward},
739-
{"GetForwardFunctionCount", sm_GetForwardFunctionCount},
740-
{"AddToForward", sm_AddToForward},
741-
{"RemoveFromForward", sm_RemoveFromForward},
742-
{"RemoveAllFromForward", sm_RemoveAllFromForward},
743-
{"Call_StartFunction", sm_CallStartFunction},
744-
{"Call_StartForward", sm_CallStartForward},
745-
{"Call_PushCell", sm_CallPushCell},
746-
{"Call_PushCellRef", sm_CallPushCellRef},
747-
{"Call_PushFloat", sm_CallPushFloat},
748-
{"Call_PushFloatRef", sm_CallPushFloatRef},
749-
{"Call_PushArray", sm_CallPushArray},
750-
{"Call_PushArrayEx", sm_CallPushArrayEx},
751-
{"Call_PushString", sm_CallPushString},
752-
{"Call_PushStringEx", sm_CallPushStringEx},
753-
{"Call_PushNullVector", sm_CallPushNullVector},
754-
{"Call_PushNullString", sm_CallPushNullString},
755-
{"Call_Finish", sm_CallFinish},
756-
{"Call_Cancel", sm_CallCancel},
757-
{"RequestFrame", sm_AddFrameAction},
758-
{NULL, NULL},
736+
{"GetFunctionByName", sm_GetFunctionByName},
737+
{"CreateGlobalForward", sm_CreateGlobalForward},
738+
{"CreateForward", sm_CreateForward},
739+
{"GetForwardFunctionCount", sm_GetForwardFunctionCount},
740+
{"AddToForward", sm_AddToForward},
741+
{"RemoveFromForward", sm_RemoveFromForward},
742+
{"RemoveAllFromForward", sm_RemoveAllFromForward},
743+
{"Call_StartFunction", sm_CallStartFunction},
744+
{"Call_StartForward", sm_CallStartForward},
745+
{"Call_PushCell", sm_CallPushCell},
746+
{"Call_PushCellRef", sm_CallPushCellRef},
747+
{"Call_PushFloat", sm_CallPushFloat},
748+
{"Call_PushFloatRef", sm_CallPushFloatRef},
749+
{"Call_PushArray", sm_CallPushArray},
750+
{"Call_PushArrayEx", sm_CallPushArrayEx},
751+
{"Call_PushString", sm_CallPushString},
752+
{"Call_PushStringEx", sm_CallPushStringEx},
753+
{"Call_PushNullVector", sm_CallPushNullVector},
754+
{"Call_PushNullString", sm_CallPushNullString},
755+
{"Call_Finish", sm_CallFinish},
756+
{"Call_Cancel", sm_CallCancel},
757+
{"RequestFrame", sm_AddFrameAction},
758+
759+
{"GlobalForward.GlobalForward", sm_CreateGlobalForward},
760+
{"GlobalForward.FunctionCount.get", sm_GetForwardFunctionCount},
761+
762+
{"PrivateForward.PrivateForward", sm_CreateForward},
763+
{"PrivateForward.AddFunction", sm_AddToForward},
764+
{"PrivateForward.RemoveFunction", sm_RemoveFromForward},
765+
{"PrivateForward.RemoveAllFunctions", sm_RemoveAllFromForward},
766+
767+
{NULL, NULL},
759768
};

plugins/adminmenu.sp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public Plugin myinfo =
4848
};
4949

5050
/* Forwards */
51-
Handle hOnAdminMenuReady = null;
52-
Handle hOnAdminMenuCreated = null;
51+
GlobalForward hOnAdminMenuReady;
52+
GlobalForward hOnAdminMenuCreated;
5353

5454
/* Menus */
5555
TopMenu hAdminMenu;
@@ -75,8 +75,8 @@ public void OnPluginStart()
7575
LoadTranslations("common.phrases");
7676
LoadTranslations("adminmenu.phrases");
7777

78-
hOnAdminMenuCreated = CreateGlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
79-
hOnAdminMenuReady = CreateGlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
78+
hOnAdminMenuCreated = new GlobalForward("OnAdminMenuCreated", ET_Ignore, Param_Cell);
79+
hOnAdminMenuReady = new GlobalForward("OnAdminMenuReady", ET_Ignore, Param_Cell);
8080

8181
RegAdminCmd("sm_admin", Command_DisplayMenu, ADMFLAG_GENERIC, "Displays the admin menu");
8282
}

plugins/include/functions.inc

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,74 @@ enum ExecType
111111
* @endsection
112112
*/
113113

114+
methodmap GlobalForward < Handle {
115+
// Creates a global forward.
116+
//
117+
// @note The name used to create the forward is used as its public function in all target plugins.
118+
// @note This is ideal for global, static forwards that are never changed.
119+
// @note Global forwards cannot be cloned.
120+
// @note Use CloseHandle() to destroy these.
121+
//
122+
// @param name Name of public function to use in forward.
123+
// @param type Execution type to be used.
124+
// @param ... Variable number of parameter types (up to 32).
125+
// @return Handle to new global forward.
126+
// @error More than 32 paramater types passed.
127+
public native GlobalForward(const char[] name, ExecType type, ParamType ...);
128+
129+
// Returns the number of functions in a global or private forward's call list.
130+
property int FunctionCount {
131+
public native get();
132+
}
133+
};
134+
135+
methodmap PrivateForward < GlobalForward {
136+
// Creates a private forward.
137+
//
138+
// @note No functions are automatically added. Use AddToForward() to do this.
139+
// @note Private forwards can be cloned.
140+
// @note Use CloseHandle() to destroy these.
141+
//
142+
// @param type Execution type to be used.
143+
// @param ... Variable number of parameter types (up to 32).
144+
// @return Handle to new private forward.
145+
// @error More than 32 paramater types passed.
146+
public native PrivateForward(ExecType type, ParamType ...);
147+
148+
// Adds a function to a private forward's call list.
149+
//
150+
// @note Cannot be used during an incomplete call.
151+
//
152+
// @param plugin Handle of the plugin that contains the function.
153+
// Pass INVALID_HANDLE to specify the calling plugin.
154+
// @param func Function to add to forward.
155+
// @return True on success, false otherwise.
156+
// @error Invalid or corrupt private forward handle, invalid or corrupt plugin handle, or invalid function.
157+
public native bool AddFunction(Handle plugin, Function func);
158+
159+
// Removes a function from a private forward's call list.
160+
//
161+
// @note Only removes one instance.
162+
// @note Functions will be removed automatically if their parent plugin is unloaded.
163+
//
164+
// @param plugin Handle of the plugin that contains the function.
165+
// Pass INVALID_HANDLE to specify the calling plugin.
166+
// @param func Function to remove from forward.
167+
// @return True on success, false otherwise.
168+
// @error Invalid or corrupt private forward handle, invalid or corrupt plugin handle, or invalid function.
169+
public native bool RemoveFunction(Handle plugin, Function func);
170+
171+
// Removes all instances of a plugin from a private forward's call list.
172+
//
173+
// @note Functions will be removed automatically if their parent plugin is unloaded.
174+
//
175+
// @param plugin Handle of the plugin to remove instances of.
176+
// Pass INVALID_HANDLE to specify the calling plugin.
177+
// @return Number of functions removed from forward.
178+
// @error Invalid or corrupt private forward handle or invalid or corrupt plugin handle.
179+
public native int RemoveAllFunctions(Handle plugin);
180+
};
181+
114182
/**
115183
* Gets a function id from a function name.
116184
*
@@ -136,7 +204,7 @@ native Function GetFunctionByName(Handle plugin, const char[] name);
136204
* @return Handle to new global forward.
137205
* @error More than 32 paramater types passed.
138206
*/
139-
native Handle CreateGlobalForward(const char[] name, ExecType type, ParamType ...);
207+
native GlobalForward CreateGlobalForward(const char[] name, ExecType type, ParamType ...);
140208

141209
/**
142210
* Creates a private forward.
@@ -150,7 +218,7 @@ native Handle CreateGlobalForward(const char[] name, ExecType type, ParamType ..
150218
* @return Handle to new private forward.
151219
* @error More than 32 paramater types passed.
152220
*/
153-
native Handle CreateForward(ExecType type, ParamType ...);
221+
native PrivateForward CreateForward(ExecType type, ParamType ...);
154222

155223
/**
156224
* Returns the number of functions in a global or private forward's call list.

plugins/mapchooser.sp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ int g_mapFileSerial = -1;
9595

9696
MapChange g_ChangeTime;
9797

98-
Handle g_NominationsResetForward = null;
99-
Handle g_MapVoteStartedForward = null;
98+
GlobalForward g_NominationsResetForward;
99+
GlobalForward g_MapVoteStartedForward;
100100

101101
/* Upper bound of how many team there could be */
102102
#define MAXTEAMS 10
@@ -181,8 +181,8 @@ public void OnPluginStart()
181181
g_Cvar_Bonusroundtime.SetBounds(ConVarBound_Upper, true, 30.0);
182182
}
183183

184-
g_NominationsResetForward = CreateGlobalForward("OnNominationRemoved", ET_Ignore, Param_String, Param_Cell);
185-
g_MapVoteStartedForward = CreateGlobalForward("OnMapVoteStarted", ET_Ignore);
184+
g_NominationsResetForward = new GlobalForward("OnNominationRemoved", ET_Ignore, Param_String, Param_Cell);
185+
g_MapVoteStartedForward = new GlobalForward("OnMapVoteStarted", ET_Ignore);
186186
}
187187

188188
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)

0 commit comments

Comments
 (0)