Project Alice
Loading...
Searching...
No Matches
serialization.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include "container_types.hpp"
4#include "unordered_dense.h"
5#include "text.hpp"
6#include "simple_fs.hpp"
7
8namespace sys {
9
10template<typename T>
11inline size_t serialize_size(std::vector<T> const& vec) {
12 return sizeof(uint32_t) + sizeof(T) * vec.size();
13}
14
15template<typename T>
16inline uint8_t* serialize(uint8_t* ptr_in, std::vector<T> const& vec) {
17 uint32_t length = uint32_t(vec.size());
18 memcpy(ptr_in, &length, sizeof(uint32_t));
19 memcpy(ptr_in + sizeof(uint32_t), vec.data(), sizeof(T) * vec.size());
20 return ptr_in + sizeof(uint32_t) + sizeof(T) * vec.size();
21}
22
23template<typename T>
24inline uint8_t const* deserialize(uint8_t const* ptr_in, std::vector<T>& vec) {
25 uint32_t length = 0;
26 memcpy(&length, ptr_in, sizeof(uint32_t));
27 vec.resize(length);
28 memcpy(vec.data(), ptr_in + sizeof(uint32_t), sizeof(T) * length);
29 return ptr_in + sizeof(uint32_t) + sizeof(T) * length;
30}
31
32template<typename T>
33inline uint8_t* memcpy_serialize(uint8_t* ptr_in, T const& obj) {
34 memcpy(ptr_in, &obj, sizeof(T));
35 return ptr_in + sizeof(T);
36}
37
38template<typename T>
39inline uint8_t const* memcpy_deserialize(uint8_t const* ptr_in, T& obj) {
40 memcpy(&obj, ptr_in, sizeof(T));
41 return ptr_in + sizeof(T);
42}
43
44template<typename T, typename tag_type>
45inline size_t serialize_size(tagged_vector<T, tag_type> const& vec) {
46 return sizeof(uint32_t) + sizeof(T) * vec.size();
47}
48
49template<typename T, typename tag_type>
51 uint32_t length = uint32_t(vec.size());
52 memcpy(ptr_in, &length, sizeof(uint32_t));
53 memcpy(ptr_in + sizeof(uint32_t), vec.data(), sizeof(T) * vec.size());
54 return ptr_in + sizeof(uint32_t) + sizeof(T) * vec.size();
55}
56
57template<typename T, typename tag_type>
58inline uint8_t const* deserialize(uint8_t const* ptr_in, tagged_vector<T, tag_type>& vec) {
59 uint32_t length = 0;
60 memcpy(&length, ptr_in, sizeof(uint32_t));
61 vec.resize(length);
62 memcpy(vec.data(), ptr_in + sizeof(uint32_t), sizeof(T) * length);
63 return ptr_in + sizeof(uint32_t) + sizeof(T) * length;
64}
65
66inline size_t serialize_size(
67 ankerl::unordered_dense::map<dcon::text_key, uint32_t, text::vector_backed_ci_hash, text::vector_backed_ci_eq> const&
68 vec) {
69 return serialize_size(vec.values());
70}
71
72inline size_t serialize_size(
73 ankerl::unordered_dense::set<dcon::text_key, text::vector_backed_ci_hash, text::vector_backed_ci_eq> const&
74 vec) {
75 return serialize_size(vec.values());
76}
77
78inline uint8_t* serialize(uint8_t* ptr_in,
79 ankerl::unordered_dense::map<dcon::text_key, uint32_t, text::vector_backed_ci_hash, text::vector_backed_ci_eq> const&
80 vec) {
81 return serialize(ptr_in, vec.values());
82}
83inline uint8_t* serialize(uint8_t* ptr_in,
84 ankerl::unordered_dense::set<dcon::text_key, text::vector_backed_ci_hash, text::vector_backed_ci_eq> const&
85 vec) {
86 return serialize(ptr_in, vec.values());
87}
88inline uint8_t const* deserialize(uint8_t const* ptr_in,
89 ankerl::unordered_dense::map<dcon::text_key, uint32_t, text::vector_backed_ci_hash, text::vector_backed_ci_eq>& vec) {
90 uint32_t length = 0;
91 memcpy(&length, ptr_in, sizeof(uint32_t));
92
93 std::remove_cvref_t<decltype(vec.values())> new_vec;
94 new_vec.resize(length);
95 memcpy(new_vec.data(), ptr_in + sizeof(uint32_t), sizeof(vec.values()[0]) * length);
96 vec.replace(std::move(new_vec));
97
98 return ptr_in + sizeof(uint32_t) + sizeof(vec.values()[0]) * length;
99}
100inline uint8_t const* deserialize(uint8_t const* ptr_in,
101 ankerl::unordered_dense::set<dcon::text_key, text::vector_backed_ci_hash, text::vector_backed_ci_eq>& vec) {
102 uint32_t length = 0;
103 memcpy(&length, ptr_in, sizeof(uint32_t));
104
105 std::remove_cvref_t<decltype(vec.values())> new_vec;
106 new_vec.resize(length);
107 memcpy(new_vec.data(), ptr_in + sizeof(uint32_t), sizeof(vec.values()[0]) * length);
108 vec.replace(std::move(new_vec));
109
110 return ptr_in + sizeof(uint32_t) + sizeof(vec.values()[0]) * length;
111}
112
113inline size_t serialize_size(
114 ankerl::unordered_dense::map<dcon::modifier_id, dcon::gfx_object_id, sys::modifier_hash> const& vec) {
115 return serialize_size(vec.values());
116}
117
118inline uint8_t* serialize(uint8_t* ptr_in,
119 ankerl::unordered_dense::map<dcon::modifier_id, dcon::gfx_object_id, sys::modifier_hash> const& vec) {
120 return serialize(ptr_in, vec.values());
121}
122inline uint8_t const* deserialize(uint8_t const* ptr_in,
123 ankerl::unordered_dense::map<dcon::modifier_id, dcon::gfx_object_id, sys::modifier_hash>& vec) {
124 uint32_t length = 0;
125 memcpy(&length, ptr_in, sizeof(uint32_t));
126
127 std::remove_cvref_t<decltype(vec.values())> new_vec;
128 new_vec.resize(length);
129 memcpy(new_vec.data(), ptr_in + sizeof(uint32_t), sizeof(vec.values()[0]) * length);
130 vec.replace(std::move(new_vec));
131
132 return ptr_in + sizeof(uint32_t) + sizeof(vec.values()[0]) * length;
133}
134
135inline size_t serialize_size(ankerl::unordered_dense::map<uint16_t, dcon::text_key> const& vec) {
136 return serialize_size(vec.values());
137}
138
139inline uint8_t* serialize(uint8_t* ptr_in, ankerl::unordered_dense::map<uint16_t, dcon::text_key> const& vec) {
140 return serialize(ptr_in, vec.values());
141}
142inline uint8_t const* deserialize(uint8_t const* ptr_in, ankerl::unordered_dense::map<uint16_t, dcon::text_key>& vec) {
143 uint32_t length = 0;
144 memcpy(&length, ptr_in, sizeof(uint32_t));
145
146 std::remove_cvref_t<decltype(vec.values())> new_vec;
147 new_vec.resize(length);
148 memcpy(new_vec.data(), ptr_in + sizeof(uint32_t), sizeof(vec.values()[0]) * length);
149 vec.replace(std::move(new_vec));
150
151 return ptr_in + sizeof(uint32_t) + sizeof(vec.values()[0]) * length;
152}
153
154constexpr inline uint32_t save_file_version = 42;
156
162};
163
169 dcon::national_identity_id tag;
170 dcon::government_type_id cgov;
172 char save_name[32];
173};
174
179};
180
181void read_mod_path(uint8_t const* ptr_in, uint8_t const* lim, native_string& path_out);
182uint8_t const* load_mod_path(uint8_t const* ptr_in, sys::state& state);
183uint8_t* write_mod_path(uint8_t* ptr_in, native_string const& path_in);
184size_t sizeof_mod_path(native_string const& path_in);
185
186uint8_t const* read_scenario_header(uint8_t const* ptr_in, scenario_header& header_out);
187uint8_t const* read_save_header(uint8_t const* ptr_in, save_header& header_out);
188uint8_t* write_scenario_header(uint8_t* ptr_in, scenario_header const& header_in);
189uint8_t* write_save_header(uint8_t* ptr_in, save_header const& header_in);
190size_t sizeof_scenario_header(scenario_header const& header_in);
191size_t sizeof_save_header(save_header const& header_in);
192
194
195uint8_t* write_compressed_section(uint8_t* ptr_out, uint8_t const* ptr_in, uint32_t uncompressed_size);
196
197// Note: these functions are for read / writing the *uncompressed* data
198uint8_t const* read_scenario_section(uint8_t const* ptr_in, uint8_t const* section_end, sys::state& state);
199uint8_t const* read_save_section(uint8_t const* ptr_in, uint8_t const* section_end, sys::state& state);
205};
208
213
214void write_save_file(sys::state& state, sys::save_type type = sys::save_type::normal, std::string const& name = std::string(""));
216
217} // namespace sys
auto data() const
auto size() const
void resize(size_t size)
Definition: constants.hpp:4
uint8_t * write_mod_path(uint8_t *ptr_in, native_string const &path_in)
uint8_t * write_scenario_section(uint8_t *ptr_in, sys::state &state)
bool try_read_scenario_as_save_file(sys::state &state, native_string_view name)
size_t sizeof_save_section(sys::state &state)
uint8_t const * read_save_header(uint8_t const *ptr_in, save_header &header_out)
mod_identifier extract_mod_information(uint8_t const *ptr_in, uint64_t file_size)
constexpr uint32_t scenario_file_version
size_t sizeof_mod_path(native_string const &path_in)
uint8_t const * memcpy_deserialize(uint8_t const *ptr_in, T &obj)
save_type
Definition: constants.hpp:565
@ normal
Definition: constants.hpp:566
uint8_t * write_scenario_header(uint8_t *ptr_in, scenario_header const &header_in)
void write_scenario_file(sys::state &state, native_string_view name, uint32_t count)
uint8_t const * deserialize(uint8_t const *ptr_in, std::vector< T > &vec)
uint8_t * write_save_section(uint8_t *ptr_in, sys::state &state)
scenario_size sizeof_scenario_section(sys::state &state)
uint8_t const * read_scenario_header(uint8_t const *ptr_in, scenario_header &header_out)
void read_mod_path(uint8_t const *ptr_in, uint8_t const *lim, native_string &path_out)
uint8_t * write_save_header(uint8_t *ptr_in, save_header const &header_in)
uint8_t * memcpy_serialize(uint8_t *ptr_in, T const &obj)
bool try_read_scenario_file(sys::state &state, native_string_view name)
size_t serialize_size(std::vector< T > const &vec)
uint8_t * serialize(uint8_t *ptr_in, std::vector< T > const &vec)
uint8_t const * read_save_section(uint8_t const *ptr_in, uint8_t const *section_end, sys::state &state)
size_t sizeof_save_header(save_header const &header_in)
uint8_t * write_compressed_section(uint8_t *ptr_out, uint8_t const *ptr_in, uint32_t uncompressed_size)
bool try_read_save_file(sys::state &state, native_string_view name)
void write_save_file(sys::state &state, save_type type, std::string const &name)
uint8_t const * load_mod_path(uint8_t const *ptr_in, sys::state &state)
size_t sizeof_scenario_header(scenario_header const &header_in)
constexpr uint32_t save_file_version
uint8_t const * read_scenario_section(uint8_t const *ptr_in, uint8_t const *section_end, sys::state &state)
bool try_read_scenario_and_save_file(sys::state &state, native_string_view name)
std::string_view native_string_view
std::string native_string
uint uint32_t
ulong uint64_t
uchar uint8_t
native_string mod_path
dcon::national_identity_id tag
dcon::government_type_id cgov
checksum_key checksum