ltk_modpkg 0.4.0

League Toolkit mod package (.modpkg) reader/writer and utilities
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
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
//! High-level utilities for packing mod projects to `.modpkg` format.
//!
//! This module requires the `project` feature to be enabled.
//!
//! # Example
//!
//! ```ignore
//! use ltk_modpkg::project::{pack_from_project, PackOptions};
//! use camino::Utf8Path;
//!
//! let project_root = Utf8Path::new("my-mod");
//! let output_path = Utf8Path::new("build/my-mod_1.0.0.modpkg");
//!
//! pack_from_project(project_root, output_path, &mod_project)?;
//! ```

use crate::{
    builder::{ModpkgBuilder, ModpkgBuilderError, ModpkgChunkBuilder, ModpkgLayerBuilder},
    metadata::CURRENT_SCHEMA_VERSION,
    utils::hash_layer_name,
    ModpkgCompression, ModpkgLayerMetadata, ModpkgMetadata,
};
use camino::{Utf8Path, Utf8PathBuf};
use image::ImageFormat;
use ltk_mod_project::{ModProject, ModProjectAuthor, ModProjectLayer, ModProjectLicense};
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::{self, BufReader, BufWriter, Cursor, Read, Write};

/// Error type for project packing operations.
#[derive(Debug, thiserror::Error)]
pub enum PackError {
    #[error("IO error: {0}")]
    Io(#[from] io::Error),

    #[error("Builder error: {0}")]
    Builder(#[from] ModpkgBuilderError),

    #[error("Config file not found in project directory: {0}")]
    ConfigNotFound(Utf8PathBuf),

    #[error("Layer directory missing: {layer} at {path}")]
    LayerDirMissing { layer: String, path: Utf8PathBuf },

    #[error("Invalid layer name: {0}")]
    InvalidLayerName(String),

    #[error("Base layer must have priority 0, got: {0}")]
    InvalidBaseLayerPriority(i32),

    #[error("Failed to process thumbnail: {0}")]
    ThumbnailError(String),

    #[error("Invalid version format: {0}")]
    InvalidVersion(String),

    #[error("Glob pattern error: {0}")]
    GlobError(#[from] glob::PatternError),

    #[error("Invalid UTF-8 path: {0}")]
    InvalidUtf8Path(String),
}

/// Options for packing a mod project.
#[derive(Debug, Clone, Default)]
pub struct PackOptions {
    /// Custom output file name (without path). If None, uses `{name}_{version}.modpkg`.
    pub file_name: Option<String>,
}

/// Result of a successful pack operation.
#[derive(Debug)]
pub struct PackResult {
    /// The path to the created `.modpkg` file.
    pub output_path: Utf8PathBuf,
}

/// Create a standard modpkg file name from a mod project.
///
/// If `custom_name` is provided, it will be used (with `.modpkg` extension added if missing).
/// Otherwise, generates `{name}_{version}.modpkg`.
pub fn create_file_name(mod_project: &ModProject, custom_name: Option<String>) -> String {
    match custom_name {
        Some(name) => {
            if name.ends_with(".modpkg") {
                name
            } else {
                format!("{}.modpkg", name)
            }
        }
        None => {
            format!("{}_{}.modpkg", mod_project.name, mod_project.version)
        }
    }
}

/// Pack a mod project to a `.modpkg` file.
///
/// # Arguments
///
/// * `project_root` - Path to the mod project directory (containing `mod.config.json` or `mod.config.toml`)
/// * `output_path` - Path where the `.modpkg` file will be written
/// * `mod_project` - The parsed mod project configuration
///
/// # Returns
///
/// Returns `PackResult` on success with the output path.
pub fn pack_from_project(
    project_root: &Utf8Path,
    output_path: &Utf8Path,
    mod_project: &ModProject,
) -> Result<PackResult, PackError> {
    let content_dir = project_root.join("content");

    // Validate layers
    validate_layers(mod_project, project_root)?;

    // Build the modpkg
    let mut builder = ModpkgBuilder::default().with_layer(ModpkgLayerBuilder::base());
    let mut chunk_filepaths: HashMap<(u64, u64), Utf8PathBuf> = HashMap::new();

    // Add metadata
    builder = build_metadata(builder, mod_project)?;

    // Add layers and their content
    builder = build_layers(builder, &content_dir, mod_project, &mut chunk_filepaths)?;

    // Add meta chunks (README, thumbnail)
    builder = add_meta_chunks(builder, project_root, mod_project)?;

    // Create output directory if needed
    if let Some(parent) = output_path.parent() {
        if !parent.exists() {
            fs::create_dir_all(parent)?;
        }
    }

    // Write the modpkg file
    let mut writer = BufWriter::new(File::create(output_path)?);

    builder
        .build_to_writer(&mut writer, |chunk_builder, cursor| {
            write_chunk_payload(chunk_builder, cursor, &chunk_filepaths)
                .map_err(ModpkgBuilderError::from)
        })
        .map_err(PackError::Builder)?;

    Ok(PackResult {
        output_path: output_path.to_owned(),
    })
}

/// Validate that all layers exist and have valid configuration.
fn validate_layers(mod_project: &ModProject, project_root: &Utf8Path) -> Result<(), PackError> {
    for layer in &mod_project.layers {
        // Validate layer name is a valid slug
        if !is_valid_slug(&layer.name) {
            return Err(PackError::InvalidLayerName(layer.name.clone()));
        }

        // Base layer must have priority 0
        if layer.name == "base" && layer.priority != 0 {
            return Err(PackError::InvalidBaseLayerPriority(layer.priority));
        }

        // Check layer directory exists
        let layer_dir = project_root.join("content").join(&layer.name);
        if !layer_dir.exists() {
            return Err(PackError::LayerDirMissing {
                layer: layer.name.clone(),
                path: layer_dir,
            });
        }
    }

    Ok(())
}

/// Check if a string is a valid slug (lowercase alphanumeric with hyphens).
fn is_valid_slug(s: &str) -> bool {
    !s.is_empty()
        && s.chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
        && !s.starts_with('-')
        && !s.ends_with('-')
}

fn build_metadata(
    builder: ModpkgBuilder,
    mod_project: &ModProject,
) -> Result<ModpkgBuilder, PackError> {
    let version = semver::Version::parse(&mod_project.version)
        .map_err(|e| PackError::InvalidVersion(e.to_string()))?;

    let builder = builder
        .with_metadata(ModpkgMetadata {
            schema_version: CURRENT_SCHEMA_VERSION,
            name: mod_project.name.clone(),
            display_name: mod_project.display_name.clone(),
            description: Some(mod_project.description.clone()),
            version,
            distributor: None,
            authors: mod_project
                .authors
                .iter()
                .map(convert_project_author)
                .collect(),
            license: convert_project_license(mod_project.license.as_ref()),
            tags: mod_project.tags.iter().map(|t| t.to_string()).collect(),
            champions: mod_project.champions.clone(),
            maps: mod_project.maps.iter().map(|m| m.to_string()).collect(),
            layers: build_metadata_layers(mod_project),
        })
        .map_err(PackError::Builder)?;

    Ok(builder)
}

/// Convert a project author to modpkg metadata author format.
fn convert_project_author(author: &ModProjectAuthor) -> crate::ModpkgAuthor {
    match author {
        ModProjectAuthor::Name(name) => crate::ModpkgAuthor {
            name: name.clone(),
            role: None,
        },
        ModProjectAuthor::Role { name, role } => crate::ModpkgAuthor {
            name: name.clone(),
            role: Some(role.clone()),
        },
    }
}

/// Convert a project license to modpkg metadata license format.
fn convert_project_license(license: Option<&ModProjectLicense>) -> crate::ModpkgLicense {
    match license {
        None => crate::ModpkgLicense::None,
        Some(ModProjectLicense::Spdx(id)) => crate::ModpkgLicense::Spdx {
            spdx_id: id.clone(),
        },
        Some(ModProjectLicense::Custom { name, url }) => crate::ModpkgLicense::Custom {
            name: name.clone(),
            url: url.clone(),
        },
    }
}

/// Build the per-layer metadata section.
fn build_metadata_layers(mod_project: &ModProject) -> Vec<ModpkgLayerMetadata> {
    let mut layers = Vec::new();

    // Base layer: always present, even if omitted from config
    let base_from_config = mod_project.layers.iter().find(|l| l.name == "base");
    let base_display_name = base_from_config.and_then(|l| l.display_name.clone());
    let base_description = base_from_config
        .and_then(|l| l.description.clone())
        .or_else(|| Some("Base layer of the mod".to_string()));
    let base_string_overrides = base_from_config
        .map(|l| l.string_overrides.clone())
        .unwrap_or_default();

    layers.push(ModpkgLayerMetadata {
        name: "base".to_string(),
        display_name: base_display_name,
        priority: 0,
        description: base_description,
        string_overrides: base_string_overrides,
    });

    // Non-base layers
    for layer in mod_project.layers.iter().filter(|l| l.name != "base") {
        layers.push(ModpkgLayerMetadata {
            name: layer.name.clone(),
            display_name: layer.display_name.clone(),
            priority: layer.priority,
            description: layer.description.clone(),
            string_overrides: layer.string_overrides.clone(),
        });
    }

    layers
}

fn build_layers(
    mut builder: ModpkgBuilder,
    content_dir: &Utf8Path,
    mod_project: &ModProject,
    chunk_filepaths: &mut HashMap<(u64, u64), Utf8PathBuf>,
) -> Result<ModpkgBuilder, PackError> {
    // Process base layer
    builder = build_layer_from_dir(
        builder,
        content_dir,
        &ModProjectLayer::base(),
        chunk_filepaths,
    )?;

    // Process additional layers
    for layer in &mod_project.layers {
        if layer.name == "base" {
            continue;
        }

        builder =
            builder.with_layer(ModpkgLayerBuilder::new(&layer.name).with_priority(layer.priority));
        builder = build_layer_from_dir(builder, content_dir, layer, chunk_filepaths)?;
    }

    Ok(builder)
}

fn build_layer_from_dir(
    mut builder: ModpkgBuilder,
    content_dir: &Utf8Path,
    layer: &ModProjectLayer,
    chunk_filepaths: &mut HashMap<(u64, u64), Utf8PathBuf>,
) -> Result<ModpkgBuilder, PackError> {
    let layer_dir = content_dir.join(&layer.name);
    let pattern = layer_dir.join("**/*");

    for entry in glob::glob(pattern.as_str())?
        .filter_map(Result::ok)
        .filter(|e| e.is_file())
    {
        let entry = Utf8PathBuf::from_path_buf(entry)
            .map_err(|p| PackError::InvalidUtf8Path(p.display().to_string()))?;

        let layer_hash = hash_layer_name(&layer.name);
        let (new_builder, path_hash) = build_chunk_from_file(builder, layer, &entry, &layer_dir)?;

        chunk_filepaths
            .entry((path_hash, layer_hash))
            .or_insert(entry);

        builder = new_builder;
    }

    Ok(builder)
}

fn build_chunk_from_file(
    builder: ModpkgBuilder,
    layer: &ModProjectLayer,
    file_path: &Utf8Path,
    layer_dir: &Utf8Path,
) -> Result<(ModpkgBuilder, u64), PackError> {
    let relative_path = file_path
        .strip_prefix(layer_dir)
        .map_err(|e| PackError::Io(io::Error::other(e.to_string())))?;

    let relative_str = relative_path.as_str();

    // Detect WAD association from the directory structure.
    // If the first path component is a WAD name (e.g., "Aatrox.wad.client"),
    // strip it from the chunk path and track it via wad_index instead.
    let (chunk_path, wad_name) = match relative_str.split_once('/') {
        Some((first, rest)) if first.to_ascii_lowercase().ends_with(".wad.client") => {
            (rest, Some(first))
        }
        _ => (relative_str, None),
    };

    let compression = compression_for_extension(file_path.extension());

    let mut chunk_builder = ModpkgChunkBuilder::new()
        .with_path(chunk_path)
        .map_err(PackError::Builder)?
        .with_compression(compression)
        .with_layer(&layer.name);

    if let Some(wad) = wad_name {
        chunk_builder = chunk_builder.with_wad(wad);
    }

    let path_hash = chunk_builder.path_hash();
    Ok((builder.with_chunk(chunk_builder), path_hash))
}

/// Determine the best compression strategy based on file extension.
///
/// Pre-compressed formats (textures, audio) gain little from zstd and
/// waste CPU time at both compression and decompression.
fn compression_for_extension(ext: Option<&str>) -> ModpkgCompression {
    match ext.map(|e| e.to_ascii_lowercase()).as_deref() {
        // Pre-compressed textures and images
        Some("dds" | "tex" | "webp" | "png" | "jpg" | "jpeg") => ModpkgCompression::None,
        // Pre-compressed audio
        Some("bnk" | "wpk" | "wem" | "ogg") => ModpkgCompression::None,
        // Everything else benefits from compression
        _ => ModpkgCompression::Zstd,
    }
}

fn add_meta_chunks(
    mut builder: ModpkgBuilder,
    project_root: &Utf8Path,
    mod_project: &ModProject,
) -> Result<ModpkgBuilder, PackError> {
    // README.md as meta chunk (optional)
    let readme_path = project_root.join("README.md");
    if readme_path.exists() {
        let readme_content = fs::read_to_string(&readme_path)?;
        builder = builder
            .with_readme(&readme_content)
            .map_err(PackError::Builder)?;
    }

    // Thumbnail as meta chunk (optional)
    let thumbnail_path = mod_project
        .thumbnail
        .as_ref()
        .map(|p| project_root.join(p))
        .unwrap_or_else(|| project_root.join("thumbnail.webp"));

    if thumbnail_path.exists() {
        let thumbnail_data = load_thumbnail(&thumbnail_path)?;
        builder = builder
            .with_thumbnail(thumbnail_data)
            .map_err(PackError::Builder)?;
    }

    Ok(builder)
}

/// Maximum thumbnail file size: 5MB
pub const MAX_THUMBNAIL_SIZE: u64 = 5 * 1024 * 1024;

/// Load and convert a thumbnail image to WebP format.
///
/// Supports all common image formats (PNG, JPEG, GIF, BMP, TIFF, ICO, WebP).
/// Animated GIFs are converted to animated WebP.
/// Validates file size (max 5MB).
///
/// # Arguments
///
/// * `path` - Path to the thumbnail image file
///
/// # Returns
///
/// WebP-encoded image data as bytes
pub fn load_thumbnail(path: &Utf8Path) -> Result<Vec<u8>, PackError> {
    let metadata = fs::metadata(path).map_err(PackError::Io)?;
    if metadata.len() > MAX_THUMBNAIL_SIZE {
        return Err(PackError::ThumbnailError(format!(
            "Thumbnail file size ({} bytes) exceeds maximum allowed size ({} bytes / 5MB)",
            metadata.len(),
            MAX_THUMBNAIL_SIZE
        )));
    }

    let extension = path
        .extension()
        .map(|ext| ext.to_lowercase())
        .unwrap_or_default();

    if extension == "webp" {
        let data = fs::read(path).map_err(PackError::Io)?;
        // Validate WebP magic bytes
        if data.len() >= 12 && &data[0..4] == b"RIFF" && &data[8..12] == b"WEBP" {
            return Ok(data);
        }
        return Err(PackError::ThumbnailError(
            "Invalid WebP file format".to_string(),
        ));
    }

    if extension == "gif" {
        return convert_gif_to_webp(path);
    }

    let img = image::open(path)
        .map_err(|e| PackError::ThumbnailError(format!("Failed to open image: {}", e)))?;

    let mut buffer = Cursor::new(Vec::new());
    img.write_to(&mut buffer, ImageFormat::WebP)
        .map_err(|e| PackError::ThumbnailError(format!("Failed to convert to WebP: {}", e)))?;

    Ok(buffer.into_inner())
}

fn convert_gif_to_webp(path: &Utf8Path) -> Result<Vec<u8>, PackError> {
    let file = File::open(path).map_err(PackError::Io)?;
    let reader = BufReader::new(file);
    let decoder = image::codecs::gif::GifDecoder::new(reader)
        .map_err(|e| PackError::ThumbnailError(format!("Failed to decode GIF: {}", e)))?;

    let frames: Vec<_> = image::AnimationDecoder::into_frames(decoder)
        .collect::<Result<Vec<_>, _>>()
        .map_err(|e| PackError::ThumbnailError(format!("Failed to read GIF frames: {}", e)))?;

    if frames.is_empty() {
        return Err(PackError::ThumbnailError("GIF has no frames".to_string()));
    }

    if frames.len() == 1 {
        let frame = &frames[0];
        let img = frame.buffer();
        let mut buffer = Cursor::new(Vec::new());
        img.write_to(&mut buffer, ImageFormat::WebP).map_err(|e| {
            PackError::ThumbnailError(format!("Failed to convert GIF to WebP: {}", e))
        })?;
        return Ok(buffer.into_inner());
    }

    encode_animated_webp(&frames)
}

fn encode_animated_webp(frames: &[image::Frame]) -> Result<Vec<u8>, PackError> {
    use webp_animation::prelude::*;

    if frames.is_empty() {
        return Err(PackError::ThumbnailError("No frames to encode".to_string()));
    }

    let first_frame = frames[0].buffer();
    let (width, height) = first_frame.dimensions();

    let mut encoder = Encoder::new((width, height)).map_err(|e| {
        PackError::ThumbnailError(format!("Failed to create WebP encoder: {:?}", e))
    })?;

    let mut timestamp_ms = 0i32;
    for frame in frames {
        let img_buffer = frame.buffer();
        let delay = frame.delay();
        let rgba_data = img_buffer.as_raw();

        encoder
            .add_frame(rgba_data, timestamp_ms)
            .map_err(|e| PackError::ThumbnailError(format!("Failed to add frame: {:?}", e)))?;

        let delay_ms = delay.numer_denom_ms();
        timestamp_ms += delay_ms.0 as i32;
    }

    let webp_data = encoder
        .finalize(timestamp_ms)
        .map_err(|e| PackError::ThumbnailError(format!("Failed to finalize animation: {:?}", e)))?;

    Ok(webp_data.to_vec())
}

fn write_chunk_payload(
    chunk_builder: &ModpkgChunkBuilder,
    cursor: &mut Cursor<Vec<u8>>,
    chunk_filepaths: &HashMap<(u64, u64), Utf8PathBuf>,
) -> io::Result<()> {
    // Content chunks - look up file path from the map
    let key = (
        chunk_builder.path_hash(),
        hash_layer_name(chunk_builder.layer()),
    );
    if let Some(file_path) = chunk_filepaths.get(&key) {
        let mut file = File::open(file_path)?;
        let mut buffer = Vec::new();
        file.read_to_end(&mut buffer)?;
        cursor.write_all(&buffer)?;
        return Ok(());
    }

    Err(io::Error::new(
        io::ErrorKind::NotFound,
        format!(
            "Missing file path for chunk: {} (layer: '{}')",
            chunk_builder.path,
            chunk_builder.layer()
        ),
    ))
}

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

    #[test]
    fn test_create_file_name() {
        let project = ModProject {
            name: "my-mod".to_string(),
            display_name: "My Mod".to_string(),
            version: "1.2.3".to_string(),
            description: String::new(),
            authors: vec![],
            license: None,
            tags: vec![],
            champions: vec![],
            maps: vec![],
            thumbnail: None,
            layers: vec![],
            transformers: vec![],
        };

        assert_eq!(create_file_name(&project, None), "my-mod_1.2.3.modpkg");
        assert_eq!(
            create_file_name(&project, Some("custom".to_string())),
            "custom.modpkg"
        );
        assert_eq!(
            create_file_name(&project, Some("custom.modpkg".to_string())),
            "custom.modpkg"
        );
    }

    #[test]
    fn test_is_valid_slug() {
        assert!(is_valid_slug("base"));
        assert!(is_valid_slug("my-layer"));
        assert!(is_valid_slug("layer123"));
        assert!(!is_valid_slug(""));
        assert!(!is_valid_slug("-invalid"));
        assert!(!is_valid_slug("invalid-"));
        assert!(!is_valid_slug("UPPERCASE"));
        assert!(!is_valid_slug("has spaces"));
    }
}