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

Find references crash fix #1947

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace UndertaleModTool.Windows
/// </summary>
public partial class FindReferencesTypesDialog : Window
{
private static readonly MainWindow mainWindow = Application.Current.MainWindow as MainWindow;

private readonly UndertaleResource sourceObj;
private readonly UndertaleData data;
private readonly bool dontShowWindow = false;
Expand Down Expand Up @@ -140,9 +142,21 @@ private async void SearchButton_Click(object sender, RoutedEventArgs e)
return;
}

var results = UndertaleResourceReferenceMethodsMap.GetReferencesOfObject(sourceObj, data, typesList);
FindReferencesResults dialog = new(sourceObj, data, results);
dialog.Show();
FindReferencesResults dialog = null;
try
{
var results = UndertaleResourceReferenceMethodsMap.GetReferencesOfObject(sourceObj, data, typesList);
dialog = new(sourceObj, data, results);
dialog.Show();
}
catch (Exception ex)
{
mainWindow.ShowError("An error occured in the object references related window.\n" +
$"Please report this on GitHub.\n\n{ex}");
dialog?.Close();

}

}
else
{
Expand Down Expand Up @@ -173,9 +187,19 @@ private async void SearchButton_Click(object sender, RoutedEventArgs e)
}

Hide();
var results = await UndertaleResourceReferenceMethodsMap.GetUnreferencedObjects(data, typesDict);
FindReferencesResults dialog = new(data, results);
dialog.Show();
FindReferencesResults dialog = null;
try
{
var results = await UndertaleResourceReferenceMethodsMap.GetUnreferencedObjects(data, typesDict);
dialog = new(data, results);
dialog.Show();
}
catch (Exception ex)
Copy link
Contributor

Choose a reason for hiding this comment

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

can we somehow trickle the exception type further instead of just having a catchall?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I understand we can't. This function is called directly from a button in XAML, so if we don't catch it here the application will crash.
This isn't a silent catchall and it should never be reached unless there is a bug. It is only here to stop the application from crashing if there is a bug, which is exactly what happens in #1946 and is way worse than the search just failing and showing an error message.

{
mainWindow.ShowError("An error occured in the object references related window.\n" +
$"Please report this on GitHub.\n\n{ex}");
dialog?.Close();
}
}

Close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ public static class UndertaleResourceReferenceMap
(typeof(UndertaleGameObject), "Game objects"),
(typeof(UndertaleGeneralInfo), "General info"),
(typeof(UndertaleOptions.Constant), "Game options constants"),
(typeof(UndertaleLanguage), "Languages"),
(typeof(UndertalePath), "Paths"),
(typeof(UndertaleRoom), "Rooms"),
(typeof(UndertaleScript), "Scripts"),
Expand All @@ -179,6 +178,15 @@ public static class UndertaleResourceReferenceMap
}
},
new TypesForVersion
{
// Bytecode version 16
Version = (16, uint.MaxValue, uint.MaxValue),
Types = new[]
{
(typeof(UndertaleLanguage), "Languages"),
}
},
new TypesForVersion
{
Version = (2, 0, 0),
Types = new[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,15 +560,6 @@
outDict["Game options constants"] = new object[] { new GeneralInfoEditor(data.GeneralInfo, data.Options, data.Language) };
}

if (types.Contains(typeof(UndertaleLanguage)))
{
bool langsMatches = data.Language.EntryIDs.Contains(obj)
|| data.Language.Languages.Any(x => x.Name == obj || x.Region == obj
|| x.Entries.Contains(obj));
if (langsMatches)
outDict["Languages"] = new object[] { new GeneralInfoEditor(data.GeneralInfo, data.Options, data.Language) };
}

if (types.Contains(typeof(UndertalePath)))
{
var paths = data.Paths.Where(x => x.Name == obj);
Expand Down Expand Up @@ -632,6 +623,28 @@
}
},
new PredicateForVersion()
{
// Bytecode version 16
Version = (16, uint.MaxValue, uint.MaxValue),
Predicate = (objSrc, types, checkOne) =>
{
if (!types.Contains(typeof(UndertaleLanguage)))
return null;

if (objSrc is not UndertaleString obj)
return null;

bool langsMatches = data.Language.EntryIDs.Contains(obj)
|| data.Language.Languages.Any(x => x.Name == obj || x.Region == obj
|| x.Entries.Contains(obj));

if (langsMatches)
return new() { { "Languages", new object[] { new GeneralInfoEditor(data.GeneralInfo, data.Options, data.Language) } } };
else
return null;
}
},
new PredicateForVersion()
{
Version = (2, 0, 0),
Predicate = (objSrc, types, checkOne) =>
Expand Down Expand Up @@ -1442,7 +1455,7 @@
{
UndertaleResourceReferenceMethodsMap.data = data;

var ver = (data.GeneralInfo.Major, data.GeneralInfo.Minor, data.GeneralInfo.Release);

Check warning on line 1458 in UndertaleModTool/Windows/FindReferencesTypesDialog/UndertaleResourceReferenceMethodsMap.cs

View workflow job for this annotation

GitHub Actions / build_gui (windows-latest, Debug, false, true)

The variable 'ver' is assigned but its value is never used

Check warning on line 1458 in UndertaleModTool/Windows/FindReferencesTypesDialog/UndertaleResourceReferenceMethodsMap.cs

View workflow job for this annotation

GitHub Actions / build_gui (windows-latest, Debug, false, true)

The variable 'ver' is assigned but its value is never used

Check warning on line 1458 in UndertaleModTool/Windows/FindReferencesTypesDialog/UndertaleResourceReferenceMethodsMap.cs

View workflow job for this annotation

GitHub Actions / build_gui (windows-latest, Debug, false, false)

The variable 'ver' is assigned but its value is never used

Check warning on line 1458 in UndertaleModTool/Windows/FindReferencesTypesDialog/UndertaleResourceReferenceMethodsMap.cs

View workflow job for this annotation

GitHub Actions / build_gui (windows-latest, Debug, false, false)

The variable 'ver' is assigned but its value is never used

Dictionary<string, List<object>> outDict = new();

Expand Down Expand Up @@ -1566,23 +1579,17 @@
}
}
}

await mainWindow.StopProgressBarUpdater();
mainWindow.HideProgressBar();
}
catch
finally
{
await mainWindow.StopProgressBarUpdater();
mainWindow.HideProgressBar();

mainWindow.IsEnabled = true;
stringReferences = null;
funcReferences = null;
variReferences = null;

throw;
}
mainWindow.IsEnabled = true;
stringReferences = null;
funcReferences = null;
variReferences = null;

if (outDict.Count == 0)
return null;
Expand Down
Loading