Added error overlay which is used to display errors in a more user friendly way, currently only used if the CSharp compilation fails

This commit is contained in:
EvilFactory
2023-01-06 19:58:15 -03:00
parent 9fcab5ff60
commit 94c68b9fe6
7 changed files with 110 additions and 16 deletions

View File

@@ -862,6 +862,8 @@ namespace Barotrauma
Screen.Selected.AddToGUIUpdateList();
LuaCsLogger.AddToGUIUpdateList();
Client?.AddToGUIUpdateList();
SubmarinePreview.AddToGUIUpdateList();

View File

@@ -0,0 +1,50 @@
using Microsoft.Xna.Framework;
namespace Barotrauma
{
partial class LuaCsLogger
{
private static GUIFrame overlayFrame;
private static GUITextBlock textBlock;
private static double showTimer = 0;
private static void CreateOverlay(string message)
{
overlayFrame = new GUIFrame(new RectTransform(new Vector2(0.4f, 0.03f), null), null, new Color(50, 50, 50, 100))
{
CanBeFocused = false
};
GUILayoutGroup layout = new GUILayoutGroup(new RectTransform(new Vector2(0.8f, 0.8f), overlayFrame.RectTransform, Anchor.CenterLeft), false, Anchor.Center)
{
};
textBlock = new GUITextBlock(new RectTransform(new Vector2(1f, 0f), layout.RectTransform), message);
overlayFrame.RectTransform.MinSize = new Point((int)(textBlock.TextSize.X * 1.2), 0);
layout.Recalculate();
}
public static void AddToGUIUpdateList()
{
if (overlayFrame != null && Timing.TotalTime <= showTimer)
{
overlayFrame.AddToGUIUpdateList();
}
}
public static void ShowErrorOverlay(string message, float time = 5f, float duration = 1.5f)
{
if (Timing.TotalTime <= showTimer)
{
return;
}
CreateOverlay(message);
overlayFrame.Flash(Color.Red, duration, true);
showTimer = Timing.TotalTime + time;
}
}
}

View File

@@ -56,6 +56,19 @@ namespace Barotrauma
}
};
new GUITickBox(new RectTransform(new Vector2(0.8f, 0.1f), list.Content.RectTransform), "Disalbe Error GUI Overlay")
{
Selected = GameMain.LuaCs.Config.DisableErrorGUIOverlay,
ToolTip = "",
OnSelected = (GUITickBox tick) =>
{
GameMain.LuaCs.Config.DisableErrorGUIOverlay = tick.Selected;
GameMain.LuaCs.UpdateConfig();
return true;
}
};
new GUIButton(new RectTransform(new Vector2(1f, 0.1f), list.Content.RectTransform), $"Remove Client-Side LuaCs", style: "GUIButtonSmall")
{
ToolTip = "Remove Client-Side LuaCs.",

View File

@@ -6,6 +6,14 @@ namespace Barotrauma
{
partial class LuaCsSetup
{
public void AddToGUIUpdateList()
{
if (!GameMain.LuaCs.Config.DisableErrorGUIOverlay)
{
LuaCsLogger.AddToGUIUpdateList();
}
}
public void CheckInitialize()
{
List<ContentPackage> csharpMods = new List<ContentPackage>();

View File

@@ -167,6 +167,25 @@ namespace Barotrauma
return syntaxTrees;
}
private ContentPackage FindSourcePackage(Diagnostic diagnostic)
{
if (diagnostic.Location.SourceTree == null)
{
return null;
}
string path = diagnostic.Location.SourceTree.FilePath;
foreach (var package in ContentPackageManager.AllPackages)
{
if (Path.GetFullPath(path).StartsWith(Path.GetFullPath(package.Dir)))
{
return package;
}
}
return null;
}
public List<Type> Compile()
{
IEnumerable<SyntaxTree> syntaxTrees = ParseSources();
@@ -188,6 +207,13 @@ namespace Barotrauma
foreach (Diagnostic diagnostic in failures)
{
errStr += $"\n{diagnostic}";
#if CLIENT
ContentPackage package = FindSourcePackage(diagnostic);
if (package != null)
{
LuaCsLogger.ShowErrorOverlay($"{package.Name} {package.ModVersion} is causing compilation errors. Check debug console for more details.", 7f, 7f);
}
#endif
}
LuaCsLogger.LogError(errStr, LuaCsMessageOrigin.CSharpMod);
}

View File

@@ -13,12 +13,16 @@ namespace Barotrauma
CSharpMod,
}
class LuaCsLogger
partial class LuaCsLogger
{
#if SERVER
private const string LogPrefix = "SV";
private const int NetMaxLength = 1024;
private const int NetMaxMessages = 60;
// This is used so its possible to call logging functions inside the serverLog
// hook without creating an infinite loop
private static bool lockLog = false;
#else
private const string LogPrefix = "CL";
#endif
@@ -94,10 +98,6 @@ namespace Barotrauma
#endif
}
// This is used so its possible to call logging functions inside the serverLog
// hook without creating an infinite loop
private static bool lockLog = false;
public static void Log(string message, Color? color = null, ServerLog.MessageType messageType = ServerLog.MessageType.ServerMessage)
{
MessageLogger?.Invoke(message);

View File

@@ -18,6 +18,7 @@ namespace Barotrauma
public bool ForceCsScripting = false;
public bool TreatForcedModsAsNormal = false;
public bool PreferToUseWorkshopLuaSetup = false;
public bool DisableErrorGUIOverlay = false;
public LuaCsSetupConfig() { }
}
@@ -91,14 +92,13 @@ namespace Barotrauma
{
Config = new LuaCsSetupConfig();
}
}
public void UpdateConfig()
{
FileStream file;
if (!File.Exists(configFileName)) file = File.Create(configFileName);
else file = File.Open(configFileName, FileMode.Truncate, FileAccess.Write);
if (!File.Exists(configFileName)) { file = File.Create(configFileName); }
else { file = File.Open(configFileName, FileMode.Truncate, FileAccess.Write); }
LuaCsConfig.Save(file, Config);
file.Close();
}
@@ -365,17 +365,12 @@ namespace Barotrauma
Stopwatch compilationTime = new Stopwatch();
compilationTime.Start();
var modTypes = CsScriptLoader.Compile();
modTypes.ForEach(t =>
{
try
{
t.GetConstructor(new Type[] { })?.Invoke(null);
}
catch (Exception ex)
{
LuaCsLogger.HandleException(ex, LuaCsMessageOrigin.CSharpMod);
}
t.GetConstructor(new Type[] { })?.Invoke(null);
});
compilationTime.Stop();
LuaCsLogger.LogMessage($"Took {compilationTime.ElapsedMilliseconds}ms to compile and run Cs Scripts.");
}