Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/gui/windows/error_prompt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const Label = @import("../components/Label.zig");
const VerticalList = @import("../components/VerticalList.zig");
const Button = @import("../components/Button.zig");

var errorCount: u32 = 0;
Comment thread
thecreare marked this conversation as resolved.
Outdated
var errorText: ?[]const u8 = null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make your life easy: Allocators will ignore zero-length allocations.

Suggested change
var errorText: ?[]const u8 = null;
var errorText: []const u8 = "";

var fileExplorerIcon: Texture = undefined;
var isOpen: bool = false;

pub var window = GuiWindow{
.contentSize = Vec2f{128, 64},
Expand All @@ -30,22 +33,50 @@ pub fn init() void {

pub fn deinit() void {
fileExplorerIcon.deinit();
if (errorText != null) {
main.globalAllocator.free(errorText.?);
errorText = null;
}
Comment thread
thecreare marked this conversation as resolved.
Outdated
}

fn openLog() void {
main.files.openDirInWindow("logs");
}

pub fn raiseError(newText: []const u8) void {
if (isOpen) {
errorCount += 1;
onClose();
onOpen();
} else {
if (errorText != null) main.globalAllocator.free(errorText.?);
Comment thread
thecreare marked this conversation as resolved.
Outdated
errorText = main.globalAllocator.dupe(u8, newText);
errorCount = 0;
gui.openWindowFromRef(&window);
}
}

const padding: f32 = 8;
pub fn update() void {
if (main.Window.Gamepad.wereControllerMappingsDownloaded()) {
gui.closeWindowFromRef(&window);
}
}

const singleErrorFmtText = "#ff0000{s}";
const multipleErrorFmtText = "#ff0000{s}\n#ffff00And {d} more...\nCheck the logs for details.";

pub fn onOpen() void {
isOpen = true;
const list = VerticalList.init(.{padding, 16 + padding}, 300, 16);
list.add(Label.init(.{padding, 16 + padding}, 128, "#ffff00The game encountered errors. Check the logs for details", .center));
var str: []const u8 = undefined;
if (errorCount == 0) {
str = std.fmt.allocPrint(main.stackAllocator.allocator, singleErrorFmtText, .{errorText.?}) catch unreachable;
} else {
str = std.fmt.allocPrint(main.stackAllocator.allocator, multipleErrorFmtText, .{errorText.?, errorCount}) catch unreachable;
}
defer main.stackAllocator.free(str);
list.add(Label.init(.{padding, 16 + padding}, 256, str, .center));
list.add(Button.initIcon(.{0, 0}, .{16, 16}, fileExplorerIcon, false, .init(openLog)));
list.finish(.center);
window.rootComponent = list.toComponent();
Expand All @@ -54,6 +85,7 @@ pub fn onOpen() void {
}

pub fn onClose() void {
isOpen = false;
if (window.rootComponent) |*comp| {
comp.deinit();
}
Expand Down
7 changes: 2 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ fn cacheString(comptime str: []const u8) []const u8 {
var logFile: ?std.Io.File = undefined;
var logFileTs: ?std.Io.File = undefined;
var supportsANSIColors: bool = undefined;
var openingErrorWindow: bool = false;
// overwrite the log function:
pub const std_options: std.Options = .{ // MARK: std_options
.log_level = .debug,
Expand Down Expand Up @@ -136,10 +135,8 @@ noinline fn runtimeLogFn(level: std.log.Level, format: []const u8, args: []const
logToStdErr("[{s}]: {s}{s}", .{filePrefix, writer.buffered(), fileSuffix});
}

if (level == .err and !openingErrorWindow and !settings.launchConfig.headlessServer) {
openingErrorWindow = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exists to prevent infinite recursion when there is an error while trying to open the error window.
(this e.g. happens when there is an error before the gui is initialized)

gui.openWindow("error_prompt");
openingErrorWindow = false;
if (level == .err and !settings.launchConfig.headlessServer) {
gui.windowlist.error_prompt.raiseError(writer.buffered());
}
}

Expand Down
Loading