Skip to content

Commit 7657f63

Browse files
Create a launchConfig.zon that can be used to overwrite the global cubyz dir and save location
fixes PixelGuys#1836
1 parent f3cf190 commit 7657f63

File tree

7 files changed

+49
-11
lines changed

7 files changed

+49
-11
lines changed

‎.github/workflows/release.yml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ jobs:
2020
version: ${{ env.VERSION }}
2121

2222
- run: zig build -Dtarget=x86_64-linux-gnu -Doptimize=ReleaseFast -Drelease=true -Dcpu=baseline
23-
- run: tar -czf Linux-x86_64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz -C zig-out/bin Cubyz
23+
- run: tar -czf Linux-x86_64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz launchConfig.zon -C zig-out/bin Cubyz
2424

2525
- run: zig build -Dtarget=aarch64-linux-gnu -Doptimize=ReleaseFast -Drelease=true -Dcpu=baseline
26-
- run: tar -czf Linux-aarch64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz -C zig-out/bin Cubyz
26+
- run: tar -czf Linux-aarch64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz launchConfig.zon -C zig-out/bin Cubyz
2727

2828
- run: zig build -Dtarget=x86_64-windows-gnu -Doptimize=ReleaseFast -Drelease=true -Dcpu=baseline
29-
- run: tar -czf Windows-x86_64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz -C zig-out/bin Cubyz.exe
29+
- run: tar -czf Windows-x86_64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz launchConfig.zon -C zig-out/bin Cubyz.exe
3030

3131
- run: zig build -Dtarget=aarch64-windows-gnu -Doptimize=ReleaseFast -Drelease=true -Dcpu=baseline
32-
- run: tar -czf Windows-aarch64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz -C zig-out/bin Cubyz.exe
32+
- run: tar -czf Windows-aarch64.tar.gz --transform 's,^,Cubyz/,' assets/cubyz launchConfig.zon -C zig-out/bin Cubyz.exe
3333

3434
- name: Create release
3535
env:

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ zig-cache/
66
serverAssets/
77
gamecontrollerdb.txt
88
gamecontrollerdb.stamp
9+
launchConfig.zon
910

1011
test.png
1112

‎build.zig‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,18 @@ pub fn makeModFeaturesStep(step: *std.Build.Step, _: std.Build.Step.MakeOptions)
141141
try makeModFeature(step, "rotation");
142142
}
143143

144+
fn createLaunchConfig() !void {
145+
std.fs.cwd().access("launchConfig.zon", .{}) catch {
146+
try std.fs.cwd().writeFile(.{
147+
.data = ".{\n\t.cubyzDir = \"\",\n}\n",
148+
.sub_path = "launchConfig.zon",
149+
});
150+
};
151+
}
152+
144153
pub fn build(b: *std.Build) !void {
154+
try createLaunchConfig();
155+
145156
// Standard target options allows the person running `zig build` to choose
146157
// what target to build for. Here we do not override the defaults, which
147158
// means any target is allowed, and the default is native. Other options

‎src/files.zig‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,21 @@ pub fn cubyzDirStr() []const u8 {
5151
}
5252

5353
fn flawedInit() !void {
54+
if(main.settings.launchConfig.cubyzDir.len != 0) {
55+
cubyzDir_ = try std.fs.cwd().makeOpenPath(main.settings.launchConfig.cubyzDir, .{});
56+
cubyzDirStr_ = main.globalAllocator.dupe(u8, main.settings.launchConfig.cubyzDir);
57+
return;
58+
}
5459
const homePath = try std.process.getEnvVarOwned(main.stackAllocator.allocator, if(builtin.os.tag == .windows) "USERPROFILE" else "HOME");
5560
defer main.stackAllocator.free(homePath);
5661
var homeDir = try std.fs.openDirAbsolute(homePath, .{});
5762
defer homeDir.close();
5863
if(builtin.os.tag == .windows) {
5964
cubyzDir_ = try homeDir.makeOpenPath("Saved Games/Cubyz", .{});
60-
cubyzDirStr_ = std.mem.concat(main.stackAllocator.allocator, u8, &.{homePath, "/Saved Games/Cubyz"}) catch unreachable;
65+
cubyzDirStr_ = std.mem.concat(main.globalAllocator.allocator, u8, &.{homePath, "/Saved Games/Cubyz"}) catch unreachable;
6166
} else {
6267
cubyzDir_ = try homeDir.makeOpenPath(".cubyz", .{});
63-
cubyzDirStr_ = std.mem.concat(main.stackAllocator.allocator, u8, &.{homePath, "/.cubyz"}) catch unreachable;
68+
cubyzDirStr_ = std.mem.concat(main.globalAllocator.allocator, u8, &.{homePath, "/.cubyz"}) catch unreachable;
6469
}
6570
}
6671

@@ -75,7 +80,7 @@ pub fn deinit() void {
7580
cubyzDir_.?.close();
7681
}
7782
if(cubyzDirStr_.ptr != ".".ptr) {
78-
main.stackAllocator.free(cubyzDirStr_);
83+
main.globalAllocator.free(cubyzDirStr_);
7984
}
8085
}
8186

‎src/main.zig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ pub fn main() void { // MARK: main()
578578
gui.initWindowList();
579579
defer gui.deinitWindowList();
580580

581+
settings.launchConfig.init();
582+
defer settings.launchConfig.deinit();
583+
581584
files.init();
582585
defer files.deinit();
583586

‎src/renderer.zig‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,19 +538,19 @@ pub const MenuBackGround = struct {
538538
}
539539

540540
fn chooseBackgroundImagePath(allocator: main.heap.NeverFailingAllocator) ![]const u8 {
541+
var dir = try main.files.cubyzDir().openIterableDir("backgrounds");
542+
defer dir.close();
543+
541544
// Whenever the version changes copy over the new background image and display it.
542545
if(!std.mem.eql(u8, settings.lastVersionString, settings.version.version)) {
543546
const defaultImageData = try main.files.cwd().read(main.stackAllocator, "assets/cubyz/default_background.png");
544547
defer main.stackAllocator.free(defaultImageData);
545-
try main.files.cubyzDir().write("backgrounds/default_background.png", defaultImageData);
548+
try dir.write("default_background.png", defaultImageData);
546549

547550
return std.fmt.allocPrint(allocator.allocator, "{s}/backgrounds/default_background.png", .{main.files.cubyzDirStr()}) catch unreachable;
548551
}
549552

550553
// Otherwise load a random texture from the backgrounds folder. The player may make their own pictures which can be chosen as well.
551-
var dir = try main.files.cubyzDir().openIterableDir("backgrounds");
552-
defer dir.close();
553-
554554
var walker = dir.walk(main.stackAllocator);
555555
defer walker.deinit();
556556
var fileList = main.List([]const u8).init(main.stackAllocator);

‎src/settings.zig‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,21 @@ pub fn save() void {
182182
std.log.err("Couldn't write settings to file: {s}", .{@errorName(err)});
183183
};
184184
}
185+
186+
pub const launchConfig = struct {
187+
pub var cubyzDir: []const u8 = "";
188+
189+
pub fn init() void {
190+
const zon: ZonElement = main.files.cwd().readToZon(main.stackAllocator, "launchConfig.zon") catch |err| blk: {
191+
std.log.err("Could not read launchConfig.zon: {s}", .{@errorName(err)});
192+
break :blk .null;
193+
};
194+
defer zon.deinit(main.stackAllocator);
195+
196+
cubyzDir = main.globalAllocator.dupe(u8, zon.get([]const u8, "cubyzDir", cubyzDir));
197+
}
198+
199+
pub fn deinit() void {
200+
main.globalAllocator.free(cubyzDir);
201+
}
202+
};

0 commit comments

Comments
 (0)