コード例 #1
0
        private static IniConfigSource LoadConfig(string location)
        {
            IniConfigSource currentConfig      = new IniConfigSource();
            List <string>   currentConfigLines = new List <string>();

            string[] configLines = null;

            if (IsUrl(location))
            {
                // Web-based loading
                string responseStr;
                if (WebUtil.TryGetUrl(location, out responseStr))
                {
                    configLines = responseStr.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                }
                else
                {
                    m_log.Error("Failed to load web config file " + location + ": " + responseStr);
                }
            }
            else
            {
                // Local file loading
                try
                {
                    configLines = new List <string>(File.ReadAllLines(location)).ToArray();
                }
                catch (Exception ex)
                {
                    m_log.Error("Failed to load config file " + location + ": " + ex.Message);
                }
            }

            if (configLines != null)
            {
                for (int i = 0; i < configLines.Length; i++)
                {
                    string line = configLines[i].Trim();

                    if (line.StartsWith("Include "))
                    {
                        // Compile the current config lines, compile the included config file, and combine them
                        currentConfig.Merge(CompileConfig(currentConfigLines));
                        currentConfigLines.Clear();

                        // Compile the included config file
                        string          includeFilename = GetConfigPath(line.Substring(8).Trim());
                        IniConfigSource includeConfig   = LoadConfig(includeFilename);

                        // Merge the included config with the curent config
                        currentConfig.Merge(includeConfig);
                    }
                    else if (!String.IsNullOrEmpty(line) && !line.StartsWith(";"))
                    {
                        currentConfigLines.Add(line);
                    }
                }

                currentConfig.Merge(CompileConfig(currentConfigLines));
            }

            return(currentConfig);
        }