1: /*
2: Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
3:
4: Permission is hereby granted, free of charge, to any person obtaining a copy
5: of this software and associated documentation files (the "Software"), to deal
6: in the Software without restriction, including without limitation the rights
7: to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8: copies of the Software, and to permit persons to whom the Software is
9: furnished to do so, subject to the following conditions:
10:
11: The above copyright notice and this permission notice shall be included in
12: all copies or substantial portions of the Software.
13:
14: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15: IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16: FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17: AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18: LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19: OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20: THE SOFTWARE.*/
21:
22: #region Usings
23: using System;
24: using System.Collections.Generic;
25: using System.Reflection;
26: using Gestalt.Attributes;
27: using Gestalt.Interfaces;
28: using Utilities.DataTypes.Patterns.BaseClasses;
29: using System.Text;
30: #endregion
31:
32: namespace Gestalt
33: {
34: /// <summary>
35: /// Config manager
36: /// </summary>
37: public class Manager : Singleton<Manager>
38: {
39: #region Constructor
40:
41: /// <summary>
42: /// Constructor
43: /// </summary>
44: protected Manager()
45: : base()
46: {
47: ConfigFiles = new Dictionary<string, IConfig>();
48: }
49:
50: #endregion
51:
52: #region Public Functions
53:
54: /// <summary>
55: /// Registers a config file
56: /// </summary>
57: /// <param name="Name">Name to reference by</param>
58: /// <param name="ConfigObject">Config object to register</param>
59: public void RegisterConfigFile(string Name, IConfig ConfigObject)
60: {
61: ConfigObject.Load();
62: ConfigFiles.Add(Name, ConfigObject);
63: }
64:
65: /// <summary>
66: /// Registers all config files in an assembly
67: /// </summary>
68: /// <param name="AssemblyContainingConfig">Assembly to search</param>
69: public void RegisterConfigFile(Assembly AssemblyContainingConfig)
70: {
71: List<Type> Types = Utilities.Reflection.Reflection.GetTypes(AssemblyContainingConfig, "IConfig");
72: foreach (Type Temp in Types)
73: {
74: if (!Temp.ContainsGenericParameters)
75: {
76: string Name = "";
77: object[] Attributes = Temp.GetCustomAttributes(typeof(ConfigAttribute), true);
78: if (Attributes.Length > 0)
79: {
80: Name = ((ConfigAttribute)Attributes[0]).Name;
81: }
82: IConfig TempConfig = (IConfig)Temp.Assembly.CreateInstance(Temp.FullName);
83: RegisterConfigFile(Name, TempConfig);
84: }
85: }
86: }
87:
88: /// <summary>
89: /// Registers all config files in an assembly that is not currently loaded
90: /// </summary>
91: /// <param name="AssemblyLocation">Location of the assembly</param>
92: public void RegisterConfigFile(string AssemblyLocation)
93: {
94: RegisterConfigFile(Assembly.LoadFile(AssemblyLocation));
95: }
96:
97: /// <summary>
98: /// Gets a specified config file
99: /// </summary>
100: /// <typeparam name="T">Type of the config object</typeparam>
101: /// <param name="Name">Name of the config object</param>
102: /// <returns>The config object specified</returns>
103: public T GetConfigFile<T>(string Name)
104: {
105: if (!ConfigFiles.ContainsKey(Name))
106: throw new Exception("The config object " + Name + " was not found.");
107: if (!(ConfigFiles[Name] is T))
108: throw new Exception("The config object " + Name + " is not the specified type.");
109: return (T)ConfigFiles[Name];
110: }
111:
112: public override string ToString()
113: {
114: StringBuilder Builder = new StringBuilder();
115: Builder.Append("<ul>").Append("<li>").Append(ConfigFiles.Count).Append("</li>");
116: foreach (string Name in ConfigFiles.Keys)
117: {
118: Builder.Append("<li>").Append(Name).Append(":").Append(ConfigFiles[Name].GetType().FullName).Append("</li>");
119: }
120: Builder.Append("</ul>");
121: return Builder.ToString();
122: }
123:
124: #endregion
125:
126: #region Private properties
127:
128: private static Dictionary<string, IConfig> ConfigFiles { get; set; }
129:
130: #endregion
131: }
132: }