Esempio n. 1
0
        private byte[] CalculateFileHash(ContentFile file)
        {
            using (MD5 md5 = MD5.Create())
            {
                List <string> filePaths = new List <string> {
                    file.Path
                };
                List <byte> data = new List <byte>();

                switch (file.Type)
                {
                case ContentType.Character:
                    XDocument doc           = XMLExtensions.TryLoadXml(file.Path);
                    var       rootElement   = doc.Root;
                    var       element       = rootElement.IsOverride() ? rootElement.FirstElement() : rootElement;
                    var       ragdollFolder = RagdollParams.GetFolder(doc, file.Path).CleanUpPathCrossPlatform(true);
                    if (Directory.Exists(ragdollFolder))
                    {
                        Directory.GetFiles(ragdollFolder, "*.xml").ForEach(f => filePaths.Add(f));
                    }
                    var animationFolder = AnimationParams.GetFolder(doc, file.Path).CleanUpPathCrossPlatform(true);
                    if (Directory.Exists(animationFolder))
                    {
                        Directory.GetFiles(animationFolder, "*.xml").ForEach(f => filePaths.Add(f));
                    }
                    break;
                }

                if (filePaths.Count > 1)
                {
                    using (MD5 tempMd5 = MD5.Create())
                    {
                        filePaths = filePaths.OrderBy(f => ToolBox.StringToUInt32Hash(f.CleanUpPathCrossPlatform(true).ToLowerInvariant(), tempMd5)).ToList();
                    }
                }

                foreach (string filePath in filePaths)
                {
                    if (!File.Exists(filePath))
                    {
                        continue;
                    }

                    using (var stream = File.OpenRead(filePath))
                    {
                        byte[] fileData = new byte[stream.Length];
                        stream.Read(fileData, 0, (int)stream.Length);
                        if (filePath.EndsWith(".xml", true, System.Globalization.CultureInfo.InvariantCulture))
                        {
                            string text = System.Text.Encoding.UTF8.GetString(fileData);
                            text     = text.Replace("\n", "").Replace("\r", "").Replace("\\", "/");
                            fileData = System.Text.Encoding.UTF8.GetBytes(text);
                        }
                        data.AddRange(fileData);
                    }
                }
                return(md5.ComputeHash(data.ToArray()));
            }
        }
Esempio n. 2
0
        private byte[] CalculateFileHash(ContentFile file)
        {
            var md5 = MD5.Create();

            List <string> filePaths = new List <string> {
                file.Path
            };
            List <byte> data = new List <byte>();

            switch (file.Type)
            {
            case ContentType.Character:
                XDocument doc           = XMLExtensions.TryLoadXml(file.Path);
                var       rootElement   = doc.Root;
                var       element       = rootElement.IsOverride() ? rootElement.FirstElement() : rootElement;
                var       speciesName   = element.GetAttributeString("speciesname", element.GetAttributeString("name", ""));
                var       ragdollFolder = RagdollParams.GetFolder(speciesName);
                if (Directory.Exists(ragdollFolder))
                {
                    Directory.GetFiles(ragdollFolder, "*.xml").ForEach(f => filePaths.Add(f));
                }
                var animationFolder = AnimationParams.GetFolder(speciesName);
                if (Directory.Exists(animationFolder))
                {
                    Directory.GetFiles(animationFolder, "*.xml").ForEach(f => filePaths.Add(f));
                }
                break;
            }

            foreach (string filePath in filePaths)
            {
                if (!File.Exists(filePath))
                {
                    continue;
                }
                using (var stream = File.OpenRead(filePath))
                {
                    byte[] fileData = new byte[stream.Length];
                    stream.Read(fileData, 0, (int)stream.Length);
                    if (filePath.EndsWith(".xml", true, System.Globalization.CultureInfo.InvariantCulture))
                    {
                        string text = System.Text.Encoding.UTF8.GetString(fileData);
                        text     = text.Replace("\n", "").Replace("\r", "");
                        fileData = System.Text.Encoding.UTF8.GetBytes(text);
                    }
                    data.AddRange(fileData);
                }
            }
            return(md5.ComputeHash(data.ToArray()));
        }
Esempio n. 3
0
        private byte[] CalculateFileHash(ContentFile file)
        {
            using (MD5 md5 = MD5.Create())
            {
                List <string> filePaths = new List <string> {
                    file.Path
                };
                List <byte> data = new List <byte>();

                switch (file.Type)
                {
                case ContentType.Character:
                    XDocument doc           = XMLExtensions.TryLoadXml(file.Path);
                    var       rootElement   = doc.Root;
                    var       element       = rootElement.IsOverride() ? rootElement.FirstElement() : rootElement;
                    var       ragdollFolder = RagdollParams.GetFolder(doc, file.Path).CleanUpPathCrossPlatform(true);
                    if (Directory.Exists(ragdollFolder))
                    {
                        Directory.GetFiles(ragdollFolder, "*.xml").ForEach(f => filePaths.Add(f));
                    }
                    var animationFolder = AnimationParams.GetFolder(doc, file.Path).CleanUpPathCrossPlatform(true);
                    if (Directory.Exists(animationFolder))
                    {
                        Directory.GetFiles(animationFolder, "*.xml").ForEach(f => filePaths.Add(f));
                    }
                    break;
                }

                if (filePaths.Count > 1)
                {
                    using (MD5 tempMd5 = MD5.Create())
                    {
                        // TODO: ToLower() fixes discrepencies between windows folder name capitalization for server/client interactions.
                        // The proper fix would probably be to save both a case-sensitive and non-casesensitive hash on the server,
                        //     and only compare case-sensitive clients with the case-sensitive hash, and case-insensitive clients with the
                        //     case insensitive hash.
                        // Though ultimately, the only case where this will cause issues is if someone made a mod with identical folder names in the same path.
                        // Windows... 😩
                        filePaths = filePaths.OrderBy(f => ToolBox.StringToUInt32Hash(f.CleanUpPathCrossPlatform(true).ToLower(), tempMd5)).ToList();
                    }
                }

                foreach (string filePath in filePaths)
                {
                    if (!File.Exists(filePath))
                    {
                        continue;
                    }

                    using (var stream = File.OpenRead(filePath))
                    {
                        byte[] fileData = new byte[stream.Length];
                        stream.Read(fileData, 0, (int)stream.Length);
                        if (filePath.EndsWith(".xml", true, System.Globalization.CultureInfo.InvariantCulture))
                        {
                            string text = System.Text.Encoding.UTF8.GetString(fileData);
                            text     = text.Replace("\n", "").Replace("\r", "").Replace("\\", "/");
                            fileData = System.Text.Encoding.UTF8.GetBytes(text);
                        }
                        data.AddRange(fileData);
                    }
                }
                return(md5.ComputeHash(data.ToArray()));
            }
        }