poe_data_tools 1.0.0

A library for working with Path of Exile game data
Documentation
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
use anyhow::{Result, anyhow};
use regex::Regex;
use winnow::{
    Parser,
    ascii::{dec_int, dec_uint, float, space0, space1},
    binary::length_repeat,
    combinator::{
        alt, dispatch, eof, opt, preceded as P, repeat, repeat_till, separated_pair,
        terminated as T,
    },
    token::{literal, rest},
};

use super::types::*;
use crate::file_parsers::shared::{
    lift::{SliceParser, lift},
    remove_trailing,
    winnow::{
        WinnowParser, filename, nullable_uint, quoted, quoted_comma_separated, quoted_str,
        repeat_array, unquoted, unquoted_str,
    },
};

pub fn render_passes<'a>() -> impl SliceParser<'a, &'a str, Effect> {
    let single_line = winnow::trace!(
        "single_line",
        (
            literal("RenderPasses"),
            space1,
            literal("{"),
            space0,
            literal("}"),
        )
            .value(Effect::RenderPasses(RenderPasses { passes: vec![] }))
    );

    let multiline = winnow::trace!(
        "multi_line",
        P(
            lift(literal("RenderPasses").map(String::from)),
            (
                lift(literal("{")),
                repeat_till(.., lift(rest), lift(literal("}"))),
            )
                .map(|(b0, (middle, b1)): (&str, (Vec<&str>, &str))| {
                    [[b0].as_slice(), &middle, &[b1]].concat().concat()
                }),
        )
        .try_map(|payload| {
            let payload = remove_trailing(&payload);
            serde_json::from_str(&payload)
        })
        .map(Effect::RenderPasses)
    );

    winnow::trace!(
        "RenderPasses",
        alt((
            lift(single_line), //
            multiline,
        ))
    )
}

pub fn attached_object<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "AttachedObject",
        (
            quoted_str, //
            // NOTE: Edge case: missing space between these two strings
            P(space0, quoted('"').and_then(filename("ao"))),
            opt(P(space1, literal("\"ignore_errors\""))).map(|x| x.is_some()),
        )
            .map(
                |(attachment_point, ao_file, ignore_errors)| Effect::AttachedObject {
                    attachment_point,
                    ao_file,
                    ignore_errors,
                },
            )
    )
}

pub fn particle_effect<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "ParticleEffect",
        (
            quoted_str,
            P(space1, quoted('"').and_then(filename("pet"))),
            opt(P(
                (space1, literal("\"limit\"")), //
                repeat_array(P(space1, dec_uint)),
            )),
            opt(P(space1, literal("\"ignore_errors\""))).map(|x| x.is_some()),
        )
            .map(
                |(glob, pet_file, limit, ignore_errors)| Effect::ParticleEffect {
                    glob,
                    pet_file,
                    limit,
                    ignore_errors,
                },
            )
    )
}

pub fn attached_object_ex<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "AttachedObjectEx",
        (
            opt(T(quoted_str, space1)),
            length_repeat(
                dec_uint::<_, u32, _>,
                P(
                    space1,
                    quoted('"').and_then(alt((
                        filename("ao"), //
                        filename("fmt"),
                    ))),
                ),
            ),
            repeat_array(P(space1, dec_uint)),
            repeat_array(P(space1, float)),
            (
                opt(P(space1, dec_int)), //
                opt(P(space1, dec_int)),
            )
                .map(|(r0, r1)| [r0, r1]),
            opt(P(space1, literal("\"include_aux\""))).map(|x| x.is_some()),
            opt(P(space1, literal("\"multi_attach\""))).map(|x| x.is_some()),
            opt(P(space1, literal("\"ignore_errors\""))).map(|x| x.is_some()),
        )
            .map(
                |(
                    attachment_point,
                    files,
                    uints1,
                    floats,
                    rotations,
                    include_aux,
                    ignore_errors,
                    multi_attach,
                )| {
                    Effect::AttachedObjectEx {
                        attachment_point,
                        files,
                        uints1,
                        floats,
                        rotations,
                        include_aux,
                        ignore_errors,
                        multi_attach,
                    }
                },
            )
    )
}

pub fn attached_object_bone_index<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "AttachedObject",
        (
            dec_uint, //
            P(space1, quoted('"').and_then(filename("ao"))),
        )
            .map(|(bone_index, ao_file)| Effect::AttachedObjectBoneIndex {
                bone_index,
                ao_file,
            })
    )
}

/// to/from_bone to/from_bone_index
/// or
/// to/from_bone to/from_child_bone to/from_child_bone_index
pub fn bone<'a, const FROM: bool>() -> impl WinnowParser<&'a str, Bone> {
    let (bone, bone_group_index, child_bone, child_bone_group_index) = if FROM {
        (
            "from_bone",
            "from_bone_group_index",
            "from_child_bone",
            "from_child_bone_group_index",
        )
    } else {
        (
            "to_bone",
            "to_bone_group_index",
            "to_child_bone",
            "to_child_bone_group_index",
        )
    };

    let parent_parser = (
        P(
            (literal::<_, &str, _>(bone), space1),
            quoted_comma_separated(),
        ),
        P((space1, literal(bone_group_index), space1), nullable_uint()),
    )
        .map(|(names, index)| Bone::Parent { names, index });

    let child_parser = (
        P((literal(bone), space1), quoted_comma_separated()),
        (
            P(
                (space1, literal(child_bone), space1),
                quoted_comma_separated(),
            ),
            P(
                (space1, literal(child_bone_group_index), space1),
                nullable_uint(),
            ), //
        ),
    )
        .map(|(parent_bones, (children, index))| {
            assert_eq!(parent_bones.len(), 1);
            let parent = parent_bones.into_iter().next().unwrap();

            Bone::Child {
                parent,
                children,
                index,
            }
        });

    winnow::trace!(
        "bone",
        alt((
            parent_parser, //
            child_parser,
        ))
    )
}

pub fn child_attached_object<'a>() -> impl WinnowParser<&'a str, Effect> {
    (
        quoted('"').and_then(filename("ao")), //
        P(space1, bone::<true>()),
        opt(P(space1, bone::<false>())),
    )
        .map(
            |(ao_file, from_bones, to_bones)| Effect::ChildAttachedObject {
                ao_file,
                from_bones,
                to_bones,
            },
        )
}

pub fn trail_effect<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "TrailEffect",
        (
            quoted_str,
            P(space1, quoted('"').and_then(filename("trl"))),
            opt(P(
                (space1, literal("\"limit\"")), //
                repeat_array(P(space1, dec_uint)),
            )),
            opt(P(space1, literal("\"ignore_errors\""))).map(|x| x.is_some()),
        )
            .map(
                |(glob, trl_file, limit, ignore_errors)| Effect::TrailEffect {
                    glob,
                    trl_file,
                    limit,
                    ignore_errors,
                },
            )
    )
}

pub fn hide_first_pass_after_delay<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "HideFirstPassAfterDelay",
        float.map(|delay| Effect::HideFirstPassAfterDelay { delay })
    )
}

pub fn hide_first_pass_after_delay_for_duration<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "HideFirstPassAfterDelayForDuration",
        separated_pair(float, space1, float).map(|(delay, duration)| {
            Effect::HideFirstPassAfterDelayForDuration { delay, duration }
        })
    )
}

pub fn hide_first_pass_using_epk_parameter<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "HideFirstPassUsingEPKParameter",
        (
            unquoted_str, //
            P(space1, float),
            P(space1, float),
        )
            .map(
                |(parameter, float1, float2)| Effect::HideFirstPassUsingEPKParameter {
                    parameter,
                    float1,
                    float2,
                },
            )
    )
}

pub fn hide_first_pass_using_timeline_parameter<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "HideFirstPassUsingTimelineParameter",
        (
            unquoted_str, //
            P(space1, float),
            P(space1, float),
        )
            .map(|(parameter, float1, float2)| {
                Effect::HideFirstPassUsingTimelineParameter {
                    parameter,
                    float1,
                    float2,
                }
            },)
    )
}

pub fn hide_first_pass_using_dynamic_parameter<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "HideFirstPassUsingDynamicParameter",
        (
            unquoted_str, //
            P(space1, float),
            P(space1, float),
        )
            .map(|(parameter, float1, float2)| {
                Effect::HideFirstPassUsingDynamicParameter {
                    parameter,
                    float1,
                    float2,
                }
            },)
    )
}

pub fn play_misc_effect_pack_after_delay<'a>() -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "PlayMiscEffectPackAfterDelay",
        separated_pair(quoted_str, space1, float)
            .map(|(effect, delay)| Effect::PlayMiscEffectPackAfterDelay { effect, delay })
    )
}

pub fn other_effect<'a>(name: &str) -> impl WinnowParser<&'a str, Effect> {
    winnow::trace!(
        "other_effect",
        rest.map(|rest: &str| Effect::Other {
            name: name.to_string(),
            rest: rest.to_string(),
        })
    )
}

pub fn effect<'a>() -> impl SliceParser<'a, &'a str, Effect> {
    winnow::trace!(
        "effect",
        alt((
            render_passes(),
            lift(dispatch! {
                T(unquoted(), opt(space1));
                "AttachedObject" => attached_object(),
                "AttachedObjectEx" => attached_object_ex(),
                "AttachedObjectBoneIndex" => attached_object_bone_index(),
                "ChildAttachedObject" => child_attached_object(),
                "ParticleEffect" => particle_effect(),
                "TrailEffect" => trail_effect(),
                "ParentOnlyEffects" => eof.map(|_| Effect::ParentOnlyEffects),
                "ApplyToAllPasses" => eof.map(|_| Effect::ApplyToAllPasses),
                "PlayMiscEffectPackOnEnd" => quoted_str.map(|effect| Effect::PlayMiscEffectPackOnEnd { effect }),
                "PlayMiscEffectPackOnBegin" => quoted_str.map(|effect| Effect::PlayMiscEffectPackOnBegin { effect }),
                "PlayMiscEffectPackAfterDelay" => play_misc_effect_pack_after_delay(),
                "HideFirstPassAfterDelay" => hide_first_pass_after_delay(),
                "HideFirstPassAfterDelayForDuration" => hide_first_pass_after_delay_for_duration(),
                "HideFirstPassUsingEPKParameter" => hide_first_pass_using_epk_parameter(),
                "HideFirstPassUsingTimelineParameter" => hide_first_pass_using_timeline_parameter(),
                "HideFirstPassUsingDynamicParameter" => hide_first_pass_using_dynamic_parameter(),
                name => other_effect(name),
            }),
        ))
    )
}

pub fn parse_epk_str(contents: &str) -> Result<EPKFile> {
    let contents = contents.trim();

    // NOTE: Edge case: sometimes the final quoted string ends in "" when AttachedObject is the
    // last item
    let contents = if contents.ends_with("\"\"") {
        &contents[..contents.len() - 1]
    } else {
        contents
    };

    // NOTE: Edge case: sometimes there's an errouneous line break when AttachedObject is the last
    // item
    let re = Regex::new("\"<root>\"\\s*\n").unwrap();
    let contents = re.replace_all(contents, "\"<root>\" ");

    let lines = contents
        .lines()
        .map(|l| l.trim_end())
        .filter(|l| !l.is_empty() && !l.starts_with("//"))
        .collect::<Vec<_>>();

    let mut parser = repeat(.., effect());

    let epk_file = parser
        .parse(lines.as_slice())
        .map_err(|e| anyhow!("Failed to parse file: {e:?}"))?;

    Ok(epk_file)
}