Skip to content

Commit

Permalink
Some fixes and improvements:
Browse files Browse the repository at this point in the history
1) The "Find all references" feature also searches game objects in code, but only in single-argument functions (e.g. `instance_exists(id)`). The same for fonts.
2) Font and game objects references search in code also works for GM 2023.8.
3) Refactored the code.
  • Loading branch information
VladiStep committed Jan 3, 2024
1 parent edfa370 commit da1b697
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 119 deletions.
81 changes: 38 additions & 43 deletions UndertaleModLib/Decompiler/Decompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,7 @@ public ExpressionAssetRef(int encodedResourceIndex)
{
Type = UndertaleInstruction.DataType.Variable;

// Break down index - first 24 bits are the ID, the rest is the ref type
AssetIndex = encodedResourceIndex & 0xffffff;
AssetRefType = (RefType)(encodedResourceIndex >> 24);
(AssetIndex, AssetRefType) = DecodeResourceIndexAndType(encodedResourceIndex);
}

public ExpressionAssetRef(int resourceIndex, RefType resourceType)
Expand All @@ -641,46 +639,7 @@ public override string ToString(DecompileContext context)
{
if (context.GlobalContext.Data != null)
{
IList assetList = null;
switch (AssetRefType)
{
case RefType.Sprite:
assetList = (IList)context.GlobalContext.Data.Sprites;
break;
case RefType.Background:
assetList = (IList)context.GlobalContext.Data.Backgrounds;
break;
case RefType.Sound:
assetList = (IList)context.GlobalContext.Data.Sounds;
break;
case RefType.Font:
assetList = (IList)context.GlobalContext.Data.Fonts;
break;
case RefType.Path:
assetList = (IList)context.GlobalContext.Data.Paths;
break;
case RefType.Timeline:
assetList = (IList)context.GlobalContext.Data.Timelines;
break;
case RefType.Room:
assetList = (IList)context.GlobalContext.Data.Rooms;
break;
case RefType.Object:
assetList = (IList)context.GlobalContext.Data.GameObjects;
break;
case RefType.Shader:
assetList = (IList)context.GlobalContext.Data.Shaders;
break;
case RefType.AnimCurve:
assetList = (IList)context.GlobalContext.Data.AnimationCurves;
break;
case RefType.Sequence:
assetList = (IList)context.GlobalContext.Data.Sequences;
break;
case RefType.ParticleSystem:
assetList = (IList)context.GlobalContext.Data.ParticleSystems;
break;
}
IList assetList = GetAssetListFromType(context.GlobalContext.Data, AssetRefType);

if (assetList != null && AssetIndex >= 0 && AssetIndex < assetList.Count)
return ((UndertaleNamedResource)assetList[AssetIndex]).Name.Content;
Expand All @@ -707,6 +666,42 @@ internal override AssetIDType DoTypePropagation(DecompileContext context, AssetI
_ => throw new NotImplementedException($"Missing ref type {AssetRefType}")
};
}

public static (int resourceIndex, RefType resourceType) DecodeResourceIndexAndType(int encodedResourceIndex)
{
// Break down index - first 24 bits are the ID, the rest is the ref type
return (encodedResourceIndex & 0xffffff, (RefType)(encodedResourceIndex >> 24));
}
public static int DecodeResourceIndex(int encodedResourceIndex)
{
return encodedResourceIndex & 0xffffff;
}
public static RefType DecodeResourceType(int encodedResourceIndex)
{
return (RefType)(encodedResourceIndex >> 24);
}

public static IList GetAssetListFromType(UndertaleData data, RefType type)
{
object list = type switch
{
RefType.Sprite => data.Sprites,
RefType.Background => data.Backgrounds,
RefType.Sound => data.Sounds,
RefType.Font => data.Fonts,
RefType.Path => data.Paths,
RefType.Timeline => data.Timelines,
RefType.Room => data.Rooms,
RefType.Object => data.GameObjects,
RefType.Shader => data.Shaders,
RefType.AnimCurve => data.AnimationCurves,
RefType.Sequence => data.Sequences,
RefType.ParticleSystem => data.ParticleSystems,
_ => null
};

return list as IList;
}
}

// Represents an expression converted to one of another data type - makes no difference on high-level code.
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModTool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ private void DisposeGameData()
UpdateLayout();
Dispatcher.Invoke(() => { }, DispatcherPriority.ApplicationIdle);

UndertaleResourceReferenceMethodsMap.ClearFontFunctionList();
UndertaleResourceReferenceMethodsMap.ClearFunctionLists();

Data.Dispose();
Data = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ public static class UndertaleResourceReferenceMap
Version = (1, 0, 0),
Types = new[]
{
(typeof(UndertaleCode), "Code"),
(typeof(UndertaleRoom.GameObject), "Room object instance")
}
},
Expand Down
Loading

0 comments on commit da1b697

Please sign in to comment.