Code cleanup

This commit is contained in:
pointfeev 2021-08-07 18:24:13 -05:00
parent bfaf295339
commit 730d96a473
5 changed files with 112 additions and 66 deletions

View file

@ -1,12 +1,12 @@
using System; using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO; using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using System.Threading;
using System.IO.Compression;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
namespace CreamInstaller namespace CreamInstaller
{ {
@ -34,7 +34,10 @@ namespace CreamInstaller
if (log && !logTextBox.IsDisposed) if (log && !logTextBox.IsDisposed)
{ {
if (logTextBox.Text.Length > 0) if (logTextBox.Text.Length > 0)
{
logTextBox.AppendText(Environment.NewLine, color); logTextBox.AppendText(Environment.NewLine, color);
}
logTextBox.AppendText(userInfoLabel.Text, color); logTextBox.AppendText(userInfoLabel.Text, color);
} }
} }
@ -102,7 +105,9 @@ namespace CreamInstaller
UpdateProgress(100); UpdateProgress(100);
if (!Program.IsProgramRunningDialog(this, selection)) if (!Program.IsProgramRunningDialog(this, selection))
{
throw new OperationCanceledException(); throw new OperationCanceledException();
}
UpdateProgress(0); UpdateProgress(0);
UpdateUser("Installing CreamAPI files for " + selection.ProgramName + " . . . ", LogColor.Operation); UpdateUser("Installing CreamAPI files for " + selection.ProgramName + " . . . ", LogColor.Operation);
@ -159,7 +164,9 @@ namespace CreamInstaller
string file = keyValuePair.Key; string file = keyValuePair.Key;
string backup = keyValuePair.Value; string backup = keyValuePair.Value;
if (!string.IsNullOrEmpty(backup)) if (!string.IsNullOrEmpty(backup))
{
File.Delete(backup); File.Delete(backup);
}
} }
} }
UpdateProgress(100); UpdateProgress(100);
@ -170,12 +177,16 @@ namespace CreamInstaller
foreach (ProgramSelection selection in Program.ProgramSelections.ToList()) foreach (ProgramSelection selection in Program.ProgramSelections.ToList())
{ {
if (Program.Canceled) if (Program.Canceled)
{
break; break;
}
Program.Cleanup(cancel: false, logout: false); Program.Cleanup(cancel: false, logout: false);
if (!Program.IsProgramRunningDialog(this, selection)) if (!Program.IsProgramRunningDialog(this, selection))
{
throw new OperationCanceledException(); throw new OperationCanceledException();
}
try try
{ {
@ -202,7 +213,7 @@ namespace CreamInstaller
} }
} }
private int ProgramCount = Program.ProgramSelections.Count; private readonly int ProgramCount = Program.ProgramSelections.Count;
private async void Start() private async void Start()
{ {

View file

@ -1,12 +1,12 @@
using System; using CG.Web.MegaApiClient;
using Onova;
using Onova.Models;
using Onova.Services;
using System;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using CG.Web.MegaApiClient;
using Onova;
using Onova.Models;
using Onova.Services;
namespace CreamInstaller namespace CreamInstaller
{ {
@ -28,7 +28,7 @@ namespace CreamInstaller
cancellationTokenSource.Dispose(); cancellationTokenSource.Dispose();
cancellationTokenSource = null; cancellationTokenSource = null;
} }
Hide(); Hide();
new SelectForm(this).ShowDialog(); new SelectForm(this).ShowDialog();
Close(); Close();
@ -60,7 +60,9 @@ namespace CreamInstaller
cancellationTokenSource.Dispose(); cancellationTokenSource.Dispose();
cancellationTokenSource = null; cancellationTokenSource = null;
if (checkForUpdatesResult.CanUpdate) if (checkForUpdatesResult.CanUpdate)
{
latestVersion = checkForUpdatesResult.LastVersion; latestVersion = checkForUpdatesResult.LastVersion;
}
} }
catch { } catch { }
} }

View file

@ -24,10 +24,9 @@ namespace CreamInstaller
public static string BackupFileExtension = ".creaminstaller.backup"; public static string BackupFileExtension = ".creaminstaller.backup";
[STAThread] [STAThread]
static void Main() private static void Main()
{ {
bool createdNew = true; Mutex mutex = new Mutex(true, "CreamInstaller", out bool createdNew);
Mutex mutex = new Mutex(true, "CreamInstaller", out createdNew);
if (createdNew) if (createdNew)
{ {
Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.SetHighDpiMode(HighDpiMode.SystemAware);
@ -47,7 +46,9 @@ namespace CreamInstaller
$"ERROR: {selection.ProgramName} is currently running!" + $"ERROR: {selection.ProgramName} is currently running!" +
"\n\nPlease close the program/game to continue . . .", "\n\nPlease close the program/game to continue . . .",
"Retry", "Cancel") == DialogResult.OK) "Retry", "Cancel") == DialogResult.OK)
{
return IsProgramRunningDialog(form, selection); return IsProgramRunningDialog(form, selection);
}
} }
else else
{ {

View file

@ -18,7 +18,9 @@ namespace CreamInstaller
{ {
string file = directory + "\\steam_api64.dll"; string file = directory + "\\steam_api64.dll";
if (file.IsFilePathLocked()) if (file.IsFilePathLocked())
{
return true; return true;
}
} }
return false; return false;
} }
@ -32,13 +34,17 @@ namespace CreamInstaller
public void Add() public void Add()
{ {
if (!Program.ProgramSelections.Contains(this)) if (!Program.ProgramSelections.Contains(this))
{
Program.ProgramSelections.Add(this); Program.ProgramSelections.Add(this);
}
} }
public void Remove() public void Remove()
{ {
if (Program.ProgramSelections.Contains(this)) if (Program.ProgramSelections.Contains(this))
{
Program.ProgramSelections.Remove(this); Program.ProgramSelections.Remove(this);
}
} }
} }
} }

View file

@ -1,14 +1,14 @@
using System; using CG.Web.MegaApiClient;
using System.Windows.Forms;
using CG.Web.MegaApiClient;
using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;
using Gameloop.Vdf; using Gameloop.Vdf;
using Gameloop.Vdf.Linq; using Gameloop.Vdf.Linq;
using System.Threading.Tasks; using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CreamInstaller namespace CreamInstaller
{ {
@ -59,16 +59,25 @@ namespace CreamInstaller
private List<string> GetSteamApiDllDirectoriesFromGameDirectory(string gameDirectory, List<string> steamApiDllDirectories = null) private List<string> GetSteamApiDllDirectoriesFromGameDirectory(string gameDirectory, List<string> steamApiDllDirectories = null)
{ {
if (steamApiDllDirectories is null) if (steamApiDllDirectories is null)
{
steamApiDllDirectories = new(); steamApiDllDirectories = new();
}
string file = gameDirectory + "\\steam_api64.dll"; string file = gameDirectory + "\\steam_api64.dll";
if (File.Exists(file)) if (File.Exists(file))
{
steamApiDllDirectories.Add(gameDirectory); steamApiDllDirectories.Add(gameDirectory);
}
foreach (string _directory in Directory.GetDirectories(gameDirectory)) foreach (string _directory in Directory.GetDirectories(gameDirectory))
{ {
GetSteamApiDllDirectoriesFromGameDirectory(_directory, steamApiDllDirectories); GetSteamApiDllDirectoriesFromGameDirectory(_directory, steamApiDllDirectories);
} }
if (!steamApiDllDirectories.Any()) if (!steamApiDllDirectories.Any())
{
return null; return null;
}
return steamApiDllDirectories; return steamApiDllDirectories;
} }
@ -93,7 +102,7 @@ namespace CreamInstaller
return null; return null;
} }
private List<CheckBox> checkBoxes = new(); private readonly List<CheckBox> checkBoxes = new();
private void GetCreamApiApplicablePrograms(IProgress<int> progress) private void GetCreamApiApplicablePrograms(IProgress<int> progress)
{ {
int maxProgress = 0; int maxProgress = 0;
@ -120,7 +129,9 @@ namespace CreamInstaller
rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string launcherDirectory = rootDirectory + "\\Programs\\Paradox Interactive"; string launcherDirectory = rootDirectory + "\\Programs\\Paradox Interactive";
if (Directory.Exists(launcherDirectory)) if (Directory.Exists(launcherDirectory))
{
directories = GetSteamApiDllDirectoriesFromGameDirectory(launcherDirectory); directories = GetSteamApiDllDirectoriesFromGameDirectory(launcherDirectory);
}
} }
else else
{ {
@ -138,51 +149,51 @@ namespace CreamInstaller
} }
if (!(directories is null)) if (!(directories is null))
{ {
flowLayoutPanel1.Invoke((MethodInvoker) delegate flowLayoutPanel1.Invoke((MethodInvoker)delegate
{ {
ProgramSelection selection = new(); ProgramSelection selection = new();
selection.ProgramName = node.Name; selection.ProgramName = node.Name;
selection.ProgramDirectory = rootDirectory; selection.ProgramDirectory = rootDirectory;
selection.SteamApiDllDirectories = new(); selection.SteamApiDllDirectories = new();
selection.SteamApiDllDirectories.AddRange(directories); selection.SteamApiDllDirectories.AddRange(directories);
foreach (INode _node in fileNodes) foreach (INode _node in fileNodes)
{ {
if (_node.Type == NodeType.File && _node.ParentId == node.Id) if (_node.Type == NodeType.File && _node.ParentId == node.Id)
{ {
selection.DownloadNode = _node; selection.DownloadNode = _node;
break; break;
} }
} }
CheckBox checkBox = new(); CheckBox checkBox = new();
checkBoxes.Add(checkBox); checkBoxes.Add(checkBox);
checkBox.AutoSize = true; checkBox.AutoSize = true;
checkBox.Parent = flowLayoutPanel1; checkBox.Parent = flowLayoutPanel1;
checkBox.Text = node.Name; checkBox.Text = node.Name;
checkBox.Checked = true; checkBox.Checked = true;
checkBox.Enabled = false; checkBox.Enabled = false;
checkBox.TabStop = true; checkBox.TabStop = true;
checkBox.TabIndex = 1 + checkBoxes.Count; checkBox.TabIndex = 1 + checkBoxes.Count;
checkBox.CheckedChanged += (sender, e) => checkBox.CheckedChanged += (sender, e) =>
{ {
if (checkBox.Checked) if (checkBox.Checked)
{ {
selection.Add(); selection.Add();
} }
else else
{ {
selection.Remove(); selection.Remove();
} }
acceptButton.Enabled = Program.ProgramSelections.Count > 0; acceptButton.Enabled = Program.ProgramSelections.Count > 0;
allCheckBox.CheckedChanged -= OnAllCheckBoxChanged; allCheckBox.CheckedChanged -= OnAllCheckBoxChanged;
allCheckBox.Checked = checkBoxes.TrueForAll(checkBox => checkBox.Checked); allCheckBox.Checked = checkBoxes.TrueForAll(checkBox => checkBox.Checked);
allCheckBox.CheckedChanged += OnAllCheckBoxChanged; allCheckBox.CheckedChanged += OnAllCheckBoxChanged;
}; };
}); });
} }
} }
} }
@ -202,7 +213,7 @@ namespace CreamInstaller
} }
else else
{ {
int p = (int)((float)((float)_progress / (float)maxProgress) * 100); int p = (int)((float)(_progress / (float)maxProgress) * 100);
label2.Text = "Finding CreamAPI-applicable programs on your computer . . . " + p + "% (" + _progress + "/" + maxProgress + ")"; label2.Text = "Finding CreamAPI-applicable programs on your computer . . . " + p + "% (" + _progress + "/" + maxProgress + ")";
progressBar1.Value = p; progressBar1.Value = p;
} }
@ -218,7 +229,9 @@ namespace CreamInstaller
{ {
allCheckBox.Enabled = true; allCheckBox.Enabled = true;
foreach (CheckBox checkBox in checkBoxes) foreach (CheckBox checkBox in checkBoxes)
{
checkBox.Enabled = true; checkBox.Enabled = true;
}
acceptButton.Enabled = true; acceptButton.Enabled = true;
} }
@ -226,7 +239,7 @@ namespace CreamInstaller
{ {
noneFoundLabel.Visible = true; noneFoundLabel.Visible = true;
} }
} }
private void OnAccept(object sender, EventArgs e) private void OnAccept(object sender, EventArgs e)
@ -236,7 +249,9 @@ namespace CreamInstaller
foreach (ProgramSelection selection in Program.ProgramSelections) foreach (ProgramSelection selection in Program.ProgramSelections)
{ {
if (!Program.IsProgramRunningDialog(this, selection)) if (!Program.IsProgramRunningDialog(this, selection))
{
return; return;
}
} }
Hide(); Hide();
@ -254,7 +269,10 @@ namespace CreamInstaller
Location = new Point(X, Y); Location = new Point(X, Y);
Show(); Show();
} }
else Close(); else
{
Close();
}
} }
} }
@ -267,10 +285,18 @@ namespace CreamInstaller
{ {
bool shouldCheck = false; bool shouldCheck = false;
foreach (CheckBox checkBox in checkBoxes) foreach (CheckBox checkBox in checkBoxes)
{
if (!checkBox.Checked) if (!checkBox.Checked)
{
shouldCheck = true; shouldCheck = true;
}
}
foreach (CheckBox checkBox in checkBoxes) foreach (CheckBox checkBox in checkBoxes)
{
checkBox.Checked = shouldCheck; checkBox.Checked = shouldCheck;
}
allCheckBox.Checked = shouldCheck; allCheckBox.Checked = shouldCheck;
} }
} }