From 546d5bfef9189661f4927aeb50fb91a94922b169 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Fri, 1 Jul 2022 13:34:20 +0200 Subject: [PATCH] feat: build script now generates mmc-pack.json --- build.zig | 127 ++++++++++++++++++++++++++++++++++++++++++-------- mmc-pack.json | 51 -------------------- settings.zig | 3 ++ 3 files changed, 110 insertions(+), 71 deletions(-) delete mode 100644 mmc-pack.json diff --git a/build.zig b/build.zig index d5c2194..a4c3d80 100755 --- a/build.zig +++ b/build.zig @@ -38,6 +38,8 @@ pub fn main() !void { try archiveCreateDir(zip.?, entry.?, "minecraft/"); try archiveCreateDir(zip.?, entry.?, "minecraft/mods/"); + const writer = ArchiveWriter{ .context = zip.? }; + var overrides = try std.fs.cwd().openDir("overrides", .{ .iterate = true }); defer overrides.close(); var walker = try overrides.walk(std.heap.c_allocator); @@ -77,25 +79,13 @@ pub fn main() !void { } } - var mmc_pack_file = try std.fs.cwd().openFile("mmc-pack.json", .{}); - defer mmc_pack_file.close(); - try archiveFile( - zip.?, - entry.?, - &buf, - "mmc-pack.json", - mmc_pack_file, - ); + try installMmcPackJson(zip.?, entry.?); const instance_cfg_data = "InstanceType=OneSix"; c.archive_entry_set_pathname(entry, "instance.cfg"); c.archive_entry_set_size(entry, instance_cfg_data.len); try handleArchiveErr(c.archive_write_header(zip, entry), zip); - _ = c.archive_write_data( - zip, - instance_cfg_data, - instance_cfg_data.len, - ); + try writer.writeAll(instance_cfg_data); var mods_arena = std.heap.ArenaAllocator.init(std.heap.c_allocator); defer mods_arena.deinit(); @@ -115,6 +105,20 @@ pub fn main() !void { try handleArchiveErr(c.archive_write_close(zip), zip); } +const ArchiveWriter = std.io.Writer( + *c.archive, + error{ArchiveError}, + writeArchive, +); + +fn writeArchive(archive: *c.archive, bytes: []const u8) error{ArchiveError}!usize { + const result = c.archive_write_data(archive, bytes.ptr, bytes.len); + if (result < 0) { + try handleArchiveErr(result, archive); + } + return @intCast(usize, result); +} + fn archiveFile( archive: *c.archive, entry: *c.archive_entry, @@ -127,9 +131,10 @@ fn archiveFile( c.archive_entry_set_size(entry, @intCast(i64, (try file.stat()).size)); try handleArchiveErr(c.archive_write_header(archive, entry), archive); + const writer = ArchiveWriter{ .context = archive }; var read = try file.read(buf); while (read != 0) { - _ = c.archive_write_data(archive, buf.ptr, read); + try writer.writeAll(buf[0..read]); read = try file.read(buf); } } @@ -145,6 +150,91 @@ fn archiveCreateDir( try handleArchiveErr(c.archive_write_header(archive, entry), archive); } +fn installMmcPackJson(archive: *c.archive, entry: *c.archive_entry) !void { + const Requires = struct { + uid: []const u8, + equals: ?[]const u8 = null, + suggests: ?[]const u8 = null, + }; + + const Component = struct { + cachedName: []const u8, + cachedRequires: ?[]const Requires = null, + cachedVersion: []const u8, + cachedVolatile: ?bool = null, + dependencyOnly: ?bool = null, + important: ?bool = null, + uid: []const u8, + version: []const u8, + }; + + const data = .{ + .components = &[_]Component{ + .{ + .cachedName = "LWJGL 3", + .cachedVersion = "3.2.2", + .cachedVolatile = true, + .dependencyOnly = true, + .uid = "org.lwjgl3", + .version = "3.2.2", + }, + .{ + .cachedName = "Minecraft", + .cachedRequires = &.{ + .{ + .uid = "org.lwjgl3", + .suggests = "3.2.2", + }, + }, + .cachedVersion = settings.minecraft_version, + .important = true, + .uid = "net.minecraft", + .version = settings.minecraft_version, + }, + .{ + .cachedName = "Intermediary Mappings", + .cachedRequires = &.{ + .{ + .equals = settings.minecraft_version, + .uid = "net.minecraft", + }, + }, + .cachedVersion = settings.minecraft_version, + .cachedVolatile = true, + .dependencyOnly = true, + .uid = "net.fabricmc.intermediary", + .version = settings.minecraft_version, + }, + .{ + .cachedName = "Fabric Loader", + .cachedRequires = &.{ + .{ + .uid = "net.fabricmc.intermediary", + }, + }, + .cachedVersion = settings.fabric_loader_version, + .uid = "net.fabricmc.fabric-loader", + .version = settings.fabric_loader_version, + }, + }, + .formatVersion = 1, + }; + + const json = try std.json.stringifyAlloc( + std.heap.c_allocator, + data, + .{ .emit_null_optional_fields = false }, + ); + defer std.heap.c_allocator.free(json); + + entrySetFile(entry); + c.archive_entry_set_size(entry, @intCast(i64, json.len)); + c.archive_entry_set_pathname(entry, "mmc-pack.json"); + + try handleArchiveErr(c.archive_write_header(archive, entry), archive); + try (ArchiveWriter{ .context = archive }).writeAll(json); +} + fn readMods(list: *std.ArrayList([]u8), alloc: std.mem.Allocator) !void { var file = try std.fs.cwd().openFile("mods.txt", .{}); defer file.close(); @@ -213,6 +303,7 @@ fn downloadMods( try handleCurlErr(c.curl_easy_setopt(curl, c.CURLOPT_NOPROGRESS, @as(c_long, 0))); try handleCurlErr(c.curl_easy_setopt(curl, c.CURLOPT_FOLLOWLOCATION, @as(c_long, 1))); + const writer = ArchiveWriter{ .context = zip }; var mod_buf = std.ArrayList(u8).init(std.heap.c_allocator); defer mod_buf.deinit(); defer std.io.getStdOut().writeAll("\x1b[0\n") catch {}; @@ -261,11 +352,7 @@ fn downloadMods( c.archive_entry_set_pathname(entry, archive_path.ptr); c.archive_entry_set_size(entry, @intCast(i64, mod_buf.items.len)); try handleArchiveErr(c.archive_write_header(zip, entry), zip); - _ = c.archive_write_data( - zip, - mod_buf.items.ptr, - mod_buf.items.len, - ); + try writer.writeAll(mod_buf.items); std.io.getStdOut().writer().print( "\x1b[2K\r\x1b[34m{s}\n", .{filename.?}, diff --git a/mmc-pack.json b/mmc-pack.json deleted file mode 100644 index c0f4e74..0000000 --- a/mmc-pack.json +++ /dev/null @@ -1,51 +0,0 @@ -{ - "components": [ - { - "cachedName": "LWJGL 3", - "cachedVersion": "3.2.2", - "cachedVolatile": true, - "dependencyOnly": true, - "uid": "org.lwjgl3", - "version": "3.2.2" - }, - { - "cachedName": "Minecraft", - "cachedRequires": [ - { - "suggests": "3.2.2", - "uid": "org.lwjgl3" - } - ], - "cachedVersion": "1.18.2", - "important": true, - "uid": "net.minecraft", - "version": "1.18.2" - }, - { - "cachedName": "Intermediary Mappings", - "cachedRequires": [ - { - "equals": "1.18.2", - "uid": "net.minecraft" - } - ], - "cachedVersion": "1.18.2", - "cachedVolatile": true, - "dependencyOnly": true, - "uid": "net.fabricmc.intermediary", - "version": "1.18.2" - }, - { - "cachedName": "Fabric Loader", - "cachedRequires": [ - { - "uid": "net.fabricmc.intermediary" - } - ], - "cachedVersion": "0.14.8", - "uid": "net.fabricmc.fabric-loader", - "version": "0.14.8" - } - ], - "formatVersion": 1 -} diff --git a/settings.zig b/settings.zig index d6739af..89316b9 100644 --- a/settings.zig +++ b/settings.zig @@ -1,5 +1,8 @@ // Version number used for the archive name pub const version = "0.1.0"; pub const build_dir = "build"; +pub const minecraft_version = "1.18.2"; +pub const fabric_loader_version = "0.14.8"; // zip compression level. 9 is max. ask libarchive why this is a string. pub const compression_level = "9"; +