Skip to content

Commit

Permalink
Fix the configuration manager code to properly upgrade from version t…
Browse files Browse the repository at this point in the history
…o version.
  • Loading branch information
Kinematics committed Mar 16, 2015
1 parent 7492f1a commit 5afe1b3
Showing 1 changed file with 63 additions and 11 deletions.
74 changes: 63 additions & 11 deletions NetTally/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NetTally
{
Expand All @@ -12,21 +9,76 @@ namespace NetTally
/// </summary>
public static class NetTallyConfig
{
// Keep the configuration file for the duration of the program run.
static Configuration config = null;

public static void Load(Tally tally, QuestCollectionWrapper questsWrapper)
{
Upgrade();

config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);

QuestsSection questConfig = config.Sections[QuestsSection.DefinedName] as QuestsSection;

questConfig?.Load(questsWrapper);
}

private static void Upgrade()
{
var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
if (conf.HasFile)
return;

var map = GetUpgradeMap();

var upgradeConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.PerUserRoaming);

QuestsSection questConfig = upgradeConfig.Sections[QuestsSection.DefinedName] as QuestsSection;

if (questConfig == null)
{
questConfig = new QuestsSection();
questConfig.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser;
config.Sections.Add(QuestsSection.DefinedName, questConfig);
}
return;

QuestCollectionWrapper questWrapper = new QuestCollectionWrapper(null, null);
questConfig.Load(questWrapper);

upgradeConfig.SaveAs(conf.FilePath, ConfigurationSaveMode.Full);
}


private static ExeConfigurationFileMap GetUpgradeMap()
{
var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming);
FileInfo defaultFile = new FileInfo(conf.FilePath);

var dir = defaultFile.Directory;
var parent = dir.Parent;

if (!parent.Exists)
return null;

var versionDirectories = parent.EnumerateDirectories("*.*.*.*", SearchOption.TopDirectoryOnly);
if (versionDirectories.Count() == 0)
return null;

// Get 'newest' directory that is not the one we expect to use
var latestDir = versionDirectories.OrderBy(d => d.Name).Last(d => d.Name != dir.Name);

var upgradeFile = Path.Combine(latestDir.FullName, defaultFile.Name);

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.RoamingUserConfigFilename = upgradeFile;


conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
map.MachineConfigFilename = conf.FilePath;
map.ExeConfigFilename = conf.FilePath;

conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
map.LocalUserConfigFilename = conf.FilePath;

questConfig.Load(questsWrapper);
return map;
}


public static void Save(Tally tally, QuestCollectionWrapper questsWrapper)
{
Expand Down

0 comments on commit 5afe1b3

Please sign in to comment.