private void WriteFile(string file)
        {
            var hash = new byte[0];
            var crcFile = $"{file}.crc";

            if (Path.GetExtension(file) == ".bak")
                crcFile = Path.ChangeExtension(file, ".crc.bak");

            using (var fs = File.OpenWrite(file))
            using (var writer = new BinaryWriter(fs))
            {
                writer.Write(1); // File Format
                if ((Int32) this._worldData["mnVersion"] != -1)
                    writer.Write((Int32) this._worldData["mnVersion"]);
                else writer.Write(4);
                writer.Write((string) this._worldData["mName"]);
                writer.Write((Int32) this._worldData["mnWorldSeed"]);
                writer.Write((Single) this._worldData["mrGravity"]);
                writer.Write((Single) this._worldData["mrMovementSpeed"]);
                writer.Write((Single) this._worldData["mrJumpSpeed"]);
                writer.Write((Single) this._worldData["mrMaxFallingSpeed"]);
                writer.Write((Int32) this._worldData["meGameMode"]);
                writer.Write((string) this._worldData["mInjectionSet"]);
                writer.Write((Int32) this._worldData["mnResourceLevel"]);
                writer.Write((Int32) this._worldData["mnPowerLevel"]);
                writer.Write((Int32) this._worldData["mnConveyorLevel"]);
                writer.Write((Int32) this._worldData["mnDayLevel"]);
                writer.Write((Int32) this._worldData["meDeathEffect"]);
                writer.Write((Int64) this._worldData["mSpawnX"]);
                writer.Write((Int64) this._worldData["mSpawnY"]);
                writer.Write((Int64) this._worldData["mSpawnZ"]);
                writer.Write((Boolean) this._worldData["mbIntroCompleted"]);
                writer.Write((Int32) this._worldData["mnMobLevel"]);
                writer.Write((Boolean) this._worldData["mbTutorialCompleted"]);
                writer.Write((Boolean) this._worldData["mbRushMode"]);
                writer.Write((Single) this._worldData["mrWorldTimePlayed"]);
                writer.Write((Int32) this._worldData["mnSharedResearchMode"]);
                writer.Write((Single) this._worldData["mrCurrentTimeOfDay"]);
                writer.Write((Int64) this._worldData["mCPHCoordX"]);
                writer.Write((Int64) this._worldData["mCPHCoordY"]);
                writer.Write((Int64) this._worldData["mCPHCoordZ"]);
                writer.Write((Boolean) this._worldData["mbFlatland"]);
                writer.Write((Boolean) this._worldData["mbCarePackage"]);
            }

            using (var fs = File.OpenRead(file))
            {
                var crc = new Crc32(3988292384u, 4294967295u);
                hash = crc.ComputeHash(fs);
            }

            using (var fs = File.Open(crcFile, FileMode.OpenOrCreate))
            using (var writer = new StreamWriter(fs))
            {
                for (var i = 0; i < hash.Length; i++)
                {
                    writer.Write(hash[i].ToString("X2").ToLower());
                }
            }
        }
        public Boolean check(Boolean action)
        {

            List<string> filessimilartemp = new List<string>();
            this.dirssimilar = new List<string>();
            this.dirsdelete = new List<string>();
            this.dirscreate = new List<string>();
            this.filescreate = new List<string>();
            this.filesdelete = new List<string>();
            this.filesmodify = new List<string>();
            this.filessimilar = new List<string>();

            Console.WriteLine("#################### CHECK DIR SIMILAR ###########################");
            foreach(var item in  this.ldirs.Intersect((IEnumerable<string>)this.lserverdirs))
            {
                this.dirssimilar.Add(item);
            }
            Console.WriteLine("#################### CHECK DIR TO CREATE ###########################");
            foreach (var item in this.lserverdirs.Except(this.dirssimilar))
            {
                this.dirscreate.Add(item);
            }
            Console.WriteLine("#################### CHECK DIR TO REMOVE ###########################");
            foreach (var item in this.ldirs.Except(this.dirssimilar))
            {
                this.dirsdelete.Add(item);
            }
            Console.WriteLine("#################### CHECK File SIMILAR ###########################");
            foreach (var item in this.lfiles.Intersect((IEnumerable<string>)this.lserverfiles))
            {
                this.filessimilar.Add(item);
            }
            Console.WriteLine("#################### CHECK DIR TO CREATE ###########################");
            foreach (var item in this.lserverfiles.Except(this.filessimilar))
            {
                this.filescreate.Add(item);
            }
            Console.WriteLine("#################### CHECK DIR TO REMOVE ###########################");
            foreach (var item in this.lfiles.Except(this.filessimilar))
            {
                this.filesdelete.Add(item);
            }
            Console.WriteLine("#################### CHECK Files TO Modify ###########################");
            foreach (var item in filessimilar)
            {
                int index = this.lserverfiles.IndexOf(item);
                String CRC = String.Empty;
                CRC = this.lserverfilesCrc.ElementAt(index);

                Crc32 crc32 = new Crc32();
                String hash = String.Empty;

                using (FileStream fs = File.Open(this.repoModsDir+item, FileMode.Open))
                {
                    foreach (byte b in crc32.ComputeHash(fs)) hash += b.ToString("x2").ToLower();
                }
                
                hash = hash.ToUpper();
                CRC = CRC.PadLeft(8, '0');

                if (hash == CRC)
                {
                    //Ok
                }
                else
                {
                    Console.WriteLine("ERREUR CRC : " + hash + " : " + CRC);
                    this.filesmodify.Add(item);
                    filessimilartemp.Add(item);
                }
            }

            foreach (var item in filessimilartemp)
            {
                this.filessimilar.Remove(item);
            }

            if (action)
            {
                Console.WriteLine("#################### Action #######################################");
                foreach (var item in this.dirscreate)
                {
                    Directory.CreateDirectory(this.repoModsDir + item);
                }
                foreach (var item in this.filesdelete)
                {
                    Console.WriteLine("# FILE : DELETE : " + item);
                    File.Delete(this.repoModsDir + item);
                }
                this.dirsdelete.Sort();
                this.dirsdelete.Reverse();
                foreach (var item in dirsdelete)
                {
                    Console.WriteLine("# DIR : DELETE : " + item);
                    Directory.Delete(this.repoModsDir + item);
                }

                foreach (var item in this.filescreate)
                {
                    // Get the object used to communicate with the server.
                    WebClient request = new WebClient();
                    // This example assumes the FTP site uses anonymous logon.
                    request.Credentials = new NetworkCredential(this.ftpUser, this.ftpPassword);
                    byte[] newFileData = request.DownloadData(this.ftpUrl + this.basedir + "mods/"+item);
                    FileStream newfile = new FileStream(this.repoModsDir+item, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    newfile.Write(newFileData, 0, newFileData.Length);
                    newfile.Close();
                }

                foreach (var item in this.filesmodify)
                {
                    File.Delete(this.repoModsDir + item);
                    //Download
                    // Get the object used to communicate with the server.
                    using(WebClient request = new WebClient()){
                        // This example assumes the FTP site uses anonymous logon.
                        request.Credentials = new NetworkCredential(this.ftpUser, this.ftpPassword);
                        byte[] newFileData = request.DownloadData(this.ftpUrl + this.basedir + "mods/" + item);
                        FileStream newfile = new FileStream(this.repoModsDir + item, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                        newfile.Write(newFileData, 0, newFileData.Length);
                        newfile.Close();
                    }
                }
                
            }

            if ((dirsdelete.Count + dirscreate.Count + filescreate.Count + filesdelete.Count + filesmodify.Count) > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        private void GetFileInformation(Uri uri, string fullPath, string[] currentContentStatus)
        {
            Crc32 crc32 = new Crc32();
            String hash = String.Empty;
            try
            {
                FileStream fs = File.Open(fullPath, FileMode.Open);
                foreach (byte b in crc32.ComputeHash(fs))
                {
                    hash += b.ToString("x2").ToLower();
                }
                fs.Close();

                bool newVersion = hash.Equals(currentContentStatus.Last());
                FileInfo savedFile = new FileInfo(fullPath);
                StreamWriter sw = new StreamWriter(fullPath + ".stat");
                sw.Write(uri + " " + savedFile.LastWriteTimeUtc.ToShortTimeString() + " " + newVersion + " " + hash);
                sw.Close();

            }
            catch (Exception exc)
            {
                this.logger.Error(exc.Message);
            }
        }
Beispiel #4
0
        // Yrittää lukea tiedoston muistiin ja korvaa/lisää siihen Image-olioon tallennetut DPI-arvot
        public List<byte> readFile(Image image)
        {
            List<byte> binaryFile = new List<byte>();
            try
            {
                using (BinaryReader binReader = new BinaryReader(File.Open(image.Path, FileMode.Open)))
                {
                    long pos = 0;
                    long length = binReader.BaseStream.Length;
                    List<byte> byteBuffer = new List<byte>();
                    List<byte> pHYs = Encoding.ASCII.GetBytes("pHYs").ToList<byte>();
                    List<byte> IDAT = Encoding.ASCII.GetBytes("IDAT").ToList<byte>();
                    bool dpiReplaced = false;
                    while (pos < length)
                    {
                        byte B = binReader.ReadByte();
                        binaryFile.Add(B);

                        if (!dpiReplaced)
                        {
                            byteBuffer.Add(B);
                            while (byteBuffer.Count > 4) byteBuffer.RemoveAt(0);

                            if (byteBuffer.SequenceEqual(pHYs))
                            {
                                binReader.ReadBytes(9 + 4);
                                pos += sizeof(byte) * (9 + 4);

                                List<byte> data = new List<byte>();
                                data.AddRange(image.GetDpmByteList("x"));
                                data.AddRange(image.GetDpmByteList("y"));
                                data.Add(image.GetUnitAsByte());

                                binaryFile.AddRange(data);

                                Crc32 crc32 = new Crc32();
                                binaryFile.AddRange(crc32.ComputeHash(new MemoryStream(data.ToArray())));

                                dpiReplaced = true;
                            }
                            else if (byteBuffer.SequenceEqual(IDAT))
                            {
                                List<byte> data = new List<byte>();
                                data.AddRange(image.GetDpmByteList("x"));
                                data.AddRange(image.GetDpmByteList("y"));
                                data.Add(image.GetUnitAsByte());

                                List<byte> physChunk = new List<byte>();

                                byte[] size = BitConverter.GetBytes(9);
                                if (BitConverter.IsLittleEndian) Array.Reverse(size);

                                physChunk.AddRange(size);
                                physChunk.AddRange(pHYs);
                                physChunk.AddRange(data);

                                Crc32 crc32 = new Crc32();
                                physChunk.AddRange(crc32.ComputeHash(new MemoryStream(data.ToArray())));

                                binaryFile.InsertRange(binaryFile.Count - 8, physChunk);
                                dpiReplaced = true;
                            }
                        }

                        pos += sizeof(byte);
                    }
                }
            }
            catch { return null; }

            return binaryFile;
        }
Beispiel #5
0
        private string ComputeHash(string path)
        {
            Crc32 crc32 = new Crc32();
            String hash = String.Empty;

            using (FileStream fs = File.Open(path, FileMode.Open))
            {
                foreach (byte b in crc32.ComputeHash(fs))
                {
                    hash += b.ToString("x2").ToLower();
                }
            }

            return hash;
        }