lovely-packager 0.1.0

A LÖVE >= 11 distribution toolchain for web, desktop, and Steam builds.
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
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
use crate::fsutil;
use crate::{LovelyError, Result};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

pub const CONFIG_FILE: &str = "lovely.toml";

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
    pub game: GameConfig,
    pub paths: PathConfig,
    pub targets: TargetsConfig,
    pub itch: ItchConfig,
    pub steam: SteamConfig,
    pub compatibility: CompatibilityConfig,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GameConfig {
    pub id: String,
    pub name: String,
    pub version: String,
    pub author: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PathConfig {
    pub source: PathBuf,
    pub output: PathBuf,
    pub icon: Option<PathBuf>,
    pub includes: Vec<String>,
    pub excludes: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TargetsConfig {
    pub web: WebTargetConfig,
    pub windows: DesktopTargetConfig,
    pub macos: DesktopTargetConfig,
    pub linux: DesktopTargetConfig,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WebTargetConfig {
    pub enabled: bool,
    pub variant: String,
    pub html_template: Option<PathBuf>,
    pub html_assets: Vec<PathBuf>,
    pub runtime_path: Option<PathBuf>,
    pub memory_bytes: u64,
    pub arguments: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DesktopTargetConfig {
    pub enabled: bool,
    pub runtime_archive: Option<PathBuf>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ItchConfig {
    pub project: Option<String>,
    pub prerelease_channel: String,
    pub release_channel: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SteamConfig {
    pub app_id: Option<String>,
    pub windows_depot_id: Option<String>,
    pub macos_depot_id: Option<String>,
    pub linux_depot_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompatibilityConfig {
    pub allow_warnings: Vec<String>,
}

impl Config {
    pub fn default_for_dir(dir: &Path) -> Self {
        let id = dir
            .file_name()
            .map(|name| slugify(&name.to_string_lossy()))
            .filter(|name| !name.is_empty())
            .unwrap_or_else(|| "my-love-game".to_string());

        let name = id
            .split('-')
            .map(|part| {
                let mut chars = part.chars();
                match chars.next() {
                    Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
                    None => String::new(),
                }
            })
            .collect::<Vec<_>>()
            .join(" ");

        Self {
            game: GameConfig {
                id,
                name,
                version: "0.1.0".to_string(),
                author: "Unknown".to_string(),
            },
            paths: PathConfig {
                source: PathBuf::from("."),
                output: PathBuf::from("dist"),
                icon: Some(PathBuf::from("assets/icon.png")),
                includes: vec!["**/*".to_string()],
                excludes: vec!["node_modules/**".to_string()],
            },
            targets: TargetsConfig::default(),
            itch: ItchConfig {
                project: None,
                prerelease_channel: "web-prerelease".to_string(),
                release_channel: "web".to_string(),
            },
            steam: SteamConfig {
                app_id: None,
                windows_depot_id: None,
                macos_depot_id: None,
                linux_depot_id: None,
            },
            compatibility: CompatibilityConfig {
                allow_warnings: Vec::new(),
            },
        }
    }

    pub fn load_from(path: &Path) -> Result<Self> {
        let text = fsutil::read_to_string(path)?;
        Self::parse(&text)
    }

    pub fn parse(text: &str) -> Result<Self> {
        let mut table: BTreeMap<String, BTreeMap<String, String>> = BTreeMap::new();
        let mut section = String::new();

        for (index, raw_line) in text.lines().enumerate() {
            let line = strip_comment(raw_line).trim();
            if line.is_empty() {
                continue;
            }
            if line.starts_with('[') && line.ends_with(']') {
                section = line[1..line.len() - 1].trim().to_string();
                continue;
            }

            let Some((key, value)) = line.split_once('=') else {
                return Err(LovelyError::Config(format!(
                    "line {} is not a key/value pair",
                    index + 1
                )));
            };
            table
                .entry(section.clone())
                .or_default()
                .insert(key.trim().to_string(), value.trim().to_string());
        }

        let mut config = Self::default_for_dir(Path::new("."));
        if let Some(game) = table.get("game") {
            config.game.id = get_string(game, "id").unwrap_or(config.game.id);
            config.game.name = get_string(game, "name").unwrap_or(config.game.name);
            config.game.version = get_string(game, "version").unwrap_or(config.game.version);
            config.game.author = get_string(game, "author").unwrap_or(config.game.author);
        }
        if let Some(paths) = table.get("paths") {
            if let Some(source) = get_string(paths, "source") {
                config.paths.source = PathBuf::from(source);
            }
            if let Some(output) = get_string(paths, "output") {
                config.paths.output = PathBuf::from(output);
            }
            config.paths.icon = get_optional_path(paths, "icon", config.paths.icon);
            if let Some(includes) = get_string_array(paths, "includes")? {
                config.paths.includes = includes;
            }
            if let Some(excludes) = get_string_array(paths, "excludes")? {
                config.paths.excludes = excludes;
            }
        }

        apply_web(&mut config, table.get("targets.web"))?;
        apply_desktop(&mut config.targets.windows, table.get("targets.windows"));
        apply_desktop(&mut config.targets.macos, table.get("targets.macos"));
        apply_desktop(&mut config.targets.linux, table.get("targets.linux"));

        if let Some(itch) = table.get("itch") {
            config.itch.project = get_optional_string(itch, "project", config.itch.project);
            config.itch.prerelease_channel =
                get_string(itch, "prerelease_channel").unwrap_or(config.itch.prerelease_channel);
            config.itch.release_channel =
                get_string(itch, "release_channel").unwrap_or(config.itch.release_channel);
        }
        if let Some(steam) = table.get("steam") {
            config.steam.app_id = get_optional_string(steam, "app_id", config.steam.app_id);
            config.steam.windows_depot_id =
                get_optional_string(steam, "windows_depot_id", config.steam.windows_depot_id);
            config.steam.macos_depot_id =
                get_optional_string(steam, "macos_depot_id", config.steam.macos_depot_id);
            config.steam.linux_depot_id =
                get_optional_string(steam, "linux_depot_id", config.steam.linux_depot_id);
        }
        if let Some(compatibility) = table.get("compatibility") {
            if let Some(warnings) = get_string_array(compatibility, "allow_warnings")? {
                config.compatibility.allow_warnings = warnings;
            }
        }

        config.validate()?;
        Ok(config)
    }

    pub fn validate(&self) -> Result<()> {
        if self.game.id.trim().is_empty() {
            return Err(LovelyError::Config("game.id must not be empty".to_string()));
        }
        if self.game.name.trim().is_empty() {
            return Err(LovelyError::Config(
                "game.name must not be empty".to_string(),
            ));
        }
        if !matches!(
            self.targets.web.variant.as_str(),
            "web-compat" | "web-threaded"
        ) {
            return Err(LovelyError::Config(
                "targets.web.variant must be web-compat or web-threaded".to_string(),
            ));
        }
        Ok(())
    }

    pub fn to_toml(&self) -> String {
        format!(
            r#"[game]
id = "{id}"
name = "{name}"
version = "{version}"
author = "{author}"

[paths]
source = "{source}"
output = "{output}"
icon = {icon}
includes = {includes}
excludes = {excludes}

[targets.web]
enabled = true
variant = "web-compat"
memory_bytes = 67108864
html_template = ""
html_assets = {html_assets}
runtime_path = {runtime_path}
arguments = {arguments}

[targets.windows]
enabled = true
runtime_archive = ""

[targets.macos]
enabled = true
runtime_archive = ""

[targets.linux]
enabled = true
runtime_archive = ""

[itch]
project = ""
prerelease_channel = "web-prerelease"
release_channel = "web"

[steam]
app_id = ""
windows_depot_id = ""
macos_depot_id = ""
linux_depot_id = ""

[compatibility]
allow_warnings = []
"#,
            id = escape(&self.game.id),
            name = escape(&self.game.name),
            version = escape(&self.game.version),
            author = escape(&self.game.author),
            source = escape(&fsutil::normalize_slashes(&self.paths.source)),
            output = escape(&fsutil::normalize_slashes(&self.paths.output)),
            icon = self
                .paths
                .icon
                .as_ref()
                .map(|path| format!("\"{}\"", escape(&fsutil::normalize_slashes(path))))
                .unwrap_or_else(|| "\"\"".to_string()),
            runtime_path = self
                .targets
                .web
                .runtime_path
                .as_ref()
                .map(|path| format!("\"{}\"", escape(&fsutil::normalize_slashes(path))))
                .unwrap_or_else(|| "\"\"".to_string()),
            html_assets = format_path_array(&self.targets.web.html_assets),
            includes = format_string_array(&self.paths.includes),
            excludes = format_string_array(&self.paths.excludes),
            arguments = format_string_array(&self.targets.web.arguments)
        )
    }
}

impl Default for TargetsConfig {
    fn default() -> Self {
        Self {
            web: WebTargetConfig {
                enabled: true,
                variant: "web-compat".to_string(),
                html_template: None,
                html_assets: Vec::new(),
                runtime_path: None,
                memory_bytes: 67_108_864,
                arguments: Vec::new(),
            },
            windows: DesktopTargetConfig {
                enabled: true,
                runtime_archive: None,
            },
            macos: DesktopTargetConfig {
                enabled: true,
                runtime_archive: None,
            },
            linux: DesktopTargetConfig {
                enabled: true,
                runtime_archive: None,
            },
        }
    }
}

fn apply_web(config: &mut Config, values: Option<&BTreeMap<String, String>>) -> Result<()> {
    let Some(values) = values else {
        return Ok(());
    };
    config.targets.web.enabled = get_bool(values, "enabled").unwrap_or(config.targets.web.enabled);
    config.targets.web.variant =
        get_string(values, "variant").unwrap_or(config.targets.web.variant.clone());
    config.targets.web.html_template = get_optional_path(
        values,
        "html_template",
        config.targets.web.html_template.clone(),
    );
    if let Some(assets) = get_string_array(values, "html_assets")? {
        config.targets.web.html_assets = assets.into_iter().map(PathBuf::from).collect();
    }
    config.targets.web.runtime_path = get_optional_path(
        values,
        "runtime_path",
        config.targets.web.runtime_path.clone(),
    );
    if let Some(memory) = values.get("memory_bytes") {
        config.targets.web.memory_bytes = parse_integer(memory, "targets.web.memory_bytes")?;
    }
    if let Some(arguments) = get_string_array(values, "arguments")? {
        config.targets.web.arguments = arguments;
    }
    Ok(())
}

fn apply_desktop(config: &mut DesktopTargetConfig, values: Option<&BTreeMap<String, String>>) {
    let Some(values) = values else {
        return;
    };
    config.enabled = get_bool(values, "enabled").unwrap_or(config.enabled);
    config.runtime_archive =
        get_optional_path(values, "runtime_archive", config.runtime_archive.clone());
}

fn get_string(values: &BTreeMap<String, String>, key: &str) -> Option<String> {
    let value = values.get(key)?;
    parse_string(value).filter(|value| !value.is_empty())
}

fn get_optional_string(
    values: &BTreeMap<String, String>,
    key: &str,
    previous: Option<String>,
) -> Option<String> {
    match values.get(key).and_then(|value| parse_string(value)) {
        Some(value) if value.is_empty() => None,
        Some(value) => Some(value),
        None => previous,
    }
}

fn get_optional_path(
    values: &BTreeMap<String, String>,
    key: &str,
    previous: Option<PathBuf>,
) -> Option<PathBuf> {
    match values.get(key).and_then(|value| parse_string(value)) {
        Some(value) if value.is_empty() => None,
        Some(value) => Some(PathBuf::from(value)),
        None => previous,
    }
}

fn get_bool(values: &BTreeMap<String, String>, key: &str) -> Option<bool> {
    match values.get(key)?.as_str() {
        "true" => Some(true),
        "false" => Some(false),
        _ => None,
    }
}

fn get_string_array(values: &BTreeMap<String, String>, key: &str) -> Result<Option<Vec<String>>> {
    let Some(value) = values.get(key) else {
        return Ok(None);
    };
    parse_string_array(value).map(Some)
}

fn parse_string(value: &str) -> Option<String> {
    let value = value.trim();
    if value.starts_with('"') && value.ends_with('"') && value.len() >= 2 {
        Some(value[1..value.len() - 1].replace("\\\"", "\""))
    } else {
        None
    }
}

fn parse_integer(value: &str, field: &str) -> Result<u64> {
    value
        .trim()
        .parse::<u64>()
        .map_err(|_| LovelyError::Config(format!("{field} must be an integer")))
}

fn parse_string_array(value: &str) -> Result<Vec<String>> {
    let value = value.trim();
    if !(value.starts_with('[') && value.ends_with(']')) {
        return Err(LovelyError::Config(
            "expected an array of strings".to_string(),
        ));
    }
    let inner = value[1..value.len() - 1].trim();
    if inner.is_empty() {
        return Ok(Vec::new());
    }

    inner
        .split(',')
        .map(|part| {
            parse_string(part.trim()).ok_or_else(|| {
                LovelyError::Config(format!("array item {part:?} is not a quoted string"))
            })
        })
        .collect()
}

fn strip_comment(line: &str) -> &str {
    let mut in_string = false;
    for (index, ch) in line.char_indices() {
        match ch {
            '"' => in_string = !in_string,
            '#' if !in_string => return &line[..index],
            _ => {}
        }
    }
    line
}

fn slugify(input: &str) -> String {
    let mut slug = String::new();
    let mut last_dash = false;
    for ch in input.chars().flat_map(char::to_lowercase) {
        if ch.is_ascii_alphanumeric() {
            slug.push(ch);
            last_dash = false;
        } else if !last_dash {
            slug.push('-');
            last_dash = true;
        }
    }
    slug.trim_matches('-').to_string()
}

fn escape(input: &str) -> String {
    input.replace('\\', "\\\\").replace('"', "\\\"")
}

fn format_string_array(values: &[String]) -> String {
    let values = values
        .iter()
        .map(|value| format!("\"{}\"", escape(value)))
        .collect::<Vec<_>>()
        .join(", ");
    format!("[{values}]")
}

fn format_path_array(values: &[PathBuf]) -> String {
    let values = values
        .iter()
        .map(|value| format!("\"{}\"", escape(&fsutil::normalize_slashes(value))))
        .collect::<Vec<_>>()
        .join(", ");
    format!("[{values}]")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_minimal_config() {
        let config = Config::parse(
            r#"[game]
id = "sailman"
name = "Sailman"
version = "1.2.3"
author = "Team"

[targets.web]
variant = "web-threaded"
runtime_path = "runtimes/web"
html_assets = ["src/templates/logo.png"]
arguments = ["--demo-capture"]

[compatibility]
allow_warnings = ["web.native"]
"#,
        )
        .unwrap();

        assert_eq!(config.game.id, "sailman");
        assert_eq!(config.targets.web.variant, "web-threaded");
        assert_eq!(
            config.targets.web.runtime_path,
            Some(PathBuf::from("runtimes/web"))
        );
        assert_eq!(
            config.targets.web.html_assets,
            vec![PathBuf::from("src/templates/logo.png")]
        );
        assert_eq!(config.targets.web.arguments, vec!["--demo-capture"]);
        assert_eq!(config.compatibility.allow_warnings, vec!["web.native"]);
    }

    #[test]
    fn parses_path_excludes() {
        let config = Config::parse(
            r#"[paths]
includes = ["main.lua", "src/**"]
excludes = ["src/dev/**"]
"#,
        )
        .unwrap();

        assert_eq!(config.paths.includes, vec!["main.lua", "src/**"]);
        assert_eq!(config.paths.excludes, vec!["src/dev/**"]);
    }
}