Ejemplo n.º 1
0
        public static ZtFile Create(string unmodifiedFile, string modifiedFile)
        {
            byte[] unmodifiedBytes = File.ReadAllBytes(unmodifiedFile);
            byte[] modifiedBytes   = File.ReadAllBytes(modifiedFile);

            return(ZtFile.Create(unmodifiedBytes, modifiedBytes));
        }
Ejemplo n.º 2
0
        public static ZtFile FromFile(string fileName)
        {
            var zt = new ZtFile
            {
                FileName = fileName
            };

            FileStream filestream = null;

            try
            {
                filestream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                using (BinaryReader file = new BinaryReader(filestream))
                {
                    filestream = null;

                    zt.TargetName = Encoding.ASCII.GetString(file.ReadBytes(13)).TrimEnd('\0');

                    int count = file.ReadUInt16();

                    for (int i = 0; i < count; i++)
                    {
                        int    offset = file.ReadInt32();
                        byte   length = file.ReadByte();
                        byte[] bytes  = file.ReadBytes(length);

                        zt.Patches.Add(offset, bytes);
                    }

                    if (file.BaseStream.Position != file.BaseStream.Length)
                    {
                        byte[] commentBytes = file.ReadBytes((int)file.BaseStream.Length - (int)file.BaseStream.Position);

                        zt.Comment = Encoding.UTF8.GetString(commentBytes);
                    }

                    zt.Compact();
                }
            }
            finally
            {
                if (filestream != null)
                {
                    filestream.Dispose();
                }
            }

            return(zt);
        }
Ejemplo n.º 3
0
        public static ZtFile Create(byte[] unmodifiedBytes, byte[] modifiedBytes)
        {
            if (unmodifiedBytes == null)
            {
                throw new ArgumentNullException(nameof(unmodifiedBytes));
            }

            if (modifiedBytes == null)
            {
                throw new ArgumentNullException(nameof(modifiedBytes));
            }

            if (unmodifiedBytes.Length != modifiedBytes.Length)
            {
                throw new InvalidOperationException();
            }

            ZtFile zt = new ZtFile();

            for (int offset = 0; offset < unmodifiedBytes.Length; offset++)
            {
                int start = offset;
                int count = 0;

                while (offset < unmodifiedBytes.Length && unmodifiedBytes[offset] != modifiedBytes[offset])
                {
                    offset++;
                    count++;
                }

                if (count != 0)
                {
                    byte[] bytes = new byte[count];
                    Array.Copy(modifiedBytes, start, bytes, 0, count);

                    zt.Add(start, bytes);
                }
            }

            return(zt);
        }
Ejemplo n.º 4
0
        private void OpenPatchButton_Click(object sender, RoutedEventArgs e)
        {
            string filename = this.GetOpenFileName();

            if (string.IsNullOrEmpty(filename))
            {
                return;
            }

            this.RunBusyAction(disp =>
            {
                try
                {
                    this.PatchFile = ZtFile.FromFile(filename);

                    disp(() => this.UpdateDataContext());
                }
                catch (Exception ex)
                {
                    disp(() => Xceed.Wpf.Toolkit.MessageBox.Show(this, ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error));
                }
            });
        }
Ejemplo n.º 5
0
        public static ZtFile FromFile(string fileName)
        {
            var zt = new ZtFile();

            zt.FileName = fileName;

            FileStream filestream = null;

            try
            {
                filestream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                using (BinaryReader file = new BinaryReader(filestream))
                {
                    filestream = null;

                    zt.TargetName = Encoding.ASCII.GetString(file.ReadBytes(13)).TrimEnd('\0');

                    int count = file.ReadUInt16();

                    for (int i = 0; i < count; i++)
                    {
                        int offset = file.ReadInt32();
                        byte length = file.ReadByte();
                        byte[] bytes = file.ReadBytes(length);

                        zt.patches.Add(offset, bytes);
                    }

                    if (file.BaseStream.Position != file.BaseStream.Length)
                    {
                        byte[] commentBytes = file.ReadBytes((int)file.BaseStream.Length - (int)file.BaseStream.Position);

                        zt.Comment = Encoding.UTF8.GetString(commentBytes);
                    }

                    zt.Compact();
                }
            }
            finally
            {
                if (filestream != null)
                {
                    filestream.Dispose();
                }
            }

            return zt;
        }
Ejemplo n.º 6
0
        public static ZtFile Create(byte[] unmodifiedBytes, byte[] modifiedBytes)
        {
            if (unmodifiedBytes == null)
            {
                throw new ArgumentNullException("unmodifiedBytes");
            }

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

            if (unmodifiedBytes.Length != modifiedBytes.Length)
            {
                throw new InvalidOperationException();
            }

            ZtFile zt = new ZtFile();

            for (int offset = 0; offset < unmodifiedBytes.Length; offset++)
            {
                int start = offset;
                int count = 0;

                while (offset < unmodifiedBytes.Length && unmodifiedBytes[offset] != modifiedBytes[offset])
                {
                    offset++;
                    count++;
                }

                if (count != 0)
                {
                    byte[] bytes = new byte[count];
                    Array.Copy(modifiedBytes, start, bytes, 0, count);

                    zt.Add(start, bytes);
                }
            }

            return zt;
        }