protected List <TradEntry> ReadDictionary(uint entryCount, MemoryStream ms) { var entries = new List <TradEntry>(); byte[] hashBuffer; var buffer = new byte[4]; for (int i = 0; i < entryCount; i++) { var entry = new TradEntry { OffsetDic = (uint)ms.Position }; hashBuffer = new byte[8]; ms.Read(hashBuffer, 0, hashBuffer.Length); entry.Hash = hashBuffer; ms.Read(buffer, 0, buffer.Length); entry.OffsetCont = BitConverter.ToUInt32(buffer, 0); ms.Read(buffer, 0, buffer.Length); entry.ContLen = BitConverter.ToUInt32(buffer, 0); entries.Add(entry); } return(entries); }
public static void CalculateHash(TradEntry item) { const string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var allowedChars = characters.ToCharArray().ToList(); var wordToHash = new StringBuilder(); foreach (char t in item.Content) { if (allowedChars.Contains(t)) { wordToHash.Append(t); } } var word = wordToHash.ToString(); item.Hash = Utils.CreateLocalisationHash(word, word.Length); }
private void AddEntryExecute(object obj) { var newEntry = new TradEntry { Content = "New entry", UserCreated = true }; //CalculateHash(newEntry); int newIndex; if (Entries.Count > 1) { newIndex = Entries.Count - 1; } else { newIndex = Entries.Count; } Entries.Insert(newIndex, newEntry); }
public byte[] BuildTradFile() { using (var ms = new MemoryStream()) { byte[] buffer = Encoding.ASCII.GetBytes("TRAD"); ms.Write(buffer, 0, buffer.Length); buffer = BitConverter.GetBytes(Entries.Count); ms.Write(buffer, 0, buffer.Length); var glyphEntry = Entries.FirstOrDefault(x => BitConverter.ToUInt64(x.Hash, 0).Equals(GlyphHash)); if (glyphEntry == null) { glyphEntry = new TradEntry() { Hash = BitConverter.GetBytes(GlyphHash) }; Entries.Add(glyphEntry); } var orderedEntried = Entries.OrderBy(x => BitConverter.ToUInt64(x.Hash, 0)).ToList(); orderedEntried.Remove(glyphEntry); glyphEntry.Content = BuildGlyphContent(orderedEntried); orderedEntried.Add(glyphEntry); // Write dictionary foreach (TradEntry entry in orderedEntried) { entry.OffsetDic = (uint)ms.Position; // Hash ms.Write(entry.Hash, 0, entry.Hash.Length); // Content offset : we dont know it yet ms.Seek(4, SeekOrigin.Current); // Content length buffer = BitConverter.GetBytes(entry.Content.Length); ms.Write(buffer, 0, buffer.Length); } foreach (TradEntry entry in orderedEntried) { entry.OffsetCont = (uint)ms.Position; buffer = Encoding.Unicode.GetBytes(entry.Content); ms.Write(buffer, 0, buffer.Length); } foreach (TradEntry entry in orderedEntried) { ms.Seek(entry.OffsetDic, SeekOrigin.Begin); ms.Seek(8, SeekOrigin.Current); buffer = BitConverter.GetBytes(entry.OffsetCont); ms.Write(buffer, 0, buffer.Length); } //Util.Utils.SaveDebug("dicttest.diccmp",ms.ToArray()); return(ms.ToArray()); } }