Prevent settings from breaking when no previous file is present. (#1719)

Fix #1718
This commit is contained in:
codemob 2025-07-25 12:09:51 -04:00 committed by GitHub
parent 08c78ff48d
commit a47c8d14a1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 6 deletions

View File

@ -183,7 +183,7 @@ pub fn deinit() void {
}
pub fn save() void { // MARK: save()
const guiZon = ZonElement.initObject(main.stackAllocator);
var guiZon = ZonElement.initObject(main.stackAllocator);
defer guiZon.deinit(main.stackAllocator);
for(windowList.items) |window| {
const windowZon = ZonElement.initObject(main.stackAllocator);
@ -218,7 +218,7 @@ pub fn save() void { // MARK: save()
}
// Merge with the old settings file to preserve unknown settings.
const oldZon: ZonElement = main.files.cubyzDir().readToZon(main.stackAllocator, "gui_layout.zig.zon") catch |err| blk: {
var oldZon: 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)});
}
@ -226,7 +226,13 @@ pub fn save() void { // MARK: save()
};
defer oldZon.deinit(main.stackAllocator);
oldZon.join(guiZon);
if(oldZon == .object) {
oldZon.join(guiZon);
} else {
oldZon.deinit(main.stackAllocator);
oldZon = guiZon;
guiZon = .null;
}
main.files.cubyzDir().writeZon("gui_layout.zig.zon", oldZon) catch |err| {
std.log.err("Could not write gui_layout.zig.zon: {s}", .{@errorName(err)});

View File

@ -124,7 +124,7 @@ pub fn deinit() void {
}
pub fn save() void {
const zonObject = ZonElement.initObject(main.stackAllocator);
var zonObject = ZonElement.initObject(main.stackAllocator);
defer zonObject.deinit(main.stackAllocator);
inline for(@typeInfo(@This()).@"struct".decls) |decl| {
@ -154,7 +154,7 @@ pub fn save() void {
zonObject.put("keyboard", keyboard);
// Merge with the old settings file to preserve unknown settings.
const oldZonObject: ZonElement = main.files.cubyzDir().readToZon(main.stackAllocator, settingsFile) catch |err| blk: {
var oldZonObject: ZonElement = main.files.cubyzDir().readToZon(main.stackAllocator, settingsFile) catch |err| blk: {
if(err != error.FileNotFound) {
std.log.err("Could not read settings file: {s}", .{@errorName(err)});
}
@ -162,7 +162,13 @@ pub fn save() void {
};
defer oldZonObject.deinit(main.stackAllocator);
oldZonObject.join(zonObject);
if(oldZonObject == .object) {
oldZonObject.join(zonObject);
} else {
oldZonObject.deinit(main.stackAllocator);
oldZonObject = zonObject;
zonObject = .null;
}
main.files.cubyzDir().writeZon(settingsFile, oldZonObject) catch |err| {
std.log.err("Couldn't write settings to file: {s}", .{@errorName(err)});