Skip to content

Commit d2d59bb

Browse files
committed
Update mcp-chattags to automatically switch profiles when permissions change
1 parent 29dd9c3 commit d2d59bb

2 files changed

Lines changed: 50 additions & 13 deletions

File tree

Modules.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@ Replacement for Custom Chat-Colors and Custom Chat-Colors Toggle.
99

1010
Config is compatible, but you should also be able to key on flag groups and overrides.
1111

12-
A difference to CCC Toggle is, that players can select which of the profiles they want active.
13-
If you want it to behave like CCC without CCC Toggle, just activating the first profile, you can
14-
set the convar `chattag_menu_enabled` to 0.
12+
A difference to CCC Toggle is, that players can select which of the profiles they
13+
want active. If you want it to behave like CCC without CCC Toggle, you can set the
14+
convar `chattag_menu_enabled` to 0.
15+
16+
Setting the convar `chattag_load_behaviour` to 1, as well will also reload the
17+
first matching profile every time a player connects, while zero will not change
18+
the profile on connect.
19+
20+
If you set `chattag_load_behaviour` to 2, it will try to detect changes in access to
21+
profiles, and switch profile, if the list of accessible profiles changes for a user.
22+
This is intended to help with ranks that get unlocked externally, so users don't have
23+
to know /settings is a thing. It is not possible to detect what profile excatly was
24+
granted or revoked permission to, so it will always pick first in the config.
1525

1626
Reloading the config can be done with the ADMFLAG_CONFIG command `sm_reloadchattags`.
1727

scripting/mcp-chattags.sp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ ArrayList profiles;
3434

3535
Cookie mcpct_style; //what to color
3636
Cookie mcpct_profile; //profile name
37+
Cookie mcpct_crc; //if the crc changes, we have a new profile
3738

3839
GlobalForward mcpct_fwd_change;
3940

4041
ConVar cvar_settingsMenuEnabled;
42+
ConVar cvar_loadBehaviour;
4143

4244
public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max)
4345
{
@@ -54,12 +56,14 @@ 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_settingsMenuEnabled = 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

@@ -91,16 +95,39 @@ public Action Cmd_Reload(int admin, int args)
9195

9296
public void OnClientAuthorized(int client, const char[] auth) {
9397
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");
98+
bool refresh;
99+
ArrayList list = FindApplicableProfiles(client);
100+
101+
//check profile hash
102+
// // make crc16
103+
int crc;
104+
for (int i; i<=list.Length; i++) {
105+
list.GetString(i, tmp, sizeof(tmp));
106+
for (int c; tmp[c] != 0; c+=2) {
107+
crc += (tmp[c]<<8)|(tmp[c+1]);
108+
if ((crc & 0x10000)!=0) crc = (crc&0xffff)+1;
109+
if (tmp[c+1]==0) break; //next loop would be oob
101110
}
102-
delete list;
103111
}
112+
// // get old hash
113+
mcpct_crc.Get(client, tmp, sizeof(tmp));
114+
int oldCrc = StringToInt(tmp,16);
115+
// // store new hash
116+
FormatEx(tmp, sizeof(tmp), "%04X", crc);
117+
mcpct_crc.Set(client, tmp);
118+
// // get load behaviour
119+
cvar_loadBehaviour.GetString(tmp, sizeof(tmp));
120+
int load = StringToInt(tmp);
121+
// // should we refresh?
122+
refresh |= ((crc != oldCrc && load==2) || load==1);
123+
124+
//if profile should refresh, pick the first match and save
125+
if (refresh && list.Length>0) {
126+
list.GetString(0, tmp, sizeof(tmp));
127+
mcpct_profile.Set(client, tmp);
128+
mcpct_style.Set(client, "15");
129+
}
130+
delete list;
104131
UpdateProfile(client);
105132
}
106133

@@ -395,8 +422,8 @@ void ShowChatTagProfileMenu(int client, int page=1) {
395422
}
396423
}
397424

398-
choices.Sort(Sort_Descending, Sort_String);
399-
for (int i=choices.Length-1; i>=0; i--) {
425+
int max=choices.Length;
426+
for (int i=0; i<max; i+=1) {
400427
choices.GetString(i, name, sizeof(name));
401428
if (StrEqual(name, active)) {
402429
FormatEx(buffer, sizeof(buffer), "[%s]", name);

0 commit comments

Comments
 (0)