/// <summary> /// initializes <see cref="SettingsContainer"/> and corresponding data file. /// </summary> /// <seealso cref="initializeSettingsContainerFromExistingDataFile"/> /// <seealso cref="initializeDefaultSettingsContainerAndDataFile"/> /// <exception cref="FileReadSettingsInitializationException"> /// thrown if reading data from <see cref="SettingsContainer"/> file failed /// </exception> /// <exception cref="CorruptFileSettingsInitializationException"> /// thrown if <see cref="SettingsContainer"/> file data is not in a valid JSON format, /// or <see cref="SettingsContainer"/> parsing from data failed /// </exception> /// <exception cref="FileCreateSettingsInitializationException"> /// thrown if creating a <see cref="SettingsContainer"/> data file with default settings /// failed /// </exception> private void initializeSettings() { if (FileIOUtils.FileExists(SETTINGS_FILE_PATH)) // settings file exists { ConsoleIOManager.Instance.LogNotice( "Settings file found. Using existing file.", ConsoleIOManager.eOutputReportType.System); try { initializeSettingsContainerFromExistingDataFile(); } catch (Exception exception) { if (exception is FileReadException) // settings file read failed { throw new FileReadSettingsInitializationException( SETTINGS_FILE_PATH, exception); } // corrupt settings file else if ( exception is JsonSerializationException || exception is SettingsContainerJsonObjectParseException) { throw new CorruptFileSettingsInitializationException( SETTINGS_FILE_PATH, exception); } else // unhandled exception { throw exception; } } } else // settings file does not exist { ConsoleIOManager.Instance.LogNotice( "Settings file not found. Creating new settings file with default values ..", ConsoleIOManager.eOutputReportType.System); try { initializeDefaultSettingsContainerAndDataFile(); ConsoleIOManager.Instance.LogNotice( "New Settings file created successfully.", ConsoleIOManager.eOutputReportType.System); } catch (FileWriteException fileWriteException) { throw new FileCreateSettingsInitializationException( SETTINGS_FILE_PATH, fileWriteException); } } }
/// <summary> /// initializes <see cref="UserDefinedCommandContainer"/> and corresponding data file. /// </summary> /// <exception cref="FileReadUserDefinedCommandsInitializationException"> /// thrown if reading data from existing <see cref="UserDefinedCommandContainer"/> /// data file failed /// </exception> /// <exception cref="CorruptFileUserDefinedCommandsInitializationException"> /// thrown if data in existing <see cref="UserDefinedCommandContainer"/> data file /// is invalid /// </exception> private void initializeUserDefinedCommands() { if (FileIOUtils.FileExists(USER_DEFINED_COMMANDS_FILE_PATH)) // saved commands file exists { try { // initialize UserDefinedCommandsContainer with data from file string userDefinedCommandsContainerJsonString = FileIOUtils.ReadTextFromFile( USER_DEFINED_COMMANDS_FILE_PATH); this.userDefinedCommandContainer = JsonUtils.DeserializeObject <UserDefinedCommandContainer>( userDefinedCommandsContainerJsonString); } catch (Exception exception) { // reading from user defined commands file failed if (exception is FileReadException) { throw new FileReadUserDefinedCommandsInitializationException( USER_DEFINED_COMMANDS_FILE_PATH, exception); } // corrupt user defined commands file else if (exception is JsonSerializationException) { throw new CorruptFileUserDefinedCommandsInitializationException( USER_DEFINED_COMMANDS_FILE_PATH, exception); } else // unhandled exception { throw exception; } } } else // saved commands file does not exist { // initialize an empty UserDefinedCommandContainer this.userDefinedCommandContainer = new UserDefinedCommandContainer(); // write empty UserDefinedCommandContainer data to file writeUserDefinedCommandContainerDataToFile(); } }