Beispiel #1
0
        public static void LoadFactions()
        {
            Prefabs?.ForEach(set => set.Dispose());
            Prefabs = new List <FactionPrefab>();
            IEnumerable <ContentFile> files = GameMain.Instance.GetFilesOfType(ContentType.Factions);

            foreach (ContentFile file in files)
            {
                XDocument doc         = XMLExtensions.TryLoadXml(file.Path);
                XElement? rootElement = doc?.Root;

                if (doc == null || rootElement == null)
                {
                    continue;
                }

                if (doc.Root.IsOverride())
                {
                    Prefabs.Clear();
                    DebugConsole.NewMessage($"Overriding all factions with '{file.Path}'", Color.Yellow);
                }

                foreach (XElement element in rootElement.Elements())
                {
                    bool     isOverride    = element.IsOverride();
                    XElement sourceElement = isOverride ? element.FirstElement() : element;
                    string   elementName   = sourceElement.Name.ToString().ToLowerInvariant();
                    string   identifier    = sourceElement.GetAttributeString("identifier", null);

                    if (string.IsNullOrWhiteSpace(identifier))
                    {
                        DebugConsole.ThrowError($"No identifier defined for the faction config '{elementName}' in file '{file.Path}'");
                        continue;
                    }

                    var existingParams = Prefabs.Find(set => set.Identifier == identifier);
                    if (existingParams != null)
                    {
                        if (isOverride)
                        {
                            DebugConsole.NewMessage($"Overriding faction config '{identifier}' using the file '{file.Path}'", Color.Yellow);
                            Prefabs.Remove(existingParams);
                        }
                        else
                        {
                            DebugConsole.ThrowError($"Duplicate faction config: '{identifier}' defined in {elementName} of '{file.Path}'");
                            continue;
                        }
                    }

                    Prefabs.Add(new FactionPrefab(element));
                }
            }
        }