Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathStockpileSerializer.cpp
More file actions
2425 lines (2129 loc) · 99.8 KB
/
Copy pathStockpileSerializer.cpp
File metadata and controls
2425 lines (2129 loc) · 99.8 KB
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
// stockpiles plugin
#include "OrganicMatLookup.h"
#include "StockpileSerializer.h"
#include "StockpileUtils.h"
// dfhack
#include "Debug.h"
#include "MiscUtils.h"
#include "modules/Items.h"
// df
#include "df/building_stockpilest.h"
#include "df/creature_raw.h"
#include "df/caste_raw.h"
#include "df/descriptor_color.h"
#include "df/inorganic_raw.h"
#include "df/item_quality.h"
#include <df/itemdef_ammost.h>
#include <df/itemdef_armorst.h>
#include <df/itemdef_glovesst.h>
#include <df/itemdef_helmst.h>
#include <df/itemdef_pantsst.h>
#include <df/itemdef_shieldst.h>
#include <df/itemdef_shoesst.h>
#include <df/itemdef_trapcompst.h>
#include <df/itemdef_weaponst.h>
#include "df/furniture_type.h"
#include "df/material.h"
#include "df/plant_raw.h"
#include "df/stockpile_group_set.h"
// protobuf
#include <google/protobuf/io/zero_copy_stream_impl.h>
using std::string;
using std::vector;
using namespace DFHack;
using namespace df::enums;
using namespace google::protobuf;
using namespace dfstockpiles;
using df::global::world;
using std::placeholders::_1;
using std::placeholders::_2;
namespace DFHack
{
DBG_EXTERN(stockpiles, log);
}
static struct OtherMatsFurniture {
const std::map<int, string> mats;
OtherMatsFurniture() : mats(getMats()) {}
std::map<int, string> getMats() {
std::map<int, string> ret;
ret.emplace(0, "WOOD");
ret.emplace(1, "PLANT_CLOTH");
ret.emplace(2, "BONE");
ret.emplace(3, "TOOTH");
ret.emplace(4, "HORN");
ret.emplace(5, "PEARL");
ret.emplace(6, "SHELL");
ret.emplace(7, "LEATHER");
ret.emplace(8, "SILK");
ret.emplace(9, "AMBER");
ret.emplace(10, "CORAL");
ret.emplace(11, "GREEN_GLASS");
ret.emplace(12, "CLEAR_GLASS");
ret.emplace(13, "CRYSTAL_GLASS");
ret.emplace(14, "YARN");
return ret;
}
} mOtherMatsFurniture;
static struct OtherMatsBars {
const std::map<int, string> mats;
OtherMatsBars() : mats(getMats()) {}
std::map<int, string> getMats() {
std::map<int, string> ret;
ret.emplace(0, "COAL");
ret.emplace(1, "POTASH");
ret.emplace(2, "ASH");
ret.emplace(3, "PEARLASH");
ret.emplace(4, "SOAP");
return ret;
}
} mOtherMatsBars;
static struct OtherMatsBlocks {
const std::map<int, string> mats;
OtherMatsBlocks() : mats(getMats()) {}
std::map<int, string> getMats() {
std::map<int, string> ret;
ret.emplace(0, "GREEN_GLASS");
ret.emplace(1, "CLEAR_GLASS");
ret.emplace(2, "CRYSTAL_GLASS");
ret.emplace(3, "WOOD");
return ret;
}
} mOtherMatsBlocks;
static struct OtherMatsFinishedGoods {
const std::map<int, string> mats;
OtherMatsFinishedGoods() : mats(getMats()) {}
std::map<int, string> getMats() {
std::map<int, string> ret;
ret.emplace(0, "WOOD");
ret.emplace(1, "PLANT_CLOTH");
ret.emplace(2, "BONE");
ret.emplace(3, "TOOTH");
ret.emplace(4, "HORN");
ret.emplace(5, "PEARL");
ret.emplace(6, "SHELL");
ret.emplace(7, "LEATHER");
ret.emplace(8, "SILK");
ret.emplace(9, "AMBER");
ret.emplace(10, "CORAL");
ret.emplace(11, "GREEN_GLASS");
ret.emplace(12, "CLEAR_GLASS");
ret.emplace(13, "CRYSTAL_GLASS");
ret.emplace(14, "YARN");
ret.emplace(15, "WAX");
return ret;
}
} mOtherMatsFinishedGoods;
static struct OtherMatsWeaponsArmor {
const std::map<int, string> mats;
OtherMatsWeaponsArmor() : mats(getMats()) {}
std::map<int, string> getMats() {
std::map<int, string> ret;
ret.emplace(0, "WOOD");
ret.emplace(1, "PLANT_CLOTH");
ret.emplace(2, "BONE");
ret.emplace(3, "SHELL");
ret.emplace(4, "LEATHER");
ret.emplace(5, "SILK");
ret.emplace(6, "GREEN_GLASS");
ret.emplace(7, "CLEAR_GLASS");
ret.emplace(8, "CRYSTAL_GLASS");
ret.emplace(9, "YARN");
return ret;
}
} mOtherMatsWeaponsArmor;
StockpileSettingsSerializer::StockpileSettingsSerializer(df::stockpile_settings *settings)
: mSettings(settings) { }
StockpileSettingsSerializer::~StockpileSettingsSerializer() { }
StockpileSerializer::StockpileSerializer(df::building_stockpilest* stockpile)
: StockpileSettingsSerializer(&stockpile->settings), mPile(stockpile) { }
StockpileSerializer::~StockpileSerializer() { }
bool StockpileSettingsSerializer::serialize_to_ostream(color_ostream& out, std::ostream* output, uint32_t includedElements) {
if (output->fail())
return false;
mBuffer.Clear();
write(out, includedElements);
{
io::OstreamOutputStream zero_copy_output(output);
if (!mBuffer.SerializeToZeroCopyStream(&zero_copy_output))
return false;
}
return output->good();
}
bool StockpileSettingsSerializer::serialize_to_file(color_ostream& out, const string& file, uint32_t includedElements) {
std::fstream output(file, std::ios::out | std::ios::binary | std::ios::trunc);
if (output.fail()) {
WARN(log, out).print("ERROR: failed to open file for writing: '{}'\n",
file);
return false;
}
return serialize_to_ostream(out, &output, includedElements);
}
bool StockpileSettingsSerializer::parse_from_istream(color_ostream &out, std::istream* input, DeserializeMode mode, const vector<string>& filters) {
if (input->fail())
return false;
mBuffer.Clear();
io::IstreamInputStream zero_copy_input(input);
const bool res = mBuffer.ParseFromZeroCopyStream(&zero_copy_input)
&& input->eof();
if (res)
read(out, mode, filters);
return res;
}
bool StockpileSettingsSerializer::unserialize_from_file(color_ostream &out, const string& file, DeserializeMode mode, const vector<string>& filters) {
std::fstream input(file, std::ios::in | std::ios::binary);
if (input.fail()) {
WARN(log, out).print("failed to open file for reading: '{}'\n",
file);
return false;
}
return parse_from_istream(out, &input, mode, filters);
}
/**
* Find an enum's value based off the string label.
* @param token the string value in key_table
* @return the enum's value, -1 if not found
*/
template <typename E>
static typename df::enum_traits<E>::base_type token_to_enum_val(const string& token) {
E val;
if (!find_enum_item(&val, token))
return -1;
return val;
}
static bool matches_filter(color_ostream& out, const vector<string>& filters, const string& name) {
for (auto & filter : filters) {
DEBUG(log, out).print("searching for '{}' in '{}'\n", filter, name);
if (std::search(name.begin(), name.end(), filter.begin(), filter.end(),
[](unsigned char ch1, unsigned char ch2) { return std::toupper(ch1) == std::toupper(ch2); }
) != name.end())
return true;
}
return !filters.size();
}
static void set_flag(color_ostream& out, const char* name, const vector<string>& filters, bool all, char val, bool enabled, bool& elem) {
if ((all || enabled) && matches_filter(out, filters, name)) {
DEBUG(log, out).print("setting {} to {}\n", name, val);
elem = val;
}
}
static void set_filter_elem(color_ostream& out, const char* subcat, const vector<string>& filters, char val,
const string& name, const string& id, char& elem) {
if (matches_filter(out, filters, subcat + ((*subcat ? "/" : "") + name))) {
DEBUG(log, out).print("setting {} ({}) to {}\n", name, id, val);
elem = val;
}
}
template<typename T_val, typename T_id>
static void set_filter_elem(color_ostream& out, const char* subcat, const vector<string>& filters, T_val val,
const string& name, T_id id, T_val& elem) {
if (matches_filter(out, filters, subcat + ((*subcat ? "/" : "") + name))) {
DEBUG(log, out).print("setting {} ({}) to {}\n", name, id, val);
elem = val;
}
}
/**
* There are many repeated (un)serialization cases throughout the stockpile_settings structure,
* so the most common cases have been generalized into generic functions using lambdas.
*
* The basic process to serialize a stockpile_settings structure is:
* 1. loop through the list
* 2. for every element that is TRUE:
* 3. map the specific stockpile_settings index into a general material, creature, etc index
* 4. verify that type is allowed in the list (e.g., no stone in gems stockpiles)
* 5. add it to the protobuf using FuncWriteExport
*
* The unserialization process is the same in reverse.
*/
static bool serialize_list_itemdef(color_ostream& out, FuncWriteExport add_value,
vector<char> list,
vector<df::itemdef*> items,
item_type::item_type type) {
bool all = true;
for (size_t i = 0; i < list.size(); ++i) {
if (!list.at(i)) {
all = false;
continue;
}
const df::itemdef* a = items.at(i);
// skip procedurally generated items
if (a->base_flags.is_set(df::itemdef_flags::GENERATED))
continue;
ItemTypeInfo ii;
if (!ii.decode(type, i))
continue;
DEBUG(log, out).print("adding itemdef type {}\n", ii.getToken());
add_value(ii.getToken());
}
return all;
}
static void unserialize_list_itemdef(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
FuncReadImport read_value, int32_t list_size, vector<char>& pile_list, item_type::item_type type) {
int num_elems = Items::getSubtypeCount(type);
pile_list.resize(num_elems, '\0');
if (all) {
for (auto idx = 0; idx < num_elems; ++idx) {
ItemTypeInfo ii;
ii.decode(type, idx);
set_filter_elem(out, subcat, filters, val, ii.toString(), idx, pile_list.at(idx));
}
return;
}
for (auto i = 0; i < list_size; ++i) {
string id = read_value(i);
ItemTypeInfo ii;
if (!ii.find(id))
continue;
if (ii.subtype < 0 || size_t(ii.subtype) >= pile_list.size()) {
WARN(log, out).print("item type index invalid: {}\n", ii.subtype);
continue;
}
set_filter_elem(out, subcat, filters, val, id, ii.subtype, pile_list.at(ii.subtype));
}
}
static bool serialize_list_quality(color_ostream& out, FuncWriteExport add_value,
const bool(&quality_list)[7]) {
using df::enums::item_quality::item_quality;
using quality_traits = df::enum_traits<item_quality>;
bool all = true;
for (size_t i = 0; i < 7; ++i) {
if (!quality_list[i]) {
all = false;
continue;
}
const string f_type(quality_traits::key_table[i]);
add_value(f_type);
DEBUG(log, out).print("adding quality {}\n", f_type);
}
return all;
}
static void quality_clear(bool(&pile_list)[7]) {
std::fill(pile_list, pile_list + 7, false);
}
static void unserialize_list_quality(color_ostream& out, const char* subcat, bool all, bool val, const vector<string>& filters,
FuncReadImport read_value, int32_t list_size, bool(&pile_list)[7]) {
if (all) {
for (auto idx = 0; idx < 7; ++idx) {
string id = ENUM_KEY_STR(item_quality, (df::item_quality)idx);
set_filter_elem(out, subcat, filters, val, id, idx, pile_list[idx]);
}
return;
}
using df::enums::item_quality::item_quality;
for (int i = 0; i < list_size; ++i) {
const string quality = read_value(i);
df::enum_traits<item_quality>::base_type idx = token_to_enum_val<item_quality>(quality);
if (idx < 0) {
WARN(log, out).print("invalid quality token: {}\n", quality);
continue;
}
set_filter_elem(out, subcat, filters, val, quality, idx, pile_list[idx]);
}
}
static string other_mats_index(const std::map<int, string> other_mats,
int idx) {
auto it = other_mats.find(idx);
if (it == other_mats.end())
return string();
return it->second;
}
static int other_mats_token(const std::map<int, string> other_mats,
const string& token) {
for (auto it = other_mats.begin(); it != other_mats.end(); ++it) {
if (it->second == token)
return it->first;
}
return -1;
}
static bool serialize_list_other_mats(color_ostream& out,
const std::map<int, string> other_mats,
FuncWriteExport add_value,
vector<char> list) {
bool all = true;
for (size_t i = 0; i < list.size(); ++i) {
if (!list.at(i)) {
all = false;
continue;
}
const string token = other_mats_index(other_mats, i);
if (token.empty()) {
WARN(log, out).print("invalid other material with index {}\n", i);
continue;
}
add_value(token);
DEBUG(log, out).print("other mats {} is {}\n", i, token);
}
return all;
}
static void unserialize_list_other_mats(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
const std::map<int, string> other_mats, FuncReadImport read_value, int32_t list_size, vector<char>& pile_list) {
size_t num_elems = other_mats.size();
pile_list.resize(num_elems, '\0');
if (all) {
for (auto & entry : other_mats)
set_filter_elem(out, subcat, filters, val, entry.second, entry.first, pile_list.at(entry.first));
return;
}
for (int i = 0; i < list_size; ++i) {
const string token = read_value(i);
size_t idx = other_mats_token(other_mats, token);
if (idx < 0) {
WARN(log, out).print("invalid other mat with token {}\n", token);
continue;
}
if (idx >= num_elems) {
WARN(log, out).print("other_mats index too large! idx[{}] max_size[{}]\n", idx, num_elems);
continue;
}
set_filter_elem(out, subcat, filters, val, token, idx, pile_list.at(idx));
}
}
static bool serialize_list_organic_mat(color_ostream& out, FuncWriteExport add_value,
const vector<char>* list,
organic_mat_category::organic_mat_category cat) {
bool all = true;
if (!list) {
DEBUG(log, out).print("serialize_list_organic_mat: list null\n");
return all;
}
for (size_t i = 0; i < list->size(); ++i) {
if (!list->at(i)) {
all = false;
continue;
}
OrganicMatLookup::FoodMat food_mat;
OrganicMatLookup::food_mat_by_idx(out, cat, i, food_mat);
string token = OrganicMatLookup::food_token_by_idx(out, food_mat);
if (token.empty()) {
DEBUG(log, out).print("food mat invalid :(\n");
continue;
}
DEBUG(log, out).print("organic_material {} is {}\n", i, token);
add_value(token);
}
return all;
}
static string get_filter_string(color_ostream& out, const OrganicMatLookup::FoodMat& food_mat) {
auto str = OrganicMatLookup::food_token_by_idx(out, food_mat);
if (auto plant = food_mat.material.plant) {
if (plant->flags.is_set(df::plant_raw_flags::DRINK))
str += "/brewable";
if (plant->flags.is_set(df::plant_raw_flags::MILL))
str += "/millable";
if (auto mat = food_mat.material.material) {
if (mat->flags.is_set(df::material_flags::STRUCTURAL_PLANT_MAT) &&
(plant->flags.is_set(df::plant_raw_flags::THREAD) ||
plant->flags.is_set(df::plant_raw_flags::EXTRACT_VIAL) ||
plant->flags.is_set(df::plant_raw_flags::EXTRACT_BARREL)))
str += "/processable";
}
}
return str;
}
static void unserialize_list_organic_mat(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
FuncReadImport read_value, size_t list_size, vector<char>& pile_list,
organic_mat_category::organic_mat_category cat) {
size_t num_elems = OrganicMatLookup::food_max_size(cat);
pile_list.resize(num_elems, '\0');
if (all) {
for (size_t idx = 0; idx < num_elems; ++idx) {
OrganicMatLookup::FoodMat food_mat;
OrganicMatLookup::food_mat_by_idx(out, cat, idx, food_mat);
set_filter_elem(out, subcat, filters, val, get_filter_string(out, food_mat), idx, pile_list.at(idx));
}
return;
}
for (size_t i = 0; i < list_size; ++i) {
const string token = read_value(i);
int16_t idx = OrganicMatLookup::food_idx_by_token(out, cat, token);
if (idx < 0 || size_t(idx) >= num_elems) {
WARN(log, out).print("organic mat index too large! idx[{}] max_size[{}]\n", idx, num_elems);
continue;
}
OrganicMatLookup::FoodMat food_mat;
OrganicMatLookup::food_mat_by_idx(out, cat, idx, food_mat);
set_filter_elem(out, subcat, filters, val, get_filter_string(out, food_mat), idx, pile_list.at(idx));
}
}
static bool serialize_list_color(color_ostream& out, FuncWriteExport add_value, const vector<char>* colors) {
bool all = world->raws.descriptors.colors.size() == colors->size();
if (!colors) {
DEBUG(log, out).print("serialize_list_color: list null\n");
return all;
}
for (size_t i = 0; i < colors->size(); ++i) {
if (!colors->at(i)) {
all = false;
continue;
}
add_value(world->raws.descriptors.colors[i]->id);
}
return all;
}
// Find the index of a named color, returning SIZE_MAX on unknown.
static size_t find_color_by_token(const string& token) {
auto& colors = world->raws.descriptors.colors;
size_t num_colors = colors.size();
for (size_t idx = 0; idx < num_colors; ++ idx) {
if (colors[idx]->id == token) {
return idx;
}
}
return SIZE_MAX;
}
static void unserialize_list_color(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
FuncReadImport read_value, int32_t list_size, vector<char>& pile_list) {
size_t num_elems = world->raws.descriptors.colors.size();
pile_list.resize(num_elems, '\0');
if (all) {
for (size_t idx = 0; idx < num_elems; ++idx) {
set_filter_elem(out, subcat, filters, val, world->raws.descriptors.colors[idx]->id, idx, pile_list[idx]);
}
return;
}
for (int32_t idx = 0; idx < list_size; ++idx) {
const string& value = read_value(idx);
size_t color = find_color_by_token(value);
if (color == SIZE_MAX) {
WARN(log, out).print("unknown color {}\n", value);
continue;
}
set_filter_elem(out, subcat, filters, val, value, color, pile_list[color]);
}
}
static bool serialize_list_item_type(color_ostream& out, FuncItemAllowed is_allowed,
FuncWriteExport add_value, const vector<char>& list) {
using df::enums::item_type::item_type;
using type_traits = df::enum_traits<item_type>;
bool all = true;
size_t num_item_types = list.size();
DEBUG(log, out).print("item_type size = {} size limit = {} typecasted: {}\n",
num_item_types, type_traits::last_item_value,
(size_t)type_traits::last_item_value);
for (size_t i = 0; i <= (size_t)type_traits::last_item_value; ++i) {
if (i < num_item_types && !list.at(i)) {
all = false;
continue;
}
const item_type type = (item_type)((df::enum_traits<item_type>::base_type)i);
string r_type(type_traits::key_table[i + 1]);
if (!is_allowed(type))
continue;
add_value(r_type);
DEBUG(log, out).print("item_type key_table[{}] type[{}] is {}\n", i + 1, (int16_t)type, r_type);
}
return all;
}
static void unserialize_list_item_type(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
FuncItemAllowed is_allowed, FuncReadImport read_value, int32_t list_size, vector<char>& pile_list) {
// TODO can we remove the hardcoded list size?
size_t num_elems = 112;
pile_list.resize(num_elems, '\0');
for (size_t i = 0; i < num_elems; ++i) {
if (!is_allowed((df::item_type)i))
pile_list.at(i) = 1;
}
if (all) {
for (size_t idx = 0; idx < num_elems; ++idx) {
string id = ENUM_KEY_STR(item_type, (df::item_type)idx);
set_filter_elem(out, subcat, filters, val, id, idx, pile_list.at(idx));
}
return;
}
using df::enums::item_type::item_type;
for (int i = 0; i < list_size; ++i) {
const string token = read_value(i);
const df::enum_traits<item_type>::base_type idx = token_to_enum_val<item_type>(token);
if (!is_allowed((item_type)idx))
continue;
if (idx < 0 || size_t(idx) >= num_elems) {
WARN(log, out).print("error item_type index too large! idx[{}] max_size[{}]\n", idx, num_elems);
continue;
}
set_filter_elem(out, subcat, filters, val, token, idx, pile_list.at(idx));
}
}
static bool serialize_list_material(color_ostream& out, FuncMaterialAllowed is_allowed,
FuncWriteExport add_value, const vector<char>& list) {
bool all = true;
MaterialInfo mi;
for (size_t i = 0; i < list.size(); ++i) {
if (!list.at(i)) {
all = false;
continue;
}
mi.decode(0, i);
if (!is_allowed(mi))
continue;
DEBUG(log, out).print("adding material {}\n", mi.getToken());
add_value(mi.getToken());
}
return all;
}
static void unserialize_list_material(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
FuncMaterialAllowed is_allowed, FuncReadImport read_value, int32_t list_size,
vector<char>& pile_list) {
// we initialize all disallowed values to 1
// why? because that's how the memory is in DF before we muck with it.
size_t num_elems = world->raws.inorganics.all.size();
pile_list.resize(num_elems, 0);
for (size_t i = 0; i < pile_list.size(); ++i) {
MaterialInfo mi(0, i);
if (!is_allowed(mi))
pile_list.at(i) = 1;
}
if (all) {
for (size_t idx = 0; idx < num_elems; ++idx) {
MaterialInfo mi;
mi.decode(0, idx);
set_filter_elem(out, subcat, filters, val, mi.toString(), idx, pile_list.at(idx));
}
return;
}
for (auto i = 0; i < list_size; ++i) {
string id = read_value(i);
MaterialInfo mi;
if (!mi.find(id) || !is_allowed(mi))
continue;
if (mi.index < 0 || size_t(mi.index) >= pile_list.size()) {
WARN(log, out).print("material type index invalid: {}\n", mi.index);
continue;
}
set_filter_elem(out, subcat, filters, val, id, mi.index, pile_list.at(mi.index));
}
}
static bool serialize_list_creature(color_ostream& out, FuncWriteExport add_value, const vector<char>& list) {
bool all = true;
for (size_t i = 0; i < list.size(); ++i) {
if (!list.at(i)) {
all = false;
continue;
}
df::creature_raw* r = find_creature(i);
if (r->flags.is_set(creature_raw_flags::GENERATED)
|| r->creature_id == "EQUIPMENT_WAGON")
continue;
DEBUG(log, out).print("adding creature {}\n", r->creature_id);
add_value(r->creature_id);
}
return all;
}
static string get_filter_string(df::creature_raw *r) {
if (!r->caste.size() ||
(!r->caste[0]->flags.is_set(df::enums::caste_raw_flags::PET) &&
!r->caste[0]->flags.is_set(df::enums::caste_raw_flags::PET_EXOTIC)))
return r->name[0];
return r->name[0] + "/tameable";
}
static void unserialize_list_creature(color_ostream& out, const char* subcat, bool all, char val, const vector<string>& filters,
FuncReadImport read_value, int32_t list_size, vector<char>& pile_list) {
size_t num_elems = world->raws.creatures.all.size();
pile_list.resize(num_elems, '\0');
if (all) {
for (size_t idx = 0; idx < num_elems; ++idx) {
auto r = find_creature(idx);
set_filter_elem(out, subcat, filters, val, get_filter_string(r), r->creature_id, pile_list.at(idx));
}
return;
}
for (auto i = 0; i < list_size; ++i) {
string id = read_value(i);
int idx = find_creature(id);
if (idx < 0 || size_t(idx) >= num_elems) {
WARN(log, out).print("animal index invalid: {}\n", idx);
continue;
}
auto r = find_creature(idx);
set_filter_elem(out, subcat, filters, val, get_filter_string(r), r->creature_id, pile_list.at(idx));
}
}
template<typename T_cat_set>
static void write_cat(color_ostream& out, const char* name, bool include_types, uint32_t cat_flags,
enum df::stockpile_group_set::Mask cat_mask,
std::function<T_cat_set* ()> mutable_cat_fn,
std::function<bool(color_ostream&, T_cat_set*)> write_cat_fn) {
if (!(cat_flags & cat_mask))
return;
T_cat_set* cat_set = mutable_cat_fn();
if (!include_types) {
DEBUG(log, out).print("including all for {} since only category is being recorded\n", name);
cat_set->set_all(true);
return;
}
if (write_cat_fn(out, cat_set)) {
// all fields were set. clear them and use the "all" flag instead so "all" can be applied
// to other worlds with other generated types
DEBUG(log, out).print("including all for {} since all fields were enabled\n", name);
cat_set->Clear();
cat_set->set_all(true);
}
}
void StockpileSettingsSerializer::write(color_ostream& out, uint32_t includedElements) {
if (includedElements & INCLUDED_ELEMENTS_GENERAL)
write_general(out);
if (!(includedElements & INCLUDED_ELEMENTS_CATEGORIES))
return;
DEBUG(log, out).print("GROUP SET {}\n",
bitfield_to_string(mSettings->flags).c_str());
bool include_types = 0 != (includedElements & INCLUDED_ELEMENTS_TYPES);
write_cat<StockpileSettings_AmmoSet>(out, "ammo", include_types,
mSettings->flags.whole,
mSettings->flags.mask_ammo,
std::bind(&StockpileSettings::mutable_ammo, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_ammo, this, _1, _2));
write_cat<StockpileSettings_AnimalsSet>(out, "animals", include_types,
mSettings->flags.whole,
mSettings->flags.mask_animals,
std::bind(&StockpileSettings::mutable_animals, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_animals, this, _1, _2));
write_cat<StockpileSettings_ArmorSet>(out, "armor", include_types,
mSettings->flags.whole,
mSettings->flags.mask_armor,
std::bind(&StockpileSettings::mutable_armor, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_armor, this, _1, _2));
write_cat<StockpileSettings_BarsBlocksSet>(out, "bars_blocks", include_types,
mSettings->flags.whole,
mSettings->flags.mask_bars_blocks,
std::bind(&StockpileSettings::mutable_barsblocks, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_bars_blocks, this, _1, _2));
write_cat<StockpileSettings_ClothSet>(out, "cloth", include_types,
mSettings->flags.whole,
mSettings->flags.mask_cloth,
std::bind(&StockpileSettings::mutable_cloth, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_cloth, this, _1, _2));
write_cat<StockpileSettings_CoinSet>(out, "coin", include_types,
mSettings->flags.whole,
mSettings->flags.mask_coins,
std::bind(&StockpileSettings::mutable_coin, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_coins, this, _1, _2));
write_cat<StockpileSettings_FinishedGoodsSet>(out, "finished_goods", include_types,
mSettings->flags.whole,
mSettings->flags.mask_finished_goods,
std::bind(&StockpileSettings::mutable_finished_goods, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_finished_goods, this, _1, _2));
write_cat<StockpileSettings_FoodSet>(out, "food", include_types,
mSettings->flags.whole,
mSettings->flags.mask_food,
std::bind(&StockpileSettings::mutable_food, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_food, this, _1, _2));
write_cat<StockpileSettings_FurnitureSet>(out, "furniture", include_types,
mSettings->flags.whole,
mSettings->flags.mask_furniture,
std::bind(&StockpileSettings::mutable_furniture, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_furniture, this, _1, _2));
write_cat<StockpileSettings_GemsSet>(out, "gems", include_types,
mSettings->flags.whole,
mSettings->flags.mask_gems,
std::bind(&StockpileSettings::mutable_gems, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_gems, this, _1, _2));
write_cat<StockpileSettings_LeatherSet>(out, "leather", include_types,
mSettings->flags.whole,
mSettings->flags.mask_leather,
std::bind(&StockpileSettings::mutable_leather, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_leather, this, _1, _2));
write_cat<StockpileSettings_CorpsesSet>(out, "corpses", include_types,
mSettings->flags.whole,
mSettings->flags.mask_corpses,
std::bind(&StockpileSettings::mutable_corpses_v50, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_corpses, this, _1, _2));
write_cat<StockpileSettings_RefuseSet>(out, "refuse", include_types,
mSettings->flags.whole,
mSettings->flags.mask_refuse,
std::bind(&StockpileSettings::mutable_refuse, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_refuse, this, _1, _2));
write_cat<StockpileSettings_SheetSet>(out, "sheet", include_types,
mSettings->flags.whole,
mSettings->flags.mask_sheet,
std::bind(&StockpileSettings::mutable_sheet, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_sheet, this, _1, _2));
write_cat<StockpileSettings_StoneSet>(out, "stone", include_types,
mSettings->flags.whole,
mSettings->flags.mask_stone,
std::bind(&StockpileSettings::mutable_stone, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_stone, this, _1, _2));
write_cat<StockpileSettings_WeaponsSet>(out, "weapons", include_types,
mSettings->flags.whole,
mSettings->flags.mask_weapons,
std::bind(&StockpileSettings::mutable_weapons, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_weapons, this, _1, _2));
write_cat<StockpileSettings_WoodSet>(out, "wood", include_types,
mSettings->flags.whole,
mSettings->flags.mask_wood,
std::bind(&StockpileSettings::mutable_wood, &mBuffer),
std::bind(&StockpileSettingsSerializer::write_wood, this, _1, _2));
}
void StockpileSerializer::write(color_ostream& out, uint32_t includedElements) {
if (includedElements & INCLUDED_ELEMENTS_CONTAINERS)
write_containers(out);
StockpileSettingsSerializer::write(out, includedElements);
}
void StockpileSettingsSerializer::read(color_ostream &out, DeserializeMode mode, const vector<string>& filters) {
DEBUG(log).print("==READ==\n");
read_general(out, mode);
read_ammo(out, mode, filters);
read_animals(out, mode, filters);
read_armor(out, mode, filters);
read_bars_blocks(out, mode, filters);
read_cloth(out, mode, filters);
read_coins(out, mode, filters);
read_finished_goods(out, mode, filters);
read_food(out, mode, filters);
read_furniture(out, mode, filters);
read_gems(out, mode, filters);
read_leather(out, mode, filters);
// support for old versions before corpses had a set
if (mBuffer.has_corpses()) {
StockpileSettings::CorpsesSet* corpses = mBuffer.mutable_corpses_v50();
corpses->set_all(true);
}
read_corpses(out, mode, filters);
read_refuse(out, mode, filters);
read_sheet(out, mode, filters);
read_stone(out, mode, filters);
read_weapons(out, mode, filters);
read_wood(out, mode, filters);
}
void StockpileSerializer::read(color_ostream &out, DeserializeMode mode, const vector<string>& filters) {
read_containers(out, mode);
StockpileSettingsSerializer::read(out, mode, filters);
}
void StockpileSerializer::write_containers(color_ostream& out) {
DEBUG(log, out).print("writing container settings\n");
mBuffer.set_max_bins(mPile->storage.max_bins);
mBuffer.set_max_barrels(mPile->storage.max_barrels);
mBuffer.set_max_wheelbarrows(mPile->storage.max_wheelbarrows);
}
template<typename T_elem, typename T_elem_ret>
static void read_elem(color_ostream& out, const char* name, DeserializeMode mode,
std::function<bool()> has_elem_fn,
std::function<T_elem_ret()> elem_fn,
T_elem &setting) {
if (!has_elem_fn())
return;
bool is_set = elem_fn() != 0;
if (mode == DESERIALIZE_MODE_SET || is_set) {
T_elem val = (mode == DESERIALIZE_MODE_DISABLE) ? 0 : elem_fn();
DEBUG(log, out).print("setting {} to {}\n", name, val);
setting = val;
}
}
template<typename T_cat>
static void read_category(color_ostream& out, const char* name, DeserializeMode mode,
std::function<bool()> has_cat_fn,
std::function<const T_cat &()> cat_fn,
uint32_t & cat_flags,
enum df::stockpile_group_set::Mask cat_mask,
std::function<void()> clear_fn,
std::function<void(bool, char)> set_fn) {
if (mode == DESERIALIZE_MODE_SET) {
DEBUG(log, out).print("clearing {}\n", name);
cat_flags &= ~cat_mask;
clear_fn();
}
if (!has_cat_fn())
return;
if (mode == DESERIALIZE_MODE_DISABLE && !(cat_flags & cat_mask))
return;
if (mode == DESERIALIZE_MODE_SET || mode == DESERIALIZE_MODE_ENABLE)
cat_flags |= cat_mask;
bool all = cat_fn().all();
char val = (mode == DESERIALIZE_MODE_DISABLE) ? (char)0 : (char)1;
DEBUG(log, out).print("setting {} {} elements to {}\n",
all ? "all" : "marked", name, val);
set_fn(all, val);
}
void StockpileSerializer::read_containers(color_ostream& out, DeserializeMode mode) {
read_elem<int16_t, int32_t>(out, "max_bins", mode,
std::bind(&StockpileSettings::has_max_bins, mBuffer),
std::bind(&StockpileSettings::max_bins, mBuffer),
mPile->storage.max_bins);
read_elem<int16_t, int32_t>(out, "max_barrels", mode,
std::bind(&StockpileSettings::has_max_barrels, mBuffer),
std::bind(&StockpileSettings::max_barrels, mBuffer),
mPile->storage.max_barrels);
read_elem<int16_t, int32_t>(out, "max_wheelbarrows", mode,
std::bind(&StockpileSettings::has_max_wheelbarrows, mBuffer),
std::bind(&StockpileSettings::max_wheelbarrows, mBuffer),
mPile->storage.max_wheelbarrows);
}
void StockpileSettingsSerializer::write_general(color_ostream& out) {
DEBUG(log, out).print("writing general settings\n");
mBuffer.set_allow_inorganic(mSettings->misc.allow_inorganic);
mBuffer.set_allow_organic(mSettings->misc.allow_organic);
}
void StockpileSerializer::write_general(color_ostream& out) {
StockpileSettingsSerializer::write_general(out);
mBuffer.set_use_links_only(mPile->stockpile_flag.bits.use_links_only);
}
void StockpileSettingsSerializer::read_general(color_ostream& out, DeserializeMode mode) {
read_elem<bool, bool>(out, "allow_inorganic", mode,
std::bind(&StockpileSettings::has_allow_inorganic, mBuffer),
std::bind(&StockpileSettings::allow_inorganic, mBuffer),
mSettings->misc.allow_inorganic);
read_elem<bool, bool>(out, "allow_organic", mode,
std::bind(&StockpileSettings::has_allow_organic, mBuffer),
std::bind(&StockpileSettings::allow_organic, mBuffer),
mSettings->misc.allow_organic);
}
void StockpileSerializer::read_general(color_ostream& out, DeserializeMode mode) {
StockpileSettingsSerializer::read_general(out, mode);
if (!mBuffer.has_use_links_only())
return;
bool use_links_only = mBuffer.use_links_only();
DEBUG(log, out).print("setting use_links_only to {}\n", use_links_only);
mPile->stockpile_flag.bits.use_links_only = use_links_only;
}
static bool ammo_mat_is_allowed(const MaterialInfo& mi) {
return mi.isValid() && mi.material && mi.material->flags.is_set(material_flags::IS_METAL);
}
bool StockpileSettingsSerializer::write_ammo(color_ostream& out, StockpileSettings::AmmoSet* ammo) {
bool all = serialize_list_itemdef(out,
[&](const string& token) { ammo->add_type(token); },
mSettings->ammo.type,
vector<df::itemdef*>(world->raws.itemdefs.ammo.begin(), world->raws.itemdefs.ammo.end()),
item_type::AMMO);
all = serialize_list_material(out,
ammo_mat_is_allowed,
[&](const string& token) { ammo->add_mats(token); },
mSettings->ammo.mats) && all;
if (mSettings->ammo.other_mats.size() > 2) {
WARN(log, out).print("ammo other materials > 2: {}\n",
mSettings->ammo.other_mats.size());
}
size_t num_other_mats = std::min(size_t(2),
mSettings->ammo.other_mats.size());
for (size_t i = 0; i < num_other_mats; ++i) {
You can’t perform that action at this time.
