more potential io exception fixes

This commit is contained in:
pointfeev 2023-01-29 21:37:39 -05:00
parent 720a28ead5
commit 2d773481cc
24 changed files with 460 additions and 336 deletions

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Drawing;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using CreamInstaller.Platforms.Paradox;
@ -51,8 +50,8 @@ internal sealed class ContextMenuItem : ToolStripMenuItem
switch (imageIdentifier)
{
case "Paradox Launcher":
if (Directory.Exists(ParadoxLauncher.InstallPath))
foreach (string file in Directory.EnumerateFiles(ParadoxLauncher.InstallPath, "*.exe"))
if (ParadoxLauncher.InstallPath.DirectoryExists())
foreach (string file in ParadoxLauncher.InstallPath.EnumerateDirectory("*.exe"))
{
image = file.GetFileIconImage();
break;

View file

@ -209,7 +209,7 @@ internal sealed class CustomTreeView : TreeView
foreach ((string directory, BinaryType _) in pair.Key.ExecutableDirectories)
{
string path = directory + @"\" + proxy + ".dll";
if (!path.Exists(form: form) || path.IsResourceFile(ResourceIdentifier.Koaloader))
if (!path.FileExists(form: form) || path.IsResourceFile(ResourceIdentifier.Koaloader))
continue;
canUse = false;
break;

View file

@ -91,7 +91,6 @@ namespace CreamInstaller.Forms
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";

View file

@ -80,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 => 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))
if (directory.GetKoaloaderProxies().Any(proxy => proxy.FileExists(form: this) && proxy.IsResourceFile(ResourceIdentifier.Koaloader))
|| directory != selection.RootDirectory && Koaloader.AutoLoadDLLs.Any(pair => (directory + @"\" + pair.dll).FileExists(form: this))
|| old_config.FileExists(form: this) || config.FileExists(form: this))
{
UpdateUser("Uninstalling Koaloader from " + selection.Name + $" in incorrect directory \"{directory}\" . . . ", LogTextBox.Operation);
await Koaloader.Uninstall(directory, selection.RootDirectory, this);
@ -95,9 +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 => 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))
if (directory.GetKoaloaderProxies().Any(proxy => proxy.FileExists(form: this) && proxy.IsResourceFile(ResourceIdentifier.Koaloader))
|| Koaloader.AutoLoadDLLs.Any(pair => (directory + @"\" + pair.dll).FileExists(form: this)) || old_config.FileExists(form: this)
|| config.FileExists(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
? 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))
? api32_o.FileExists(form: this) || api64_o.FileExists(form: this) || old_config.FileExists(form: this) || config.FileExists(form: this)
|| old_log.FileExists(form: this) || log.FileExists(form: this) || cache.FileExists(form: this)
: api32.FileExists(form: this) || api64.FileExists(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
? 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))
? api32_o.FileExists(form: this) || api64_o.FileExists(form: this) || config.FileExists(form: this) || log.FileExists(form: this)
: api32.FileExists(form: this) || api64.FileExists(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
? 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))
? api32_o.FileExists(form: this) || api64_o.FileExists(form: this) || config.FileExists(form: this) || log.FileExists(form: this)
: api32.FileExists(form: this) || api64.FileExists(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
? 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))
? api32_o.FileExists(form: this) || api64_o.FileExists(form: this) || config.FileExists(form: this) || log.FileExists(form: this)
: old_api32.FileExists(form: this) || old_api64.FileExists(form: this) || api32.FileExists(form: this) || api64.FileExists(form: this))
{
UpdateUser(
$"{(uninstallProxy ? "Uninstalling" : "Installing")} Uplay R2 Unlocker" + $" {(uninstallProxy ? "from" : "for")} " + selection.Name

View file

@ -2,7 +2,6 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
@ -520,15 +519,15 @@ internal sealed partial class SelectForm : CustomForm
if (!scan && (programsToScan is null || !programsToScan.Any() || forceProvideChoices))
{
List<(Platform platform, string id, string name, bool alreadySelected)> gameChoices = new();
if (Directory.Exists(ParadoxLauncher.InstallPath))
if (ParadoxLauncher.InstallPath.DirectoryExists())
gameChoices.Add((Platform.Paradox, "PL", "Paradox Launcher",
programsToScan is not null && programsToScan.Any(p => p.platform is Platform.Paradox && p.id == "PL")));
if (Directory.Exists(SteamLibrary.InstallPath))
if (SteamLibrary.InstallPath.DirectoryExists())
foreach ((string appId, string name, string _, int _, string _) in (await SteamLibrary.GetGames()).Where(g
=> !Program.IsGameBlocked(g.name, g.gameDirectory)))
gameChoices.Add((Platform.Steam, appId, name,
programsToScan is not null && programsToScan.Any(p => p.platform is Platform.Steam && p.id == appId)));
if (Directory.Exists(EpicLibrary.EpicManifestsPath))
if (EpicLibrary.EpicManifestsPath.DirectoryExists())
foreach (Manifest manifest in (await EpicLibrary.GetGames()).Where(m => !Program.IsGameBlocked(m.DisplayName, m.InstallLocation)))
gameChoices.Add((Platform.Epic, manifest.CatalogNamespace, manifest.DisplayName,
programsToScan is not null && programsToScan.Any(p => p.platform is Platform.Epic && p.id == manifest.CatalogNamespace)));
@ -571,10 +570,14 @@ internal sealed partial class SelectForm : CustomForm
progressLabel.Text = setup ? $"Setting up SteamCMD . . . {p}%" : $"Gathering and caching your applicable games and their DLCs . . . {p}%";
progressBar.Value = p;
};
if (Directory.Exists(SteamLibrary.InstallPath) && programsToScan is not null && programsToScan.Any(c => c.platform is Platform.Steam))
if (SteamLibrary.InstallPath.DirectoryExists() && programsToScan is not null && programsToScan.Any(c => c.platform is Platform.Steam))
{
progressLabel.Text = "Setting up SteamCMD . . . ";
await SteamCMD.Setup(iProgress);
if (!await SteamCMD.Setup(iProgress))
{
OnLoad(forceScan, true);
return;
}
}
setup = false;
progressLabel.Text = "Gathering and caching your applicable games and their DLCs . . . ";
@ -748,10 +751,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 (appInfoVDF.Exists(form: this) || appInfoJSON.Exists(form: this))
if (appInfoVDF.FileExists(form: this) || appInfoJSON.FileExists(form: this))
{
List<ContextMenuItem> queries = new();
if (appInfoJSON.Exists(form: this))
if (appInfoJSON.FileExists(form: this))
{
string platformString = selection is null || selection.Platform is Platform.Steam
? "Steam Store "
@ -760,7 +763,7 @@ internal sealed partial class SelectForm : CustomForm
: "";
queries.Add(new($"Open {platformString}Query", "Notepad", (_, _) => Diagnostics.OpenFileInNotepad(appInfoJSON)));
}
if (appInfoVDF.Exists(form: this))
if (appInfoVDF.FileExists(form: this))
queries.Add(new("Open SteamCMD Query", "Notepad", (_, _) => Diagnostics.OpenFileInNotepad(appInfoVDF)));
if (queries.Any())
{
@ -769,9 +772,9 @@ internal sealed partial class SelectForm : CustomForm
items.Add(query);
items.Add(new ContextMenuItem("Refresh Queries", "Command Prompt", (_, _) =>
{
appInfoVDF.Delete();
appInfoJSON.Delete();
cooldown.Delete();
appInfoVDF.DeleteFile();
appInfoJSON.DeleteFile();
cooldown.DeleteFile();
OnLoad(true);
}));
}
@ -798,9 +801,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 (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))
if (api32.FileExists(form: this) || api32_o.FileExists(form: this) || api64.FileExists(form: this) || api64_o.FileExists(form: this)
|| old_config.FileExists(form: this) || config.FileExists(form: this) || old_log.FileExists(form: this) || log.FileExists(form: this)
|| cache.FileExists(form: this))
items.Add(new ContextMenuItem($"Open Steamworks Directory #{++steam}", "File Explorer",
(_, _) => Diagnostics.OpenDirectoryInFileExplorer(directory)));
}
@ -809,8 +812,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 (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))
if (api32.FileExists(form: this) || api32_o.FileExists(form: this) || api64.FileExists(form: this) || api64_o.FileExists(form: this)
|| config.FileExists(form: this) || log.FileExists(form: this))
items.Add(new ContextMenuItem($"Open EOS Directory #{++epic}", "File Explorer",
(_, _) => Diagnostics.OpenDirectoryInFileExplorer(directory)));
}
@ -819,14 +822,15 @@ 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 (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))
if (api32.FileExists(form: this) || api32_o.FileExists(form: this) || api64.FileExists(form: this) || api64_o.FileExists(form: this)
|| config.FileExists(form: this) || log.FileExists(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 (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))
if (old_api32.FileExists(form: this) || old_api64.FileExists(form: this) || api32.FileExists(form: this)
|| api32_o.FileExists(form: this) || api64.FileExists(form: this) || api64_o.FileExists(form: this) || config.FileExists(form: this)
|| log.FileExists(form: this))
items.Add(new ContextMenuItem($"Open Uplay R2 Directory #{++r2}", "File Explorer",
(_, _) => Diagnostics.OpenDirectoryInFileExplorer(directory)));
}

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
@ -36,13 +35,13 @@ internal static class EpicLibrary
{
List<Manifest> games = new();
string manifests = EpicManifestsPath;
if (!Directory.Exists(manifests))
if (!manifests.DirectoryExists())
return games;
foreach (string file in Directory.EnumerateFiles(manifests, "*.item"))
foreach (string file in manifests.EnumerateDirectory("*.item"))
{
if (Program.Canceled)
return games;
string json = file.Read();
string json = file.ReadFile();
try
{
Manifest manifest = JsonSerializer.Deserialize<Manifest>(json);

View file

@ -25,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 = cacheFile.Exists();
bool cachedExists = cacheFile.FileExists();
Response response = null;
if (!cachedExists || ProgramData.CheckCooldown(categoryNamespace, CooldownEntitlement))
{
response = await QueryGraphQL(categoryNamespace);
try
{
cacheFile.Write(JsonConvert.SerializeObject(response, Formatting.Indented));
cacheFile.WriteFile(JsonConvert.SerializeObject(response, Formatting.Indented));
}
catch
{
@ -42,11 +42,11 @@ internal static class EpicStore
else
try
{
response = JsonConvert.DeserializeObject<Response>(cacheFile.Read());
response = JsonConvert.DeserializeObject<Response>(cacheFile.ReadFile());
}
catch
{
cacheFile.Delete();
cacheFile.DeleteFile();
}
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 => pair.path.Exists(form: form) && pair.path.IsResourceFile());
.Any(pair => pair.path.FileExists(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 || 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);
smokeInstalled = smokeInstalled || api32_o.FileExists(form: form) || api64_o.FileExists(form: form)
|| (old_config.FileExists(form: form) || config.FileExists(form: form)) && !koaloaderInstalled
|| api32.FileExists(form: form) && api32.IsResourceFile(ResourceIdentifier.Steamworks32)
|| api64.FileExists(form: form) && api64.IsResourceFile(ResourceIdentifier.Steamworks64);
await SmokeAPI.Uninstall(directory, deleteOthers: false);
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);
if (steamOriginalSdk32 is null && api32.FileExists(form: form) && !api32.IsResourceFile(ResourceIdentifier.Steamworks32))
steamOriginalSdk32 = api32.ReadFileBytes(true);
if (steamOriginalSdk64 is null && api64.FileExists(form: form) && !api64.IsResourceFile(ResourceIdentifier.Steamworks64))
steamOriginalSdk64 = api64.ReadFileBytes(true);
directory.GetScreamApiComponents(out api32, out api32_o, out api64, out api64_o, out config, out string log);
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);
screamInstalled = screamInstalled || api32_o.FileExists(form: form) || api64_o.FileExists(form: form)
|| (config.FileExists(form: form) || log.FileExists(form: form)) && !koaloaderInstalled
|| api32.FileExists(form: form) && api32.IsResourceFile(ResourceIdentifier.EpicOnlineServices32)
|| api64.FileExists(form: form) && api64.IsResourceFile(ResourceIdentifier.EpicOnlineServices64);
await ScreamAPI.Uninstall(directory, deleteOthers: false);
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);
if (epicOriginalSdk32 is null && api32.FileExists(form: form) && !api32.IsResourceFile(ResourceIdentifier.EpicOnlineServices32))
epicOriginalSdk32 = api32.ReadFileBytes(true);
if (epicOriginalSdk64 is null && api64.FileExists(form: form) && !api64.IsResourceFile(ResourceIdentifier.EpicOnlineServices64))
epicOriginalSdk64 = api64.ReadFileBytes(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

@ -2,13 +2,15 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CreamInstaller.Forms;
using CreamInstaller.Resources;
using CreamInstaller.Utility;
using Gameloop.Vdf.Linq;
@ -117,23 +119,34 @@ internal static class SteamCMD
goto wait_for_lock;
});
internal static async Task Setup(IProgress<int> progress)
internal static async Task<bool> Setup(IProgress<int> progress)
{
await Cleanup();
if (!FilePath.Exists())
if (!FilePath.FileExists())
{
HttpClient httpClient = HttpClientManager.HttpClient;
if (httpClient is null)
return;
return false;
while (!Program.Canceled)
try
{
byte[] file = await httpClient.GetByteArrayAsync(new Uri("https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip"));
file.WriteResource(ArchivePath);
ZipFile.ExtractToDirectory(ArchivePath, DirectoryPath);
ArchivePath.Delete();
ArchivePath.ExtractZip(DirectoryPath);
ArchivePath.DeleteFile();
break;
}
if (!DllPath.Exists())
catch (Exception e)
{
using DialogForm dialogForm = new(Form.ActiveForm);
if (dialogForm.Show(SystemIcons.Warning, "Failed to download SteamCMD:\n " + e.Message, "Retry", "OK") is not DialogResult.OK)
return false;
}
}
if (DllPath.FileExists())
return true;
FileSystemWatcher watcher = new(DirectoryPath) { Filter = "*", IncludeSubdirectories = true, EnableRaisingEvents = true };
if (DllPath.Exists())
if (DllPath.FileExists())
progress.Report(-15); // update (not used at the moment)
else
progress.Report(-1660); // install
@ -142,36 +155,32 @@ internal static class SteamCMD
watcher.Changed += (_, _) => progress.Report(++cur);
_ = await Run(null);
watcher.Dispose();
}
return true;
}
internal static async Task Cleanup()
=> await Task.Run(async () =>
{
if (!Directory.Exists(DirectoryPath))
if (!DirectoryPath.DirectoryExists())
return;
await Kill();
try
{
if (Directory.Exists(ConfigPath))
foreach (string file in Directory.EnumerateFiles(ConfigPath, "*.tmp"))
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.old"))
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.delete"))
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.crash"))
file.Delete();
foreach (string file in Directory.EnumerateFiles(DirectoryPath, "*.ntfs_transaction_failed"))
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))
Directory.Delete(DumpsPath, true);
if (Directory.Exists(LogsPath))
Directory.Delete(LogsPath, true);
if (Directory.Exists(SteamAppsPath))
Directory.Delete(SteamAppsPath, true); // this is just a useless folder created from +app_update 4
if (ConfigPath.DirectoryExists())
foreach (string file in ConfigPath.EnumerateDirectory("*.tmp"))
file.DeleteFile();
foreach (string file in DirectoryPath.EnumerateDirectory("*.old"))
file.DeleteFile();
foreach (string file in DirectoryPath.EnumerateDirectory("*.delete"))
file.DeleteFile();
foreach (string file in DirectoryPath.EnumerateDirectory("*.crash"))
file.DeleteFile();
foreach (string file in DirectoryPath.EnumerateDirectory("*.ntfs_transaction_failed"))
file.DeleteFile();
AppCachePath.DeleteDirectory(); // this is definitely needed, so SteamCMD gets the latest information for us
DumpsPath.DeleteDirectory();
LogsPath.DeleteDirectory();
SteamAppsPath.DeleteDirectory(); // this is just a useless folder created from +app_update 4
}
catch
{
@ -187,7 +196,7 @@ internal static class SteamCMD
restart:
if (Program.Canceled)
return null;
string output = appUpdateFile.Read();
string output = appUpdateFile.ReadFile();
if (output is null)
{
output = await Run(appId) ?? "";
@ -197,7 +206,7 @@ internal static class SteamCMD
{
output = $"\"{appId}\"\n" + output[openBracket..(1 + closeBracket)];
output = output.Replace("ERROR! Failed to install app '4' (Invalid platform)", "");
appUpdateFile.Write(output);
appUpdateFile.WriteFile(output);
}
else
goto restart;
@ -206,7 +215,7 @@ internal static class SteamCMD
return null;
if (!ValveDataFile.TryDeserialize(output, out VProperty appInfo) || appInfo.Value is VValue)
{
appUpdateFile.Delete();
appUpdateFile.DeleteFile();
goto restart;
}
if (appInfo.Value.Children().ToList().Count == 0)
@ -221,8 +230,8 @@ internal static class SteamCMD
return appInfo;
List<string> dlcAppIds = await ParseDlcAppIds(appInfo);
foreach (string dlcAppUpdateFile in dlcAppIds.Select(id => $@"{AppInfoPath}\{id}.vdf"))
dlcAppUpdateFile.Delete();
appUpdateFile.Delete();
dlcAppUpdateFile.DeleteFile();
appUpdateFile.DeleteFile();
goto restart;
}
@ -275,7 +284,6 @@ internal static class SteamCMD
internal static void Dispose()
{
Kill().Wait();
if (Directory.Exists(DirectoryPath))
Directory.Delete(DirectoryPath, true);
DirectoryPath.DeleteDirectory();
}
}

View file

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CreamInstaller.Utility;
@ -48,13 +47,13 @@ internal static class SteamLibrary
=> await Task.Run(() =>
{
List<(string appId, string name, string branch, int buildId, string gameDirectory)> games = new();
if (Program.Canceled || !Directory.Exists(libraryDirectory))
if (Program.Canceled || !libraryDirectory.DirectoryExists())
return games;
foreach (string file in Directory.EnumerateFiles(libraryDirectory, "*.acf"))
foreach (string file in libraryDirectory.EnumerateDirectory("*.acf"))
{
if (Program.Canceled)
return games;
if (!ValveDataFile.TryDeserialize(file.Read(), out VProperty result))
if (!ValveDataFile.TryDeserialize(file.ReadFile(), out VProperty result))
continue;
string appId = result.Value.GetChild("appid")?.ToString();
string installdir = result.Value.GetChild("installdir")?.ToString();
@ -85,14 +84,14 @@ internal static class SteamLibrary
if (Program.Canceled)
return gameDirectories;
string steamInstallPath = InstallPath;
if (steamInstallPath == null || !Directory.Exists(steamInstallPath))
if (steamInstallPath == null || !steamInstallPath.DirectoryExists())
return gameDirectories;
string libraryFolder = steamInstallPath + @"\steamapps";
if (!Directory.Exists(libraryFolder))
if (!libraryFolder.DirectoryExists())
return gameDirectories;
gameDirectories.Add(libraryFolder);
string libraryFolders = libraryFolder + @"\libraryfolders.vdf";
if (!libraryFolders.Exists() || !ValveDataFile.TryDeserialize(libraryFolders.Read(), out VProperty result))
if (!libraryFolders.FileExists() || !ValveDataFile.TryDeserialize(libraryFolders.ReadFile(), out VProperty result))
return gameDirectories;
foreach (VToken vToken in result.Value.Where(p => p is VProperty property && int.TryParse(property.Key, out int _)))
{
@ -101,7 +100,7 @@ internal static class SteamLibrary
if (string.IsNullOrWhiteSpace(path))
continue;
path += @"\steamapps";
if (Directory.Exists(path) && !gameDirectories.Contains(path))
if (path.DirectoryExists() && !gameDirectories.Contains(path))
gameDirectories.Add(path);
}
return gameDirectories;

View file

@ -35,7 +35,7 @@ internal static class SteamStore
if (Program.Canceled)
return null;
string cacheFile = ProgramData.AppInfoPath + @$"\{appId}.json";
bool cachedExists = cacheFile.Exists();
bool cachedExists = cacheFile.FileExists();
if (!cachedExists || ProgramData.CheckCooldown(appId, isDlc ? CooldownDlc : CooldownGame))
{
string response = await HttpClientManager.EnsureGet($"https://store.steampowered.com/api/appdetails?appids={appId}");
@ -64,7 +64,7 @@ internal static class SteamStore
{
try
{
cacheFile.Write(JsonConvert.SerializeObject(data, Formatting.Indented));
cacheFile.WriteFile(JsonConvert.SerializeObject(data, Formatting.Indented));
}
catch
#if DEBUG
@ -118,11 +118,11 @@ internal static class SteamStore
if (cachedExists)
try
{
return JsonConvert.DeserializeObject<AppData>(cacheFile.Read());
return JsonConvert.DeserializeObject<AppData>(cacheFile.ReadFile());
}
catch
{
cacheFile.Delete();
cacheFile.DeleteFile();
}
if (isDlc || attempts >= 10)
return null;

View file

@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
@ -48,7 +47,7 @@ internal static class Program
return true;
if (directory is null || ProtectedGameDirectoryExceptions.Contains(name))
return false;
return ProtectedGameDirectories.Any(path => Directory.Exists(directory + path));
return ProtectedGameDirectories.Any(path => (directory + path).DirectoryExists());
}
internal static bool IsProgramRunningDialog(Form form, ProgramSelection selection)

View file

@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CreamInstaller.Components;
using CreamInstaller.Resources;
using CreamInstaller.Utility;
using static CreamInstaller.Resources.Resources;
namespace CreamInstaller;
@ -133,12 +133,12 @@ internal sealed class ProgramSelection
_ = All.Remove(this);
return;
}
if (!Directory.Exists(RootDirectory))
if (!RootDirectory.DirectoryExists())
{
_ = All.Remove(this);
return;
}
_ = DllDirectories.RemoveAll(directory => !Directory.Exists(directory));
_ = DllDirectories.RemoveAll(directory => !directory.DirectoryExists());
if (!DllDirectories.Any())
_ = All.Remove(this);
}

View file

@ -60,16 +60,16 @@ internal static class Koaloader
private static void CheckConfig(string directory, InstallForm installForm = null)
{
directory.GetKoaloaderComponents(out string old_config, out string config);
if (old_config.Exists(form: installForm))
if (old_config.FileExists(form: installForm))
{
if (!config.Exists(form: installForm))
if (!config.FileExists(form: installForm))
{
old_config.Move(config!);
old_config.MoveFile(config!);
installForm?.UpdateUser($"Converted old configuration: {Path.GetFileName(old_config)} -> {Path.GetFileName(config)}", LogTextBox.Action, false);
}
else
{
old_config.Delete();
old_config.DeleteFile();
installForm?.UpdateUser($"Deleted old configuration: {Path.GetFileName(old_config)}", LogTextBox.Action, false);
}
}
@ -79,15 +79,15 @@ internal static class Koaloader
{
/*if (installForm is not null)
installForm.UpdateUser("Generating Koaloader configuration for " + selection.Name + $" in directory \"{directory}\" . . . ", LogTextBox.Operation);*/
config.Create(true, installForm);
config.CreateFile(true, installForm);
StreamWriter writer = new(config, true, Encoding.UTF8);
WriteConfig(writer, targets, modules, installForm);
writer.Flush();
writer.Close();
}
else if (config.Exists(form: installForm))
else if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
installForm?.UpdateUser($"Deleted unnecessary configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
}
@ -136,26 +136,26 @@ internal static class Koaloader
=> await Task.Run(async () =>
{
directory.GetKoaloaderComponents(out string old_config, out string config);
foreach (string proxyPath in directory.GetKoaloaderProxies()
.Where(proxyPath => proxyPath.Exists(form: installForm) && proxyPath.IsResourceFile(ResourceIdentifier.Koaloader)))
foreach (string proxyPath in directory.GetKoaloaderProxies().Where(proxyPath
=> proxyPath.FileExists(form: installForm) && proxyPath.IsResourceFile(ResourceIdentifier.Koaloader)))
{
proxyPath.Delete();
proxyPath.DeleteFile();
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 => pair.path.Exists(form: installForm) && pair.path.IsResourceFile()))
.Where(pair => pair.path.FileExists(form: installForm) && pair.path.IsResourceFile()))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted {unlocker}: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
if (deleteConfig && old_config.Exists(form: installForm))
if (deleteConfig && old_config.FileExists(form: installForm))
{
old_config.Delete();
old_config.DeleteFile();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(old_config)}", LogTextBox.Action, false);
}
if (deleteConfig && config.Exists(form: installForm))
if (deleteConfig && config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
await SmokeAPI.Uninstall(directory, installForm, deleteConfig);
@ -173,18 +173,18 @@ 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 && p.Exists(form: installForm) && p.IsResourceFile(ResourceIdentifier.Koaloader)))
.Where(p => p != path && p.FileExists(form: installForm) && p.IsResourceFile(ResourceIdentifier.Koaloader)))
{
_path.Delete();
_path.DeleteFile();
installForm?.UpdateUser($"Deleted Koaloader: {Path.GetFileName(_path)}", LogTextBox.Action, false);
}
if (path.Exists(form: installForm) && !path.IsResourceFile(ResourceIdentifier.Koaloader))
if (path.FileExists(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,
false);
bool bit32 = false, bit64 = false;
foreach (string executable in Directory.EnumerateFiles(directory, "*.exe"))
foreach (string executable in directory.EnumerateDirectory("*.exe"))
if (executable.TryGetFileBinaryType(out BinaryType binaryType))
{
switch (binaryType)
@ -206,9 +206,9 @@ internal static class Koaloader
path = directory + @"\SmokeAPI32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted SmokeAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\SmokeAPI32.dll";
@ -223,9 +223,9 @@ internal static class Koaloader
path = directory + @"\SmokeAPI64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted SmokeAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\SmokeAPI64.dll";
@ -246,9 +246,9 @@ internal static class Koaloader
path = directory + @"\ScreamAPI32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted ScreamAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\ScreamAPI32.dll";
@ -263,9 +263,9 @@ internal static class Koaloader
path = directory + @"\ScreamAPI64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted ScreamAPI from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action, false);
}
path = rootDirectory + @"\ScreamAPI64.dll";
@ -285,9 +285,9 @@ internal static class Koaloader
path = directory + @"\UplayR1Unlocker32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}
@ -303,9 +303,9 @@ internal static class Koaloader
path = directory + @"\UplayR1Unlocker64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}
@ -322,9 +322,9 @@ internal static class Koaloader
path = directory + @"\UplayR2Unlocker32.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}
@ -340,9 +340,9 @@ internal static class Koaloader
path = directory + @"\UplayR2Unlocker64.dll";
if (rootDirectory is not null && directory != rootDirectory)
{
if (path.Exists(form: installForm))
if (path.FileExists(form: installForm))
{
path.Delete();
path.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker from non-root directory: {Path.GetFileName(path)}", LogTextBox.Action,
false);
}

View file

@ -507,9 +507,9 @@ internal static class Resources
=> await Task.Run(() =>
{
List<(string path, BinaryType binaryType)> executables = new();
if (Program.Canceled || !Directory.Exists(rootDirectory))
if (Program.Canceled || !rootDirectory.DirectoryExists())
return null;
foreach (string path in Directory.EnumerateFiles(rootDirectory, "*.exe", new EnumerationOptions { RecurseSubdirectories = true }))
foreach (string path in rootDirectory.EnumerateDirectory("*.exe", true))
{
if (Program.Canceled)
return null;
@ -519,7 +519,7 @@ internal static class Resources
executables.Add((path, binaryType));
Thread.Sleep(1);
}
foreach (string path in Directory.EnumerateFiles(rootDirectory, "*.exe", new EnumerationOptions { RecurseSubdirectories = true }))
foreach (string path in rootDirectory.EnumerateDirectory("*.exe", true))
{
if (Program.Canceled)
return null;
@ -546,10 +546,9 @@ internal static class Resources
=> await Task.Run(() =>
{
List<string> dllDirectories = new();
if (Program.Canceled || !Directory.Exists(gameDirectory))
if (Program.Canceled || !gameDirectory.DirectoryExists())
return null;
foreach (string directory in Directory.EnumerateDirectories(gameDirectory, "*", new EnumerationOptions { RecurseSubdirectories = true })
.Append(gameDirectory))
foreach (string directory in gameDirectory.EnumerateSubdirectories("*", true).Append(gameDirectory))
{
if (Program.Canceled)
return null;
@ -557,35 +556,36 @@ internal static class Resources
if (dllDirectories.Contains(subDirectory))
continue;
bool koaloaderInstalled = Koaloader.AutoLoadDLLs.Select(pair => (pair.unlocker, path: directory + @"\" + pair.dll))
.Any(pair => pair.path.Exists(form: form) && pair.path.IsResourceFile());
.Any(pair => pair.path.FileExists(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 (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)
if (api.FileExists(form: form) || api_o.FileExists(form: form) || api64.FileExists(form: form) || api64_o.FileExists(form: form)
|| (old_config.FileExists(form: form) || config.FileExists(form: form) || old_log.FileExists(form: form) || log.FileExists(form: form)
|| cache.FileExists(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 (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)
if (api32.FileExists(form: form) || api32_o.FileExists(form: form) || api64.FileExists(form: form) || api64_o.FileExists(form: form)
|| (config.FileExists(form: form) || log.FileExists(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 (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)
if (api32.FileExists(form: form) || api32_o.FileExists(form: form) || api64.FileExists(form: form) || api64_o.FileExists(form: form)
|| (config.FileExists(form: form) || log.FileExists(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 (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)
if (old_api32.FileExists(form: form) || old_api64.FileExists(form: form) || api32.FileExists(form: form) || api32_o.FileExists(form: form)
|| api64.FileExists(form: form) || api64_o.FileExists(form: form)
|| (config.FileExists(form: form) || log.FileExists(form: form)) && !koaloaderInstalled)
dllDirectories.Add(subDirectory);
}
}
@ -604,7 +604,7 @@ internal static class Resources
private static string ComputeMD5(this string filePath)
{
if (!filePath.Exists())
if (!filePath.FileExists())
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);*/
config.Create(true, installForm);
config.CreateFile(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 (config.Exists(form: installForm))
else if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
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 (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
if (api32.Exists(form: installForm))
if (api32.FileExists(form: installForm))
{
api32.Delete();
api32.DeleteFile();
installForm?.UpdateUser($"Deleted ScreamAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
api32_o.Move(api32!);
api32_o.MoveFile(api32!);
installForm?.UpdateUser($"Restored EOS: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
if (api64.Exists(form: installForm))
if (api64.FileExists(form: installForm))
{
api64.Delete();
api64.DeleteFile();
installForm?.UpdateUser($"Deleted ScreamAPI: {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
api64_o.Move(api64!);
api64_o.MoveFile(api64!);
installForm?.UpdateUser($"Restored EOS: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (config.Exists(form: installForm))
if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (log.Exists(form: installForm))
if (log.FileExists(form: installForm))
{
log.Delete();
log.DeleteFile();
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 (api32.Exists(form: installForm) && !api32_o.Exists(form: installForm))
if (api32.FileExists(form: installForm) && !api32_o.FileExists(form: installForm))
{
api32.Move(api32_o!);
api32.MoveFile(api32_o!);
installForm?.UpdateUser($"Renamed EOS: {Path.GetFileName(api32)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
"ScreamAPI.EOSSDK-Win32-Shipping.dll".WriteManifestResource(api32);
installForm?.UpdateUser($"Wrote ScreamAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (api64.Exists(form: installForm) && !api64_o.Exists(form: installForm))
if (api64.FileExists(form: installForm) && !api64_o.FileExists(form: installForm))
{
api64.Move(api64_o!);
api64.MoveFile(api64_o!);
installForm?.UpdateUser($"Renamed EOS: {Path.GetFileName(api64)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
"ScreamAPI.EOSSDK-Win64-Shipping.dll".WriteManifestResource(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 (old_config.Exists(form: installForm))
if (old_config.FileExists(form: installForm))
{
old_config.Delete();
old_config.DeleteFile();
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);*/
config.Create(true, installForm);
config.CreateFile(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 (config.Exists(form: installForm))
else if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
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 (oldConfig.Exists(form: installForm))
if (oldConfig.FileExists(form: installForm))
{
oldConfig.Delete();
oldConfig.DeleteFile();
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 (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
if (api32.Exists(form: installForm))
if (api32.FileExists(form: installForm))
{
api32.Delete();
api32.DeleteFile();
installForm?.UpdateUser($"Deleted SmokeAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
api32_o.Move(api32!);
api32_o.MoveFile(api32!);
installForm?.UpdateUser($"Restored Steamworks: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
if (api64.Exists(form: installForm))
if (api64.FileExists(form: installForm))
{
api64.Delete();
api64.DeleteFile();
installForm?.UpdateUser($"Deleted SmokeAPI: {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
api64_o.Move(api64!);
api64_o.MoveFile(api64!);
installForm?.UpdateUser($"Restored Steamworks: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (old_config.Exists(form: installForm))
if (old_config.FileExists(form: installForm))
{
old_config.Delete();
old_config.DeleteFile();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(old_config)}", LogTextBox.Action, false);
}
if (config.Exists(form: installForm))
if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (cache.Exists(form: installForm))
if (cache.FileExists(form: installForm))
{
cache.Delete();
cache.DeleteFile();
installForm?.UpdateUser($"Deleted cache: {Path.GetFileName(cache)}", LogTextBox.Action, false);
}
if (old_log.Exists(form: installForm))
if (old_log.FileExists(form: installForm))
{
old_log.Delete();
old_log.DeleteFile();
installForm?.UpdateUser($"Deleted log: {Path.GetFileName(old_log)}", LogTextBox.Action, false);
}
if (log.Exists(form: installForm))
if (log.FileExists(form: installForm))
{
log.Delete();
log.DeleteFile();
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 (oldConfig.Exists(form: installForm))
if (oldConfig.FileExists(form: installForm))
{
oldConfig.Delete();
oldConfig.DeleteFile();
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 (api32.Exists(form: installForm) && !api32_o.Exists(form: installForm))
if (api32.FileExists(form: installForm) && !api32_o.FileExists(form: installForm))
{
api32.Move(api32_o!);
api32.MoveFile(api32_o!);
installForm?.UpdateUser($"Renamed Steamworks: {Path.GetFileName(api32)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
"SmokeAPI.steam_api.dll".WriteManifestResource(api32);
installForm?.UpdateUser($"Wrote SmokeAPI: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (api64.Exists(form: installForm) && !api64_o.Exists(form: installForm))
if (api64.FileExists(form: installForm) && !api64_o.FileExists(form: installForm))
{
api64.Move(api64_o!);
api64.MoveFile(api64_o!);
installForm?.UpdateUser($"Renamed Steamworks: {Path.GetFileName(api64)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
"SmokeAPI.steam_api64.dll".WriteManifestResource(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);*/
config.Create(true, installForm);
config.CreateFile(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 (config.Exists(form: installForm))
else if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
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 (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
if (api32.Exists(form: installForm))
if (api32.FileExists(form: installForm))
{
api32.Delete();
api32.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
api32_o.Move(api32!);
api32_o.MoveFile(api32!);
installForm?.UpdateUser($"Restored Uplay R1: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
if (api64.Exists(form: installForm))
if (api64.FileExists(form: installForm))
{
api64.Delete();
api64.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R1 Unlocker: {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
api64_o.Move(api64!);
api64_o.MoveFile(api64!);
installForm?.UpdateUser($"Restored Uplay R1: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api64)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (config.Exists(form: installForm))
if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (log.Exists(form: installForm))
if (log.FileExists(form: installForm))
{
log.Delete();
log.DeleteFile();
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 (api32.Exists(form: installForm) && !api32_o.Exists(form: installForm))
if (api32.FileExists(form: installForm) && !api32_o.FileExists(form: installForm))
{
api32.Move(api32_o!);
api32.MoveFile(api32_o!);
installForm?.UpdateUser($"Renamed Uplay R1: {Path.GetFileName(api32)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
"UplayR1.uplay_r1_loader.dll".WriteManifestResource(api32);
installForm?.UpdateUser($"Wrote Uplay R1 Unlocker: {Path.GetFileName(api32)}", LogTextBox.Action, false);
}
if (api64.Exists(form: installForm) && !api64_o.Exists(form: installForm))
if (api64.FileExists(form: installForm) && !api64_o.FileExists(form: installForm))
{
api64.Move(api64_o!);
api64.MoveFile(api64_o!);
installForm?.UpdateUser($"Renamed Uplay R1: {Path.GetFileName(api64)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
"UplayR1.uplay_r1_loader64.dll".WriteManifestResource(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);*/
config.Create(true, installForm);
config.CreateFile(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 (config.Exists(form: installForm))
else if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
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 (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
string api = old_api32.Exists(form: installForm) ? old_api32 : api32;
if (api.Exists(form: installForm))
string api = old_api32.FileExists(form: installForm) ? old_api32 : api32;
if (api.FileExists(form: installForm))
{
api.Delete();
api.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);
}
api32_o.Move(api!);
api32_o.MoveFile(api!);
installForm?.UpdateUser($"Restored Uplay R2: {Path.GetFileName(api32_o)} -> {Path.GetFileName(api)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
string api = old_api64.Exists(form: installForm) ? old_api64 : api64;
if (api.Exists(form: installForm))
string api = old_api64.FileExists(form: installForm) ? old_api64 : api64;
if (api.FileExists(form: installForm))
{
api.Delete();
api.DeleteFile();
installForm?.UpdateUser($"Deleted Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);
}
api64_o.Move(api!);
api64_o.MoveFile(api!);
installForm?.UpdateUser($"Restored Uplay R2: {Path.GetFileName(api64_o)} -> {Path.GetFileName(api)}", LogTextBox.Action, false);
}
if (!deleteOthers)
return;
if (config.Exists(form: installForm))
if (config.FileExists(form: installForm))
{
config.Delete();
config.DeleteFile();
installForm?.UpdateUser($"Deleted configuration: {Path.GetFileName(config)}", LogTextBox.Action, false);
}
if (log.Exists(form: installForm))
if (log.FileExists(form: installForm))
{
log.Delete();
log.DeleteFile();
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 = old_api32.Exists(form: installForm) ? old_api32 : api32;
if (api.Exists(form: installForm) && !api32_o.Exists(form: installForm))
string api = old_api32.FileExists(form: installForm) ? old_api32 : api32;
if (api.FileExists(form: installForm) && !api32_o.FileExists(form: installForm))
{
api.Move(api32_o!);
api.MoveFile(api32_o!);
installForm?.UpdateUser($"Renamed Uplay R2: {Path.GetFileName(api)} -> {Path.GetFileName(api32_o)}", LogTextBox.Action, false);
}
if (api32_o.Exists(form: installForm))
if (api32_o.FileExists(form: installForm))
{
"UplayR2.upc_r2_loader.dll".WriteManifestResource(api);
installForm?.UpdateUser($"Wrote Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);
}
api = old_api64.Exists(form: installForm) ? old_api64 : api64;
if (api.Exists(form: installForm) && !api64_o.Exists(form: installForm))
api = old_api64.FileExists(form: installForm) ? old_api64 : api64;
if (api.FileExists(form: installForm) && !api64_o.FileExists(form: installForm))
{
api.Move(api64_o!);
api.MoveFile(api64_o!);
installForm?.UpdateUser($"Renamed Uplay R2: {Path.GetFileName(api)} -> {Path.GetFileName(api64_o)}", LogTextBox.Action, false);
}
if (api64_o.Exists(form: installForm))
if (api64_o.FileExists(form: installForm))
{
"UplayR2.upc_r2_loader64.dll".WriteManifestResource(api);
installForm?.UpdateUser($"Wrote Uplay R2 Unlocker: {Path.GetFileName(api)}", LogTextBox.Action, false);

View file

@ -22,13 +22,13 @@ internal static class Diagnostics
internal static string GetNotepadPath()
{
string npp = NppPath + @"\notepad++.exe";
return npp.Exists() ? npp : Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\notepad.exe";
return npp.FileExists() ? npp : Environment.GetFolderPath(Environment.SpecialFolder.Windows) + @"\notepad.exe";
}
internal static void OpenFileInNotepad(string path)
{
string npp = NppPath + @"\notepad++.exe";
if (npp.Exists())
if (npp.FileExists())
OpenFileInNotepadPlusPlus(npp, path);
else
OpenFileInWindowsNotepad(path);

View file

@ -3,6 +3,9 @@ using System.Drawing;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack;
#if DEBUG
using CreamInstaller.Forms;
#endif
namespace CreamInstaller.Utility;
@ -25,13 +28,21 @@ internal static class HttpClientManager
_ = response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
#if DEBUG
catch (Exception e)
{
DebugForm.Current.Log("Get request failed to " + url + ": " + e, LogTextBox.Warning);
return null;
}
#else
catch
{
return null;
}
#endif
}
internal static HtmlDocument ToHtmlDocument(this string html)
private static HtmlDocument ToHtmlDocument(this string html)
{
HtmlDocument document = new();
document.LoadHtml(html);
@ -39,9 +50,9 @@ internal static class HttpClientManager
}
internal static async Task<HtmlNodeCollection> GetDocumentNodes(string url, string xpath)
=> (await EnsureGet(url))?.ToHtmlDocument()?.DocumentNode?.SelectNodes(xpath);
=> (await EnsureGet(url))?.ToHtmlDocument()?.GetDocumentNodes(xpath);
internal static HtmlNodeCollection GetDocumentNodes(this HtmlDocument htmlDocument, string xpath) => htmlDocument.DocumentNode?.SelectNodes(xpath);
private static HtmlNodeCollection GetDocumentNodes(this HtmlDocument htmlDocument, string xpath) => htmlDocument.DocumentNode?.SelectNodes(xpath);
internal static async Task<Image> GetImageFromUrl(string url)
{

View file

@ -17,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) => path.Exists() ? Icon.ExtractAssociatedIcon(path)?.ToBitmap() : null;
internal static Image GetFileIconImage(this string path) => path.FileExists() ? Icon.ExtractAssociatedIcon(path)?.ToBitmap() : null;
internal static Image GetNotepadImage() => GetFileIconImage(Diagnostics.GetNotepadPath());

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
@ -30,25 +29,22 @@ internal static class ProgramData
internal static async Task Setup(Form form = null)
=> await Task.Run(() =>
{
if (Directory.Exists(DirectoryPathOld))
if (DirectoryPathOld.DirectoryExists())
{
if (Directory.Exists(DirectoryPath))
Directory.Delete(DirectoryPath, true);
Directory.Move(DirectoryPathOld, DirectoryPath);
DirectoryPath.DeleteDirectory();
DirectoryPathOld.MoveDirectory(DirectoryPath, true, form);
}
if (!Directory.Exists(DirectoryPath))
_ = Directory.CreateDirectory(DirectoryPath);
if (!AppInfoVersionPath.Exists(form: form) || !Version.TryParse(AppInfoVersionPath.Read(), out Version version) || version < MinimumAppInfoVersion)
DirectoryPath.CreateDirectory();
if (!AppInfoVersionPath.FileExists(form: form) || !Version.TryParse(AppInfoVersionPath.ReadFile(), out Version version)
|| version < MinimumAppInfoVersion)
{
if (Directory.Exists(AppInfoPath))
Directory.Delete(AppInfoPath, true);
_ = Directory.CreateDirectory(AppInfoPath);
AppInfoVersionPath.Write(Program.Version);
AppInfoPath.DeleteDirectory();
AppInfoPath.CreateDirectory();
AppInfoVersionPath.WriteFile(Program.Version);
}
if (!Directory.Exists(CooldownPath))
_ = Directory.CreateDirectory(CooldownPath);
if (OldProgramChoicesPath.Exists(form: form))
OldProgramChoicesPath.Delete();
CooldownPath.CreateDirectory();
if (OldProgramChoicesPath.FileExists(form: form))
OldProgramChoicesPath.DeleteFile();
});
internal static bool CheckCooldown(string identifier, int cooldown)
@ -63,14 +59,14 @@ internal static class ProgramData
private static DateTime? GetCooldown(string identifier)
{
if (!Directory.Exists(CooldownPath))
if (!CooldownPath.DirectoryExists())
return null;
string cooldownFile = CooldownPath + @$"\{identifier}.txt";
if (!cooldownFile.Exists())
if (!cooldownFile.FileExists())
return null;
try
{
if (DateTime.TryParse(cooldownFile.Read(), out DateTime cooldown))
if (DateTime.TryParse(cooldownFile.ReadFile(), out DateTime cooldown))
return cooldown;
}
catch
@ -82,12 +78,11 @@ internal static class ProgramData
private static void SetCooldown(string identifier, DateTime time)
{
if (!Directory.Exists(CooldownPath))
_ = Directory.CreateDirectory(CooldownPath);
CooldownPath.CreateDirectory();
string cooldownFile = CooldownPath + @$"\{identifier}.txt";
try
{
cooldownFile.Write(time.ToString(CultureInfo.InvariantCulture));
cooldownFile.WriteFile(time.ToString(CultureInfo.InvariantCulture));
}
catch
{
@ -97,10 +92,10 @@ internal static class ProgramData
internal static IEnumerable<(Platform platform, string id)> ReadProgramChoices()
{
if (ProgramChoicesPath.Exists())
if (ProgramChoicesPath.FileExists())
try
{
if (JsonConvert.DeserializeObject(ProgramChoicesPath.Read(), typeof(List<(Platform platform, string id)>)) is
if (JsonConvert.DeserializeObject(ProgramChoicesPath.ReadFile(), typeof(List<(Platform platform, string id)>)) is
List<(Platform platform, string id)> choices)
return choices;
}
@ -116,9 +111,9 @@ internal static class ProgramData
try
{
if (choices is null || !choices.Any())
ProgramChoicesPath.Delete();
ProgramChoicesPath.DeleteFile();
else
ProgramChoicesPath.Write(JsonConvert.SerializeObject(choices));
ProgramChoicesPath.WriteFile(JsonConvert.SerializeObject(choices));
}
catch
{
@ -128,10 +123,10 @@ internal static class ProgramData
internal static IEnumerable<(Platform platform, string gameId, string dlcId)> ReadDlcChoices()
{
if (DlcChoicesPath.Exists())
if (DlcChoicesPath.FileExists())
try
{
if (JsonConvert.DeserializeObject(DlcChoicesPath.Read(), typeof(IEnumerable<(Platform platform, string gameId, string dlcId)>)) is
if (JsonConvert.DeserializeObject(DlcChoicesPath.ReadFile(), typeof(IEnumerable<(Platform platform, string gameId, string dlcId)>)) is
IEnumerable<(Platform platform, string gameId, string dlcId)> choices)
return choices;
}
@ -147,9 +142,9 @@ internal static class ProgramData
try
{
if (choices is null || !choices.Any())
DlcChoicesPath.Delete();
DlcChoicesPath.DeleteFile();
else
DlcChoicesPath.Write(JsonConvert.SerializeObject(choices));
DlcChoicesPath.WriteFile(JsonConvert.SerializeObject(choices));
}
catch
{
@ -159,10 +154,10 @@ internal static class ProgramData
internal static IEnumerable<(Platform platform, string id, string proxy, bool enabled)> ReadKoaloaderChoices()
{
if (KoaloaderProxyChoicesPath.Exists())
if (KoaloaderProxyChoicesPath.FileExists())
try
{
if (JsonConvert.DeserializeObject(KoaloaderProxyChoicesPath.Read(),
if (JsonConvert.DeserializeObject(KoaloaderProxyChoicesPath.ReadFile(),
typeof(IEnumerable<(Platform platform, string id, string proxy, bool enabled)>)) is
IEnumerable<(Platform platform, string id, string proxy, bool enabled)> choices)
return choices;
@ -179,9 +174,9 @@ internal static class ProgramData
try
{
if (choices is null || !choices.Any())
KoaloaderProxyChoicesPath.Delete();
KoaloaderProxyChoicesPath.DeleteFile();
else
KoaloaderProxyChoicesPath.Write(JsonConvert.SerializeObject(choices));
KoaloaderProxyChoicesPath.WriteFile(JsonConvert.SerializeObject(choices));
}
catch
{

View file

@ -1,5 +1,8 @@
using System.Drawing;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CreamInstaller.Forms;
@ -8,7 +11,100 @@ namespace CreamInstaller.Utility;
internal static class SafeIO
{
internal static bool Exists(this string filePath, bool crucial = false, Form form = null)
internal static bool DirectoryExists(this string directoryPath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
{
bool exists = Directory.Exists(directoryPath);
if (exists || !crucial || directoryPath.IOWarn("Failed to find a crucial directory", form) is not DialogResult.OK)
return exists;
}
return false;
}
internal static void CreateDirectory(this string directoryPath, bool crucial = false, Form form = null)
{
if (directoryPath.DirectoryExists())
return;
while (!Program.Canceled)
try
{
Directory.CreateDirectory(directoryPath);
break;
}
catch
{
if (!crucial || directoryPath.IOWarn("Failed to create a crucial directory", form) is not DialogResult.OK)
break;
}
}
internal static void MoveDirectory(this string directoryPath, string newDirectoryPath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
Directory.Move(directoryPath, newDirectoryPath);
break;
}
catch
{
if (!crucial || directoryPath.IOWarn("Failed to move a crucial directory", form) is not DialogResult.OK)
break;
}
}
internal static void DeleteDirectory(this string directoryPath, bool crucial = false, Form form = null)
{
if (!directoryPath.DirectoryExists())
return;
while (!Program.Canceled)
try
{
Directory.Delete(directoryPath, true);
break;
}
catch
{
if (!crucial || directoryPath.IOWarn("Failed to delete a crucial directory", form) is not DialogResult.OK)
break;
}
}
internal static IEnumerable<string> EnumerateDirectory(this string directoryPath, string filePattern, bool subdirectories = false, bool crucial = false,
Form form = null)
{
while (!Program.Canceled)
try
{
return subdirectories
? Directory.EnumerateFiles(directoryPath, filePattern, new EnumerationOptions { RecurseSubdirectories = true })
: Directory.EnumerateFiles(directoryPath, filePattern);
}
catch
{
if (!crucial || directoryPath.IOWarn("Failed to enumerate a crucial directory's files", form) is not DialogResult.OK)
break;
}
return Enumerable.Empty<string>();
}
internal static IEnumerable<string> EnumerateSubdirectories(this string directoryPath, string directoryPattern, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
return Directory.EnumerateDirectories(directoryPath, directoryPattern);
}
catch
{
if (!crucial || directoryPath.IOWarn("Failed to enumerate a crucial directory's subdirectories", form) is not DialogResult.OK)
break;
}
return Enumerable.Empty<string>();
}
internal static bool FileExists(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
{
@ -19,7 +115,7 @@ internal static class SafeIO
return false;
}
internal static void Create(this string filePath, bool crucial = false, Form form = null)
internal static void CreateFile(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
@ -34,7 +130,7 @@ internal static class SafeIO
}
}
internal static void Move(this string filePath, string newFilePath, bool crucial = false, Form form = null)
internal static void MoveFile(this string filePath, string newFilePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
@ -44,14 +140,14 @@ internal static class SafeIO
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to move a crucial file", form) is not DialogResult.OK)
if (!crucial || !filePath.FileExists(true) || filePath.IOWarn("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)
internal static void DeleteFile(this string filePath, bool crucial = false, Form form = null)
{
if (!filePath.Exists(form: form))
if (!filePath.FileExists(form: form))
return;
while (!Program.Canceled)
try
@ -66,7 +162,7 @@ internal static class SafeIO
}
}
internal static string Read(this string filePath, bool crucial = false, Form form = null)
internal static string ReadFile(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
@ -75,13 +171,13 @@ internal static class SafeIO
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to read a crucial file", form) is not DialogResult.OK)
if (!crucial || !filePath.FileExists(true) || filePath.IOWarn("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)
internal static byte[] ReadFileBytes(this string filePath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
@ -90,13 +186,13 @@ internal static class SafeIO
}
catch
{
if (!crucial || !filePath.Exists(true) || filePath.IOWarn("Failed to read a crucial file", form) is not DialogResult.OK)
if (!crucial || !filePath.FileExists(true) || filePath.IOWarn("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)
internal static void WriteFile(this string filePath, string text, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
@ -111,6 +207,21 @@ internal static class SafeIO
}
}
internal static void ExtractZip(this string archivePath, string destinationPath, bool crucial = false, Form form = null)
{
while (!Program.Canceled)
try
{
ZipFile.ExtractToDirectory(archivePath, destinationPath);
break;
}
catch
{
if (!crucial || archivePath.IOWarn("Failed to extract a crucial zip file", form) is not DialogResult.OK)
break;
}
}
internal static DialogResult IOWarn(this string filePath, string message, Form form = null)
{
using DialogForm dialogForm = new(form ?? Form.ActiveForm);