protected bool MldbRead(FileStream stream) { stream.Seek(4, SeekOrigin.Begin); try { while (true) { Int32 categoryID = 0; if (!this.MldbReadInteger(stream, ref categoryID)) { return(false); } if (categoryID == 0) { this._isLoaded = true; return(true); } MdbCategory category = this.GetCategory(categoryID); if (category == null) { category = new MdbCategory(categoryID, 0); this._categories.Add(category); } Int32 entryID = 0; if (!this.MldbReadInteger(stream, ref entryID)) { return(false); } Int32 size = 0; if (!this.MldbReadInteger(stream, ref size)) { return(false); } byte[] buffer = new byte[size]; stream.Read(buffer, 0, buffer.Length); string message = Encoding.UTF8.GetString(buffer); MdbEntry entry = new MdbEntry(entryID, 0, message); category.Add(entry); } } catch { } return(false); }
public bool Add(MdbEntry entry) { if (entry == null) { return(false); } if (this.GetEntry(entry.EntryID) != null) { return(false); } if (this._entries != null) { lock (this._entries) { this._entries.Add(entry); return(true); } } return(false); }
public static DescrambledMessage Decode(string scrambledString, bool recursive, MdbReader reader) { if (reader == null) { reader = new MdbReader(); } if (scrambledString.Length < 2) { throw new Exception("Invalid message length!"); } if (!scrambledString.StartsWith("~")) { throw new Exception("Invalid message start! Expecting: ~"); } if (!scrambledString.EndsWith("~") && !recursive) { throw new Exception("Invalid message end! Expecting: ~"); } scrambledString = scrambledString.Substring(1); if (!recursive) { scrambledString = scrambledString.Substring(0, scrambledString.Length - 1); } DescrambledMessage aoDescrambledMessage = new DescrambledMessage(0, 0, null); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(scrambledString)); while (true) { if (stream.Position >= stream.Length) { break; } //string type = char.ConvertFromUtf32(stream.ReadByte()); /* Not Supported on Mono */ string type = Convert.ToChar(stream.ReadByte()).ToString(); switch (type) { case "&": // Message start, Category ID and Entry ID aoDescrambledMessage = new DescrambledMessage( Base85.Decode(Read(stream, 5)), Base85.Decode(Read(stream, 5)), "~" + scrambledString + "~" ); break; case "s": // String Int32 textSize = stream.ReadByte(); String text = Read(stream, textSize - 1); aoDescrambledMessage.Append(new DescrambledArgument(text)); break; case "i": // Integer Int32 integer = Base85.Decode(Read(stream, 5)); aoDescrambledMessage.Append(new DescrambledArgument(integer)); break; case "u": // Unsigned Integer UInt32 unsignedInteger = (UInt32)Base85.Decode(Read(stream, 5)); aoDescrambledMessage.Append(new DescrambledArgument(unsignedInteger)); break; case "f": // Float Single single = (Single)Base85.Decode(Read(stream, 5)); aoDescrambledMessage.Append(new DescrambledArgument(single)); break; case "R": // Reference, Category ID and Entry ID String referenceMessage = string.Empty; Int32 referenceCategoryID = Base85.Decode(Read(stream, 5)); Int32 referenceEntryID = Base85.Decode(Read(stream, 5)); if (reader != null) { MdbEntry referenceEntry = reader.GetEntry(referenceCategoryID, referenceEntryID); if (referenceEntry != null) { referenceMessage = referenceEntry.Message; } } DescrambledArgument reference = new DescrambledArgument( referenceCategoryID, referenceEntryID, referenceMessage ); aoDescrambledMessage.Append(reference); break; case "F": // Recursive, Complete new message Int32 recursiveSize = stream.ReadByte(); String recursiveText = Read(stream, recursiveSize - 1); aoDescrambledMessage.Append(new DescrambledArgument(Decode(recursiveText, true, reader))); break; default: throw new Exception("Unknown type detected: " + type); } } if (reader != null) { MdbEntry entry = reader.GetEntry(aoDescrambledMessage.CategoryID, aoDescrambledMessage.EntryID); if (entry != null) { try { aoDescrambledMessage.Message = String.Format(PrintfToFormatString(entry.Message), aoDescrambledMessage.Arguments); } catch { } } } return(aoDescrambledMessage); }
protected bool MmdbRead(FileStream stream) { // Go to start position stream.Seek(8, SeekOrigin.Begin); try { // Read categories Int32 currentLocation = 8; bool notFinishedCategories = true; while (notFinishedCategories) { Int32 categoryID = 0; Int32 offset = 0; if (!this.MmdbReadKey(stream, ref categoryID, ref offset)) { throw new Exception(); } currentLocation += 8; if (categoryID == -1) { this._endOfCategories = currentLocation; this._endOfEntries = offset; notFinishedCategories = false; } else { MdbCategory category = new MdbCategory(categoryID, offset); this._categories.Add(category); } } // Read Entries bool notFinishedEntries = true; while (notFinishedEntries) { stream.Seek(currentLocation, SeekOrigin.Begin); Int32 entryID = 0; Int32 offset = 0; string message = String.Empty; if (!this.MmdbReadKey(stream, ref entryID, ref offset)) { throw new Exception(); } if (!this.MmdbReadString(stream, offset, ref message)) { throw new Exception(); } MdbEntry entry = new MdbEntry(entryID, offset, message); MdbCategory category = null; foreach (MdbCategory cat in this._categories) { if (cat.Offset > currentLocation) { break; } category = cat; } if (category != null) { category.Add(entry); } currentLocation += 8; if (currentLocation >= this._endOfEntries) { break; } } } catch { Console.WriteLine("ERROR: Error during reading " + this._file); return(false); } if (stream != null) { stream.Close(); } this._isLoaded = true; return(true); }