コード例 #1
0
ファイル: ConfigReader.cs プロジェクト: xielong87/Essentials
        public static bool LoadConfig2()
        {
            Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loading unmerged system/template portal configuration file.");
            try
            {
                // Check for local config file first
                var filePath = Global.FilePathPrefix + ConfigWriter.LocalConfigFolder + Global.DirectorySeparator + Global.ConfigFileName;

                bool localConfigFound = false;

                Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to load Local config file: '{0}'", filePath);

                // Check for local config directory first

                var configFiles = GetConfigFiles(filePath);

                if (configFiles != null)
                {
                    if (configFiles.Length > 1)
                    {
                        Debug.Console(0, Debug.ErrorLogLevel.Error,
                                      "****Error: Multiple Local Configuration files present. Please ensure only a single file exists and reset program.****");
                        return(false);
                    }
                    else if (configFiles.Length == 1)
                    {
                        localConfigFound = true;
                        Debug.Console(0, Debug.ErrorLogLevel.Notice, "Found Local config file: '{0}'", filePath);
                    }
                }
                else
                {
                    Debug.Console(0, Debug.ErrorLogLevel.Notice,
                                  "Local Configuration file not present.", filePath);
                }

                // Check for Portal Config
                if (!localConfigFound)
                {
                    filePath = Global.FilePathPrefix + Global.ConfigFileName;

                    Debug.Console(0, Debug.ErrorLogLevel.Notice, "Attempting to load Portal config file: '{0}'", filePath);

                    configFiles = GetConfigFiles(filePath);

                    if (configFiles != null)
                    {
                        if (configFiles.Length > 1)
                        {
                            Debug.Console(0, Debug.ErrorLogLevel.Error,
                                          "****Error: Multiple Portal Configuration files present. Please ensure only a single file exists and reset program.****");
                            return(false);
                        }
                        else
                        {
                            Debug.Console(0, Debug.ErrorLogLevel.Notice, "Found Portal config file: '{0}'", filePath);
                        }
                    }
                    else
                    {
                        Debug.Console(0, Debug.ErrorLogLevel.Error,
                                      "ERROR: Portal Configuration file not present. Please load file and reset program.");
                        return(false);
                    }
                }

                // Get the actual file path
                filePath = configFiles[0].FullName;

                // Read the file
                using (StreamReader fs = new StreamReader(filePath))
                {
                    Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loading config file: '{0}'", filePath);

                    if (localConfigFound)
                    {
                        ConfigObject = JObject.Parse(fs.ReadToEnd()).ToObject <EssentialsConfig>();

                        Debug.Console(0, Debug.ErrorLogLevel.Notice, "Successfully Loaded Local Config");

                        return(true);
                    }
                    else
                    {
                        var doubleObj = JObject.Parse(fs.ReadToEnd());
                        ConfigObject = PortalConfigReader.MergeConfigs(doubleObj).ToObject <EssentialsConfig>();

                        // Extract SystemUrl and TemplateUrl into final config output

                        if (doubleObj["system_url"] != null)
                        {
                            ConfigObject.SystemUrl = doubleObj["system_url"].Value <string>();
                        }

                        if (doubleObj["template_url"] != null)
                        {
                            ConfigObject.TemplateUrl = doubleObj["template_url"].Value <string>();
                        }
                    }

                    Debug.Console(0, Debug.ErrorLogLevel.Notice, "Successfully Loaded Merged Config");

                    return(true);
                }
            }
            catch (Exception e)
            {
                Debug.Console(0, Debug.ErrorLogLevel.Error, "ERROR: Config load failed: \r{0}", e);
                return(false);
            }
        }
コード例 #2
0
        /// <summary>
        /// Read, evaluate and udpate status
        /// </summary>
        public void EvaluateFile(string portalFilepath)
        {
            PortalFilepath = portalFilepath;

            OnBoolChange(false, 0, JsonToSimplConstants.JsonIsValidBoolChange);
            if (string.IsNullOrEmpty(PortalFilepath))
            {
                CrestronConsole.PrintLine("Cannot evaluate file. JSON file path not set");
                return;
            }

            // Resolve possible wildcarded filename

            // If the portal file is xyz.json, then
            // the file we want to check for first will be called xyz.local.json
            var localFilepath = Path.ChangeExtension(PortalFilepath, "local.json");

            Debug.Console(0, this, "Checking for local file {0}", localFilepath);
            var actualLocalFile = GetActualFileInfoFromPath(localFilepath);

            if (actualLocalFile != null)
            {
                ActualFilePath = actualLocalFile.FullName;
                OnStringChange(ActualFilePath, 0, JsonToSimplConstants.ActualFilePathChange);
            }
            // If the local file does not exist, then read the portal file xyz.json
            // and create the local.
            else
            {
                Debug.Console(1, this, "Local JSON file not found {0}\rLoading portal JSON file", localFilepath);
                var actualPortalFile = GetActualFileInfoFromPath(portalFilepath);
                if (actualPortalFile != null)
                {
                    var newLocalPath = Path.ChangeExtension(actualPortalFile.FullName, "local.json");
                    // got the portal file, hand off to the merge / save method
                    PortalConfigReader.ReadAndMergeFileIfNecessary(actualPortalFile.FullName, newLocalPath);
                    ActualFilePath = newLocalPath;
                    OnStringChange(ActualFilePath, 0, JsonToSimplConstants.ActualFilePathChange);
                }
                else
                {
                    var msg = string.Format("Portal JSON file not found: {0}", PortalFilepath);
                    Debug.Console(1, this, msg);
                    ErrorLog.Error(msg);
                    return;
                }
            }

            // At this point we should have a local file.  Do it.
            Debug.Console(1, "Reading local JSON file {0}", ActualFilePath);

            string json = File.ReadToEnd(ActualFilePath, System.Text.Encoding.ASCII);

            try
            {
                JsonObject = JObject.Parse(json);
                foreach (var child in Children)
                {
                    child.ProcessAll();
                }
                OnBoolChange(true, 0, JsonToSimplConstants.JsonIsValidBoolChange);
            }
            catch (Exception e)
            {
                var msg = string.Format("JSON parsing failed:\r{0}", e);
                CrestronConsole.PrintLine(msg);
                ErrorLog.Error(msg);
                return;
            }
        }