private static RantPackage LoadOldPackage(EasyReader reader) { int numPatterns = reader.ReadInt32(); int numTables = reader.ReadInt32(); if (numPatterns < 0 || numTables < 0) { throw new InvalidDataException("File is corrupt."); } var pkg = new RantPackage(); // Patterns for (int i = 0; i < numPatterns; i++) { var name = reader.ReadString(); var code = reader.ReadString(); pkg.AddPattern(new RantPattern(name, RantPatternOrigin.String, code)); } // Tables for (int i = 0; i < numTables; i++) { var name = reader.ReadString(); var subs = reader.ReadStringArray(); int numEntries = reader.ReadInt32(); var hiddenClasses = reader.ReadStringArray(); var entries = new RantDictionaryEntry[numEntries]; for (int j = 0; j < numEntries; j++) { int weight = reader.ReadInt32(); bool flags = reader.ReadBoolean(); // unused int numTerms = reader.ReadInt32(); var terms = new RantDictionaryTerm[numTerms]; for (int k = 0; k < numTerms; k++) { var value = reader.ReadString(); var pron = reader.ReadString(); terms[k] = new RantDictionaryTerm(value, pron); } var classes = reader.ReadStringArray(); entries[j] = new RantDictionaryEntry(terms, classes, weight); } pkg.AddTable(new RantDictionaryTable(name, subs, entries, hiddenClasses)); } return(pkg); }
/// <summary> /// Loads a package from the specified stream and returns it as a RantPackage object. /// </summary> /// <param name="source">The stream to load the package data from.</param> /// <returns></returns> public static RantPackage Load(Stream source) { using (var reader = new EasyReader(source)) { string magic = Encoding.ASCII.GetString(reader.ReadBytes(4)); if (magic != MAGIC) { throw new InvalidDataException(GetString("err-file-corrupt")); } ushort version = reader.ReadUInt16(); if (version != PACKAGE_FORMAT_VERSION) { throw new InvalidDataException(GetString("err-invalid-package-version", version)); } bool compress = reader.ReadBoolean(); var package = new RantPackage(); package.Title = reader.ReadString(); package.ID = reader.ReadString(); package.Description = reader.ReadString(); package.Tags = reader.ReadStringArray(); package.Authors = reader.ReadStringArray(); int vmaj = reader.ReadInt32(); int vmin = reader.ReadInt32(); int vrev = reader.ReadInt32(); package.Version = new RantPackageVersion(vmaj, vmin, vrev); int depCount = reader.ReadInt32(); for (int i = 0; i < depCount; i++) { var depId = reader.ReadString(); int depVerMaj = reader.ReadInt32(); int depVerMin = reader.ReadInt32(); int depVerRev = reader.ReadInt32(); bool depAllowNewer = reader.ReadBoolean(); package.AddDependency(new RantPackageDependency(depId, new RantPackageVersion(depVerMaj, depVerMin, depVerRev)) { AllowNewer = depAllowNewer }); } int resCount = reader.ReadInt32(); if (compress) { using (var decompressStream = new DeflateStream(reader.BaseStream, CompressionMode.Decompress, true)) { for (int i = 0; i < resCount; i++) { package._resources.Add(RantResource.DeserializeResource(new EasyReader(decompressStream, true))); } } } else { for (int i = 0; i < resCount; i++) { package._resources.Add(RantResource.DeserializeResource(reader)); } } return(package); } }
private static string[] ReadStringsFromBinary(string path) { using (EasyReader reader = new EasyReader(path)) { return reader.ReadStringArray(); } }
/// <summary> /// Loads a package from the specified stream and returns it as a RantPackage object. /// </summary> /// <param name="source">The stream to load the package data from.</param> /// <returns></returns> public static RantPackage Load(Stream source) { using (var reader = new EasyReader(source)) { if (Encoding.ASCII.GetString(reader.ReadBytes(4)) != Magic) throw new InvalidDataException("File is corrupt."); int numPatterns = reader.ReadInt32(); int numTables = reader.ReadInt32(); if (numPatterns < 0 || numTables < 0) throw new InvalidDataException("File is corrupt."); var pkg = new RantPackage(); // Patterns for (int i = 0; i < numPatterns; i++) { var name = reader.ReadString(); var code = reader.ReadString(); pkg.AddPattern(new RantPattern(name, RantPatternSource.String, code)); } // Tables for (int i = 0; i < numTables; i++) { var name = reader.ReadString(); var subs = reader.ReadStringArray(); int numEntries = reader.ReadInt32(); var hiddenClasses = reader.ReadStringArray(); var entries = new RantDictionaryEntry[numEntries]; for (int j = 0; j < numEntries; j++) { int weight = reader.ReadInt32(); bool flags = reader.ReadBoolean(); // unused int numTerms = reader.ReadInt32(); var terms = new RantDictionaryTerm[numTerms]; for (int k = 0; k < numTerms; k++) { var value = reader.ReadString(); var pron = reader.ReadString(); terms[k] = new RantDictionaryTerm(value, pron); } var classes = reader.ReadStringArray(); entries[j] = new RantDictionaryEntry(terms, classes, weight); } pkg.AddTable(new RantDictionaryTable(name, subs, entries, hiddenClasses)); } return pkg; } }
internal override void DeserializeData(EasyReader reader) { this.Name = reader.ReadString(); this.Language = reader.ReadString(); this.TermsPerEntry = reader.ReadInt32(); for (int i = 0; i < TermsPerEntry; i++) { foreach (var subtype in reader.ReadStringArray()) { AddSubtype(subtype, i); } } _hidden.AddRange(reader.ReadStringArray()); int numEntries = reader.ReadInt32(); for (int i = 0; i < numEntries; i++) { var terms = new RantDictionaryTerm[TermsPerEntry]; for (int j = 0; j < TermsPerEntry; j++) { var value = reader.ReadString(); var pron = reader.ReadString(); int valueSplit = reader.ReadInt32(); int pronSplit = reader.ReadInt32(); terms[j] = new RantDictionaryTerm(value, pron, valueSplit, pronSplit); } float weight = reader.ReadSingle(); var entry = new RantDictionaryEntry(terms) { Weight = weight }; foreach (var reqClass in reader.ReadStringArray()) { entry.AddClass(reqClass, false); } foreach (var optClass in reader.ReadStringArray()) { entry.AddClass(optClass, true); } int metaCount = reader.ReadInt32(); for (int j = 0; j < metaCount; j++) { bool isArray = reader.ReadBoolean(); var key = reader.ReadString(); if (isArray) { entry.SetMetadata(key, reader.ReadStringArray()); } else { entry.SetMetadata(key, reader.ReadString()); } } AddEntry(entry); } }