Skip to content

Commit fb37ecd

Browse files
committed
Code refactor, added ability to log to a file, updated dependancies
1 parent 2555da1 commit fb37ecd

15 files changed

+387
-326
lines changed

FileAES/DecryptForm.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,20 @@ namespace FAES_GUI
77
{
88
public partial class DecryptForm : Form
99
{
10-
private UpdateForm _updateForm;
11-
1210
public DecryptForm(FAES_File faesFile)
1311
{
1412
InitializeComponent();
1513

1614
titleLabel.Text += Program.GetVersion();
17-
this.Text = titleLabel.Text;
15+
base.Text = titleLabel.Text;
1816

1917
decryptPanel.LockFileSelect(true);
2018
decryptPanel.setCloseAfterOperationSuccessful(true);
2119
decryptPanel.SetFileToDecrypt(faesFile);
2220

23-
if (!Program.programManager.GetSkipUpdates())
24-
{
25-
_updateForm = new UpdateForm();
26-
_updateForm.CheckForUpdate();
27-
}
21+
if (Program.programManager.GetSkipUpdates()) return;
22+
UpdateForm updateForm = new UpdateForm();
23+
updateForm.CheckForUpdate();
2824
}
2925

3026
private void titleBar_Paint(object sender, PaintEventArgs e)

FileAES/DevForm.cs

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using FAES_GUI.CustomControls;
33
using System;
44
using System.Drawing;
5+
using System.Globalization;
56
using System.IO;
67
using System.Net;
78
using System.Text;
@@ -20,7 +21,7 @@ public DevForm()
2021
InitializeComponent();
2122

2223
titleLabel.Text += Program.GetVersion();
23-
this.Text = titleLabel.Text;
24+
base.Text = titleLabel.Text;
2425

2526
Console.SetOut(new RichTextBoxWriter(consoleTextBox));
2627
}
@@ -125,22 +126,7 @@ private void DoCheckUpdate()
125126

126127
private void ExportLog_Click(object sender, EventArgs e)
127128
{
128-
string logPath = "FileAES-" + DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString() + ".log";
129-
130-
_overrideLogPath = Program.programManager.GetLogPath();
131-
132-
_overrideLogPath = _overrideLogPath.Replace('/', '\\').TrimStart('/', '\\');
133-
134-
if (!string.IsNullOrWhiteSpace(_overrideLogPath))
135-
{
136-
if (_overrideLogPath.Contains("{default}") || _overrideLogPath.Contains("{d}"))
137-
logPath = _overrideLogPath.Replace("{default}", logPath).Replace("{d}", logPath);
138-
else
139-
logPath = _overrideLogPath;
140-
141-
string dir = Directory.GetParent(logPath).FullName;
142-
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
143-
}
129+
string logPath = Utilities.CreateLogFile(true);
144130

145131
try
146132
{
@@ -175,8 +161,7 @@ private void CommandInput(RichTextBox textbox)
175161

176162
if (input[0] == "cryptostreambuffer" || input[0] == "csbuffer" || input[0] == "buffer")
177163
{
178-
uint csBufferTmp = 0;
179-
if (input.Length > 1 && !string.IsNullOrEmpty(input[1]) && uint.TryParse(input[1], out csBufferTmp))
164+
if (input.Length > 1 && !string.IsNullOrEmpty(input[1]) && uint.TryParse(input[1], out uint csBufferTmp))
180165
{
181166
Logging.Log(String.Format("CryptoStream Buffer set to {0} bytes", csBufferTmp));
182167
FileAES_Utilities.SetCryptoStreamBuffer(csBufferTmp);
@@ -222,7 +207,7 @@ private void CommandInput(RichTextBox textbox)
222207
string verCheck = String.Format("https://api.mullak99.co.uk/FAES/IsUpdate.php?app=faes_gui&ver=latest&branch={0}&showver=true", branch);
223208

224209
Logging.Log(String.Format("Getting the latest FAES_GUI version number on branch '{0}'.", branch));
225-
Logging.Log(String.Format("This process may take a few seconds..."));
210+
Logging.Log("This process may take a few seconds...");
226211

227212
WebClient webClient = new WebClient();
228213
string latestVer = webClient.DownloadString(new Uri(verCheck));
@@ -234,7 +219,7 @@ private void CommandInput(RichTextBox textbox)
234219
}
235220
catch
236221
{
237-
Logging.Log(String.Format("Unable to connect to the update server! Please check your internet connection."), Severity.WARN);
222+
Logging.Log("Unable to connect to the update server! Please check your internet connection.", Severity.WARN);
238223
}
239224
});
240225
updateCheckThread.Start();
@@ -246,7 +231,7 @@ private void CommandInput(RichTextBox textbox)
246231
string latestVer = GetLatestVersion();
247232
string currentVer = ConvertVersionToNonFormatted(Program.GetVersion());
248233

249-
string branch = Program.programManager.GetBranch();
234+
Program.programManager.GetBranch();
250235
string compareVersions = String.Format("https://api.mullak99.co.uk/FAES/CompareVersions.php?app=faes_gui&branch={0}&version1={1}&version2={2}", "dev", currentVer, latestVer);
251236

252237
WebClient client = new WebClient();
@@ -255,19 +240,19 @@ private void CommandInput(RichTextBox textbox)
255240
string result = utf.GetString(html).ToLower();
256241

257242
if (String.IsNullOrEmpty(result) || result == "null")
258-
Logging.Log(String.Format("Unable to connect to the update server! Please check your internet connection."), Severity.WARN);
243+
Logging.Log("Unable to connect to the update server! Please check your internet connection.", Severity.WARN);
259244
else if (result.Contains("not exist in the database!") || result == "version1 is newer than version2")
260245
Logging.Log(String.Format("You are on a private build. ({0} is newer than {1}).", currentVer, latestVer));
261246
else if (result == "version1 is older than version2")
262247
Logging.Log(String.Format("You are on an outdated build. ({0} is older than {1}).", currentVer, latestVer));
263248
else if (result == "version1 is equal to version2")
264249
Logging.Log(String.Format("You are on the latest build. ({0} is equal to {1}).", currentVer, latestVer));
265250
else
266-
Logging.Log(String.Format("Unable to connect to the update server! Please check your internet connection."), Severity.WARN);
251+
Logging.Log("Unable to connect to the update server! Please check your internet connection.", Severity.WARN);
267252
}
268253
catch
269254
{
270-
Logging.Log(String.Format("Unable to connect to the update server! Please check your internet connection."), Severity.WARN);
255+
Logging.Log("Unable to connect to the update server! Please check your internet connection.", Severity.WARN);
271256
}
272257

273258
DoCheckUpdate();
@@ -285,13 +270,13 @@ private void CommandInput(RichTextBox textbox)
285270
verToSpoof += input[i].Replace("\"", "").Replace("\'", "");
286271
verToSpoof += " ";
287272
}
288-
verToSpoof.TrimEnd(' ');
273+
verToSpoof = verToSpoof.TrimEnd(' ');
289274
}
290275
else verToSpoof = input[1];
291276

292277
if (verToSpoof.ToLower() == "reset" || verToSpoof.ToLower() == "off" || verToSpoof.ToLower() == "false")
293278
{
294-
Logging.Log(String.Format("Disabled Version Spoofing."));
279+
Logging.Log("Disabled Version Spoofing.");
295280
Program.SetSpoofedVersion(false);
296281
}
297282
else
@@ -302,7 +287,7 @@ private void CommandInput(RichTextBox textbox)
302287
}
303288
else
304289
{
305-
Logging.Log(String.Format("Disabled Version Spoofing."));
290+
Logging.Log("Disabled Version Spoofing.");
306291
Program.SetSpoofedVersion(false);
307292
}
308293
}
@@ -390,8 +375,7 @@ private void TooFewArgsError(string command)
390375

391376
private void ConsoleInputTextBox_TextChanged(object sender, EventArgs e)
392377
{
393-
if (string.IsNullOrWhiteSpace(consoleInputTextBox.Text)) sendInputButton.Enabled = false;
394-
else sendInputButton.Enabled = true;
378+
sendInputButton.Enabled = !string.IsNullOrWhiteSpace(consoleInputTextBox.Text);
395379
}
396380

397381
private string GetLatestVersion()

FileAES/EncryptForm.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
using FAES;
22
using System;
33
using System.Drawing;
4-
using System.Threading;
54
using System.Windows.Forms;
65

76
namespace FAES_GUI
87
{
98
public partial class EncryptForm : Form
109
{
11-
private UpdateForm _updateForm;
12-
1310
public EncryptForm(FAES_File faesFile)
1411
{
1512
InitializeComponent();
1613

1714
titleLabel.Text += Program.GetVersion();
18-
this.Text = titleLabel.Text;
15+
base.Text = titleLabel.Text;
1916

2017
encryptPanel.LockFileSelect(true);
2118
encryptPanel.setCloseAfterOperationSuccessful(true);
2219
encryptPanel.SetFileToEncrypt(faesFile);
2320

24-
if (!Program.programManager.GetSkipUpdates())
25-
{
26-
_updateForm = new UpdateForm();
27-
_updateForm.CheckForUpdate();
28-
}
21+
if (Program.programManager.GetSkipUpdates()) return;
22+
UpdateForm updateForm = new UpdateForm();
23+
updateForm.CheckForUpdate();
2924
}
3025

3126
private void titleBar_Paint(object sender, PaintEventArgs e)

FileAES/Logging.cs

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,81 @@
11
using FAES;
22
using System;
3+
using System.IO;
34

45
namespace FAES_GUI
56
{
67
internal class Logging
78
{
9+
private static string _lastLogPath;
10+
private static bool _wasLogPathSuccess = true;
11+
private static string _logPath;
12+
813
public static void Log(string log, Severity severity = Severity.INFO)
914
{
10-
if (FileAES_Utilities.GetVerboseLogging())
15+
string rawLog;
16+
switch (severity)
1117
{
12-
switch (severity)
18+
case Severity.DEBUG:
19+
if (FileAES_Utilities.GetVerboseLogging())
20+
{
21+
rawLog = String.Format("[DEBUG] {0}", log);
22+
Console.WriteLine(rawLog);
23+
WriteToLogFile(rawLog);
24+
}
25+
break;
26+
27+
case Severity.WARN:
28+
rawLog = String.Format("[WARN] {0}", log);
29+
Console.WriteLine(rawLog);
30+
WriteToLogFile(rawLog);
31+
break;
32+
33+
case Severity.ERROR:
34+
rawLog = String.Format("[ERROR] {0}", log);
35+
Console.WriteLine(rawLog);
36+
WriteToLogFile(rawLog);
37+
break;
38+
39+
default:
40+
rawLog = String.Format("[INFO] {0}", log);
41+
Console.WriteLine(rawLog);
42+
WriteToLogFile(rawLog);
43+
break;
44+
}
45+
}
46+
47+
private static void LogPathInit()
48+
{
49+
if (String.IsNullOrWhiteSpace(_logPath) || _lastLogPath != _logPath)
50+
_logPath = Utilities.CreateLogFile(false);
51+
}
52+
53+
private static void WriteToLogFile(string log)
54+
{
55+
if (Program.programManager != null && Program.programManager.GetLogToFile())
56+
{
57+
LogPathInit();
58+
if (_wasLogPathSuccess)
1359
{
14-
case Severity.DEBUG:
15-
{
16-
Console.WriteLine("[DEBUG] {0}", log);
17-
break;
18-
}
19-
case Severity.WARN:
20-
{
21-
Console.WriteLine("[WARN] {0}", log);
22-
break;
23-
}
24-
case Severity.ERROR:
25-
{
26-
Console.WriteLine("[ERROR] {0}", log);
27-
break;
28-
}
29-
case Severity.INFO:
30-
default:
31-
{
32-
Console.WriteLine("[INFO] {0}", log);
33-
break;
34-
}
60+
_lastLogPath = _logPath;
61+
62+
try
63+
{
64+
File.AppendAllText(_logPath, log + "\r\n");
65+
_wasLogPathSuccess = true;
66+
}
67+
catch (UnauthorizedAccessException)
68+
{
69+
_wasLogPathSuccess = false;
70+
Log(String.Format("You do not have permission to write a log file to this location ({0})!", _lastLogPath), Severity.ERROR);
71+
}
72+
catch (Exception e)
73+
{
74+
_wasLogPathSuccess = false;
75+
Log(String.Format("An unknown error occurred when writing to the log file ({0})! Exception: {1}", _lastLogPath, e), Severity.ERROR);
76+
}
3577
}
3678
}
37-
else if (severity > 0) Console.WriteLine(log);
3879
}
3980
}
4081

0 commit comments

Comments
 (0)