Ejemplo n.º 1
0
        public static bool CanBeApplied(string exeFileName, Patch patch)
        {
            if (exeFileName == null)
            {
                throw new ArgumentNullException("exeFileName");
            }

            if (patch == null)
            {
                throw new ArgumentNullException("patch");
            }

            using (FileStream file = File.Open(exeFileName, FileMode.Open, FileAccess.Read))
            {
                Patcher.FileVerifyExeVersion(file);

                foreach (var item in patch.Items)
                {
                    if (!Patcher.FileCheckBytes(file, item.Offset, item.OldValues))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        public void Apply(string exeFileName, bool writeNewValues)
        {
            if (exeFileName == null)
            {
                throw new ArgumentNullException("exeFileName");
            }

            using (FileStream file = File.Open(exeFileName, FileMode.Open, FileAccess.ReadWrite))
            {
                Patcher.FileVerifyExeVersion(file);

                foreach (var patch in this.Patches)
                {
                    foreach (var item in patch.Items)
                    {
                        if (writeNewValues)
                        {
                            if (!Patcher.FileCheckBytes(file, item.Offset, item.OldValues))
                            {
                                throw new InvalidDataException();
                            }
                        }
                        else
                        {
                            if (!Patcher.FileCheckBytes(file, item.Offset, item.NewValues))
                            {
                                throw new InvalidDataException();
                            }
                        }
                    }

                    foreach (var item in patch.Items)
                    {
                        if (writeNewValues)
                        {
                            file.Seek(item.Offset, SeekOrigin.Begin);
                            file.Write(item.NewValues, 0, item.NewValues.Length);
                        }
                        else
                        {
                            file.Seek(item.Offset, SeekOrigin.Begin);
                            file.Write(item.OldValues, 0, item.OldValues.Length);
                        }
                    }
                }
            }
        }