Skip to content

Commit

Permalink
Made the launcher launch (#9)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
OneAvargeCoder193 authored Jun 16, 2024
1 parent 5cac5b3 commit baf8481
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.zig-cache/
zig-cache/
zig-out/
logs/
settings.json
gui_layout.json

.cubyz/
32 changes: 32 additions & 0 deletions src/archive.zig
Original file line number Diff line number Diff line change
@@ -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(), .{});
}
111 changes: 111 additions & 0 deletions src/gui/windows/main.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");

const main = @import("root");
const Vec2f = main.vec.Vec2f;
Expand All @@ -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},
};
Expand All @@ -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);
Expand Down
28 changes: 28 additions & 0 deletions src/http.zig
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit baf8481

Please sign in to comment.