Example #1
0
        static void Help(CommandLineArguments arguments)
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("==== Available Arguments: ====");

            Console.ResetColor();

            Console.WriteLine("--help (-h)       | Displays this argument list");
            Console.WriteLine("                  |  Usage:  modicite --help");
            Console.WriteLine("--decompile (-d)  | Decompiles the Magicite resources and assemblies");
            Console.WriteLine("                  |  Usage:  modicite --decompile <gameDir> <outputDir>");
            Console.WriteLine("--version (-V)    | Specifies the version of Magicite which is to be decompiled");
            Console.WriteLine("                  |  Usage:  modicite --decompile [--version=<ver>] <gameDir>");
            Console.WriteLine("                  |          <outputDir>");
        }
        public static CommandLineArguments Parse(string[] args)
        {
            Dictionary <string, ModeArgument> modeAliases = new Dictionary <string, ModeArgument>();

            modeAliases["help"]      = ModeArgument.Help;
            modeAliases["h"]         = ModeArgument.Help;
            modeAliases["?"]         = ModeArgument.Help;
            modeAliases["decompile"] = ModeArgument.Decompile;
            modeAliases["d"]         = ModeArgument.Decompile;

            Dictionary <string, OptionalArgument> optionalAliases = new Dictionary <string, OptionalArgument>();

            optionalAliases["version"] = OptionalArgument.Version;
            optionalAliases["V"]       = OptionalArgument.Version;

            CommandLineArguments arguments = new CommandLineArguments();

            foreach (string _arg in args)
            {
                string arg = _arg.Contains("=") ? _arg.Split('=')[0] : _arg;

                if (arg.StartsWith("--"))
                {
                    if (modeAliases.ContainsKey(arg.Substring(2).ToLower()))
                    {
                        arguments.Mode = modeAliases[arg.Substring(2).ToLower()];
                    }
                    else if (optionalAliases.ContainsKey(arg.Substring(2).ToLower()))
                    {
                        arguments.OptionalArguments.Add(optionalAliases[arg.Substring(2).ToLower()]);
                        if (_arg.Contains("="))
                        {
                            arguments.OptionalArgumentParameters[optionalAliases[arg.Substring(2).ToLower()]] = _arg.Substring(arg.Length + 1);
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Unrecognized argument '" + arg + "'.\nRun 'modicite --help' for a list of available arguments.");
                        Console.ResetColor();
                        return(null);
                    }
                }
                else if (arg.StartsWith("-"))
                {
                    char[] shortArgs = arg.Substring(1).ToCharArray();
                    foreach (char shortArg in shortArgs)
                    {
                        if (modeAliases.ContainsKey(shortArg.ToString()))
                        {
                            arguments.Mode = modeAliases[shortArg.ToString()];
                        }
                        else if (optionalAliases.ContainsKey(shortArg.ToString()))
                        {
                            arguments.OptionalArguments.Add(optionalAliases[shortArg.ToString()]);
                            if (_arg.Contains("="))
                            {
                                arguments.OptionalArgumentParameters[optionalAliases[shortArg.ToString()]] = _arg.Substring(3);
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Unrecognized argument '" + arg + "'.\nRun 'modicite --help' for a list of available arguments.");
                            Console.ResetColor();
                            return(null);
                        }
                    }
                }
                else if (arg.StartsWith("/"))
                {
                    if (modeAliases.ContainsKey(arg.Substring(1)))
                    {
                        arguments.Mode = modeAliases[arg.Substring(1)];
                    }
                    else if (optionalAliases.ContainsKey(arg.Substring(1)))
                    {
                        arguments.OptionalArguments.Add(optionalAliases[arg.Substring(1)]);
                        if (_arg.Contains("="))
                        {
                            arguments.OptionalArgumentParameters[optionalAliases[arg.Substring(1)]] = _arg.Substring(arg.Length + 1);
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Unrecognized argument '" + arg + "'.\nRun 'modicite --help' for a list of available arguments.");
                        Console.ResetColor();
                        return(null);
                    }
                }
                else
                {
                    arguments.PlainArguments.Add(arg);
                }
            }

            return(arguments);
        }
Example #3
0
        static void Decompile(CommandLineArguments arguments)
        {
            if (arguments.PlainArguments.Count < 1) {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Expected Magicite game directory.");
                Console.WriteLine("Correct Usage:  modicite --decompile [--targetVersion=<version>] <gameDir> <outputDir>");
                Console.ResetColor();
                return;
            }

            if (arguments.PlainArguments.Count < 2) {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Expected output directory.");
                Console.WriteLine("Correct Usage:  modicite --decompile <gameDir> <outputDir>");
                Console.ResetColor();
                return;
            }

            #region Get Target Version

            string targetVersion = ModiciteInfo.NewestSupportedMagiciteVersion;

            if (arguments.OptionalArguments.Contains(OptionalArgument.Version)) {
                if (arguments.OptionalArgumentParameters.ContainsKey(OptionalArgument.Version)) {
                    targetVersion = arguments.OptionalArgumentParameters[OptionalArgument.Version];
                } else {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Expected version name.");
                    Console.WriteLine("Correct Usage:  modicite --decompile --version=<ver> <gameDir> <outputDir>");
                    Console.ResetColor();
                    return;
                }
            }

            if (!Directory.Exists("./versions")) {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Directory './versions' does not exist. You need to re-install Modicite.");
                Console.ResetColor();
                return;
            }

            if (!File.Exists("./versions/" + targetVersion + ".json")) {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("No configuration file found for target version '" + targetVersion + "'.");
                Console.WriteLine("If the target version is 'default', then you need to re-install Modicite.");
                Console.WriteLine("If the target version is newer than '" + ModiciteInfo.NewestSupportedMagiciteVersion + "', then you need to update to a newer version of Modicite.");
                Console.ResetColor();
                return;
            }

            TargetFile targetFile = null;
            try {
                targetFile = SimpleJson.SimpleJson.DeserializeObject<TargetFile>(File.ReadAllText("./versions/" + targetVersion + ".json"));
            } catch (Exception ex) {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("Failed to load configuration file for target version due to " + ex.GetType().Name + ": " + ex.Message);
                Console.ResetColor();
                return;
            }

            Console.WriteLine("Decompiling with target version '" + targetVersion + "'.");

            #endregion

            #region Create Output Directory

            if (Directory.Exists(arguments.PlainArguments[1])
                && Directory.GetFiles(arguments.PlainArguments[1]).Length > 0
                && Directory.GetDirectories(arguments.PlainArguments[1]).Length > 0) {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("The output directory is not empty!");
                Console.ResetColor();
                return;
            }

            if (!Directory.Exists(arguments.PlainArguments[1])) {
                try {
                    Directory.CreateDirectory(arguments.PlainArguments[1]);
                } catch (Exception ex) {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("The output directory could not be created due to " + ex.GetType().Name + ": " + ex.Message);
                    Console.ResetColor();
                    return;
                }
            }

            string outputDir = arguments.PlainArguments[1].TrimEnd(new char[] { '/', '\\' });

            #endregion

            ClassIDDatabase.Load("./classes.txt");

            RTTIDatabase.Load("./types.dat");

            #region Unity Binaries

            Directory.CreateDirectory(outputDir + "/unity-data");

            foreach (TargetFileEntry dataFileEntry in targetFile.unityDataFiles) {
                string entryPath = arguments.PlainArguments[0].TrimEnd(new char[] { '/', '\\' }) + dataFileEntry.path;

                if (!File.Exists(entryPath)) {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Missing Unity data file '" + dataFileEntry.name + "'!");
                    Console.ResetColor();
                    continue;
                }

                Console.Write("Loading Unity data file '" + dataFileEntry.name + "'... ");
                UnityFile uf;
                try {
                    uf = UnityFile.Load(entryPath);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Done.");
                    Console.ResetColor();
                } catch (Exception ex) {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Failed due to " + ex.GetType().Name + ": " + ex.Message);
                    Console.ResetColor();
                    continue;
                }

                Directory.CreateDirectory(outputDir + "./unity-data/" + dataFileEntry.name);

                Console.Write("Exporting file header from '" + dataFileEntry.name + "'... ");
                try {
                    //uf.ExportHeaderToFile(outputDir + "./unity-data/" + dataFileEntry.name + "/header.json");
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Done.");
                    Console.ResetColor();
                } catch (Exception ex) {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("Failed due to " + ex.GetType().Name + ": " + ex.Message);
                    Console.ResetColor();
                    continue;
                }

                List<string> exportFailures = new List<string>();

                int count = 1;
                /*foreach (ObjectInfo oi in uf.Metadata.ObjectInfoList) {
                    Console.CursorLeft = 0;
                    Console.Write("Exporting object data from '" + dataFileEntry.name + "'... " + count.ToString() + "/" + uf.Metadata.NumberOfObjectInfoListMembers.ToString());

                    string objectClassName = oi.ClassID == 114 ? "114" : ClassIDDatabase.Classes[oi.ClassID];

                    if (!Directory.Exists(outputDir + "./unity-data/" + dataFileEntry.name + "/" + objectClassName)) {
                        Directory.CreateDirectory(outputDir + "./unity-data/" + dataFileEntry.name + "/" + objectClassName);
                    }

                    if (oi.ClassID != 198) {
                        try {
                            uf.ExportObjectToFile(oi, outputDir + "./unity-data/" + dataFileEntry.name + "/" + objectClassName + "/" + oi.ObjectID.ToString() + ".json", outputDir + "./unity-data/" + dataFileEntry.name + "/" + objectClassName + "/raw-" + oi.ObjectID.ToString() + ".json");
                        } catch (Exception ex) {
                            exportFailures.Add("Object " + oi.ObjectID.ToString() + " failed to export due to " + ex.GetType().Name + ": " + ex.Message);
                        }
                    } else {
                        try {
                            uf.ExportRawObjectToFile(oi, outputDir + "./unity-data/" + dataFileEntry.name + "/" + objectClassName + "/raw-" + oi.ObjectID.ToString() + ".json");
                        } catch (Exception ex) {
                            exportFailures.Add("Object " + oi.ObjectID.ToString() + " failed to export due to " + ex.GetType().Name + ": " + ex.Message);
                        }
                    }

                    count++;
                }*/
                Console.Write(" - ");
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Done.");
                Console.ResetColor();

                Console.ForegroundColor = ConsoleColor.Yellow;
                foreach (string exportFailure in exportFailures) {
                    Console.WriteLine(exportFailure);
                }
                Console.ResetColor();
            }

            #endregion
        }
        public static CommandLineArguments Parse(string[] args)
        {
            Dictionary<string, ModeArgument> modeAliases = new Dictionary<string, ModeArgument>();
            modeAliases["help"] = ModeArgument.Help;
            modeAliases["h"] = ModeArgument.Help;
            modeAliases["?"] = ModeArgument.Help;
            modeAliases["decompile"] = ModeArgument.Decompile;
            modeAliases["d"] = ModeArgument.Decompile;

            Dictionary<string, OptionalArgument> optionalAliases = new Dictionary<string, OptionalArgument>();
            optionalAliases["version"] = OptionalArgument.Version;
            optionalAliases["V"] = OptionalArgument.Version;

            CommandLineArguments arguments = new CommandLineArguments();

            foreach (string _arg in args) {
                string arg = _arg.Contains("=") ? _arg.Split('=')[0] : _arg;

                if (arg.StartsWith("--")) {
                    if (modeAliases.ContainsKey(arg.Substring(2).ToLower())) {
                        arguments.Mode = modeAliases[arg.Substring(2).ToLower()];
                    } else if (optionalAliases.ContainsKey(arg.Substring(2).ToLower())) {
                        arguments.OptionalArguments.Add(optionalAliases[arg.Substring(2).ToLower()]);
                        if (_arg.Contains("=")) {
                            arguments.OptionalArgumentParameters[optionalAliases[arg.Substring(2).ToLower()]] = _arg.Substring(arg.Length + 1);
                        }
                    } else {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Unrecognized argument '" + arg + "'.\nRun 'modicite --help' for a list of available arguments.");
                        Console.ResetColor();
                        return null;
                    }
                } else if (arg.StartsWith("-")) {
                    char[] shortArgs = arg.Substring(1).ToCharArray();
                    foreach (char shortArg in shortArgs) {
                        if (modeAliases.ContainsKey(shortArg.ToString())) {
                            arguments.Mode = modeAliases[shortArg.ToString()];
                        } else if (optionalAliases.ContainsKey(shortArg.ToString())) {
                            arguments.OptionalArguments.Add(optionalAliases[shortArg.ToString()]);
                            if (_arg.Contains("=")) {
                                arguments.OptionalArgumentParameters[optionalAliases[shortArg.ToString()]] = _arg.Substring(3);
                            }
                        } else {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("Unrecognized argument '" + arg + "'.\nRun 'modicite --help' for a list of available arguments.");
                            Console.ResetColor();
                            return null;
                        }
                    }
                } else if (arg.StartsWith("/")) {
                    if (modeAliases.ContainsKey(arg.Substring(1))) {
                        arguments.Mode = modeAliases[arg.Substring(1)];
                    } else if (optionalAliases.ContainsKey(arg.Substring(1))) {
                        arguments.OptionalArguments.Add(optionalAliases[arg.Substring(1)]);
                        if (_arg.Contains("=")) {
                            arguments.OptionalArgumentParameters[optionalAliases[arg.Substring(1)]] = _arg.Substring(arg.Length + 1);
                        }
                    } else {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("Unrecognized argument '" + arg + "'.\nRun 'modicite --help' for a list of available arguments.");
                        Console.ResetColor();
                        return null;
                    }
                } else {
                    arguments.PlainArguments.Add(arg);
                }
            }

            return arguments;
        }