Skip to content

Commit

Permalink
Added running-out-of-archive warning
Browse files Browse the repository at this point in the history
  • Loading branch information
n00mkrad committed Mar 25, 2021
1 parent 5c76d72 commit d8e8bfc
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Code/IO/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
24 changes: 1 addition & 23 deletions Code/IO/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
{
Expand All @@ -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<string> lines = new List<string>();
Expand Down
14 changes: 7 additions & 7 deletions Code/IO/Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static bool InstallationIsValid ()
requiredDirs.Add(Path.Combine(path, "utils"));

List<string> requiredFiles = new List<string>();
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"));
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down
49 changes: 37 additions & 12 deletions Code/IO/Paths.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Cupscale.UI;
using System.IO;

Expand All @@ -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);
Expand All @@ -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;
}
}
}
5 changes: 3 additions & 2 deletions Code/Main/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.IO;
using System.Windows.Forms;
using Cupscale.IO;
using DT = System.DateTime;

namespace Cupscale
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion Code/Main/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions Code/Main/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion Code/OS/EmbeddedPython.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion Code/Preview/ClipboardComparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Paths = Cupscale.IO.Paths;

namespace Cupscale
{
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Code/UI/BatchUpscaleUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions Code/UI/VideoUpscaleUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit d8e8bfc

Please sign in to comment.