示例#1
0
        public MainWindow()
        {
            InitializeComponent();

            // Subscribe to status update events
            ((App)Application.Current).OnStatusUpdate += OnStatusUpdate;

            // Find Unity paths
            var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            txtUnityPath.Text       = Utils.FindPath($@"{programFiles}\Unity\Hub\Editor\*") ?? "<not set>";
            txtUnityScriptPath.Text = Utils.FindPath($@"{programFiles}\Unity\Hub\Editor\*\Editor\Data\Resources\PackageManager\ProjectTemplates\libcache\com.unity.template.3d-*\ScriptAssemblies") ?? "<not set>";

            // Populate script target combo box and select IDA by default
            cboPyTarget.ItemsSource  = PythonScript.GetAvailableTargets();
            cboPyTarget.SelectedItem = (cboPyTarget.ItemsSource as IEnumerable <string>).First(x => x == "IDA");
        }
示例#2
0
        private static int Run(Options options)
        {
            // Banner
            var asmInfo = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location);

            Console.WriteLine(asmInfo.ProductName);
            Console.WriteLine("Version " + asmInfo.ProductVersion);
            Console.WriteLine(asmInfo.LegalCopyright);
            Console.WriteLine("");

            // Check script target is valid
            if (!PythonScript.GetAvailableTargets().Contains(options.ScriptTarget))
            {
                Console.Error.WriteLine($"Script target {options.ScriptTarget} is invalid.");
                Console.Error.WriteLine("Valid targets are: " + string.Join(", ", PythonScript.GetAvailableTargets()));
                return(1);
            }

            // Check excluded namespaces
            if (options.ExcludedNamespaces.Count() == 1 && options.ExcludedNamespaces.First().ToLower() == "none")
            {
                options.ExcludedNamespaces = new List <string>();
            }

            // Creating a Visual Studio solution requires Unity assembly references
            var unityPath           = string.Empty;
            var unityAssembliesPath = string.Empty;

            if (options.CreateSolution)
            {
                unityPath           = Utils.FindPath(options.UnityPath);
                unityAssembliesPath = Utils.FindPath(options.UnityAssembliesPath);

                if (!Directory.Exists(unityPath))
                {
                    Console.Error.WriteLine($"Unity path {unityPath} does not exist");
                    return(1);
                }

                string editorPathSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
                    ? @"/Contents/Managed/UnityEditor.dll"
                    : @"\Editor\Data\Managed\UnityEditor.dll";

                if (!File.Exists(unityPath + editorPathSuffix))
                {
                    Console.Error.WriteLine($"No Unity installation found at {unityPath}");
                    return(1);
                }

                if (!Directory.Exists(unityAssembliesPath))
                {
                    Console.Error.WriteLine($"Unity assemblies path {unityAssembliesPath} does not exist");
                    return(1);
                }

                string uiDllPath = Path.Combine(unityAssembliesPath, "UnityEngine.UI.dll");
                if (!File.Exists(uiDllPath))
                {
                    Console.Error.WriteLine($"No UnityEngine.UI.dll assemblies found at {uiDllPath}");
                    return(1);
                }

                Console.WriteLine("Using Unity editor at " + unityPath);
                Console.WriteLine("Using Unity assemblies at " + unityAssembliesPath);
            }

            // Check that specified binary files exist
            foreach (var file in options.BinaryFiles)
            {
                if (!File.Exists(file))
                {
                    Console.Error.WriteLine($"File {file} does not exist");
                    return(1);
                }
            }

            // Check files exist and determine whether they're archives or not
            List <Il2CppInspector> il2cppInspectors;

            using (new Benchmark("Analyze IL2CPP data")) {
                try {
                    il2cppInspectors = Il2CppInspector.LoadFromPackage(options.BinaryFiles);
                }
                catch (Exception ex) {
                    Console.Error.WriteLine(ex.Message);
                    return(1);
                }

                if (il2cppInspectors == null)
                {
                    if (!File.Exists(options.MetadataFile))
                    {
                        Console.Error.WriteLine($"File {options.MetadataFile} does not exist");
                        return(1);
                    }

                    try {
                        il2cppInspectors = Il2CppInspector.LoadFromFile(options.BinaryFiles.First(), options.MetadataFile);
                    }
                    catch (Exception ex) {
                        Console.Error.WriteLine(ex.Message);
                        return(1);
                    }
                }
            }

            if (il2cppInspectors == null)
            {
                Environment.Exit(1);
            }

            // Write output files for each binary
            int imageIndex = 0;

            foreach (var il2cpp in il2cppInspectors)
            {
                Console.WriteLine($"Processing image {imageIndex} - {il2cpp.BinaryImage.Arch} / {il2cpp.BinaryImage.Bits}-bit");

                // Create model
                TypeModel model;
                using (new Benchmark("Create .NET type model"))
                    model = new TypeModel(il2cpp);

                AppModel appModel;
                using (new Benchmark("Create C++ application model")) {
                    appModel = new AppModel(model, makeDefaultBuild: false).Build(options.UnityVersion, options.CppCompiler);
                }

                // C# signatures output
                using (new Benchmark("Generate C# code")) {
                    var writer = new CSharpCodeStubs(model)
                    {
                        ExcludedNamespaces = options.ExcludedNamespaces.ToList(),
                        SuppressMetadata   = options.SuppressMetadata,
                        MustCompile        = options.MustCompile
                    };

                    var csOut = getOutputPath(options.CSharpOutPath, "cs", imageIndex);

                    if (options.CreateSolution)
                    {
                        writer.WriteSolution(csOut, unityPath, unityAssembliesPath);
                    }

                    else
                    {
                        switch (options.LayoutSchema.ToLower(), options.SortOrder.ToLower())
                        {
示例#3
0
        private static int Run(Options options)
        {
            // Banner
            var asmInfo = FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetEntryAssembly().Location);

            Console.WriteLine(asmInfo.ProductName);
            Console.WriteLine("Version " + asmInfo.ProductVersion);
            Console.WriteLine(asmInfo.LegalCopyright);
            Console.WriteLine("");

            // Safe plugin manager load
            try {
                PluginManager.EnsureInit();
            }
            catch (Exception ex) when(ex is InvalidOperationException || ex is DirectoryNotFoundException)
            {
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(1);
            }

            // Check plugin options are valid
            if (!PluginOptions.ParsePluginOptions(options.PluginOptions, PluginOptions.GetPluginOptionTypes()))
            {
                return(1);
            }

            // Check script target is valid
            if (!PythonScript.GetAvailableTargets().Contains(options.ScriptTarget))
            {
                Console.Error.WriteLine($"Script target {options.ScriptTarget} is invalid.");
                Console.Error.WriteLine("Valid targets are: " + string.Join(", ", PythonScript.GetAvailableTargets()));
                return(1);
            }

            // Set load options
            var loadOptions = new LoadOptions {
                BinaryFilePath = options.BinaryFiles.First()
            };

            // Check image base
            if (!string.IsNullOrEmpty(options.ElfImageBaseString))
            {
                try {
                    loadOptions.ImageBase = Convert.ToUInt64(options.ElfImageBaseString, 16);
                } catch (Exception ex) when(ex is ArgumentException || ex is FormatException || ex is OverflowException)
                {
                    Console.Error.WriteLine("Image base must be a 32 or 64-bit hex value (optionally starting with '0x')");
                    return(1);
                }
            }

            // Check Unity asset
            if (options.UnityVersionAsset != null)
            {
                try {
                    options.UnityVersion = UnityVersion.FromAssetFile(options.UnityVersionAsset);

                    Console.WriteLine("Unity asset file has version " + options.UnityVersion);
                }
                catch (FileNotFoundException) {
                    Console.Error.WriteLine($"Unity asset file {options.UnityVersionAsset} does not exist");
                    return(1);
                } catch (ArgumentException) {
                    Console.Error.WriteLine("Could not determine Unity version from asset file - ignoring");
                }
            }

            // Check excluded namespaces
            if (options.ExcludedNamespaces.Count() == 1 && options.ExcludedNamespaces.First().ToLower() == "none")
            {
                options.ExcludedNamespaces = new List <string>();
            }

            // Creating a Visual Studio solution requires Unity assembly references
            var unityPath           = string.Empty;
            var unityAssembliesPath = string.Empty;

            if (options.CreateSolution)
            {
                unityPath           = Utils.FindPath(options.UnityPath);
                unityAssembliesPath = Utils.FindPath(options.UnityAssembliesPath);

                if (!Directory.Exists(unityPath))
                {
                    Console.Error.WriteLine($"Unity path {unityPath} does not exist");
                    return(1);
                }

                string editorPathSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
                    ? @"/Contents/Managed/UnityEditor.dll"
                    : @"\Editor\Data\Managed\UnityEditor.dll";

                if (!File.Exists(unityPath + editorPathSuffix))
                {
                    Console.Error.WriteLine($"No Unity installation found at {unityPath}");
                    return(1);
                }

                if (!Directory.Exists(unityAssembliesPath))
                {
                    Console.Error.WriteLine($"Unity assemblies path {unityAssembliesPath} does not exist");
                    return(1);
                }

                string uiDllPath = Path.Combine(unityAssembliesPath, "UnityEngine.UI.dll");
                if (!File.Exists(uiDllPath))
                {
                    Console.Error.WriteLine($"No UnityEngine.UI.dll assemblies found at {uiDllPath}");
                    return(1);
                }

                Console.WriteLine("Using Unity editor at " + unityPath);
                Console.WriteLine("Using Unity assemblies at " + unityAssembliesPath);
            }

            // Set plugin handlers
            PluginManager.ErrorHandler += (s, e) => {
                Console.Error.WriteLine($"The plugin {e.Error.Plugin.Name} encountered an error while executing {e.Error.Operation}: {e.Error.Exception.Message}."
                                        + " Plugin has been disabled.");
            };

            PluginManager.StatusHandler += (s, e) => {
                Console.WriteLine("Plugin " + e.Plugin.Name + ": " + e.Text);
            };

            // Check that specified binary files exist
            foreach (var file in options.BinaryFiles)
            {
                if (!File.Exists(file))
                {
                    Console.Error.WriteLine($"File {file} does not exist");
                    return(1);
                }
            }

            // Check files exist and determine whether they're archives or not
            bool isExtractedFromPackage = false;
            List <Il2CppInspector> il2cppInspectors;

            using (new Benchmark("Analyze IL2CPP data")) {
                try {
                    il2cppInspectors       = Il2CppInspector.LoadFromPackage(options.BinaryFiles, loadOptions);
                    isExtractedFromPackage = true;
                }
                catch (Exception ex) {
                    Console.Error.WriteLine(ex.Message);
                    return(1);
                }

                if (il2cppInspectors == null)
                {
                    isExtractedFromPackage = false;

                    if (!File.Exists(options.MetadataFile))
                    {
                        Console.Error.WriteLine($"File {options.MetadataFile} does not exist");
                        return(1);
                    }

                    try {
                        il2cppInspectors = Il2CppInspector.LoadFromFile(options.BinaryFiles.First(), options.MetadataFile, loadOptions);
                    }
                    catch (Exception ex) {
                        Console.Error.WriteLine(ex.Message);
                        return(1);
                    }
                }
            }

            if (il2cppInspectors == null)
            {
                Environment.Exit(1);
            }

            // Save metadata and binary if extracted or modified and save requested
            if (!string.IsNullOrEmpty(options.MetadataFileOut))
            {
                if (isExtractedFromPackage || il2cppInspectors[0].Metadata.IsModified)
                {
                    Console.WriteLine($"Saving metadata file to {options.MetadataFileOut}");

                    il2cppInspectors[0].SaveMetadataToFile(options.MetadataFileOut);
                }
                else
                {
                    Console.WriteLine("Metadata file was not modified - skipping save");
                }
            }

            if (!string.IsNullOrEmpty(options.BinaryFileOut))
            {
                var outputIndex = 0;
                foreach (var il2cpp in il2cppInspectors)
                {
                    // If there's an extension, strip the leading period
                    var ext = Path.GetExtension(options.BinaryFileOut);
                    if (ext.Length > 0)
                    {
                        ext = ext.Substring(1);
                    }
                    var outPath = getOutputPath(options.BinaryFileOut, ext, outputIndex);

                    if (isExtractedFromPackage || il2cpp.Binary.IsModified)
                    {
                        Console.WriteLine($"Saving binary file to {outPath}");

                        il2cpp.SaveBinaryToFile(outPath);
                    }
                    else
                    {
                        Console.WriteLine("Binary file was not modified - skipping save");
                    }

                    outputIndex++;
                }
            }

            // Write output files for each binary
            int imageIndex = 0;

            foreach (var il2cpp in il2cppInspectors)
            {
                Console.WriteLine($"Processing image {imageIndex} - {il2cpp.BinaryImage.Arch} / {il2cpp.BinaryImage.Bits}-bit");

                // Create model
                TypeModel model;
                using (new Benchmark("Create .NET type model"))
                    model = new TypeModel(il2cpp);

                AppModel appModel;
                using (new Benchmark("Create C++ application model")) {
                    appModel = new AppModel(model, makeDefaultBuild: false).Build(options.UnityVersion, options.CppCompiler);
                }

                // C# signatures output
                using (new Benchmark("Generate C# code")) {
                    var writer = new CSharpCodeStubs(model)
                    {
                        ExcludedNamespaces = options.ExcludedNamespaces.ToList(),
                        SuppressMetadata   = options.SuppressMetadata,
                        MustCompile        = options.MustCompile
                    };

                    var csOut = getOutputPath(options.CSharpOutPath, "cs", imageIndex);

                    if (options.CreateSolution)
                    {
                        writer.WriteSolution(csOut, unityPath, unityAssembliesPath);
                    }

                    else
                    {
                        switch (options.LayoutSchema.ToLower(), options.SortOrder.ToLower())
                        {
示例#4
0
        private void runTest(string testPath)
        {
            // Android
            var testFile = testPath + @"\" + Path.GetFileName(testPath) + ".so";

            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\libil2cpp.so";
            }
            // Windows
            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\" + Path.GetFileName(testPath) + ".dll";
            }
            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\GameAssembly.dll";
            }
            // iOS
            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\" + Path.GetFileName(testPath);
            }

            var inspectors = Il2CppInspector.LoadFromFile(testFile, testPath + @"\global-metadata.dat");

            // If null here, there was a problem parsing the files
            if (inspectors == null)
            {
                throw new Exception("Could not understand IL2CPP binary or metadata");
            }

            if (inspectors.Count == 0)
            {
                throw new Exception("Could not find any images in the IL2CPP binary");
            }

            // Dump each image in the binary separately
            int i = 0;

            foreach (var il2cpp in inspectors)
            {
                var model      = new TypeModel(il2cpp);
                var appModel   = new AppModel(model, makeDefaultBuild: false).Build(compiler: CppCompilerType.MSVC);
                var nameSuffix = i++ > 0 ? "-" + (i - 1) : "";

                new CSharpCodeStubs(model)
                {
                    ExcludedNamespaces = Constants.DefaultExcludedNamespaces,
                    SuppressMetadata   = false,
                    MustCompile        = true
                }.WriteSingleFile(testPath + $@"\test-result{nameSuffix}.cs");

                new JSONMetadata(appModel)
                .Write(testPath + $@"\test-result{nameSuffix}.json");

                new CppScaffolding(appModel)
                .Write(testPath + $@"\test-cpp-result{nameSuffix}");

                var python = new PythonScript(appModel);
                foreach (var target in PythonScript.GetAvailableTargets())
                {
                    python.WriteScriptToFile(testPath + $@"\test-{target.ToLower()}{nameSuffix}.py", target,
                                             testPath + $@"\test-cpp-result{nameSuffix}\appdata\il2cpp-types.h",
                                             testPath + $@"\test-result{nameSuffix}.json");
                }
            }

            // Compare test results with expected results
            for (i = 0; i < inspectors.Count; i++)
            {
                var suffix = (i > 0 ? "-" + i : "");

                compareFiles(testPath, suffix + ".cs", $"test-result{suffix}.cs");
                compareFiles(testPath, suffix + ".json", $"test-result{suffix}.json");
                compareFiles(testPath, suffix + ".h", $@"test-cpp-result{suffix}\appdata\il2cpp-types.h");
            }
        }
示例#5
0
        // Test runner
        private async Task runTest(string testPath, LoadOptions loadOptions = null)
        {
            // Android
            var testFile = testPath + @"\" + Path.GetFileName(testPath) + ".so";

            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\libil2cpp.so";
            }
            // Windows
            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\" + Path.GetFileName(testPath) + ".dll";
            }
            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\GameAssembly.dll";
            }
            // iOS
            if (!File.Exists(testFile))
            {
                testFile = testPath + @"\" + Path.GetFileName(testPath);
            }
            // Linux process map
            if (!File.Exists(testFile))
            {
                testFile = Directory.GetFiles(testPath, "*-maps.txt").FirstOrDefault();
            }
            // XAPK (selects latest version assuming lexical order)
            if (testFile == null)
            {
                testFile = Directory.GetFiles(testPath, "*.xapk").LastOrDefault();
            }
            // APK (selects latest version assuming lexical order) (prefer XAPKs)
            if (testFile == null)
            {
                testFile = Directory.GetFiles(testPath, "*.apk").LastOrDefault();
            }

            // Set load options
            if (loadOptions == null)
            {
                loadOptions = new LoadOptions();
            }

            loadOptions.BinaryFilePath = testFile;

            // Load core plugins
            PluginManager.Reload(testPath + @"\..\plugins", coreOnly: true);

            // Set up additional plugins - place desired plugins in 'plugins' sub-folder of test folder
            try {
                PluginManager.Reload(testPath + @"\plugins", reset: false);
            } catch (DirectoryNotFoundException) { }

            // Handlers for debugging output
            PluginManager.StatusHandler += (s, e) => {
                Console.WriteLine("Plugin " + e.Plugin.Name + ": " + e.Text);
            };

            PluginManager.ErrorHandler += (s, e) => {
                Assert.Fail($"{e.Error.Plugin.Name} throw an exception during {e.Error.Operation}: {e.Error.Exception.Message}.", e);
            };

            // Get plugin options - place desired options in <plugins-id>.options.txt for each plugin in test folder
            // in the format "key=value", one per line
            // Use %TESTDIR% to refer to the directory containing the test files
            foreach (var plugin in PluginManager.AvailablePlugins)
            {
                Console.WriteLine("Using plugin: " + plugin.Name);

                // Enable plugin
                var ourPlugin = PluginManager.Plugins[plugin.Id];
                ourPlugin.Enabled = true;

                // Look for options
                var optionsFile = $@"{testPath}\{plugin.Id}-options.txt";
                if (File.Exists(optionsFile))
                {
                    var options = File.ReadAllLines(optionsFile);

                    // Parse options
                    foreach (var option in options)
                    {
                        var kv = option.Split('=', 2);
                        if (kv.Length == 2)
                        {
                            var key   = kv[0].Trim();
                            var value = kv[1].Trim().Replace("%TESTDIR%", testPath);

                            Console.WriteLine($"Setting option: {key} = {value}");

                            // Null default values must be castable to object
                            ourPlugin.Plugin.Options.Single(o => o.Name == key).SetFromString(value);
                        }
                    }
                }
                PluginManager.OptionsChanged(plugin);
            }

            List <Il2CppInspector> inspectors;

            using (new Benchmark("Load IL2CPP metadata and binary"))
                try {
                    inspectors = Il2CppInspector.LoadFromFile(testFile, testPath + @"\global-metadata.dat", loadOptions);
                } catch (FileNotFoundException) {
                    inspectors = Il2CppInspector.LoadFromPackage(new[] { testFile }, loadOptions);
                }

            // If null here, there was a problem parsing the files
            if (inspectors == null)
            {
                throw new Exception("Could not understand IL2CPP binary or metadata");
            }

            if (inspectors.Count == 0)
            {
                throw new Exception("Could not find any images in the IL2CPP binary");
            }

            // End if we were only testing file load
            if (!GenerateCpp && !GenerateCS && !GenerateDLL && !GenerateJSON && !GeneratePython)
            {
                return;
            }

            // Dump each image in the binary separately
            var imageTasks = inspectors.Select((il2cpp, i) => Task.Run(async() =>
            {
                TypeModel model;
                using (new Benchmark("Create .NET type model"))
                    model = new TypeModel(il2cpp);

                Task <AppModel> appModelTask = null;
                if (GenerateCpp || GenerateJSON || GeneratePython)
                {
                    appModelTask = Task.Run(() => {
                        using (new Benchmark("Create application model"))
                            return(new AppModel(model, makeDefaultBuild: false).Build(compiler: CppCompilerType.MSVC));
                    });
                }

                var nameSuffix    = i++ > 0 ? "-" + (i - 1) : "";
                var generateTasks = new List <Task>();
                var compareTasks  = new List <Task>();

                if (GenerateCS)
                {
                    generateTasks.Add(Task.Run(() => {
                        using (new Benchmark("Create C# code stubs"))
                            new CSharpCodeStubs(model)
                            {
                                ExcludedNamespaces = Constants.DefaultExcludedNamespaces,
                                SuppressMetadata   = false,
                                MustCompile        = true
                            }.WriteSingleFile(testPath + $@"\test-result{nameSuffix}.cs");

                        compareTasks.Add(Task.Run(() => compareFiles(testPath, nameSuffix + ".cs", $"test-result{nameSuffix}.cs")));
                    }));
                }

                if (GenerateDLL)
                {
                    generateTasks.Add(Task.Run(() => {
                        using (new Benchmark("Create .NET assembly shims"))
                            new AssemblyShims(model).Write(testPath + $@"\test-dll-result{nameSuffix}");

                        compareTasks.Add(Task.Run(() => compareBinaryFiles(testPath + $@"\test-dll-result{nameSuffix}",
                                                                           testPath + @"\..\..\TestExpectedResults\dll-" + Path.GetFileName(testPath) + nameSuffix)));
                    }));
                }

                AppModel appModel = null;
                if (appModelTask != null)
                {
                    appModel = await appModelTask;
                }

                if (GenerateJSON || GeneratePython)
                {
                    generateTasks.Add(Task.Run(() => {
                        using (new Benchmark("Create JSON metadata"))
                            new JSONMetadata(appModel)
                            .Write(testPath + $@"\test-result{nameSuffix}.json");

                        compareTasks.Add(Task.Run(() => compareFiles(testPath, nameSuffix + ".json", $"test-result{nameSuffix}.json")));
                    }));
                }

                if (GenerateCpp || GeneratePython)
                {
                    generateTasks.Add(Task.Run(() => {
                        using (new Benchmark("Create C++ scaffolding"))
                            new CppScaffolding(appModel)
                            .Write(testPath + $@"\test-cpp-result{nameSuffix}");

                        compareTasks.Add(Task.Run(() => compareFiles(testPath, nameSuffix + ".h", $@"test-cpp-result{nameSuffix}\appdata\il2cpp-types.h")));
                    }));
                }

                if (GeneratePython)
                {
                    generateTasks.Add(Task.Run(() => {
                        var python = new PythonScript(appModel);
                        foreach (var target in PythonScript.GetAvailableTargets())
                        {
                            python.WriteScriptToFile(testPath + $@"\test-{target.ToLower()}{nameSuffix}.py", target,
                                                     testPath + $@"\test-cpp-result{nameSuffix}\appdata\il2cpp-types.h",
                                                     testPath + $@"\test-result{nameSuffix}.json");
                        }
                    }));
                }

                await Task.WhenAll(generateTasks);
                await Task.WhenAll(compareTasks);
            }));
            await Task.WhenAll(imageTasks);
        }