Exemplo n.º 1
0
        private static int DisplayHelp(ParserResult <Options> result, IEnumerable <Error> errors)
        {
            Console.Error.WriteLine(HelpText.AutoBuild(result));

            var help = new HelpText();

            help.Heading                      = "Available plugins:";
            help.Copyright                    = string.Empty;
            help.AddDashesToOption            = false;
            help.AdditionalNewLineAfterOption = true;
            help.MaximumDisplayWidth          = 80;
            help.AutoHelp                     = false;
            help.AutoVersion                  = false;

            var pluginOptions = PluginOptions.GetPluginOptionTypes();

            if (pluginOptions.Any())
            {
                Console.Error.WriteLine(help.AddVerbs(PluginOptions.GetPluginOptionTypes()));
            }
            return(1);
        }
Exemplo n.º 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("");

            // 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())
                        {