Skip to content

Commit

Permalink
Split cache option for batch, model tree auto-expand option
Browse files Browse the repository at this point in the history
  • Loading branch information
N00MKRAD committed Jan 21, 2021
1 parent bd41dc5 commit 40f1574
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 298 deletions.
2 changes: 1 addition & 1 deletion Code/Forms/ModelComparisonForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async Task DoUpscale (int index, ModelData mdl, bool fullImage)
if (useNcnn) backend = ESRGAN.Backend.NCNN;
string inpath = Paths.previewPath;
if (fullImage) inpath = Paths.tempImgPath.GetParentDir();
await ESRGAN.DoUpscale(inpath, Paths.compositionOut, mdl, Config.Get("tilesize"), Config.GetBool("alpha"), ESRGAN.PreviewMode.None, backend);
await ESRGAN.DoUpscale(inpath, Paths.compositionOut, mdl, false, Config.GetBool("alpha"), ESRGAN.PreviewMode.None, backend);
if (backend == ESRGAN.Backend.NCNN)
outImg = Directory.GetFiles(Paths.compositionOut, "*.png*", SearchOption.AllDirectories)[0];
else
Expand Down
5 changes: 4 additions & 1 deletion Code/Forms/ModelSelectForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ private void ModelSelectForm_Load(object sender, EventArgs e)
}
DirectoryInfo modelsDir = new DirectoryInfo(modelDir);
BuildTree(modelsDir, modelTree.Nodes);
modelTree.ExpandAll();
if (Config.GetBool("modelSelectAutoExpand", true))
modelTree.ExpandAll();
else
modelTree.Nodes[0].Expand();
}

private async void SelectLastUsed()
Expand Down
246 changes: 135 additions & 111 deletions Code/Forms/SettingsForm.Designer.cs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Code/Forms/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void LoadSettings()
Config.LoadGuiElement(reloadImageBeforeUpscale);
Config.LoadComboxIndex(pythonRuntime);
Config.LoadComboxIndex(comparisonUseScaling);
Config.LoadGuiElement(modelSelectAutoExpand);
// Formats
Config.LoadGuiElement(jpegQ);
Config.LoadGuiElement(webpQ);
Expand Down Expand Up @@ -100,6 +101,7 @@ void SaveSettings()
Config.SaveGuiElement(reloadImageBeforeUpscale);
Config.SaveComboxIndex(pythonRuntime);
Config.SaveComboxIndex(comparisonUseScaling);
Config.SaveGuiElement(modelSelectAutoExpand);
// Formats
Config.SaveGuiElement(jpegQ, true);
Config.SaveGuiElement(webpQ, true);
Expand Down
347 changes: 185 additions & 162 deletions Code/IO/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,166 +8,189 @@

namespace Cupscale
{
internal class Config
{
private static string configPath;

private static string[] cachedLines;

public static bool disable;

public static void Init()
{
configPath = Path.Combine(IOUtils.GetAppDataDir(), "config.ini");
if (!File.Exists(configPath))
{
File.Create(configPath).Close();
}
Reload();
}

public static void Set(string key, string value)
{
if (disable) return;

string[] lines = File.ReadAllLines(configPath);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Split('|')[0] == key)
{
lines[i] = key + "|" + value;
File.WriteAllLines(configPath, lines);
cachedLines = lines;
return;
}
}
List<string> list = lines.ToList();
list.Add(key + "|" + value);
File.WriteAllLines(configPath, list.ToArray());
cachedLines = list.ToArray();
}

public static string Get(string key)
{
for (int i = 0; i < cachedLines.Length; i++)
{
string[] keyValuePair = cachedLines[i].Split('|');
if (keyValuePair[0] == key)
return keyValuePair[1];
}
return WriteDefaultValIfExists(key);
}

public static bool GetBool (string key)
{
return bool.Parse(Get(key));
}

public static int GetInt (string key)
{
for (int i = 0; i < cachedLines.Length; i++)
{
string[] keyValuePair = cachedLines[i].Split('|');
if (keyValuePair[0] == key)
return int.Parse(keyValuePair[1].Trim());
}
return int.Parse(WriteDefaultValIfExists(key).Trim());
}

private static string WriteDefaultValIfExists(string key)
{
return key switch
{
"esrganPath" => WriteDefault(key, Installer.path),
"esrganVer" => WriteDefault(key, "0"),
"tilesize" => WriteDefault(key, "1024"),
"alpha" => WriteDefault(key, "False"),
"alphaMode" => WriteDefault(key, "1"),
"alphaDepth" => WriteDefault(key, "0"),
"seamlessMode" => WriteDefault(key, "0"),
"alphaBgColor" => WriteDefault(key, "000000FF"),
"jpegExtension" => WriteDefault(key, "jpg"),
"jpegQ" => WriteDefault(key, "95"),
"webpQ" => WriteDefault(key, "95"),
"dxtMode" => WriteDefault(key, "BC1 (DXT1)"),
"ddsEnableMips" => WriteDefault(key, "True"),
"previewFormat" => WriteDefault(key, "0"),
"cudaFallback" => WriteDefault(key, "0"),
"gpuId" => WriteDefault(key, "0"),
"reloadImageBeforeUpscale" => WriteDefault(key, "False"),
"cmdDebug" => WriteDefault(key, "False"),
"flipTga" => WriteDefault(key, "True"),
"logIo" => WriteDefault(key, "False"),
"logStatus" => WriteDefault(key, "False"),
"cmdDebugMode" => WriteDefault(key, "0"),
"pythonRuntime" => WriteDefault(key, "0"),
"useMozJpeg" => WriteDefault(key, "True"),
"comparisonUseScaling" => WriteDefault(key, "0"),
"joeyAlphaMode" => WriteDefault(key, "1"),
// Video
"h265" => WriteDefault(key, "False"),
"crf" => WriteDefault(key, "18"),
"gifskiQ" => WriteDefault(key, "100"),
"vidEnableAudio" => WriteDefault(key, "True"),
_ => null,
};
}

private static string WriteDefault(string key, string def)
{
Set(key, def);
return def;
}

private static void Reload()
{
cachedLines = File.ReadAllLines(configPath);
}

public static void SaveGuiElement(TextBox textbox, bool onlyAllowNumbers = false)
{
if(onlyAllowNumbers)
Set(textbox.Name, textbox.Text.GetInt().ToString());
else
Set(textbox.Name, textbox.Text);
}

public static void SaveGuiElement(ComboBox comboBox, bool onlyAllowNumbers = false)
{
if (onlyAllowNumbers)
Set(comboBox.Name, comboBox.Text.GetInt().ToString());
else
Set(comboBox.Name, comboBox.Text);
}

public static void SaveGuiElement(CheckBox checkbox)
{
Set(checkbox.Name, checkbox.Checked.ToString());
}

public static void SaveComboxIndex(ComboBox comboBox)
{
Set(comboBox.Name, comboBox.SelectedIndex.ToString());
}

public static void LoadGuiElement(ComboBox comboBox)
{
comboBox.Text = Get(comboBox.Name);
}

public static void LoadGuiElement (TextBox textbox)
{
textbox.Text = Get(textbox.Name);
}

public static void LoadGuiElement (CheckBox checkbox)
{
checkbox.Checked = bool.Parse(Get(checkbox.Name));
}

public static void LoadComboxIndex (ComboBox comboBox)
{
comboBox.SelectedIndex = GetInt(comboBox.Name);
}
}
internal class Config
{
private static string configPath;

private static string[] cachedLines;

public static bool disable;

public static void Init()
{
configPath = Path.Combine(IOUtils.GetAppDataDir(), "config.ini");
if (!File.Exists(configPath))
{
File.Create(configPath).Close();
}
Reload();
}

public static void Set(string key, string value)
{
if (disable) return;

string[] lines = File.ReadAllLines(configPath);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Split('|')[0] == key)
{
lines[i] = key + "|" + value;
File.WriteAllLines(configPath, lines);
cachedLines = lines;
return;
}
}
List<string> list = lines.ToList();
list.Add(key + "|" + value);
File.WriteAllLines(configPath, list.ToArray());
cachedLines = list.ToArray();
}

public static string Get(string key, Type type = Type.String)
{
try
{
for (int i = 0; i < cachedLines.Length; i++)
{
string[] keyValuePair = cachedLines[i].Split('|');
if (keyValuePair[0] == key && !string.IsNullOrWhiteSpace(keyValuePair[1]))
return keyValuePair[1];
}
return WriteDefaultValIfExists(key, type);
}
catch (Exception e)
{
Logger.Log($"Failed to get {key.Wrap()} from config! {e.Message}");
}
return null;
}

public static bool GetBool(string key)
{
return bool.Parse(Get(key));
}

public static bool GetBool(string key, bool defaultVal)
{
WriteIfDoesntExist(key, (defaultVal ? "True" : "False"));
return bool.Parse(Get(key, Type.Bool));
}

public static int GetInt(string key)
{
for (int i = 0; i < cachedLines.Length; i++)
{
string[] keyValuePair = cachedLines[i].Split('|');
if (keyValuePair[0] == key)
return int.Parse(keyValuePair[1].Trim());
}
return int.Parse(WriteDefaultValIfExists(key, Type.Int).Trim());
}

static void WriteIfDoesntExist(string key, string val)
{
foreach (string line in cachedLines)
if (line.Contains(key + "|"))
return;
Set(key, val);
}

public enum Type { String, Int, Float, Bool }
private static string WriteDefaultValIfExists(string key, Type type)
{
if (key == "esrganPath") return WriteDefault(key, Installer.path);
if (key == "esrganVer") return WriteDefault(key, "0");
if (key == "tilesize") return WriteDefault(key, "1024");
if (key == "alpha") return WriteDefault(key, "False");
if (key == "alphaMode") return WriteDefault(key, "1");
if (key == "alphaDepth") return WriteDefault(key, "0");
if (key == "seamlessMode") return WriteDefault(key, "0");
if (key == "alphaBgColor") return WriteDefault(key, "000000FF");
if (key == "jpegExtension") return WriteDefault(key, "jpg");
if (key == "jpegQ") return WriteDefault(key, "95");
if (key == "webpQ") return WriteDefault(key, "95");
if (key == "dxtMode") return WriteDefault(key, "BC1 (DXT1)");
if (key == "ddsEnableMips") return WriteDefault(key, "True");
if (key == "previewFormat") return WriteDefault(key, "0");
if (key == "cudaFallback") return WriteDefault(key, "0");
if (key == "gpuId") return WriteDefault(key, "0");
if (key == "reloadImageBeforeUpscale") return WriteDefault(key, "False");
if (key == "cmdDebug") return WriteDefault(key, "False");
if (key == "flipTga") return WriteDefault(key, "True");
if (key == "logIo") return WriteDefault(key, "False");
if (key == "logStatus") return WriteDefault(key, "False");
if (key == "cmdDebugMode") return WriteDefault(key, "0");
if (key == "pythonRuntime") return WriteDefault(key, "0");
if (key == "useMozJpeg") return WriteDefault(key, "True");
if (key == "comparisonUseScaling") return WriteDefault(key, "0");
if (key == "joeyAlphaMode") return WriteDefault(key, "1");
// Video
if (key == "h265") return WriteDefault(key, "False");
if (key == "crf") return WriteDefault(key, "18");
if (key == "gifskiQ") return WriteDefault(key, "100");
if (key == "vidEnableAudio") return WriteDefault(key, "True");

if (type == Type.Int || type == Type.Float) return WriteDefault(key, "0"); // Write default int/float (0)
if (type == Type.Bool) return WriteDefault(key, "False"); // Write default bool (False)
return WriteDefault(key, "0");
}

private static string WriteDefault(string key, string def)
{
Set(key, def);
return def;
}

private static void Reload()
{
cachedLines = File.ReadAllLines(configPath);
}

public static void SaveGuiElement(TextBox textbox, bool onlyAllowNumbers = false)
{
if (onlyAllowNumbers)
Set(textbox.Name, textbox.Text.GetInt().ToString());
else
Set(textbox.Name, textbox.Text);
}

public static void SaveGuiElement(ComboBox comboBox, bool onlyAllowNumbers = false)
{
if (onlyAllowNumbers)
Set(comboBox.Name, comboBox.Text.GetInt().ToString());
else
Set(comboBox.Name, comboBox.Text);
}

public static void SaveGuiElement(CheckBox checkbox)
{
Set(checkbox.Name, checkbox.Checked.ToString());
}

public static void SaveComboxIndex(ComboBox comboBox)
{
Set(comboBox.Name, comboBox.SelectedIndex.ToString());
}

public static void LoadGuiElement(ComboBox comboBox)
{
comboBox.Text = Get(comboBox.Name);
}

public static void LoadGuiElement(TextBox textbox)
{
textbox.Text = Get(textbox.Name);
}

public static void LoadGuiElement(CheckBox checkbox)
{
checkbox.Checked = bool.Parse(Get(checkbox.Name));
}

public static void LoadComboxIndex(ComboBox comboBox)
{
comboBox.SelectedIndex = GetInt(comboBox.Name);
}
}
}
Loading

0 comments on commit 40f1574

Please sign in to comment.