Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 7f54637

Browse files
committed
refactored Constants implementation into Paths, with dynamic path calculation
1 parent 2b30cb5 commit 7f54637

File tree

5 files changed

+77
-41
lines changed

5 files changed

+77
-41
lines changed

Interop/ConfigControl.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using SourcepawnCondenser.SourcemodDefinition;
2-
using SPCode.Utils;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.Globalization;
64
using System.IO;
75
using System.Windows;
86
using System.Xml;
7+
using SourcepawnCondenser.SourcemodDefinition;
8+
using SPCode.Utils;
99

1010
namespace SPCode.Interop
1111
{
@@ -14,20 +14,20 @@ public static class ConfigLoader
1414
public static Config[] Load()
1515
{
1616
var configs = new List<Config>();
17-
if (File.Exists(Constants.ConfigFilePath))
17+
if (File.Exists(Paths.GetConfigFilePath()))
1818
{
1919
try
2020
{
2121
// Document gets loaded
2222
var document = new XmlDocument();
23-
document.Load(Constants.ConfigFilePath);
23+
document.Load(Paths.GetConfigFilePath());
2424

2525
// Document gets checked for proper Configurations tag
2626
if (document.ChildNodes.Count < 1)
2727
{
2828
throw new Exception("No main 'Configurations' node.");
2929
}
30-
30+
3131
// We check for main node and its main child 'Config' node
3232
var mainNode = document.ChildNodes[0];
3333
if (mainNode.ChildNodes.Count < 1)
@@ -60,9 +60,11 @@ public static Config[] Load()
6060
var SMDirectoriesSplitted = _SMDirectoryStr.Split(';');
6161
var SMDirs = new List<string>();
6262

63+
// If it's the default config the program comes with, add as SMDirectory the default one
64+
// (calculate it based on installation being standalone or portable)
6365
if (IsStandardConfig && string.IsNullOrEmpty(_SMDirectoryStr))
6466
{
65-
SMDirs.Add(Constants.SPCodeAppDataPath + @"sourcepawn\configs\sm_1_10_0_6478");
67+
SMDirs.Add(Paths.GetConfigsFolderPath());
6668
}
6769

6870
foreach (var dir in SMDirectoriesSplitted)
@@ -74,6 +76,12 @@ public static Config[] Load()
7476
}
7577
}
7678

79+
// Extra assurance for the program to always load a proper config
80+
if (IsStandardConfig && SMDirs.Count == 0)
81+
{
82+
SMDirs.Add(Paths.GetConfigsFolderPath());
83+
}
84+
7785
int _OptimizationLevel = 2, _VerboseLevel = 1;
7886
if (int.TryParse(ReadAttributeStringSafe(ref node, "OptimizationLevel", "2"), out var subValue))
7987
{
@@ -158,8 +166,8 @@ public static Config[] Load()
158166
else
159167
{
160168
MessageBox.Show(
161-
$"The Editor could not find the Configs.xml file in {Constants.ConfigFilePath}. Without it, the editor will not start. Reinstall your program.",
162-
"File not found.", MessageBoxButton.OK, MessageBoxImage.Warning);
169+
"The Editor could not find the Configs.xml file, neither locally nor in AppData. Without it, the editor will not start. Reinstall your program.",
170+
"Configs file not found.", MessageBoxButton.OK, MessageBoxImage.Warning);
163171
Environment.Exit(Environment.ExitCode);
164172
}
165173

Spcode.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
<DependentUpon>AboutWindow.xaml</DependentUpon>
198198
</Compile>
199199
<Compile Include="Utils\Constants.cs" />
200+
<Compile Include="Utils\Paths.cs" />
200201
<Compile Include="Utils\FTP.cs" />
201202
<Compile Include="Utils\ManagedAES.cs" />
202203
<Compile Include="Utils\SPSyntaxTidy\SPSyntaxTidy.cs" />

UI/Windows/ConfigWindow.xaml.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using MahApps.Metro;
2-
using MahApps.Metro.Controls.Dialogs;
3-
using Microsoft.Win32;
4-
using Microsoft.WindowsAPICodePack.Dialogs;
5-
using SPCode.Interop;
6-
using SPCode.Utils;
7-
using System;
1+
using System;
82
using System.Collections.Generic;
93
using System.Diagnostics;
104
using System.Globalization;
@@ -14,6 +8,12 @@
148
using System.Windows.Controls;
159
using System.Windows.Input;
1610
using System.Xml;
11+
using MahApps.Metro;
12+
using MahApps.Metro.Controls.Dialogs;
13+
using Microsoft.Win32;
14+
using Microsoft.WindowsAPICodePack.Dialogs;
15+
using SPCode.Interop;
16+
using SPCode.Utils;
1717

1818
namespace SPCode.UI.Windows
1919
{
@@ -348,11 +348,11 @@ private void MetroWindow_Closed(object sender, EventArgs e)
348348
Program.MainWindow.ChangeConfig(Program.SelectedConfig);
349349
var outString = new StringBuilder();
350350
var settings = new XmlWriterSettings
351-
{
352-
Indent = true,
353-
IndentChars = "\t",
354-
NewLineOnAttributes = false,
355-
OmitXmlDeclaration = true
351+
{
352+
Indent = true,
353+
IndentChars = "\t",
354+
NewLineOnAttributes = false,
355+
OmitXmlDeclaration = true
356356
};
357357
using (var writer = XmlWriter.Create(outString, settings))
358358
{
@@ -392,7 +392,7 @@ private void MetroWindow_Closed(object sender, EventArgs e)
392392
writer.Flush();
393393
}
394394

395-
File.WriteAllText(Constants.ConfigFilePath, outString.ToString());
395+
File.WriteAllText(Paths.GetConfigFilePath(), outString.ToString());
396396
}
397397

398398
private void Language_Translate()

Utils/Constants.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace SPCode.Utils
1+
namespace SPCode.Utils
82
{
93
public class Constants
104
{
11-
public Constants() { }
12-
13-
// I believe we need to implement functions here or refactor this solution to make dynamic directory checks for the different execution environments:
14-
// - debugging (VS)
15-
// - standalone installer (all configs go to appdata and need to be fetched there)
16-
// - portable version (keep everything in the same root folder)
17-
18-
public static string SPCodeAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\spcode\\";
19-
public static string ConfigFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\spcode\\sourcepawn\\configs\\Configs.xml";
20-
public static string OptionsFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\spcode\\options_0.dat";
21-
public static string TemplatesFilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\spcode\\sourcepawn\\templates\\Templates.xml";
225
public static string GitHubNewIssueLink = "https://github.com/Hexer10/SPCode/issues/new";
23-
public static string SMAPILink = "";
246
}
257
}

Utils/Paths.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace SPCode.Utils
5+
{
6+
public class Paths
7+
{
8+
public Paths() { }
9+
10+
private static readonly string SPCodeAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\spcode";
11+
12+
public static bool IsLocalInstallation()
13+
{
14+
var localConfigsPath = @".\sourcepawn\configs\Configs.xml";
15+
var localTemplatesPath = @".\sourcepawn\templates\Templates.xml";
16+
return File.Exists(localTemplatesPath) && File.Exists(localConfigsPath);
17+
}
18+
19+
public static string GetConfigsFolderPath()
20+
{
21+
var appDataPath = SPCodeAppDataPath + @"\sourcepawn\configs\sm_1_10_0_6478";
22+
var localPath = @".\sourcepawn\configs\sm_1_10_0_6478";
23+
return IsLocalInstallation() ? localPath : appDataPath;
24+
}
25+
26+
public static string GetConfigFilePath()
27+
{
28+
var appDataPath = SPCodeAppDataPath + @"\sourcepawn\configs\Configs.xml";
29+
var localPath = @".\sourcepawn\configs\Configs.xml";
30+
return IsLocalInstallation() ? localPath : appDataPath;
31+
}
32+
33+
public static string GetTemplatesFilePath()
34+
{
35+
var appDataPath = SPCodeAppDataPath + @"\spcode\sourcepawn\templates\Templates.xml";
36+
var localPath = @".\sourcepawn\templates\Templates.xml";
37+
return IsLocalInstallation() ? localPath : appDataPath;
38+
}
39+
40+
public static string GetOptionsFilePath()
41+
{
42+
return IsLocalInstallation() ? @".\options_0.dat" : SPCodeAppDataPath + @"\options_0.dat";
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)