/// <summary> /// Creates an empty BSON document. /// <param name="useStringTable">Whether or not to generate and use a string table.</param> /// </summary> public BsonDocument( BsonStringTableMode mode = BsonStringTableMode.None, Dictionary<int, string> reverseStringTable = null) { _stringTableMode = mode; _stringTable = new Dictionary<string, int>(); _reverseStringTable = reverseStringTable; Top = new BsonItem(); }
/// <summary> /// Creates an empty BSON document. /// <param name="useStringTable">Whether or not to generate and use a string table.</param> /// </summary> public BsonDocument( BsonStringTableMode mode = BsonStringTableMode.None, Dictionary <int, string> reverseStringTable = null) { _stringTableMode = mode; _stringTable = new Dictionary <string, int>(); _reverseStringTable = reverseStringTable; Top = new BsonItem(); }
/// <summary> /// Saves the package to the specified file path. /// </summary> /// <param name="path">The path to the file to create.</param> public void Save( string path, bool compress = true, BsonStringTableMode stringTableMode = BsonStringTableMode.None) { if (String.IsNullOrEmpty(Path.GetExtension(path))) { path += ".rantpkg"; } using (var writer = new EasyWriter(File.Create(path))) { var doc = new BsonDocument(stringTableMode); var info = doc.Top["info"] = new BsonItem(); info["title"] = new BsonItem(_title); info["id"] = new BsonItem(_id); info["description"] = new BsonItem(Description); info["tags"] = new BsonItem(Tags); info["version"] = new BsonItem(Version.ToString()); info["authors"] = new BsonItem(Authors); info["dependencies"] = new BsonItem(_dependencies.Select(dep => { var depObj = new BsonItem(); depObj["id"] = dep.ID; depObj["version"] = dep.Version.ToString(); depObj["allow-newer"] = dep.AllowNewer; return(depObj); }).ToArray()); var patterns = doc.Top["patterns"] = new BsonItem(); if (_patterns != null) { foreach (var pattern in _patterns) { patterns[pattern.Name] = new BsonItem(pattern.Code); } } var tables = doc.Top["tables"] = new BsonItem(); if (_tables != null) { foreach (var table in _tables) { var t = tables[table.Name] = new BsonItem(); t["name"] = new BsonItem(table.Name); t["subs"] = new BsonItem(table.Subtypes); t["language"] = new BsonItem(table.Language); t["hidden"] = new BsonItem(table.HiddenClasses.ToArray()); t["hints"] = new BsonItem(0); var entries = new List <BsonItem>(); foreach (var entry in table.GetEntries()) { var e = new BsonItem(); if (entry.Weight != 1) { e["weight"] = new BsonItem(entry.Weight); } var requiredClasses = entry.GetRequiredClasses().ToArray(); if (requiredClasses.Length > 0) { e["classes"] = new BsonItem(requiredClasses); } var optionalClasses = entry.GetOptionalClasses().ToArray(); if (optionalClasses.Length > 0) { e["optional_classes"] = new BsonItem(); } var terms = new List <BsonItem>(); foreach (var term in entry.Terms) { var et = new BsonItem(); et["value"] = new BsonItem(term.Value); if (term.Pronunciation != "") { et["pron"] = new BsonItem(term.Pronunciation); } terms.Add(et); } e["terms"] = new BsonItem(terms.ToArray()); entries.Add(e); } t["entries"] = new BsonItem(entries.ToArray()); } } var data = doc.ToByteArray(stringTableMode != BsonStringTableMode.None); if (compress) { data = EasyCompressor.Compress(data); } writer.Write(Encoding.ASCII.GetBytes("RANT")); writer.Write((uint)2); writer.Write(compress); writer.Write(data.Length); writer.Write(data); } }
/// <summary> /// Saves the package to the specified file path. /// </summary> /// <param name="path">The path to the file to create.</param> public void Save( string path, bool compress = true, BsonStringTableMode stringTableMode = BsonStringTableMode.None) { if (String.IsNullOrEmpty(Path.GetExtension(path))) { path += ".rantpkg"; } using (var writer = new EasyWriter(File.Create(path))) { var doc = new BsonDocument(stringTableMode); var info = doc.Top["info"] = new BsonItem(); info["title"] = new BsonItem(_title); info["id"] = new BsonItem(_id); info["description"] = new BsonItem(Description); info["tags"] = new BsonItem(Tags); info["version"] = new BsonItem(Version.ToString()); info["authors"] = new BsonItem(Authors); info["dependencies"] = new BsonItem(_dependencies.Select(dep => { var depObj = new BsonItem(); depObj["id"] = dep.ID; depObj["version"] = dep.Version.ToString(); depObj["allow-newer"] = dep.AllowNewer; return depObj; }).ToArray()); var patterns = doc.Top["patterns"] = new BsonItem(); if(_patterns != null) foreach(var pattern in _patterns) patterns[pattern.Name] = new BsonItem(pattern.Code); var tables = doc.Top["tables"] = new BsonItem(); if (_tables != null) { foreach (var table in _tables) { var t = tables[table.Name] = new BsonItem(); t["name"] = new BsonItem(table.Name); t["subs"] = new BsonItem(table.Subtypes); t["language"] = new BsonItem(table.Language); t["hidden"] = new BsonItem(table.HiddenClasses.ToArray()); t["hints"] = new BsonItem(0); var entries = new List<BsonItem>(); foreach (var entry in table.GetEntries()) { var e = new BsonItem(); if (entry.Weight != 1) e["weight"] = new BsonItem(entry.Weight); var requiredClasses = entry.GetRequiredClasses().ToArray(); if (requiredClasses.Length > 0) e["classes"] = new BsonItem(requiredClasses); var optionalClasses = entry.GetOptionalClasses().ToArray(); if (optionalClasses.Length > 0) e["optional_classes"] = new BsonItem(); var terms = new List<BsonItem>(); foreach (var term in entry.Terms) { var et = new BsonItem(); et["value"] = new BsonItem(term.Value); if (term.Pronunciation != "") et["pron"] = new BsonItem(term.Pronunciation); terms.Add(et); } e["terms"] = new BsonItem(terms.ToArray()); entries.Add(e); } t["entries"] = new BsonItem(entries.ToArray()); } } var data = doc.ToByteArray(stringTableMode != BsonStringTableMode.None); if (compress) data = EasyCompressor.Compress(data); writer.Write(Encoding.ASCII.GetBytes("RANT")); writer.Write((uint)2); writer.Write(compress); writer.Write(data.Length); writer.Write(data); } }