From d8e8bfc94ecb81cf7e60e9a572dc53178701c487 Mon Sep 17 00:00:00 2001 From: N00MKRAD Date: Thu, 25 Mar 2021 12:31:11 +0100 Subject: [PATCH] Added running-out-of-archive warning --- Code/IO/Config.cs | 2 +- Code/IO/IOUtils.cs | 24 +------------- Code/IO/Installer.cs | 14 ++++----- Code/IO/Paths.cs | 49 ++++++++++++++++++++++------- Code/Main/Logger.cs | 5 +-- Code/Main/MainForm.cs | 9 +++++- Code/Main/Program.cs | 10 +++--- Code/OS/EmbeddedPython.cs | 2 +- Code/Preview/ClipboardComparison.cs | 3 +- Code/UI/BatchUpscaleUI.cs | 2 +- Code/UI/VideoUpscaleUI.cs | 4 +-- 11 files changed, 68 insertions(+), 56 deletions(-) diff --git a/Code/IO/Config.cs b/Code/IO/Config.cs index 1c9490e..e6b4f3c 100644 --- a/Code/IO/Config.cs +++ b/Code/IO/Config.cs @@ -18,7 +18,7 @@ internal class Config public static void Init() { - configPath = Path.Combine(IOUtils.GetAppDataDir(), "config.ini"); + configPath = Path.Combine(Paths.GetDataPath(), "config.ini"); if (!File.Exists(configPath)) File.Create(configPath).Close(); diff --git a/Code/IO/IOUtils.cs b/Code/IO/IOUtils.cs index 22de3b4..9e7bf24 100644 --- a/Code/IO/IOUtils.cs +++ b/Code/IO/IOUtils.cs @@ -20,24 +20,7 @@ internal class IOUtils { public static string[] compatibleExtensions = new string[] { ".png", ".jpg", ".jpeg", ".bmp", ".tga", ".webp", ".dds", ".gif" }; public static string[] videoExtensions = new string[] { ".mp4", ".m4v", ".mkv", ".webm", ".gif", ".avi" }; - static bool hasShownPortableInfo = false; - - public static string GetAppDataDir() - { - string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); - path = Path.Combine(path, "Cupscale"); - if (IsPortable()) - { - if (!hasShownPortableInfo) - { - Logger.Log("Running in portable mode. Data folder: " + Path.Combine(GetExeDir(), "CupscaleData"), false); - hasShownPortableInfo = true; - } - path = Path.Combine(GetExeDir(), "CupscaleData"); - } - Directory.CreateDirectory(path); - return path; - } + public static bool hasShownPortableInfo = false; public static bool IsPortable () { @@ -50,11 +33,6 @@ public static bool IsPortable () return true; } - public static string GetExeDir() - { - return AppDomain.CurrentDomain.BaseDirectory; - } - public static string[] ReadLines(string path) { List lines = new List(); diff --git a/Code/IO/Installer.cs b/Code/IO/Installer.cs index c447f7f..40abd96 100644 --- a/Code/IO/Installer.cs +++ b/Code/IO/Installer.cs @@ -36,7 +36,7 @@ public static bool InstallationIsValid () requiredDirs.Add(Path.Combine(path, "utils")); List requiredFiles = new List(); - requiredFiles.Add(Path.Combine(IOUtils.GetAppDataDir(), "shipped-files-version.txt")); + requiredFiles.Add(Path.Combine(Paths.GetDataPath(), "shipped-files-version.txt")); requiredFiles.Add(Path.Combine(path, "upscale.py")); requiredFiles.Add(Path.Combine(path, "ffmpeg.exe")); requiredFiles.Add(Path.Combine(path, "esrgan-ncnn-vulkan.exe")); @@ -63,7 +63,7 @@ public static bool InstallationIsValid () } } - int diskVersion = IOUtils.ReadLines(Path.Combine(IOUtils.GetAppDataDir(), "shipped-files-version.txt"))[0].Split('#')[0].GetInt(); + int diskVersion = IOUtils.ReadLines(Path.Combine(Paths.GetDataPath(), "shipped-files-version.txt"))[0].Split('#')[0].GetInt(); if (exeFilesVersion != diskVersion) { Logger.Log("[Installer] Installation invalid: Shipped file version mismatch - Executable is " + exeFilesVersion + ", installation is " + diskVersion); @@ -119,7 +119,7 @@ public static async Task Install () static async Task DownloadAndInstall(int version, string filename, bool showDialog = true) { - string savePath = Path.Combine(IOUtils.GetAppDataDir(), filename); + string savePath = Path.Combine(Paths.GetDataPath(), filename); string url = $"https://dl.nmkd.de/cupscale/shippedfiles/{version}/{filename}"; Logger.Log($"[Installer] Downloading {url}"); var client = new WebClient(); @@ -131,7 +131,7 @@ static async Task DownloadAndInstall(int version, string filename, bool showDial { if (currentDlDialog != null) currentDlDialog.ChangeText($"Installing {filename}..."); - await UnSevenzip(Path.Combine(IOUtils.GetAppDataDir(), filename)); + await UnSevenzip(Path.Combine(Paths.GetDataPath(), filename)); } if(currentDlDialog != null) currentDlDialog.Close(); @@ -155,7 +155,7 @@ static async Task UnSevenzip (string path) await Task.Delay(20); SevenZipNET.SevenZipExtractor.Path7za = path7za; SevenZipNET.SevenZipExtractor extractor = new SevenZipNET.SevenZipExtractor(path); - extractor.ExtractAll(IOUtils.GetAppDataDir(), true, true); + extractor.ExtractAll(Paths.GetDataPath(), true, true); File.Delete(path); await Task.Delay(10); } @@ -177,12 +177,12 @@ public static bool Exists() public static void Uninstall (bool full) { - if (!Directory.Exists(IOUtils.GetAppDataDir())) + if (!Directory.Exists(Paths.GetDataPath())) return; try { if (full) - Directory.Delete(IOUtils.GetAppDataDir(), true); + Directory.Delete(Paths.GetDataPath(), true); else Directory.Delete(path, true); } diff --git a/Code/IO/Paths.cs b/Code/IO/Paths.cs index 7bceb22..ec9be78 100644 --- a/Code/IO/Paths.cs +++ b/Code/IO/Paths.cs @@ -1,3 +1,4 @@ +using System; using Cupscale.UI; using System.IO; @@ -21,17 +22,17 @@ internal class Paths public static void Init() { - esrganPath = Path.Combine(IOUtils.GetAppDataDir(), "ShippedEsrgan"); - previewPath = Path.Combine(IOUtils.GetAppDataDir(), "preview"); - previewOutPath = Path.Combine(IOUtils.GetAppDataDir(), "preview-out"); - imgInPath = Path.Combine(IOUtils.GetAppDataDir(), "img-in"); - imgOutPath = Path.Combine(IOUtils.GetAppDataDir(), "img-out"); - imgOutNcnnPath = Path.Combine(IOUtils.GetAppDataDir(), "img-out-ncnn"); - tempImgPath = Path.Combine(IOUtils.GetAppDataDir(), "loaded-img", "temp.png"); - clipboardFolderPath = Path.Combine(IOUtils.GetAppDataDir(), "clipboard"); - presetsPath = Path.Combine(IOUtils.GetAppDataDir(), "model-presets"); - compositionOut = Path.Combine(IOUtils.GetAppDataDir(), "composition"); - framesOutPath = Path.Combine(IOUtils.GetAppDataDir(), "frames-out"); + esrganPath = Path.Combine(GetDataPath(), "ShippedEsrgan"); + previewPath = Path.Combine(GetDataPath(), "preview"); + previewOutPath = Path.Combine(GetDataPath(), "preview-out"); + imgInPath = Path.Combine(GetDataPath(), "img-in"); + imgOutPath = Path.Combine(GetDataPath(), "img-out"); + imgOutNcnnPath = Path.Combine(GetDataPath(), "img-out-ncnn"); + tempImgPath = Path.Combine(GetDataPath(), "loaded-img", "temp.png"); + clipboardFolderPath = Path.Combine(GetDataPath(), "clipboard"); + presetsPath = Path.Combine(GetDataPath(), "model-presets"); + compositionOut = Path.Combine(GetDataPath(), "composition"); + framesOutPath = Path.Combine(GetDataPath(), "frames-out"); progressLogfile = Path.Combine(esrganPath, "prog"); Directory.CreateDirectory(previewPath); Directory.CreateDirectory(previewOutPath); @@ -44,5 +45,29 @@ public static void Init() Directory.CreateDirectory(compositionOut); Directory.CreateDirectory(framesOutPath); } - } + + public static string GetDataPath() + { + string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + path = Path.Combine(path, "Cupscale"); + + if (IOUtils.IsPortable()) + { + if (!IOUtils.hasShownPortableInfo) + { + Logger.Log("Running in portable mode. Data folder: " + Path.Combine(GetExeDir(), "CupscaleData"), false); + IOUtils.hasShownPortableInfo = true; + } + path = Path.Combine(GetExeDir(), "CupscaleData"); + } + + Directory.CreateDirectory(path); + return path; + } + + public static string GetExeDir() + { + return AppDomain.CurrentDomain.BaseDirectory; + } + } } diff --git a/Code/Main/Logger.cs b/Code/Main/Logger.cs index 4ebf10f..7132bce 100644 --- a/Code/Main/Logger.cs +++ b/Code/Main/Logger.cs @@ -2,6 +2,7 @@ using System; using System.IO; using System.Windows.Forms; +using Cupscale.IO; using DT = System.DateTime; namespace Cupscale @@ -21,7 +22,7 @@ internal class Logger public static void Init () { - file = Path.Combine(IOUtils.GetAppDataDir(), "sessionlog.txt"); + file = Path.Combine(Paths.GetDataPath(), "sessionlog.txt"); doLogIo = Config.GetBool("logIo"); doLogStatus = Config.GetBool("logStatus"); PrintArgs(); @@ -57,7 +58,7 @@ public static void Log(string s, bool logToFile = true, bool noLineBreak = false public static void LogToFile(string s, bool noLineBreak) { if (string.IsNullOrWhiteSpace(file)) - file = Path.Combine(IOUtils.GetAppDataDir(), "sessionlog.txt"); + file = Path.Combine(Paths.GetDataPath(), "sessionlog.txt"); string time = DT.Now.Month + "-" + DT.Now.Day + "-" + DT.Now.Year + " " + DT.Now.Hour + ":" + DT.Now.Minute + ":" + DT.Now.Second; try diff --git a/Code/Main/MainForm.cs b/Code/Main/MainForm.cs index 15c4418..718c27c 100644 --- a/Code/Main/MainForm.cs +++ b/Code/Main/MainForm.cs @@ -39,8 +39,15 @@ public MainForm() private async void MainForm_Load(object sender, EventArgs e) { + if (!Directory.Exists(Path.Combine(Paths.GetExeDir(), "runtimes")) && Paths.GetExeDir().ToLower().Contains("temp")) + { + MessageBox.Show("You seem to be running Flowframes out of an archive.\nPlease extract the whole archive first!", "Error"); + IOUtils.TryDeleteIfExists(Paths.GetDataPath()); + Application.Exit(); + } + // Left Panel - UIHelpers.InitCombox(prevClipboardTypeCombox, 0); + UIHelpers.InitCombox(prevClipboardTypeCombox, 0); UIHelpers.InitCombox(preResizeScale, 1); UIHelpers.InitCombox(preResizeMode, 0); UIHelpers.FillEnumComboBox(preResizeFilter, typeof(Upscale.Filter), 0); diff --git a/Code/Main/Program.cs b/Code/Main/Program.cs index 7ade6bf..680f1b4 100644 --- a/Code/Main/Program.cs +++ b/Code/Main/Program.cs @@ -44,21 +44,21 @@ private static void Main() { try { - string lockfile = Path.Combine(IOUtils.GetAppDataDir(), "lockfile"); + string lockfile = Path.Combine(Paths.GetDataPath(), "lockfile"); File.Create(lockfile).Dispose(); FileStream fs = File.Open(lockfile, FileMode.Open, FileAccess.ReadWrite, FileShare.None); } catch { MessageBox.Show("Another instance of Cupscale seems to be running, accessing the following data folder:\n" - + IOUtils.GetAppDataDir() + ".\n\nMultiple instance are only possible if they use different data folders.\n" + + Paths.GetDataPath() + ".\n\nMultiple instance are only possible if they use different data folders.\n" + "Starting Cupscale with \"-portable\" will use the current directory as data folder.", "Error"); return; } Application.SetCompatibleTextRenderingDefault(defaultValue: false); Application.EnableVisualStyles(); - IOUtils.DeleteIfExists(Path.Combine(IOUtils.GetAppDataDir(), "sessionlog.txt")); + IOUtils.DeleteIfExists(Path.Combine(Paths.GetDataPath(), "sessionlog.txt")); Config.Init(); Logger.Init(); Paths.Init(); @@ -100,11 +100,11 @@ public static void Cleanup() IOUtils.ClearDir(Paths.imgOutPath); IOUtils.ClearDir(Paths.imgOutNcnnPath); IOUtils.ClearDir(Paths.tempImgPath.GetParentDir()); - IOUtils.ClearDir(Path.Combine(IOUtils.GetAppDataDir(), "giftemp")); + IOUtils.ClearDir(Path.Combine(Paths.GetDataPath(), "giftemp")); IOUtils.DeleteIfExists(Path.Combine(Paths.presetsPath, "lastUsed")); IOUtils.ClearDir(Paths.compositionOut); IOUtils.ClearDir(Paths.framesOutPath); - IOUtils.DeleteIfExists(Path.Combine(IOUtils.GetAppDataDir(), "frames-out.mp4")); + IOUtils.DeleteIfExists(Path.Combine(Paths.GetDataPath(), "frames-out.mp4")); } catch (Exception e) { diff --git a/Code/OS/EmbeddedPython.cs b/Code/OS/EmbeddedPython.cs index 678f593..4503dfe 100644 --- a/Code/OS/EmbeddedPython.cs +++ b/Code/OS/EmbeddedPython.cs @@ -67,7 +67,7 @@ public static async Task Download(TextBox logTextbox, HTAlt.WinForms.HTButton ru await Task.Delay(10); Print("Checking disk space before installation..."); - float diskSpaceMb = IOUtils.GetDiskSpace(IOUtils.GetAppDataDir()); + float diskSpaceMb = IOUtils.GetDiskSpace(Paths.GetDataPath()); Print($"Available disk space on the current drive: {diskSpaceMb} MB."); if (diskSpaceMb < 5000) { diff --git a/Code/Preview/ClipboardComparison.cs b/Code/Preview/ClipboardComparison.cs index e28093e..63af37d 100644 --- a/Code/Preview/ClipboardComparison.cs +++ b/Code/Preview/ClipboardComparison.cs @@ -14,6 +14,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Paths = Cupscale.IO.Paths; namespace Cupscale { @@ -249,7 +250,7 @@ public static async void BeforeAfterAnim (bool save, bool h264) DialogForm dialogForm = new DialogForm("Creating comparison " + ext.ToUpper() + "..."); - string tempPath = Path.Combine(IOUtils.GetAppDataDir(), "giftemp"); + string tempPath = Path.Combine(Paths.GetDataPath(), "giftemp"); string framesPath = Path.Combine(tempPath, "frames"); IOUtils.ClearDir(tempPath); Directory.CreateDirectory(framesPath); diff --git a/Code/UI/BatchUpscaleUI.cs b/Code/UI/BatchUpscaleUI.cs index 88779ae..cb6ea84 100644 --- a/Code/UI/BatchUpscaleUI.cs +++ b/Code/UI/BatchUpscaleUI.cs @@ -164,7 +164,7 @@ public static async Task Run (bool preprocess, bool postProcess = true, bool cac if (!IOUtils.HasEnoughDiskSpace((int)(IOUtils.GetDirSize(Paths.imgInPath) / 1024 / 1024), imgOutDir.Substring(0, 2), 2.0f) ) { - Program.ShowMessage($"Not enough disk space on {IOUtils.GetAppDataDir().Substring(0, 3)} to store temporary files!", "Error"); + Program.ShowMessage($"Not enough disk space on {Paths.GetDataPath().Substring(0, 3)} to store temporary files!", "Error"); return; } diff --git a/Code/UI/VideoUpscaleUI.cs b/Code/UI/VideoUpscaleUI.cs index 176b80b..2b138c2 100644 --- a/Code/UI/VideoUpscaleUI.cs +++ b/Code/UI/VideoUpscaleUI.cs @@ -87,7 +87,7 @@ public static async Task Run(bool preprocess) Print("Creating video from frames..."); await CreateVideo(); Print("Done creating video."); - CopyBack(Path.Combine(IOUtils.GetAppDataDir(), "frames-out.mp4")); + CopyBack(Path.Combine(Paths.GetDataPath(), "frames-out.mp4")); Print("Adding audio from source to output video..."); IOUtils.ClearDir(Paths.imgInPath); IOUtils.ClearDir(Paths.framesOutPath); @@ -161,7 +161,7 @@ static async Task CreateVideo() { DialogForm f = new DialogForm("Creating GIF from frames...\nThis can take a while for high-resolution GIFs.", 600); await Task.Delay(10); - string outpath = Path.Combine(IOUtils.GetAppDataDir(), "frames-out.mp4").Wrap(); + string outpath = Path.Combine(Paths.GetDataPath(), "frames-out.mp4").Wrap(); await FFmpeg.RunGifski($" -r {fps.RoundToInt()} -W 4096 -Q {Config.GetInt("gifskiQ")} -q -o {outpath} \"{Paths.framesOutPath}/\"*.\"png\""); f.Close(); }