Skip to content

Commit 4c3c6cb

Browse files
committed
Error popup
1 parent 8adfd7d commit 4c3c6cb

File tree

1 file changed

+55
-48
lines changed

1 file changed

+55
-48
lines changed

src/main.zig

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ pub fn main() !void {
7373
@memcpy(game_executable_path_mut[0..settings.game_executable_path.len], settings.game_executable_path);
7474
game_executable_path_mut[settings.game_executable_path.len] = 0;
7575

76+
var err_msg_buf: [1337]u8 = undefined;
77+
var err_msg_slice: []u8 = undefined;
78+
7679
while (!window.shouldClose()) {
7780
zglfw.pollEvents();
7881

@@ -110,57 +113,60 @@ pub fn main() !void {
110113
var allocator = arena.allocator();
111114

112115
var diagnostics: converter.Diagnostics = .{};
113-
converter.convert(
116+
if (converter.convert(
114117
settings.input_folder_path,
115118
settings.output_folder_path,
116119
allocator,
117120
&diagnostics,
118-
) catch |err| switch (err) {
119-
error.UnexpectedToken => {
120-
const token = diagnostics.token orelse "null";
121-
const file_path = diagnostics.file_path orelse "null";
122-
const line = diagnostics.line orelse -1;
123-
const column = diagnostics.column orelse -1;
124-
125-
std.debug.print("Error: Unexpected '{s}' at {s}:{}:{}\n", .{
126-
token,
127-
file_path,
128-
line,
129-
column,
130-
});
131-
132-
return err;
133-
},
134-
error.TooManyTabs => {
135-
const file_path = diagnostics.file_path orelse "null";
136-
const line = diagnostics.line orelse -1;
137-
const column = diagnostics.column orelse -1;
138-
139-
std.debug.print("Error: Too many tabs at {s}:{}:{}\n", .{
140-
file_path,
141-
line,
142-
column,
143-
});
144-
145-
return err;
146-
},
147-
else => |e| return e,
148-
};
149-
150-
try converter.beautifyLua(settings.output_folder_path, allocator);
151-
152-
// TODO: Run .convert() in a separate thread, letting it update a passed Progress struct so we can update a progress bar here?
153-
// TODO: Check if std/Progress.zig is of use: https://ziglang.org/documentation/master/std/src/std/Progress.zig.html
154-
// TODO: Look at this example of multithreading in Zig: https://gist.github.com/cabarger/d3879745b8477670070f826cad2f027d
155-
// var progress: f32 = 0.0;
156-
// _ = progress;
157-
// zgui.pushStyleColor4f(.{ .idx = .plot_histogram, .c = .{ 0.1 + 0.5 * (1 - progress), 0.2 + 0.7 * progress, 0.3, 1.0 } });
158-
// zgui.progressBar(.{ .fraction = progress, .overlay = "" });
159-
// zgui.popStyleColor(.{});
160-
// progress += 0.01;
161-
// if (progress > 2.0) progress = 0.0;
162-
163-
std.debug.print("Done converting!\n", .{});
121+
)) {
122+
try converter.beautifyLua(settings.output_folder_path, allocator);
123+
124+
// TODO: Run .convert() in a separate thread, letting it update a passed Progress struct so we can update a progress bar here?
125+
// TODO: Check if std/Progress.zig is of use: https://ziglang.org/documentation/master/std/src/std/Progress.zig.html
126+
// TODO: Look at this example of multithreading in Zig: https://gist.github.com/cabarger/d3879745b8477670070f826cad2f027d
127+
// var progress: f32 = 0.0;
128+
// _ = progress;
129+
// zgui.pushStyleColor4f(.{ .idx = .plot_histogram, .c = .{ 0.1 + 0.5 * (1 - progress), 0.2 + 0.7 * progress, 0.3, 1.0 } });
130+
// zgui.progressBar(.{ .fraction = progress, .overlay = "" });
131+
// zgui.popStyleColor(.{});
132+
// progress += 0.01;
133+
// if (progress > 2.0) progress = 0.0;
134+
135+
std.debug.print("Done converting!\n", .{});
136+
} else |err| {
137+
switch (err) {
138+
error.UnexpectedToken => {
139+
err_msg_slice = try std.fmt.bufPrint(&err_msg_buf, "Error: Unexpected token '{s}' in file {s} on line {} and column {}\n", .{
140+
diagnostics.token orelse "null",
141+
diagnostics.file_path orelse "null",
142+
diagnostics.line orelse -1,
143+
diagnostics.column orelse -1,
144+
});
145+
},
146+
error.TooManyTabs => {
147+
err_msg_slice = try std.fmt.bufPrint(&err_msg_buf, "Error: Too many tabs in file {s} on line {} and column {}\n", .{
148+
diagnostics.file_path orelse "null",
149+
diagnostics.line orelse -1,
150+
diagnostics.column orelse -1,
151+
});
152+
},
153+
// TODO: Add more custom error messages,
154+
// by briefly commenting out this else-statement,
155+
// and looking at the printed list of unhandled errors
156+
else => |e| {
157+
err_msg_slice = try std.fmt.bufPrint(&err_msg_buf, "{} in file {s}\n", .{
158+
e,
159+
diagnostics.file_path orelse "null",
160+
});
161+
},
162+
}
163+
std.debug.print("{s}\n", .{err_msg_slice});
164+
zgui.openPopup("error_popup", .{});
165+
}
166+
}
167+
if (zgui.beginPopup("error_popup", .{})) {
168+
zgui.text("{s}\n", .{err_msg_slice});
169+
zgui.endPopup();
164170
}
165171

166172
zgui.setNextItemWidth(@max(zgui.calcTextSize(settings.game_executable_path, .{})[0] + padding, min_width));
@@ -172,17 +178,18 @@ pub fn main() !void {
172178
if (zgui.button("Launch", .{ .w = 200.0 })) {
173179
// TODO: Handle settings.game_executable_path not being set yet
174180
try std.os.chdir(std.fs.path.dirname(settings.game_executable_path) orelse return ConverterErrors.WeirdGameDir);
181+
175182
var argv = [_][]const u8{settings.game_executable_path};
176183
const result = try std.ChildProcess.exec(.{ .argv = &argv, .allocator = gpa });
177184
_ = result;
178185
}
186+
179187
if (zgui.button("Zip", .{ .w = 200.0 })) {
180188
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
181189
defer arena.deinit();
182190
var allocator = arena.allocator();
183191

184192
try converter.zipMods(settings.input_folder_path, settings.output_folder_path, allocator);
185-
186193
std.debug.print("Done zipping!\n", .{});
187194
}
188195
}

0 commit comments

Comments
 (0)