示例#1
0
 public BytePatch(BytePatchPattern pattern, byte origByte, byte patchByte, List <int> offsets, int group, bool isSig = false, int sigOffset = 0)
 {
     this.Pattern   = pattern;
     this.OrigByte  = origByte;
     this.PatchByte = patchByte;
     this.Offsets   = offsets;
     this.IsSig     = isSig;
     this.SigOffset = sigOffset;
     this.Group     = group;
 }
        public BytePatchManager()
        {
            BytePatches.Clear();
            BytePatterns.Clear();

            XDocument xmlDoc  = null;
            string    xmlFile = Path.GetTempPath() + "chrome_patcher_patterns.xml";

            try {
                using (WebClient web = new WebClient()) {
                    string xmlStr;
                    xmlDoc = XDocument.Parse(xmlStr = web.DownloadString("https://raw.githubusercontent.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher/master/patterns.xml")); // Hardcoded defaults xml file; This makes quick fixes possible

                    File.WriteAllText(xmlFile, xmlStr);
                }
            } catch (Exception ex) {
                if (File.Exists(xmlFile))
                {
                    xmlDoc = XDocument.Parse(File.ReadAllText(xmlFile));
                    MessageBox.Show("An error occurred trying to fetch the new patterns. The old cached version will be used instead. Expect patch errors.\n\n" + ex.Message, "Warning");
                }
                else
                {
                    MessageBox.Show("An error occurred trying to fetch the new patterns. The program has to exit, as no cached version of this file has been found.\n\n" + ex.Message, "Error");
                    Environment.Exit(1);
                }
            }


            if (xmlDoc != null)
            {
                // Comma culture setter from https://stackoverflow.com/questions/9160059/set-up-dot-instead-of-comma-in-numeric-values
                CultureInfo customCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();                customCulture.NumberFormat.NumberDecimalSeparator = ".";
                Thread.CurrentThread.CurrentCulture = customCulture;

                float newVersion = float.Parse(xmlDoc.Root.Attribute("version").Value);
                if (newVersion > Program.VERSION)
                {
                    MessageBox.Show("A new version of this patcher has been found.\nDownload it at:\nhttps://github.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher/releases", "New update available");
                }

                foreach (XElement pattern in xmlDoc.Root.Element("Patterns").Elements("Pattern"))
                {
                    BytePatchPattern patternClass = new BytePatchPattern(pattern.Attribute("name").Value);

                    foreach (XElement patternList in pattern.Elements("BytePatternList"))
                    {
                        bool isX64 = patternList.Attribute("type").Value.Equals("x64");

                        foreach (XElement bytePattern in patternList.Elements("BytePattern"))
                        {
                            string[] unparsedBytes   = bytePattern.Value.Split(' ');
                            byte[]   patternBytesArr = new byte[unparsedBytes.Length];

                            for (int i = 0; i < unparsedBytes.Length; i++)
                            {
                                string unparsedByte = unparsedBytes[i].Equals("?") ? "FF" : unparsedBytes[i];
                                patternBytesArr[i] = Convert.ToByte(unparsedByte, 16);
                            }
                            (isX64 ? patternClass.AlternativePatternsX64 : patternClass.AlternativePatternsX86).Add(patternBytesArr);
                        }
                    }
                    BytePatterns.Add(patternClass.Name, patternClass);
                }

                foreach (XElement patch in xmlDoc.Root.Element("Patches").Elements("Patch"))
                {
                    BytePatchPattern pattern = BytePatterns[patch.Attribute("pattern").Value];
                    int group = int.Parse(patch.Attribute("group").Value);

                    byte origX64 = 0, origX86 = 0, patchX64 = 0, patchX86 = 0;
                    int  offsetX64 = 0, offsetX86 = 0, aoffsetX64 = -1, aoffsetX86 = -1;

                    foreach (XElement patchData in patch.Elements("PatchData"))
                    {
                        byte orig   = Convert.ToByte(patchData.Attribute("orig").Value.Replace("0x", ""), 16);
                        byte patchB = Convert.ToByte(patchData.Attribute("patch").Value.Replace("0x", ""), 16);
                        int  offset = Convert.ToInt32(patchData.Attribute("offset").Value.Replace("0x", ""), 16);

                        XAttribute alternativeOffsetAttr = patchData.Attribute("alternativeOffset");
                        int        aoffset = alternativeOffsetAttr == null ? -1 : Convert.ToInt32(alternativeOffsetAttr.Value.Replace("0x", ""), 16);

                        if (patchData.Attribute("type").Value.Equals("x64"))
                        {
                            origX64    = orig;
                            patchX64   = patchB;
                            offsetX64  = offset;
                            aoffsetX64 = aoffset;
                        }
                        else
                        {
                            origX86    = orig;
                            patchX86   = patchB;
                            offsetX86  = offset;
                            aoffsetX86 = aoffset;
                        }
                    }
                    BytePatches.Add(new BytePatch(pattern, origX64, patchX64, offsetX64, aoffsetX64, origX86, patchX86, offsetX86, aoffsetX86, group));
                }
            }
        }
示例#3
0
        public BytePatchManager(WriteLineOrMessageBox log)
        {
            this.BytePatches.Clear();
            this.bytePatterns.Clear();

            XDocument xmlDoc  = null;
            string    xmlFile = Program.DEBUG ? @"..\..\..\patterns.xml" : (Path.GetTempPath() + "chrome_patcher_patterns.xml");

            try {
                if (Program.DEBUG)
                {
                    throw new Exception("Forcing to use local patterns.xml");
                }

                using (WebClient web = new WebClient()) {
                    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;                     // TLS 1.2, which is required for Github

                    string xmlStr;
                    xmlDoc = XDocument.Parse(xmlStr = web.DownloadString("https://raw.githubusercontent.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher/master/patterns.xml"));                     // Hardcoded defaults xml file; This makes quick fixes possible

                    File.WriteAllText(xmlFile, xmlStr);
                }
            } catch (Exception ex) {
                if (File.Exists(xmlFile))
                {
                    xmlDoc = XDocument.Parse(File.ReadAllText(xmlFile));
                    log("An error occurred trying to fetch the new patterns. The old cached version will be used instead. Expect patch errors.\n\n" + ex.Message, "Warning");
                }
                else
                {
                    log("An error occurred trying to fetch the new patterns. The program has to exit, as no cached version of this file has been found.\n\n" + ex.Message, "Error");
                    Environment.Exit(1);
                }
            }


            // Comma culture setter from https://stackoverflow.com/questions/9160059/set-up-dot-instead-of-comma-in-numeric-values
            CultureInfo customCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone(); customCulture.NumberFormat.NumberDecimalSeparator = ".";

            Thread.CurrentThread.CurrentCulture = customCulture;

            float   newVersion = float.Parse(xmlDoc.Root.Attribute("version").Value);
            Version myVersion  = Assembly.GetCallingAssembly().GetName().Version;

            if (newVersion > float.Parse(myVersion.Major + "." + myVersion.Minor))
            {
                log("A new version of this patcher has been found.\nDownload it at:\nhttps://github.com/Ceiridge/Chrome-Developer-Mode-Extension-Warning-Patcher/releases", "New update available");
            }

            foreach (XElement pattern in xmlDoc.Root.Element("Patterns").Elements("Pattern"))
            {
                BytePatchPattern patternClass = new BytePatchPattern(pattern.Attribute("name").Value);

                foreach (XElement bytePattern in pattern.Elements("BytePattern"))
                {
                    string[] unparsedBytes   = bytePattern.Value.Split(' ');
                    byte[]   patternBytesArr = new byte[unparsedBytes.Length];

                    for (int i = 0; i < unparsedBytes.Length; i++)
                    {
                        string unparsedByte = unparsedBytes[i].Equals("?") ? "FF" : unparsedBytes[i];
                        patternBytesArr[i] = Convert.ToByte(unparsedByte, 16);
                    }
                    patternClass.AlternativePatternsX64.Add(patternBytesArr);
                }

                this.bytePatterns.Add(patternClass.Name, patternClass);
            }

            foreach (XElement patch in xmlDoc.Root.Element("Patches").Elements("Patch"))
            {
                BytePatchPattern pattern = this.bytePatterns[patch.Attribute("pattern").Value];
                int group = int.Parse(patch.Attribute("group").Value);

                byte       origX64 = 0, patchX64 = 0;
                List <int> offsetsX64 = new List <int>();
                int        sigOffset  = 0;
                bool       sig        = false;

                foreach (XElement patchData in patch.Elements("PatchData"))
                {
                    foreach (XElement offsetElement in patchData.Elements("Offset"))
                    {
                        offsetsX64.Add(Convert.ToInt32(offsetElement.Value.Replace("0x", ""), 16));
                    }

                    origX64  = Convert.ToByte(patchData.Attribute("orig").Value.Replace("0x", ""), 16);
                    patchX64 = Convert.ToByte(patchData.Attribute("patch").Value.Replace("0x", ""), 16);
                    sig      = Convert.ToBoolean(patchData.Attribute("sig").Value);
                    if (patchData.Attributes("sigOffset").Any())
                    {
                        sigOffset = Convert.ToInt32(patchData.Attribute("sigOffset").Value.Replace("0x", ""), 16);
                    }
                    break;
                }

                this.BytePatches.Add(new BytePatch(pattern, origX64, patchX64, offsetsX64, @group, sig, sigOffset));
            }

            foreach (XElement patchGroup in xmlDoc.Root.Element("GroupedPatches").Elements("GroupedPatch"))
            {
                this.PatchGroups.Add(new GuiPatchGroupData {
                    Group   = int.Parse(patchGroup.Attribute("group").Value),
                    Default = bool.Parse(patchGroup.Attribute("default").Value),
                    Name    = patchGroup.Element("Name").Value,
                    Tooltip = patchGroup.Element("Tooltip").Value
                });
            }
        }