From 74464f2b240fa9875b11441dd8ef4fa65c9ec90d Mon Sep 17 00:00:00 2001 From: Jordan Date: Fri, 28 Oct 2022 04:04:06 -0400 Subject: [PATCH] Moved Everything Onto New File Moving framework. Fixed mod Page sort Bug Fixed Mod Page Info Bug Fixed Master Server Turn off Fixed Win 11 Install Compat Fixed Launch Issues Fixed .Net 3 Compat. --- .../.vs/VTOL/DesignTimeBuild/.dtbcache.v2 | Bin 463504 -> 463504 bytes VTOL_2.0.0/.vs/VTOL/v17/.futdcache.v1 | Bin 1499 -> 1499 bytes VTOL_2.0.0/.vs/VTOL/v17/fileList.bin | Bin 63028 -> 63576 bytes VTOL_2.0.0/MainWindow.xaml.cs | 148 +++++- VTOL_2.0.0/Pages/Page_Home.xaml.cs | 290 ++++++++--- VTOL_2.0.0/Pages/Page_Mods.xaml.cs | 463 +++++++++++++----- VTOL_2.0.0/Pages/Page_Server.xaml.cs | 156 +++++- VTOL_2.0.0/Pages/Page_Skins.xaml.cs | 143 +++++- VTOL_2.0.0/Pages/Page_Thunderstore.xaml.cs | 324 +++++++++++- VTOL_2.0.0/Pages/Page_Tools.xaml.cs | 175 ++++++- VTOL_2.0.0/VTOL.csproj | 32 ++ 11 files changed, 1499 insertions(+), 232 deletions(-) diff --git a/VTOL_2.0.0/.vs/VTOL/DesignTimeBuild/.dtbcache.v2 b/VTOL_2.0.0/.vs/VTOL/DesignTimeBuild/.dtbcache.v2 index 1d5bff72450f01a99a29ac4f7a4b5b8b27ae92a6..70958bcb87ca58a57f62043c47b312205e455542 100644 GIT binary patch delta 915 zcmZ|MOHUI~6ae5m(|36miWDeXe4(hr(3U=k#GpukAP_*Jk%Y`l@6<8vOr4pM5EoQT z)Tj%jCO0l!u-1i!rLtn<&R^i#%}M$PJf#Ii!z5oO=blIIV{0e8wG-wObLnocfOLH?A*GBx6 zaEySAGiq)Q3DNo70qswR4D_p~dgVeqz{6$b7I*ZPs^XLM)i2BhbbQukxE9fffK-Z9f z29bz{P!z>b97WIxb2m;L%wf|os(xz-$R=>Wg9DsT!L`km!94~ZaDx|o;D-iitYFXt z&Cmj^5Ez3t2w<-LFc&z=1#dzJoT@-4gdoxd-OvMJ2x4tF^ulR4Q^BA8Bi7X6lqHbFx{ z*C9b;BoTz9^Kq1m7a0`>=dnffH9TjMC+vh1brD-y#ki-fV03&)!I1ThO>9~xMd#@Z z7kBohz*YM0%}^tRY;zSm1Es)mvYn1?w>qttxhr&cu#@iZ^w9U)z0OgbC4N3nvP6vL zcP=!*ivYd*dXjx#+k&o|tvOj)mMgeO)(zip^uCbh)4Y(&WknukAHc=Bff^H6v7s{m z8vbueh!w0Vu`s`=RZ|G@c|Hw|HRPsb-NMjv!hk@U3O#-F@26M_+Q%o1NkbtjHu*c) zP9a`kV$O%i=^}`p?t5p1KK$G`34N!_{PYU*vhY|ot0{rC5rFfd`%E5OuXXGjgvj`Y zjTf{{?9IT$?hFEi>uDHvxz4j+p~w)NFA|%L>-uG+tn0Ui&Bql;(ZZL0r}{e-^#uNo zvB~;p+~Is^sV^U!zJNYDhz>*BP1#U4CWv91M7L5RlOW)HICrWB$n((K-By6)v_cF` z;TLsn$z7*qvo5l!vLV^Ey8CdU>kJl*d7X|9l9wQQ%OZw%Bd$giU4I^i>jmIFcI9hS zg7z704NtJm+ZJD{mQ!i=3*>s zxlz24rLm=fsa`;0dTP9HeoC=kMPhD_o{2@GsezHH<>dK$6c__Q%B3L6O^l6A%q;c7 zLi~O749qMnO$?JvCMWI+RRJ4jYG7etVF)oSBtJg~YMMb>s^X#5Yx;J zOw27I%02RPQz3RICtH{$8JJ9N+^abG&Ta!qh*AqzXJKk$Vql1=X!6d3 p2Fg%l%nglz?gx7pyOpiG3?ZJe1cr;b5nj(s&faafS$j99H2{D;d58c2 delta 240 zcmccdfqBatW?lv`Fm&i;WMGJ!+{op-F)BGBGt=2BCZH%ky(lqP!7VcyUl3E0 zSd^Yxf~*jzz%4=#B0g!a+~h5i^^@)RxHh{bdPOh>OrE&YK+@2_#3J6@(A31lST8KZ z-$&0Z(bU{D$slR6;2xF9EB6?xfK^yp8Uxkn1tg}Y#)stR=M?KzB if (Directory.Exists(Current_Install_Folder + @"TempCopyFolder")) { - Directory.Delete(Current_Install_Folder + @"TempCopyFolder", true); + TryDeleteDirectory(Current_Install_Folder + @"TempCopyFolder", true); } @@ -2675,27 +2685,27 @@ await Task.Run(async () => { if (Directory.Exists(Current_Install_Folder + @"ns_startup_args.txt")) { - File.Copy(Current_Install_Folder + @"ns_startup_args.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", true); + TryCopyFile(Current_Install_Folder + @"ns_startup_args.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", true); } if (Directory.Exists(Current_Install_Folder + @"ns_startup_args_dedi.txt")) { - System.IO.File.Copy(Current_Install_Folder + @"ns_startup_args_dedi.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", true); + TryCopyFile(Current_Install_Folder + @"ns_startup_args_dedi.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", true); } } else { - Directory.CreateDirectory(Current_Install_Folder + @"TempCopyFolder"); + TryCreateDirectory(Current_Install_Folder + @"TempCopyFolder"); if (Directory.Exists(Current_Install_Folder + @"ns_startup_args.txt")) { - File.Copy(Current_Install_Folder + @"ns_startup_args.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", true); + TryCopyFile(Current_Install_Folder + @"ns_startup_args.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", true); } if (Directory.Exists(Current_Install_Folder + @"ns_startup_args_dedi.txt")) { - File.Copy(Current_Install_Folder + @"ns_startup_args_dedi.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", true); + TryCopyFile(Current_Install_Folder + @"ns_startup_args_dedi.txt", Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", true); } } - Directory.CreateDirectory(DocumentsFolder + @"\VTOL_DATA\Releases\"); + TryCreateDirectory(DocumentsFolder + @"\VTOL_DATA\Releases\"); webClient.DownloadFileAsync(new Uri(current_Northstar_version_Url), DocumentsFolder + @"\VTOL_DATA\Releases\Northstar_Release.zip"); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback_Progress_Window); while (unpack_flg == false) @@ -2710,7 +2720,7 @@ await Task.Run(async () => else { - Directory.CreateDirectory(DocumentsFolder + @"\VTOL_DATA\Releases\"); + TryCreateDirectory(DocumentsFolder + @"\VTOL_DATA\Releases\"); webClient.DownloadFileAsync(new Uri(current_Northstar_version_Url), DocumentsFolder + @"\VTOL_DATA\Releases\Northstar_Release.zip"); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback_Progress_Window); while (unpack_flg == false) @@ -2729,7 +2739,7 @@ await Task.Run(async () => else { - Directory.CreateDirectory(DocumentsFolder + @"\VTOL_DATA\Releases\"); + TryCreateDirectory(DocumentsFolder + @"\VTOL_DATA\Releases\"); webClient.DownloadFileAsync(new Uri(current_Northstar_version_Url), DocumentsFolder + @"\VTOL_DATA\Releases\Northstar_Release.zip"); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback_Progress_Window); @@ -2772,38 +2782,56 @@ await Task.Run(async () => } }); } - public IEnumerable GetFile(string directory, string Search) + + public string GetFile(string directory, string Search, + int maxRetries = 10, + int millisecondsDelay = 30) { - List files = new List(); + if (directory == null) + throw new ArgumentNullException(directory); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); - try + for (int i = 0; i < maxRetries; ++i) { - if (Directory.Exists(directory)) + try { + List files = new List(); + files.AddRange(Directory.GetFiles(directory, Search, SearchOption.AllDirectories)); if (files.Count >= 1) { - return files; + return files.FirstOrDefault(); } else { - return null; + return ""; } - } - - } - catch (Exception ex) - { - Log.Error(ex, $"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}"); - Main.logger2.Open(); - Main.logger2.Log($"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}" + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source +Environment.NewLine + ex.InnerException + Environment.NewLine + ex.TargetSite + Environment.NewLine + "From VERSION - " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + Environment.NewLine + System.Reflection.MethodBase.GetCurrentMethod().Name); -Main.logger2.Close(); + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + catch (Exception ex) + { + Log.Error(ex, $"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}"); + Main.logger2.Open(); + Main.logger2.Log($"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}" + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source + Environment.NewLine + ex.InnerException + Environment.NewLine + ex.TargetSite + Environment.NewLine + "From VERSION - " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + Environment.NewLine + System.Reflection.MethodBase.GetCurrentMethod().Name); + Main.logger2.Close(); + } } - return null; + return null; } + private void DownloadProgressCallback_Progress_Window(object sender, DownloadProgressChangedEventArgs e) { @@ -2885,63 +2913,213 @@ private void Completed(object sender, AsyncCompletedEventArgs e) } } + public bool TryDeleteDirectory( +string directoryPath, bool overwrite = true, +int maxRetries = 10, +int millisecondsDelay = 300) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCreateDirectory( + string directoryPath, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + Directory.CreateDirectory(directoryPath); + + if (Directory.Exists(directoryPath)) + { + + return true; + } + + + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryMoveFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Move(Origin, Destination, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCopyFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 300) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Copy(Origin, Destination, true); + } + Thread.Sleep(millisecondsDelay); + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } private void Unpack_To_Location(string Target_Zip, string Destination_Zip) { unpack_flg = false; if (Directory.Exists(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Client\Locked_Folder")) { - Directory.Delete(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Client\Locked_Folder", true); + TryDeleteDirectory(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Client\Locked_Folder", true); } if (Directory.Exists(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Custom\Locked_Folder")) { - Directory.Delete(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Custom\Locked_Folder", true); + TryDeleteDirectory(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Custom\Locked_Folder", true); } if (Directory.Exists(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\Locked_Folder")) { - Directory.Delete(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\Locked_Folder", true); + TryDeleteDirectory(Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\Locked_Folder", true); } - string nrml = GetFile(Current_Install_Folder , @"ns_startup_args.txt").First(); - string cfg = GetFile(Current_Install_Folder , @"autoexec_ns_server.cfg").First(); - string dedi = GetFile(Current_Install_Folder , @"ns_startup_args_dedi.txt").First(); + string nrml = GetFile(Current_Install_Folder , @"ns_startup_args.txt"); + string cfg = GetFile(Current_Install_Folder, @"autoexec_ns_server.cfg"); + string dedi = GetFile(Current_Install_Folder , @"ns_startup_args_dedi.txt"); if (do_not_overwrite_Ns_file == true) { if (!Directory.Exists(Current_Install_Folder + @"TempCopyFolder")) { - Directory.CreateDirectory(Current_Install_Folder + @"TempCopyFolder"); + TryCreateDirectory(Current_Install_Folder + @"TempCopyFolder"); } - if (File.Exists(nrml)) + if (IsValidPath(nrml)) { - File.Copy(nrml, Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", true); + if (File.Exists(nrml)) + { + TryCopyFile(nrml, Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", true); + } } - if (File.Exists(cfg)) + if (IsValidPath(cfg)) { - - File.Copy(cfg, Current_Install_Folder + @"TempCopyFolder\autoexec_ns_server.cfg", true); - + if (File.Exists(cfg)) + { + TryCopyFile(cfg, Current_Install_Folder + @"TempCopyFolder\autoexec_ns_server.cfg", true); + } } - if (File.Exists(dedi)) + if (IsValidPath(dedi)) { + if (File.Exists(dedi)) + { - File.Copy(dedi, Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", true); - + TryCopyFile(dedi, Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", true); + } } @@ -2968,17 +3146,17 @@ private void Unpack_To_Location(string Target_Zip, string Destination_Zip) if (File.Exists(Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt")) { - File.Copy(Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", Current_Install_Folder + @"ns_startup_args.txt", true); + TryCopyFile(Current_Install_Folder + @"TempCopyFolder\ns_startup_args.txt", Current_Install_Folder + @"ns_startup_args.txt", true); } if (File.Exists(Current_Install_Folder + @"TempCopyFolder\autoexec_ns_server.cfg")) { - File.Copy(Current_Install_Folder + @"TempCopyFolder\autoexec_ns_server.cfg", Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\mod\cfg\autoexec_ns_server.cfg", true); + TryCopyFile(Current_Install_Folder + @"TempCopyFolder\autoexec_ns_server.cfg", Current_Install_Folder + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\mod\cfg\autoexec_ns_server.cfg", true); } if (File.Exists(Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt")) { - File.Copy(Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", Current_Install_Folder + @"ns_startup_args_dedi.txt", true); + TryCopyFile(Current_Install_Folder + @"TempCopyFolder\ns_startup_args_dedi.txt", Current_Install_Folder + @"ns_startup_args_dedi.txt", true); } } @@ -2988,7 +3166,7 @@ private void Unpack_To_Location(string Target_Zip, string Destination_Zip) if (Directory.Exists(Current_Install_Folder + @"TempCopyFolder")) { - Directory.Delete(Current_Install_Folder + @"TempCopyFolder", true); + TryDeleteDirectory(Current_Install_Folder + @"TempCopyFolder", true); } DispatchIfNecessary(() => { diff --git a/VTOL_2.0.0/Pages/Page_Mods.xaml.cs b/VTOL_2.0.0/Pages/Page_Mods.xaml.cs index 48a50259..64a878dc 100644 --- a/VTOL_2.0.0/Pages/Page_Mods.xaml.cs +++ b/VTOL_2.0.0/Pages/Page_Mods.xaml.cs @@ -262,8 +262,7 @@ async void Call_Mods_From_Folder() { if (Page_Home.IsDirectoryEmpty(new DirectoryInfo(dirInfo.FullName)) == true) { - Directory.Delete(dirInfo.FullName, true); - + TryDeleteDirectoryAsync(dirInfo.FullName, true); } @@ -277,13 +276,12 @@ async void Call_Mods_From_Folder() - Directory.Delete(dirInfo + @"\Locked_Folder"); - + TryDeleteDirectoryAsync(dirInfo + @"\Locked_Folder"); } int Flag_mod = 0; string ToolTip_Dynamic = VTOL.Resources.Languages.Language.Page_Mods_Call_Mods_From_Folder_ThereIsAnIssueDetectedWithYourMod; - if (!File.Exists(dirInfo.FullName + @"\Locked_Folder" + @"\mod.json")) + if (!File.Exists(dirInfo.FullName + @"\Locked_Folder" + @"\mod.json") ) { ToolTip_Dynamic = VTOL.Resources.Languages.Language.Page_Mods_Call_Mods_From_Folder_PleaseOpenYourFolderAt + dirInfo.Parent + VTOL.Resources.Languages.Language.Page_Mods_Call_Mods_From_Folder_AndManuallyRepairTheMod + dirInfo.Name; Flag_mod = 100; @@ -315,35 +313,204 @@ async void Call_Mods_From_Folder() ApplyDataBinding(); }); } - else - { - - } + } - else - { + + } + + + } + } + catch (Exception ex) + { + Log.Error(ex, $"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}"); + Main.logger2.Open(); + Main.logger2.Log($"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}" + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source +Environment.NewLine + ex.InnerException + Environment.NewLine + ex.TargetSite + Environment.NewLine + "From VERSION - " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + Environment.NewLine + System.Reflection.MethodBase.GetCurrentMethod().Name); +Main.logger2.Close(); + } + } + + public async Task TryDeleteDirectoryAsync( +string directoryPath, bool overwrite = true, +int maxRetries = 10, +int millisecondsDelay = 30) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); - } + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath, overwrite); } - else + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryDeleteDirectory( +string directoryPath, bool overwrite = true, +int maxRetries = 10, +int millisecondsDelay = 300) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) { + Directory.Delete(directoryPath, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCreateDirectory( + string directoryPath, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + + Directory.CreateDirectory(directoryPath); + if (Directory.Exists(directoryPath)) + { + return true; } + + + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); } } - catch (Exception ex) + + return false; + } + public bool TryMoveFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) { - Log.Error(ex, $"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}"); - Main.logger2.Open(); - Main.logger2.Log($"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}" + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source +Environment.NewLine + ex.InnerException + Environment.NewLine + ex.TargetSite + Environment.NewLine + "From VERSION - " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + Environment.NewLine + System.Reflection.MethodBase.GetCurrentMethod().Name); -Main.logger2.Close(); + try + { + if (File.Exists(Origin)) + { + File.Move(Origin, Destination, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } } + return false; + } + public bool TryCopyFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 300) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Copy(Origin, Destination, true); + } + Thread.Sleep(millisecondsDelay); + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; } public bool Template_traverse(System.IO.DirectoryInfo root, String Search) { @@ -395,7 +562,7 @@ public bool Template_traverse(System.IO.DirectoryInfo root, String Search) if (IsDirectoryEmpty(Dir)) { - Directory.Delete(outt, true); + TryDeleteDirectory(outt, true); } } else @@ -404,7 +571,7 @@ public bool Template_traverse(System.IO.DirectoryInfo root, String Search) if (IsDirectoryEmpty(Dir)) { - Directory.Delete(outt, true); + TryDeleteDirectory(outt, true); } } @@ -466,6 +633,7 @@ private void ApplyDataBinding() var sorted = Keep_List_State(false, Reverse_); Mod_List_Box.ItemsSource = sorted; + _Completed_Mod_call = true; @@ -481,38 +649,7 @@ private void ApplyDataBinding() } } - private static void MoveFiles(string sourceDir, string targetDir) - { - TlsPaperTrailLogger logger2 = new TlsPaperTrailLogger("logs5.papertrailapp.com", 38137); - - try - { - - System.IO.DirectoryInfo Targ = new DirectoryInfo(targetDir); - System.IO.DirectoryInfo src = new DirectoryInfo(sourceDir); - - if (!Page_Home.IsDirectoryEmpty(Targ)) - { - if (!Page_Home.IsDirectoryEmpty(src)) - { - IEnumerable files = Directory.GetFiles(sourceDir).Select(f => new FileInfo(f)); - foreach (var file in files) - { - File.Move(file.FullName, Path.Combine(targetDir, file.Name), true); - } - - } - - } - } - catch (Exception ex) - { - logger2.Open(); - logger2.Log($"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}" + ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source +Environment.NewLine + ex.InnerException + Environment.NewLine + ex.TargetSite + Environment.NewLine + "From VERSION - " + Assembly.GetExecutingAssembly().GetName().Version.ToString() + Environment.NewLine); -logger2.Close();Log.Error(ex, $"A crash happened at {DateTime.Now.ToString("yyyy - MM - dd HH - mm - ss.ff", CultureInfo.InvariantCulture)}{Environment.NewLine}"); - - } - } + public void Move_Mods(string val, bool Enable_Disable) { @@ -523,50 +660,69 @@ public void Move_Mods(string val, bool Enable_Disable) { - if (Enable_Disable == false) - { + if (val != null) - { - System.IO.DirectoryInfo rootDirs = new DirectoryInfo(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val); + { + DirectoryInfo rootDirs = new DirectoryInfo(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val); - if (!Page_Home.IsDirectoryEmpty(rootDirs)) + if (!Page_Home.IsDirectoryEmpty(rootDirs)) { - - if (Directory.Exists(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder")) + if(Enable_Disable == true) + { + if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder" + @"\mod.json")) { + TryMoveFile(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder" + @"\mod.json", User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\mod.json", true); - if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\mod.json")) + DirectoryInfo Locked = new DirectoryInfo(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder"); + if (Page_Home.IsDirectoryEmpty(Locked)) { - File.Move(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\mod.json", User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder" + @"\mod.json", true); + TryDeleteDirectory(Locked.FullName); } - else - { - MoveFiles(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val, User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder"); - - } + } else { - Directory.CreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder"); - if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\mod.json")) - { - File.Move(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\mod.json", User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder" + @"\mod.json", true); + Snackbar.Title = "ERROR"; + Snackbar.Appearance = Wpf.Ui.Common.ControlAppearance.Caution; + Snackbar.Message = "File" + val + " Could not be Edited."; + Snackbar.Show(); + } + + + - } - else - { - MoveFiles(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val, User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder"); + } + else + { + if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\mod.json")) + { + TryCreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder"); + + TryMoveFile(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\mod.json", User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder" + @"\mod.json", true); + - } } + else + { + Snackbar.Title = "ERROR"; + Snackbar.Appearance = Wpf.Ui.Common.ControlAppearance.Caution; + Snackbar.Message = "File" + val + " Could not be Edited."; + Snackbar.Show(); + } + + } + + + } + @@ -585,8 +741,8 @@ public void Move_Mods(string val, bool Enable_Disable) { - System.IO.DirectoryInfo rootDirs = new DirectoryInfo(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val); - System.IO.DirectoryInfo Locked = new DirectoryInfo(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder"); + DirectoryInfo rootDirs = new DirectoryInfo(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val); + DirectoryInfo Locked = new DirectoryInfo(User_Settings_Vars.NorthstarInstallLocation + @"R2Northstar\mods\" + val + @"\Locked_Folder"); if (!Page_Home.IsDirectoryEmpty(rootDirs)) { @@ -595,11 +751,11 @@ public void Move_Mods(string val, bool Enable_Disable) if (Page_Home.IsDirectoryEmpty(Locked)) { - Directory.Delete(Locked.FullName); + TryDeleteDirectory(Locked.FullName); } - MoveFiles(Locked.FullName, rootDirs.FullName); - Directory.Delete(Locked.FullName); + TryMoveFile(Locked.FullName, rootDirs.FullName); + TryDeleteDirectory(Locked.FullName); } @@ -636,14 +792,12 @@ private void Mod_Btn_Click(object sender, RoutedEventArgs e) if (Name_ != null) { - - if (Button_.Tag == "Enable") + if (Button_.Tag.ToString() == "Enable") { Move_Mods(Name_, true); - Call_Mods_From_Folder(); - + } @@ -651,7 +805,7 @@ private void Mod_Btn_Click(object sender, RoutedEventArgs e) { Move_Mods(Name_, false); - Call_Mods_From_Folder(); + } } @@ -659,8 +813,12 @@ private void Mod_Btn_Click(object sender, RoutedEventArgs e) + Call_Mods_From_Folder(); + DispatchIfNecessary(() => { + ApplyDataBinding(); + }); } catch (Exception ex) @@ -858,100 +1016,115 @@ IEnumerable Keep_List_State(bool Searching, bool reverse = false) try { - - if (reverse == false) + if (Filter.SelectedItem != null) { - - if (Searching == true) + if (_Completed_Mod_call == true) { + if (Reverse_ == false) + { + if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Name")) + { - return Final_List.Where(ob => ob.Mod_Name_.ToLower().Contains(Search_Bar_Suggest_Mods.Text.ToLower())); + Search_Bar_Suggest_Mods.Text = "~Search"; - } - - - else - { - if (Filter.SelectedItem != null) - { - if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Name")) - { return Final_List.OrderBy(ob => ob.Mod_Name_).ToArray(); + + + + } else if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Date")) { - return Final_List.OrderByDescending(ob => ob.Mod_Date_).ToArray(); + + Search_Bar_Suggest_Mods.Text = "~Search"; + + + + return Final_List.OrderByDescending(ob => Convert.ToDateTime(ob.Mod_Date_)).ToArray(); + + } else if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Status")) { + Search_Bar_Suggest_Mods.Text = "~Search"; + + return Final_List.OrderBy(ob => ob.Is_Active_).ToArray(); + } else { + + Search_Bar_Suggest_Mods.Text = "~Search"; + + return Final_List.OrderBy(ob => ob.Mod_Name_).ToArray(); + + } } else { - return Final_List.OrderBy(ob => ob.Mod_Name_).ToArray(); - } - - } - } - else - { + if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Name")) + { - if (Searching == true) - { + Search_Bar_Suggest_Mods.Text = "~Search"; - return Final_List.Where(ob => ob.Mod_Name_.ToLower().Contains(Search_Bar_Suggest_Mods.Text.ToLower())).Reverse(); + return Final_List.OrderByDescending(ob => ob.Mod_Name_).ToArray(); - } - else - { - if (Filter.SelectedItem != null) - { - if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Name")) - { - return Final_List.OrderByDescending(ob => ob.Mod_Name_); } else if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Date")) { - return Final_List.OrderBy(ob => ob.Mod_Date_); + + + Search_Bar_Suggest_Mods.Text = "~Search"; + + + + return Final_List.OrderBy(ob => Convert.ToDateTime(ob.Mod_Date_)).ToArray(); + + } else if (Filter.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "").Trim().Contains("Status")) { - return Final_List.OrderByDescending(ob => ob.Is_Active_); + Search_Bar_Suggest_Mods.Text = "~Search"; + + + return Final_List.OrderByDescending(ob => ob.Is_Active_).ToArray(); } else { - return Final_List.OrderByDescending(ob => ob.Mod_Name_); + + Search_Bar_Suggest_Mods.Text = "~Search"; + + + return Final_List.OrderByDescending(ob => ob.Mod_Name_).ToArray(); + + } } } - - } - return Final_List.OrderByDescending(ob => ob.Mod_Name_); + return Final_List.OrderBy(ob => ob.Mod_Name_); } catch (Exception ex) @@ -1254,12 +1427,12 @@ void Open_Mod_Info(string Mod_name) if (mod_Json != null && File.Exists(mod_Json)) { var myJsonString = File.ReadAllText(mod_Json); - var myJObject = JObject.Parse(myJsonString); - string name = "Name: " + myJObject.SelectToken("Name").Value(); - string version = "Version: " + myJObject.SelectToken("Version").Value(); - string Description = "Description: " + myJObject.SelectToken("Description").Value(); + dynamic myJObject = JObject.Parse(myJsonString); + string name = "Name: " + myJObject.Name; + string version = "Version: " + myJObject.Version; + string Description = "Description: " + myJObject.Description; string Content = Description + Environment.NewLine + version; - DialogF.ButtonLeftName = "OK"; + DialogF.ButtonLeftName = "Ok"; DialogF.ButtonLeftAppearance = Wpf.Ui.Common.ControlAppearance.Success; DialogF.ButtonRightName =VTOL.Resources.Languages.Language.Page_Mods_Open_Mod_Info_OpenFolder; @@ -1473,7 +1646,7 @@ void Delete_Action(object sender, RoutedEventArgs e) } else { - Directory.Delete(temp_Dir, true); + TryDeleteDirectory(temp_Dir, true); Dialog.Hide(); @@ -1653,9 +1826,31 @@ private void DialogF_ButtonRightClick(object sender, RoutedEventArgs e) } private void DialogF_ButtonLeftClick(object sender, RoutedEventArgs e) - { + { + DialogF.Hide(); + //DispatchIfNecessary(() => + //{ + + // Wpf.Ui.Controls.Dialog _Dialog; + // _Dialog = sender as Wpf.Ui.Controls.Dialog; + // Page_Thunderstore p = new Page_Thunderstore(); + // // p.InitializeComponent(); + // p.Thunderstore_List.ItemsSource = null; + + + + + + // p.Call_Ts_Mods(true, Search_: true, SearchQuery: _Dialog.Title); + // p.Search_Bar_Suggest_Mods.Text = _Dialog.Title.ToString(); + + + + // p.Thunderstore_List.Refresh(); + // Main.RootNavigation.Navigate(typeof(VTOL.Pages.Page_Thunderstore)); + //}); } private void Dialog_ButtonRightClick(object sender, RoutedEventArgs e) @@ -1725,7 +1920,7 @@ void Install_Mod_Zip(string Target_Zip, string Destination) if (!Directory.Exists(Destination)) { - Directory.CreateDirectory(Destination); + TryCreateDirectory(Destination); } if (Directory.Exists(Destination)) { @@ -1758,7 +1953,7 @@ void Install_Mod_Zip(string Target_Zip, string Destination) - Directory.CreateDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); + TryCreateDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); if (Directory.Exists(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder")) { CopyFilesRecursively(firstFolder, Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); @@ -1768,7 +1963,7 @@ void Install_Mod_Zip(string Target_Zip, string Destination) Clear_Folder(Destination); CopyFilesRecursively(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", Destination); - Directory.Delete(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", true); + TryDeleteDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", true); } Console.WriteLine("Unpacked - " + Destination); diff --git a/VTOL_2.0.0/Pages/Page_Server.xaml.cs b/VTOL_2.0.0/Pages/Page_Server.xaml.cs index c455a06c..eb538b54 100644 --- a/VTOL_2.0.0/Pages/Page_Server.xaml.cs +++ b/VTOL_2.0.0/Pages/Page_Server.xaml.cs @@ -11,6 +11,7 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -1806,7 +1807,7 @@ private void Import_Server_Config_Click(object sender, RoutedEventArgs e) } else { - Directory.CreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"VTOL_Dedicated_Workspace"); + TryCreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"VTOL_Dedicated_Workspace"); string extractPath = Path.GetFullPath(User_Settings_Vars.NorthstarInstallLocation + @"VTOL_Dedicated_Workspace"); @@ -1928,7 +1929,11 @@ public void ZIP_LIST(List filesToZip, string sZipFileName, bool deleteEx string strRootDirectory = Path.GetDirectoryName(filesToZip[0]); // Set up a temporary directory to save the files to (that we will eventually zip up) - DirectoryInfo dirTemp = Directory.CreateDirectory(strRootDirectory + "/" + DateTime.Now.ToString("yyyyMMddhhmmss")); + + string x = strRootDirectory + "/" + DateTime.Now.ToString("yyyyMMddhhmmss"); + TryCreateDirectory(x); + DirectoryInfo dirTemp = new DirectoryInfo(x); + // Copy all files to the temporary directory foreach (string strFilePath in filesToZip) @@ -1997,7 +2002,7 @@ private void Export_Server_Config_Click(object sender, RoutedEventArgs e) } else { - Directory.CreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"VTOL_Dedicated_Workspace"); + TryCreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"VTOL_Dedicated_Workspace"); if (File.Exists(Ns_dedi_File) && File.Exists(Convar_File)) { @@ -2145,6 +2150,151 @@ private string Get_And_Set_Filepaths(string rootDir, string Filename) return "Exited with No Due to Missing Or Inaccurate Path"; + } + public bool TryDeleteDirectory( +string directoryPath, bool overwrite = true, +int maxRetries = 10, +int millisecondsDelay = 300) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCreateDirectory( + string directoryPath, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + + Directory.CreateDirectory(directoryPath); + + if (Directory.Exists(directoryPath)) + { + + return true; + } + + + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryMoveFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Move(Origin, Destination, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCopyFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 300) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Copy(Origin, Destination, true); + } + Thread.Sleep(millisecondsDelay); + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; } private void Launch_Northstar_Advanced_Click(object sender, RoutedEventArgs e) { diff --git a/VTOL_2.0.0/Pages/Page_Skins.xaml.cs b/VTOL_2.0.0/Pages/Page_Skins.xaml.cs index b3dd7a0e..d637cbba 100644 --- a/VTOL_2.0.0/Pages/Page_Skins.xaml.cs +++ b/VTOL_2.0.0/Pages/Page_Skins.xaml.cs @@ -145,7 +145,7 @@ await Task.Run(() => else { - Directory.CreateDirectory(User_Settings_Vars.NorthstarInstallLocation+ @"Skins_Unpack_Mod_MNGR"); + TryCreateDirectory(User_Settings_Vars.NorthstarInstallLocation+ @"Skins_Unpack_Mod_MNGR"); current_skin_folder = User_Settings_Vars.NorthstarInstallLocation+ @"Skins_Unpack_Mod_MNGR"; ZipFile zipFile = new ZipFile(Zip_Path); @@ -262,7 +262,7 @@ await Task.Run(() => { dir_.Delete(true); } - Directory.Delete(current_skin_folder); + TryDeleteDirectory(current_skin_folder); Task.Delay(500).Wait(); @@ -617,7 +617,146 @@ private void Making_Your_Own_Click(object sender, RoutedEventArgs e) OPEN_WEBPAGE("https://titanfall-skin-group.gitbook.io/titanfall-2-skin-creation/"); } + public bool TryDeleteDirectory( +string directoryPath, bool overwrite = true, +int maxRetries = 10, +int millisecondsDelay = 30) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCreateDirectory( + string directoryPath, + int maxRetries = 10, + int millisecondsDelay = 30) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.CreateDirectory(directoryPath); + } + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryMoveFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 30) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(Origin)) + { + File.Move(Origin, Destination, true); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCopyFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 30) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(Origin)) + { + File.Copy(Origin, Destination, true); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } private void FAQ_Click(object sender, RoutedEventArgs e) { OPEN_WEBPAGE("https://r2northstar.gitbook.io/r2northstar-wiki/faq"); diff --git a/VTOL_2.0.0/Pages/Page_Thunderstore.xaml.cs b/VTOL_2.0.0/Pages/Page_Thunderstore.xaml.cs index b07433aa..de5a913b 100644 --- a/VTOL_2.0.0/Pages/Page_Thunderstore.xaml.cs +++ b/VTOL_2.0.0/Pages/Page_Thunderstore.xaml.cs @@ -115,7 +115,151 @@ private bool IsPilot(string Name) } } string current_skin_folder = null; + public bool TryDeleteDirectory( + string directoryPath, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 300) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCreateDirectory( + string directoryPath, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + for (int i = 0; i < maxRetries; ++i) + { + try + { + + Directory.CreateDirectory(directoryPath); + + if (Directory.Exists(directoryPath)) + { + + return true; + } + + + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryMoveFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Move(Origin, Destination, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCopyFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 300) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Copy(Origin, Destination, true); + } + Thread.Sleep(millisecondsDelay); + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } public void Install_Skin_From_Path(string Zip_Path) { @@ -140,7 +284,7 @@ public void Install_Skin_From_Path(string Zip_Path) else { - Directory.CreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"Skins_Unpack_Mod_MNGR"); + TryCreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"Skins_Unpack_Mod_MNGR"); current_skin_folder = User_Settings_Vars.NorthstarInstallLocation + @"Skins_Unpack_Mod_MNGR"; ZipFile zipFile = new ZipFile(Zip_Path); @@ -255,7 +399,7 @@ public void Install_Skin_From_Path(string Zip_Path) { dir_.Delete(true); } - Directory.Delete(current_skin_folder); + TryDeleteDirectory(current_skin_folder); @@ -947,6 +1091,146 @@ void Check_Reverse(bool Apply_Change = true) } } + public bool TryDeleteDirectory( +string directoryPath, bool overwrite = true, +int maxRetries = 10, +int millisecondsDelay = 30) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCreateDirectory( + string directoryPath, + int maxRetries = 10, + int millisecondsDelay = 30) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + + Directory.CreateDirectory(directoryPath); + + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryMoveFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Move(Origin, Destination, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCopyFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 300) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Copy(Origin, Destination, true); + } + Thread.Sleep(millisecondsDelay); + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } public List orderlist(List List) { @@ -1687,7 +1971,7 @@ private static void CopyFilesRecursively(string sourcePath, string targetPath) //Now Create all of the directories foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) { - Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); + Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); } //Copy all the files & Replaces any files with the same name @@ -1718,7 +2002,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio if (!Directory.Exists(Destination)) { - Directory.CreateDirectory(Destination); + TryCreateDirectory(Destination); } if (Directory.Exists(Destination)) { @@ -1791,7 +2075,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio - Directory.CreateDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); + TryCreateDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); if (Directory.Exists(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder")) { CopyFilesRecursively(firstFolder, Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); @@ -1801,7 +2085,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio Clear_Folder(Destination); CopyFilesRecursively(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", Destination); - Directory.Delete(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", true); + TryDeleteDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", true); } Console.WriteLine("Unpacked - " + Destination); @@ -1849,24 +2133,24 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio { if (Directory.Exists(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Client\Locked_Folder")) { - Directory.Delete(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Client\Locked_Folder", true); + TryDeleteDirectory(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Client\Locked_Folder", true); } if (Directory.Exists(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Custom\Locked_Folder")) { - Directory.Delete(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Custom\Locked_Folder", true); + TryDeleteDirectory(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.Custom\Locked_Folder", true); } if (Directory.Exists(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\Locked_Folder")) { - Directory.Delete(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\Locked_Folder", true); + TryDeleteDirectory(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\Locked_Folder", true); } if (!Directory.Exists(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder")) { - Directory.CreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder"); + TryCreateDirectory(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder"); } if (do_not_overwrite_Ns_file == true) @@ -1874,7 +2158,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args.txt")) { - System.IO.File.Copy(User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args.txt",User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args.txt", true); + TryCopyFile(User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args.txt",User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args.txt", true); } @@ -1884,7 +2168,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\mod\cfg\autoexec_ns_server.cfg")) { - System.IO.File.Copy(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\mod\cfg\autoexec_ns_server.cfg", User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\autoexec_ns_server.cfg", true); + TryCopyFile(User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\mod\cfg\autoexec_ns_server.cfg", User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\autoexec_ns_server.cfg", true); @@ -1896,7 +2180,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio { - System.IO.File.Copy(User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args_dedi.txt", User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args_dedi.txt", true); + TryCopyFile(User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args_dedi.txt", User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args_dedi.txt", true); } @@ -1938,14 +2222,14 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio } Console.WriteLine("Unpacked - " + Destination); - Directory.Delete(Destination,true); + TryDeleteDirectory(Destination,true); if (do_not_overwrite_Ns_file == true) { if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args.txt")) { - System.IO.File.Copy(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args.txt", User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args.txt", true); + TryCopyFile(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args.txt", User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args.txt", true); } @@ -1955,7 +2239,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio if (File.Exists(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\autoexec_ns_server.cfg")) { - System.IO.File.Copy (User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\autoexec_ns_server.cfg",User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\mod\cfg\autoexec_ns_server.cfg",true); + TryCopyFile (User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\autoexec_ns_server.cfg",User_Settings_Vars.NorthstarInstallLocation + User_Settings_Vars.Profile_Path + @"\mods\Northstar.CustomServers\mod\cfg\autoexec_ns_server.cfg",true); @@ -1967,7 +2251,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio { - System.IO.File.Copy(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args_dedi.txt", User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args_dedi.txt", true); + TryCopyFile(User_Settings_Vars.NorthstarInstallLocation + @"TempCopyFolder\ns_startup_args_dedi.txt", User_Settings_Vars.NorthstarInstallLocation + @"ns_startup_args_dedi.txt", true); } @@ -2059,7 +2343,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio - Directory.CreateDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); + TryCreateDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); if (Directory.Exists(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder")) { CopyFilesRecursively(firstFolder, Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder"); @@ -2069,7 +2353,7 @@ public async Task Unpack_To_Location_Custom(string Target_Zip, string Destinatio Clear_Folder(Destination); CopyFilesRecursively(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", Destination); - Directory.Delete(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", true); + TryDeleteDirectory(Destinfo.Parent.FullName + @"\" + "Temp_Working_Folder", true); } Console.WriteLine("Unpacked - " + Destination); @@ -2226,7 +2510,7 @@ await Task.Run(async () => //Task.Run automatically unwraps nested Task types! if (Directory.Exists(Destination) && Destination != "") { - Directory.Delete(Destination, true); + TryDeleteDirectory(Destination, true); } }); } diff --git a/VTOL_2.0.0/Pages/Page_Tools.xaml.cs b/VTOL_2.0.0/Pages/Page_Tools.xaml.cs index 0fe363e5..eb43b174 100644 --- a/VTOL_2.0.0/Pages/Page_Tools.xaml.cs +++ b/VTOL_2.0.0/Pages/Page_Tools.xaml.cs @@ -190,7 +190,7 @@ public void ToDX10(DXGI_FORMAT format) public void Save(string path) { - Directory.CreateDirectory(Path.GetDirectoryName(path)); + Directory.CreateDirectory(Path.GetDirectoryName(path)); BinaryWriter writer = new(new FileStream(path, FileMode.Create)); try { @@ -802,6 +802,151 @@ await Task.Run(() => } }); } + public bool TryDeleteDirectory( +string directoryPath, bool overwrite = true, +int maxRetries = 10, +int millisecondsDelay = 300) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (Directory.Exists(directoryPath)) + { + Directory.Delete(directoryPath, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCreateDirectory( + string directoryPath, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (directoryPath == null) + throw new ArgumentNullException(directoryPath); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + + Directory.CreateDirectory(directoryPath); + + if (Directory.Exists(directoryPath)) + { + + return true; + } + + + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryMoveFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 200) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Move(Origin, Destination, overwrite); + } + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } + public bool TryCopyFile( + string Origin, string Destination, bool overwrite = true, + int maxRetries = 10, + int millisecondsDelay = 300) + { + if (Origin == null) + throw new ArgumentNullException(Origin); + if (maxRetries < 1) + throw new ArgumentOutOfRangeException(nameof(maxRetries)); + if (millisecondsDelay < 1) + throw new ArgumentOutOfRangeException(nameof(millisecondsDelay)); + + for (int i = 0; i < maxRetries; ++i) + { + try + { + if (File.Exists(Origin)) + { + File.Copy(Origin, Destination, true); + } + Thread.Sleep(millisecondsDelay); + + return true; + } + catch (IOException) + { + Thread.Sleep(millisecondsDelay); + } + catch (UnauthorizedAccessException) + { + Thread.Sleep(millisecondsDelay); + } + } + + return false; + } private void Save_Mod_Click(object sender, RoutedEventArgs e) { try @@ -813,7 +958,7 @@ private void Save_Mod_Click(object sender, RoutedEventArgs e) if (Directory.Exists(Current_Output_Dir)) { FileInfo Dir = new FileInfo(Current_Mod_To_Pack); - Directory.CreateDirectory(Current_Output_Dir + @"\" + Mod_name.Text.Trim()); + TryCreateDirectory(Current_Output_Dir + @"\" + Mod_name.Text.Trim()); if (Directory.Exists(Current_Output_Dir + @"\" + Mod_name.Text.Trim())) { GenerateThumbImage(Mod_Icon_Path, Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "icon.png", 256, 256); @@ -831,9 +976,9 @@ private void Save_Mod_Click(object sender, RoutedEventArgs e) { if (!Directory.Exists(Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" )) { - Directory.CreateDirectory(Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" ); + TryCreateDirectory(Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" ); } - File.Copy(Current_Mod_To_Pack, Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" + Dir.Name, true); + TryCopyFile(Current_Mod_To_Pack, Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" + Dir.Name, true); } @@ -841,7 +986,7 @@ private void Save_Mod_Click(object sender, RoutedEventArgs e) { if (!Directory.Exists(Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" )) { - Directory.CreateDirectory(Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" ); + TryCreateDirectory(Current_Output_Dir + @"\" + Mod_name.Text.Trim() + @"\" + "mods" + @"\" ); } ZipFile_ zipFile = new ZipFile_(Current_Mod_To_Pack); @@ -880,7 +1025,7 @@ private void Save_Mod_Click(object sender, RoutedEventArgs e) if (File.Exists(Current_Output_Dir + @"\" + Mod_name.Text.Trim() + ".zip")) { - Directory.Delete(Current_Output_Dir + @"\" + Mod_name.Text.Trim(), true); + TryDeleteDirectory(Current_Output_Dir + @"\" + Mod_name.Text.Trim(), true); } SnackBar.Appearance = ControlAppearance.Success; SnackBar.Title = "SUCCESS"; @@ -1938,13 +2083,13 @@ public void Convert() // // directory for unzipped file - Directory.CreateDirectory(skinTempFolderPath); + TryCreateDirectory(skinTempFolderPath); // directory for TS-compliant mod - Directory.CreateDirectory(modTempFolderPath); + TryCreateDirectory(modTempFolderPath); // directory for RePak things - Directory.CreateDirectory(repakTempFolderPath); + TryCreateDirectory(repakTempFolderPath); /////////////////////////////// @@ -1974,7 +2119,7 @@ public void Convert() //////////////////////////////////// - Directory.CreateDirectory(modTempFolderPath + "\\mods\\" + Mod_Adv_Author_name + "." + Mod_Adv_Skin_Name + "\\paks"); + TryCreateDirectory(modTempFolderPath + "\\mods\\" + Mod_Adv_Author_name + "." + Mod_Adv_Skin_Name + "\\paks"); ///////////////////// @@ -2067,7 +2212,7 @@ public void Convert() // copy png over - File.Copy(Mod_Adv_Icon_Path, modTempFolderPath + "\\icon.png",true); + TryCopyFile(Mod_Adv_Icon_Path, modTempFolderPath + "\\icon.png",true); } // ConvertTaskComplete(); @@ -2166,7 +2311,7 @@ public void Convert() } isFirst = false; // copy file - Directory.CreateDirectory(Directory.GetParent($"{repakTempFolderPath}\\assets\\{texturePath}.dds").FullName); + TryCreateDirectory(Directory.GetParent($"{repakTempFolderPath}\\assets\\{texturePath}.dds").FullName); DdsHandler handler = new(texture); handler.Convert(); @@ -2233,7 +2378,7 @@ public void Convert() //////////////////////////////////// - File.Move(tempFolderPath + "\\" + Mod_Adv_Author_name + "." + Mod_Adv_Skin_Name + ".zip", Mod_Adv_Output_Path + "\\" + Mod_Adv_Author_name + "." + Mod_Adv_Skin_Name + ".zip", true); + TryMoveFile(tempFolderPath + "\\" + Mod_Adv_Author_name + "." + Mod_Adv_Skin_Name + ".zip", Mod_Adv_Output_Path + "\\" + Mod_Adv_Author_name + "." + Mod_Adv_Skin_Name + ".zip", true); } catch (Exception ex) @@ -3125,7 +3270,7 @@ await Task.Run(() => { if (!Directory.Exists(Tools_Dir+ Sub_Name)) { - Directory.CreateDirectory(Tools_Dir + Sub_Name); + TryCreateDirectory(Tools_Dir + Sub_Name); } IDownload downloader = DownloadBuilder.New() .WithUrl(URL) @@ -3334,7 +3479,7 @@ await Task.Run(async () => //Task.Run automatically unwraps nested Task types! else { - Directory.CreateDirectory(Tools_Dir); + TryCreateDirectory(Tools_Dir); Check_For_Tools(); } diff --git a/VTOL_2.0.0/VTOL.csproj b/VTOL_2.0.0/VTOL.csproj index 068941b4..1cbe739a 100644 --- a/VTOL_2.0.0/VTOL.csproj +++ b/VTOL_2.0.0/VTOL.csproj @@ -131,6 +131,13 @@ + + + + + + + @@ -180,6 +187,7 @@ + @@ -437,6 +445,30 @@ PreserveNewest + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + + + Always + PreserveNewest