/// <summary> /// Implements a configuration type /// </summary> /// <typeparam name="TConfig">The type of config to implement</typeparam> public ConfigImplementer Implement <TConfig>() { Type configType = typeof(TConfig); // Get config type var configAttribute = (ConfigAttribute)configType.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault(); // Get ConfigAttribute from the class var implementation = new ConfigImplementation <TConfig>(); // If the class is not marked as config, throw exception if (configAttribute == null) { if (GlobalSettings.UseConsole) { Logger.LogError($"The type {configType.ToString()} is not a valid Config type."); return(this); } else { throw new BadConfigTypeException($"The type {configType.ToString()} is not a valid Config type."); } } // If the type is already implemented, throw exception if (mConfigImplementations.Any(x => x.Type == configType)) { if (GlobalSettings.UseConsole) { Logger.LogError($"The configuration type {configType.ToString()} is already implemented."); return(this); } else { throw new DuplicatedConfigException($"The configuration type {configType.ToString()} is already implemented."); } } // Configure the implementation implementation.Name = configAttribute.Name != null ? configAttribute.Name : configType.Name; // Set the name implementation.Type = configType; // Set type // Check if its needs migration //int migrationNeeded = MigrationNeeded(implementation); //if (migrationNeeded != 0) // MigrateFiles(migrationNeeded, implementation); // Migrate if necessary //else implementation.File = new ConfigFile(Path.Combine(mManager.Path, $"{implementation.Name}{GlobalSettings.DEFAULT_EXTENSION}")); // Get object instance DeserializeContentFromFile <TConfig>(implementation); // Add to the list mConfigImplementations.Add(implementation); Logger.LogInfo($"{implementation.Name} implemented. ({mConfigImplementations.Count} implemented)"); return(this); }
/// <summary> /// Implements a configuration type /// </summary> /// <param name="configType">The type of config to implement</param> public ConfigImplementer Implement(Type configType) { var configAttribute = (ConfigAttribute)configType.GetCustomAttributes(typeof(ConfigAttribute), true).FirstOrDefault(); // Get ConfigAttribute from the class var implementation = new ConfigImplementation(configType); // If the class is not marked as config, throw exception if (configAttribute == null) { if (GlobalSettings.UseConsole) { Logger.LogError($"The type {configType.ToString()} is not a valid Config type."); return(this); } else { throw new BadConfigTypeException($"The type {configType.ToString()} is not a valid Config type."); } } // If the type is already implemented, throw exception if (mConfigImplementations.Any(x => x.Type == configType)) { if (GlobalSettings.UseConsole) { Logger.LogError($"The configuration type {configType.ToString()} is already implemented."); return(this); } else { throw new DuplicatedConfigException($"The configuration type {configType.ToString()} is already implemented."); } } // Configure the implementation implementation.Name = configAttribute.Name != null ? configAttribute.Name : configType.Name; // Set the name implementation.Type = configType; // Set type implementation.File = new ConfigFile(Path.Combine(mManager.Path, $"{implementation.Name}{GlobalSettings.DEFAULT_EXTENSION}")); // Get content from file DeserializeContentFromFile(implementation); // Add to the list mConfigImplementations.Add(implementation); return(this); }
/// <summary> /// Apply defaults for everything not specified on the current /// instance and expose the merged result as a configration abstraction. /// </summary> /// <returns></returns> public ILogStreamerConfiguration ToConfiguration() { var c = new ConfigImplementation(); c.SetContent(this); return c; }