forked from eeveelo/PapyrusUtil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
External.cpp
1354 lines (1139 loc) · 38.9 KB
/
External.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "External.h"
#include <vector>
#include <sstream>
#include <iostream>
#include <fstream>
#include <streambuf>
//#include <direct.h>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
#include <boost/algorithm/string.hpp>
#include "skse64/PapyrusVM.h"
#include "skse64/PapyrusNativeFunctions.h"
namespace External {
namespace fs = boost::filesystem;
static ICriticalSection* s_loadLock = NULL;
static FileVector* s_Files = NULL;
// Make value for storage
inline Value ExternalFile::make(SInt32 v) const {
return Value::Int(v);
}
inline Value ExternalFile::make(float v) const {
return Value(v);
}
inline Value ExternalFile::make(BSFixedString v) const {
return Value(v.data);
}
inline Value ExternalFile::make(TESForm* v) const {
return Value(Forms::GetFormString(v));
}
// Parse json value for output
inline SInt32 ExternalFile::parse(Value value, SInt32 missing) const {
return (value.isInt() || (!value.isNull() && value.isConvertibleTo(Json::intValue))) ? value.asInt() : missing;
}
inline float ExternalFile::parse(Value value, float missing) const {
return (value.isDouble() || (!value.isNull() && value.isConvertibleTo(Json::realValue))) ? value.asFloat() : missing;
}
inline TESForm* ExternalFile::parse(Value value, TESForm* missing) const {
return value.isString() ? Forms::ParseFormString(value.asString()) : missing;
}
inline BSFixedString ExternalFile::parse(Value value, BSFixedString missing) const {
return value.isString() ? BSFixedString(value.asCString()) : (!value.isNull() && value.isConvertibleTo(Json::stringValue)) ? BSFixedString(value.asString().c_str()) : missing;
}
template<typename T> inline bool ExternalFile::HasKey(const std::string &key) { return root.isMember(Type<T>()) && root[Type<T>()].isMember(key); }
inline bool ExternalFile::HasKey(std::string &type, const std::string &key) { return root.isMember(type) && root[type].isMember(key); }
/*inline void ExternalFile::getvar(const char* type, const std::string &key, Value &var) {
if (root.isMember(type) && root.isMember(key))
var = root[type][key];
}
template <> inline void ExternalFile::getvar<SInt32>(const std::string &key, Value &var) { return getvar("int", key, var); }
template <> inline void ExternalFile::getvar<float>(const std::string &key, Value &var) { return getvar("float", key, var); }
template <> inline void ExternalFile::getvar<BSFixedString>(const std::string &key, Value &var) { return getvar("string", key, var); }
template <> inline void ExternalFile::getvar<TESForm*>(const std::string &key, Value &var) { return getvar("form", key, var); }
inline void ExternalFile::setvar(const char* type, const std::string &key, Value &var) {
root[type][key] = var;
}
template <> inline void ExternalFile::setvar<SInt32>(const std::string &key, Value &var) { return setvar("int", key, var); }
template <> inline void ExternalFile::setvar<float>(const std::string &key, Value &var) { return setvar("float", key, var); }
template <> inline void ExternalFile::setvar<BSFixedString>(const std::string &key, Value &var) { return setvar("string", key, var); }
template <> inline void ExternalFile::setvar<TESForm*>(const std::string &key, Value &var) { return setvar("form", key, var); }
*/
/*
//
// GLOBAL KEY => VALUE STORAGE
//
*/
#ifdef NEW_STYLE_JSON
template <typename T>
void ExternalFile::SetValue(std::string key, T value) {
s_dataLock.Enter();
boost::to_lower(key);
root[Type<T>()][key] = make(value);
isModified = true;
s_dataLock.Leave();
}
template void ExternalFile::SetValue<SInt32>(std::string key, SInt32 value);
template void ExternalFile::SetValue<float>(std::string key, float value);
template void ExternalFile::SetValue<BSFixedString>(std::string key, BSFixedString value);
template void ExternalFile::SetValue<TESForm*>(std::string key, TESForm* value);
template <typename T>
T ExternalFile::GetValue(std::string key, T value) {
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey<T>(key))
value = parse(root[Type<T>()][key], value);
s_dataLock.Leave();
return value;
}
template SInt32 ExternalFile::GetValue<SInt32>(std::string key, SInt32 value);
template float ExternalFile::GetValue<float>(std::string key, float value);
template BSFixedString ExternalFile::GetValue<BSFixedString>(std::string key, BSFixedString value);
template TESForm* ExternalFile::GetValue<TESForm*>(std::string key, TESForm* value);
template <typename T>
bool ExternalFile::UnsetValue(std::string key) {
bool removed = false;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey<T>(key)) {
isModified = true;
removed = true;
root[Type<T>()].removeMember(key);
//if (root[type].empty())
// root.removeMember(type);
}
s_dataLock.Leave();
return removed;
}
template bool ExternalFile::UnsetValue<SInt32>(std::string key);
template bool ExternalFile::UnsetValue<float>(std::string key);
template bool ExternalFile::UnsetValue<BSFixedString>(std::string key);
template bool ExternalFile::UnsetValue<TESForm*>(std::string key);
template <typename T>
bool ExternalFile::HasValue(std::string key) {
bool has = false;
s_dataLock.Enter();
boost::to_lower(key);
has = root.isMember(Type<T>()) && root[Type<T>()].isMember(key);
s_dataLock.Leave();
return has;
}
template bool ExternalFile::HasValue<SInt32>(std::string key);
template bool ExternalFile::HasValue<float>(std::string key);
template bool ExternalFile::HasValue<BSFixedString>(std::string key);
template bool ExternalFile::HasValue<TESForm*>(std::string key);
SInt32 ExternalFile::AdjustValue(std::string key, SInt32 value) {
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey<SInt32>(key)) root[Type<SInt32>()][key] = Value::Int(value + root[Type<SInt32>()][key].asInt());
else root[Type<SInt32>()][key] = Value::Int(value);
isModified = true;
s_dataLock.Leave();
return value;
}
float ExternalFile::AdjustValue(std::string key, float value) {
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey<float>(key)) root[Type<float>()][key] = Value(value + root[Type<float>()][key].asFloat());
else root[Type<float>()][key] = Value(value);
isModified = true;
s_dataLock.Leave();
return value;
}
/*
//
// GLOBAL KEY => VALUE => LIST STORAGE
//
*/
template <typename T> int ListAdd(std::string key, T value, bool allowDuplicate) {
int size = -1;
s_dataLock.Enter();
boost::to_lower(key);
if (!allowDuplicate) {
}
s_dataLock.Leave();
return size;
}
template <typename T> T ListGet(std::string key, int index);
template <typename T> T ListSet(std::string key, int index, T value);
template <typename T> int ListRemove(std::string key, T removing, bool allInstances);
template <typename T> bool ListRemoveAt(std::string key, int index);
template <typename T> bool ListInsertAt(std::string key, int index, T value);
template <typename T> int ListClear(std::string key);
template <typename T> int ListCount(std::string key);
template <typename T> int ListCountValue(std::string key, T value, bool exclude);
template <typename T> int ListFind(std::string key, T value) {
int index = -1;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey<T>(key)) {
Value var = make(value);
Value &list = root[Type<T>()][key];
for (Value::iterator itr = list.begin(); itr != list.end(); ++itr) {
if (var == (*itr) || (var.isString() && boost::iequals((*itr).asString(), var.asString()))) {
index = itr.key().asInt();
break;
}
}
}
s_dataLock.Leave();
return index;
}
template <typename T> bool ListHas(std::string key, T value);
template <typename T> int ListResize(std::string key, int length, T filler);
#endif
#ifdef OLD_STYLE_JSON
void ExternalFile::SetValue(std::string type, std::string key, Value value) {
s_dataLock.Enter();
boost::to_lower(key);
root[type][key] = value;
isModified = true;
s_dataLock.Leave();
}
Value ExternalFile::GetValue(std::string type, std::string key, Value value) {
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key))
value = root[type][key];
s_dataLock.Leave();
return value;
}
Value ExternalFile::AdjustValue(std::string type, std::string key, Value value) {
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)){
if (value.isInt()) value = Value::Int(value.asInt() + root[type][key].asInt());
else value = Value::Int(value.asFloat() + root[type][key].asFloat());
}
root[type][key] = value;
isModified = true;
s_dataLock.Leave();
return value;
}
bool ExternalFile::UnsetValue(std::string type, std::string key){
bool removed = false;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)){
isModified = true;
removed = true;
root[type].removeMember(key);
//if (root[type].empty())
// root.removeMember(type);
}
s_dataLock.Leave();
return removed;
}
bool ExternalFile::HasValue(std::string type, std::string key){
bool has = false;
s_dataLock.Enter();
boost::to_lower(key);
has = root.isMember(type) && root[type].isMember(key);
s_dataLock.Leave();
return has;
}
// Global key=>value=>list
int ExternalFile::ListAdd(std::string type, std::string key, Value value, bool allowDuplicate) {
if (!allowDuplicate && ListFind(type, key, value) != -1) return -1;
s_dataLock.Enter();
boost::to_lower(key);
value = root[type][key].append(value);
isModified = true;
s_dataLock.Leave();
return (root[type][key].size() - 1);
}
Value ExternalFile::ListGet(std::string type, std::string key, int index) {
s_dataLock.Enter();
boost::to_lower(key);
Value value;
if (HasKey(type, key) && root[type][key].isValidIndex(index))
value = root[type][key].get(index, Value::null);
s_dataLock.Leave();
return value;
}
Value ExternalFile::ListSet(std::string type, std::string key, int index, Value value) {
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key) && root[type][key].isValidIndex(index)) {
root[type][key][index] = value;
isModified = true;
}
s_dataLock.Leave();
return value;
}
int ExternalFile::ListRemove(std::string type, std::string key, Value removing, bool allInstances) {
int removed = 0;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)) {
Value list = Value(Json::arrayValue);
for (Value::iterator itr = root[type][key].begin(); itr != root[type][key].end(); ++itr) {
if (!allInstances && removed > 0) list.append((*itr));
else if (removing != (*itr)) list.append((*itr));
else removed += 1;
}
root[type][key].swap(list);
/*if (root[type][key].size() == 0){
root[type].removeMember(key);
if (root[type].size() == 0)
root.removeMember(type);
}*/
isModified = true;
}
s_dataLock.Leave();
return removed;
}
bool ExternalFile::ListRemoveAt(std::string type, std::string key, int index) {
bool removed = false;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key) && root[type][key].isValidIndex(index)) {
Value list = Value(Json::arrayValue);
Value::iterator itr = root[type][key].begin();
for (itr = root[type][key].begin(); itr != root[type][key].end(); ++itr) {
if (itr.key().asInt() == index) removed = true;
else list.append((*itr));
}
root[type][key] = list;
/*if (root[type][key].size() == 0){
root[type].removeMember(key);
if (root[type].size() == 0)
root.removeMember(type);
}*/
isModified = true;
}
s_dataLock.Leave();
return removed;
}
bool ExternalFile::ListInsertAt(std::string type, std::string key, int index, Value value) {
bool inserted = false;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key) && root[type][key].isValidIndex(index)) {
Value list = Value(Json::arrayValue);
Value::iterator itr = root[type][key].begin();
for (itr = root[type][key].begin(); itr != root[type][key].end(); ++itr) {
if (itr.key().asInt() == index) {
list.append(value);
inserted = true;
}
list.append((*itr));
}
root[type][key] = list;
isModified = true;
}
s_dataLock.Leave();
return inserted;
}
int ExternalFile::ListClear(std::string type, std::string key) {
int cleared = 0;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)) {
isModified = true;
cleared = root[type][key].size();
root[type].removeMember(key);
//if (root[type].size() == 0)
// root.removeMember(type);
}
s_dataLock.Leave();
return cleared;
}
/*void ExternalFile::ListSort(std::string type, std::string key) {
int cleared = 0;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)) {
isModified = true;
std::sort(root[type][key].begin(), root[type][key].end());
}
s_dataLock.Leave();
}*/
int ExternalFile::ListCount(std::string type, std::string key) {
int count = 0;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)) count = root[type][key].size();
s_dataLock.Leave();
return count;
}
int ExternalFile::ListCountValue(std::string type, std::string key, Value value, bool exclude) {
int count = 0;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)) {
for (Value::iterator itr = root[type][key].begin(); itr != root[type][key].end(); ++itr) {
if (value == (*itr) || (value.isString() && boost::iequals((*itr).asString(), value.asString())))
count += 1;
}
if (exclude) count = (root[type][key].size() - count);
}
s_dataLock.Leave();
return count;
}
int ExternalFile::ListFind(std::string type, std::string key, Value value) {
int index = -1;
s_dataLock.Enter();
boost::to_lower(key);
if (HasKey(type, key)) {
for (Value::iterator itr = root[type][key].begin(); itr != root[type][key].end(); ++itr) {
if (value == (*itr) || (value.isString() && boost::iequals((*itr).asString(), value.asString()))) {
index = itr.key().asInt();
//_MESSAGE("\t[%d] %s == %s", index, (*itr).asCString(), value.asCString());
break;
}
}
}
s_dataLock.Leave();
return index;
}
int ExternalFile::ListResize(std::string type, std::string key, int length, Value filler) {
if (length == 0) return ListClear(type, key) * -1;
s_dataLock.Enter();
int start = 0;
boost::to_lower(key);
if (HasKey(type, key)) {
start = root[type][key].size();
if (length < start)
root[type][key].resize(length);
else {
for (int i = 0; length > root[type][key].size() && i < 500; ++i)
root[type][key].append(filler);
}
}
else {
for (int i = 0; length > root[type][key].size() && i < 500; ++i)
root[type][key].append(filler);
}
isModified = true;
s_dataLock.Leave();
return root[type][key].size() - start;
}
template <typename T>
T ExternalFile::ListAdjust(std::string key, int index, T adjustBy) {
s_dataLock.Enter();
boost::to_lower(key);
std::string type = List<T>();
if (HasKey(type, key) && root[type][key].isValidIndex(index)) {
adjustBy = adjustBy + ParseValue<T>(root[type][key][index], T());
root[type][key][index] = MakeValue<T>(adjustBy);
isModified = true;
}
s_dataLock.Leave();
return adjustBy;
}
template SInt32 ExternalFile::ListAdjust<SInt32>(std::string key, int index, SInt32 adjustBy);
template float ExternalFile::ListAdjust<float>(std::string key, int index, float adjustBy);
template <typename T>
void ExternalFile::ListSlice(std::string key, VMArray<T> Output, int startIndex) {
if (Output.Length() > 0) {
UInt32 length(Output.Length());
s_dataLock.Enter();
boost::to_lower(key);
std::string type = List<T>();
if (HasKey(type, key)) {
Value::iterator itr = root[type][key].begin();
std::advance(itr, startIndex);
for (int index = 0; index < length && itr != root[type][key].end(); ++itr, ++index) {
T value = ParseValue<T>(root[type][key].get(itr.index(), Value::null));
Output.Set(&value, index);
}
}
s_dataLock.Leave();
}
}
template void ExternalFile::ListSlice<SInt32>(std::string key, VMArray<SInt32> Output, int startIndex);
template void ExternalFile::ListSlice<float>(std::string key, VMArray<float> Output, int startIndex);
template void ExternalFile::ListSlice<BSFixedString>(std::string key, VMArray<BSFixedString> Output, int startIndex);
template void ExternalFile::ListSlice<TESForm*>(std::string key, VMArray<TESForm*> Output, int startIndex);
template <typename T>
bool ExternalFile::ListCopy(std::string key, VMArray<T> Input) {
if (Input.Length() > 0) {
UInt32 length(Input.Length());
s_dataLock.Enter();
T var;
boost::to_lower(key);
std::string type = List<T>();
if (HasKey(type, key)) root[type][key].clear();
for (UInt32 i = 0; i < length; ++i) {
Input.Get(&var, i);
root[type][key].append(MakeValue<T>(var));
}
isModified = true;
s_dataLock.Leave();
return true;
}
return false;
}
template bool ExternalFile::ListCopy<SInt32>(std::string key, VMArray<SInt32> Input);
template bool ExternalFile::ListCopy<float>(std::string key, VMArray<float> Input);
template bool ExternalFile::ListCopy<BSFixedString>(std::string key, VMArray<BSFixedString> Input);
template bool ExternalFile::ListCopy<TESForm*>(std::string key, VMArray<TESForm*> Input);
template <typename T>
VMResultArray<T> ExternalFile::ToArray(std::string key) {
VMResultArray<T> arr;
s_dataLock.Enter();
boost::to_lower(key);
std::string type = List<T>();
if (HasKey(type, key)) {
arr.reserve(root[type][key].size());
for (Value::iterator itr = root[type][key].begin(); itr != root[type][key].end(); ++itr) {
T var = ParseValue<T>(*itr);
arr.push_back(var);
}
}
s_dataLock.Leave();
return arr;
}
template VMResultArray<SInt32> ExternalFile::ToArray<SInt32>(std::string key);
template VMResultArray<float> ExternalFile::ToArray<float>(std::string key);
template VMResultArray<BSFixedString> ExternalFile::ToArray<BSFixedString>(std::string key);
template VMResultArray<TESForm*> ExternalFile::ToArray<TESForm*>(std::string key);
int ExternalFile::CountPrefix(std::string type, std::string prefix) {
int count = 0;
if (root.isMember(type) && !root[type].empty()) {
s_dataLock.Enter();
boost::to_lower(prefix);
Value::Members members = root[type].getMemberNames();
for (Value::Members::iterator itr = members.begin(); itr != members.end(); ++itr) {
if (boost::starts_with((*itr), prefix)) count++;
}
s_dataLock.Leave();
}
return count;
}
#endif
/*int ExternalFile::ClearPrefix(std::string type, std::string prefix) {
int count = 0;
if (root.isMember(type) && !root[type].empty()) {
s_dataLock.Enter();
boost::to_lower(prefix);
Value list = Value(Json::objectValue);
for (Value::iterator itr = root[type].begin(); itr != root[type].end(); ++itr) {
if (boost::starts_with((*itr), prefix)) count++;
else list.append(*itr);
}
root[type] = list;
if (count > 0) isModified = true;
s_dataLock.Leave();
}
return count;
}*/
/*
//
// RESOLVE PATH HANDLING
//
*/
inline Value ExternalFile::Resolve(const std::string &pathto) { Path path(pathto.front() != '.' ? '.' + pathto : pathto); return path.resolve(root, Value(Json::nullValue)); }
inline Value ExternalFile::Resolve(const std::string &pathto, Value missing) { Path path(pathto.front() != '.' ? '.' + pathto : pathto); return path.resolve(root, missing); }
template <typename T> void ExternalFile::SetPathValue(const std::string &path, Value value) {
if (!path.empty()) {
s_dataLock.Enter();
Path pathto(path.front() != '.' ? '.' + path : path);
pathto.make(root).swap(value);
isModified = true;
s_dataLock.Leave();
}
}
template void ExternalFile::SetPathValue<SInt32>(const std::string &path, Value value);
template void ExternalFile::SetPathValue<float>(const std::string &path, Value value);
template void ExternalFile::SetPathValue<BSFixedString>(const std::string &path, Value value);
template void ExternalFile::SetPathValue<TESForm*>(const std::string &path, Value value);
void ExternalFile::ClearPath(const std::string &path) {
if (!path.empty()) {
// Full clear
if (path == "."){
ClearAll();
return;
}
s_dataLock.Enter();
std::string pathto = path.front() != '.' ? '.' + path : path;
Value value = Resolve(pathto, Value(Json::nullValue));
if (!value.isNull()){
std::vector<std::string> args;
boost::iter_split(args, pathto, boost::first_finder("."));
//_MESSAGE("path0: %s", args[0].c_str());
//if (args.size() > 1) _MESSAGE("path1: %s", args[1].c_str());
switch (args.size()){
case 2:
root.removeMember(args[1]);
break;
case 3:
root[args[1]].removeMember(args[2]);
break;
case 4:
root[args[1]][args[2]].removeMember(args[3]);
break;
case 5:
root[args[1]][args[2]][args[3]].removeMember(args[4]);
break;
case 6:
root[args[1]][args[2]][args[3]][args[4]].removeMember(args[5]);
break;
case 7:
root[args[1]][args[2]][args[3]][args[4]][args[5]].removeMember(args[6]);
break;
case 8:
root[args[1]][args[2]][args[3]][args[4]][args[5]][args[6]].removeMember(args[7]);
break;
case 9:
root[args[1]][args[2]][args[3]][args[4]][args[5]][args[6]][args[7]].removeMember(args[8]);
break;
}
isModified = true;
}
else {
_MESSAGE("ClearPath: Unable to resolve %s", path.c_str());
}
s_dataLock.Leave();
}
}
void ExternalFile::ClearPathIndex(const std::string &path, int idx) {
if (!path.empty()) {
s_dataLock.Enter();
std::string pathto = path.front() != '.' ? '.' + path : path;
Value pathArray = Resolve(pathto, Value(Json::nullValue));
if (!pathArray.isNull() && pathArray.isArray()) {
std::vector<std::string> args;
boost::iter_split(args, pathto, boost::first_finder("."));
Value value = Value(Json::nullValue);
switch (args.size()) {
case 2:
isModified = root[args[1]].removeIndex(idx, &value);
break;
case 3:
isModified = root[args[1]][args[2]].removeIndex(idx, &value);
break;
case 4:
isModified = root[args[1]][args[2]][args[3]].removeIndex(idx, &value);
break;
case 5:
isModified = root[args[1]][args[2]][args[3]][args[4]].removeIndex(idx, &value);
break;
case 6:
isModified = root[args[1]][args[2]][args[3]][args[4]][args[5]].removeIndex(idx, &value);
break;
case 7:
isModified = root[args[1]][args[2]][args[3]][args[4]][args[5]][args[6]].removeIndex(idx, &value);
break;
case 8:
isModified = root[args[1]][args[2]][args[3]][args[4]][args[5]][args[6]][args[7]].removeIndex(idx, &value);
break;
case 9:
isModified = root[args[1]][args[2]][args[3]][args[4]][args[5]][args[6]][args[7]][args[8]].removeIndex(idx, &value);
break;
}
if (isModified) _MESSAGE("ClearPathIndex: Removed Index %d from array %s", idx, path.c_str());
else _MESSAGE("ClearPathIndex: FAILED. Index was likely out of range.");
}
else {
_MESSAGE("ClearPathIndex: Unable to resolve an array at %s", path.c_str());
}
s_dataLock.Leave();
}
}
template <typename T> void ExternalFile::SetPathArray(const std::string &path, VMArray<T> Input, bool append) {
if (!path.empty()) {
s_dataLock.Enter();
Path pathto(path.front() != '.' ? '.' + path : path);
Value value = Resolve(path, Value(Json::arrayValue));
if (!value.isArray()) value = Value(Json::arrayValue);
else if (!append && value.size() > 0) value.clear();
UInt32 length(Input.Length());
T var;
for (UInt32 i = 0; i < length; ++i) {
Input.Get(&var, i);
value.append(MakeValue<T>(var));
}
pathto.make(root).swap(value);
isModified = true;
s_dataLock.Leave();
}
}
template void ExternalFile::SetPathArray<SInt32>(const std::string &path, VMArray<SInt32> arr, bool append);
template void ExternalFile::SetPathArray<float>(const std::string &path, VMArray<float> arr, bool append);
template void ExternalFile::SetPathArray<BSFixedString>(const std::string &path, VMArray<BSFixedString> arr, bool append);
template void ExternalFile::SetPathArray<TESForm*>(const std::string &path, VMArray<TESForm*> arr, bool append);
template <typename T> T ExternalFile::GetPathValue(const std::string &path, T defaultValue) {
if (!path.empty()) {
s_dataLock.Enter();
Value value = Resolve(path, Value(Json::nullValue));
if (!value.isNull()) defaultValue = parse(value, defaultValue);
s_dataLock.Leave();
}
return defaultValue;
}
template SInt32 ExternalFile::GetPathValue<SInt32>(const std::string &path, SInt32 defaultValue);
template float ExternalFile::GetPathValue<float>(const std::string &path, float defaultValue);
template BSFixedString ExternalFile::GetPathValue<BSFixedString>(const std::string &path, BSFixedString defaultValue);
template TESForm* ExternalFile::GetPathValue<TESForm*>(const std::string &path, TESForm* defaultValue);
/*
template <> SInt32 ExternalFile::GetPathValue(std::string path, SInt32 defaultValue) {
if (path.empty()) return defaultValue;
s_dataLock.Enter();
Value value = Resolve(path, Value::Int(defaultValue));
try
{
if (value.isInt() || value.isConvertibleTo(Json::intValue))
defaultValue = value.asInt();
}
catch (Json::LogicError &msg)
{
_MESSAGE("Json::LogicError: %s", msg.what());
}
s_dataLock.Leave();
return defaultValue;
}
template <> SInt32 ExternalFile::GetPathValue(std::string path, SInt32 defaultValue) {
if (path.empty()) return defaultValue;
s_dataLock.Enter();
Value value = Resolve(path, Value::Int(defaultValue));
try
{
if (value.isInt() || value.isConvertibleTo(Json::intValue))
defaultValue = value.asInt();
}
catch (Json::LogicError &msg)
{
_MESSAGE("Json::LogicError: %s", msg.what());
}
s_dataLock.Leave();
return defaultValue;
}
template <> float ExternalFile::GetPathValue(std::string path, float defaultValue) {
if (path.empty()) return defaultValue;
s_dataLock.Enter();
Value value = Resolve(path, Value(defaultValue));
try
{
if (value.isInt() || value.isConvertibleTo(Json::realValue))
defaultValue = value.asFloat();
}
catch (Json::LogicError &msg)
{
_MESSAGE("Json::LogicError: %s", msg.what());
}
s_dataLock.Leave();
return defaultValue;
}
template <> BSFixedString ExternalFile::GetPathValue(std::string path, BSFixedString defaultValue) {
if (path.empty()) return defaultValue;
s_dataLock.Enter();
Value value = Resolve(path, Value(Json::nullValue));
try
{
defaultValue = parse(value, defaultValue);
//else if (value.isArray())+
// defaultValue = "(array)";
//else if (value.isObject())
// defaultValue = "(object)";
}
catch (Json::LogicError &msg)
{
_MESSAGE("Json::LogicError: %s", msg.what());
}
s_dataLock.Leave();
return defaultValue;
}
template <> TESForm* ExternalFile::GetPathValue(std::string path, TESForm* defaultValue) {
if (path.empty()) return defaultValue;
s_dataLock.Enter();
Value value = Resolve(path, MakeValue(defaultValue));
try
{
if (value.isString()) defaultValue = Forms::ParseFormString(value.asString());
}
catch (Json::LogicError &msg)
{
_MESSAGE("Json::LogicError: %s", msg.what());
}
s_dataLock.Leave();
return defaultValue;
}
*/
template <typename T> VMResultArray<T> ExternalFile::PathElements(const std::string &path, T invalidType) {
VMResultArray<T> arr;
if (path.empty()) return arr;
s_dataLock.Enter();
Value value = Resolve(path);
if (!value.isNull() && value.isArray() && value.size() > 0) {
arr.reserve(value.size());
for (Value::iterator itr = value.begin(); itr != value.end(); ++itr) {
arr.push_back(ParseValue<T>(*itr, invalidType));
}
}
s_dataLock.Leave();
return arr;
}
template <typename T> int ExternalFile::FindPathElement(const std::string &path, Value toFind) {
int index = -1;
s_dataLock.Enter();
Value arr = Resolve(path);
if (arr.isArray() && !arr.empty()) {
for (Value::iterator itr = arr.begin(); itr != arr.end(); ++itr) {
if (toFind == (*itr) || (toFind.isString() && boost::iequals((*itr).asString(), toFind.asString()))) {
index = itr.key().asInt();
break;
}
}
}
s_dataLock.Leave();
return index;
}
template int ExternalFile::FindPathElement<SInt32>(const std::string &path, Value toFind);
template int ExternalFile::FindPathElement<float>(const std::string &path, Value toFind);
template int ExternalFile::FindPathElement<BSFixedString>(const std::string &path, Value toFind);
template int ExternalFile::FindPathElement<TESForm*>(const std::string &path, Value toFind);
/*template <> VMResultArray<BSFixedString> ExternalFile::PathElements(std::string path, BSFixedString invalidType) {
VMResultArray<BSFixedString> arr;
if (path.empty()) return arr;
s_dataLock.Enter();
Value value = Resolve(path);
if (!value.isNull() && value.isArray() && value.size() > 0) {
arr.reserve(value.size());
for (Value::iterator itr = value.begin(); itr != value.end(); ++itr) {
Value& var = *itr;
if (var.isString())
arr.push_back(BSFixedString(var.asCString()));
else if (var.isConvertibleTo(Json::stringValue))
arr.push_back(BSFixedString(var.asString().c_str()));
else
arr.push_back(invalidType);
}
}
s_dataLock.Leave();
return arr;
}*/
template VMResultArray<SInt32> ExternalFile::PathElements<SInt32>(const std::string &path, SInt32 invalidType);
template VMResultArray<float> ExternalFile::PathElements<float>(const std::string &path, float invalidType);
template VMResultArray<BSFixedString> ExternalFile::PathElements<BSFixedString>(const std::string &path, BSFixedString invalidType);
template VMResultArray<TESForm*> ExternalFile::PathElements<TESForm*>(const std::string &path, TESForm* invalidType);
VMResultArray<BSFixedString> ExternalFile::PathMembers(const std::string &path) {
VMResultArray<BSFixedString> arr;
if (path.empty()) return arr;
s_dataLock.Enter();
Value value = Resolve(path);
if (value.isObject() && !value.empty()) {
arr.reserve(value.size());
Value::Members members = value.getMemberNames();
for (Value::Members::iterator itr = members.begin(); itr != members.end(); ++itr) {
arr.push_back(BSFixedString((*itr).c_str()));
}
}
s_dataLock.Leave();
return arr;
}
int ExternalFile::PathCount(const std::string &path) {
if (path.empty()) return -1;
s_dataLock.Enter();
Value value = Resolve(path);
int count = (value.isArray() || value.isObject()) ? value.size() : -1;
s_dataLock.Leave();
return count;
}
bool ExternalFile::CanResolve(const std::string &path) {
return !Resolve(path).isNull();
}
bool ExternalFile::IsObject(const std::string &path) {
return Resolve(path).isObject();
}
bool ExternalFile::IsArray(const std::string &path) {
return Resolve(path).isArray();
}
bool ExternalFile::IsString(const std::string &path) {
return Resolve(path).isString();
}
bool ExternalFile::IsNumber(const std::string &path) {
return Resolve(path).isNumeric();
}
bool ExternalFile::IsBool(const std::string &path) {
return Resolve(path).isBool();
}