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

Commit 4bcab95

Browse files
committed
added load and backup options logic
1 parent bee036f commit 4bcab95

File tree

7 files changed

+111
-17
lines changed

7 files changed

+111
-17
lines changed

Interop/OptionsControl.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,11 @@ public void NormalizeSHColors()
221221

222222
public static void Save()
223223
{
224+
if (Program.PreventOptionsSaving)
225+
{
226+
return;
227+
}
228+
224229
try
225230
{
226231
var formatter = new BinaryFormatter();

Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static class Program
3636
public static bool RCCKMade;
3737
public static DiscordRpcClient DiscordClient = new(Constants.DiscordRPCAppID);
3838
public static Timestamps DiscordTime = Timestamps.Now;
39+
public static bool PreventOptionsSaving = false;
3940

4041
public static string Indentation => OptionsObject.Editor_ReplaceTabsToWhitespace
4142
? new string(' ', OptionsObject.Editor_IndentationSize)

Spcode.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
<DependentUpon>ConfigWindow.xaml</DependentUpon>
173173
</Compile>
174174
<Compile Include="UI\Windows\ConfigWindow\ConfigWindowConfigsManager.cs" />
175+
<Compile Include="UI\Windows\OptionsWindow\OptionsWindowOptionsManager.cs" />
175176
<Compile Include="UI\Windows\RenameWindow.xaml.cs">
176177
<DependentUpon>RenameWindow.xaml</DependentUpon>
177178
</Compile>

UI/Windows/OptionsWindow.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
<controls:MetroWindow.RightWindowCommands>
4545
<controls:WindowCommands>
46+
<Button Name="BackupButton" Content="Backup options" Click="BackupButton_Clicked" />
47+
<Button Name="LoadButton" Content="Load options" Click="LoadButton_Clicked" />
4648
<Button Name="ResetButton" Content="Reset options to default" Click="RestoreButton_Clicked" />
4749
</controls:WindowCommands>
4850
</controls:MetroWindow.RightWindowCommands>

UI/Windows/OptionsWindow.xaml.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,6 @@ private void MetroWindow_KeyDown(object sender, KeyEventArgs e)
7474
}
7575
}
7676

77-
private async void RestoreButton_Clicked(object sender, RoutedEventArgs e)
78-
{
79-
var result = await this.ShowMessageAsync(Translate("ResetOptions"),
80-
Translate("ResetOptQues"), MessageDialogStyle.AffirmativeAndNegative, Program.MainWindow.MetroDialogOptions);
81-
if (result == MessageDialogResult.Affirmative)
82-
{
83-
Program.OptionsObject = new OptionsControl();
84-
Program.OptionsObject.ReCreateCryptoKey();
85-
Program.MainWindow.OptionMenuEntry.IsEnabled = false;
86-
await this.ShowMessageAsync(Translate("RestartEditor"),
87-
Translate("YRestartEditor"), MessageDialogStyle.Affirmative,
88-
Program.MainWindow.MetroDialogOptions);
89-
Close();
90-
}
91-
}
92-
9377
private void HardwareAcc_Changed(object sender, RoutedEventArgs e)
9478
{
9579
if (!AllowChanging)
@@ -755,7 +739,6 @@ private void Language_Translate()
755739
Title = Translate("Options");
756740
HardwareSalts.Content = Translate("HardwareEncryption");
757741
ProgramHeader.Header = $" {Translate("Program")}";
758-
DefaultButton.Content = Translate("DefaultValues");
759742
HardwareAcc.Content = Translate("HardwareAcc");
760743
UIAnimation.Content = Translate("UIAnim");
761744
OpenIncludes.Content = Translate("OpenInc");
@@ -784,6 +767,9 @@ private void Language_Translate()
784767
DiscordPresence.Content = Translate("EnableRPC");
785768
DiscordPresenceTime.Content = Translate("EnableRPCTime");
786769
DiscordPresenceFile.Content = Translate("EnableRPCFile");
770+
DefaultButton.Content = Translate("DefaultValues");
771+
BackupButton.Content = Translate("BackupOptions");
772+
LoadButton.Content = Translate("LoadOptions");
787773

788774
foreach (var item in HotkeysGrid.Children)
789775
{
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using MahApps.Metro.Controls.Dialogs;
9+
using Microsoft.Win32;
10+
using SPCode.Utils;
11+
using static SPCode.Interop.TranslationProvider;
12+
13+
namespace SPCode.UI.Windows
14+
{
15+
public partial class OptionsWindow
16+
{
17+
private async void RestoreButton_Clicked(object sender, RoutedEventArgs e)
18+
{
19+
var result = await this.ShowMessageAsync(Translate("Warning"),
20+
Translate("ResetOptQues"), MessageDialogStyle.AffirmativeAndNegative, Program.MainWindow.MetroDialogOptions);
21+
if (result == MessageDialogResult.Affirmative)
22+
{
23+
Program.OptionsObject = new OptionsControl();
24+
Program.OptionsObject.ReCreateCryptoKey();
25+
Program.MainWindow.OptionMenuEntry.IsEnabled = false;
26+
await this.ShowMessageAsync(Translate("RestartEditor"),
27+
Translate("YRestartEditor"), MessageDialogStyle.Affirmative,
28+
Program.MainWindow.MetroDialogOptions);
29+
Close();
30+
}
31+
}
32+
33+
private async void BackupButton_Clicked(object sender, RoutedEventArgs e)
34+
{
35+
var optionsFile = new FileInfo(Paths.GetOptionsFilePath());
36+
var sfd = new SaveFileDialog
37+
{
38+
FileName = "options_0",
39+
DefaultExt = ".dat",
40+
Filter = Constants.DatFilesFilter
41+
};
42+
43+
if (sfd.ShowDialog() is bool result && result)
44+
{
45+
try
46+
{
47+
if (File.Exists(sfd.FileName))
48+
{
49+
File.Delete(sfd.FileName);
50+
}
51+
OptionsControl.Save();
52+
File.Copy(optionsFile.FullName, sfd.FileName);
53+
await this.ShowMessageAsync(Translate("Success"), Translate("BackupOptionsSuccessMessage"), settings: Program.MainWindow.MetroDialogOptions);
54+
}
55+
catch (Exception ex)
56+
{
57+
await this.ShowMessageAsync(Translate("Error"), $"{Translate("BackupOptionsErrorMessage")}. {Translate("Details")}: {ex.Message}", settings: Program.MainWindow.MetroDialogOptions);
58+
}
59+
}
60+
}
61+
62+
private async void LoadButton_Clicked(object sender, RoutedEventArgs e)
63+
{
64+
65+
var confirmationResult = await this.ShowMessageAsync(Translate("Warning"), Translate("LoadOptionsWarningMessage"),
66+
MessageDialogStyle.AffirmativeAndNegative, Program.MainWindow.MetroDialogOptions);
67+
68+
if (confirmationResult == MessageDialogResult.Negative)
69+
{
70+
return;
71+
}
72+
73+
var optionsFile = new FileInfo(Paths.GetOptionsFilePath());
74+
var ofd = new OpenFileDialog
75+
{
76+
Filter = Constants.DatFilesFilter
77+
};
78+
79+
if (ofd.ShowDialog() is bool result && result)
80+
{
81+
try
82+
{
83+
var pickedFile = new FileInfo(ofd.FileName);
84+
optionsFile.Delete();
85+
pickedFile.CopyTo(optionsFile.FullName);
86+
Program.PreventOptionsSaving = true;
87+
Program.MainWindow.OptionMenuEntry.IsEnabled = false;
88+
await this.ShowMessageAsync(Translate("Success"), $"{Translate("LoadOptionsSuccessMessage")}.", settings: Program.MainWindow.MetroDialogOptions);
89+
Close();
90+
}
91+
catch (Exception ex)
92+
{
93+
await this.ShowMessageAsync(Translate("Error"), $"{Translate("LoadOptionsErrorMessage")}. {Translate("Details")}: {ex.Message}", settings: Program.MainWindow.MetroDialogOptions);
94+
}
95+
}
96+
}
97+
}
98+
}

Utils/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static class Constants
4141
public const string FileSaveFilters = @"Sourcepawn Files (*.sp *.inc)|*.sp;*.inc|All Files (*.*)|*.*";
4242
public const string FileOpenFilters = @"Sourcepawn Files (*.sp *.inc)|*.sp;*.inc|Sourcemod Plugins (*.smx)|*.smx|All Files (*.*)|*.*";
4343
public static string DecompileFileFilters = "Sourcepawn Plugins (*.smx)|*.smx";
44+
public const string DatFilesFilter = "Dat files (.dat)|*.dat";
4445
#endregion
4546

4647
#region Other

0 commit comments

Comments
 (0)