-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathData.h
196 lines (165 loc) · 6.5 KB
/
Data.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <boost/container/flat_map.hpp>
//#include <boost/algorithm/string/replace.hpp>
#include "common/ICriticalSection.h"
#include "skse64/PluginAPI.h"
#include "skse64/GameTypes.h"
#include "skse64/GameData.h"
#include "skse64/PapyrusVM.h"
#include "skse64/PapyrusArgs.h"
#include "Forms.h"
inline SInt32 cast(SInt32 &v) { return v; }
inline float cast(float &v) { return v; }
inline std::string cast(BSFixedString &v) { return v.data; }
inline BSFixedString cast(std::string &v) { return BSFixedString(v.c_str()); }
inline UInt32 cast(TESForm* v) { return v == NULL ? 0 : v->formID; }
inline TESForm* cast(UInt32 &v) { return v == 0 ? NULL : LookupFormByID(v); }
namespace Data
{
void InitLists();
template <typename S> inline void EncodeValue(S &v) {}
inline void EncodeValue(std::string &v) {
if (v.empty()) v += (char)0x1B;
else std::replace(
v.begin(),
v.end(),
(char)' ',
(char)0x7
);
}
//inline void EncodeValue(std::string &v) { if (v.empty()) v = (char)0x1B; else boost::replace_all(v, ' ', (char)0x7); }
template <typename S> inline void DecodeValue(S &v) {}
inline void DecodeValue(UInt32 &v){ v = Forms::ResolveFormID(v); }
inline void DecodeValue(std::string &v) {
if (v.size() == 1 && v[0] == (char)0x1B) v.clear();
else std::replace(
v.begin(),
v.end(),
(char)0x7,
(char)' '
);
}
//inline void DecodeValue(std::string &v) { if (v.size() == 1 && v[0] == (char)0x1B) v.clear(); else boost::replace_all(v, (char)0x7, ' '); }
/*
* Decalare classes methods
*/
template <typename T, typename S>
class Values {
public:
ICriticalSection s_dataLock;
typedef boost::container::flat_map<std::string, S> Obj;
typedef boost::container::flat_map<UInt64, Obj> Map;
Map Data;
void SetValue(UInt64 obj, std::string key, T value);
T GetValue(UInt64 obj, std::string key, T value);
T AdjustValue(UInt64 obj, std::string key, T value);
T PluckValue(UInt64 obj, std::string key, T value);
bool UnsetValue(UInt64 obj, std::string key);
bool HasValue(UInt64 obj, std::string key);
int ClearPrefixKey(std::string prefix);
int CountPrefixKey(std::string prefix);
int ClearPrefixKey(UInt64 obj, std::string prefix);
int CountPrefixKey(UInt64 obj, std::string prefix);
// Serialization
void Revert();
void LoadStream(std::stringstream &ss);
void SaveStream(std::stringstream &ss);
// Debug
inline int GetObjCount() { return Data.size(); }
inline int GetKeyCount(UInt64 obj) { return Data.find(obj) != Data.end() ? Data[obj].size() : 0; }
TESForm* GetNthObj(UInt32 i);
const std::string GetNthKey(UInt64 obj, UInt32 i);
VMResultArray<TESForm*> GetAllObj();
VMResultArray<BSFixedString> GetAllObjKeys(UInt64 obj);
void RemoveForm(UInt64 &obj);
int Cleanup();
};
template <typename T, typename S>
class Lists {
public:
ICriticalSection s_dataLock;
typedef std::vector<S> List;
typedef boost::container::flat_map<std::string, List> Obj;
typedef boost::container::flat_map<UInt64, Obj> Map;
Map Data;
int ListAdd(UInt64 obj, std::string key, T value, bool allowDuplicate);
T ListGet(UInt64 obj, std::string key, UInt32 index);
T ListSet(UInt64 obj, std::string key, UInt32 index, T value);
T ListAdjust(UInt64 obj, std::string key, UInt32 index, T value);
T ListPluck(UInt64 obj, std::string key, UInt32 index, T value);
T ListShift(UInt64 obj, std::string key);
T ListPop(UInt64 obj, std::string key);
int ListRemove(UInt64 obj, std::string key, T value, bool allInstances);
bool ListInsertAt(UInt64 obj, std::string key, UInt32 index, T value);
bool ListRemoveAt(UInt64 obj, std::string key, UInt32 index);
int ListClear(UInt64 obj, std::string key);
int ListCount(UInt64 obj, std::string key);
int ListCountValue(UInt64 obj, std::string key, T value, bool exclude);
int ListFind(UInt64 obj, std::string key, T value);
bool ListHas(UInt64 obj, std::string key, T value);
void ListSort(UInt64 obj, std::string key);
void ListSlice(UInt64 obj, std::string key, VMArray<T> Output, UInt32 startIndex);
int ListResize(UInt64 obj, std::string key, UInt32 length, T filler);
bool ListCopy(UInt64 obj, std::string key, VMArray<T> Input);
VMResultArray<T> ToArray(UInt64 obj, std::string key);
VMResultArray<T> FilterByTypes(UInt64 obj, std::string key, VMArray<UInt32> types, bool matching);
int ClearPrefixKey(std::string prefix);
int CountPrefixKey(std::string prefix);
int ClearPrefixKey(UInt64 obj, std::string prefix);
int CountPrefixKey(UInt64 obj, std::string prefix);
T ListRandom(UInt64 obj, std::string key);
VMResultArray<T> ListRandomArray(UInt64 obj, std::string key, UInt32 count, bool duplicates);
List* GetVector(UInt64 &obj, std::string &key){
Map::iterator itr = Data.find(obj);
if (itr == Data.end())
return NULL;
Obj::iterator itr2 = itr->second.find(key);
if (itr2 == itr->second.end())
return NULL;
return &itr2->second;
}
// Serialization
void Revert();
void LoadStream(std::stringstream &ss);
void SaveStream(std::stringstream &ss);
// Debug
inline int GetObjCount() { return Data.size(); }
inline int GetKeyCount(UInt64 obj) { return Data.find(obj) != Data.end() ? Data[obj].size() : 0; }
TESForm* GetNthObj(UInt32 i);
const std::string GetNthKey(UInt64 obj, UInt32 i);
VMResultArray<TESForm*> GetAllObj();
VMResultArray<BSFixedString> GetAllObjKeys(UInt64 obj);
void RemoveForm(UInt64 &obj);
int Cleanup();
};
/*
* Data storage holders
*/
// TODO: Change storage method
typedef Values<TESForm*, std::pair<UInt32, std::string>> formv;
typedef Lists<TESForm*, std::pair<UInt32, std::string>> forml;
typedef Values<SInt32, SInt32> intv;
typedef Values<float, float> flov;
typedef Values<BSFixedString, std::string> strv;
typedef Values<TESForm*, UInt32> forv;
template <typename T, typename S> Values<T, S>* GetValues();
template <> intv* GetValues<SInt32, SInt32>();
template <> flov* GetValues<float, float>();
template <> strv* GetValues<BSFixedString, std::string>();
template <> forv* GetValues<TESForm*, UInt32>();
typedef Lists<SInt32, SInt32> intl;
typedef Lists<float, float> flol;
typedef Lists<BSFixedString, std::string> strl;
typedef Lists<TESForm*, UInt32> forl;
template <typename T, typename S> Lists<T, S>* GetLists();
template <> intl* GetLists<SInt32, SInt32>();
template <> flol* GetLists<float, float>();
template <> strl* GetLists<BSFixedString, std::string>();
template <> forl* GetLists<TESForm*, UInt32>();
//forl* GetPackages();
//aniv* GetAnimations();
}