Load the settings and gui_layout files from a global directory.

For linux and mac I just use "~/.cubyz"
For windows there seems to be no consensus on where game data should be located.
Out of the commonly used locations, "~/Saved Games/Cubyz" seemed the most sane option to me.

fixes #457
This commit is contained in:
IntegratedQuantum 2024-12-01 11:48:31 +01:00
parent b06b4e7224
commit 5f7ccd0504
5 changed files with 40 additions and 8 deletions

4
.gitignore vendored
View File

@ -5,10 +5,6 @@ zig-out/
zig-cache/
.zig-cache/
serverAssets/
settings.json
gui_layout.json
settings.zig.zon
gui_layout.zig.zon
gamecontrollerdb.txt
gamecontrollerdb.stamp

View File

@ -1,4 +1,5 @@
const std = @import("std");
const builtin = @import("builtin");
const main = @import("root");
const NeverFailingAllocator = main.utils.NeverFailingAllocator;
@ -42,6 +43,38 @@ fn cwd() Dir {
};
}
var cubyzDir_: ?std.fs.Dir = null;
pub fn cubyzDir() Dir {
return .{
.dir = cubyzDir_ orelse std.fs.cwd(),
};
}
fn flawedInit() !void {
const homePath = try std.process.getEnvVarOwned(main.stackAllocator.allocator, if(builtin.os.tag == .windows) "USERPROFILE" else "HOME");
defer main.stackAllocator.free(homePath);
var homeDir = try std.fs.openDirAbsolute(homePath, .{});
defer homeDir.close();
if(builtin.os.tag == .windows) {
cubyzDir_ = try homeDir.makeOpenPath("Saved Games/Cubyz", .{});
} else {
cubyzDir_ = try homeDir.makeOpenPath(".cubyz", .{});
}
}
pub fn init() void {
flawedInit() catch |err| {
std.log.err("Error {s} while opening global Cubyz directory. Using working directory instead.", .{@errorName(err)});
};
}
pub fn deinit() void {
if(cubyzDir_ != null) {
cubyzDir_.?.close();
}
}
pub const Dir = struct {
dir: std.fs.Dir,

View File

@ -222,13 +222,13 @@ pub fn save() void { // MARK: save()
guiZon.put(window.id, windowZon);
}
main.files.writeZon("gui_layout.zig.zon", guiZon) catch |err| {
main.files.cubyzDir().writeZon("gui_layout.zig.zon", guiZon) catch |err| {
std.log.err("Could not write gui_layout.zig.zon: {s}", .{@errorName(err)});
};
}
fn load() void {
const zon: ZonElement = main.files.readToZon(main.stackAllocator, "gui_layout.zig.zon") catch |err| blk: {
const zon: ZonElement = main.files.cubyzDir().readToZon(main.stackAllocator, "gui_layout.zig.zon") catch |err| blk: {
if(err != error.FileNotFound) {
std.log.err("Could not read gui_layout.zig.zon: {s}", .{@errorName(err)});
}

View File

@ -541,6 +541,9 @@ pub fn main() void { // MARK: main()
}
} else |_| {}
files.init();
defer files.deinit();
settings.init();
defer settings.deinit();

View File

@ -60,7 +60,7 @@ pub var developerGPUInfiniteLoopDetection: bool = false;
pub var controllerAxisDeadzone: f32 = 0.0;
pub fn init() void {
const zon: ZonElement = main.files.readToZon(main.stackAllocator, "settings.zig.zon") catch |err| blk: {
const zon: ZonElement = main.files.cubyzDir().readToZon(main.stackAllocator, "settings.zig.zon") catch |err| blk: {
if(err != error.FileNotFound) {
std.log.err("Could not read settings file: {s}", .{@errorName(err)});
}
@ -149,7 +149,7 @@ pub fn save() void {
zonObject.put("keyboard", keyboard);
// Write to file:
main.files.writeZon("settings.zig.zon", zonObject) catch |err| {
main.files.cubyzDir().writeZon("settings.zig.zon", zonObject) catch |err| {
std.log.err("Couldn't write settings to file: {s}", .{@errorName(err)});
};
}