コード例 #1
0
ファイル: EmpyrionObject.cs プロジェクト: Lyceq/empyrion-dse
        public virtual void ParseConfig(ConfigurationReader config)
        {
            KeyValuePair <string, string> line = config.ReadParsedLine();

            while (line.Key != null)
            {
                if (line.Key == "{")
                {
                    if (string.IsNullOrEmpty(line.Value))
                    {
                        // Just an untyped child object.
                        this.Children.Add(new EmpyrionObject(
                                              this.Configuration,
                                              new Tuple <EmpyrionObjectType, int?, string, string>(EmpyrionObjectType.None, null, null, null),
                                              config));
                    }
                    else
                    {
                        string[] header = line.Value.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        this.Children.Add(new ChildObject(
                                              this.Configuration,
                                              new Tuple <EmpyrionObjectType, int?, string, string>(EmpyrionObjectType.Child, null, header[1], null),
                                              config));
                    }
                }
                else if (line.Key == "}")
                {
                    // End of this object.
                    return;
                }
                else
                {
                    // Assumed to be a new property.
                    Property p = new Property($"{line.Key}: {line.Value}");
                    this.Properties[p.Key.ToLower()] = p;
                }

                line = config.ReadParsedLine();
            }
        }
コード例 #2
0
        public void Load(string path)
        {
            Log.Info("Loading configuration file " + path);
            using (StreamReader file = File.OpenText(path))
            {
                ConfigurationReader           reader = new ConfigurationReader(file);
                KeyValuePair <string, string> line   = reader.ReadParsedLine();
                while (line.Key != null)
                {
                    if (line.Key == "{")
                    {
                        // Line starts the definition of a new object. Detect the object type and pass control to the object for reading its configuration.
                        Tuple <EmpyrionObjectType, int?, string, string> header = ParseObjectHeader(line.Value);
                        string key = (header.Item2?.ToString() ?? "") + header.Item3;
                        if (this.ObjectsByKey.ContainsKey(key))
                        {
                            this.ObjectsByKey[key].ParseConfig(reader);
                        }
                        else
                        {
                            EmpyrionObject obj = null;
                            switch (header.Item1)
                            {
                            case EmpyrionObjectType.Block:
                                Block block = new Block(this, header, reader);
                                obj = block;
                                this.BlocksByID[block.ID ?? -1] = block;
                                this.BlocksByName[block.Name]   = block;
                                break;

                            case EmpyrionObjectType.Container:
                                obj = new EmpyrionObject(this, header, reader);
                                this.ContainersByID[obj.ID ?? -1] = obj;
                                break;

                            case EmpyrionObjectType.Entity:
                                obj = new EmpyrionObject(this, header, reader);
                                this.EntitiesByName[obj.Name] = obj;
                                break;

                            case EmpyrionObjectType.Item:
                                obj = new EmpyrionObject(this, header, reader);
                                this.ItemsByID[obj.ID ?? -1] = obj;
                                this.ItemsByName[obj.Name]   = obj;
                                break;

                            case EmpyrionObjectType.LootGroup:
                                obj = new EmpyrionObject(this, header, reader);
                                this.LootGroupsByName[obj.Name] = obj;
                                break;

                            case EmpyrionObjectType.Template:
                                CraftingTemplate template = new CraftingTemplate(this, header, reader);
                                obj = template;
                                this.TemplatesByName[template.Name] = template;
                                break;

                            default: obj = new EmpyrionObject(this, header, reader); break;
                            }

                            this.ObjectsByKey[obj.Key] = obj;
                            if (obj.ID != null)
                            {
                                this.ObjectsByID[obj.ID ?? -1] = obj;
                            }
                            if (!string.IsNullOrWhiteSpace(obj.Name))
                            {
                                this.ObjectsByName[obj.Name] = obj;
                            }
                        }
                    }
                    else if (line.Key.ToLower() == "version")
                    {
                        this.Version = Int32.Parse(line.Value);
                    }
                    else
                    {
                        throw new Exception($"Unparsable line encountered: {line}");
                    }

                    line = reader.ReadParsedLine();
                }
            }
        }