Пример #1
0
 static void CreateUnityProject()
 {
     Project.Create();
     Simplify.SimplifyLib("Assembly-CSharp.dll", "simplified-kingmaker.dll");
     Simplify.SimplifyLib("Assembly-CSharp-FirstPass.dll", "simplified-kingmaker-FirstPass.dll");
     ILSpy.DecompileProject("simplified-kingmaker.dll", $"{ProjectDir}/Assets/Kingmaker");
     ILSpy.DecompileProject("simplified-kingmaker-firstPass.dll", $"{ProjectDir}/Assets/Standard Assets");
 }
        static void CreateUnityProject()
        {
            Project.Create();
            Simplify.RunTest("Assembly-CSharp.dll", "simplified-kingmaker.dll");
            Simplify.RunTest("Assembly-CSharp-FirstPass.dll", "simplified-kingmaker-FirstPass.dll");
            var outputDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            TestILSpy.RunTest("simplified-kingmaker.dll", $"{ProjectDir}/Assets/Kingmaker");
            TestILSpy.RunTest("simplified-kingmaker-firstPass.dll", $"{ProjectDir}/Assets/Standard Assets");
        }
        static void DecompileAll()
        {
            var unityAssemblies = new string[] {
                "Unity.Analytics.DataPrivacy.dll",
                "UnityEngine.AccessibilityModule.dll",
                "UnityEngine.AIModule.dll",
                "UnityEngine.Analytics.dll",
                "UnityEngine.AnimationModule.dll",
                "UnityEngine.ARModule.dll",
                "UnityEngine.AssetBundleModule.dll",
                "UnityEngine.AudioModule.dll",
                "UnityEngine.BaselibModule.dll",
                "UnityEngine.ClothModule.dll",
                "UnityEngine.CloudWebServicesModule.dll",
                "UnityEngine.ClusterInputModule.dll",
                "UnityEngine.ClusterRendererModule.dll",
                "UnityEngine.CoreModule.dll",
                "UnityEngine.CrashReportingModule.dll",
                "UnityEngine.DirectorModule.dll",
                "UnityEngine.dll",
                "UnityEngine.FacebookModule.dll",
                "UnityEngine.GameCenterModule.dll",
                "UnityEngine.GridModule.dll",
                "UnityEngine.HotReloadModule.dll",
                "UnityEngine.ImageConversionModule.dll",
                "UnityEngine.IMGUIModule.dll",
                "UnityEngine.InputModule.dll",
                "UnityEngine.JSONSerializeModule.dll",
                "UnityEngine.Networking.dll",
                "UnityEngine.ParticlesLegacyModule.dll",
                "UnityEngine.ParticleSystemModule.dll",
                "UnityEngine.PerformanceReportingModule.dll",
                "UnityEngine.Physics2DModule.dll",
                "UnityEngine.PhysicsModule.dll",
                "UnityEngine.ScreenCaptureModule.dll",
                "UnityEngine.SharedInternalsModule.dll",
                "UnityEngine.SpatialTracking.dll",
                "UnityEngine.SpatialTrackingModule.dll",
                "UnityEngine.SpriteMaskModule.dll",
                "UnityEngine.SpriteShapeModule.dll",
                "UnityEngine.StandardEvents.dll",
                "UnityEngine.StyleSheetsModule.dll",
                "UnityEngine.SubstanceModule.dll",
                "UnityEngine.TerrainModule.dll",
                "UnityEngine.TerrainPhysicsModule.dll",
                "UnityEngine.TextRenderingModule.dll",
                "UnityEngine.TilemapModule.dll",
                "UnityEngine.Timeline.dll",
                "UnityEngine.TimelineModule.dll",
                "UnityEngine.TLSModule.dll",
                "UnityEngine.UI.dll",
                "UnityEngine.UIElementsModule.dll",
                "UnityEngine.UIModule.dll",
                "UnityEngine.UmbraModule.dll",
                "UnityEngine.UNETModule.dll",
                "UnityEngine.UnityAnalyticsModule.dll",
                "UnityEngine.UnityConnectModule.dll",
                "UnityEngine.UnityWebRequestAssetBundleModule.dll",
                "UnityEngine.UnityWebRequestAudioModule.dll",
                "UnityEngine.UnityWebRequestModule.dll",
                "UnityEngine.UnityWebRequestTextureModule.dll",
                "UnityEngine.UnityWebRequestWWWModule.dll",
                "UnityEngine.VehiclesModule.dll",
                "UnityEngine.VideoModule.dll",
                "UnityEngine.VRModule.dll",
                "UnityEngine.WebModule.dll",
                "UnityEngine.WindModule.dll",
                "UnityEngine.XRModule.dll",
            };
            var thirdParty = new string[] {
                "AuraSupportPlugin.dll",
                "DemiLib.dll",
                "DOTween.dll",
                "DOTween46.dll",
                "DOTweenPro.dll",
                "Pathfinding.ClipperLib.dll",
                "Pathfinding.Ionic.Zip.Reduced.dll",
                "Pathfinding.JsonFx.dll",
                "Pathfinding.Poly2Tri.dll",
                "Kingmaker.Import.dll",
            };
            var mainAssemblies = new string[] {
                "Assembly-CSharp.dll",
                "Assembly-CSharp-FirstPass.dll"
            };

            foreach (var assembly in unityAssemblies)
            {
                Simplify.RunTest(assembly, $"simplified-{assembly}");
                TestILSpy.RunTest($"simplified-{assembly}", $"{ProjectDir}/UnityEngine");
            }
            foreach (var assembly in mainAssemblies)
            {
                Simplify.RunTest(assembly, $"simplified-{assembly}");
                TestILSpy.RunTest($"simplified-{assembly}", $"{ProjectDir}/{assembly.Replace(".dll", "")}");
            }
            foreach (var assembly in thirdParty)
            {
                Simplify.RunTest(assembly, $"simplified-{assembly}");
                TestILSpy.RunTest($"simplified-{assembly}", $"{ProjectDir}/ThirdParty");
            }
        }
Пример #4
0
        public static void SimplifyLib(string assemblyName, string outputPath)
        {
            var         simplify      = new Simplify();
            ModuleDefMD mod           = ModuleDefMD.Load(Path.Combine(Program.ManagedDir, assemblyName));
            var         structRef     = new TypeRefUser(mod, "System", "ValueType", mod.CorLibTypes.AssemblyRef);
            var         exceptionRef  = new TypeRefUser(mod, "System", "NotImplementedException", mod.CorLibTypes.AssemblyRef);
            var         exceptionCtor = new MemberRefUser(mod, ".ctor",
                                                          MethodSig.CreateInstance(exceptionRef.ToTypeSig()),
                                                          exceptionRef);

            simplify.NotImplementedBody = new List <Instruction>()
            {
                OpCodes.Nop.ToInstruction(),
                    OpCodes.Newobj.ToInstruction(exceptionCtor),
                OpCodes.Throw.ToInstruction()
            };
            Console.WriteLine($"Simplifying {assemblyName}");
            var AttributeBlackList = new HashSet <string>()
            {
                "System.Runtime.CompilerServices.AsyncStateMachineAttribute",
                "System.Diagnostics.DebuggerHiddenAttribute",
                "System.Security.SecurityCriticalAttribute"
            };
            var MethodWhiteList = new HashSet <string>()
            {
                "System.Void QuickGraph.SUndirectedTaggedEdge`2::add_TagChanged(System.EventHandler)",
                "System.Void QuickGraph.SUndirectedTaggedEdge`2::remove_TagChanged(System.EventHandler)",
                "System.Void QuickGraph.STaggedEquatableEdge`2::add_TagChanged(System.EventHandler)",
                "System.Void QuickGraph.STaggedEquatableEdge`2::remove_TagChanged(System.EventHandler)",
                "System.Void QuickGraph.STaggedEdge`2::add_TagChanged(System.EventHandler)",
                "System.Void QuickGraph.STaggedEdge`2::remove_TagChanged(System.EventHandler)",
                "System.Void Steamworks.CallResult`1::add_m_Func(Steamworks.CallResult`1/APIDispatchDelegate<T>)",
                "System.Void Steamworks.CallResult`1::remove_m_Func(Steamworks.CallResult`1/APIDispatchDelegate<T>)",
                "Kingmaker.Utility.Feet Kingmaker.Utility.FeetExtension::Feet(System.Int32)",
                "Kingmaker.UnitLogic.Mechanics.ContextValue Kingmaker.UnitLogic.Mechanics.ContextValue::op_Implicit(System.Int32)",
                "System.Boolean Kingmaker.UnitLogic.Customization.UnitCustomizationVariation::Equals(Kingmaker.UnitLogic.Customization.UnitCustomizationVariation)",
                "System.Boolean Kingmaker.UnitLogic.Customization.UnitCustomizationVariation::Equals(System.Object)",
            };
            var PropertyWhiteList = new HashSet <string>()
            {
                "System.String UnityEngine.CreateAssetMenuAttribute::menuName()",
                "System.String UnityEngine.CreateAssetMenuAttribute::ileName()",
                "System.Int32 UnityEngine.CreateAssetMenuAttribute::order()",
                "System.String UnityEngine.Bindings.NativeTypeAttribute::Header()",
                "Kingmaker.Blueprints.BlueprintComponent Kingmaker.Blueprints.BlueprintScriptableObject::ComponentsArray()",
                "UnityEngine.Color UnityEngine.Color::red()",
                "UnityEngine.Color UnityEngine.Color::green()",
                "UnityEngine.Color UnityEngine.Color::blue()",
                "UnityEngine.Color UnityEngine.Color::white()",
                "UnityEngine.Color UnityEngine.Color::black()",
                "UnityEngine.Color UnityEngine.Color::yellow()",
                "UnityEngine.Color UnityEngine.Color::cyan()",
                "UnityEngine.Color UnityEngine.Color::magenta()",
                "UnityEngine.Color UnityEngine.Color::gray()",
                "UnityEngine.Color UnityEngine.Color::grey()",
                "UnityEngine.Color UnityEngine.Color::clear()",
                "UnityEngine.Vector3 UnityEngine.Vector3::zero()",
                "UnityEngine.Vector3 UnityEngine.Vector3::one()",
                "UnityEngine.Vector3 UnityEngine.Vector3::up()",
                "UnityEngine.Vector3 UnityEngine.Vector3::down()",
                "UnityEngine.Vector3 UnityEngine.Vector3::left()",
                "UnityEngine.Vector3 UnityEngine.Vector3::right()",
                "UnityEngine.Vector3 UnityEngine.Vector3::forward()",
                "UnityEngine.Vector3 UnityEngine.Vector3::back()",
                "UnityEngine.Vector3 UnityEngine.Vector3::positiveInfinity()",
                "UnityEngine.Vector3 UnityEngine.Vector3::negativeInfinity()",
            };

            RemoveInterfaceProperties(mod);
            foreach (var type in mod.GetTypes())
            {
                foreach (var property in type.Properties.ToArray())
                {
                    /*
                     * Properties must be changed or preserved in unison
                     * removing a getter body while keeping the setter body causes the
                     * setter to become recursive
                     * PropertyName {
                     *  getter { throw new NotImplementedException() }
                     *  setter { PropertyName = value }
                     * }
                     * TODO: Look at replacing properties with default implemtation
                     * PropertyName { get; set; }
                     */
                    if (PropertyWhiteList.Contains(property.FullName))
                    {
                        continue;
                    }
                    var  getter        = property.GetMethod;
                    var  setter        = property.SetMethod;
                    bool canSkipGetter = getter == null || !getter.HasBody || getter.Body.Instructions.Count <= 3;
                    bool canSkipSetter = getter == null || !getter.HasBody || getter.Body.Instructions.Count <= 4;
                    if (canSkipGetter && canSkipSetter)
                    {
                        continue;
                    }
                    if (getter != null && getter.HasBody)
                    {
                        simplify.ClearMethod(getter);
                    }
                    if (setter != null && setter.HasBody)
                    {
                        simplify.ClearMethod(setter);
                    }
                }
                foreach (var method in type.Methods.ToArray())
                {
                    if (method.IsGetter || method.IsSetter)
                    {
                        continue;
                    }
                    if (MethodWhiteList.Contains(method.FullName))
                    {
                        continue;
                    }
                    foreach (var att in method.CustomAttributes.ToArray())
                    {
                        if (AttributeBlackList.Contains(att.TypeFullName))
                        {
                            method.CustomAttributes.Remove(att);
                        }
                    }
                    simplify.ClearMethod(method);
                }
            }
            simplify.ManualFixes(mod);
            mod.Write(Path.Combine(Program.ManagedDir, outputPath));
        }