Skip to content
This repository was archived by the owner on Sep 24, 2023. It is now read-only.

Commit 17df8b8

Browse files
committed
Add LevelEntityList
1 parent cb6d8a3 commit 17df8b8

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

scripting/include/level_keyvalues.inc

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@ methodmap LevelEntityKeyValues < StringMultiMap {
4545
}
4646
}
4747

48+
/**
49+
* ArrayList-like accessors to a global, static collection of LevelEntityKeyValues handles.
50+
*/
51+
methodmap LevelEntityList {
52+
/**
53+
* Returns a LevelEntityKeyValues handle from the list by index. The handle must be freed.
54+
*/
55+
public static native LevelEntityKeyValues Get(int index);
56+
57+
/**
58+
* Removes a LevelEntityKeyValues handle from the list by index.
59+
*/
60+
public static native void Erase(int index);
61+
62+
/**
63+
* Adds a new entity entry. All values should be strings.
64+
* The key / values are internally copied to a new handle, so any changes to the specified
65+
* LevelEntityKeyValues handle will not be propagated to the internal copy.
66+
*/
67+
public static native void Push(LevelEntityKeyValues entity);
68+
69+
/**
70+
* Returns the number of LevelEntityKeyValues handles in the list.
71+
*/
72+
public static native int Length();
73+
}
74+
4875
/**
4976
* Called when a block of entity keys is finished parsing.
5077
* Changes to the StringMultiMap will be reflected in the level.
@@ -55,14 +82,12 @@ forward Action LevelEntity_OnEntityKeysParsed(LevelEntityKeyValues entity);
5582

5683
/**
5784
* Called when the entity string in `OnLevelInit` has been fully parsed out.
58-
* Plugins may call LevelEntity_InsertEntityKeys here to insert new entities into the entity
59-
* string.
85+
* Any modifications to the LevelEntityList or its member LevelEntityKeyValues will be written
86+
* out to the entity string.
6087
*/
6188
forward void LevelEntity_OnAllEntitiesParsed();
6289

63-
/**
64-
* Adds a new entity entry. All values should be strings.
65-
*/
90+
#pragma deprecated Use LevelEntityList.Push instead
6691
native void LevelEntity_InsertEntityKeys(LevelEntityKeyValues entity);
6792

6893
/**

scripting/level_keyvalues.sp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#pragma newdecls required
1919

20-
#define PLUGIN_VERSION "0.2.1"
20+
#define PLUGIN_VERSION "0.3.0"
2121
public Plugin myinfo = {
2222
name = "Level KeyValues",
2323
author = "nosoop",
@@ -27,6 +27,7 @@ public Plugin myinfo = {
2727
}
2828

2929
ArrayList g_MapEntities;
30+
bool g_bMutableList;
3031

3132
Handle g_OnEntityKeysParsed, g_OnAllEntitiesParsed;
3233

@@ -54,6 +55,11 @@ public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max)
5455
CreateNative("LevelEntityKeyValuesIterator.GetVector", Native_MapIterGetVector);
5556
CreateNative("LevelEntityKeyValuesIterator.SetVector", Native_MapIterSetVector);
5657

58+
CreateNative("LevelEntityList.Get", Native_LevelListGet);
59+
CreateNative("LevelEntityList.Push", Native_InsertEntity);
60+
CreateNative("LevelEntityList.Erase", Native_LevelListErase);
61+
CreateNative("LevelEntityList.Length", Native_LevelListGetLength);
62+
5763
return APLRes_Success;
5864
}
5965

@@ -75,8 +81,10 @@ public Action OnLevelInit(const char[] mapName, char mapEntities[2097152]) {
7581

7682
g_MapEntities = ParseEntityList(mapEntities);
7783

84+
g_bMutableList = true;
7885
Call_StartForward(g_OnAllEntitiesParsed);
7986
Call_Finish();
87+
g_bMutableList = false;
8088

8189
mapEntities = "";
8290
WriteEntityList(g_MapEntities, mapEntities, sizeof(mapEntities));
@@ -100,12 +108,45 @@ public int Native_GetKeysByHammerID(Handle plugin, int argc) {
100108
return 0;
101109
}
102110

111+
public int Native_LevelListGet(Handle plugin, int argc) {
112+
return view_as<int>(CloneHandle(g_MapEntities.Get(GetNativeCell(1))));
113+
}
114+
115+
public int Native_LevelListErase(Handle plugin, int argc) {
116+
if (!g_bMutableList) {
117+
ThrowNativeError(1, "Can't remove entities from list during non-mutable state.");
118+
}
119+
g_MapEntities.Erase(GetNativeCell(1));
120+
return 0;
121+
}
122+
103123
public int Native_InsertEntity(Handle plugin, int argc) {
124+
if (!g_bMutableList) {
125+
ThrowNativeError(1, "Can't push new entity into list during non-mutable state.");
126+
}
104127
StringMultiMap entity = GetNativeCell(1);
105-
g_MapEntities.Push(CloneHandle(entity));
128+
g_MapEntities.Push(CloneStringMultiMap(entity));
106129
return;
107130
}
108131

132+
public int Native_LevelListGetLength(Handle plugin, int argc) {
133+
return g_MapEntities.Length;
134+
}
135+
136+
StringMultiMap CloneStringMultiMap(StringMultiMap source) {
137+
char key[256], value[256];
138+
StringMultiMapIterator iter = source.GetIterator();
139+
140+
StringMultiMap output = new StringMultiMap();
141+
while (iter.Next()) {
142+
iter.GetKey(key, sizeof(key));
143+
if (iter.GetString(value, sizeof(value))) {
144+
output.AddString(key, value);
145+
}
146+
}
147+
return output;
148+
}
149+
109150
/**
110151
* Parses the level entity string into an ArrayList of StringMultiMap handles.
111152
*/

0 commit comments

Comments
 (0)