Files
CreamInstaller/CreamInstaller/SelectForm.cs
T
2021-08-06 22:55:12 -05:00

278 lines
11 KiB
C#

using System;
using System.Windows.Forms;
using CG.Web.MegaApiClient;
using System.Collections.Generic;
using System.IO;
using Microsoft.Win32;
using Gameloop.Vdf;
using Gameloop.Vdf.Linq;
using System.Threading.Tasks;
using System.Drawing;
using System.Linq;
namespace CreamInstaller
{
public partial class SelectForm : Form
{
public SelectForm(IWin32Window owner)
{
Owner = owner as Form;
InitializeComponent();
Program.SelectForm = this;
Text = Program.ApplicationName;
}
private List<string> gameLibraryDirectories;
private List<string> GameLibraryDirectories
{
get
{
if (gameLibraryDirectories != null)
{
return gameLibraryDirectories;
}
List<string> gameDirectories = new List<string>();
string steamInstallPath = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Valve\\Steam", "InstallPath", null) as string;
if (steamInstallPath == null)
{
steamInstallPath = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Valve\\Steam", "InstallPath", null) as string;
}
if (steamInstallPath != null)
{
string mainLibraryFolder = steamInstallPath + "\\steamapps\\common";
gameDirectories.Add(mainLibraryFolder);
string libraryFolders = steamInstallPath + "\\steamapps\\libraryfolders.vdf";
VProperty property = VdfConvert.Deserialize(File.ReadAllText(libraryFolders));
foreach (VProperty _property in property.Value)
{
if (int.TryParse(_property.Key, out _))
{
gameDirectories.Add(_property.Value.ToString());
}
}
}
gameLibraryDirectories = gameDirectories;
return gameDirectories;
}
}
private List<string> GetSteamApiDllDirectoriesFromGameDirectory(string gameDirectory, List<string> steamApiDllDirectories = null)
{
if (steamApiDllDirectories is null)
steamApiDllDirectories = new();
string file = gameDirectory + "\\steam_api64.dll";
if (File.Exists(file))
steamApiDllDirectories.Add(gameDirectory);
foreach (string _directory in Directory.GetDirectories(gameDirectory))
{
GetSteamApiDllDirectoriesFromGameDirectory(_directory, steamApiDllDirectories);
}
if (!steamApiDllDirectories.Any())
return null;
return steamApiDllDirectories;
}
private string GetGameDirectoryFromLibraryDirectory(string gameName, string libraryDirectory)
{
if (Path.GetFileName(libraryDirectory) == gameName)
{
return libraryDirectory;
}
try
{
foreach (string _directory in Directory.GetDirectories(libraryDirectory))
{
string dir = GetGameDirectoryFromLibraryDirectory(gameName, _directory);
if (dir != null)
{
return dir;
}
}
}
catch { }
return null;
}
private List<CheckBox> checkBoxes = new();
private void GetCreamApiApplicablePrograms(IProgress<int> progress)
{
int maxProgress = 0;
IEnumerable<INode> fileNodes = Program.MegaApiClient.GetNodesFromLink(new Uri("https://mega.nz/folder/45YBwIxZ#fsZNZZu9twY2PVLgrB86fA"));
foreach (INode node in fileNodes)
{
if (node.Type == NodeType.Directory && node.Name != "CreamAPI" && node.Name != "Outdated")
{
++maxProgress;
}
}
progress.Report(maxProgress);
int curProgress = 0;
progress.Report(curProgress);
foreach (INode node in fileNodes)
{
if (node.Type == NodeType.Directory && node.Name != "CreamAPI" && node.Name != "Outdated")
{
progress.Report(++curProgress);
string rootDirectory;
List<string> directories = null;
if (node.Name == "Paradox Launcher")
{
rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string launcherDirectory = rootDirectory + "\\Programs\\Paradox Interactive";
if (Directory.Exists(launcherDirectory))
directories = GetSteamApiDllDirectoriesFromGameDirectory(launcherDirectory);
}
else
{
rootDirectory = null;
directories = null;
foreach (string libraryDirectory in GameLibraryDirectories)
{
rootDirectory = GetGameDirectoryFromLibraryDirectory(node.Name, libraryDirectory);
if (rootDirectory != null)
{
directories = GetSteamApiDllDirectoriesFromGameDirectory(rootDirectory);
break;
}
}
}
if (!(directories is null))
{
flowLayoutPanel1.Invoke((MethodInvoker) delegate
{
ProgramSelection selection = new();
selection.ProgramName = node.Name;
selection.ProgramDirectory = rootDirectory;
selection.SteamApiDllDirectories = new();
selection.SteamApiDllDirectories.AddRange(directories);
foreach (INode _node in fileNodes)
{
if (_node.Type == NodeType.File && _node.ParentId == node.Id)
{
selection.DownloadNode = _node;
break;
}
}
CheckBox checkBox = new();
checkBoxes.Add(checkBox);
checkBox.AutoSize = true;
checkBox.Parent = flowLayoutPanel1;
checkBox.Text = node.Name;
checkBox.Checked = true;
checkBox.Enabled = false;
checkBox.TabStop = true;
checkBox.TabIndex = 1 + checkBoxes.Count;
checkBox.CheckedChanged += (sender, e) =>
{
if (checkBox.Checked)
{
selection.Add();
}
else
{
selection.Remove();
}
acceptButton.Enabled = Program.ProgramSelections.Count > 0;
allCheckBox.CheckedChanged -= OnAllCheckBoxChanged;
allCheckBox.Checked = checkBoxes.TrueForAll(checkBox => checkBox.Checked);
allCheckBox.CheckedChanged += OnAllCheckBoxChanged;
};
});
}
}
}
progress.Report(maxProgress);
}
private async void OnLoad(object sender, EventArgs e)
{
label2.Text = "Finding CreamAPI-applicable programs on your computer . . . ";
int maxProgress = 0;
Progress<int> progress = new();
progress.ProgressChanged += (sender, _progress) =>
{
if (maxProgress == 0)
{
maxProgress = _progress;
}
else
{
int p = (int)((float)((float)_progress / (float)maxProgress) * 100);
label2.Text = "Finding CreamAPI-applicable programs on your computer . . . " + p + "% (" + _progress + "/" + maxProgress + ")";
progressBar1.Value = p;
}
};
await Task.Run(() => GetCreamApiApplicablePrograms(progress));
groupBox1.Size = new Size(groupBox1.Size.Width, groupBox1.Size.Height + 44);
label2.Hide();
progressBar1.Hide();
if (Program.ProgramSelections.Any())
{
allCheckBox.Enabled = true;
foreach (CheckBox checkBox in checkBoxes)
checkBox.Enabled = true;
acceptButton.Enabled = true;
}
else
{
noneFoundLabel.Visible = true;
}
}
private void OnAccept(object sender, EventArgs e)
{
if (Program.ProgramSelections.Count > 0)
{
foreach (ProgramSelection selection in Program.ProgramSelections)
{
if (!Program.IsProgramRunningDialog(this, selection))
return;
}
Hide();
InstallForm installForm = new InstallForm(this);
installForm.ShowDialog();
if (installForm.Reselecting)
{
foreach (CheckBox checkBox in checkBoxes)
{
checkBox.Checked = !checkBox.Checked;
checkBox.Checked = !checkBox.Checked; // to fire CheckChanged
}
int X = (installForm.Location.X + installForm.Size.Width / 2) - Size.Width / 2;
int Y = (installForm.Location.Y + installForm.Size.Height / 2) - Size.Height / 2;
Location = new Point(X, Y);
Show();
}
else Close();
}
}
private void OnCancel(object sender, EventArgs e)
{
Close();
}
private void OnAllCheckBoxChanged(object sender, EventArgs e)
{
bool shouldCheck = false;
foreach (CheckBox checkBox in checkBoxes)
if (!checkBox.Checked)
shouldCheck = true;
foreach (CheckBox checkBox in checkBoxes)
checkBox.Checked = shouldCheck;
allCheckBox.Checked = shouldCheck;
}
}
}