private static void LoadConfigs()
        {
            // clear config cache so it can be rebuilt
            lastConfigs.Clear();

            var load = new LoadVisitor();

            foreach (KeyValuePair <string, ReflectedConfig> pair in ConfigReflection.Instance.Configs)
            {
                ConfigNode[] nodes = GameDatabase.Instance.GetConfigNodes(pair.Key);
                if (pair.Value.Reflection.AllowMultiple)
                {
                    var root = new ConfigNode();
                    foreach (ConfigNode configNode in nodes)
                    {
                        root.AddNode(configNode);
                    }

                    load.Node = root;
                    object instance = pair.Value.Instance;
                    int    errors   = pair.Value.Reflection.Load(load, ref instance);

                    if (errors > 0)
                    {
                        FARLogger.ErrorFormat("{0} errors while loading {1}", errors.ToString(), pair.Key);
                    }
                }
                else
                {
                    if (nodes.Length == 0)
                    {
                        FARLogger.Warning($"Could not find config nodes {pair.Key}");
                        continue;
                    }

                    if (nodes.Length > 1)
                    {
                        FARLogger.Warning($"Found {nodes.Length.ToString()} {pair.Key} nodes");
                    }

                    foreach (ConfigNode node in nodes)
                    {
                        load.Node = node;
                        object instance = pair.Value.Instance;
                        int    errors   = pair.Value.Reflection.Load(load, ref instance);

                        if (errors > 0)
                        {
                            FARLogger.ErrorFormat("{0} errors while loading {1}", errors.ToString(), node.name);
                        }
                    }
                }
            }

            using LoadGuard guard = Guard(Operation.Saving);
            SaveConfigs("Custom", true, ".cfg.far", FARConfig.Debug.DumpOnLoad);
        }
        private static IEnumerator DoSetup()
        {
            using LoadGuard guard = Guard(Operation.None);

            Task task = Task.Factory.StartNew(() => ConfigReflection.Instance.Initialize());

            while (!task.IsCompleted)
            {
                yield return(null);
            }

            if (task.Exception != null)
            {
                FARLogger.Exception(task.Exception, "Exception while setting up config");
            }
        }
        private IEnumerator DoLoadConfig()
        {
            if (loading)
            {
                yield break;
            }

            while (saving)
            {
                yield return(null);
            }

            using LoadGuard guard = Guard(Operation.Loading);
            Task task = Task.Factory.StartNew(LoadConfigs);

            yield return(new WaitWhile(() => !task.IsCompleted));

            if (task.Exception != null)
            {
                FARLogger.Exception(task.Exception, "Exception while loading config");
            }
        }
        public static IEnumerator SaveAll()
        {
            if (Instance.saving)
            {
                yield break;
            }

            while (Instance.loading)
            {
                yield return(null);
            }

            using LoadGuard guard = Guard(Operation.Saving);

            Task task = Task.Factory.StartNew(Save);

            yield return(new WaitUntil(() => task.IsCompleted));

            if (task.Exception != null)
            {
                FARLogger.Exception(task.Exception, "Exception while saving up configs");
            }
        }
 public static void Save()
 {
     using LoadGuard guard = Guard(Operation.Saving);
     SaveConfigs("Custom");
 }