Skip to content

Commit b30e1c9

Browse files
committed
Read from and write to settings.json
1 parent 1f9d03c commit b30e1c9

File tree

1 file changed

+80
-50
lines changed

1 file changed

+80
-50
lines changed

src/main.zig

Lines changed: 80 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,20 @@ const ConverterErrors = error{
1010
WeirdGameDir,
1111
};
1212

13+
const Settings = struct {
14+
input_folder_path: []const u8 = "",
15+
output_folder_path: []const u8 = "",
16+
game_executable_path: []const u8 = "",
17+
};
18+
1319
pub fn main() !void {
1420
zglfw.init() catch {
1521
std.log.err("Failed to initialize GLFW library.", .{});
1622
return;
1723
};
1824
defer zglfw.terminate();
1925

20-
const window = zglfw.Window.create(1000, 300, "Legacy Mod Converter 1.0 for Pre-Release 5.2", null) catch {
26+
const window = zglfw.Window.create(1600, 300, "Legacy Mod Converter 1.0 for Pre-Release 5.2", null) catch {
2127
std.log.err("Failed to create window.", .{});
2228
return;
2329
};
@@ -45,6 +51,28 @@ pub fn main() !void {
4551
);
4652
defer zgui.backend.deinit();
4753

54+
const settings_text = std.fs.cwd().readFileAlloc(gpa, "settings.json", 1337) catch |err| switch (err) {
55+
error.FileNotFound => try gpa.dupe(u8, "{}"),
56+
else => return err,
57+
};
58+
defer gpa.free(settings_text);
59+
60+
const settings_parsed = try std.json.parseFromSlice(Settings, gpa, settings_text, .{});
61+
defer settings_parsed.deinit();
62+
var settings = settings_parsed.value;
63+
64+
var input_folder_path_mut: [std.fs.MAX_PATH_BYTES + 1:0]u8 = undefined;
65+
@memcpy(input_folder_path_mut[0..settings.input_folder_path.len], settings.input_folder_path);
66+
input_folder_path_mut[settings.input_folder_path.len] = 0;
67+
68+
var output_folder_path_mut: [std.fs.MAX_PATH_BYTES + 1:0]u8 = undefined;
69+
@memcpy(output_folder_path_mut[0..settings.output_folder_path.len], settings.output_folder_path);
70+
output_folder_path_mut[settings.output_folder_path.len] = 0;
71+
72+
var game_executable_path_mut: [std.fs.MAX_PATH_BYTES + 1:0]u8 = undefined;
73+
@memcpy(game_executable_path_mut[0..settings.game_executable_path.len], settings.game_executable_path);
74+
game_executable_path_mut[settings.game_executable_path.len] = 0;
75+
4876
while (!window.shouldClose()) {
4977
zglfw.pollEvents();
5078

@@ -55,45 +83,35 @@ pub fn main() !void {
5583

5684
// Set the starting window position and size to custom values
5785
zgui.setNextWindowPos(.{ .x = 0.0, .y = 0.0, .cond = .always });
58-
// zgui.setNextWindowSize(.{ .w = -1.0, .h = -1.0, .cond = .always });
59-
60-
if (zgui.begin("a", .{ .flags = .{ .no_title_bar = true, .no_resize = true, .no_background = true } })) {
61-
const static = struct {
62-
var input_mod_path_buf: [std.fs.MAX_PATH_BYTES + 1]u8 = undefined;
63-
var output_mod_path_buf: [std.fs.MAX_PATH_BYTES + 1]u8 = undefined;
64-
var progress: f32 = 0.0;
65-
};
66-
// std.debug.print("xd: {d}\n", .{"I:/Programming/Cortex-Command-Community-Project-Source".len});
67-
// zgui.pushItemWidth("I:/Programming/Cortex-Command-Community-Project-Source".len);
68-
// zgui.setNextItemWidth("I:/Programming/Cortex-Command-Community-Project-Source".len);
69-
if (zgui.inputTextWithHint("Input/ directory path", .{ .hint = "Copy-paste a path from File Explorer here", .buf = static.input_mod_path_buf[0..] })) {
70-
std.debug.print("The user edited input_mod_path_buf\n", .{});
86+
zgui.setNextWindowSize(.{ .w = -1.0, .h = -1.0, .cond = .always });
87+
// zgui.setNextWindowSize(.{ .w = 420, .h = 300, .cond = .always });
88+
89+
if (zgui.begin("invisible_title", .{ .flags = .{ .no_title_bar = true, .no_resize = true, .no_background = true } })) {
90+
zgui.setNextItemWidth(@max(zgui.calcTextSize(settings.input_folder_path, .{})[0], 585));
91+
if (zgui.inputTextWithHint("Input/ folder path", .{ .hint = "Copy-paste a path from File Explorer here", .buf = &input_folder_path_mut })) {
92+
settings.input_folder_path = std.mem.span(@as([*:0]u8, &input_folder_path_mut));
93+
std.debug.print("settings.input_folder_path is now '{s}'\n", .{settings.input_folder_path});
94+
try writeSettings(settings);
7195
}
72-
// zgui.popItemWidth();
73-
if (zgui.inputTextWithHint("Mods/ directory path", .{ .hint = "Copy-paste a path from File Explorer here", .buf = static.output_mod_path_buf[0..] })) {
74-
std.debug.print("The user edited output_mod_path_buf\n", .{});
96+
97+
zgui.setNextItemWidth(@max(zgui.calcTextSize(settings.output_folder_path, .{})[0], 585));
98+
if (zgui.inputTextWithHint("Mods/ folder path", .{ .hint = "Copy-paste a path from File Explorer here", .buf = &output_folder_path_mut })) {
99+
settings.output_folder_path = std.mem.span(@as([*:0]u8, &output_folder_path_mut));
100+
std.debug.print("settings.output_folder_path is now '{s}'\n", .{settings.output_folder_path});
101+
try writeSettings(settings);
75102
}
103+
76104
if (zgui.button("Convert", .{ .w = 200.0 })) {
77105
std.debug.print("Converting...\n", .{});
78106

79107
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
80108
defer arena.deinit();
81109
var allocator = arena.allocator();
82110

83-
const cwd = std.fs.cwd();
84-
85-
var input_mod_path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
86-
// const input_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Mod-Converter-Engine/tests/mod/in", &input_mod_path_buffer);
87-
const input_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Community-Project-Data/LegacyModConverter-v1.0-pre5.2/Input", &input_mod_path_buffer);
88-
89-
var output_mod_path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
90-
// const output_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Mod-Converter-Engine/tests/mod/out", &output_mod_path_buffer);
91-
const output_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Community-Project-Data/Mods", &output_mod_path_buffer);
92-
93111
var diagnostics: converter.Diagnostics = .{};
94112
converter.convert(
95-
input_mod_path,
96-
output_mod_path,
113+
settings.input_folder_path,
114+
settings.output_folder_path,
97115
allocator,
98116
&diagnostics,
99117
) catch |err| switch (err) {
@@ -124,12 +142,31 @@ pub fn main() !void {
124142
else => |e| return e,
125143
};
126144

145+
// TODO: Run .convert() in a separate thread, letting it update a passed Progress struct so we can update a progress bar here?
146+
// TODO: Check if std/Progress.zig is of use: https://ziglang.org/documentation/master/std/src/std/Progress.zig.html
147+
// TODO: Look at this example of multithreading in Zig: https://gist.github.com/cabarger/d3879745b8477670070f826cad2f027d
148+
// var progress: f32 = 0.0;
149+
// _ = progress;
150+
// zgui.pushStyleColor4f(.{ .idx = .plot_histogram, .c = .{ 0.1 + 0.5 * (1 - progress), 0.2 + 0.7 * progress, 0.3, 1.0 } });
151+
// zgui.progressBar(.{ .fraction = progress, .overlay = "" });
152+
// zgui.popStyleColor(.{});
153+
// progress += 0.01;
154+
// if (progress > 2.0) progress = 0.0;
155+
127156
std.debug.print("Done converting!\n", .{});
128157
}
158+
159+
zgui.setNextItemWidth(@max(zgui.calcTextSize(settings.game_executable_path, .{})[0], 585));
160+
if (zgui.inputTextWithHint("Game .exe path", .{ .hint = "Copy-paste a path from File Explorer here", .buf = &game_executable_path_mut })) {
161+
settings.game_executable_path = std.mem.span(@as([*:0]u8, &game_executable_path_mut));
162+
std.debug.print("settings.game_executable_path is now '{s}'\n", .{settings.game_executable_path});
163+
try writeSettings(settings);
164+
}
165+
129166
if (zgui.button("Launch", .{ .w = 200.0 })) {
130-
const path = "I:/Programming/Cortex-Command-Community-Project-Data/Cortex Command.debug.release.exe";
131-
try std.os.chdir(std.fs.path.dirname(path) orelse return ConverterErrors.WeirdGameDir);
132-
var argv = [_][]const u8{path};
167+
// TODO: Handle settings.game_executable_path not being set yet
168+
try std.os.chdir(std.fs.path.dirname(settings.game_executable_path) orelse return ConverterErrors.WeirdGameDir);
169+
var argv = [_][]const u8{settings.game_executable_path};
133170
const result = try std.ChildProcess.exec(.{ .argv = &argv, .allocator = gpa });
134171
_ = result;
135172
}
@@ -138,27 +175,10 @@ pub fn main() !void {
138175
defer arena.deinit();
139176
var allocator = arena.allocator();
140177

141-
// TODO: Why am I using cwd.realpath()?
142-
143-
const cwd = std.fs.cwd();
144-
145-
var input_mod_path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
146-
const input_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Community-Project-Data/LegacyModConverter-v1.0-pre5.2/Input", &input_mod_path_buffer);
147-
148-
var output_mod_path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
149-
const output_mod_path = try cwd.realpath("I:/Programming/Cortex-Command-Community-Project-Data/Mods", &output_mod_path_buffer);
150-
151-
try converter.zip_mods(input_mod_path, output_mod_path, allocator);
178+
try converter.zip_mods(settings.input_folder_path, settings.output_folder_path, allocator);
152179

153180
std.debug.print("Done zipping!\n", .{});
154181
}
155-
156-
zgui.pushStyleColor4f(.{ .idx = .plot_histogram, .c = .{ 0.1 + 0.5 * (1 - static.progress), 0.2 + 0.7 * static.progress, 0.3, 1.0 } });
157-
zgui.progressBar(.{ .fraction = static.progress, .overlay = "" });
158-
zgui.popStyleColor(.{});
159-
160-
static.progress += 0.03;
161-
if (static.progress > 2.0) static.progress = 0.0;
162182
}
163183
zgui.end();
164184

@@ -184,3 +204,13 @@ pub fn main() !void {
184204
_ = gctx.present();
185205
}
186206
}
207+
208+
fn writeSettings(settings: Settings) !void {
209+
const output_file = try std.fs.cwd().createFile("settings.json", .{});
210+
defer output_file.close();
211+
212+
var buffered = std.io.bufferedWriter(output_file.writer());
213+
const buffered_writer = buffered.writer();
214+
try std.json.stringify(settings, .{}, buffered_writer);
215+
try buffered.flush();
216+
}

0 commit comments

Comments
 (0)