コード例 #1
0
ファイル: RangeFilter.cs プロジェクト: thygrrr/rant3
 public override void Deserialize(EasyReader input)
 {
     if (input.ReadBoolean())
     {
         _min = input.ReadInt32();
     }
     if (input.ReadBoolean())
     {
         _max = input.ReadInt32();
     }
 }
コード例 #2
0
ファイル: RantPackage.cs プロジェクト: lzxkulou/Rant
        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);
        }
コード例 #3
0
ファイル: BsonDocument.cs プロジェクト: nicolasmaurice/Rant
        /// <summary>
        /// Reads a BSON document from the specified EasyReader.
        /// </summary>
        /// <param name="reader">The reader that will be used to read this document.</param>
        /// <returns>The document that was read.</returns>
        internal static BsonDocument Read(EasyReader reader, BsonDocument parent = null, bool inArray = false)
        {
            var stringTableMode = BsonStringTableMode.None;
            Dictionary <int, string> stringTable = null;

            if (parent == null)
            {
                var includesStringTable = reader.ReadBoolean();
                if (includesStringTable)
                {
                    stringTable     = new Dictionary <int, string>();
                    stringTableMode = (BsonStringTableMode)reader.ReadByte();
                    var version = reader.ReadByte();
                    if (version != STRING_TABLE_VERSION)
                    {
                        throw new InvalidDataException("Unsupported string table version: " + version);
                    }
                    var tableLength  = reader.ReadInt32();
                    var tableEntries = reader.ReadInt32();
                    for (var i = 0; i < tableEntries; i++)
                    {
                        var num = reader.ReadInt32();
                        var val = reader.ReadString(Encoding.UTF8);
                        stringTable[num] = val;
                    }
                }
            }
            else
            {
                stringTable     = parent.ReverseStringTable;
                stringTableMode = parent.StringTableMode;
            }
            var document = new BsonDocument(stringTableMode, stringTable);

            var length = reader.ReadInt32();

            while (!reader.EndOfStream)
            {
                var code = reader.ReadByte();
                if (code == 0x00) // end of document
                {
                    break;
                }
                var name = reader.ReadCString();
                if (!inArray && document.StringTableMode != BsonStringTableMode.None)
                {
                    name = document.ReverseStringTable[int.Parse(name)];
                }
                var data = ReadItem(code, document, reader);
                document.Top[name] = data;
            }
            return(document);
        }
コード例 #4
0
        internal void Deserialize(EasyReader input)
        {
            int typeCount = input.ReadInt32();

            for (int i = 0; i < typeCount; i++)
            {
                var type = input.ReadEnum <CarrierComponentType>();
                int num  = input.ReadInt32();
                for (int j = 0; j < num; j++)
                {
                    AddComponent(type, input.ReadString());
                }
            }
        }
コード例 #5
0
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            _query = new Query();

            _query.Name      = input.ReadString();
            _query.Subtype   = input.ReadString();
            _query.Exclusive = input.ReadBoolean();

            // Read filters
            int filterCount = input.ReadInt32();

            for (int i = 0; i < filterCount; i++)
            {
                var filter = Filter.GetFilterInstance(input.ReadUInt16());
                if (filter == null)
                {
                    continue;
                }
                filter.Deserialize(input);
                _query.AddFilter(filter);
            }

            // Carrier
            if (input.ReadBoolean())
            {
                _query.Carrier = new Carrier();
                _query.Carrier.Deserialize(input);
            }

            var complementRequest = new DeserializeRequest();

            yield return(complementRequest);

            _query.Complement = complementRequest.Result;
        }
コード例 #6
0
        public override void Deserialize(EasyReader input)
        {
            var options = RegexOptions.Compiled | RegexOptions.ExplicitCapture;

            Outcome  = input.ReadBoolean();
            options |= (RegexOptions)input.ReadInt32();
            Regex    = new Regex(input.ReadString(), options);
        }
コード例 #7
0
ファイル: RstEscape.cs プロジェクト: thygrrr/rant3
 protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
 {
     input.ReadChar(out _codeHigh);
     input.ReadChar(out _codeLow);
     input.ReadInt32(out _times);
     input.ReadBoolean(out _unicode);
     yield break;
 }
コード例 #8
0
ファイル: ClassFilter.cs プロジェクト: thygrrr/rant3
        public override void Deserialize(EasyReader input)
        {
            int count = input.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                int swLength = input.ReadInt32();
                var sw       = new ClassFilterRule[swLength];
                for (int j = 0; j < swLength; j++)
                {
                    bool   shouldMatch = input.ReadBoolean();
                    string clName      = input.ReadString();
                    sw[j] = new ClassFilterRule(clName, shouldMatch);
                }
                _items.Add(sw);
            }
        }
コード例 #9
0
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            input.ReadInt32(out _count);
            input.ReadBoolean(out _weighted);
            if (_weighted)
            {
                // Read constant weights
                if (input.ReadBoolean())
                {
                    input.ReadDouble(out _constantWeightSum);
                    _weights = new double[_count];
                    for (int i = 0; i < _count; i++)
                    {
                        input.ReadDouble(out _weights[i]);
                    }
                }

                // Read dynamic weights
                int numDW = input.ReadInt32();
                if (numDW > 0)
                {
                    _dynamicWeights = new List <_ <int, RST> >(numDW);
                    for (int i = 0; i < numDW; i++)
                    {
                        int index   = input.ReadInt32();
                        var request = new DeserializeRequest();
                        yield return(request);

                        _dynamicWeights.Add(new _ <int, RST>(index, request.Result));
                    }
                }
            }

            // Read elements
            _elements = new List <RST>(_count);
            for (int i = 0; i < _count; i++)
            {
                var request = new DeserializeRequest();
                yield return(request);

                _elements.Add(request.Result);
            }
        }
コード例 #10
0
ファイル: RstSequence.cs プロジェクト: thygrrr/rant3
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            int count = input.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                var request = new DeserializeRequest();
                yield return(request);

                Actions.Add(request.Result);
            }
        }
コード例 #11
0
ファイル: RstFunction.cs プロジェクト: thygrrr/rant3
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            input.ReadInt32(out _argc);
            string funcName = input.ReadString();

            _funcInfo = RantFunctionRegistry.GetFunction(funcName, _argc);
            if (_args == null)
            {
                _args = new List <RST>(_argc);
            }
            for (int i = 0; i < _argc; i++)
            {
                var request = new DeserializeRequest();
                yield return(request);

                _args.Add(request.Result);
            }
        }
コード例 #12
0
ファイル: RstDefineSubroutine.cs プロジェクト: sizzles/Rant
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            var iterMain = base.Deserialize(input);

            while (iterMain.MoveNext())
            {
                yield return(iterMain.Current);
            }
            int pCount = input.ReadInt32();

            if (Parameters == null)
            {
                Parameters = new Dictionary <string, SubroutineParameterType>(pCount);
            }
            for (int i = 0; i < pCount; i++)
            {
                string key = input.ReadString();
                Parameters[key] = (SubroutineParameterType)input.ReadByte();
            }
        }
コード例 #13
0
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            var iterMain = base.Deserialize(input);

            while (iterMain.MoveNext())
            {
                yield return(iterMain.Current);
            }
            int pCount = input.ReadInt32();

            if (Parameters == null)
            {
                Parameters = new List <SubroutineParameter>();
            }
            for (int i = 0; i < pCount; i++)
            {
                string paramName = input.ReadString();
                var    paramType = (SubroutineParameterType)input.ReadByte();
                Parameters.Add(new SubroutineParameter(paramName, paramType));
            }
        }
コード例 #14
0
        protected override IEnumerator <DeserializeRequest> Deserialize(EasyReader input)
        {
            var iterMain = base.Deserialize(input);

            while (iterMain.MoveNext())
            {
                yield return(iterMain.Current);
            }
            input.ReadBoolean(out _inModule);
            input.ReadString(out _moduleFunctionName);
            int argc = input.ReadInt32();

            if (Arguments == null)
            {
                Arguments = new List <RST>(argc);
            }
            for (int i = 0; i < argc; i++)
            {
                var request = new DeserializeRequest();
                yield return(request);

                Arguments.Add(request.Result);
            }
        }
コード例 #15
0
ファイル: RantPackage.cs プロジェクト: W-h-a-t-s/Rant
        /// <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))
            {
                var magic = Encoding.ASCII.GetString(reader.ReadBytes(4));
                if (magic == OLD_MAGIC)
                    return LoadOldPackage(reader);
                if (magic != MAGIC)
                    throw new InvalidDataException("File is corrupt.");
                var package = new RantPackage();
                var version = reader.ReadUInt32();
                if (version != PACKAGE_VERSION)
                    throw new InvalidDataException("Invalid package version: " + version);
                var compress = reader.ReadBoolean();
                var size = reader.ReadInt32();
                var data = reader.ReadBytes(size);
                if (compress)
                    data = EasyCompressor.Decompress(data);
                var doc = BsonDocument.Read(data);

                var info = doc["info"];
				if (info == null)
					throw new InvalidDataException("Metadata is missing from package.");

				package.Title = info["title"];
				package.ID = info["id"];
				package.Version = RantPackageVersion.Parse(info["version"]);
				package.Description = info["description"];
	            package.Authors = (string[])info["authors"];
				package.Tags = (string[])info["tags"];
				var deps = info["dependencies"];
				if (deps != null && deps.IsArray)
				{
					for(int i = 0; i < deps.Count; i++)
					{
						var dep = deps[i];
						var depId = dep["id"].Value;
						var depVersion = dep["version"].Value;
						bool depAllowNewer = (bool)dep["allow-newer"].Value;
						package.AddDependency(new RantPackageDependency(depId.ToString(), depVersion.ToString()) { AllowNewer = depAllowNewer });
					}
				}

				var patterns = doc["patterns"];
                if(patterns != null)
                {
                    var names = patterns.Keys;
                    foreach (string name in names)
                        package.AddPattern(new RantPattern(name, RantPatternOrigin.File, patterns[name]));
                }

                var tables = doc["tables"];
                if(tables != null)
                {
                    var names = tables.Keys;
                    foreach(string name in names)
                    {
                        var table = tables[name];
                        string tableName = table["name"];
                        string[] tableSubs = (string[])table["subs"];
                        string[] hiddenClasses = (string[])table["hidden"];

                        var entries = new List<RantDictionaryEntry>();
                        var entryList = table["entries"];
                        for(var i = 0; i < entryList.Count; i++)
                        {
                            var loadedEntry = entryList[i];
                            int weight = 1;
                            if (loadedEntry.HasKey("weight"))
                                weight = (int)loadedEntry["weight"].Value;
                            string[] requiredClasses = (string[])loadedEntry["classes"];
                            string[] optionalClasses = (string[])loadedEntry["optional_classes"];
                            var terms = new List<RantDictionaryTerm>();
                            var termList = loadedEntry["terms"];
                            for(var j = 0; j < termList.Count; j++)
                            {
                                var t = new RantDictionaryTerm(termList[j]["value"], termList[j]["pron"]);
                                terms.Add(t);
                            }
                            var entry = new RantDictionaryEntry(
                                terms.ToArray(),
                                requiredClasses.Concat(optionalClasses.Select(x => x + "?")),
                                weight
                            );
                            entries.Add(entry);
                        }
                        var rantTable = new RantDictionaryTable(
                            tableName,
                            tableSubs,
                            entries,
                            hiddenClasses
                        );
                        package.AddTable(rantTable);
                    }
                }

                return package;
            }
        }
コード例 #16
0
ファイル: RantPackage.cs プロジェクト: thygrrr/rant3
        /// <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);
            }
        }
コード例 #17
0
ファイル: RantPackage.cs プロジェクト: inkadnb/Rant
        /// <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;
            }
        }
コード例 #18
0
ファイル: BsonDocument.cs プロジェクト: nicolasmaurice/Rant
        private static BsonItem ReadItem(byte code, BsonDocument document, EasyReader reader)
        {
            object val = null;

            switch (code)
            {
            case 0x01:     // double
                val = reader.ReadDouble();
                break;

            case 0x02:     // string
                val = reader.ReadString(Encoding.UTF8).TrimEnd('\x00');
                if (document.StringTableMode == BsonStringTableMode.KeysAndValues)
                {
                    val = document.ReverseStringTable[int.Parse((string)val)];
                }
                break;

            case 0x03:     // document
                val = Read(reader, document).Top;
                break;

            case 0x04:     // array
                val = Read(reader, document, true).Top;
                break;

            case 0x05:     // binary
                var length  = reader.ReadInt32();
                var subtype = reader.ReadByte();
                if (subtype != 0x00)
                {
                    throw new NotSupportedException("BSON subtypes other than 'generic binary data' are not supported.");
                }
                val = reader.ReadBytes(length);
                break;

            case 0x06:     // undefined
                break;

            case 0x07:     // ObjectId
                // why does this parser support ObjectIds and not other binary data?
                // shhhhh
                val = Encoding.ASCII.GetString(reader.ReadBytes(12));
                break;

            case 0x08:     // boolean
                val = reader.ReadBoolean();
                break;

            case 0x09:     // UTC datetime
                val = reader.ReadInt64();
                break;

            case 0x0A:     // null
                break;

            case 0x0B:     // regex
                // why are you using regex in a Rant package?
                throw new NotSupportedException("Regular expressions are not supported.");

            case 0x0C:     // db pointer
                throw new NotSupportedException("DB pointers are not supported.");

            case 0x0D:     // Javascript code
            case 0x0F:     // JS code with scope
                throw new NotSupportedException("Javascript in BSON is not supported.");

            case 0x0E:     // depreceated
                val = reader.ReadString(Encoding.UTF8);
                break;

            case 0x10:     // 32 bit integer
                val = reader.ReadInt32();
                break;

            case 0x11:     // timestamp
            case 0x12:     // 64 bit integer
                val = reader.ReadInt64();
                break;

            case 0xFF:     // min key
            case 0x7F:     // max key
                // we don't care about these so let's just skip em
                break;
            }
            if (!(val is BsonItem))
            {
                return new BsonItem(val)
                       {
                           Type = code
                       }
            }
            ;
            var i = (BsonItem)val;

            i.Type = code;
            return(i);
        }
    }
コード例 #19
0
ファイル: BsonDocument.cs プロジェクト: W-h-a-t-s/Rant
 private static BsonItem ReadItem(byte code, BsonDocument document, EasyReader reader)
 {
     object val = null;
     switch (code)
     {
         case 0x01: // double
             val = reader.ReadDouble();
             break;
         case 0x02: // string
             val = reader.ReadString(Encoding.UTF8).TrimEnd('\x00');
             if (document.StringTableMode == BsonStringTableMode.KeysAndValues)
                 val = document.ReverseStringTable[int.Parse((string)val)];
             break;
         case 0x03: // document
             val = Read(reader, document).Top;
             break;
         case 0x04: // array
             val = Read(reader, document, true).Top;
             break;
         case 0x05: // binary
             var length = reader.ReadInt32();
             var subtype = reader.ReadByte();
             if (subtype != 0x00)
                 throw new NotSupportedException("BSON subtypes other than 'generic binary data' are not supported.");
             val = reader.ReadBytes(length);
             break;
         case 0x06: // undefined
             break;
         case 0x07: // ObjectId
             // why does this parser support ObjectIds and not other binary data?
             // shhhhh
             val = Encoding.ASCII.GetString(reader.ReadBytes(12));
             break;
         case 0x08: // boolean
             val = reader.ReadBoolean();
             break;
         case 0x09: // UTC datetime
             val = reader.ReadInt64();
             break;
         case 0x0A: // null
             break;
         case 0x0B: // regex
             // why are you using regex in a Rant package?
             throw new NotSupportedException("Regular expressions are not supported.");
         case 0x0C: // db pointer
             throw new NotSupportedException("DB pointers are not supported.");
         case 0x0D: // Javascript code
         case 0x0F: // JS code with scope
             throw new NotSupportedException("Javascript in BSON is not supported.");
         case 0x0E: // depreceated
             val = reader.ReadString(Encoding.UTF8);
             break;
         case 0x10: // 32 bit integer
             val = reader.ReadInt32();
             break;
         case 0x11: // timestamp
         case 0x12: // 64 bit integer
             val = reader.ReadInt64();
             break;
         case 0xFF: // min key
         case 0x7F: // max key
             // we don't care about these so let's just skip em
             break;
     }
     if (!(val is BsonItem))
         return new BsonItem(val) { Type = code };
     var i = (BsonItem)val;
     i.Type = code;
     return i;
 }
コード例 #20
0
ファイル: BsonDocument.cs プロジェクト: W-h-a-t-s/Rant
        /// <summary>
        /// Reads a BSON document from the specified EasyReader.
        /// </summary>
        /// <param name="reader">The reader that will be used to read this document.</param>
        /// <returns>The document that was read.</returns>
        internal static BsonDocument Read(EasyReader reader, BsonDocument parent = null, bool inArray = false)
        {
            var stringTableMode = BsonStringTableMode.None;
            Dictionary<int, string> stringTable = null;
            if (parent == null)
            {
                var includesStringTable = reader.ReadBoolean();
                if (includesStringTable)
                {
                    stringTable = new Dictionary<int, string>();
                    stringTableMode = (BsonStringTableMode)reader.ReadByte();
                    var version = reader.ReadByte();
                    if (version != STRING_TABLE_VERSION)
                        throw new InvalidDataException("Unsupported string table version: " + version);
                    var tableLength = reader.ReadInt32();
                    var tableEntries = reader.ReadInt32();
                    for (var i = 0; i < tableEntries; i++)
                    {
                        var num = reader.ReadInt32();
                        var val = reader.ReadString(Encoding.UTF8);
                        stringTable[num] = val;
                    }
                }
            }
            else
            {
                stringTable = parent.ReverseStringTable;
                stringTableMode = parent.StringTableMode;
            }
            var document = new BsonDocument(stringTableMode, stringTable);

            var length = reader.ReadInt32();
            while(!reader.EndOfStream)
            {
                var code = reader.ReadByte();
                if (code == 0x00) // end of document
                    break;
                var name = reader.ReadCString();
                if (!inArray && document.StringTableMode != BsonStringTableMode.None)
                    name = document.ReverseStringTable[int.Parse(name)];
                var data = ReadItem(code, document, reader);
                document.Top[name] = data;
            }
            return document;
        }
コード例 #21
0
ファイル: RST.cs プロジェクト: thygrrr/rant3
        public static RST DeserializeRST(EasyReader input)
        {
            Type rootType;

            if (!_rstTypeMap.TryGetValue(input.ReadUInt32(), out rootType) || rootType != typeof(RstSequence))
            {
                throw new InvalidDataException(GetString("err-pgmload-invalid-rst-root"));
            }

            int rootLine  = input.ReadInt32();
            int rootCol   = input.ReadInt32();
            int rootIndex = input.ReadInt32();
            var rootRST   = Activator.CreateInstance(rootType, new LineCol(rootLine, rootCol, rootIndex)) as RstSequence;

            if (rootRST == null)
            {
                throw new InvalidDataException(GetString("err-pgmload-root-instance-fail"));
            }

            var stack = new Stack <IEnumerator <DeserializeRequest> >(10);

            stack.Push(rootRST.DeserializeObject(input));

top:
            while (stack.Count > 0)
            {
                var deserializer = stack.Peek();

                while (deserializer.MoveNext())
                {
                    uint code = input.ReadUInt32();

                    if (code == NullRST)
                    {
                        deserializer.Current.SetResult(null);
                        continue;
                    }

                    Type type;
                    if (!_rstTypeMap.TryGetValue(code, out type))
                    {
                        throw new InvalidDataException(GetString("err-pgmload-bad-type", code));
                    }

                    int line  = input.ReadInt32();
                    int col   = input.ReadInt32();
                    int index = input.ReadInt32();
                    var rst   = Activator.CreateInstance(type, new LineCol(line, col, index)) as RST;
                    if (rst == null)
                    {
                        throw new InvalidDataException(GetString("err-pgmload-rst-creation-fail", type.Name));
                    }
                    deserializer.Current.SetResult(rst);
                    stack.Push(rst.DeserializeObject(input));
                    goto top;
                }

                stack.Pop();
            }

            return(rootRST);
        }
コード例 #22
0
ファイル: RantPackage.cs プロジェクト: lzxkulou/Rant
        /// <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))
            {
                var magic = Encoding.ASCII.GetString(reader.ReadBytes(4));
                if (magic == OLD_MAGIC)
                {
                    return(LoadOldPackage(reader));
                }
                if (magic != MAGIC)
                {
                    throw new InvalidDataException("File is corrupt.");
                }
                var package = new RantPackage();
                var version = reader.ReadUInt32();
                if (version != PACKAGE_VERSION)
                {
                    throw new InvalidDataException("Invalid package version: " + version);
                }
                var compress = reader.ReadBoolean();
                var size     = reader.ReadInt32();
                var data     = reader.ReadBytes(size);
                if (compress)
                {
                    data = EasyCompressor.Decompress(data);
                }
                var doc = BsonDocument.Read(data);

                var info = doc["info"];
                if (info == null)
                {
                    throw new InvalidDataException("Metadata is missing from package.");
                }

                package.Title       = info["title"];
                package.ID          = info["id"];
                package.Version     = RantPackageVersion.Parse(info["version"]);
                package.Description = info["description"];
                package.Authors     = (string[])info["authors"];
                package.Tags        = (string[])info["tags"];
                var deps = info["dependencies"];
                if (deps != null && deps.IsArray)
                {
                    for (int i = 0; i < deps.Count; i++)
                    {
                        var  dep           = deps[i];
                        var  depId         = dep["id"].Value;
                        var  depVersion    = dep["version"].Value;
                        bool depAllowNewer = (bool)dep["allow-newer"].Value;
                        package.AddDependency(new RantPackageDependency(depId.ToString(), depVersion.ToString())
                        {
                            AllowNewer = depAllowNewer
                        });
                    }
                }

                var patterns = doc["patterns"];
                if (patterns != null)
                {
                    var names = patterns.Keys;
                    foreach (string name in names)
                    {
                        package.AddPattern(new RantPattern(name, RantPatternOrigin.File, patterns[name]));
                    }
                }

                var tables = doc["tables"];
                if (tables != null)
                {
                    var names = tables.Keys;
                    foreach (string name in names)
                    {
                        var      table         = tables[name];
                        string   tableName     = table["name"];
                        string[] tableSubs     = (string[])table["subs"];
                        string[] hiddenClasses = (string[])table["hidden"];

                        var entries   = new List <RantDictionaryEntry>();
                        var entryList = table["entries"];
                        for (var i = 0; i < entryList.Count; i++)
                        {
                            var loadedEntry = entryList[i];
                            int weight      = 1;
                            if (loadedEntry.HasKey("weight"))
                            {
                                weight = (int)loadedEntry["weight"].Value;
                            }
                            string[] requiredClasses = (string[])loadedEntry["classes"];
                            string[] optionalClasses = (string[])loadedEntry["optional_classes"];
                            var      terms           = new List <RantDictionaryTerm>();
                            var      termList        = loadedEntry["terms"];
                            for (var j = 0; j < termList.Count; j++)
                            {
                                var t = new RantDictionaryTerm(termList[j]["value"], termList[j]["pron"]);
                                terms.Add(t);
                            }
                            var entry = new RantDictionaryEntry(
                                terms.ToArray(),
                                requiredClasses.Concat(optionalClasses.Select(x => x + "?")),
                                weight
                                );
                            entries.Add(entry);
                        }
                        var rantTable = new RantDictionaryTable(
                            tableName,
                            tableSubs,
                            entries,
                            hiddenClasses
                            );
                        package.AddTable(rantTable);
                    }
                }

                return(package);
            }
        }
コード例 #23
0
        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);
            }
        }