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

Commit 341dfbc

Browse files
committed
paths class cleanup
1 parent 2339884 commit 341dfbc

14 files changed

+156
-147
lines changed

Interop/ConfigControl.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ namespace SPCode.Interop
1010
{
1111
public static class ConfigLoader
1212
{
13-
public static Config[] Load()
13+
public static List<Config> Load()
1414
{
1515
var configs = new List<Config>();
16-
if (File.Exists(Paths.GetConfigFilePath()))
16+
if (File.Exists(PathsHelper.ConfigFilePath))
1717
{
1818
try
1919
{
2020
// Document gets loaded
2121
var document = new XmlDocument();
22-
document.Load(Paths.GetConfigFilePath());
22+
document.Load(PathsHelper.ConfigFilePath);
2323

2424
// Document gets checked for proper Configurations tag
2525
if (document.ChildNodes.Count < 1)
@@ -63,7 +63,7 @@ public static Config[] Load()
6363
// (calculate it based on installation being standalone or portable)
6464
if (IsStandardConfig && string.IsNullOrEmpty(_SMDirectoryStr))
6565
{
66-
SMDirs.Add(Paths.GetConfigsDirectory());
66+
SMDirs.Add(PathsHelper.ConfigsDirectory);
6767
}
6868

6969
foreach (var dir in SMDirectoriesSplitted)
@@ -78,7 +78,7 @@ public static Config[] Load()
7878
// Extra assurance for the program to always load a proper config
7979
if (IsStandardConfig && SMDirs.Count == 0)
8080
{
81-
SMDirs.Add(Paths.GetConfigsDirectory());
81+
SMDirs.Add(PathsHelper.ConfigsDirectory);
8282
}
8383

8484
int _OptimizationLevel = 2, _VerboseLevel = 1;
@@ -169,7 +169,7 @@ public static Config[] Load()
169169
Environment.Exit(Environment.ExitCode);
170170
}
171171

172-
return configs.ToArray();
172+
return configs;
173173
}
174174

175175
private static string ReadAttributeStringSafe(ref XmlNode node, string attributeName, string defaultValue = "")

Interop/OptionsControl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public static void Save()
229229
try
230230
{
231231
var formatter = new BinaryFormatter();
232-
using var fileStream = new FileStream(Paths.GetOptionsFilePath(), FileMode.Create, FileAccess.ReadWrite,
232+
using var fileStream = new FileStream(PathsHelper.OptionsFilePath, FileMode.Create, FileAccess.ReadWrite,
233233
FileShare.None);
234234
formatter.Serialize(fileStream, Program.OptionsObject);
235235
}
@@ -242,11 +242,11 @@ public static OptionsControl Load(out bool ProgramIsNew)
242242
{
243243
try
244244
{
245-
if (File.Exists(Paths.GetOptionsFilePath()))
245+
if (File.Exists(PathsHelper.OptionsFilePath))
246246
{
247247
OptionsControl optionsObject;
248248
var formatter = new BinaryFormatter();
249-
using (var fileStream = new FileStream(Paths.GetOptionsFilePath(), FileMode.Open, FileAccess.Read,
249+
using (var fileStream = new FileStream(PathsHelper.OptionsFilePath, FileMode.Open, FileAccess.Read,
250250
FileShare.ReadWrite))
251251
{
252252
optionsObject = (OptionsControl)formatter.Deserialize(fileStream);

Interop/TranslationProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ public class TranslationProvider
1616
public List<string> AvailableLanguageIDs = new();
1717
public List<string> AvailableLanguages = new();
1818

19-
private readonly string _tempDir = Paths.GetTempDirectory();
20-
private readonly string _translationsDir = Paths.GetTranslationsDirectory();
19+
private readonly string _tempDir = PathsHelper.TempDirectory;
20+
private readonly string _translationsDir = PathsHelper.TranslationsDirectory;
2121
private static readonly Dictionary<string, string> _langDictionary = new(StringComparer.OrdinalIgnoreCase);
2222
private static readonly Dictionary<string, string> _defaultDictionary = new(StringComparer.OrdinalIgnoreCase);
2323
private Release _latestVersion;

Program.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static class Program
2828
public static OptionsControl OptionsObject;
2929
public static TranslationProvider Translations;
3030
public static List<HotkeyInfo> HotkeysList;
31-
public static Config[] Configs;
31+
public static List<Config> Configs;
3232
public static int SelectedConfig;
3333
public static string SelectedTemplatePath;
3434
public static Stack<string> RecentFilesStack = new();
@@ -43,8 +43,6 @@ public static class Program
4343
? new string(' ', OptionsObject.Editor_IndentationSize)
4444
: "\t";
4545

46-
public static bool _IsLocalInstallation;
47-
4846
[STAThread]
4947
public static void Main(string[] args)
5048
{
@@ -65,7 +63,6 @@ public static void Main(string[] args)
6563
ProfileOptimization.SetProfileRoot(Environment.CurrentDirectory);
6664
ProfileOptimization.StartProfile("Startup.Profile");
6765
#endif
68-
_IsLocalInstallation = Paths.IsLocalInstallation();
6966
UpdateStatus = new UpdateInfo();
7067
OptionsObject = OptionsControl.Load(out var ProgramIsNew);
7168

@@ -117,7 +114,7 @@ public static void Main(string[] args)
117114
}
118115

119116
Configs = ConfigLoader.Load();
120-
for (var i = 0; i < Configs.Length; ++i)
117+
for (var i = 0; i < Configs.Count; ++i)
121118
{
122119
if (Configs[i].Name == OptionsObject.Program_SelectedConfig)
123120
{
@@ -158,11 +155,12 @@ public static void Main(string[] args)
158155
}
159156
catch (Exception e)
160157
{
161-
File.WriteAllText($@"{Paths.GetCrashLogDirectory()}\CRASH_{Environment.TickCount}.txt",
158+
var crashDir = PathsHelper.CrashLogDirectory;
159+
File.WriteAllText($@"{crashDir}\CRASH_{Environment.TickCount}.txt",
162160
BuildExceptionString(e, "SPCODE LOADING"));
163161
MessageBox.Show(
164162
"An error occured." + Environment.NewLine +
165-
$"A crash report was written in {Paths.GetCrashLogDirectory()}",
163+
$"A crash report was written in {crashDir}",
166164
"Error",
167165
MessageBoxButton.OK,
168166
MessageBoxImage.Error);
@@ -185,11 +183,12 @@ public static void Main(string[] args)
185183
}
186184
catch (Exception e)
187185
{
188-
File.WriteAllText($@"{Paths.GetCrashLogDirectory()}\CRASH_{Environment.TickCount}.txt",
186+
var crashDir = PathsHelper.CrashLogDirectory;
187+
File.WriteAllText($@"{crashDir}\CRASH_{Environment.TickCount}.txt",
189188
BuildExceptionString(e, "SPCODE MAIN"));
190189
MessageBox.Show(
191190
"An error occured." + Environment.NewLine +
192-
$"A crash report was written in {Paths.GetCrashLogDirectory()}",
191+
$"A crash report was written in {crashDir}",
193192
"Error",
194193
MessageBoxButton.OK,
195194
MessageBoxImage.Error);
@@ -277,7 +276,7 @@ private static string BuildExceptionString(Exception e, string SectionName)
277276
var outString = new StringBuilder();
278277
outString.AppendLine("Section: " + SectionName);
279278
outString.AppendLine(".NET Version: " + Environment.Version);
280-
outString.AppendLine("Is local installation?: " + _IsLocalInstallation);
279+
outString.AppendLine("Is local installation?: " + PathsHelper.LocalInstallation);
281280
outString.AppendLine("OS: " + Environment.OSVersion.VersionString);
282281
outString.AppendLine("64 bit OS: " + (Environment.Is64BitOperatingSystem ? "TRUE" : "FALSE"));
283282
outString.AppendLine("64 bit mode: " + (Environment.Is64BitProcess ? "TRUE" : "FALSE"));

Spcode.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
<Compile Include="Utils\HotkeyUtils.cs" />
210210
<Compile Include="Utils\NamesHelper.cs" />
211211
<Compile Include="Utils\ObjectBrowserUtils.cs" />
212-
<Compile Include="Utils\Paths.cs" />
212+
<Compile Include="Utils\PathsHelper.cs" />
213213
<Compile Include="Utils\Models\FTP.cs" />
214214
<Compile Include="Utils\ManagedAES.cs" />
215215
<Compile Include="UI\Components\EditorElement\Highlighting\SPBracketSearcher.cs" />

UI/MainWindow/MainWindowConfigHandler.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class MainWindow
1818
public void FillConfigMenu()
1919
{
2020
ConfigMenu.Items.Clear();
21-
for (var i = 0; i < Program.Configs.Length; ++i)
21+
for (var i = 0; i < Program.Configs.Count; ++i)
2222
{
2323
var item = new MenuItem
2424
{
@@ -56,7 +56,7 @@ private void Item_Click(object sender, RoutedEventArgs e)
5656
/// <param name="index">The config index to change to</param>
5757
public void ChangeConfig(int index)
5858
{
59-
if (index < 0 || index >= Program.Configs.Length)
59+
if (index < 0 || index >= Program.Configs.Count)
6060
{
6161
return;
6262
}
@@ -106,7 +106,7 @@ public void ChangeConfig(int index)
106106
/// <param name="name">Name of the config to change to.</param>
107107
private void ChangeConfig(string name)
108108
{
109-
for (var i = 0; i < Program.Configs.Length; ++i)
109+
for (var i = 0; i < Program.Configs.Count; ++i)
110110
{
111111
if (Program.Configs[i].Name == name)
112112
{

UI/MainWindow/MainWindowSPCompiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ private string ExecuteCommandLine(string code, string directory, string copyDir,
586586
return null;
587587
}
588588

589-
var batchFile = new FileInfo(@$"{Paths.GetTempDirectory()}\{Environment.TickCount}_{(uint)code.GetHashCode() ^ (uint)directory.GetHashCode()}_temp.bat").FullName;
589+
var batchFile = new FileInfo(@$"{PathsHelper.TempDirectory}\{Environment.TickCount}_{(uint)code.GetHashCode() ^ (uint)directory.GetHashCode()}_temp.bat").FullName;
590590
File.WriteAllText(batchFile, code);
591591
string result;
592592
using (var process = new Process())

UI/Windows/ConfigWindow.xaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private void BuildCommandsBoxes_OnFocus(object sender, RoutedEventArgs e)
218218

219219
private void LoadConfigToUI(int index)
220220
{
221-
if (index < 0 || index >= Program.Configs.Length)
221+
if (index < 0 || index >= Program.Configs.Count)
222222
{
223223
return;
224224
}
@@ -261,7 +261,7 @@ private void NewButton_Clicked(object sender, RoutedEventArgs e)
261261
SMDirectories = new List<string>()
262262
};
263263
var configList = new List<Config>(Program.Configs) { c };
264-
Program.Configs = configList.ToArray();
264+
Program.Configs = configList;
265265
ConfigListBox.Items.Add(new ListBoxItem { Content = Translate("NewConfig") });
266266
}
267267

@@ -279,7 +279,7 @@ private void DeleteButton_Clicked(object sender, RoutedEventArgs e)
279279

280280
var configList = new List<Config>(Program.Configs);
281281
configList.RemoveAt(index);
282-
Program.Configs = configList.ToArray();
282+
Program.Configs = configList;
283283
ConfigListBox.Items.RemoveAt(index);
284284
if (index == Program.SelectedConfig)
285285
{
@@ -633,7 +633,7 @@ private void MetroWindow_Closing(object sender, CancelEventArgs e)
633633
writer.Flush();
634634
}
635635

636-
File.WriteAllText(Paths.GetConfigFilePath(), outString.ToString());
636+
File.WriteAllText(PathsHelper.ConfigFilePath, outString.ToString());
637637
LoggingControl.LogAction($"Configs saved.", 2);
638638
}
639639

UI/Windows/NewFileWindow.xaml.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ namespace SPCode.UI.Windows
2121
public partial class NewFileWindow
2222
{
2323
#region Variables
24-
private readonly string PathStr = Program.Configs[Program.SelectedConfig].SMDirectories[0];
24+
private readonly string _pathStr = Program.Configs[Program.SelectedConfig].SMDirectories[0];
25+
private readonly string _templatesFile = PathsHelper.TemplatesFilePath;
2526
private bool TemplateEditMode = false;
2627
private bool TemplateNewMode = false;
2728
private ICommand textBoxButtonFileCmd;
@@ -132,7 +133,7 @@ private void TemplateListBox_SelectionChanged(object sender, SelectionChangedEve
132133
if ((TemplateListBox.SelectedItem as ListBoxItem)?.Tag is TemplateInfo templateInfo)
133134
{
134135
PreviewBox.editor.Text = File.ReadAllText(templateInfo.Path);
135-
PathBox.Text = Path.Combine(PathStr, templateInfo.NewName);
136+
PathBox.Text = Path.Combine(_pathStr, templateInfo.NewName);
136137
}
137138
}
138139
catch (Exception ex)
@@ -297,10 +298,10 @@ private void CacheTemplates()
297298
{
298299
try
299300
{
300-
if (File.Exists(Paths.GetTemplatesFilePath()))
301+
if (File.Exists(_templatesFile))
301302
{
302303
LBIList = new List<ListBoxItem>();
303-
using Stream stream = File.OpenRead(Paths.GetTemplatesFilePath());
304+
using Stream stream = File.OpenRead(_templatesFile);
304305
var doc = new XmlDocument();
305306
doc.Load(stream);
306307
if (doc.ChildNodes.Count <= 0)
@@ -324,7 +325,7 @@ private void CacheTemplates()
324325
var NewNameStr = attributes?["NewName"].Value;
325326

326327
Debug.Assert(FileNameStr != null, nameof(FileNameStr) + " != null");
327-
var FilePathStr = Path.Combine(Paths.GetTemplatesDirectory(), FileNameStr);
328+
var FilePathStr = Path.Combine(PathsHelper.TemplatesDirectory, FileNameStr);
328329
if (File.Exists(FilePathStr))
329330
{
330331
Debug.Assert(NameStr != null, nameof(NameStr) + " != null");
@@ -395,14 +396,13 @@ private void SaveTemplate(string name)
395396
try
396397
{
397398
var temp = TemplateListBox.SelectedItem as ListBoxItem;
398-
var tempFilePath = Paths.GetTemplatesFilePath();
399399

400400
// Save the file's content
401401
var path = (temp.Tag as TemplateInfo).Path;
402402
File.WriteAllText(path, PreviewBox.editor.Text);
403403

404404
// Save the new entry in Templates.xml
405-
using Stream stream = File.OpenRead(tempFilePath);
405+
using Stream stream = File.OpenRead(_templatesFile);
406406
var doc = new XmlDocument();
407407
doc.Load(stream);
408408
stream.Dispose();
@@ -434,7 +434,7 @@ private void SaveTemplate(string name)
434434
FileName = pathboxFileInfo.Name.Replace(" ", "")
435435
};
436436
}
437-
doc.Save(tempFilePath);
437+
doc.Save(_templatesFile);
438438
break;
439439
}
440440
}
@@ -451,7 +451,7 @@ private void DeleteTemplate(ListBoxItem temp)
451451
{
452452
TemplateListBox.Items.RemoveAt(TemplateListBox.SelectedIndex);
453453
File.Delete(new FileInfo((temp.Tag as TemplateInfo).Path).FullName);
454-
using Stream stream = File.OpenRead(Paths.GetTemplatesFilePath());
454+
using Stream stream = File.OpenRead(_templatesFile);
455455
var doc = new XmlDocument();
456456
doc.Load(stream);
457457
stream.Dispose();
@@ -460,7 +460,7 @@ private void DeleteTemplate(ListBoxItem temp)
460460
if (node.Attributes["Name"].Value == temp.Content.ToString())
461461
{
462462
doc.ChildNodes[0].RemoveChild(node);
463-
doc.Save(Paths.GetTemplatesFilePath());
463+
doc.Save(_templatesFile);
464464
return;
465465
}
466466
}
@@ -475,11 +475,10 @@ private void CreateTemplate(string name)
475475
{
476476
try
477477
{
478-
var tempFilePath = Paths.GetTemplatesFilePath();
479478
var pathBoxFileInfo = new FileInfo(PathBox.Text);
480479

481480
// Save in XML
482-
using Stream stream = File.OpenRead(tempFilePath);
481+
using Stream stream = File.OpenRead(_templatesFile);
483482
var doc = new XmlDocument();
484483
doc.Load(stream);
485484
stream.Dispose();
@@ -490,10 +489,10 @@ private void CreateTemplate(string name)
490489
newNode.SetAttribute("File", pathBoxFileInfo.Name.Replace(" ", ""));
491490

492491
doc.ChildNodes[0].AppendChild(newNode);
493-
doc.Save(tempFilePath);
492+
doc.Save(_templatesFile);
494493

495494
// Create file
496-
var newFilePath = Path.GetDirectoryName(tempFilePath) + Path.DirectorySeparatorChar + pathBoxFileInfo.Name.Replace(" ", "");
495+
var newFilePath = Path.GetDirectoryName(_templatesFile) + Path.DirectorySeparatorChar + pathBoxFileInfo.Name.Replace(" ", "");
497496
File.WriteAllText(newFilePath, PreviewBox.editor.Text);
498497

499498
// Save ListBoxItem

UI/Windows/OptionsWindow/OptionsWindowOptionsManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ await this.ShowMessageAsync(Translate("RestartEditor"),
3232

3333
private async void BackupButton_Clicked(object sender, RoutedEventArgs e)
3434
{
35-
var optionsFile = new FileInfo(Paths.GetOptionsFilePath());
35+
var optionsFile = new FileInfo(PathsHelper.OptionsFilePath);
3636
var sfd = new SaveFileDialog
3737
{
3838
FileName = "options_0",
@@ -70,7 +70,7 @@ private async void LoadButton_Clicked(object sender, RoutedEventArgs e)
7070
return;
7171
}
7272

73-
var optionsFile = new FileInfo(Paths.GetOptionsFilePath());
73+
var optionsFile = new FileInfo(PathsHelper.OptionsFilePath);
7474
var ofd = new OpenFileDialog
7575
{
7676
Filter = Constants.DatFilesFilter

Utils/DecompileUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static FileInfo GetFile()
3030

3131
public static string GetDecompiledPlugin(FileInfo file)
3232
{
33-
var decompilerPath = Paths.GetLysisDirectory();
33+
var decompilerPath = PathsHelper.LysisDirectory;
3434
var destFile = file.FullName + ".sp";
3535
var standardOutput = new StringBuilder();
3636
using var process = new Process();

Utils/DirUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static class DirUtils
66
{
77
public static void ClearTempFolder()
88
{
9-
var tempDir = Paths.GetTempDirectory();
9+
var tempDir = PathsHelper.TempDirectory;
1010
foreach (var file in Directory.GetFiles(tempDir))
1111
{
1212
File.Delete(file);

0 commit comments

Comments
 (0)