Skip to content

Commit

Permalink
A fix of the object ID label. (#1004)
Browse files Browse the repository at this point in the history
* Fixed the object ID label for strings and code entries (and probably more types)

* Addressed @Miepee comments
  • Loading branch information
VladiStep committed Jun 26, 2022
1 parent 765cf57 commit ff2ec6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
68 changes: 27 additions & 41 deletions UndertaleModLib/UndertaleData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,22 @@ public class UndertaleData : IDisposable
/// </summary>
public bool GMLCacheIsReady { get; set; } = true;

/// <summary>
/// An array of a <see cref="UndertaleData"/> properties with <see cref="IList{T}"/> as their type.
/// </summary>
public PropertyInfo[] AllListProperties { get; private set; }

/// <summary>
/// Initializes new <see cref="UndertaleData"/> instance.
/// </summary>
public UndertaleData()
{
AllListProperties = GetType().GetProperties()
.Where(x => x.PropertyType.IsGenericType
&& x.PropertyType.GetGenericTypeDefinition() == typeof(IList<>))
.ToArray();
}

/// <summary>
/// Get a resource from the data file by name.
/// </summary>
Expand Down Expand Up @@ -411,49 +427,22 @@ public UndertaleNamedResource ByName(string name, bool ignoreCase = false)
}

/// <summary>
/// Reports the zero-based index of the first occurence of the specified <see cref="UndertaleNamedResource"/>.
/// Reports the zero-based index of the first occurence of the specified <see cref="UndertaleResource"/>.
/// </summary>
/// <param name="obj">The object to get the index of.</param>
/// <param name="panicIfInvalid">Whether to throw if <paramref name="obj"/> is not a valid object.</param>
/// <returns>The zero-based index position of the <paramref name="obj"/> parameter if it is found or -2 if it is not.</returns>
/// <exception cref="InvalidOperationException"><paramref name="panicIfInvalid"/> is <see langword="true"/>
/// and <paramref name="obj"/> could not be found.</exception>
public int IndexOf(UndertaleNamedResource obj, bool panicIfInvalid = true)
public int IndexOf(UndertaleResource obj, bool panicIfInvalid = true)
{
if (obj is UndertaleSound)
return Sounds.IndexOf(obj as UndertaleSound);
if (obj is UndertaleSprite)
return Sprites.IndexOf(obj as UndertaleSprite);
if (obj is UndertaleBackground)
return Backgrounds.IndexOf(obj as UndertaleBackground);
if (obj is UndertalePath)
return Paths.IndexOf(obj as UndertalePath);
if (obj is UndertaleScript)
return Scripts.IndexOf(obj as UndertaleScript);
if (obj is UndertaleFont)
return Fonts.IndexOf(obj as UndertaleFont);
if (obj is UndertaleGameObject)
return GameObjects.IndexOf(obj as UndertaleGameObject);
if (obj is UndertaleRoom)
return Rooms.IndexOf(obj as UndertaleRoom);
if (obj is UndertaleExtension)
return Extensions.IndexOf(obj as UndertaleExtension);
if (obj is UndertaleShader)
return Shaders.IndexOf(obj as UndertaleShader);
if (obj is UndertaleTimeline)
return Timelines.IndexOf(obj as UndertaleTimeline);
if (obj is UndertaleAnimationCurve)
return AnimationCurves.IndexOf(obj as UndertaleAnimationCurve);
if (obj is UndertaleSequence)
return Sequences.IndexOf(obj as UndertaleSequence);
if (obj is UndertaleEmbeddedAudio)
return EmbeddedAudio.IndexOf(obj as UndertaleEmbeddedAudio);
if (obj is UndertaleEmbeddedTexture)
return EmbeddedTextures.IndexOf(obj as UndertaleEmbeddedTexture);
if (obj is UndertaleTexturePageItem)
return TexturePageItems.IndexOf(obj as UndertaleTexturePageItem);
if (obj is UndertaleAudioGroup)
return AudioGroups.IndexOf(obj as UndertaleAudioGroup);
Type objType = obj.GetType();
PropertyInfo objListPropInfo = AllListProperties.FirstOrDefault(x => x.PropertyType.GetGenericArguments()[0] == objType);
if (objListPropInfo is not null)
{
if (objListPropInfo.GetValue(this) is IList list)
return list.IndexOf(obj);
}

if (panicIfInvalid)
throw new InvalidOperationException();
Expand Down Expand Up @@ -614,12 +603,9 @@ public void Dispose()
{
GC.SuppressFinalize(this);

Type listType = typeof(IList<>);
Type disposableType = typeof(IDisposable);
PropertyInfo[] dataProperties = GetType().GetProperties();
var dataListProperties = dataProperties.Where(x => x.PropertyType.IsGenericType
&& x.PropertyType.GetGenericTypeDefinition() == listType);
var dataDisposableProps = dataProperties.Except(dataListProperties)
var dataDisposableProps = dataProperties.Except(AllListProperties)
.Where(x => disposableType.IsAssignableFrom(x.PropertyType));

// Dispose disposable properties
Expand All @@ -633,7 +619,7 @@ public void Dispose()
}

// Clear all object lists (sprites, code, etc.)
foreach (PropertyInfo dataListProperty in dataListProperties)
foreach (PropertyInfo dataListProperty in AllListProperties)
{
// If list is null
if (dataListProperty.GetValue(this) is not IList list)
Expand Down
13 changes: 11 additions & 2 deletions UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3081,8 +3081,17 @@ private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)

public void UpdateObjectLabel(object obj)
{
int foundIndex = obj is UndertaleNamedResource ? Data.IndexOf(obj as UndertaleNamedResource, false) : -1;
SetIDString(foundIndex == -1 ? "None" : (foundIndex == -2 ? "N/A" : Convert.ToString(foundIndex)));
int foundIndex = obj is UndertaleResource res ? Data.IndexOf(res, false) : -1;
string idString;

if (foundIndex == -1)
idString = "None";
else if (foundIndex == -2)
idString = "N/A";
else
idString = Convert.ToString(foundIndex);

SetIDString(idString);
}

public void HighlightObject(object obj, bool silent = true)
Expand Down

0 comments on commit ff2ec6e

Please sign in to comment.