From baf8481add29d02ce6e6577c905cdeb1621a240a Mon Sep 17 00:00:00 2001 From: OneAvargeCoder193 <85588535+OneAvargeCoder193@users.noreply.github.com> Date: Sun, 16 Jun 2024 09:31:25 -0400 Subject: [PATCH] Made the launcher launch (#9) * Fix carridge return * Remove debug logging * Remove more debug logging * the launcher launches now :D * Add zig-cache to gitignore * Add .cubyz * Delete zig-cache directory * Made main not return !void * Replace four spaces with tabs * Made it work on platforms other than windows (hopefully) * Fix * Changes * Remove .gitignore * Replace proc.cwd with proc.cwd_dir on non-windows systems * Fixed proc.cwd_dir * Replace osArchName with @tagName * Added executable bit * Removed useless lines --- .gitignore | 3 +- src/archive.zig | 32 +++++++++++ src/gui/windows/main.zig | 111 +++++++++++++++++++++++++++++++++++++++ src/http.zig | 28 ++++++++++ src/main.zig | 2 + 5 files changed, 175 insertions(+), 1 deletion(-) create mode 100644 src/archive.zig create mode 100644 src/http.zig diff --git a/.gitignore b/.gitignore index fd19e8a..bb10cb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .zig-cache/ +zig-cache/ zig-out/ logs/ settings.json gui_layout.json - +.cubyz/ \ No newline at end of file diff --git a/src/archive.zig b/src/archive.zig new file mode 100644 index 0000000..d895f3d --- /dev/null +++ b/src/archive.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const fs = std.fs; +const zip = std.zip; +const tar = std.tar; +const compress = std.compress; + +pub fn decompress_zip(zip_path: []const u8, output_dir: []const u8) !void { + const zip_file = try fs.cwd().openFile(zip_path, .{}); + defer zip_file.close(); + + fs.cwd().makeDir(output_dir) catch {}; + + var output_dir_handle = try fs.cwd().openDir(output_dir, .{}); + defer output_dir_handle.close(); + + try zip.extract(output_dir_handle, zip_file.seekableStream(), .{}); +} + +pub fn decompress_tar_xz(zip_path: []const u8, output_dir: []const u8) !void { + const tar_file = try fs.cwd().openFile(zip_path, .{}); + defer tar_file.close(); + + fs.cwd().makeDir(output_dir) catch {}; + + var output_dir_handle = try fs.cwd().openDir(output_dir, .{}); + defer output_dir_handle.close(); + + var xz_decompress = try compress.xz.decompress(std.heap.page_allocator, tar_file.reader()); + defer xz_decompress.deinit(); + + try std.tar.pipeToFileSystem(output_dir_handle, xz_decompress.reader(), .{}); +} \ No newline at end of file diff --git a/src/gui/windows/main.zig b/src/gui/windows/main.zig index a772416..fa56275 100644 --- a/src/gui/windows/main.zig +++ b/src/gui/windows/main.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const main = @import("root"); const Vec2f = main.vec.Vec2f; @@ -9,6 +10,10 @@ const GuiWindow = gui.GuiWindow; const Button = @import("../components/Button.zig"); const VerticalList = @import("../components/VerticalList.zig"); +const http = main.http; +const archive = main.archive; +const files = main.files; + pub var window = GuiWindow { .contentSize = Vec2f{128, 256}, }; @@ -18,8 +23,114 @@ const padding: f32 = 8; fn exitGame(_:usize) void { main.Window.c.glfwSetWindowShouldClose(main.Window.window,main.Window.c.GLFW_TRUE); } + +pub fn play(_:usize) void { + std.fs.cwd().deleteTree(".cubyz/Cubyz-master") catch |err| { + std.log.err("Error deleting .cubyz/Cubyz-master: {}\n", .{err}); + return; + }; + + http.fetchAndSave(main.stackAllocator, "https://www.github.com/PixelGuys/Cubyz/archive/master.zip", ".cubyz/master.zip") catch |err| { + std.log.err("Error fetching https://www.github.com/PixelGuys/Cubyz/archive/master.zip into .cubyz/master.zip: {}\n", .{err}); + return; + }; + + archive.decompress_zip(".cubyz/master.zip", ".cubyz") catch |err| { + std.log.err("Error extracting .cubyz/master.zip into .cubyz: {}\n", .{err}); + return; + }; + + std.fs.cwd().deleteFile(".cubyz/master.zip") catch {}; + + var dir = files.openDir(".cubyz/Cubyz-master") catch |err| { + std.log.err("Error opening .cubyz/Cubyz-master: {}\n", .{err}); + return; + }; + defer dir.close(); + + const ver = dir.read(main.stackAllocator, ".zig-version") catch |err| { + std.log.err("Error reading the zig version: {}\n", .{err}); + return; + }; + + defer main.stackAllocator.free(ver); + + if (builtin.target.os.tag == .windows) { + var buf: [128]u8 = undefined; + const zig_http = std.fmt.bufPrint(&buf, "https://ziglang.org/builds/zig-{s}-{s}-{s}.zip", .{@tagName(builtin.target.os.tag), @tagName(builtin.target.cpu.arch), ver}) catch |err| { + std.log.err("Error getting the http url: {}\n", .{err}); + return; + }; + + _ = std.fs.cwd().makeDir(".cubyz/Cubyz-master/compiler") catch |err| { + std.log.err("Error creating .cubyz/Cubyz-master/compiler: {}\n", .{err}); + return; + }; + + http.fetchAndSave(main.stackAllocator, zig_http, ".cubyz/Cubyz-master/compiler/archive.zip") catch |err| { + std.log.err("Error fetching the compiler: {}\n", .{err}); + return; + }; + + archive.decompress_zip(".cubyz/Cubyz-master/compiler/archive.zip", ".cubyz/Cubyz-master/compiler") catch |err| { + std.log.err("Error extracting the compiler: {}\n", .{err}); + return; + }; + + var commandBuf: [1024]u8 = undefined; + const command = std.fmt.bufPrint(&commandBuf, ".\\compiler\\zig-{s}-{s}-{s}\\zig.exe", .{@tagName(builtin.target.os.tag), @tagName(builtin.target.cpu.arch), ver}) catch |err| { + std.log.err("Error creating the run command: {}\n", .{err}); + return; + }; + + const argv = [_][]const u8{command, "build", "run", "-Doptimize=ReleaseSafe"}; + var proc = std.process.Child.init(&argv, main.stackAllocator.allocator); + proc.cwd = ".cubyz\\Cubyz-master"; + _ = proc.spawnAndWait() catch |err| { + std.log.err("Error running cubyz: {}\n", .{err}); + return; + }; + } else { + var buf: [128]u8 = undefined; + const zig_http = std.fmt.bufPrint(&buf, "https://ziglang.org/builds/zig-{s}-{s}-{s}.tar.xz", .{@tagName(builtin.target.os.tag), @tagName(builtin.target.cpu.arch), ver}) catch |err| { + std.log.err("Error getting the http url: {}\n", .{err}); + return; + }; + + _ = std.fs.cwd().makeDir(".cubyz/Cubyz-master/compiler") catch |err| { + std.log.err("Error creating .cubyz/Cubyz-master/compiler: {}\n", .{err}); + return; + }; + + http.fetchAndSave(main.stackAllocator, zig_http, ".cubyz/Cubyz-master/compiler/archive.tar.xz") catch |err| { + std.log.err("Error fetching the compiler: {}\n", .{err}); + return; + }; + + archive.decompress_tar_xz(".cubyz/Cubyz-master/compiler/archive.tar.xz", ".cubyz/Cubyz-master/compiler") catch |err| { + std.log.err("Error extracting the compiler: {}\n", .{err}); + return; + }; + + var commandBuf: [1024]u8 = undefined; + const command = std.fmt.bufPrint(&commandBuf, "./compiler/zig-{s}-{s}-{s}/zig", .{@tagName(builtin.target.os.tag), @tagName(builtin.target.cpu.arch), ver}) catch |err| { + std.log.err("Error creating the run command: {}\n", .{err}); + return; + }; + + const argv = [_][]const u8{command, "build", "run", "-Doptimize=ReleaseSafe"}; + var proc = std.process.Child.init(&argv, main.stackAllocator.allocator); + proc.cwd_dir = dir.dir; + _ = proc.spawnAndWait() catch |err| { + std.log.err("Error running cubyz: {}\n", .{err}); + return; + }; + } +} + pub fn onOpen() void { const list = VerticalList.init(.{padding, 16 + padding}, 300, 16); + list.add(Button.initText(.{0, 0}, 128, "Play", .{.callback = &play,})); list.add(Button.initText(.{0, 0}, 128, "Settings", gui.openWindowCallback("settings"))); list.add(Button.initText(.{0, 0}, 128, "Touch Grass", .{.callback = &exitGame})); list.finish(.center); diff --git a/src/http.zig b/src/http.zig new file mode 100644 index 0000000..35c18c9 --- /dev/null +++ b/src/http.zig @@ -0,0 +1,28 @@ +const std = @import("std"); +const utils = @import("utils.zig"); +const http = std.http; +const fs = std.fs; + +pub fn fetchAndSave(allocator: utils.NeverFailingAllocator, url: []const u8, out: []const u8) !void { + var stream = fs.cwd().createFile(out, .{}) catch try fs.cwd().openFile(out, .{}); + defer stream.close(); + + var client = http.Client{.allocator = allocator.allocator}; + defer client.deinit(); + + var response: std.ArrayList(u8) = std.ArrayList(u8).init(allocator.allocator); + defer response.deinit(); + + const result = try client.fetch(.{ + .method = .GET, + .response_storage = .{ .dynamic = &response }, + .max_append_size = std.math.maxInt(usize), + .location = .{ + .url = url + } + }); + + _ = result; + + try stream.writeAll(response.items); +} \ No newline at end of file diff --git a/src/main.zig b/src/main.zig index 894c768..c08ba79 100644 --- a/src/main.zig +++ b/src/main.zig @@ -10,6 +10,8 @@ pub const renderer = @import("renderer.zig"); pub const settings = @import("settings.zig"); pub const utils = @import("utils.zig"); pub const vec = @import("vec.zig"); +pub const http = @import("http.zig"); +pub const archive = @import("archive.zig"); pub const Window = @import("graphics/Window.zig");