Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OneAvargeCoder193 committed Jun 16, 2024
1 parent 388f8fd commit adbd598
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 96 deletions.
88 changes: 44 additions & 44 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
.{
.name = "Cubyzig",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.local = .{
.path = "../Cubyz-libs/zig-out", // Local alternative to the cubyz_deps below
.lazy = true,
},
.cubyz_deps_headers = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_headers.tar.gz",
.hash = "1220f83952166e8649c95fdbc3585c7fc8cc2d0de878c26feec2d4b0277cc0c86106",
.lazy = true,
},
.cubyz_deps_aarch64_macos = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_aarch64-macos-none.tar.gz",
.hash = "12204ba799cc74baeec8284b8a22cd0b597b58d5fa32c6eac999635fdc1834c950fc",
.lazy = true,
},
.cubyz_deps_aarch64_linux = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_aarch64-linux-musl.tar.gz",
.hash = "1220c94c49f56b65a1646d683f868d167db5178c71d90c60759d788d6afa0e484c64",
.lazy = true,
},
.cubyz_deps_aarch64_windows = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_aarch64-windows-gnu.tar.gz",
.hash = "1220c27cb071f1340304f5befa8e273bc995c31f833f26c148ca17714d648ae1365a",
.lazy = true,
},
.cubyz_deps_x86_64_macos = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_x86_64-macos-none.tar.gz",
.hash = "1220cd7c8fd567132ba3fa64bf60765434a6dbab9c33444ca37e6ab53563cabfbf94",
.lazy = true,
},
.cubyz_deps_x86_64_linux = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_x86_64-linux-musl.tar.gz",
.hash = "1220234373ac197e8dad7a2d1beeb81619a6112e44dba6b2672af9a38fb01a16eef8",
.lazy = true,
},
.cubyz_deps_x86_64_windows = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_x86_64-windows-gnu.tar.gz",
.hash = "1220a2630c691c0ce40afd51022941b0544c9bacc9190667f701036e0eb3ec6333d5",
.lazy = true,
},
},
.name = "Cubyzig",
.version = "0.0.0",
.paths = .{""},
.dependencies = .{
.local = .{
.path = "../Cubyz-libs/zig-out", // Local alternative to the cubyz_deps below
.lazy = true,
},
.cubyz_deps_headers = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_headers.tar.gz",
.hash = "1220f83952166e8649c95fdbc3585c7fc8cc2d0de878c26feec2d4b0277cc0c86106",
.lazy = true,
},
.cubyz_deps_aarch64_macos = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_aarch64-macos-none.tar.gz",
.hash = "12204ba799cc74baeec8284b8a22cd0b597b58d5fa32c6eac999635fdc1834c950fc",
.lazy = true,
},
.cubyz_deps_aarch64_linux = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_aarch64-linux-musl.tar.gz",
.hash = "1220c94c49f56b65a1646d683f868d167db5178c71d90c60759d788d6afa0e484c64",
.lazy = true,
},
.cubyz_deps_aarch64_windows = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_aarch64-windows-gnu.tar.gz",
.hash = "1220c27cb071f1340304f5befa8e273bc995c31f833f26c148ca17714d648ae1365a",
.lazy = true,
},
.cubyz_deps_x86_64_macos = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_x86_64-macos-none.tar.gz",
.hash = "1220cd7c8fd567132ba3fa64bf60765434a6dbab9c33444ca37e6ab53563cabfbf94",
.lazy = true,
},
.cubyz_deps_x86_64_linux = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_x86_64-linux-musl.tar.gz",
.hash = "1220234373ac197e8dad7a2d1beeb81619a6112e44dba6b2672af9a38fb01a16eef8",
.lazy = true,
},
.cubyz_deps_x86_64_windows = .{
.url = "https://github.com/PixelGuys/Cubyz-Libs/releases/download/3/cubyz_deps_x86_64-windows-gnu.tar.gz",
.hash = "1220a2630c691c0ce40afd51022941b0544c9bacc9190667f701036e0eb3ec6333d5",
.lazy = true,
},
},
}
76 changes: 28 additions & 48 deletions src/gui/windows/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 @@ -23,126 +24,105 @@ fn exitGame(_:usize) void {
main.Window.c.glfwSetWindowShouldClose(main.Window.window,main.Window.c.GLFW_TRUE);
}

fn deleteDirectoryRecursive(fs: std.fs.Dir, allocator: std.mem.Allocator, path: []const u8) anyerror!void {
var dir = try fs.openDir(path, .{.iterate = true});
defer dir.close();

var it = dir.iterate();

while (try it.next()) |entry| {
if (entry.kind == .file) {
try dir.deleteFile(entry.name);
} else if (entry.kind == .directory) {
try deleteDirectoryRecursive(dir, allocator, entry.name);
}
}

try fs.deleteDir(path);
}

pub fn play(_:usize) void {
deleteDirectoryRecursive(std.fs.cwd(), std.heap.page_allocator, ".cubyz/Cubyz-master") catch |err| {
std.debug.print("{}\n", .{err});
std.fs.cwd().deleteTree(".cubyz/Cubyz-master") catch |err| {
std.log.err("Error deleting .cubyz/Cubyz-master: {}\n", .{err});
return;
};

http.fetchAndSave(std.heap.page_allocator, "https://www.github.com/PixelGuys/Cubyz/archive/master.zip", ".cubyz/master.zip") catch |err| {
std.debug.print("{}\n", .{err});
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.debug.print("{}\n", .{err});
std.log.err("Error extracting .cubyz/master.zip into .cubyz: {}\n", .{err});
return;
};

std.fs.cwd().deleteFile(".cubyz/master.zip") catch {};

const dir = std.fs.cwd().openDir(".cubyz/Cubyz-master", .{}) catch |err| {
std.debug.print("{}\n", .{err});
var dir = files.openDir(".cubyz/Cubyz-master") catch |err| {
std.log.err("Error opening .cubyz/Cubyz-master: {}\n", .{err});
return;
};
defer dir.close();

const file = dir.openFile(".zig-version", .{}) catch |err| {
std.debug.print("{}\n", .{err});
const ver = dir.read(main.stackAllocator, ".zig-version") catch |err| {
std.log.err("Error reading the zig version: {}\n", .{err});
return;
};
defer file.close();

const ver = file.readToEndAlloc(std.heap.page_allocator, 32) catch |err| {
std.debug.print("{}\n", .{err});
return;
};
defer std.heap.page_allocator.free(ver);
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), builtin.target.osArchName(), ver}) catch |err| {
std.debug.print("{}\n", .{err});
std.log.err("Error getting the http url: {}\n", .{err});
return;
};

_ = std.fs.cwd().makeDir(".cubyz/Cubyz-master/compiler") catch |err| {
std.debug.print("{}\n", .{err});
std.log.err("Error creating .cubyz/Cubyz-master/compiler: {}\n", .{err});
return;
};

http.fetchAndSave(std.heap.page_allocator, zig_http, ".cubyz/Cubyz-master/compiler/archive.zip") catch |err| {
std.debug.print("{}\n", .{err});
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.debug.print("{}\n", .{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), builtin.target.osArchName(), ver}) catch |err| {
std.debug.print("{}\n", .{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, std.heap.page_allocator);
var proc = std.process.Child.init(&argv, main.stackAllocator.allocator);
proc.cwd = ".cubyz\\Cubyz-master";
_ = proc.spawnAndWait() catch |err| {
std.debug.print("{}\n", .{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), builtin.target.osArchName(), ver}) catch |err| {
std.debug.print("{}\n", .{err});
std.log.err("Error getting the http url: {}\n", .{err});
return;
};

_ = std.fs.cwd().makeDir(".cubyz/Cubyz-master/compiler") catch |err| {
std.debug.print("{}\n", .{err});
std.log.err("Error creating .cubyz/Cubyz-master/compiler: {}\n", .{err});
return;
};

http.fetchAndSave(std.heap.page_allocator, zig_http, ".cubyz/Cubyz-master/compiler/archive.tar.xz") catch |err| {
std.debug.print("{}\n", .{err});
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.debug.print("{}\n", .{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), builtin.target.osArchName(), ver}) catch |err| {
std.debug.print("{}\n", .{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, std.heap.page_allocator);
var proc = std.process.Child.init(&argv, main.stackAllocator.allocator);
proc.cwd = ".cubyz/Cubyz-master";
_ = proc.spawnAndWait() catch |err| {
std.debug.print("{}\n", .{err});
std.log.err("Error running cubyz: {}\n", .{err});
return;
};
}
Expand Down
10 changes: 6 additions & 4 deletions src/http.zig
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
const std = @import("std");
const utils = @import("utils.zig");
const http = std.http;
const fs = std.fs;

pub fn fetchAndSave(allocator: std.mem.Allocator, url: []const u8, out: []const u8) !void {
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};
var client = http.Client{.allocator = allocator.allocator};
defer client.deinit();

var response: std.ArrayList(u8) = std.ArrayList(u8).init(allocator);
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 = 268435456, // 256 Megabytes should be enough for everyone
.max_append_size = std.math.maxInt(usize),
.location = .{
.url = url
}
Expand Down

0 comments on commit adbd598

Please sign in to comment.