private void LoadPreferences()
{
BinaryFormatter bf = new BinaryFormatter();
LauncherEncryption myEncryption = new LauncherEncryption();
IsolatedStorageFile inFile = IsolatedStorageFile.GetUserStoreForAssembly();
//do we have the desired locale?
Locales myLocales = new Locales(CultureInfo.CurrentUICulture.Name);
if (App.Current.Resources["UserPreferences"] == null)
{
bool blnSetDefaults = false;
//here we will attempt to load the settings object from the user's profil
//or make them up if nothing has been chosen yet (first run)
using (IsolatedStorageFileStream inStream = new IsolatedStorageFileStream("settings.cfg", FileMode.OpenOrCreate, System.IO.FileAccess.Read, inFile))
using (BinaryReader read = new BinaryReader(inStream))
{
if (read.BaseStream.Length == 0)
{
blnSetDefaults = true;
}
else
{
try
{
//read in the saved bytes
byte[] arInBytes = read.ReadBytes((int)read.BaseStream.Length);
//decrypt
byte[] arDecrypted = myEncryption.Decrypt(arInBytes);
//deserialize
using (MemoryStream msIn = new MemoryStream(arDecrypted))
{
theSettings = bf.Deserialize(msIn) as UserSettings;
}
if (theSettings == null)
{
//make a default one
blnSetDefaults = true;
}
}
catch (Exception ex)
{
string strYar = ex.Message;
}
}
}
if (blnSetDefaults)
{
//make a new one
theSettings = new UserSettings
{
Locale = myLocales.CurrentLocale.Locale,
Skin = "SWG_DataPad",
Pallette = "Alpha Blue",
GhostFont = "Aurek-Besh"
};
//and save that into the file
using (MemoryStream msOut = new MemoryStream())
using (IsolatedStorageFileStream outStream = new IsolatedStorageFileStream("settings.cfg", FileMode.OpenOrCreate, System.IO.FileAccess.Write, inFile))
using (BinaryWriter bwOut = new BinaryWriter(outStream))
{
//serialize it
bf.Serialize(msOut, theSettings);
//encrypt it and write it out
bwOut.Write(myEncryption.Encrypt(msOut.ToArray()));
}
}
//and put it in the resources
App.Current.Resources.Add("UserPreferences", theSettings);
}
else
{
//collect them from the Application Resources Collection
theSettings = App.Current.Resources["UserPreferences"] as UserSettings;
}
}