/// <summary> /// Saves the package using the old Rant package format to the specified file path. /// </summary> /// <param name="path">The path to the file to create.</param> public void SaveOld(string path) { if (String.IsNullOrEmpty(Path.GetExtension(path))) { path += ".rantpkg"; } using (var writer = new EasyWriter(File.Create(path))) { // Magic writer.WriteBytes(Encoding.ASCII.GetBytes(OLD_MAGIC)); // Counts writer.Write(_patterns?.Count ?? 0); writer.Write(_tables?.Count ?? 0); // Patterns if (_patterns != null) { foreach (var pattern in _patterns) { writer.Write(pattern.Name); writer.Write(pattern.Code); } } // Tables if (_tables != null) { foreach (var table in _tables) { writer .Write(table.Name) .Write(table.Subtypes) .Write(table.EntryCount) .Write(table.HiddenClasses.ToArray()); foreach (var entry in table.GetEntries()) { writer .Write(entry.Weight) .Write(false) // Used to be the NSFW field, will use for something else in the future! .Write(entry.Terms.Length); for (int i = 0; i < entry.Terms.Length; i++) { writer .Write(entry.Terms[i].Value) .Write(entry.Terms[i].Pronunciation); } writer.Write(entry.GetClasses().ToArray()); } } } } }
internal static void SerializeResource(RantResource resource, EasyWriter writer) { if (!_resourceIdRegistry.TryGetValue(resource.GetType(), out string rstr)) { throw new ArgumentException($"Resource type '{resource.GetType()}' is not registered."); } writer.WriteBytes(Encoding.ASCII.GetBytes(rstr)); resource.SerializeData(writer); }
/// <summary> /// Saves the compiled program to the specified stream. /// </summary> /// <param name="stream">The stream to save the program to.</param> public void SaveToStream(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } using (var output = new EasyWriter(stream, Endian.Little, true)) { output.WriteBytes(Encoding.ASCII.GetBytes(Magic)); RST.SerializeRST(SyntaxTree, output); } stream.Flush(); }
/// <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) { if (String.IsNullOrEmpty(Path.GetExtension(path))) { path += ".rantpkg"; } using (var writer = new EasyWriter(File.Create(path))) { // Magic writer.WriteBytes(Encoding.ASCII.GetBytes(Magic)); // Counts writer.Write(_patterns?.Count ?? 0); writer.Write(_tables?.Count ?? 0); // Patterns if (_patterns != null) { foreach (var pattern in _patterns) { writer.Write(pattern.Name); writer.Write(pattern.Code); } } // Tables if (_tables != null) { foreach (var table in _tables) { writer .Write(table.Name) .Write(table.Subtypes) .Write(table.EntryCount) .Write(table.HiddenClasses.ToArray()); foreach (var entry in table.GetEntries()) { writer .Write(entry.Weight) .Write(false) // Used to be the NSFW field, will use for something else in the future! .Write(entry.Terms.Length); for (int i = 0; i < entry.Terms.Length; i++) { writer .Write(entry.Terms[i].Value) .Write(entry.Terms[i].Pronunciation); } writer.Write(entry.GetClasses().ToArray()); } } } } }
private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false) { if(!isTop) { writer.Write(item.Type); writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true); } if (item.HasValues) // object or array - we need to write a document { var stream = new MemoryStream(); var vWriter = new EasyWriter(stream); foreach(string key in item.Keys) WriteItem(vWriter, item[key], key, false, item.IsArray); vWriter.Close(); var data = stream.ToArray(); // length of data + length of length + null terminator writer.Write(data.Length + 4 + 1); writer.Write(data); writer.Write((byte)0x00); return; } switch(item.Type) { case 0x01: // double writer.Write((double)item.Value); break; case 0x02: // string var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true)); writer.Write(bytes.Length + 1); // includes null terminator writer.WriteBytes(bytes); writer.Write((byte)0x00); break; case 0x05: // binary var byteArray = (byte[])item.Value; writer.Write(byteArray.Length); writer.Write((byte)0x00); writer.Write(byteArray); break; case 0x07: // objectid writer.Write((byte[])item.Value); break; case 0x08: // bool writer.Write((bool)item.Value); break; case 0x09: // UTC datetime case 0x11: // timestamp case 0x12: // 64 bit int writer.Write((long)item.Value); break; case 0x10: // 32 bit int writer.Write((int)item.Value); break; default: throw new NotSupportedException($"Item type {item.Type} not supported."); } }
private void WriteItem(EasyWriter writer, BsonItem item, string name, bool isTop = false, bool isArray = false) { if (!isTop) { writer.Write(item.Type); writer.Write(isArray ? name : GetKeyName(name), Encoding.UTF8, true); } if (item.HasValues) // object or array - we need to write a document { var stream = new MemoryStream(); var vWriter = new EasyWriter(stream); foreach (string key in item.Keys) { WriteItem(vWriter, item[key], key, false, item.IsArray); } vWriter.Close(); var data = stream.ToArray(); // length of data + length of length + null terminator writer.Write(data.Length + 4 + 1); writer.Write(data); writer.Write((byte)0x00); return; } switch (item.Type) { case 0x01: // double writer.Write((double)item.Value); break; case 0x02: // string var bytes = Encoding.UTF8.GetBytes(GetKeyName((string)item.Value, true)); writer.Write(bytes.Length + 1); // includes null terminator writer.WriteBytes(bytes); writer.Write((byte)0x00); break; case 0x05: // binary var byteArray = (byte[])item.Value; writer.Write(byteArray.Length); writer.Write((byte)0x00); writer.Write(byteArray); break; case 0x07: // objectid writer.Write((byte[])item.Value); break; case 0x08: // bool writer.Write((bool)item.Value); break; case 0x09: // UTC datetime case 0x11: // timestamp case 0x12: // 64 bit int writer.Write((long)item.Value); break; case 0x10: // 32 bit int writer.Write((int)item.Value); break; default: throw new NotSupportedException($"Item type {item.Type} not supported."); } }