Esempio n. 1
0
        public ProtocolDatabase(int version)
        {
            m_Protocols = new Dictionary <int, ProtocolDefinition>();

            var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var protoDir  = Path.Combine(directory, ConfigurationManager.AppSettings["ProtocolFolder"]);



            var filenames = Directory.GetFiles(protoDir, "*.def").OrderByDescending(s => s);

            foreach (var filename in filenames)
            {
                string shortName = Path.GetFileName(filename).Split('.')[0];
                int    code      = OpcodeDatabase.Instance.GetOpcodeByName(shortName);
                if (code != 0 && !m_Protocols.ContainsKey(code))
                {
                    var stream     = File.OpenRead(filename);
                    var definition = ProtocolDefinition.ParseFromStream(stream, shortName, version);
                    if (definition != null)
                    {
                        m_Protocols.Add(code, definition);
                    }
                }
            }

            OpcodeDatabase.Instance.NamesToOpcodes.Where(kvp => !m_Protocols.ContainsKey(kvp.Value))
            .ToList()
            .ForEach(kvp => m_Protocols.Add(kvp.Value, new ProtocolDefinition(kvp.Key)));
        }
        public static ProtocolDefinition ParseFromStream(Stream stream, string name, int currentVersion)
        {
            ProtocolDefinition result = new ProtocolDefinition(name);

            var termsDictionary = Enum.GetValues(typeof(ProtocolTerm)).Cast <ProtocolTerm>().ToDictionary(e => e.ToString().ToLowerInvariant(), e => e);

            using (StreamReader reader = new StreamReader(stream))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (!string.IsNullOrWhiteSpace(line))
                    {
                        if (line.StartsWith("#"))
                        {
                            if (line.StartsWith("# majorPatchVersion >= "))
                            {
                                int v = 0;
                                if (Int32.TryParse(line.Substring(23), out v) && v > currentVersion)
                                {
                                    return(null);
                                }
                            }

                            continue;
                        }
                        line = line.TrimStart();
                        line = line.Replace("-", "- ");
                        string[] words = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        int      i     = 0;
                        while (words[i].CompareTo("-") == 0)
                        {
                            i++;
                        }
                        ProtocolTerm term;
                        if (words.Length >= i + 2 && termsDictionary.TryGetValue(words[i], out term))
                        {
                            result.Lines.Add(new ProtocolLine(i, term, words[i + 1]));
                        }
                        else
                        {
                        }
                    }
                }
            }
            return(result);
        }