Skip to content

Commit

Permalink
Add detailed error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
JungleDruid committed Feb 27, 2024
1 parent fcd5a49 commit 71fd961
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 137 deletions.
2 changes: 1 addition & 1 deletion BG3LocalizationMerger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PlatformTarget>x64</PlatformTarget>
<NeutralLanguage>en</NeutralLanguage>
<Version>$(VersionPrefix)1.0.4</Version>
<Version>$(VersionPrefix)1.1.0</Version>
<SignAssembly>False</SignAssembly>
</PropertyGroup>

Expand Down
4 changes: 2 additions & 2 deletions Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ ref childrenCount
}
}

Children = childrenCount == 0 ? Array.Empty<string>() : children[..childrenCount];
Texts = textsCount == 0 ? Array.Empty<string>() : texts[..textsCount];
Children = childrenCount == 0 ? [] : children[..childrenCount];
Texts = textsCount == 0 ? [] : texts[..textsCount];
Debug.Assert(!string.IsNullOrWhiteSpace(Id));
shared.Return(children);
shared.Return(texts);
Expand Down
205 changes: 143 additions & 62 deletions HandleExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -48,95 +49,175 @@ internal static partial class HandleExtractor
)]
private static partial Regex TooltipFilter();

public static IEnumerable<string> ExtractCharacterHandles(string path)
public static IEnumerable<string>? ExtractCharacterHandles(string path)
{
var text = ReadText(path);
if (!TemplateCharacterFilter().IsMatch(text))
return Enumerable.Empty<string>();
return ExtractHandles(text, CharacterHandles());
try
{
var text = ReadText(path);
if (!TemplateCharacterFilter().IsMatch(text))
return Enumerable.Empty<string>();
return ExtractHandles(text, CharacterHandles());
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static IEnumerable<string> ExtractWaypointHandles(string path)
public static IEnumerable<string>? ExtractWaypointHandles(string path)
{
return ExtractGenericHandles(path);
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static IEnumerable<string> ExtractQuestMarkerHandles(string path)
public static IEnumerable<string>? ExtractQuestMarkerHandles(string path)
{
return ExtractGenericHandles(path);
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static IEnumerable<string> ExtractBookHandles(string path)
public static IEnumerable<string>? ExtractBookHandles(string path)
{
return ExtractGenericHandles(path);
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static (IEnumerable<string>, IEnumerable<string>) ExtractQuestHandles(string path)
public static (IEnumerable<string>, IEnumerable<string>)? ExtractQuestHandles(string path)
{
string text = ReadText(path);
MatchCollection matches = QuestHandle().Matches(text);
var group = matches.GroupBy(x => x.Groups["Type"].Value);
return (
group
.FirstOrDefault(x => x.Key == "QuestTitle")
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>(),
group
.Where(x => x.Key.EndsWith("Description"))
.SelectMany(x => x)
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>()
);
try
{
string text = ReadText(path);
MatchCollection matches = QuestHandle().Matches(text);
var group = matches.GroupBy(x => x.Groups["Type"].Value);
return (
group
.FirstOrDefault(x => x.Key == "QuestTitle")
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>(),
group
.Where(x => x.Key.EndsWith("Description"))
.SelectMany(x => x)
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>()
);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static (IEnumerable<string>, IEnumerable<string>) ExtractStatusHandles(string path)
public static (IEnumerable<string>, IEnumerable<string>)? ExtractStatusHandles(string path)
{
string text = ReadText(path);
MatchCollection matches = StatusHandle().Matches(text);
var group = matches.GroupBy(x => x.Groups["Type"].Value);
return (
group
.FirstOrDefault(x => x.Key == "DisplayName")
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>(),
group
.Where(x => x.Key.EndsWith("Description"))
.SelectMany(x => x)
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>()
);
try
{
string text = ReadText(path);
MatchCollection matches = StatusHandle().Matches(text);
var group = matches.GroupBy(x => x.Groups["Type"].Value);
return (
group
.FirstOrDefault(x => x.Key == "DisplayName")
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>(),
group
.Where(x => x.Key.EndsWith("Description"))
.SelectMany(x => x)
?.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value) ?? Enumerable.Empty<string>()
);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static IEnumerable<Group> ExtractItemGroup(string path)
public static IEnumerable<Group>? ExtractItemGroup(string path)
{
string text = ReadText(path);
if (!TemplateItemFilter().IsMatch(text))
return Enumerable.Empty<Group>();
MatchCollection matches = TemplateHandles().Matches(text);
return matches
.SelectMany(x => x.Groups.Values.Skip(1))
.Where(x => !string.IsNullOrEmpty(x.Value));
try
{
string text = ReadText(path);
if (!TemplateItemFilter().IsMatch(text))
return Enumerable.Empty<Group>();
MatchCollection matches = TemplateHandles().Matches(text);
return matches
.SelectMany(x => x.Groups.Values.Skip(1))
.Where(x => !string.IsNullOrEmpty(x.Value));
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static IEnumerable<string> ExtractTooltipHandles(string path)
public static IEnumerable<string>? ExtractTooltipHandles(string path)
{
string text = ReadText(path);
MatchCollection matches = GenericHandle().Matches(text);
return matches
.Where(x => TooltipFilter().IsMatch(x.Groups[0].Value))
.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value)
.Where(x => !string.IsNullOrEmpty(x));
try
{
string text = ReadText(path);
MatchCollection matches = GenericHandle().Matches(text);
return matches
.Where(x => TooltipFilter().IsMatch(x.Groups[0].Value))
.SelectMany(x => x.Groups.Values.Skip(1))
.Select(x => x.Value)
.Where(x => !string.IsNullOrEmpty(x));
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static IEnumerable<string> ExtractHintHandles(string path)
public static IEnumerable<string>? ExtractHintHandles(string path)
{
return ExtractGenericHandles(path);
try
{
return ExtractGenericHandles(path);
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

public static IEnumerable<string> ExtractGenericHandles(string path)
public static IEnumerable<string>? ExtractGenericHandles(string path)
{
return ExtractHandles(ReadText(path), GenericHandle());
try
{
return ExtractHandles(ReadText(path), GenericHandle());
}
catch (Exception e)
{
MainWindow.LogFileError(path, e);
return null;
}
}

private static string ReadText(string path)
Expand Down
2 changes: 1 addition & 1 deletion MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<RowDefinition Height="70"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox x:Name="LogTextBox" Grid.Row="6" TextWrapping="Wrap" Text="" Grid.ColumnSpan="3" IsReadOnly="True" VerticalScrollBarVisibility="Visible"/>
<RichTextBox x:Name="LogTextBox" Grid.Row="6" Grid.ColumnSpan="3" IsReadOnly="True" VerticalScrollBarVisibility="Visible"/>
<Label Content="{ns:Loc UnpackedData}" VerticalContentAlignment="Center" Margin="10,10,10,10"/>
<Label Content="{ns:Loc LanguagePack}" VerticalContentAlignment="Center" Grid.Row="1" Margin="10,10,10,10"/>
<Label Content="{ns:Loc ReferencePack}" VerticalContentAlignment="Center" Grid.Row="2" Margin="10,10,10,10"/>
Expand Down
49 changes: 44 additions & 5 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Ookii.Dialogs.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
Expand All @@ -13,6 +14,7 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace BG3LocalizationMerger
Expand All @@ -26,6 +28,9 @@ public partial class MainWindow

public static MainWindow Instance => s_instance!;

public static int ErrorCount { get; private set; }
public static bool CacheValid { get; private set; }

public MainWindow()
{
s_instance = this;
Expand Down Expand Up @@ -100,26 +105,60 @@ private void InitCulture()
LanguageComboBox.SelectedIndex = 0;
}

public static void Log(string text)
public static void Log(string text, Brush? foreground = null)
{
s_instance!.Dispatcher.Invoke(() =>
{
var textbox = s_instance.LogTextBox;
text = $"[{DateTime.Now.ToLongTimeString()}] {text}";
if (!string.IsNullOrEmpty(textbox.Text))
text = '\n' + text;
textbox.Text += text;
Paragraph paragraph = new();
Run run = new($"[{DateTime.Now.ToLongTimeString()}] {text}");
paragraph.Inlines.Add(run);
paragraph.Margin = new(0);
if (foreground != null)
{
paragraph.Foreground = foreground;
}
textbox.Document.Blocks.Add(paragraph);
textbox.ScrollToEnd();
});
}

public static void LogError(string text)
{
ErrorCount += 1;
Log(text, Brushes.Red);
}

public static void LogFileError(string path, Exception e)
{
LogError($"ERROR: {e.Message} ({path[Settings.Default.UnpackedDataPath.Length..]})");
}

private async void Merge_Click(object sender, RoutedEventArgs e)
{
if (!await VerifyTextBoxes(true))
return;
SaveSettings();

ErrorCount = 0;
LogTextBox.Document.Blocks.Clear();

await RunMerge(PackageManager.Instance.Merge);

if (ErrorCount > 0)
{
CacheValid = false;
MessageBox.Show(
$"{ErrorCount} error{(ErrorCount > 1 ? "s" : "")} occurred. Check the logs for details.",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error
);
}
else
{
CacheValid = true;
}
}

private Task<bool> VerifyTextBoxes(bool showWarning, bool checkUnpackedData = true)
Expand Down
Loading

0 comments on commit 71fd961

Please sign in to comment.