|
| 1 | +/** |
| 2 | + * [ANY?] Level KeyValues |
| 3 | + * |
| 4 | + * Parses the level entity string (from SDKHooks' `OnLevelInit` forward) into a KeyValues |
| 5 | + * handle, indexed by Hammer ID. |
| 6 | + */ |
| 7 | +#pragma semicolon 1 |
| 8 | +#pragma dynamic 1048576 |
| 9 | +#include <sourcemod> |
| 10 | + |
| 11 | +#include <sdkhooks> |
| 12 | +#include <regex> |
| 13 | + |
| 14 | +#pragma newdecls required |
| 15 | + |
| 16 | +#define PLUGIN_VERSION "0.0.0" |
| 17 | +public Plugin myinfo = { |
| 18 | + name = "Level KeyValues", |
| 19 | + author = "nosoop", |
| 20 | + description = "Parses the map entity string into a KeyValues structure.", |
| 21 | + version = PLUGIN_VERSION, |
| 22 | + url = "https://github.com/nosoop/SM-LevelKeyValues/" |
| 23 | +} |
| 24 | + |
| 25 | +KeyValues g_MapEntities; |
| 26 | + |
| 27 | +public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max) { |
| 28 | + RegPluginLibrary("level-keyvalues"); |
| 29 | + |
| 30 | + CreateNative("LevelEntity_GetKeysByHammerID", Native_GetKeysByHammerID); |
| 31 | + |
| 32 | + return APLRes_Success; |
| 33 | +} |
| 34 | + |
| 35 | +public Action OnLevelInit(const char[] mapName, char mapEntities[2097152]) { |
| 36 | + if (g_MapEntities) { |
| 37 | + delete g_MapEntities; |
| 38 | + } |
| 39 | + |
| 40 | + g_MapEntities = ParseEntityList(mapEntities); |
| 41 | + |
| 42 | + /** |
| 43 | + * create a forward to allow manipulation of the KV? create a cloned copy? |
| 44 | + * could open up possibilities for dynamically filtering / adding new entities in ways that |
| 45 | + * Stripper:Source doesn't support natively |
| 46 | + */ |
| 47 | + |
| 48 | + return Plugin_Continue; |
| 49 | +} |
| 50 | + |
| 51 | +public int Native_GetKeysByHammerID(Handle plugin, int argc) { |
| 52 | + // native KeyValues LevelEntity_GetKeysByHammerID(int iHammerID); |
| 53 | + int iHammerID = GetNativeCell(1); |
| 54 | + |
| 55 | + char hammerID[64]; |
| 56 | + IntToString(iHammerID, hammerID, sizeof(hammerID)); |
| 57 | + |
| 58 | + if (g_MapEntities && g_MapEntities.JumpToKey(hammerID, false)) { |
| 59 | + // create and transfer ownership of KeyValues |
| 60 | + KeyValues kv = new KeyValues(hammerID); |
| 61 | + KeyValues retval = view_as<KeyValues>(CloneHandle(kv, plugin)); |
| 62 | + |
| 63 | + kv.Import(g_MapEntities); |
| 64 | + g_MapEntities.GoBack(); |
| 65 | + |
| 66 | + delete kv; |
| 67 | + |
| 68 | + return view_as<int>(retval); |
| 69 | + } |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Parses the level entity string into a KeyValues struct. |
| 75 | + * The KeyValues struct organizes keys using the `hammerid` as the section names. |
| 76 | + */ |
| 77 | +static KeyValues ParseEntityList(const char mapEntities[2097152]) { |
| 78 | + static Regex s_KeyValueLine; |
| 79 | + |
| 80 | + if (!s_KeyValueLine) { |
| 81 | + // Pattern copied from alliedmodders/stripper-source/master/parser.cpp |
| 82 | + s_KeyValueLine = new Regex("\"([^\"]+)\"\\s+\"([^\"]+)\""); |
| 83 | + } |
| 84 | + |
| 85 | + KeyValues mapKeyValues = new KeyValues("map_entities"); |
| 86 | + |
| 87 | + int nKeys; |
| 88 | + |
| 89 | + char key[256], value[256]; |
| 90 | + |
| 91 | + int i, n; |
| 92 | + char lineBuffer[4096]; |
| 93 | + while ((n = SplitString(mapEntities[i], "\n", lineBuffer, sizeof(lineBuffer))) != -1) { |
| 94 | + switch(lineBuffer[0]) { |
| 95 | + case '{': { |
| 96 | + char sectionValue[128]; |
| 97 | + Format(sectionValue, sizeof(sectionValue), "__parsed_unknown_%d", nKeys); |
| 98 | + |
| 99 | + mapKeyValues.JumpToKey(sectionValue, true); |
| 100 | + nKeys++; |
| 101 | + } |
| 102 | + case '}': { |
| 103 | + mapKeyValues.GoBack(); |
| 104 | + |
| 105 | + // next open bracket starts on same line |
| 106 | + if (lineBuffer[1] == '{') { |
| 107 | + char sectionValue[128]; |
| 108 | + Format(sectionValue, sizeof(sectionValue), "__parsed_unknown_%d", nKeys); |
| 109 | + |
| 110 | + mapKeyValues.JumpToKey(sectionValue, true); |
| 111 | + nKeys++; |
| 112 | + } |
| 113 | + } |
| 114 | + default: { |
| 115 | + if (s_KeyValueLine.Match(lineBuffer)) { |
| 116 | + s_KeyValueLine.GetSubString(1, key, sizeof(key)); |
| 117 | + s_KeyValueLine.GetSubString(2, value, sizeof(value)); |
| 118 | + |
| 119 | + KeyValues_AddString(mapKeyValues, key, value); |
| 120 | + |
| 121 | + // change section name to hammerid for quick lookup (`m_iHammerID` dataprop) |
| 122 | + if (StrEqual(key, "hammerid")) { |
| 123 | + mapKeyValues.SetSectionName(value); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + i += n; |
| 129 | + } |
| 130 | + mapKeyValues.GoBack(); |
| 131 | + mapKeyValues.SetNum("num_entities", nKeys); |
| 132 | + |
| 133 | + return mapKeyValues; |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Adds a new key/value pair to the KeyValues structure in a way that allows for duplicate key |
| 138 | + * names. |
| 139 | + */ |
| 140 | +static void KeyValues_AddString(KeyValues kv, const char[] key, const char[] value) { |
| 141 | + static int s_nTempKey; |
| 142 | + |
| 143 | + char tempKey[64]; |
| 144 | + Format(tempKey, sizeof(tempKey), "__levelkv_temp_buffer_%d", s_nTempKey++); |
| 145 | + |
| 146 | + kv.SetString(tempKey, value); |
| 147 | + |
| 148 | + kv.JumpToKey(tempKey); |
| 149 | + kv.GotoFirstSubKey(false); |
| 150 | + kv.SetSectionName(key); |
| 151 | + kv.GotoNextKey(); |
| 152 | + |
| 153 | + kv.GoBack(); |
| 154 | +} |
0 commit comments