--- trunk/RomCheater.UserSettingsSupport/SettingSubscriber.cs 2012/05/27 22:24:30 146 +++ trunk/RomCheater.UserSettingsSupport/SettingSubscriber.cs 2013/06/18 19:18:05 722 @@ -4,45 +4,235 @@ using System.Linq; using System.Text; using System.Reflection; using System.Configuration; +using System.Xml.Serialization; +using System.IO; +using System.Collections; +using System.Security.Policy; +using System.Security.Cryptography; namespace RomCheater.UserSettingsSupport { public sealed class SettingSubscriber { - private class SettingsEventArgs : EventArgs + #region t + + + #endregion + + + public SettingSubscriber() { } + //public void AddSubscriber(System.Configuration.ApplicationSettingsBase settings) { AddSubscriber(null, settings); } + public void AddSubscriber(object consumer, System.Configuration.ApplicationSettingsBase ownersettings) + { + this.OwnerSettings = ownersettings; + + if (this.OwnerSettings == null) + { + throw new ArgumentNullException("ownersettings", "ownersettings cannot be null"); + } + + this.Consumer = consumer; + LoadSettings(); + this.UpgradeSettingsIfNeeded(); + } + + + + private object _Consumer; + /// <summary> + /// The object that will consume the settings + /// </summary> + private object Consumer + { + get { return _Consumer; } + set { _Consumer = value; } + } + + + + /// <summary> + /// The Assembly that owns the settings + /// </summary> + private Assembly OwningAsembly + { + get { return this.OwnerSettings.GetType().Assembly; } + } + + /// <summary> + /// The Assembly that will consume the settings + /// </summary> + private Assembly ConsumerAssembly + { + get { return this.Consumer == null ? Assembly.GetExecutingAssembly() : this.Consumer.GetType().Assembly; } + } + + private System.Configuration.ApplicationSettingsBase _OwnerSettings; + public System.Configuration.ApplicationSettingsBase OwnerSettings + { + get { return _OwnerSettings; } + private set { _OwnerSettings = value; } + } + + /// <summary> + /// Indicates if the consumer is the owning assemlby of the settings + /// If the value is false, then the settings are being accessed remotely + /// </summary> + private bool IsOwningAssembly + { + get { return this.ConsumerAssembly == this.OwningAsembly; } + } + + private Configuration _config = null; + + #region LoadSettings() + private void LoadSettings() + { + if (!this.IsOwningAssembly) + { + //// we need to read the settings in + ////string ConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath; + ////string ConfigFile = ConfigurationManager.OpenExeConfiguration(SettingsAssmebly.Location).FilePath; + //string path = string.Empty; + //string profile_dir = Environment.GetEnvironmentVariable("LocalAppData"); + //string company_name = ((AssemblyCompanyAttribute)Attribute.GetCustomAttribute(this.OwningAsembly, typeof(AssemblyCompanyAttribute), false)).Company; + //string app_fullname = new FileInfo(this.OwningAsembly.Location).Name; + //string evidence_type = this.OwningAsembly.GetName().GetPublicKey().Length == 0 ? EVIDENCE_URL : EVIDENCE_STRONGNAME; + //string evidence_hash = "<HASH>"; + //string version = this.OwningAsembly.GetName().Version.ToString(); + //string config = "user.config"; + //StringBuilder builder = new StringBuilder(); + //builder.AppendFormat(@"{0}", profile_dir); + //if (company_name != string.Empty) + //{ + // builder.AppendFormat(@"\{0}", company_name.Replace(" ", "_")); + //} + //builder.AppendFormat(@"\{0}", app_fullname); + //builder.AppendFormat(@"_{0}", evidence_type); + //builder.AppendFormat(@"_{0}", evidence_hash); + //builder.AppendFormat(@"\{0}", version); + //builder.AppendFormat(@"\{0}", config); + //path = builder.ToString(); + //#region Hash testing + //Hash owner = new Hash(this.OwningAsembly); + //Hash consumer = new Hash(this.ConsumerAssembly); + //#endregion + + IsolatedStoragePath isp1 = new IsolatedStoragePath(this.OwningAsembly); + IsolatedStoragePath isp2 = new IsolatedStoragePath(this.ConsumerAssembly); + + } + else + { + string ConfigFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath; + } + } + #endregion + + + + public void UpgradeSettings() + { + if (this.OwnerSettings != null) + { + this.OwnerSettings.Upgrade(); + return; + } + throw new ArgumentNullException("Settings", "Settings cannot be null"); + } + + public void SaveSettings() { - public SettingsEventArgs() :this(null) { } - public SettingsEventArgs(System.Configuration.ApplicationSettingsBase settings) { this.Settings = settings; } - public System.Configuration.ApplicationSettingsBase Settings { get; private set; } + if (this.OwnerSettings != null) + { + this.OwnerSettings.Save(); + return; + } + throw new ArgumentNullException("Settings", "Settings cannot be null"); + } - private static EventHandler<SettingsEventArgs> OnSubscribeAddedEvent; - static SettingSubscriber() { OnSubscribeAddedEvent += new EventHandler<SettingsEventArgs>(OnSubscribeAdded); } - public static void AddSubscriber(System.Configuration.ApplicationSettingsBase settings) { AddSubscriber(null, settings); } - public static void AddSubscriber(object Subscriber, System.Configuration.ApplicationSettingsBase settings) { if (OnSubscribeAddedEvent != null) { OnSubscribeAddedEvent(Subscriber, new SettingsEventArgs(settings)); } } - private static void OnSubscribeAdded(object Subscriber, SettingsEventArgs e) + public void ReloadSettings() { - if (e.Settings != null) + if (this.OwnerSettings != null) { - foreach (SettingsProperty prop in e.Settings.Properties) + this.OwnerSettings.Reload(); + return; + } + throw new ArgumentNullException("Settings", "Settings cannot be null"); + } + public void ResetSettings() + { + if (this.OwnerSettings != null) + { + this.OwnerSettings.Reset(); + return; + } + throw new ArgumentNullException("Settings", "Settings cannot be null"); + } + public void UpgradeSettingsIfNeeded() + { + if (this.OwnerSettings != null) + { + //SettingCollections.Add(e.Settings); + foreach (SettingsProperty prop in this.OwnerSettings.Properties) { if (prop.Name == "UpgradeRequired") { - bool UpgradeRequired = Convert.ToBoolean(e.Settings[prop.Name]); + bool UpgradeRequired = Convert.ToBoolean(this.OwnerSettings[prop.Name]); try { if (UpgradeRequired) { - e.Settings.Upgrade(); - e.Settings.Reload(); - e.Settings[prop.Name] = false; - e.Settings.Save(); + UpgradeSettings(); + //Settings.Reload(); + SetValue(prop.Name, false); + SaveSettings(); } } - catch { e.Settings.Reset(); } + catch { ResetSettings(); } break; } } } + else + { + throw new ArgumentNullException("Settings", "Settings cannot be null"); + } + } + public void SetValue(string propertyName, object value) + { + if (this.OwnerSettings != null) + { + try + { + this.OwnerSettings[propertyName] = value; + } + catch (Exception ex) + { + throw new KeyNotFoundException(string.Format("Could not find property: '{0}'"), ex); + } + } + else + { + throw new ArgumentNullException("Settings", "Settings cannot be null"); + } + } + public object GetValue(string propertyName) + { + if (this.OwnerSettings != null) + { + try + { + return this.OwnerSettings[propertyName]; + } + catch (Exception ex) + { + throw new KeyNotFoundException(string.Format("Could not find property: '{0}'"), ex); + } + } + else + { + throw new ArgumentNullException("Settings", "Settings cannot be null"); + } } } } |