/[RomCheater]/trunk/RomCheater.UserSettingsSupport/SettingSubscriber.cs
ViewVC logotype

Annotation of /trunk/RomCheater.UserSettingsSupport/SettingSubscriber.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 722 - (hide annotations) (download)
Tue Jun 18 19:18:05 2013 UTC (7 years, 10 months ago) by william
File size: 8591 byte(s)

1 william 34 using System;
2     using System.Collections.Generic;
3     using System.Linq;
4     using System.Text;
5     using System.Reflection;
6     using System.Configuration;
7 william 722 using System.Xml.Serialization;
8     using System.IO;
9     using System.Collections;
10     using System.Security.Policy;
11     using System.Security.Cryptography;
12 william 34
13     namespace RomCheater.UserSettingsSupport
14     {
15     public sealed class SettingSubscriber
16     {
17 william 722 #region t
18    
19    
20     #endregion
21    
22    
23     public SettingSubscriber() { }
24     //public void AddSubscriber(System.Configuration.ApplicationSettingsBase settings) { AddSubscriber(null, settings); }
25     public void AddSubscriber(object consumer, System.Configuration.ApplicationSettingsBase ownersettings)
26 william 34 {
27 william 722 this.OwnerSettings = ownersettings;
28    
29     if (this.OwnerSettings == null)
30     {
31     throw new ArgumentNullException("ownersettings", "ownersettings cannot be null");
32     }
33    
34     this.Consumer = consumer;
35     LoadSettings();
36     this.UpgradeSettingsIfNeeded();
37 william 34 }
38 william 722
39    
40    
41     private object _Consumer;
42     /// <summary>
43     /// The object that will consume the settings
44     /// </summary>
45     private object Consumer
46 william 34 {
47 william 722 get { return _Consumer; }
48     set { _Consumer = value; }
49     }
50    
51    
52    
53     /// <summary>
54     /// The Assembly that owns the settings
55     /// </summary>
56     private Assembly OwningAsembly
57     {
58     get { return this.OwnerSettings.GetType().Assembly; }
59     }
60    
61     /// <summary>
62     /// The Assembly that will consume the settings
63     /// </summary>
64     private Assembly ConsumerAssembly
65     {
66     get { return this.Consumer == null ? Assembly.GetExecutingAssembly() : this.Consumer.GetType().Assembly; }
67     }
68    
69     private System.Configuration.ApplicationSettingsBase _OwnerSettings;
70     public System.Configuration.ApplicationSettingsBase OwnerSettings
71     {
72     get { return _OwnerSettings; }
73     private set { _OwnerSettings = value; }
74     }
75    
76     /// <summary>
77     /// Indicates if the consumer is the owning assemlby of the settings
78     /// If the value is false, then the settings are being accessed remotely
79     /// </summary>
80     private bool IsOwningAssembly
81     {
82     get { return this.ConsumerAssembly == this.OwningAsembly; }
83     }
84    
85     private Configuration _config = null;
86    
87     #region LoadSettings()
88     private void LoadSettings()
89     {
90     if (!this.IsOwningAssembly)
91 william 34 {
92 william 722 //// we need to read the settings in
93     ////string ConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
94     ////string ConfigFile = ConfigurationManager.OpenExeConfiguration(SettingsAssmebly.Location).FilePath;
95     //string path = string.Empty;
96     //string profile_dir = Environment.GetEnvironmentVariable("LocalAppData");
97     //string company_name = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(this.OwningAsembly, typeof(AssemblyCompanyAttribute), false)).Company;
98     //string app_fullname = new FileInfo(this.OwningAsembly.Location).Name;
99     //string evidence_type = this.OwningAsembly.GetName().GetPublicKey().Length == 0 ? EVIDENCE_URL : EVIDENCE_STRONGNAME;
100     //string evidence_hash = "<HASH>";
101     //string version = this.OwningAsembly.GetName().Version.ToString();
102     //string config = "user.config";
103     //StringBuilder builder = new StringBuilder();
104     //builder.AppendFormat(@"{0}", profile_dir);
105     //if (company_name != string.Empty)
106     //{
107     // builder.AppendFormat(@"\{0}", company_name.Replace(" ", "_"));
108     //}
109     //builder.AppendFormat(@"\{0}", app_fullname);
110     //builder.AppendFormat(@"_{0}", evidence_type);
111     //builder.AppendFormat(@"_{0}", evidence_hash);
112     //builder.AppendFormat(@"\{0}", version);
113     //builder.AppendFormat(@"\{0}", config);
114     //path = builder.ToString();
115     //#region Hash testing
116     //Hash owner = new Hash(this.OwningAsembly);
117     //Hash consumer = new Hash(this.ConsumerAssembly);
118     //#endregion
119    
120     IsolatedStoragePath isp1 = new IsolatedStoragePath(this.OwningAsembly);
121     IsolatedStoragePath isp2 = new IsolatedStoragePath(this.ConsumerAssembly);
122    
123     }
124     else
125     {
126     string ConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
127     }
128     }
129     #endregion
130    
131    
132    
133     public void UpgradeSettings()
134     {
135     if (this.OwnerSettings != null)
136     {
137     this.OwnerSettings.Upgrade();
138     return;
139     }
140     throw new ArgumentNullException("Settings", "Settings cannot be null");
141     }
142    
143     public void SaveSettings()
144     {
145     if (this.OwnerSettings != null)
146     {
147     this.OwnerSettings.Save();
148     return;
149     }
150     throw new ArgumentNullException("Settings", "Settings cannot be null");
151    
152     }
153     public void ReloadSettings()
154     {
155     if (this.OwnerSettings != null)
156     {
157     this.OwnerSettings.Reload();
158     return;
159     }
160     throw new ArgumentNullException("Settings", "Settings cannot be null");
161     }
162     public void ResetSettings()
163     {
164     if (this.OwnerSettings != null)
165     {
166     this.OwnerSettings.Reset();
167     return;
168     }
169     throw new ArgumentNullException("Settings", "Settings cannot be null");
170     }
171     public void UpgradeSettingsIfNeeded()
172     {
173     if (this.OwnerSettings != null)
174     {
175     //SettingCollections.Add(e.Settings);
176     foreach (SettingsProperty prop in this.OwnerSettings.Properties)
177 william 34 {
178     if (prop.Name == "UpgradeRequired")
179     {
180 william 722 bool UpgradeRequired = Convert.ToBoolean(this.OwnerSettings[prop.Name]);
181 william 34 try
182     {
183     if (UpgradeRequired)
184     {
185 william 722 UpgradeSettings();
186     //Settings.Reload();
187     SetValue(prop.Name, false);
188     SaveSettings();
189 william 34 }
190     }
191 william 722 catch { ResetSettings(); }
192 william 34 break;
193     }
194     }
195     }
196 william 722 else
197     {
198     throw new ArgumentNullException("Settings", "Settings cannot be null");
199     }
200 william 34 }
201 william 722 public void SetValue(string propertyName, object value)
202     {
203     if (this.OwnerSettings != null)
204     {
205     try
206     {
207     this.OwnerSettings[propertyName] = value;
208     }
209     catch (Exception ex)
210     {
211     throw new KeyNotFoundException(string.Format("Could not find property: '{0}'"), ex);
212     }
213     }
214     else
215     {
216     throw new ArgumentNullException("Settings", "Settings cannot be null");
217     }
218     }
219     public object GetValue(string propertyName)
220     {
221     if (this.OwnerSettings != null)
222     {
223     try
224     {
225     return this.OwnerSettings[propertyName];
226     }
227     catch (Exception ex)
228     {
229     throw new KeyNotFoundException(string.Format("Could not find property: '{0}'"), ex);
230     }
231     }
232     else
233     {
234     throw new ArgumentNullException("Settings", "Settings cannot be null");
235     }
236     }
237 william 34 }
238     }

  ViewVC Help
Powered by ViewVC 1.1.22