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

Commit b6ed033

Browse files
committed
Add implicit string conversion natives
LevelEntityKeyValues' values must all be strings, so some wrappers are provided to perform the conversion.
1 parent bf73fa2 commit b6ed033

3 files changed

Lines changed: 211 additions & 5 deletions

File tree

scripting/include/level_keyvalues.inc

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,50 @@
55

66
#include <more_adt>
77

8+
static stock KeyValues KeyValueBuffer() {
9+
static KeyValues s_KVBuffer;
10+
if (!s_KVBuffer) {
11+
s_KVBuffer = new KeyValues("");
12+
}
13+
return s_KVBuffer;
14+
}
15+
816
/**
9-
* Wrapper for StringMultiMap that uses an intermediary parsing step to ensure all values are
10-
* strings.
17+
* Wrapper for StringMultiMap that performs implicit string conversion.
18+
*/
19+
methodmap LevelEntityKeyValuesIterator < StringMultiMapIterator {
20+
public native int GetNum(int defaultValue = 0);
21+
public native void SetNum(int value);
22+
23+
public native float GetFloat(float flDefaultValue = 0.0);
24+
public native void SetFloat(float flValue);
25+
26+
public native void GetVector(float vec[3], const float defvalue[3])
27+
public native void SetVector(const float vec[3])
28+
}
29+
30+
/**
31+
* Wrapper for StringMultiMap that performs implicit string conversion.
1132
*/
1233
methodmap LevelEntityKeyValues < StringMultiMap {
1334
public LevelEntityKeyValues() {
1435
return view_as<LevelEntityKeyValues>(new StringMultiMap());
1536
}
1637

17-
// TODO implementation -- for now make sure that you only use the string-related
18-
// getters / setters in StringMultiMap.
38+
public native int GetNum(const char[] key, int defaultValue = 0);
39+
public native void AddNum(const char[] key, int value);
40+
41+
public native float GetFloat(const char[] key, float flDefaultValue = 0);
42+
public native void AddFloat(const char[] key, float value);
43+
44+
public native void GetVector(const char[] key, float vec[3],
45+
const float defaultValue[3] = { 0.0, ... });
46+
public native void AddVector(const char[] key, const float vec[3]);
47+
48+
public LevelEntityKeyValuesIterator GetKeyIterator(const char[] key) {
49+
return view_as<LevelEntityKeyValuesIterator>(
50+
view_as<StringMultiMap>(this).GetKeyIterator(key));
51+
}
1952
}
2053

2154
/**

scripting/level_keyvalues.sp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
#include <more_adt>
1515

16+
#include "level_keyvalues/map_string_natives.sp"
17+
1618
#pragma newdecls required
1719

18-
#define PLUGIN_VERSION "0.1.2"
20+
#define PLUGIN_VERSION "0.2.0"
1921
public Plugin myinfo = {
2022
name = "Level KeyValues",
2123
author = "nosoop",
@@ -34,6 +36,24 @@ public APLRes AskPluginLoad2(Handle self, bool late, char[] error, int err_max)
3436
CreateNative("LevelEntity_GetKeysByHammerID", Native_GetKeysByHammerID);
3537
CreateNative("LevelEntity_InsertEntityKeys", Native_InsertEntity);
3638

39+
CreateNative("LevelEntityKeyValues.GetNum", Native_MapGetNum);
40+
CreateNative("LevelEntityKeyValues.AddNum", Native_MapAddNum);
41+
42+
CreateNative("LevelEntityKeyValues.GetFloat", Native_MapGetFloat);
43+
CreateNative("LevelEntityKeyValues.AddFloat", Native_MapAddFloat);
44+
45+
CreateNative("LevelEntityKeyValues.GetVector", Native_MapGetVector);
46+
CreateNative("LevelEntityKeyValues.AddVector", Native_MapAddVector);
47+
48+
CreateNative("LevelEntityKeyValuesIterator.GetNum", Native_MapIterGetNum);
49+
CreateNative("LevelEntityKeyValuesIterator.SetNum", Native_MapIterSetNum);
50+
51+
CreateNative("LevelEntityKeyValuesIterator.GetFloat", Native_MapIterGetFloat);
52+
CreateNative("LevelEntityKeyValuesIterator.SetFloat", Native_MapIterSetFloat);
53+
54+
CreateNative("LevelEntityKeyValuesIterator.GetVector", Native_MapIterGetVector);
55+
CreateNative("LevelEntityKeyValuesIterator.SetVector", Native_MapIterSetVector);
56+
3757
return APLRes_Success;
3858
}
3959

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/**
2+
* Wrapper natives that stringify / unstringify parsed entity string values.
3+
*/
4+
5+
static stock KeyValues KeyValueBuffer() {
6+
static KeyValues s_KVBuffer;
7+
if (!s_KVBuffer) {
8+
s_KVBuffer = new KeyValues("");
9+
}
10+
return s_KVBuffer;
11+
}
12+
13+
static stock void VectorToString(const float vec[3], char[] buffer, int maxlen) {
14+
KeyValueBuffer().SetVector("key", vec);
15+
KeyValueBuffer().GetString("key", buffer, maxlen);
16+
}
17+
18+
static stock void StringToVector(const char[] buffer, float vec[3]) {
19+
KeyValueBuffer().SetString("key", buffer);
20+
KeyValueBuffer().GetVector("key", vec);
21+
}
22+
23+
24+
public int Native_MapGetNum(Handle plugin, int argc) {
25+
StringMultiMap map = GetNativeCell(1);
26+
27+
char key[128], value[128];
28+
GetNativeString(2, key, sizeof(key));
29+
30+
return map.GetString(key, value, sizeof(value))? StringToInt(value) : GetNativeCell(3);
31+
}
32+
33+
public int Native_MapAddNum(Handle plugin, int argc) {
34+
StringMultiMap map = GetNativeCell(1);
35+
36+
char key[128], value[128];
37+
GetNativeString(2, key, sizeof(key));
38+
IntToString(GetNativeCell(3), value, sizeof(value));
39+
40+
map.AddString(key, value);
41+
return 0;
42+
}
43+
44+
public int Native_MapGetFloat(Handle plugin, int argc) {
45+
StringMultiMap map = GetNativeCell(1);
46+
47+
char key[128], value[128];
48+
GetNativeString(2, key, sizeof(key));
49+
50+
return view_as<int>(map.GetString(key, value, sizeof(value))? StringToFloat(value) : GetNativeCell(3));
51+
}
52+
53+
public int Native_MapAddFloat(Handle plugin, int argc) {
54+
StringMultiMap map = GetNativeCell(1);
55+
56+
char key[128], value[128];
57+
GetNativeString(2, key, sizeof(key));
58+
FloatToString(GetNativeCell(3), value, sizeof(value));
59+
60+
map.AddString(key, value);
61+
return 0;
62+
}
63+
64+
public int Native_MapGetVector(Handle plugin, int argc) {
65+
StringMultiMap map = GetNativeCell(1);
66+
67+
char key[128], value[128];
68+
GetNativeString(2, key, sizeof(key));
69+
70+
float outputVec[3];
71+
if (map.GetString(key, value, sizeof(value))) {
72+
StringToVector(value, outputVec);
73+
} else {
74+
GetNativeArray(4, outputVec, 3);
75+
}
76+
SetNativeArray(3, outputVec, 3);
77+
}
78+
79+
public int Native_MapAddVector(Handle plugin, int argc) {
80+
StringMultiMap map = GetNativeCell(1);
81+
82+
char key[128], value[128];
83+
GetNativeString(2, key, sizeof(key));
84+
85+
float vec[3];
86+
GetNativeArray(3, vec, 3);
87+
88+
VectorToString(vec, value, sizeof(value));
89+
map.AddString(key, value);
90+
return 0;
91+
}
92+
93+
public int Native_MapIterGetNum(Handle plugin, int argc) {
94+
StringMultiMapIterator mapIter = GetNativeCell(1);
95+
96+
char value[128];
97+
return mapIter.GetString(value, sizeof(value))? StringToInt(value) : GetNativeCell(2);
98+
}
99+
100+
public int Native_MapIterSetNum(Handle plugin, int argc) {
101+
StringMultiMapIterator mapIter = GetNativeCell(1);
102+
103+
char value[128];
104+
IntToString(GetNativeCell(2), value, sizeof(value));
105+
106+
mapIter.SetString(value);
107+
return;
108+
}
109+
110+
public int Native_MapIterGetFloat(Handle plugin, int argc) {
111+
StringMultiMapIterator mapIter = GetNativeCell(1);
112+
113+
char value[128];
114+
return view_as<int>(mapIter.GetString(value, sizeof(value))?
115+
StringToFloat(value) : GetNativeCell(2));
116+
}
117+
118+
public int Native_MapIterSetFloat(Handle plugin, int argc) {
119+
StringMultiMapIterator mapIter = GetNativeCell(1);
120+
121+
char value[128];
122+
FloatToString(GetNativeCell(2), value, sizeof(value));
123+
124+
mapIter.SetString(value);
125+
return 0;
126+
}
127+
128+
public int Native_MapIterGetVector(Handle plugin, int argc) {
129+
StringMultiMapIterator mapIter = GetNativeCell(1);
130+
131+
char value[128];
132+
133+
float outputVec[3];
134+
if (mapIter.GetString(value, sizeof(value))) {
135+
StringToVector(value, outputVec);
136+
} else {
137+
GetNativeArray(3, outputVec, 3);
138+
}
139+
SetNativeArray(2, outputVec, 3);
140+
}
141+
142+
public int Native_MapIterSetVector(Handle plugin, int argc) {
143+
StringMultiMapIterator mapIter = GetNativeCell(1);
144+
145+
char value[128];
146+
147+
float vec[3];
148+
GetNativeArray(2, vec, 3);
149+
150+
VectorToString(vec, value, sizeof(value));
151+
mapIter.SetString(value);
152+
return 0;
153+
}

0 commit comments

Comments
 (0)