66#pragma semicolon 1
77#pragma newdecls required
88
9- #define PLUGIN_VERSION " 23w31a "
9+ #define PLUGIN_VERSION " 23w31b "
1010
1111#define STR_NO_PROFILE " None"
1212#define STR_PERSONAL " Personal"
@@ -34,10 +34,12 @@ ArrayList profiles;
3434
3535Cookie mcpct_style ; //what to color
3636Cookie mcpct_profile ; //profile name
37+ Cookie mcpct_crc ; //if the crc changes, we have a new profile
3738
3839GlobalForward mcpct_fwd_change ;
3940
4041ConVar cvar_settingsMenuEnabled ;
42+ ConVar cvar_loadBehaviour ;
4143
4244public APLRes AskPluginLoad2 (Handle myself , bool late , char [] error , int err_max )
4345{
@@ -54,19 +56,19 @@ public void OnPluginStart() {
5456
5557 mcpct_style = new Cookie (" mcpchattag_style" , " Style of MCP Chat Tag" , CookieAccess_Private );
5658 mcpct_profile = new Cookie (" mcpchattag_profile" , " Style of MCP Chat Tag" , CookieAccess_Private );
59+ mcpct_crc = new Cookie (" mcpchattag_checksum" , " CRC of available profiles to detect changes" , CookieAccess_Private );
5760
5861 mcpct_fwd_change = CreateGlobalForward (" MCP_CT_OnProfileChanged" , ET_Event , Param_Cell , Param_String , Param_String , Param_String , Param_CellByRef );
5962
6063 SetCookieMenuItem (ChatTagCookieMenu , 0 , " Chat Tag Settings" );
6164
6265 cvar_settingsMenuEnabled = CreateConVar (" chattag_menu_enabled" , " 1" , " 0=Disable the /settings menu, 1=Enable" , _ , true , 0.0 , true , 1.0 );
66+ cvar_loadBehaviour = CreateConVar (" chattag_load_behaviour" , " 2" , " What to do when a client connects. 0=Use last active profil, 1=Use forst matching profile, 2=Like 1 if available profiles changed" , _ , true , 0.0 , true , 2.0 );
6367
6468 RegAdminCmd (" sm_reloadchattags" , Cmd_Reload , ADMFLAG_CONFIG , " Reload chat tags" );
6569
6670 for (int client = 1 ;client <= MaxClients ;client ++ ) {
67- if (IsClientInGame (client ) && ! IsFakeClient (client ) && ! IsClientReplay (client ) && ! IsClientSourceTV (client ) && IsClientAuthorized (client )) {
68- OnClientAuthorized (client , " " );
69- }
71+ OnClientPostAdminCheck (client );
7072 }
7173}
7274
@@ -76,31 +78,52 @@ public void ChatTagCookieMenu(int client, CookieMenuAction action, any info, cha
7678 }
7779}
7880
79- public Action Cmd_Reload (int admin , int args )
80- {
81+ public Action Cmd_Reload (int admin , int args ) {
8182 LoadConfig ();
8283 for (int client = 1 ;client <= MaxClients ;client ++ ) {
83- if (IsClientInGame (client ) && ! IsFakeClient (client ) && ! IsClientReplay (client ) && ! IsClientSourceTV (client ) && IsClientAuthorized (client )) {
84- OnClientAuthorized (client , " " );
85- }
84+ OnClientPostAdminCheck (client );
8685 }
8786 ReplyToCommand (admin , " [ChatTag] Reloaded %d profiles" , profiles .Length );
8887 return Plugin_Handled ;
8988}
9089
91-
92- public void OnClientAuthorized (int client , const char [] auth ) {
90+ public void OnClientPostAdminCheck (int client ) {
91+ if (! IsClientInGame (client ) || IsFakeClient (client ) || IsClientReplay (client ) || IsClientSourceTV (client ) || ! IsClientAuthorized (client ))
92+ return ;
93+
9394 char tmp [32 ];
94- cvar_settingsMenuEnabled .GetString (tmp , sizeof (tmp ));
95- if (StringToInt (tmp )== 0 ) {
96- ArrayList list = FindApplicableProfiles (client );
97- if (list .Length > 0 ) {
98- list .GetString (0 , tmp , sizeof (tmp ));
99- mcpct_profile .Set (client , tmp );
100- mcpct_style .Set (client , " 15" );
95+ ArrayList list = FindApplicableProfiles (client );
96+
97+ //check profile hash
98+ // // make crc16
99+ int crc ;
100+ for (int i ; i < list .Length ; i ++ ) {
101+ list .GetString (i , tmp , sizeof (tmp ));
102+ for (int c ; tmp [c ] != 0 ; c += 2 ) {
103+ crc += (tmp [c ]<< 8 )| (tmp [c + 1 ]);
104+ if ((crc & 0x10000 )!= 0 ) crc = (crc & 0xffff )+ 1 ;
105+ if (tmp [c + 1 ]== 0 ) break ; //next loop would be oob
101106 }
102- delete list ;
103107 }
108+ // // get old hash
109+ mcpct_crc .Get (client , tmp , sizeof (tmp ));
110+ int oldCrc = StringToInt (tmp ,16 );
111+ // // store new hash
112+ FormatEx (tmp , sizeof (tmp ), " %04X " , crc );
113+ mcpct_crc .Set (client , tmp );
114+ // // get load behaviour
115+ cvar_loadBehaviour .GetString (tmp , sizeof (tmp ));
116+ int load = StringToInt (tmp );
117+ // // should we refresh?
118+ bool refresh = ((crc != oldCrc && load == 2 ) || load == 1 );
119+
120+ //if profile should refresh, pick the first match and save
121+ if (refresh && list .Length > 0 ) {
122+ list .GetString (0 , tmp , sizeof (tmp ));
123+ mcpct_profile .Set (client , tmp );
124+ mcpct_style .Set (client , " 15" );
125+ }
126+ delete list ;
104127 UpdateProfile (client );
105128}
106129
@@ -120,11 +143,14 @@ void LoadConfig() {
120143 ChatStyle style ;
121144 if (kv .GotoFirstSubKey ()) {
122145 do {
146+ bool isPersonal ;
123147 kv .GetSectionName (buffer , sizeof (buffer ));
124148 if (strncmp (buffer ," STEAM_" ,6 )== 0 || buffer [0 ]== ' [' ) {
149+ isPersonal = true ;
125150 strcopy (style .filter , sizeof (ChatStyle ::filter ), buffer );
126151 style .name = STR_PERSONAL ;
127152 } else {
153+ isPersonal = false ;
128154 kv .GetString (" flag" , style .filter , sizeof (ChatStyle ::filter ), " " );
129155 strcopy (style .name , sizeof (ChatStyle ::name ), buffer );
130156 }
@@ -142,7 +168,14 @@ void LoadConfig() {
142168 kv .GetString (" textcolor" , buffer , sizeof (buffer ), " \x01 " );
143169 TranslateColor (buffer , sizeof (buffer ));
144170 strcopy (style .chat , sizeof (ChatStyle ::chat ), buffer );
145- profiles .PushArray (style );
171+ if (isPersonal ) {
172+ //insert front
173+ profiles .ShiftUp (0 );
174+ profiles .SetArray (0 , style );
175+ } else {
176+ //push back
177+ profiles .PushArray (style );
178+ }
146179 } while (kv .GotoNextKey ());
147180 }
148181
@@ -169,10 +202,11 @@ bool UpdateProfile(int client) {
169202 ChatStyle prof ;
170203 for (int i = profiles .Length - 1 ; i >= 0 ; i -- ) {
171204 profiles .GetArray (i ,prof );
172- if (StrEqual (prof .name , name , false )) {
205+ if ( StrEqual (prof .name , name , false ) ||
206+ (StrEqual (STR_PERSONAL , name ) && (prof .filter [0 ]== ' [' || strncmp (prof .filter , " STEAM_" , 6 )== 0 )) ) {
173207
174208 // can we still use this group?
175- if (! HasPermission (client , prof .filter )) break ;
209+ if (! HasPermission (client , prof .filter )) continue ;
176210
177211 prof .apply (client , style );
178212 return true ;
@@ -184,7 +218,7 @@ bool UpdateProfile(int client) {
184218
185219void ProcessTagStyle (char [] tag , int taglen , ChatStyleOptions style ) {
186220 char tagcopy [MCP_MAXLENGTH_NAME ];
187- if (style != CS_NONE ) {
221+ if (( style & CS_PREFIX ) != CS_NONE ) {
188222 strcopy (tagcopy , sizeof (tagcopy ), tag );
189223 if ((style & CS_TAGTEXT )== CS_NONE ) style &=~ CS_TAGCOLOR ; //no point w/o text
190224 switch (style & CS_PREFIX ) {
@@ -247,7 +281,8 @@ ArrayList FindApplicableProfiles(int client) {
247281 return applicable ;
248282 }
249283 ChatStyle prof ;
250- for (int i = profiles .Length - 1 ; i >= 0 ; i -- ) {
284+ int max = profiles .Length ;
285+ for (int i ; i < max ; i ++ ) {
251286 profiles .GetArray (i ,prof );
252287
253288 // can we use this group?
@@ -387,16 +422,16 @@ void ShowChatTagProfileMenu(int client, int page=1) {
387422 int at = choices .FindString (STR_PERSONAL );
388423 if (at >= 0 ) {
389424 choices .Erase (at );
390- if (StrEqual (STR_NO_PROFILE , active )) {
425+ if (StrEqual (STR_PERSONAL , active )) {
391426 FormatEx (buffer , sizeof (buffer ), " [%s ]" , STR_PERSONAL );
392427 menu .AddItem (STR_PERSONAL , buffer , ITEMDRAW_DISABLED );
393428 } else {
394429 menu .AddItem (STR_PERSONAL , STR_PERSONAL );
395430 }
396431 }
397432
398- choices .Sort ( Sort_Descending , Sort_String ) ;
399- for (int i = choices . Length - 1 ; i >= 0 ; i -- ) {
433+ int max = choices .Length ;
434+ for (int i = 0 ; i < max ; i += 1 ) {
400435 choices .GetString (i , name , sizeof (name ));
401436 if (StrEqual (name , active )) {
402437 FormatEx (buffer , sizeof (buffer ), " [%s ]" , name );
@@ -426,9 +461,9 @@ public int ChatTagProfileMenuHandler(Menu menu, MenuAction action, int param1, i
426461 }
427462 } else if (action == MenuAction_Select ) {
428463 char buffer [32 ], name [32 ];
429- menu .GetItem (param2 , buffer , sizeof (buffer ));
464+ menu .GetItem (param2 , buffer , 0 , _ , name , sizeof (name )); //info buffer is too small
465+ if (! StrEqual (name , STR_NO_PROFILE )) buffer = name ;
430466 mcpct_profile .Set (param1 , buffer );
431- name = (buffer [0 ]== 0 ) ? STR_NO_PROFILE : buffer ;
432467 if (UpdateProfile (param1 )) {
433468 PrintToChat (param1 , " [ChatTag] You activate profile \" %s \" " , name );
434469 } else {
0 commit comments