예제 #1
0
        public static int Main(string[] args)
        {
            bool interactive = true;

            if (args.Length > 0)
            {
                interactive = false;
            }

            foreach (string arg in args)
            {
                if (arg.Contains("--generatediffs"))
                {
                    gendiffs = true;
                }
            }

            newAssCS = true;

            Console.WriteLine(string.Format("[( Pluton Patcher v{0} )]", Version));
            try {
                plutonAssembly = AssemblyPatcher.FromFile("Pluton.dll");
                rustAssembly   = AssemblyPatcher.FromFile("Assembly-CSharp.dll");
            } catch (FileNotFoundException ex) {
                Console.WriteLine("You are missing " + ex.FileName + " did you move the patcher to the managed folder ?");
                if (interactive)
                {
                    Console.WriteLine("Press any key to continue...");
                }
                return((int)ExitCode.DLL_MISSING);
            } catch (Exception ex) {
                Console.WriteLine("An error occured while reading the assemblies :");
                Console.WriteLine(ex.ToString());
                if (interactive)
                {
                    Console.WriteLine("Press any key to continue...");
                }
                return((int)ExitCode.DLL_READ_ERROR);
            }

            bNPC        = rustAssembly.GetType("BaseNPC");
            bPlayer     = rustAssembly.GetType("BasePlayer");
            codeLock    = rustAssembly.GetType("CodeLock");
            hooksClass  = plutonAssembly.GetType("Pluton.Hooks");
            itemCrafter = rustAssembly.GetType("ItemCrafter");
            pLoot       = rustAssembly.GetType("PlayerLoot");

            //Check if patching is required
            TypePatcher plutonClass = rustAssembly.GetType("PlutonPatched");

            if (plutonClass == null)
            {
                try {
                    if (gendiffs)
                    {
                        string hash = string.Empty;
                        using (var sha512 = new System.Security.Cryptography.SHA512Managed())
                            hash = BitConverter.ToString(sha512.ComputeHash(File.ReadAllBytes("Assembly-CSharp.dll"))).Replace("-", "").ToLower();

                        Directory.CreateDirectory("diffs");

                        string hashpath = "diffs" + Path.DirectorySeparatorChar + "lastHash";

                        if (File.Exists(hashpath))
                        {
                            newAssCS = hash != File.ReadAllText(hashpath);
                        }

                        if (newAssCS)
                        {
                            foreach (var difffile in Directory.GetFiles("diffs"))
                            {
                                if (difffile.Contains(".htm"))
                                {
                                    string filename = Path.GetFileName(difffile);
                                    string dirname  = Path.GetDirectoryName(difffile);
                                    Directory.CreateDirectory(Path.Combine(dirname, "old"));
                                    File.Move(difffile, difffile.Replace(Path.Combine(dirname, filename), Path.Combine(dirname, "old", filename)));
                                }
                            }
                        }

                        if (gendiffs && newAssCS)
                        {
                            File.WriteAllText(hashpath, hash);
                        }
                    }
                    PatchASMCSharp();
                    Console.WriteLine("Patched Assembly-CSharp !");
                } catch (Exception ex) {
                    interactive = true;
                    Console.WriteLine("An error occured while patching Assembly-CSharp :");
                    Console.WriteLine();
                    Console.WriteLine(ex.Message.ToString());

                    //Normal handle for the others
                    Console.WriteLine();
                    Console.WriteLine(ex.StackTrace.ToString());
                    Console.WriteLine();

                    if (interactive)
                    {
                        Console.WriteLine("Press any key to continue...");
                    }
                    return((int)ExitCode.ACDLL_GENERIC_PATCH_ERR);
                }
            }
            else
            {
                Console.WriteLine("Assembly-CSharp.dll is already patched!");
                return((int)ExitCode.ACDLL_ALREADY_PATCHED);
            }

            try {
                rustAssembly.Write("Assembly-CSharp.dll");
            } catch (Exception ex) {
                Console.WriteLine("An error occured while writing the assembly :");
                Console.WriteLine("Error at: " + ex.TargetSite.Name);
                Console.WriteLine("Error msg: " + ex.Message);

                if (interactive)
                {
                    Console.WriteLine("Press any key to continue...");
                }

                return((int)ExitCode.DLL_WRITE_ERROR);
            }

            //Successfully patched the server
            Console.WriteLine("Completed !");

            if (interactive)
            {
                System.Threading.Thread.Sleep(250);
            }

            return((int)ExitCode.SUCCESS);
        }