Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new pop for errors in critical areas of the mod #38

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file modified 1.4/Assemblies/CombatAI.dll
Binary file not shown.
25 changes: 25 additions & 0 deletions Source/Rule56/Debugging/ExceptionUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Diagnostics;
using Verse;
namespace CombatAI
{
public static class ExceptionUtility
{
public static bool enabled = true;

public static void ShowExceptionGui(this Exception er, bool rethrow = true)
{
Log.Error($"ISMA: base error {er.ToString()}");
if (enabled && Find.WindowStack.windows.Count(w => w is Window_Exception) <= 3)
{
StackTrace trace = new StackTrace();
Window_Exception window = new Window_Exception(er, trace, string.Empty);
Find.WindowStack.Add(window);
}
if (rethrow)
{
throw er;
}
}
}
}
92 changes: 92 additions & 0 deletions Source/Rule56/Debugging/Window_Exception.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using CombatAI.Gui;
using UnityEngine;
using Verse;
using GUIUtility = CombatAI.Gui.GUIUtility;
namespace CombatAI
{
public class Window_Exception : Window
{
private readonly Listing_Collapsible collapsible;
private readonly Exception exception;
private readonly string report;
private readonly StackTrace trace;

public Window_Exception(Exception exception, StackTrace trace, string report)
{
this.drawShadow = true;
this.forcePause = true;
this.layer = WindowLayer.Super;
this.draggable = false;
this.collapsible = new Listing_Collapsible();
this.exception = exception;
this.trace = trace;
this.report = report.NullOrEmpty() ? exception.Message : report;

}

public override Vector2 InitialSize
{
get
{
Vector2 vec = new Vector2();
vec.x = Mathf.RoundToInt(Maths.Max(UI.screenWidth * 0.45f, 500));
vec.y = Mathf.RoundToInt(Maths.Max(UI.screenHeight * 0.45f, 400));
return vec;
}
}

public override void DoWindowContents(Rect inRect)
{
Rect bot = inRect.BottomPartPixels(60);
Rect top = inRect.TopPartPixels(inRect.height - 60);
collapsible.Expanded = true;
collapsible.Begin(top, "IMPORTANT: CAI-5000 Has encountered an <color=red>Error</color>");
try
{
collapsible.Label($"Error type:<color=yellow>\t{typeof(Exception)}</color>");
collapsible.Label($"{report}");
collapsible.Line(1);
collapsible.Label("Trace:", fontStyle: FontStyle.Bold);
for (int i = 1; i < trace.FrameCount; i++)
{
StackFrame frame = trace.GetFrame(i);
if (typeof(Root).IsAssignableFrom(frame.GetMethod().GetType()))
{
break;
}
collapsible.Gap(2);
collapsible.Label($"<color=red>{String.Format("{0,2}", i)}.strace</color>: {frame.GetMethod().DeclaringType}/{frame.GetMethod().ReflectedType}:{frame.GetMethod().Name}", fontSize: GUIFontSize.Smaller, fontStyle: FontStyle.Normal);
Type t = frame.GetMethod().GetType();
collapsible.Label($"<color=red>{String.Format("{0,2}", i)}.source.2</color>: assembly ({t.Assembly})", fontSize: GUIFontSize.Tiny);
}
collapsible.Line(1);
collapsible.Gap();
}
catch (Exception er)
{
Log.Error(er.ToString());
}
collapsible.End(ref top);
Widgets.TextArea(bot.TopPartPixels(40), "Please report this to CAI-5000 discord https://discord.gg/ftCjYB7jDe or on github https://github.com/kbatbouta/CAI-5000/issues/new", true);
GUIUtility.ExecuteSafeGUIAction(() =>
{
if (Widgets.ButtonText(bot.BottomPartPixels(20).LeftHalf(), "Close"))
{
Close();
}
GUI.color = Color.red;
if (Widgets.ButtonText(bot.BottomPartPixels(20).RightHalf(), "Disable Pop-ups and Close"))
{
ExceptionUtility.enabled = false;
Close();
}
});
}
}
}
Loading