Skip to content

Commit c27834d

Browse files
authored
Merge pull request #50 from EverseDevelopment/develop
Hotfix Settings
2 parents 25134cb + b56c3d3 commit c27834d

File tree

5 files changed

+112
-34
lines changed

5 files changed

+112
-34
lines changed

e-verse.Navisworks.SelectByRevitId.Plugin/App.config

Lines changed: 0 additions & 18 deletions
This file was deleted.

e-verse.Navisworks.SelectByRevitId.Plugin/Utils/Analytics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static async Task Send(string action, string details)
2323
string user = Environment.UserName;
2424
ApplicationVersion versionNavis = Application.Version;
2525
string software = string.Concat("Navisworks ", versionNavis.ApiMajor);
26-
string version = SettingsConfig.GetValue("version");
26+
string version = SettingsConfig.currentVersion;
2727

2828
// Use string interpolation to incorporate variables into the JSON string
2929
string json = $@"{{
Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,127 @@
1-
using System.Configuration;
1+
using System.Collections.Generic;
2+
using System;
3+
using System.Configuration;
24
using System.IO;
35
using System.Reflection;
6+
using System.Xml;
47
using Configuration = System.Configuration.Configuration;
8+
using System.Windows.Input;
59

610
namespace EVerse.Navisworks.SelectByRevitId.Plugin
711
{
812
public static class SettingsConfig
913
{
10-
private static readonly string BinaryLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
11-
private static string appSettingsName = string.Concat(Assembly.GetExecutingAssembly().GetName().Name, ".dll.config");
12-
private static string appSettingsFile = System.IO.Path.Combine(BinaryLocation, appSettingsName);
14+
public static readonly string currentVersion = "1.0.20";
15+
public static readonly string currentApiKey = "PlaceHolderApiKey";
16+
17+
private static readonly string _configDir =
18+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Pris");
19+
20+
private static readonly string _configFile =
21+
Path.Combine(_configDir, "pris.config");
22+
23+
private static readonly object _locker = new object();
24+
25+
static SettingsConfig()
26+
{
27+
// Ensure folder exists
28+
if (!Directory.Exists(_configDir))
29+
Directory.CreateDirectory(_configDir);
30+
31+
// Ensure file exists with defaults
32+
if (!File.Exists(_configFile))
33+
CreateDefaultConfig();
34+
}
1335

1436
public static string GetValue(string key)
1537
{
16-
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = appSettingsFile }, ConfigurationUserLevel.None);
17-
return configuration.AppSettings.Settings[key].Value;
38+
if (!File.Exists(_configFile))
39+
CreateDefaultConfig();
40+
41+
lock (_locker)
42+
{
43+
try
44+
{
45+
Configuration config = OpenConfig();
46+
KeyValueConfigurationElement setting = config.AppSettings.Settings[key];
47+
return setting != null ? setting.Value : null;
48+
}
49+
catch (Exception ex)
50+
{
51+
throw new InvalidOperationException(
52+
$"Error retrieving value for key '{key}'.", ex);
53+
}
54+
}
1855
}
1956

2057
public static void SetValue(string key, string value)
2158
{
22-
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = appSettingsFile }, ConfigurationUserLevel.None);
23-
configuration.AppSettings.Settings[key].Value = value;
24-
configuration.Save(ConfigurationSaveMode.Modified, true);
25-
ConfigurationManager.RefreshSection("appSettings");
59+
if (!File.Exists(_configFile))
60+
CreateDefaultConfig();
61+
62+
lock (_locker)
63+
{
64+
try
65+
{
66+
Configuration config = OpenConfig();
67+
KeyValueConfigurationCollection settings = config.AppSettings.Settings;
68+
69+
if (settings[key] == null)
70+
settings.Add(key, value);
71+
else
72+
settings[key].Value = value;
73+
74+
config.Save(ConfigurationSaveMode.Modified);
75+
ConfigurationManager.RefreshSection("appSettings");
76+
}
77+
catch (Exception ex)
78+
{
79+
throw new InvalidOperationException(
80+
$"Error setting value for key '{key}'.", ex);
81+
}
82+
}
83+
}
84+
private static Configuration OpenConfig()
85+
{
86+
var map = new ExeConfigurationFileMap { ExeConfigFilename = _configFile };
87+
return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
88+
}
89+
90+
/// <summary>
91+
/// Creates leia.config with the requested default keys/values.
92+
/// </summary>
93+
private static void CreateDefaultConfig()
94+
{
95+
// Your requested defaults (removed the duplicated "runs" key to avoid errors).
96+
var defaults = new Dictionary<string, string>
97+
{
98+
{ "runs", "0" },
99+
{ "version", "0.0.0" },
100+
{ "user", "user01" },
101+
{ "release", "0" },
102+
{ "apikey", "0" }
103+
};
104+
105+
106+
var doc = new XmlDocument();
107+
var decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
108+
doc.AppendChild(decl);
109+
110+
XmlElement configuration = doc.CreateElement("configuration");
111+
doc.AppendChild(configuration);
112+
113+
XmlElement appSettings = doc.CreateElement("appSettings");
114+
configuration.AppendChild(appSettings);
115+
116+
foreach (var kvp in defaults)
117+
{
118+
XmlElement add = doc.CreateElement("add");
119+
add.SetAttribute("key", kvp.Key);
120+
add.SetAttribute("value", kvp.Value);
121+
appSettings.AppendChild(add);
122+
}
123+
124+
doc.Save(_configFile);
26125
}
27126
}
28-
}
127+
}

e-verse.Navisworks.SelectByRevitId.Plugin/Windows/LabelVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class LabelVersion
44
{
55
public static void Update(MainWindowViewModel mainWindowViewModel)
66
{
7-
string version = SettingsConfig.GetValue("version");
7+
string version = SettingsConfig.currentVersion;
88

99
mainWindowViewModel.Version = version;
1010
}

e-verse.Navisworks.SelectByRevitId.Plugin/e-verse.Navisworks.SelectByRevitId.Plugin.projitems

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,4 @@
4343
<Generator>MSBuild:Compile</Generator>
4444
</Page>
4545
</ItemGroup>
46-
<ItemGroup>
47-
<None Include="$(MSBuildThisFileDirectory)App.config" />
48-
</ItemGroup>
4946
</Project>

0 commit comments

Comments
 (0)