Raised This Month: $51 Target: $400
 12% 

A few questions about functions / forwards


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
pcmaster
AlliedModders Donor
Join Date: Sep 2009
Old 01-04-2017 , 17:01   A few questions about functions / forwards
Reply With Quote #1

I am currently writing a (small) plugin for properly handling chat message interception / modification between various different plugins.
For that, I have a few questions for which I couldn't really find any answers for:

a) Is it a good idea to start a second forward while being in a forward?
Example: chat-processor starts forward OnChatMessage, I call my other plugins, then return to the original forward with one result. Is this a no-go or a valid use case?

b) Also regarding forwards, how expensive is it to call RemoveAllFromForward, GetFunctionbyName / FindPluginByFile adding a single function to it, calling the forward and then doing that again a few times (at most 5 times).
This would happen every time a user enters a chat message.

c) Is there any way to get the name of a function returned by GetNativeFunction()?
Reason being that I can't save a function in a stringmap (at least not in a future-safe way), so I have to save its name and call GetFunctionByName, then resolve that later.
To reduce the possibility of making mistakes, however, I would like to enforce a certain function signature when my native is called (which isn't possible by the string method).

Thanks in advance!
__________________
Stopped hosting servers as of November 2018, no longer active around here.
pcmaster is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 01-04-2017 , 20:26   Re: A few questions about functions / forwards
Reply With Quote #2

Quote:
Originally Posted by pcmaster View Post
a) Is it a good idea to start a second forward while being in a forward?
Example: chat-processor starts forward OnChatMessage, I call my other plugins, then return to the original forward with one result. Is this a no-go or a valid use case?
It's fine, and how things normally work.

Quote:
Originally Posted by pcmaster View Post
b) Also regarding forwards, how expensive is it to call RemoveAllFromForward, GetFunctionbyName / FindPluginByFile adding a single function to it, calling the forward and then doing that again a few times (at most 5 times).
This would happen every time a user enters a chat message.
Sounds like you're trying to re-implement global forwards.

Quote:
Originally Posted by pcmaster View Post
c) Is there any way to get the name of a function returned by GetNativeFunction()?
Reason being that I can't save a function in a stringmap (at least not in a future-safe way), so I have to save its name and call GetFunctionByName, then resolve that later.
To reduce the possibility of making mistakes, however, I would like to enforce a certain function signature when my native is called (which isn't possible by the string method).
Sounds like you're trying to re-implement private forwards.
__________________
asherkin is offline
pcmaster
AlliedModders Donor
Join Date: Sep 2009
Old 01-05-2017 , 10:19   Re: A few questions about functions / forwards
Reply With Quote #3

Quote:
Originally Posted by asherkin View Post
Sounds like you're trying to re-implement global forwards.
Sounds like you're trying to re-implement private forwards.
Well, I kinda am.
The original problem is that when I am using multiple plugins which modify the chat (name color / message color / prefix etc.), conflicts are natural.
So if I have, for example, a plugin for admin tags, one for vips, another one for store and a timer one, and the client has tags in all of these, stuff like [VIP] name [ADMIN] name [Custom] name [Ski]: message message message message" would occur.
Therefore, I thought about implementing a central "coordinator", which catches the regular OnChatMessage forward, then calls each plugin on each own, and if the message was modified, returns immediately (sorted by priority), thus avoiding stuff like above (and also adding the possiblity of disabling tags entirely).

On a second thought, maybe Call_StartFunction would be a better idea instead of using a full-blown forward? Or do they basically do the same?
__________________
Stopped hosting servers as of November 2018, no longer active around here.
pcmaster is offline
WildCard65
Veteran Member
Join Date: Aug 2013
Location: Canada
Old 01-06-2017 , 20:23   Re: A few questions about functions / forwards
Reply With Quote #4

sounds like you want a forward's ET_Event or ET_Hook. ET_Event returns first highest result from Action enum, Plugin_Stop doesn't bail call, ET_Hook works like ET_Event but with Plugin_Stop bail.
__________________
WildCard65 is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 01-07-2017 , 05:58   Re: A few questions about functions / forwards
Reply With Quote #5

Quote:
Originally Posted by WildCard65 View Post
sounds like you want a forward's ET_Event or ET_Hook. ET_Event returns first highest result from Action enum, Plugin_Stop doesn't bail call, ET_Hook works like ET_Event but with Plugin_Stop bail.
That wouldn't work since you have no way of associating the changes with the return value.

I think the best solution here is to have natives to set tags, colours, etc. with priority that are designed to be used inside a global forward. In pseudo-code:

Managing plugin:
PHP Code:
/* Include */
enum ChatPriority: {
  
ChatPriority_Invalid = -1,
  
ChatPriority_Lowest 0,
  
ChatPriority_Low 500,
  
ChatPriority_Normal 1000,
  
ChatPriority_High 1500,
  
ChatPriority_Highest 2000,
};

forward OnChatMessage(int client, const char[] msg);

native Chat_SetTag(char[] tagint priority ChatPriority_Normal);
native Chat_SetColor(char[] colorint priority ChatPriority_Normal);

/* Plugin */
char[32currentTag;
int currentTagPriority ChatPriority_Invalid;

char[32currentColor;
int currentColorPriority ChatPriority_Invalid;

public 
int Native_SetTag(Handle pluginint args) {
  
int priority GetNativeCell(2);
  if (
priority currentTagPriority) {
    return;
  }

  
GetNativeString(1currentTagsizeof(currentTag));
}

// You get the idea for the Chat_SetColor handler...

public void OnClientSayCommand(int clientchar[] args) {
  
Call_StartForward(...);
  
Call_PushCell(...);
  
Call_PushString(...);
  
Call_Execute();

  
MagicalChatMessageRewitingFunction(argscurrentTagcurrentColor);

Plugin A:
PHP Code:
public void OnChatMessage(int client, const char[] msg) {
  
Chat_SetTag("A tag for every player!"ChatPriority_Lowest);

Plugin B:
PHP Code:
public void OnChatMessage(int client, const char[] msg) {
  if (
IsClientAdmin(client)) {
    
Chat_SetTag("ADMIN");
    
Chat_SetColor("purple"ChatPriority_Highest);
  }

__________________

Last edited by asherkin; 01-07-2017 at 05:58.
asherkin is offline
pcmaster
AlliedModders Donor
Join Date: Sep 2009
Old 01-07-2017 , 17:34   Re: A few questions about functions / forwards
Reply With Quote #6

Well, I went with my original approach, sub the AddToForward/RemoveAllFromForward nonesense.
Instead, decided to use the Call_StartFunction function, which does exactly what I want (call one function, get its results, and continue to the next one if Plugin_Continue is returned).
Now, the only thing which would be nice is getting the name of a function returned by GetNativeFunction, but I guess this isn't possible without an extension (and currently, the pass-string approach works fine).
__________________
Stopped hosting servers as of November 2018, no longer active around here.
pcmaster is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:51.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode