Skip to content

Commit

Permalink
Fixes twilightmenu settings when bigger images are being used.
Browse files Browse the repository at this point in the history
  • Loading branch information
KirovAir committed Apr 1, 2020
1 parent 250dd8e commit a03c44a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
4 changes: 3 additions & 1 deletion TwilightBoxart.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static void Main(string[] args)
config.SdRoot = choice;
}

var boxArtPath = config.GetBoxartPath();
var boxArtPath = config.GetCorrectBoxartPath();
ConsoleEx.WriteGreenLine("Loaded settings:");
Console.WriteLine("SDRoot / Roms location: \t" + config.SdRoot);
Console.WriteLine("BoxArt location: \t\t" + boxArtPath);
Expand All @@ -87,6 +87,8 @@ static void Main(string[] args)

var progress = new Progress<string>(Console.WriteLine);
var crawler = new BoxartCrawler(progress);
config.BoxartPath = boxArtPath;
config.SettingsPath = config.GetCorrectSettingsIniPath();
crawler.DownloadArt(config).Wait();
}
}
Expand Down
4 changes: 3 additions & 1 deletion TwilightBoxart.CLI/TwilightBoxart.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
[Config]
SdRoot=
BoxartPath={sdroot}/_nds/TWiLightMenu/boxart
; Large for NDS = 256 x 192
SettingsPath={sdroot}/_nds/TWiLightMenu/settings.ini

; Large for NDS = 208 x 143
BoxartWidth=128
BoxartHeight=115
KeepAspectRatio=true
Expand Down
10 changes: 4 additions & 6 deletions TwilightBoxart.UX/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions TwilightBoxart.UX/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private void SetUx()

if (!chkManualBoxartLocation.Checked && !string.IsNullOrEmpty(txtSdRoot.Text))
{
txtBoxart.Text = _config.GetBoxartPath(txtSdRoot.Text);
txtBoxart.Text = _config.GetCorrectBoxartPath(txtSdRoot.Text);
}

txtBoxart.ReadOnly = !chkManualBoxartLocation.Checked;
Expand Down Expand Up @@ -171,6 +171,7 @@ private void Go()
{
SdRoot = txtSdRoot.Text,
BoxartPath = txtBoxart.Text,
SettingsPath = _config.GetCorrectSettingsIniPath(txtSdRoot.Text),
BoxartWidth = (int) numWidth.Value,
BoxartHeight = (int) numHeight.Value,
KeepAspectRatio = chkKeepAspectRatio.Checked,
Expand Down Expand Up @@ -229,8 +230,9 @@ private void rbtCustom_CheckedChanged(object sender, EventArgs e)

private void rbtLarge_CheckedChanged(object sender, EventArgs e)
{
numWidth.Value = 210; // 200
numHeight.Value = 146; // 180

numWidth.Value = 168;
numHeight.Value = 130;
SetUx();
}

Expand All @@ -243,8 +245,8 @@ private void rbtDefault_CheckedChanged(object sender, EventArgs e)

private void rbtFullscreen_CheckedChanged(object sender, EventArgs e)
{
numWidth.Value = 256;
numHeight.Value = 192;
numWidth.Value = 208;
numHeight.Value = 143;
SetUx();
}

Expand Down
23 changes: 17 additions & 6 deletions TwilightBoxart/BoxartConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ public interface IAppConfig : ISharedConfig
{
string SdRoot { get; set; }
string BoxartPath { get; set; }
string SettingsPath { get; set; }
bool OverwriteExisting { get; set; }
}

public class BoxartConfig : IniSettings, IAppConfig
{
public string SdRoot { get; set; } = "";
public string BoxartPath { get; set; } = @"{sdroot}\_nds\TWiLightMenu\boxart";
public string SettingsPath { get; set; } = @"{sdroot}\_nds\TWiLightMenu\settings.ini";
public int BoxartWidth { get; set; } = 128;
public int BoxartHeight { get; set; } = 115;
public bool KeepAspectRatio { get; set; } = true;
Expand All @@ -34,7 +36,6 @@ public class BoxartConfig : IniSettings, IAppConfig
public int BoxartBorderThickness { get; set; }
public uint BoxartBorderColor { get; set; }
public bool DisableUpdates { get; set; } = false;

public const string MagicDir = "_nds";
public const string FileName = "TwilightBoxart.ini";
public const string Repository = "KirovAir/TwilightBoxart";
Expand All @@ -51,16 +52,26 @@ public void Load()
Load(FileName);
}

public string GetBoxartPath(string root = "")
public string GetCorrectBoxartPath(string root = "")
{
return GetCorrectPath(BoxartPath, root);
}

public string GetCorrectSettingsIniPath(string root = "")
{
return GetCorrectPath(SettingsPath, root);
}

public string GetCorrectPath(string pathMask, string root = "")
{
if (root == "")
{
root = SdRoot;
}

if (!BoxartPath.StartsWith("{sdroot}"))
if (!pathMask.StartsWith("{sdroot}"))
{
return BoxartPath;
return pathMask;
}

if (root.Contains(Path.DirectorySeparatorChar.ToString()))
Expand Down Expand Up @@ -90,9 +101,9 @@ public string GetBoxartPath(string root = "")
catch { }
}

return Path.Combine(root.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, BoxartPath.Replace("{sdroot}", "").TrimStart(Path.DirectorySeparatorChar));
return Path.Combine(root.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar, pathMask.Replace("{sdroot}", "").TrimStart(Path.DirectorySeparatorChar));
}

public static readonly List<string> SupportedFiles = new List<string>
{
".nes",
Expand Down
32 changes: 32 additions & 0 deletions TwilightBoxart/BoxartCrawler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class BoxartCrawler
};
private readonly HttpClient _httpClient;

private const long MaxCacheSizeForTwilight = 43000;

public BoxartCrawler(IProgress<string> progress = null)
{
_httpClient = new HttpClient(_handler);
Expand All @@ -38,6 +40,7 @@ public async Task DownloadArt(IAppConfig downloadConfig)
return;
}

long maxLength = 0;
foreach (var romFile in Directory.EnumerateFiles(downloadConfig.SdRoot, "*.*", SearchOption.AllDirectories))
{
if (_cancelToken.IsCancellationRequested)
Expand Down Expand Up @@ -92,6 +95,10 @@ public async Task DownloadArt(IAppConfig downloadConfig)
using (var fs = new FileStream(targetArtFile, FileMode.Create))
{
await result.Content.CopyToAsync(fs);
if (fs.Length > maxLength)
{
maxLength = fs.Length;
}
}

_progress?.Report("Got it!");
Expand All @@ -108,6 +115,18 @@ public async Task DownloadArt(IAppConfig downloadConfig)
}
}

if (maxLength >= MaxCacheSizeForTwilight)
{
try
{
FixTwilightSettings(downloadConfig.SettingsPath);
}
catch (Exception e)
{
_progress.Report($"Error while fixing TwilightMenu++ settings: {e}");
}
}

_progress?.Report("Finished scan.");
}
catch (Exception e)
Expand All @@ -116,6 +135,19 @@ public async Task DownloadArt(IAppConfig downloadConfig)
}
}

private void FixTwilightSettings(string path)
{
if (!File.Exists(path)) return;

var settings = File.ReadAllText(path);
if (!settings.Contains("CACHE_BOX_ART = 1")) return;

_progress?.Report("Detected large boxart size in output files. Disabling boxart cache in TwilightMenu++ settings so these will be displayed correctly.");

settings = settings.Replace("CACHE_BOX_ART = 1", "CACHE_BOX_ART = 0");
File.WriteAllText(path, settings);
}

public void Stop()
{
_cancelToken?.Cancel();
Expand Down

0 comments on commit a03c44a

Please sign in to comment.