-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
348 lines (317 loc) · 11.1 KB
/
build.rs
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
use std::collections::HashMap;
use std::env;
use std::fmt::{Debug, Formatter, Pointer};
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use std::str::Split;
trait FromValueSplit {
fn from_value_split(id: u16, value_split: &Vec<&str>) -> Self;
}
enum ObjectInformationExtra {
None,
Treasure(Vec<u16>),
}
impl Debug for ObjectInformationExtra {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ObjectInformationExtra::None => write!(f, "ObjectInformationExtra::None"),
ObjectInformationExtra::Treasure(treasure) => {
write!(f, "ObjectInformationExtra::Treasure(&{:?})", treasure)
}
}
}
}
#[derive(Debug)]
struct ObjectInformation {
pub name: String,
pub price: u32,
pub edibility: i16,
pub type_and_category: String,
pub display_name: String,
pub description: String,
pub extra: ObjectInformationExtra,
}
impl FromValueSplit for ObjectInformation {
fn from_value_split(id: u16, value_split: &Vec<&str>) -> Self {
let extra: ObjectInformationExtra = match id {
535u16 | 536u16 | 537u16 | 749u16 | 275u16 => ObjectInformationExtra::Treasure(
value_split[6usize]
.split(" ")
.map(|treasure: &str| treasure.parse::<u16>().unwrap())
.collect(),
),
_ => ObjectInformationExtra::None,
};
Self {
name: value_split[0usize].to_string(),
price: value_split[1usize].parse::<u32>().unwrap(),
edibility: value_split[2usize].parse::<i16>().unwrap(),
type_and_category: value_split[3usize].to_string(),
display_name: value_split[4usize].to_string(),
description: value_split[5usize].to_string(),
extra,
}
}
}
#[derive(Debug)]
struct BigCraftablesInformation {
pub name: String,
pub price: u32,
pub edibility: i16,
pub type_and_category: String,
pub description: String,
pub can_be_set_outdoors: bool,
pub can_be_set_indoors: bool,
pub fragility: u8,
pub display_name: String,
}
impl FromValueSplit for BigCraftablesInformation {
fn from_value_split(id: u16, value_split: &Vec<&str>) -> Self {
Self {
name: value_split[0usize].to_string(),
price: value_split[1usize].parse::<u32>().unwrap(),
edibility: value_split[2usize].parse::<i16>().unwrap(),
type_and_category: value_split[3usize].to_string(),
description: value_split[4usize].to_string(),
can_be_set_outdoors: value_split[5usize].parse::<bool>().unwrap(),
can_be_set_indoors: value_split[6usize].parse::<bool>().unwrap(),
fragility: value_split[7usize].parse::<u8>().unwrap(),
display_name: value_split[8usize].to_string(),
}
}
}
#[derive(Debug)]
struct Furniture {
pub name: String,
pub type_: String,
pub source_rectangle_width: u8,
pub source_rectangle_height: u8,
pub bounding_box_width: u8,
pub bounding_box_height: u8,
pub rotations: u8,
pub price: u32,
}
impl FromValueSplit for Furniture {
// Integer types (Furniture.getTypeNumberFromName):
// 0 = chair
// 1 = bench
// 2 = couch
// 3 = armchair
// 4 = dresser
// 5 = long table
// 6 = painting
// 7 = lamp
// 8 = decor
// 9 = [default]
// 10 = bookcase
// 11 = table
// 12 = rug
// 13 = window
// 14 = fireplace
// 15 = bed...
// 16 = torch
// 17 = sconce
fn from_value_split(id: u16, value_split: &Vec<&str>) -> Self {
let type_: &str = value_split[1usize];
let (source_rectangle_width, source_rectangle_height): (u8, u8) = match value_split[2usize]
{
"-1" => {
// Furniture.getDefaultSourceRectForType
match type_ {
"chair" | "decor" | "window" | "torch" | "sconce" => (1u8, 2u8),
"bench" | "armchair" | "dresser" | "painting" => (2u8, 2u8),
"couch" | "rug" => (3u8, 2u8),
"long table" => (5u8, 3u8),
"lamp" => (1u8, 3u8),
"bookcase" | "table" => (2u8, 3u8),
"fireplace" => (2u8, 5u8),
_ => panic!("{}", type_),
}
}
source_rectangle => {
let mut source_rectangle_split: Split<&str> = source_rectangle.split(" ");
(
source_rectangle_split
.next()
.unwrap()
.parse::<u8>()
.unwrap(),
source_rectangle_split
.next()
.unwrap()
.parse::<u8>()
.unwrap(),
)
}
};
let (bounding_box_width, bounding_box_height): (u8, u8) = match value_split[2usize] {
"-1" => {
// Furniture.getDefaultBoundingBoxForType
match type_ {
"chair" | "lamp" | "decor" | "torch" => (1u8, 1u8),
"bench" | "armchair" | "dresser" | "bookcase" | "fireplace" => (2u8, 1u8),
"couch" => (3u8, 1u8),
"long table" => (5u8, 2u8),
"painting" | "table" => (2u8, 2u8),
"rug" => (3u8, 2u8),
"window" | "sconce" => (1u8, 2u8),
_ => panic!("{}", type_),
}
}
bounding_box => {
let mut bounding_box_split: Split<&str> = bounding_box.split(" ");
(
bounding_box_split.next().unwrap().parse::<u8>().unwrap(),
bounding_box_split.next().unwrap().parse::<u8>().unwrap(),
)
}
};
Self {
name: value_split[0usize].to_string(),
type_: type_.to_string(),
source_rectangle_width,
source_rectangle_height,
bounding_box_width,
bounding_box_height,
rotations: value_split[4usize].parse::<u8>().unwrap(),
price: value_split[5usize].parse::<u32>().unwrap(),
}
}
}
#[derive(Debug)]
struct ClothingInformation {
pub name: String,
pub display_name: String,
pub description: String,
pub male_index: u16,
pub female_index: u16,
pub price: u32,
pub rgb: (u8, u8, u8),
pub dyeable: bool,
pub type_: String,
}
impl FromValueSplit for ClothingInformation {
fn from_value_split(id: u16, value_split: &Vec<&str>) -> Self {
let male_index: u16 = value_split[3usize].parse::<u16>().unwrap();
let female_index: u16 = match value_split[4usize] {
"-1" => male_index,
_ => value_split[4usize].parse::<u16>().unwrap(),
};
let mut rgb_split: Split<&str> = value_split[6usize].split(" ");
Self {
name: value_split[0usize].to_string(),
display_name: value_split[1usize].to_string(),
description: value_split[2usize].to_string(),
male_index,
female_index,
price: value_split[5usize].parse::<u32>().unwrap(),
rgb: (
rgb_split.next().unwrap().parse::<u8>().unwrap(),
rgb_split.next().unwrap().parse::<u8>().unwrap(),
rgb_split.next().unwrap().parse::<u8>().unwrap(),
),
dyeable: value_split[7usize].parse::<bool>().unwrap(),
type_: value_split[8usize].to_string(),
}
}
}
#[derive(Debug)]
struct Hats {
pub name: String,
pub description: String,
pub hair_draw_type: u8,
pub ignore_hairstyle_offset: bool,
pub is_prismatic: bool,
}
impl FromValueSplit for Hats {
fn from_value_split(id: u16, value_split: &Vec<&str>) -> Self {
let hair_draw_type: u8 = match value_split[2usize] {
"true" => 0u8,
"false" => 1u8,
"hide" => 2u8,
_ => panic!(),
};
let mut is_prismatic = false;
if value_split.len() > 4 {
for special_tag in value_split[4usize].split(" ") {
match special_tag {
"Prismatic" => {
is_prismatic = true;
}
_ => {}
}
}
}
Self {
name: value_split[0usize].to_string(),
description: value_split[1usize].to_string(),
hair_draw_type,
ignore_hairstyle_offset: value_split[3usize].parse::<bool>().unwrap(),
is_prismatic,
}
}
}
fn load<T: Debug + FromValueSplit>(
out_file: &mut BufWriter<File>,
path: &Path,
constant_name: &str,
struct_name: &str,
) {
let file: File = File::open(path).unwrap();
let json: serde_json::Value = serde_json::from_reader(file).unwrap();
let mut map: HashMap<u16, T> = HashMap::new();
for (key, value) in json.get("content").unwrap().as_object().unwrap() {
let id: u16 = match key.parse::<u16>() {
Ok(key) => key,
Err(_) => u16::MAX - (-key.parse::<i16>().unwrap() as u16), // This is very much a hack, but changing everything to i16 would require major changes.
};
let value_split: Vec<&str> = value.as_str().unwrap().split("/").collect();
map.insert(
// Clothing has some negative keys.
id,
T::from_value_split(id, &value_split),
);
}
let mut builder: phf_codegen::Map<u16> = phf_codegen::Map::new();
for (key, value) in &map {
builder.entry(*key, format!("{:?}", value).as_str());
}
writeln!(
out_file,
"pub static {}: phf::Map<u16, {}> = {};",
constant_name,
struct_name,
builder.build(),
)
.unwrap();
}
fn main() {
let out_path: PathBuf = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
let mut out_file: BufWriter<File> = BufWriter::new(File::create(&out_path).unwrap());
let object_information_path: &Path = Path::new("assets/ObjectInformation.json");
load::<ObjectInformation>(
&mut out_file,
object_information_path,
"OBJECT_INFORMATION",
"ObjectInformation",
);
let big_craftables_information_path: &Path = Path::new("assets/BigCraftablesInformation.json");
load::<BigCraftablesInformation>(
&mut out_file,
big_craftables_information_path,
"BIG_CRAFTABLES_INFORMATION",
"BigCraftablesInformation",
);
let furniture_path: &Path = Path::new("assets/Furniture.json");
load::<Furniture>(&mut out_file, furniture_path, "FURNITURE", "Furniture");
let clothing_information_path: &Path = Path::new("assets/ClothingInformation.json");
load::<ClothingInformation>(
&mut out_file,
clothing_information_path,
"CLOTHING_INFORMATION",
"ClothingInformation",
);
let hats_path: &Path = Path::new("assets/hats.json");
load::<Hats>(&mut out_file, hats_path, "HATS", "Hats");
}