v4.6.0.0 (not finished)

- Fixed all (or at least almost all) potential IOExceptions
- Added a minimum & maximum height and width, as well as scroll bars, to dialog boxes
This commit is contained in:
pointfeev 2023-01-29 19:53:21 -05:00
parent 7ae160c610
commit 2253cf973a
23 changed files with 430 additions and 334 deletions

View file

@ -86,8 +86,7 @@ internal class CustomForm : Form
private void OnActivation(object sender, EventArgs args) => Activate();
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
private static extern void SetWindowPos(nint hWnd, nint hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
internal void BringToFrontWithoutActivation()

View file

@ -1,12 +1,12 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using CreamInstaller.Forms;
using CreamInstaller.Resources;
using CreamInstaller.Utility;
using static CreamInstaller.Resources.Resources;
namespace CreamInstaller.Components;
@ -209,7 +209,7 @@ internal sealed class CustomTreeView : TreeView
foreach ((string directory, BinaryType _) in pair.Key.ExecutableDirectories)
{
string path = directory + @"\" + proxy + ".dll";
if (!File.Exists(path) || path.IsResourceFile(ResourceIdentifier.Koaloader))
if (!path.Exists(form: form) || path.IsResourceFile(ResourceIdentifier.Koaloader))
continue;
canUse = false;
break;

View file

@ -4,7 +4,7 @@
<TargetFramework>net7.0-windows</TargetFramework>
<UseWindowsForms>True</UseWindowsForms>
<ApplicationIcon>Resources\ini.ico</ApplicationIcon>
<Version>4.5.0.0</Version>
<Version>4.6.0.0</Version>
<Copyright>2021, pointfeev (https://github.com/pointfeev)</Copyright>
<Company>CreamInstaller</Company>
<Product>Automatic DLC Unlocker Installer &amp; Configuration Generator</Product>

View file

@ -39,7 +39,7 @@ namespace CreamInstaller.Forms
this.cancelButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cancelButton.Location = new System.Drawing.Point(32, 9);
this.cancelButton.Location = new System.Drawing.Point(132, 9);
this.cancelButton.Name = "cancelButton";
this.cancelButton.Padding = new System.Windows.Forms.Padding(12, 0, 12, 0);
this.cancelButton.Size = new System.Drawing.Size(115, 24);
@ -54,7 +54,7 @@ namespace CreamInstaller.Forms
this.acceptButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.acceptButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.acceptButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.acceptButton.Location = new System.Drawing.Point(153, 9);
this.acceptButton.Location = new System.Drawing.Point(253, 9);
this.acceptButton.Name = "acceptButton";
this.acceptButton.Padding = new System.Windows.Forms.Padding(12, 0, 12, 0);
this.acceptButton.Size = new System.Drawing.Size(116, 24);
@ -85,16 +85,18 @@ namespace CreamInstaller.Forms
//
// descriptionPanel
//
this.descriptionPanel.AutoScroll = true;
this.descriptionPanel.AutoSize = true;
this.descriptionPanel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.descriptionPanel.Controls.Add(this.icon);
this.descriptionPanel.Controls.Add(this.descriptionLabel);
this.descriptionPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.descriptionPanel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
this.descriptionPanel.Location = new System.Drawing.Point(0, 0);
this.descriptionPanel.Margin = new System.Windows.Forms.Padding(0);
this.descriptionPanel.Name = "descriptionPanel";
this.descriptionPanel.Padding = new System.Windows.Forms.Padding(12, 12, 12, 6);
this.descriptionPanel.Size = new System.Drawing.Size(284, 73);
this.descriptionPanel.Size = new System.Drawing.Size(384, 73);
this.descriptionPanel.TabIndex = 5;
//
// buttonPanel
@ -108,7 +110,7 @@ namespace CreamInstaller.Forms
this.buttonPanel.Location = new System.Drawing.Point(0, 73);
this.buttonPanel.Name = "buttonPanel";
this.buttonPanel.Padding = new System.Windows.Forms.Padding(12, 6, 0, 12);
this.buttonPanel.Size = new System.Drawing.Size(284, 48);
this.buttonPanel.Size = new System.Drawing.Size(384, 48);
this.buttonPanel.TabIndex = 6;
//
// DialogForm
@ -119,12 +121,14 @@ namespace CreamInstaller.Forms
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.CancelButton = this.cancelButton;
this.ClientSize = new System.Drawing.Size(284, 121);
this.ClientSize = new System.Drawing.Size(384, 121);
this.Controls.Add(this.descriptionPanel);
this.Controls.Add(this.buttonPanel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(1600, 900);
this.MinimizeBox = false;
this.MinimumSize = new System.Drawing.Size(400, 160);
this.Name = "DialogForm";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;

View file

@ -70,8 +70,7 @@ internal sealed partial class InstallForm : CustomForm
$"{(uninstalling ? "Uninstalling" : "Installing")}" + $" {(uninstalling ? "from" : "for")} " + selection.Name
+ $" with root directory \"{selection.RootDirectory}\" . . . ", LogTextBox.Operation);
IEnumerable<string> invalidDirectories = (await selection.RootDirectory.GetExecutables())
?.Where(d => selection.ExecutableDirectories.All(s => s.directory != Path.GetDirectoryName(d.path)))
.Select(d => Path.GetDirectoryName(d.path));
?.Where(d => selection.ExecutableDirectories.All(s => s.directory != Path.GetDirectoryName(d.path))).Select(d => Path.GetDirectoryName(d.path));
if (selection.ExecutableDirectories.All(s => s.directory != selection.RootDirectory))
invalidDirectories = invalidDirectories?.Append(selection.RootDirectory);
invalidDirectories = invalidDirectories?.Distinct();
@ -81,9 +80,9 @@ internal sealed partial class InstallForm : CustomForm
if (Program.Canceled)
throw new CustomMessageException("The operation was canceled.");
directory.GetKoaloaderComponents(out string old_config, out string config);
if (directory.GetKoaloaderProxies().Any(proxy => File.Exists(proxy) && proxy.IsResourceFile(ResourceIdentifier.Koaloader))
|| directory != selection.RootDirectory && Koaloader.AutoLoadDLLs.Any(pair => File.Exists(directory + @"\" + pair.dll))
|| File.Exists(old_config) || File.Exists(config))
if (directory.GetKoaloaderProxies().Any(proxy => proxy.Exists(form: this) && proxy.IsResourceFile(ResourceIdentifier.Koaloader))
|| directory != selection.RootDirectory && Koaloader.AutoLoadDLLs.Any(pair => (directory + @"\" + pair.dll).Exists(form: this))
|| old_config.Exists(form: this) || config.Exists(form: this))
{
UpdateUser("Uninstalling Koaloader from " + selection.Name + $" in incorrect directory \"{directory}\" . . . ", LogTextBox.Operation);
await Koaloader.Uninstall(directory, selection.RootDirectory, this);
@ -96,8 +95,9 @@ internal sealed partial class InstallForm : CustomForm
if (Program.Canceled)
throw new CustomMessageException("The operation was canceled.");
directory.GetKoaloaderComponents(out string old_config, out string config);
if (directory.GetKoaloaderProxies().Any(proxy => File.Exists(proxy) && proxy.IsResourceFile(ResourceIdentifier.Koaloader))
|| Koaloader.AutoLoadDLLs.Any(pair => File.Exists(directory + @"\" + pair.dll)) || File.Exists(old_config) || File.Exists(config))
if (directory.GetKoaloaderProxies().Any(proxy => proxy.Exists(form: this) && proxy.IsResourceFile(ResourceIdentifier.Koaloader))
|| Koaloader.AutoLoadDLLs.Any(pair => (directory + @"\" + pair.dll).Exists(form: this)) || old_config.Exists(form: this)
|| config.Exists(form: this))
{
UpdateUser("Uninstalling Koaloader from " + selection.Name + $" in directory \"{directory}\" . . . ", LogTextBox.Operation);
await Koaloader.Uninstall(directory, selection.RootDirectory, this);
@ -115,9 +115,9 @@ internal sealed partial class InstallForm : CustomForm
directory.GetSmokeApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string old_config,
out string config, out string old_log, out string log, out string cache);
if (uninstallProxy
? File.Exists(api32_o) || File.Exists(api64_o) || File.Exists(old_config) || File.Exists(config) || File.Exists(old_log)
|| File.Exists(log) || File.Exists(cache)
: File.Exists(api32) || File.Exists(api64))
? api32_o.Exists(form: this) || api64_o.Exists(form: this) || old_config.Exists(form: this) || config.Exists(form: this)
|| old_log.Exists(form: this) || log.Exists(form: this) || cache.Exists(form: this)
: api32.Exists(form: this) || api64.Exists(form: this))
{
UpdateUser(
$"{(uninstallProxy ? "Uninstalling" : "Installing")} SmokeAPI" + $" {(uninstallProxy ? "from" : "for")} " + selection.Name
@ -132,8 +132,8 @@ internal sealed partial class InstallForm : CustomForm
{
directory.GetScreamApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string config, out string log);
if (uninstallProxy
? File.Exists(api32_o) || File.Exists(api64_o) || File.Exists(config) || File.Exists(log)
: File.Exists(api32) || File.Exists(api64))
? api32_o.Exists(form: this) || api64_o.Exists(form: this) || config.Exists(form: this) || log.Exists(form: this)
: api32.Exists(form: this) || api64.Exists(form: this))
{
UpdateUser(
$"{(uninstallProxy ? "Uninstalling" : "Installing")} ScreamAPI" + $" {(uninstallProxy ? "from" : "for")} " + selection.Name
@ -148,8 +148,8 @@ internal sealed partial class InstallForm : CustomForm
{
directory.GetUplayR1Components(out string api32, out string api32_o, out string api64, out string api64_o, out string config, out string log);
if (uninstallProxy
? File.Exists(api32_o) || File.Exists(api64_o) || File.Exists(config) || File.Exists(log)
: File.Exists(api32) || File.Exists(api64))
? api32_o.Exists(form: this) || api64_o.Exists(form: this) || config.Exists(form: this) || log.Exists(form: this)
: api32.Exists(form: this) || api64.Exists(form: this))
{
UpdateUser(
$"{(uninstallProxy ? "Uninstalling" : "Installing")} Uplay R1 Unlocker" + $" {(uninstallProxy ? "from" : "for")} " + selection.Name
@ -161,8 +161,8 @@ internal sealed partial class InstallForm : CustomForm
}
directory.GetUplayR2Components(out string old_api32, out string old_api64, out api32, out api32_o, out api64, out api64_o, out config, out log);
if (uninstallProxy
? File.Exists(api32_o) || File.Exists(api64_o) || File.Exists(config) || File.Exists(log)
: File.Exists(old_api32) || File.Exists(old_api64) || File.Exists(api32) || File.Exists(api64))
? api32_o.Exists(form: this) || api64_o.Exists(form: this) || config.Exists(form: this) || log.Exists(form: this)
: old_api32.Exists(form: this) || old_api64.Exists(form: this) || api32.Exists(form: this) || api64.Exists(form: this))
{
UpdateUser(
$"{(uninstallProxy ? "Uninstalling" : "Installing")} Uplay R2 Unlocker" + $" {(uninstallProxy ? "from" : "for")} " + selection.Name

View file

@ -126,7 +126,7 @@ internal sealed partial class SelectForm : CustomForm
List<Task> appTasks = new();
if (programsToScan.Any(c => c.platform is Platform.Paradox))
{
List<string> dllDirectories = await ParadoxLauncher.InstallPath.GetDllDirectoriesFromGameDirectory(Platform.Paradox);
List<string> dllDirectories = await ParadoxLauncher.InstallPath.GetDllDirectoriesFromGameDirectory(Platform.Paradox, this);
if (dllDirectories is not null)
{
ProgramSelection selection = ProgramSelection.FromPlatformId(Platform.Paradox, "PL");
@ -169,7 +169,7 @@ internal sealed partial class SelectForm : CustomForm
{
if (Program.Canceled)
return;
List<string> dllDirectories = await gameDirectory.GetDllDirectoriesFromGameDirectory(Platform.Steam);
List<string> dllDirectories = await gameDirectory.GetDllDirectoriesFromGameDirectory(Platform.Steam, this);
if (dllDirectories is null)
{
Interlocked.Decrement(ref steamGamesToCheck);
@ -325,7 +325,7 @@ internal sealed partial class SelectForm : CustomForm
{
if (Program.Canceled)
return;
List<string> dllDirectories = await directory.GetDllDirectoriesFromGameDirectory(Platform.Epic);
List<string> dllDirectories = await directory.GetDllDirectoriesFromGameDirectory(Platform.Epic, this);
if (dllDirectories is null)
{
RemoveFromRemainingGames(name);
@ -447,7 +447,7 @@ internal sealed partial class SelectForm : CustomForm
{
if (Program.Canceled)
return;
List<string> dllDirectories = await gameDirectory.GetDllDirectoriesFromGameDirectory(Platform.Ubisoft);
List<string> dllDirectories = await gameDirectory.GetDllDirectoriesFromGameDirectory(Platform.Ubisoft, this);
if (dllDirectories is null)
{
RemoveFromRemainingGames(name);
@ -515,7 +515,7 @@ internal sealed partial class SelectForm : CustomForm
resetKoaloaderButton.Enabled = false;
progressLabel.Text = "Waiting for user to select which programs/games to scan . . .";
ShowProgressBar();
await ProgramData.Setup();
await ProgramData.Setup(this);
bool scan = forceScan;
if (!scan && (programsToScan is null || !programsToScan.Any() || forceProvideChoices))
{
@ -748,10 +748,10 @@ internal sealed partial class SelectForm : CustomForm
string appInfoVDF = $@"{SteamCMD.AppInfoPath}\{id}.vdf";
string appInfoJSON = $@"{SteamCMD.AppInfoPath}\{id}.json";
string cooldown = $@"{ProgramData.CooldownPath}\{id}.txt";
if (File.Exists(appInfoVDF) || File.Exists(appInfoJSON))
if (appInfoVDF.Exists(form: this) || appInfoJSON.Exists(form: this))
{
List<ContextMenuItem> queries = new();
if (File.Exists(appInfoJSON))
if (appInfoJSON.Exists(form: this))
{
string platformString = selection is null || selection.Platform is Platform.Steam
? "Steam Store "
@ -760,7 +760,7 @@ internal sealed partial class SelectForm : CustomForm
: "";
queries.Add(new($"Open {platformString}Query", "Notepad", (_, _) => Diagnostics.OpenFileInNotepad(appInfoJSON)));
}
if (File.Exists(appInfoVDF))
if (appInfoVDF.Exists(form: this))
queries.Add(new("Open SteamCMD Query", "Notepad", (_, _) => Diagnostics.OpenFileInNotepad(appInfoVDF)));
if (queries.Any())
{
@ -771,7 +771,7 @@ internal sealed partial class SelectForm : CustomForm
{
try
{
File.Delete(appInfoVDF);
appInfoVDF.Delete();
}
catch
{
@ -779,7 +779,7 @@ internal sealed partial class SelectForm : CustomForm
}
try
{
File.Delete(appInfoJSON);
appInfoJSON.Delete();
}
catch
{
@ -787,7 +787,7 @@ internal sealed partial class SelectForm : CustomForm
}
try
{
File.Delete(cooldown);
cooldown.Delete();
}
catch
{
@ -819,8 +819,9 @@ internal sealed partial class SelectForm : CustomForm
{
directory.GetSmokeApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string old_config,
out string config, out string old_log, out string log, out string cache);
if (File.Exists(api32) || File.Exists(api32_o) || File.Exists(api64) || File.Exists(api64_o) || File.Exists(old_config)
|| File.Exists(config) || File.Exists(old_log) || File.Exists(log) || File.Exists(cache))
if (api32.Exists(form: this) || api32_o.Exists(form: this) || api64.Exists(form: this) || api64_o.Exists(form: this)
|| old_config.Exists(form: this) || config.Exists(form: this) || old_log.Exists(form: this) || log.Exists(form: this)
|| cache.Exists(form: this))
items.Add(new ContextMenuItem($"Open Steamworks Directory #{++steam}", "File Explorer",
(_, _) => Diagnostics.OpenDirectoryInFileExplorer(directory)));
}
@ -829,7 +830,8 @@ internal sealed partial class SelectForm : CustomForm
{
directory.GetScreamApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string config,
out string log);
if (File.Exists(api32) || File.Exists(api32_o) || File.Exists(api64) || File.Exists(api64_o) || File.Exists(config) || File.Exists(log))
if (api32.Exists(form: this) || api32_o.Exists(form: this) || api64.Exists(form: this) || api64_o.Exists(form: this)
|| config.Exists(form: this) || log.Exists(form: this))
items.Add(new ContextMenuItem($"Open EOS Directory #{++epic}", "File Explorer",
(_, _) => Diagnostics.OpenDirectoryInFileExplorer(directory)));
}
@ -838,13 +840,14 @@ internal sealed partial class SelectForm : CustomForm
{
directory.GetUplayR1Components(out string api32, out string api32_o, out string api64, out string api64_o, out string config,
out string log);
if (File.Exists(api32) || File.Exists(api32_o) || File.Exists(api64) || File.Exists(api64_o) || File.Exists(config) || File.Exists(log))
if (api32.Exists(form: this) || api32_o.Exists(form: this) || api64.Exists(form: this) || api64_o.Exists(form: this)
|| config.Exists(form: this) || log.Exists(form: this))
items.Add(new ContextMenuItem($"Open Uplay R1 Directory #{++r1}", "File Explorer",
(_, _) => Diagnostics.OpenDirectoryInFileExplorer(directory)));
directory.GetUplayR2Components(out string old_api32, out string old_api64, out api32, out api32_o, out api64, out api64_o, out config,
out log);
if (File.Exists(old_api32) || File.Exists(old_api64) || File.Exists(api32) || File.Exists(api32_o) || File.Exists(api64)
|| File.Exists(api64_o) || File.Exists(config) || File.Exists(log))
if (old_api32.Exists(form: this) || old_api64.Exists(form: this) || api32.Exists(form: this) || api32_o.Exists(form: this)
|| api64.Exists(form: this) || api64_o.Exists(form: this) || config.Exists(form: this) || log.Exists(form: this))
items.Add(new ContextMenuItem($"Open Uplay R2 Directory #{++r2}", "File Explorer",
(_, _) => Diagnostics.OpenDirectoryInFileExplorer(directory)));
}

View file

@ -41,7 +41,7 @@ internal static class EpicLibrary
{
if (Program.Canceled)
return games;
string json = File.ReadAllText(file);
string json = file.Read();
try
{
Manifest manifest = JsonSerializer.Deserialize<Manifest>(json);

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
@ -26,14 +25,14 @@ internal static class EpicStore
{
List<(string id, string name, string product, string icon, string developer)> dlcIds = new();
string cacheFile = ProgramData.AppInfoPath + @$"\{categoryNamespace}.json";
bool cachedExists = File.Exists(cacheFile);
bool cachedExists = cacheFile.Exists();
Response response = null;
if (!cachedExists || ProgramData.CheckCooldown(categoryNamespace, CooldownEntitlement))
{
response = await QueryGraphQL(categoryNamespace);
try
{
await File.WriteAllTextAsync(cacheFile, JsonConvert.SerializeObject(response, Formatting.Indented));
cacheFile.Write(JsonConvert.SerializeObject(response, Formatting.Indented));
}
catch
{
@ -43,11 +42,11 @@ internal static class EpicStore
else
try
{
response = JsonConvert.DeserializeObject<Response>(await File.ReadAllTextAsync(cacheFile));
response = JsonConvert.DeserializeObject<Response>(cacheFile.Read());
}
catch
{
File.Delete(cacheFile);
cacheFile.Delete();
}
if (response is null)
return dlcIds;

View file

@ -90,28 +90,28 @@ internal static class ParadoxLauncher
foreach (string directory in selection.DllDirectories)
{
bool koaloaderInstalled = Koaloader.AutoLoadDLLs.Select(pair => (pair.unlocker, path: directory + @"\" + pair.dll))
.Any(pair => File.Exists(pair.path) && pair.path.IsResourceFile());
.Any(pair => pair.path.Exists(form: form) && pair.path.IsResourceFile());
directory.GetSmokeApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string old_config,
out string config, out _, out _, out _);
smokeInstalled = smokeInstalled || File.Exists(api32_o) || File.Exists(api64_o)
|| (File.Exists(old_config) || File.Exists(config)) && !koaloaderInstalled
|| File.Exists(api32) && api32.IsResourceFile(ResourceIdentifier.Steamworks32)
|| File.Exists(api64) && api64.IsResourceFile(ResourceIdentifier.Steamworks64);
smokeInstalled = smokeInstalled || api32_o.Exists(form: form) || api64_o.Exists(form: form)
|| (old_config.Exists(form: form) || config.Exists(form: form)) && !koaloaderInstalled
|| api32.Exists(form: form) && api32.IsResourceFile(ResourceIdentifier.Steamworks32)
|| api64.Exists(form: form) && api64.IsResourceFile(ResourceIdentifier.Steamworks64);
await SmokeAPI.Uninstall(directory, deleteOthers: false);
if (steamOriginalSdk32 is null && File.Exists(api32) && !api32.IsResourceFile(ResourceIdentifier.Steamworks32))
steamOriginalSdk32 = await File.ReadAllBytesAsync(api32);
if (steamOriginalSdk64 is null && File.Exists(api64) && !api64.IsResourceFile(ResourceIdentifier.Steamworks64))
steamOriginalSdk64 = await File.ReadAllBytesAsync(api64);
if (steamOriginalSdk32 is null && api32.Exists(form: form) && !api32.IsResourceFile(ResourceIdentifier.Steamworks32))
steamOriginalSdk32 = api32.ReadBytes(true);
if (steamOriginalSdk64 is null && api64.Exists(form: form) && !api64.IsResourceFile(ResourceIdentifier.Steamworks64))
steamOriginalSdk64 = api64.ReadBytes(true);
directory.GetScreamApiComponents(out api32, out api32_o, out api64, out api64_o, out config, out string log);
screamInstalled = screamInstalled || File.Exists(api32_o) || File.Exists(api64_o)
|| (File.Exists(config) || File.Exists(log)) && !koaloaderInstalled
|| File.Exists(api32) && api32.IsResourceFile(ResourceIdentifier.EpicOnlineServices32)
|| File.Exists(api64) && api64.IsResourceFile(ResourceIdentifier.EpicOnlineServices64);
screamInstalled = screamInstalled || api32_o.Exists(form: form) || api64_o.Exists(form: form)
|| (config.Exists(form: form) || log.Exists(form: form)) && !koaloaderInstalled
|| api32.Exists(form: form) && api32.IsResourceFile(ResourceIdentifier.EpicOnlineServices32)
|| api64.Exists(form: form) && api64.IsResourceFile(ResourceIdentifier.EpicOnlineServices64);
await ScreamAPI.Uninstall(directory, deleteOthers: false);
if (epicOriginalSdk32 is null && File.Exists(api32) && !api32.IsResourceFile(ResourceIdentifier.EpicOnlineServices32))
epicOriginalSdk32 = await File.ReadAllBytesAsync(api32);
if (epicOriginalSdk64 is null && File.Exists(api64) && !api64.IsResourceFile(ResourceIdentifier.EpicOnlineServices64))
epicOriginalSdk64 = await File.ReadAllBytesAsync(api64);
if (epicOriginalSdk32 is null && api32.Exists(form: form) && !api32.IsResourceFile(ResourceIdentifier.EpicOnlineServices32))
epicOriginalSdk32 = api32.ReadBytes(true);
if (epicOriginalSdk64 is null && api64.Exists(form: form) && !api64.IsResourceFile(ResourceIdentifier.EpicOnlineServices64))
epicOriginalSdk64 = api64.ReadBytes(true);
}
using DialogForm dialogForm = new(form);
if (steamOriginalSdk32 is not null || steamOriginalSdk64 is not null || epicOriginalSdk32 is not null || epicOriginalSdk64 is not null)

View file

@ -120,7 +120,7 @@ internal static class SteamCMD
internal static async Task Setup(IProgress<int> progress)
{
await Cleanup();
if (!File.Exists(FilePath))
if (!FilePath.Exists())
{
HttpClient httpClient = HttpClientManager.HttpClient;
if (httpClient is null)
@ -128,12 +128,12 @@ internal static class SteamCMD
byte[] file = await httpClient.GetByteArrayAsync(new Uri("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip"));
file.Write(ArchivePath);
ZipFile.ExtractToDirectory(ArchivePath, DirectoryPath);
File.Delete(ArchivePath);
ArchivePath.Delete();
}
if (!File.Exists(DllPath))
if (!DllPath.Exists())
{
FileSystemWatcher watcher = new(DirectoryPath) { Filter = "*", IncludeSubdirectories = true, EnableRaisingEvents = true };
if (File.Exists(DllPath))
if (DllPath.Exists())
progress.Report(-15); // update (not used at the moment)
else
progress.Report(-1660); // install
@ -155,15 +155,15 @@ internal static class SteamCMD
{
if (Directory.Exists(ConfigPath))
foreach (string file in Directory.EnumerateFiles(ConfigPath, "*.tmp"))
File.Delete(file);
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.old"))
File.Delete(file);
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.delete"))
File.Delete(file);
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.crash"))
File.Delete(file);
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.ntfs_transaction_failed"))
File.Delete(file);
file.Delete();
if (Directory.Exists(AppCachePath))
Directory.Delete(AppCachePath, true); // this is definitely needed, so SteamCMD gets the latest information for us
if (Directory.Exists(DumpsPath))
@ -183,21 +183,12 @@ internal static class SteamCMD
{
if (Program.Canceled)
return null;
string output;
string appUpdateFile = $@"{AppInfoPath}\{appId}.vdf";
restart:
if (Program.Canceled)
return null;
if (File.Exists(appUpdateFile))
try
{
output = await File.ReadAllTextAsync(appUpdateFile, Encoding.UTF8);
}
catch
{
goto restart;
}
else
string output = appUpdateFile.Read();
if (output is null)
{
output = await Run(appId) ?? "";
int openBracket = output.IndexOf("{", StringComparison.Ordinal);
@ -206,14 +197,7 @@ internal static class SteamCMD
{
output = $"\"{appId}\"\n" + output[openBracket..(1 + closeBracket)];
output = output.Replace("ERROR! Failed to install app '4' (Invalid platform)", "");
try
{
await File.WriteAllTextAsync(appUpdateFile, output, Encoding.UTF8);
}
catch
{
goto restart;
}
appUpdateFile.Write(output);
}
else
goto restart;
@ -222,7 +206,7 @@ internal static class SteamCMD
return null;
if (!ValveDataFile.TryDeserialize(output, out VProperty appInfo) || appInfo.Value is VValue)
{
File.Delete(appUpdateFile);
appUpdateFile.Delete();
goto restart;
}
if (appInfo.Value.Children().ToList().Count == 0)
@ -237,10 +221,8 @@ internal static class SteamCMD
return appInfo;
List<string> dlcAppIds = await ParseDlcAppIds(appInfo);
foreach (string dlcAppUpdateFile in dlcAppIds.Select(id => $@"{AppInfoPath}\{id}.vdf"))
if (File.Exists(dlcAppUpdateFile))
File.Delete(dlcAppUpdateFile);
if (File.Exists(appUpdateFile))
File.Delete(appUpdateFile);
dlcAppUpdateFile.Delete();
appUpdateFile.Delete();
goto restart;
}

View file

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CreamInstaller.Utility;
using Gameloop.Vdf.Linq;
@ -55,7 +54,7 @@ internal static class SteamLibrary
{
if (Program.Canceled)
return games;
if (!ValveDataFile.TryDeserialize(File.ReadAllText(file, Encoding.UTF8), out VProperty result))
if (!ValveDataFile.TryDeserialize(file.Read(), out VProperty result))
continue;
string appId = result.Value.GetChild("appid")?.ToString();
string installdir = result.Value.GetChild("installdir")?.ToString();
@ -93,7 +92,7 @@ internal static class SteamLibrary
return gameDirectories;
gameDirectories.Add(libraryFolder);
string libraryFolders = libraryFolder + @"\libraryfolders.vdf";
if (!File.Exists(libraryFolders) || !ValveDataFile.TryDeserialize(File.ReadAllText(libraryFolders, Encoding.UTF8), out VProperty result))
if (!libraryFolders.Exists() || !ValveDataFile.TryDeserialize(libraryFolders.Read(), out VProperty result))
return gameDirectories;
foreach (VToken vToken in result.Value.Where(p => p is VProperty property && int.TryParse(property.Key, out int _)))
{

View file

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@ -35,7 +34,7 @@ internal static class SteamStore
if (Program.Canceled)
return null;
string cacheFile = ProgramData.AppInfoPath + @$"\{appId}.json";
bool cachedExists = File.Exists(cacheFile);
bool cachedExists = cacheFile.Exists();
if (!cachedExists || ProgramData.CheckCooldown(appId, isDlc ? CooldownDlc : CooldownGame))
{
string response = await HttpClientManager.EnsureGet($"https://store.steampowered.com/api/appdetails?appids={appId}");
@ -64,7 +63,7 @@ internal static class SteamStore
{
try
{
await File.WriteAllTextAsync(cacheFile, JsonConvert.SerializeObject(data, Formatting.Indented));
cacheFile.Write(JsonConvert.SerializeObject(data, Formatting.Indented));
}
catch
#if DEBUG
@ -118,18 +117,11 @@ internal static class SteamStore
if (cachedExists)
try
{
return JsonConvert.DeserializeObject<AppData>(await File.ReadAllTextAsync(cacheFile));
return JsonConvert.DeserializeObject<AppData>(cacheFile.Read());
}
catch
{
try
{
File.Delete(cacheFile);
}
catch
{
// ignored
}
cacheFile.Delete();
}
if (isDlc || attempts >= 10)
return null;

View file

@ -59,16 +59,16 @@ internal static class Koaloader
private static void CheckConfig(string directory, InstallForm installForm = null)
{
directory.GetKoaloaderComponents(out string old_config, out string config);
if (File.Exists(old_config))
if (old_config.Exists(form: installForm))
{
if (!File.Exists(config))
if (!config.Exists(form: installForm))
{
File.Move(old_config, config!);
old_config.Move(config!);
installForm?.UpdateUser($"Converted old configuration: {Path.GetFileName(old_config)} -> {Path.GetFileName(config)}", LogTextBox.Action, false);
}
else
{
File.Delete(old_config);
old_config.Delete();
installForm?.UpdateUser($"Deleted old configuration: {Path.GetFileName(old_config)}", LogTextBox.Action, false);
}
}
@ -78,15 +78,15 @@ internal static class Koaloader
{
/*if (installForm is not null)
installForm.UpdateUser("Generating Koaloader configuration for " + selection.Name + $" in directory \"{directory}\" . . . ", LogTextBox.Operation);*/
File.Create(config).Close();
config.Create(true, installForm);
StreamWriter writer = new(config, true, Encoding.UTF8);
WriteConfig(writer, targets, modules, installForm);
writer.Flush();
writer.Close();
}
else if (File.Exists(config))
else if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted unnecessary configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
}
@ -136,25 +136,25 @@ internal static class Koaloader
{
directory.GetKoaloaderComponents(out string old_config, out string config);
foreach (string proxyPath in directory.GetKoaloaderProxies()
.Where(proxyPath => File.Exists(proxyPath) && proxyPath.IsResourceFile(ResourceIdentifier.Koaloader)))
.Where(proxyPath => proxyPath.Exists(form: installForm) && proxyPath.IsResourceFile(ResourceIdentifier.Koaloader)))
{
File.Delete(proxyPath);
proxyPath.Delete();
installForm?.UpdateUser($"Deleted Koaloader: {Path.GetFileName(proxyPath)}", LogTextBox.Action, false);
}
foreach ((string unlocker, string path) in AutoLoadDLLs.Select(pair => (pair.unlocker, path: directory + @"\" + pair.dll))
.Where(pair => File.Exists(pair.path) && pair.path.IsResourceFile()))
.Where(pair => pair.path.Exists(form: installForm) && pair.path.IsResourceFile()))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted {unlocker}: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
if (deleteConfig && File.Exists(old_config))
if (deleteConfig && old_config.Exists(form: installForm))
{
File.Delete(old_config);
old_config.Delete();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(old_config)}", LogTextBox.Action, false);
}
if (deleteConfig && File.Exists(config))
if (deleteConfig && config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
await SmokeAPI.Uninstall(directory, installForm, deleteConfig);
@ -171,12 +171,13 @@ internal static class Koaloader
{
string proxy = selection.KoaloaderProxy ?? ProgramSelection.DefaultKoaloaderProxy;
string path = directory + @"\" + proxy + ".dll";
foreach (string _path in directory.GetKoaloaderProxies().Where(p => p != path && File.Exists(p) && p.IsResourceFile(ResourceIdentifier.Koaloader)))
foreach (string _path in directory.GetKoaloaderProxies()
.Where(p => p != path && p.Exists(form: installForm) && p.IsResourceFile(ResourceIdentifier.Koaloader)))
{
File.Delete(_path);
_path.Delete();
installForm?.UpdateUser($"Deleted Koaloader: {Path.GetFileName(_path)}", LogTextBox.Action, false);
}
if (File.Exists(path) && !path.IsResourceFile(ResourceIdentifier.Koaloader))
if (path.Exists(form: installForm) && !path.IsResourceFile(ResourceIdentifier.Koaloader))
throw new CustomMessageException("A non-Koaloader DLL named " + proxy + ".dll already exists in this directory!");
path.WriteProxy(proxy, binaryType);
installForm?.UpdateUser($"Wrote {(binaryType == BinaryType.BIT32 ? "32-bit" : "64-bit")} Koaloader: {Path.GetFileName(path)}", LogTextBox.Action,
@ -204,9 +205,9 @@ internal static class Koaloader
path = directory + @"\SmokeAPI32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted SmokeAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\SmokeAPI32.dll";
@ -221,9 +222,9 @@ internal static class Koaloader
path = directory + @"\SmokeAPI64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted SmokeAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\SmokeAPI64.dll";
@ -244,9 +245,9 @@ internal static class Koaloader
path = directory + @"\ScreamAPI32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted ScreamAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\ScreamAPI32.dll";
@ -261,9 +262,9 @@ internal static class Koaloader
path = directory + @"\ScreamAPI64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted ScreamAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\ScreamAPI64.dll";
@ -283,9 +284,9 @@ internal static class Koaloader
path = directory + @"\UplayR1Unlocker32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}
@ -301,9 +302,9 @@ internal static class Koaloader
path = directory + @"\UplayR1Unlocker64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}
@ -320,9 +321,9 @@ internal static class Koaloader
path = directory + @"\UplayR2Unlocker32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}
@ -338,9 +339,9 @@ internal static class Koaloader
path = directory + @"\UplayR2Unlocker64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (File.Exists(path))
if (path.Exists(form: installForm))
{
File.Delete(path);
path.Delete();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}

View file

@ -7,6 +7,7 @@ using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CreamInstaller.Utility;
namespace CreamInstaller.Resources;
@ -466,8 +467,7 @@ internal static class Resources
return false;
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode), DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
private static extern bool GetBinaryType(string lpApplicationName, out BinaryType lpBinaryType);
internal static bool TryGetFileBinaryType(this string path, out BinaryType binaryType) => GetBinaryType(path, out binaryType);
@ -522,14 +522,14 @@ internal static class Resources
|| subPath.Contains("ANTICHEAT");
}
internal static async Task<List<string>> GetDllDirectoriesFromGameDirectory(this string gameDirectory, Platform platform)
internal static async Task<List<string>> GetDllDirectoriesFromGameDirectory(this string gameDirectory, Platform platform, Form form = null)
=> await Task.Run(() =>
{
List<string> dllDirectories = new();
if (Program.Canceled || !Directory.Exists(gameDirectory))
return null;
foreach (string directory in Directory.EnumerateDirectories(gameDirectory, "*", new EnumerationOptions { RecurseSubdirectories = true })
.Append(gameDirectory))
.Append(gameDirectory))
{
if (Program.Canceled)
return null;
@ -537,35 +537,35 @@ internal static class Resources
if (dllDirectories.Contains(subDirectory))
continue;
bool koaloaderInstalled = Koaloader.AutoLoadDLLs.Select(pair => (pair.unlocker, path: directory + @"\" + pair.dll))
.Any(pair => File.Exists(pair.path) && pair.path.IsResourceFile());
.Any(pair => pair.path.Exists(form: form) && pair.path.IsResourceFile());
if (platform is Platform.Steam or Platform.Paradox)
{
subDirectory.GetSmokeApiComponents(out string api, out string api_o, out string api64, out string api64_o, out string old_config,
out string config, out string old_log, out string log, out string cache);
if (File.Exists(api) || File.Exists(api_o) || File.Exists(api64) || File.Exists(api64_o)
|| (File.Exists(old_config) || File.Exists(config) || File.Exists(old_log) || File.Exists(log) || File.Exists(cache))
&& !koaloaderInstalled)
if (api.Exists(form: form) || api_o.Exists(form: form) || api64.Exists(form: form) || api64_o.Exists(form: form)
|| (old_config.Exists(form: form) || config.Exists(form: form) || old_log.Exists(form: form) || log.Exists(form: form)
|| cache.Exists(form: form)) && !koaloaderInstalled)
dllDirectories.Add(subDirectory);
}
if (platform is Platform.Epic or Platform.Paradox)
{
subDirectory.GetScreamApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string config,
out string log);
if (File.Exists(api32) || File.Exists(api32_o) || File.Exists(api64) || File.Exists(api64_o)
|| (File.Exists(config) || File.Exists(log)) && !koaloaderInstalled)
if (api32.Exists(form: form) || api32_o.Exists(form: form) || api64.Exists(form: form) || api64_o.Exists(form: form)
|| (config.Exists(form: form) || log.Exists(form: form)) && !koaloaderInstalled)
dllDirectories.Add(subDirectory);
}
if (platform is Platform.Ubisoft)
{
subDirectory.GetUplayR1Components(out string api32, out string api32_o, out string api64, out string api64_o, out string config,
out string log);
if (File.Exists(api32) || File.Exists(api32_o) || File.Exists(api64) || File.Exists(api64_o)
|| (File.Exists(config) || File.Exists(log)) && !koaloaderInstalled)
if (api32.Exists(form: form) || api32_o.Exists(form: form) || api64.Exists(form: form) || api64_o.Exists(form: form)
|| (config.Exists(form: form) || log.Exists(form: form)) && !koaloaderInstalled)
dllDirectories.Add(subDirectory);
subDirectory.GetUplayR2Components(out string old_api32, out string old_api64, out api32, out api32_o, out api64, out api64_o, out config,
out log);
if (File.Exists(old_api32) || File.Exists(old_api64) || File.Exists(api32) || File.Exists(api32_o) || File.Exists(api64)
|| File.Exists(api64_o) || (File.Exists(config) || File.Exists(log)) && !koaloaderInstalled)
if (old_api32.Exists(form: form) || old_api64.Exists(form: form) || api32.Exists(form: form) || api32_o.Exists(form: form)
|| api64.Exists(form: form) || api64_o.Exists(form: form) || (config.Exists(form: form) || log.Exists(form: form)) && !koaloaderInstalled)
dllDirectories.Add(subDirectory);
}
}
@ -584,7 +584,7 @@ internal static class Resources
private static string ComputeMD5(this string filePath)
{
if (!File.Exists(filePath))
if (!filePath.Exists())
return null;
#pragma warning disable CA5351
using MD5 md5 = MD5.Create();

View file

@ -39,16 +39,16 @@ internal static class ScreamAPI
{
/*if (installForm is not null)
installForm.UpdateUser("Generating ScreamAPI configuration for " + selection.Name + $" in directory \"{directory}\" . . . ", LogTextBox.Operation);*/
File.Create(config).Close();
config.Create(true, installForm);
StreamWriter writer = new(config, true, Encoding.UTF8);
WriteConfig(writer, new(overrideCatalogItems.ToDictionary(pair => pair.Key, pair => pair.Value), PlatformIdComparer.String),
new(entitlements.ToDictionary(pair => pair.Key, pair => pair.Value), PlatformIdComparer.String), installForm);
writer.Flush();
writer.Close();
}
else if (File.Exists(config))
else if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted unnecessary configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
}
@ -105,36 +105,36 @@ internal static class ScreamAPI
=> await Task.Run(() =>
{
directory.GetScreamApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string config, out string log);
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
if (File.Exists(api32))
if (api32.Exists(form: installForm))
{
File.Delete(api32);
api32.Delete();
installForm?.UpdateUser($"Deleted ScreamAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
File.Move(api32_o, api32!);
api32_o.Move(api32!);
installForm?.UpdateUser($"Restored EOS: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
if (File.Exists(api64))
if (api64.Exists(form: installForm))
{
File.Delete(api64);
api64.Delete();
installForm?.UpdateUser($"Deleted ScreamAPI: {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
File.Move(api64_o, api64!);
api64_o.Move(api64!);
installForm?.UpdateUser($"Restored EOS: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (File.Exists(config))
if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (File.Exists(log))
if (log.Exists(form: installForm))
{
File.Delete(log);
log.Delete();
installForm?.UpdateUser($"Deleted log: {Path.GetFileName(log)}", LogTextBox.Action, false);
}
});
@ -143,22 +143,22 @@ internal static class ScreamAPI
=> await Task.Run(() =>
{
directory.GetScreamApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out _, out _);
if (File.Exists(api32) && !File.Exists(api32_o))
if (api32.Exists(form: installForm) && !api32_o.Exists(form: installForm))
{
File.Move(api32, api32_o!);
api32.Move(api32_o!);
installForm?.UpdateUser($"Renamed EOS: {Path.GetFileName(api32)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
"ScreamAPI.EOSSDK-Win32-Shipping.dll".Write(api32);
installForm?.UpdateUser($"Wrote ScreamAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (File.Exists(api64) && !File.Exists(api64_o))
if (api64.Exists(form: installForm) && !api64_o.Exists(form: installForm))
{
File.Move(api64, api64_o!);
api64.Move(api64_o!);
installForm?.UpdateUser($"Renamed EOS: {Path.GetFileName(api64)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
"ScreamAPI.EOSSDK-Win64-Shipping.dll".Write(api64);
installForm?.UpdateUser($"Wrote ScreamAPI: {Path.GetFileName(api64)}", LogTextBox.Action, false);

View file

@ -51,16 +51,16 @@ internal static class SmokeAPI
extraApps.Add(newExtraPair);
}
injectDlc = injectDlc.ToList();
if (File.Exists(old_config))
if (old_config.Exists(form: installForm))
{
File.Delete(old_config);
old_config.Delete();
installForm?.UpdateUser($"Deleted old configuration: {Path.GetFileName(old_config)}", LogTextBox.Action, false);
}
if (selection.ExtraSelectedDlc.Any(p => p.Value.dlc.Any()) || overrideDlc.Any() || injectDlc.Any())
{
/*if (installForm is not null)
installForm.UpdateUser("Generating SmokeAPI configuration for " + selection.Name + $" in directory \"{directory}\" . . . ", LogTextBox.Operation);*/
File.Create(config).Close();
config.Create(true, installForm);
StreamWriter writer = new(config, true, Encoding.UTF8);
WriteConfig(writer, selection.Id, new(extraApps.ToDictionary(pair => pair.Key, pair => pair.Value), PlatformIdComparer.String),
new(overrideDlc.ToDictionary(pair => pair.Key, pair => pair.Value), PlatformIdComparer.String),
@ -68,9 +68,9 @@ internal static class SmokeAPI
writer.Flush();
writer.Close();
}
else if (File.Exists(config))
else if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted unnecessary configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
}
@ -155,58 +155,58 @@ internal static class SmokeAPI
=> await Task.Run(() =>
{
directory.GetCreamApiComponents(out _, out _, out _, out _, out string oldConfig);
if (File.Exists(oldConfig))
if (oldConfig.Exists(form: installForm))
{
File.Delete(oldConfig);
oldConfig.Delete();
installForm?.UpdateUser($"Deleted old CreamAPI configuration: {Path.GetFileName(oldConfig)}", LogTextBox.Action, false);
}
directory.GetSmokeApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out string old_config,
out string config, out string old_log, out string log, out string cache);
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
if (File.Exists(api32))
if (api32.Exists(form: installForm))
{
File.Delete(api32);
api32.Delete();
installForm?.UpdateUser($"Deleted SmokeAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
File.Move(api32_o, api32!);
api32_o.Move(api32!);
installForm?.UpdateUser($"Restored Steamworks: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
if (File.Exists(api64))
if (api64.Exists(form: installForm))
{
File.Delete(api64);
api64.Delete();
installForm?.UpdateUser($"Deleted SmokeAPI: {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
File.Move(api64_o, api64!);
api64_o.Move(api64!);
installForm?.UpdateUser($"Restored Steamworks: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (File.Exists(old_config))
if (old_config.Exists(form: installForm))
{
File.Delete(old_config);
old_config.Delete();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(old_config)}", LogTextBox.Action, false);
}
if (File.Exists(config))
if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (File.Exists(cache))
if (cache.Exists(form: installForm))
{
File.Delete(cache);
cache.Delete();
installForm?.UpdateUser($"Deleted cache: {Path.GetFileName(cache)}", LogTextBox.Action, false);
}
if (File.Exists(old_log))
if (old_log.Exists(form: installForm))
{
File.Delete(old_log);
old_log.Delete();
installForm?.UpdateUser($"Deleted log: {Path.GetFileName(old_log)}", LogTextBox.Action, false);
}
if (File.Exists(log))
if (log.Exists(form: installForm))
{
File.Delete(log);
log.Delete();
installForm?.UpdateUser($"Deleted log: {Path.GetFileName(log)}", LogTextBox.Action, false);
}
});
@ -215,28 +215,28 @@ internal static class SmokeAPI
=> await Task.Run(() =>
{
directory.GetCreamApiComponents(out _, out _, out _, out _, out string oldConfig);
if (File.Exists(oldConfig))
if (oldConfig.Exists(form: installForm))
{
File.Delete(oldConfig);
oldConfig.Delete();
installForm?.UpdateUser($"Deleted old CreamAPI configuration: {Path.GetFileName(oldConfig)}", LogTextBox.Action, false);
}
directory.GetSmokeApiComponents(out string api32, out string api32_o, out string api64, out string api64_o, out _, out _, out _, out _, out _);
if (File.Exists(api32) && !File.Exists(api32_o))
if (api32.Exists(form: installForm) && !api32_o.Exists(form: installForm))
{
File.Move(api32, api32_o!);
api32.Move(api32_o!);
installForm?.UpdateUser($"Renamed Steamworks: {Path.GetFileName(api32)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
"SmokeAPI.steam_api.dll".Write(api32);
installForm?.UpdateUser($"Wrote SmokeAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (File.Exists(api64) && !File.Exists(api64_o))
if (api64.Exists(form: installForm) && !api64_o.Exists(form: installForm))
{
File.Move(api64, api64_o!);
api64.Move(api64_o!);
installForm?.UpdateUser($"Renamed Steamworks: {Path.GetFileName(api64)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
"SmokeAPI.steam_api64.dll".Write(api64);
installForm?.UpdateUser($"Wrote SmokeAPI: {Path.GetFileName(api64)}", LogTextBox.Action, false);

View file

@ -33,15 +33,15 @@ internal static class UplayR1
{
/*if (installForm is not null)
installForm.UpdateUser("Generating Uplay R1 Unlocker configuration for " + selection.Name + $" in directory \"{directory}\" . . . ", LogTextBox.Operation);*/
File.Create(config).Close();
config.Create(true, installForm);
StreamWriter writer = new(config, true, Encoding.UTF8);
WriteConfig(writer, new(blacklistDlc.ToDictionary(pair => pair.Key, pair => pair.Value), PlatformIdComparer.String), installForm);
writer.Flush();
writer.Close();
}
else if (File.Exists(config))
else if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted unnecessary configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
}
@ -75,36 +75,36 @@ internal static class UplayR1
=> await Task.Run(() =>
{
directory.GetUplayR1Components(out string api32, out string api32_o, out string api64, out string api64_o, out string config, out string log);
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
if (File.Exists(api32))
if (api32.Exists(form: installForm))
{
File.Delete(api32);
api32.Delete();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
File.Move(api32_o, api32!);
api32_o.Move(api32!);
installForm?.UpdateUser($"Restored Uplay R1: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
if (File.Exists(api64))
if (api64.Exists(form: installForm))
{
File.Delete(api64);
api64.Delete();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker: {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
File.Move(api64_o, api64!);
api64_o.Move(api64!);
installForm?.UpdateUser($"Restored Uplay R1: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (File.Exists(config))
if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (File.Exists(log))
if (log.Exists(form: installForm))
{
File.Delete(log);
log.Delete();
installForm?.UpdateUser($"Deleted log: {Path.GetFileName(log)}", LogTextBox.Action, false);
}
});
@ -113,22 +113,22 @@ internal static class UplayR1
=> await Task.Run(() =>
{
directory.GetUplayR1Components(out string api32, out string api32_o, out string api64, out string api64_o, out _, out _);
if (File.Exists(api32) && !File.Exists(api32_o))
if (api32.Exists(form: installForm) && !api32_o.Exists(form: installForm))
{
File.Move(api32, api32_o!);
api32.Move(api32_o!);
installForm?.UpdateUser($"Renamed Uplay R1: {Path.GetFileName(api32)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
"UplayR1.uplay_r1_loader.dll".Write(api32);
installForm?.UpdateUser($"Wrote Uplay R1 Unlocker: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (File.Exists(api64) && !File.Exists(api64_o))
if (api64.Exists(form: installForm) && !api64_o.Exists(form: installForm))
{
File.Move(api64, api64_o!);
api64.Move(api64_o!);
installForm?.UpdateUser($"Renamed Uplay R1: {Path.GetFileName(api64)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
"UplayR1.uplay_r1_loader64.dll".Write(api64);
installForm?.UpdateUser($"Wrote Uplay R1 Unlocker: {Path.GetFileName(api64)}", LogTextBox.Action, false);

View file

@ -35,15 +35,15 @@ internal static class UplayR2
{
/*if (installForm is not null)
installForm.UpdateUser("Generating Uplay R2 Unlocker configuration for " + selection.Name + $" in directory \"{directory}\" . . . ", LogTextBox.Operation);*/
File.Create(config).Close();
config.Create(true, installForm);
StreamWriter writer = new(config, true, Encoding.UTF8);
WriteConfig(writer, new(blacklistDlc.ToDictionary(pair => pair.Key, pair => pair.Value), PlatformIdComparer.String), installForm);
writer.Flush();
writer.Close();
}
else if (File.Exists(config))
else if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted unnecessary configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
}
@ -80,38 +80,38 @@ internal static class UplayR2
{
directory.GetUplayR2Components(out string old_api32, out string old_api64, out string api32, out string api32_o, out string api64,
out string api64_o, out string config, out string log);
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
string api = File.Exists(old_api32) ? old_api32 : api32;
if (File.Exists(api))
string api = old_api32.Exists(form: installForm) ? old_api32 : api32;
if (api.Exists(form: installForm))
{
File.Delete(api);
api.Delete();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);
}
File.Move(api32_o, api!);
api32_o.Move(api!);
installForm?.UpdateUser($"Restored Uplay R2: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
string api = File.Exists(old_api64) ? old_api64 : api64;
if (File.Exists(api))
string api = old_api64.Exists(form: installForm) ? old_api64 : api64;
if (api.Exists(form: installForm))
{
File.Delete(api);
api.Delete();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);
}
File.Move(api64_o, api!);
api64_o.Move(api!);
installForm?.UpdateUser($"Restored Uplay R2: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (File.Exists(config))
if (config.Exists(form: installForm))
{
File.Delete(config);
config.Delete();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (File.Exists(log))
if (log.Exists(form: installForm))
{
File.Delete(log);
log.Delete();
installForm?.UpdateUser($"Deleted log: {Path.GetFileName(log)}", LogTextBox.Action, false);
}
});
@ -121,24 +121,24 @@ internal static class UplayR2
{
directory.GetUplayR2Components(out string old_api32, out string old_api64, out string api32, out string api32_o, out string api64,
out string api64_o, out _, out _);
string api = File.Exists(old_api32) ? old_api32 : api32;
if (File.Exists(api) && !File.Exists(api32_o))
string api = old_api32.Exists(form: installForm) ? old_api32 : api32;
if (api.Exists(form: installForm) && !api32_o.Exists(form: installForm))
{
File.Move(api, api32_o!);
api.Move(api32_o!);
installForm?.UpdateUser($"Renamed Uplay R2: {Path.GetFileName(api)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (File.Exists(api32_o))
if (api32_o.Exists(form: installForm))
{
"UplayR2.upc_r2_loader.dll".Write(api);
installForm?.UpdateUser($"Wrote Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);
}
api = File.Exists(old_api64) ? old_api64 : api64;
if (File.Exists(api) && !File.Exists(api64_o))
api = old_api64.Exists(form: installForm) ? old_api64 : api64;
if (api.Exists(form: installForm) && !api64_o.Exists(form: installForm))
{
File.Move(api, api64_o!);
api.Move(api64_o!);
installForm?.UpdateUser($"Renamed Uplay R2: {Path.GetFileName(api)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (File.Exists(api64_o))
if (api64_o.Exists(form: installForm))
{
"UplayR2.upc_r2_loader64.dll".Write(api);
installForm?.UpdateUser($"Wrote Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);

View file

@ -7,28 +7,28 @@ namespace CreamInstaller.Utility;
internal static class Diagnostics
{
private static string notepadPlusPlusPath;
private static string nppPath;
internal static string NotepadPlusPlusPath
private static string NppPath
{
get
{
notepadPlusPlusPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Notepad++", "", null) as string;
notepadPlusPlusPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432NODE\Notepad++", "", null) as string;
return notepadPlusPlusPath;
nppPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Notepad++", "", null) as string;
nppPath ??= Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432NODE\Notepad++", "", null) as string;
return nppPath;
}
}
internal static string GetNotepadPath()
{
string npp = NotepadPlusPlusPath + @"\notepad++.exe";
return File.Exists(npp) ? npp : Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\notepad.exe";
string npp = NppPath + @"\notepad++.exe";
return npp.Exists() ? npp : Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\notepad.exe";
}
internal static void OpenFileInNotepad(string path)
{
string npp = NotepadPlusPlusPath + @"\notepad++.exe";
if (File.Exists(npp))
string npp = NppPath + @"\notepad++.exe";
if (npp.Exists())
OpenFileInNotepadPlusPlus(npp, path);
else
OpenFileInWindowsNotepad(path);

View file

@ -24,9 +24,8 @@ internal static class ExceptionHandler
if (stackTrace is not null && stackTrace.Length > 0)
{
_ = output.Append(e.GetType() + (": " + e.Message));
for (int i = 0; i < stackTrace.Length; i++)
foreach (string line in stackTrace)
{
string line = stackTrace[i];
int atNum = line.IndexOf("at ", StringComparison.Ordinal);
int inNum = line.IndexOf("in ", StringComparison.Ordinal);
int ciNum = line.LastIndexOf(@"CreamInstaller\", StringComparison.Ordinal);
@ -42,26 +41,24 @@ internal static class ExceptionHandler
e = e.InnerException;
stackDepth++;
}
string outputString = output.ToString();
if (string.IsNullOrWhiteSpace(outputString))
outputString = e?.ToString() ?? "Unknown exception";
using DialogForm dialogForm = new(form ?? Form.ActiveForm);
return dialogForm.Show(SystemIcons.Error, output.ToString(), acceptButtonText, cancelButtonText, caption) == DialogResult.OK;
return dialogForm.Show(SystemIcons.Error, outputString, acceptButtonText, cancelButtonText, caption) == DialogResult.OK;
}
internal static void HandleFatalException(this Exception e)
{
bool? restart = e?.HandleException(caption: Program.Name + " encountered a fatal exception", acceptButtonText: "Restart");
if (restart.HasValue && restart.Value)
Application.Restart();
e.HandleException(caption: Program.Name + " encountered a fatal exception", acceptButtonText: "OK", cancelButtonText: null);
Application.Exit();
}
}
public class CustomMessageException : Exception
{
public CustomMessageException() => Message = "CustomMessageException";
public CustomMessageException(string message) : base(message) => Message = message;
public CustomMessageException(string message, Exception e) : base(message, e) => Message = message;
public override string Message { get; }
public override string ToString() => Message;

View file

@ -1,6 +1,5 @@
using System;
using System.Drawing;
using System.IO;
namespace CreamInstaller.Utility;
@ -8,7 +7,7 @@ internal static class IconGrabber
{
internal const string SteamAppImagesPath = "https://cdn.cloudflare.steamstatic.com/steamcommunity/public/images/apps/";
internal const string GoogleFaviconsApiUrl = "https://www.google.com/s2/favicons";
private const string GoogleFaviconsApiUrl = "https://www.google.com/s2/favicons";
internal static Icon ToIcon(this Image image)
{
@ -18,7 +17,7 @@ internal static class IconGrabber
internal static string GetDomainFaviconUrl(string domain, int size = 16) => GoogleFaviconsApiUrl + $"?domain={domain}&sz={size}";
internal static Image GetFileIconImage(this string path) => File.Exists(path) ? Icon.ExtractAssociatedIcon(path)?.ToBitmap() : null;
internal static Image GetFileIconImage(this string path) => path.Exists() ? Icon.ExtractAssociatedIcon(path)?.ToBitmap() : null;
internal static Image GetNotepadImage() => GetFileIconImage(Diagnostics.GetNotepadPath());

View file

@ -3,8 +3,8 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace CreamInstaller.Utility;
@ -27,7 +27,7 @@ internal static class ProgramData
private static readonly string DlcChoicesPath = DirectoryPath + @"\dlc.json";
private static readonly string KoaloaderProxyChoicesPath = DirectoryPath + @"\proxies.json";
internal static async Task Setup()
internal static async Task Setup(Form form = null)
=> await Task.Run(() =>
{
if (Directory.Exists(DirectoryPathOld))
@ -38,18 +38,17 @@ internal static class ProgramData
}
if (!Directory.Exists(DirectoryPath))
_ = Directory.CreateDirectory(DirectoryPath);
if (!File.Exists(AppInfoVersionPath) || !Version.TryParse(File.ReadAllText(AppInfoVersionPath, Encoding.UTF8), out Version version)
|| version < MinimumAppInfoVersion)
if (!AppInfoVersionPath.Exists(form: form) || !Version.TryParse(AppInfoVersionPath.Read(), out Version version) || version < MinimumAppInfoVersion)
{
if (Directory.Exists(AppInfoPath))
Directory.Delete(AppInfoPath, true);
_ = Directory.CreateDirectory(AppInfoPath);
File.WriteAllText(AppInfoVersionPath, Program.Version, Encoding.UTF8);
AppInfoVersionPath.Write(Program.Version);
}
if (!Directory.Exists(CooldownPath))
_ = Directory.CreateDirectory(CooldownPath);
if (File.Exists(OldProgramChoicesPath))
File.Delete(OldProgramChoicesPath);
if (OldProgramChoicesPath.Exists(form: form))
OldProgramChoicesPath.Delete();
});
internal static bool CheckCooldown(string identifier, int cooldown)
@ -67,11 +66,11 @@ internal static class ProgramData
if (!Directory.Exists(CooldownPath))
return null;
string cooldownFile = CooldownPath + @$"\{identifier}.txt";
if (!File.Exists(cooldownFile))
if (!cooldownFile.Exists())
return null;
try
{
if (DateTime.TryParse(File.ReadAllText(cooldownFile), out DateTime cooldown))
if (DateTime.TryParse(cooldownFile.Read(), out DateTime cooldown))
return cooldown;
}
catch
@ -88,7 +87,7 @@ internal static class ProgramData
string cooldownFile = CooldownPath + @$"\{identifier}.txt";
try
{
File.WriteAllText(cooldownFile, time.ToString(CultureInfo.InvariantCulture));
cooldownFile.Write(time.ToString(CultureInfo.InvariantCulture));
}
catch
{
@ -98,17 +97,18 @@ internal static class ProgramData
internal static IEnumerable<(Platform platform, string id)> ReadProgramChoices()
{
if (!File.Exists(ProgramChoicesPath))
return Enumerable.Empty<(Platform platform, string id)>();
try
{
return JsonConvert.DeserializeObject(File.ReadAllText(ProgramChoicesPath), typeof(List<(Platform platform, string id)>)) as
List<(Platform platform, string id)>;
}
catch
{
return Enumerable.Empty<(Platform platform, string id)>();
}
if (ProgramChoicesPath.Exists())
try
{
if (JsonConvert.DeserializeObject(ProgramChoicesPath.Read(), typeof(List<(Platform platform, string id)>)) is
List<(Platform platform, string id)> choices)
return choices;
}
catch
{
// ignored
}
return Enumerable.Empty<(Platform platform, string id)>();
}
internal static void WriteProgramChoices(IEnumerable<(Platform platform, string id)> choices)
@ -116,9 +116,9 @@ internal static class ProgramData
try
{
if (choices is null || !choices.Any())
File.Delete(ProgramChoicesPath);
ProgramChoicesPath.Delete();
else
File.WriteAllText(ProgramChoicesPath, JsonConvert.SerializeObject(choices));
ProgramChoicesPath.Write(JsonConvert.SerializeObject(choices));
}
catch
{
@ -128,17 +128,18 @@ internal static class ProgramData
internal static IEnumerable<(Platform platform, string gameId, string dlcId)> ReadDlcChoices()
{
if (!File.Exists(DlcChoicesPath))
return Enumerable.Empty<(Platform platform, string gameId, string dlcId)>();
try
{
return JsonConvert.DeserializeObject(File.ReadAllText(DlcChoicesPath), typeof(IEnumerable<(Platform platform, string gameId, string dlcId)>)) as
IEnumerable<(Platform platform, string gameId, string dlcId)>;
}
catch
{
return Enumerable.Empty<(Platform platform, string gameId, string dlcId)>();
}
if (DlcChoicesPath.Exists())
try
{
if (JsonConvert.DeserializeObject(DlcChoicesPath.Read(), typeof(IEnumerable<(Platform platform, string gameId, string dlcId)>)) is
IEnumerable<(Platform platform, string gameId, string dlcId)> choices)
return choices;
}
catch
{
// ignored
}
return Enumerable.Empty<(Platform platform, string gameId, string dlcId)>();
}
internal static void WriteDlcChoices(List<(Platform platform, string gameId, string dlcId)> choices)
@ -146,9 +147,9 @@ internal static class ProgramData
try
{
if (choices is null || !choices.Any())
File.Delete(DlcChoicesPath);
DlcChoicesPath.Delete();
else
File.WriteAllText(DlcChoicesPath, JsonConvert.SerializeObject(choices));
DlcChoicesPath.Write(JsonConvert.SerializeObject(choices));
}
catch
{
@ -158,18 +159,19 @@ internal static class ProgramData
internal static IEnumerable<(Platform platform, string id, string proxy, bool enabled)> ReadKoaloaderChoices()
{
if (!File.Exists(KoaloaderProxyChoicesPath))
return Enumerable.Empty<(Platform platform, string id, string proxy, bool enabled)>();
try
{
return JsonConvert.DeserializeObject(File.ReadAllText(KoaloaderProxyChoicesPath),
typeof(IEnumerable<(Platform platform, string id, string proxy, bool enabled)>)) as
IEnumerable<(Platform platform, string id, string proxy, bool enabled)>;
}
catch
{
return Enumerable.Empty<(Platform platform, string id, string proxy, bool enabled)>();
}
if (KoaloaderProxyChoicesPath.Exists())
try
{
if (JsonConvert.DeserializeObject(KoaloaderProxyChoicesPath.Read(),
typeof(IEnumerable<(Platform platform, string id, string proxy, bool enabled)>)) is
IEnumerable<(Platform platform, string id, string proxy, bool enabled)> choices)
return choices;
}
catch
{
// ignored
}
return Enumerable.Empty<(Platform platform, string id, string proxy, bool enabled)>();
}
internal static void WriteKoaloaderProxyChoices(IEnumerable<(Platform platform, string id, string proxy, bool enabled)> choices)
@ -177,9 +179,9 @@ internal static class ProgramData
try
{
if (choices is null || !choices.Any())
File.Delete(KoaloaderProxyChoicesPath);
KoaloaderProxyChoicesPath.Delete();
else
File.WriteAllText(KoaloaderProxyChoicesPath, JsonConvert.SerializeObject(choices));
KoaloaderProxyChoicesPath.Write(JsonConvert.SerializeObject(choices));
}
catch
{

View file

@ -0,0 +1,119 @@
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using CreamInstaller.Forms;
namespace CreamInstaller.Utility;
internal static class SafeIO
{
internal static bool Exists(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
{
bool exists = File.Exists(filePath);
if (exists || !crucial || filePath.Warn("Failed to find a crucial file", form) is not DialogResult.OK)
return exists;
}
return false;
}
internal static void Create(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
File.Create(filePath).Close();
break;
}
catch
{
if (!crucial || filePath.Warn("Failed to create a crucial file", form) is not DialogResult.OK)
break;
}
}
internal static void Move(this string filePath, string newFilePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
File.Move(filePath, newFilePath);
break;
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to move a crucial file", form) is not DialogResult.OK)
break;
}
}
internal static void Delete(this string filePath, bool crucial = false, Form form = null)
{
if (!filePath.Exists(form: form))
return;
while (!Program.Canceled)
try
{
File.Delete(filePath);
break;
}
catch
{
if (!crucial || filePath.Warn("Failed to delete a crucial file", form) is not DialogResult.OK)
break;
}
}
internal static string Read(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
return File.ReadAllText(filePath, Encoding.UTF8);
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to read a crucial file", form) is not DialogResult.OK)
break;
}
return null;
}
internal static byte[] ReadBytes(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
return File.ReadAllBytes(filePath);
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.Warn("Failed to read a crucial file", form) is not DialogResult.OK)
break;
}
return null;
}
internal static void Write(this string filePath, string text, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
File.WriteAllText(filePath, text, Encoding.UTF8);
break;
}
catch
{
if (!crucial || filePath.Warn("Failed to write a crucial file", form) is not DialogResult.OK)
break;
}
}
private static DialogResult Warn(this string filePath, string message, Form form = null)
{
using DialogForm dialogForm = new(form ?? Form.ActiveForm);
return dialogForm.Show(SystemIcons.Warning, message + ": " + filePath.BeautifyPath(), "Retry", "OK");
}
}