Reduce binary size

This commit is contained in:
ds5678 2025-11-29 15:21:36 -08:00
parent 4707fb5a60
commit 8625724d72
5 changed files with 20 additions and 161 deletions

View File

@ -1,12 +1,11 @@
#nullable disable
using AssetRipper.Assets;
using AssetRipper.Assets.Metadata;
using System.Collections;
namespace AssetRipper.AssemblyDumper.InjectedTypes;
internal abstract class FetchDependenciesEnumerableBase<T> : IEnumerable<(string, PPtr)>, IEnumerator<(string, PPtr)> where T : IUnityAssetBase
internal abstract class FetchDependenciesEnumerableBase : IEnumerable<(string, PPtr)>, IEnumerator<(string, PPtr)>
{
private bool _hasBeenUsed;
@ -14,11 +13,8 @@ internal abstract class FetchDependenciesEnumerableBase<T> : IEnumerable<(string
private readonly int _initialThreadId;
protected readonly T _this;
public FetchDependenciesEnumerableBase(T @this)
protected FetchDependenciesEnumerableBase()
{
_this = @this;
_initialThreadId = Environment.CurrentManagedThreadId;
}
@ -32,7 +28,7 @@ internal abstract class FetchDependenciesEnumerableBase<T> : IEnumerable<(string
public IEnumerator<(string, PPtr)> GetEnumerator()
{
FetchDependenciesEnumerableBase<T> result;
FetchDependenciesEnumerableBase result;
if (!_hasBeenUsed && _initialThreadId == Environment.CurrentManagedThreadId)
{
result = this;
@ -51,7 +47,7 @@ internal abstract class FetchDependenciesEnumerableBase<T> : IEnumerable<(string
void IEnumerator.Reset() => throw new NotSupportedException();
private protected abstract FetchDependenciesEnumerableBase<T> CreateNew();
private protected abstract FetchDependenciesEnumerableBase CreateNew();
}
#nullable enable

View File

@ -1,139 +0,0 @@
#nullable disable
using AssetRipper.Assets;
using AssetRipper.Assets.Generics;
using AssetRipper.Assets.Metadata;
namespace AssetRipper.AssemblyDumper.InjectedTypes;
internal sealed class FetchDependenciesExampleObject : UnityObjectBase
{
private PPtr<IUnityObjectBase> field0;
private PPtr<IUnityObjectBase> field1;
private AssetList<PPtr<IUnityObjectBase>> field2;
private AssetDictionary<PPtr<IUnityObjectBase>, PPtr<IUnityObjectBase>> field3;
private AssetList<AssetList<PPtr<IUnityObjectBase>>> field4;
private AssetPair<PPtr<IUnityObjectBase>, PPtr<IUnityObjectBase>> field5;
public FetchDependenciesExampleObject(AssetInfo assetInfo) : base(assetInfo)
{
}
public new IEnumerable<(string, PPtr)> FetchDependencies()
{
return new FetchDependenciesEnumerable(this);
}
internal sealed class FetchDependenciesEnumerable : FetchDependenciesEnumerableBase<FetchDependenciesExampleObject>
{
private int _index;
private int _field2_0_i;
private int _field3_0_i;
private bool _field3_0_value;
private int _field4_0_i;
private int _field4_1_i;
private bool _field5_value;
public FetchDependenciesEnumerable(FetchDependenciesExampleObject @this) : base(@this)
{
}
private protected override FetchDependenciesEnumerableBase<FetchDependenciesExampleObject> CreateNew()
{
return new FetchDependenciesEnumerable(_this);
}
public override bool MoveNext()
{
switch (_index)
{
case 0:
_current = (nameof(_this.field0), _this.field0);
_index++;
break;
case 1:
_current = (nameof(_this.field1), _this.field1);
_index++;
break;
case 2:
if (_field2_0_i < _this.field2.Count)
{
_current = ("field2[]", _this.field2[_field2_0_i]);
_field2_0_i++;
}
else
{
_index++;
goto case 3;
}
break;
case 3:
if (_field3_0_i < _this.field3.Count)
{
if (!_field3_0_value)
{
_current = ("field3[].Key", _this.field3.GetPair(_field3_0_i).Key);
_field3_0_value = true;
}
else
{
_current = ("field3[].Value", _this.field3.GetPair(_field3_0_i).Value);
_field3_0_value = false;
_field3_0_i++;
}
}
else
{
_index++;
goto case 4;
}
break;
case 4:
if (_field4_0_i < _this.field4.Count)
{
if (_field4_1_i < _this.field4[_field4_0_i].Count)
{
_current = ("field4[][]", _this.field4[_field4_0_i][_field4_1_i]);
_field4_1_i++;
}
else
{
_field4_0_i++;
_field4_1_i = 0;
goto case 4;
}
}
else
{
_index++;
goto case 5;
}
break;
case 5:
if (!_field5_value)
{
_current = ("field5.Key", _this.field5.Key);
_field5_value = true;
}
else
{
_current = ("field5.Value", _this.field5.Value);
_field5_value = false;
_index++;
}
break;
default:
return false;
}
return true;
}
}
}
#nullable enable

View File

@ -26,7 +26,7 @@ internal static class WalkingHelper
{
walker.VisitPrimitive(pair.Key);
walker.DividePair(pair);
component.WalkRelease(walker);
component.WalkEditor(walker);
walker.ExitPair(pair);
}
}
@ -38,7 +38,7 @@ internal static class WalkingHelper
{
walker.VisitPrimitive(pair.Key);
walker.DividePair(pair);
component.WalkRelease(walker);
component.WalkStandard(walker);
walker.ExitPair(pair);
}
}

View File

@ -11,9 +11,9 @@ public static partial class Pass103_FillDependencyMethods
{
public static void DoPass()
{
TypeDefinition injectedBaseType = SharedState.Instance.InjectHelperType(typeof(FetchDependenciesEnumerableBase<>));
TypeDefinition injectedBaseType = SharedState.Instance.InjectHelperType(typeof(FetchDependenciesEnumerableBase));
FieldDefinition currentField = injectedBaseType.Fields.First(t => t.Name == "_current");
FieldDefinition thisField = injectedBaseType.Fields.First(t => t.Name == "_this");
MethodDefinition baseConstructor = injectedBaseType.GetDefaultConstructor();
ITypeDefOrRef commonPPtrTypeRef = SharedState.Instance.Importer.ImportType(typeof(PPtr));
ITypeDefOrRef ienumerableRef = SharedState.Instance.Importer.ImportType(typeof(IEnumerable<>));
@ -39,7 +39,7 @@ public static partial class Pass103_FillDependencyMethods
CilInstructionCollection instructions = method.GetInstructions();
if (anyPPtrs)
{
MethodDefinition enumerableConstructor = MakeEnumerableType(injectedBaseType, currentField, thisField, instance, rootNode);
MethodDefinition enumerableConstructor = MakeEnumerableType(injectedBaseType, currentField, baseConstructor, instance, rootNode);
instructions.Add(CilOpCodes.Ldarg_0);
instructions.Add(CilOpCodes.Newobj, enumerableConstructor);
instructions.Add(CilOpCodes.Ret);
@ -53,24 +53,26 @@ public static partial class Pass103_FillDependencyMethods
}
}
private static MethodDefinition MakeEnumerableType(TypeDefinition injectedBaseType, FieldDefinition currentField, FieldDefinition thisField, GeneratedClassInstance instance, TypeNode rootNode)
private static MethodDefinition MakeEnumerableType(TypeDefinition injectedBaseType, FieldDefinition currentField, MethodDefinition baseConstructor, GeneratedClassInstance instance, TypeNode rootNode)
{
ITypeDefOrRef baseType = injectedBaseType.MakeGenericInstanceType(instance.Type.ToTypeSignature()).ToTypeDefOrRef();
TypeDefinition enumerableType = new(
null,
"FetchDependenciesEnumerable",
TypeAttributes.NestedPrivate | TypeAttributes.Sealed,
baseType);
injectedBaseType);
instance.Type.NestedTypes.Add(enumerableType);
FieldDefinition thisField = enumerableType.AddField("_this", instance.Type.ToTypeSignature(), false, Visibility.Private);
MethodDefinition enumerableConstructor = enumerableType.AddEmptyConstructor();
enumerableConstructor.AddParameter(instance.Type.ToTypeSignature(), "_this");
{
CilInstructionCollection instructions = enumerableConstructor.GetInstructions();
instructions.Add(CilOpCodes.Ldarg_0);
instructions.Add(CilOpCodes.Ldarg_1);
IMethodDefOrRef baseConstructor = new MemberReference(baseType, ".ctor", injectedBaseType.GetConstructor(1).Signature);
instructions.Add(CilOpCodes.Call, baseConstructor);
instructions.Add(CilOpCodes.Ldarg_0);
instructions.Add(CilOpCodes.Ldarg_1);
instructions.Add(CilOpCodes.Stfld, thisField);
instructions.Add(CilOpCodes.Ret);
}
@ -80,7 +82,7 @@ public static partial class Pass103_FillDependencyMethods
{
CilInstructionCollection instructions = createNewMethod.GetInstructions();
instructions.Add(CilOpCodes.Ldarg_0);
instructions.Add(CilOpCodes.Ldfld, new MemberReference(baseType, thisField.Name, thisField.Signature));
instructions.Add(CilOpCodes.Ldfld, thisField);
instructions.Add(CilOpCodes.Newobj, enumerableConstructor);
instructions.Add(CilOpCodes.Ret);
}
@ -93,8 +95,8 @@ public static partial class Pass103_FillDependencyMethods
{
Processor = moveNextMethod.GetInstructions(),
Type = enumerableType,
CurrentField = new MemberReference(baseType, currentField.Name, currentField.Signature),
ThisField = new MemberReference(baseType, thisField.Name, thisField.Signature),
CurrentField = currentField,
ThisField = thisField,
};
TypeNodeHelper.ApplyAsRoot(rootNode, context);
context.Processor.OptimizeMacros();

View File

@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="AssetRipper.Checksum" Version="1.1.0" />
<PackageReference Include="AssetRipper.SourceGenerated" Version="1.3.7" />
<PackageReference Include="AssetRipper.SourceGenerated" Version="1.3.7.1" />
</ItemGroup>
</Project>