From db27bdce14cbcdb6e316e361f715f16d9c6d30a5 Mon Sep 17 00:00:00 2001 From: kidfearless Date: Tue, 1 Dec 2020 08:43:06 -0700 Subject: [PATCH] Added Shavit_GetStyleSetting, deprecated Shavit_GetStyleSettings --- addons/sourcemod/scripting/include/shavit.inc | 19 ++++++- addons/sourcemod/scripting/shavit-core.sp | 49 ++++++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/addons/sourcemod/scripting/include/shavit.inc b/addons/sourcemod/scripting/include/shavit.inc index 79f5c647..246e74ea 100644 --- a/addons/sourcemod/scripting/include/shavit.inc +++ b/addons/sourcemod/scripting/include/shavit.inc @@ -1533,6 +1533,19 @@ native void Shavit_OpenStatsMenu(int client, int steamid); */ native int Shavit_GetWRCount(int client); +/* + * Gets a value from the style config for the given style + * e.g. Shavit_GetStyleSetting(Shavit_GetBhopStyle(client), "TAS", sBuffer, 2); + * + * @param style Style index. + * @param key Style key to retreive. + * @param value Value buffer to store the return value in. + * @param maxlength Max length of the value buffer, cannot exceed 256. + * + * @return True if key was found, false otherwise. + */ +native bool Shavit_GetStyleSetting(int style, const char[] key, char[] value, int maxlength); + /** * Saves the style settings on `any` references. * @@ -1541,6 +1554,7 @@ native int Shavit_GetWRCount(int client); * @param size Size of the StyleSettings buffer, e.g sizeof(stylesettings_t) * @return SP_ERROR_NONE on success, anything else on failure. */ +#pragma deprecated Use different natives or Shavit_GetStyleSetting instead. native int Shavit_GetStyleSettings(int style, any[] StyleSettings, int size = sizeof(stylesettings_t)); /** @@ -1913,7 +1927,7 @@ native int Shavit_GetPlayerPreFrame(int client); * * @return Timer preframe count */ - native int Shavit_GetPlayerTimerFrame(int client); +native int Shavit_GetPlayerTimerFrame(int client); /* * Sets player's preframe length. @@ -1934,7 +1948,7 @@ native void Shavit_SetPlayerPreFrame(int client, int PreFrame); * * @noreturn */ - native void Shavit_SetPlayerTimerFrame(int client, int TimerPreFrame); +native void Shavit_SetPlayerTimerFrame(int client, int TimerPreFrame); // same as Shavit_PrintToChat() but loops through the whole server // code stolen from the base halflife.inc file @@ -2010,6 +2024,7 @@ public void __pl_shavit_SetNTVOptional() MarkNativeAsOptional("Shavit_GetStageZone"); MarkNativeAsOptional("Shavit_GetStrafeCount"); MarkNativeAsOptional("Shavit_GetStyleCount"); + MarkNativeAsOptional("Shavit_GetStyleSetting"); MarkNativeAsOptional("Shavit_GetStyleSettings"); MarkNativeAsOptional("Shavit_GetStyleStrings"); MarkNativeAsOptional("Shavit_GetSync"); diff --git a/addons/sourcemod/scripting/shavit-core.sp b/addons/sourcemod/scripting/shavit-core.sp index 5357c069..fed2d44c 100644 --- a/addons/sourcemod/scripting/shavit-core.sp +++ b/addons/sourcemod/scripting/shavit-core.sp @@ -159,6 +159,8 @@ int gI_Styles = 0; int gI_OrderedStyles[STYLE_LIMIT]; stylestrings_t gS_StyleStrings[STYLE_LIMIT]; stylesettings_t gA_StyleSettings[STYLE_LIMIT]; +StringMap gSM_StyleKeys[STYLE_LIMIT]; +int gI_CurrentParserIndex = 0; // chat settings chatstrings_t gS_ChatStrings; @@ -207,6 +209,7 @@ public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max CreateNative("Shavit_GetPerfectJumps", Native_GetPerfectJumps); CreateNative("Shavit_GetStrafeCount", Native_GetStrafeCount); CreateNative("Shavit_GetStyleCount", Native_GetStyleCount); + CreateNative("Shavit_GetStyleSetting", Native_GetStyleSetting); CreateNative("Shavit_GetStyleSettings", Native_GetStyleSettings); CreateNative("Shavit_GetStyleStrings", Native_GetStyleStrings); CreateNative("Shavit_GetSync", Native_GetSync); @@ -2028,6 +2031,22 @@ public int Native_SetClientTimescale(Handle handler, int numParams) } } +public int Native_GetStyleSetting(Handle handler, int numParams) +{ + int style = GetNativeCell(1); + + char sKey[256]; + GetNativeString(2, sKey, 256); + + int maxlength = GetNativeCell(4); + + char sValue[256]; + bool ret = gSM_StyleKeys[style].GetString(sKey, sValue, maxlength); + + SetNativeString(3, sValue, maxlength); + return ret; +} + int GetTimerStatus(int client) { if(!gA_Timers[client].bEnabled) @@ -2300,6 +2319,12 @@ bool LoadStyles() char sPath[PLATFORM_MAX_PATH]; BuildPath(Path_SM, sPath, PLATFORM_MAX_PATH, "configs/shavit-styles.cfg"); + SMCParser parser = new SMCParser(); + parser.OnEnterSection = OnStyleEnterSection; + parser.OnKeyValue = OnStyleKeyValue; + parser.ParseFile(sPath); + delete parser; + KeyValues kv = new KeyValues("shavit-styles"); if(!kv.ImportFromFile(sPath) || !kv.GotoFirstSubKey()) @@ -2312,7 +2337,7 @@ bool LoadStyles() int i = 0; do - { + { kv.GetString("name", gS_StyleStrings[i].sStyleName, sizeof(stylestrings_t::sStyleName), ""); kv.GetString("shortname", gS_StyleStrings[i].sShortName, sizeof(stylestrings_t::sShortName), ""); kv.GetString("htmlcolor", gS_StyleStrings[i].sHTMLColor, sizeof(stylestrings_t::sHTMLColor), ""); @@ -2434,6 +2459,28 @@ bool LoadStyles() return true; } +public SMCResult OnStyleEnterSection(SMCParser smc, const char[] name, bool opt_quotes) +{ + // styles key + if(!IsCharNumeric(name[0])) + { + return SMCParse_Continue; + } + + // Technically can lead to a small memory leak if a style is removed mid game. + // TODO: replace hard coded values with SMCParser logic. + gI_CurrentParserIndex = StringToInt(name); + delete gSM_StyleKeys[gI_CurrentParserIndex]; + gSM_StyleKeys[gI_CurrentParserIndex] = new StringMap(); + + return SMCParse_Continue; +} + +public SMCResult OnStyleKeyValue(SMCParser smc, const char[] key, const char[] value, bool key_quotes, bool value_quotes) +{ + gSM_StyleKeys[gI_CurrentParserIndex].SetString(key, value); +} + public int SortAscending_StyleOrder(int index1, int index2, const int[] array, any hndl) { int iOrder1 = gA_StyleSettings[index1].iOrdering;