//#define ENABLE_LOAD_SETTINGS // when present will enable calls to LoadSettings()
using System;
using System.Collections.Generic;
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;
using System.Windows.Forms;
using System.Diagnostics;
namespace RomCheater.UserSettingsSupport
{
public sealed class SettingSubscriber
{
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;
///
/// The object that will consume the settings
///
private object Consumer
{
get { return _Consumer; }
set { _Consumer = value; }
}
///
/// The Assembly that owns the settings
///
private Assembly OwningAsembly
{
get { return this.OwnerSettings.GetType().Assembly; }
}
///
/// The Assembly that will consume the settings
///
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; }
}
///
/// Indicates if the consumer is the owning assemlby of the settings
/// If the value is false, then the settings are being accessed remotely
///
private bool IsOwningAssembly
{
get { return this.ConsumerAssembly == this.OwningAsembly; }
}
//private Configuration _config = null;
#region LoadSettings()
[Conditional("ENABLE_LOAD_SETTINGS")]
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 = "";
// //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()
{
if (this.OwnerSettings != null)
{
this.OwnerSettings.Save();
return;
}
throw new ArgumentNullException("Settings", "Settings cannot be null");
}
public void ReloadSettings()
{
if (this.OwnerSettings != null)
{
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(this.OwnerSettings[prop.Name]);
try
{
if (UpgradeRequired)
{
UpgradeSettings();
//Settings.Reload();
SetValue(prop.Name, false);
SaveSettings();
}
}
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");
}
}
}
}