Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Handle getting config values exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
avdynut committed Nov 3, 2019
1 parent e0b48ee commit e65ebb3
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
40 changes: 25 additions & 15 deletions AimpLyrics/AimpLyricsPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AimpLyricsPlugin : IAIMPPlugin
{
public const string Name = "AimpLyrics";
public const string Author = "Andrey Arekhva";
public const string Version = "1.1.1";
public const string Version = "1.1.2";
public const string Description = "Display lyrics for current playing song. Find lyrics in file, tag or Google";

private LyricsWindow _lyricsWindow;
Expand All @@ -35,27 +35,37 @@ public string InfoGet(AIMPPluginInfo info) =>

public void Initialize(IAIMPCore core)
{
Core = core;
try
{
Trace.WriteLine("Start Init");

AddMenuItem();
RegisterHook();
Core = core;

_lyricsWindow = new LyricsWindow();
_hook.FileInfoReceived += _lyricsWindow.UpdateSongInfo;
SetUpLogger();

_settings = new AimpLyricsPluginSettings();
AddMenuItem();
RegisterHook();

if (_settings.OpenWindowOnInitializing)
_hook.PlayerLoaded += OnPlayerLoaded;
_lyricsWindow = new LyricsWindow();
_hook.FileInfoReceived += _lyricsWindow.UpdateSongInfo;

if (_settings.RestoreWindowHeight)
_lyricsWindow.Height = _settings.WindowHeight;
_settings = new AimpLyricsPluginSettings();

RegisterOptions();
if (_settings.OpenWindowOnInitializing)
_hook.PlayerLoaded += OnPlayerLoaded;

SetUpLogger();
if (_settings.RestoreWindowHeight)
_lyricsWindow.Height = _settings.WindowHeight;

Trace.WriteLine($"Initialized AIMP Lyrics Plugin v{Version}-beta");
RegisterOptions();

Trace.WriteLine($"Initialized AIMP Lyrics Plugin v{Version}-beta");
}
catch (Exception ex)
{
Trace.WriteLine($"Init error: {ex}");
throw;
}
}

private void AddMenuItem()
Expand Down Expand Up @@ -188,7 +198,7 @@ public void SystemNotification(AIMPSystemNotification notification, object data)
public void FinalizePlugin()
{
if (_settings?.RestoreWindowHeight == true && _lyricsWindow != null)
_settings.WindowHeight = (int)_lyricsWindow.ActualHeight;
_settings.WindowHeight = _lyricsWindow.ActualHeight;

if (_hook != null)
{
Expand Down
2 changes: 2 additions & 0 deletions AimpLyrics/Api/IAIMPServiceConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public interface IAIMPServiceConfig
{
// Delete
void Delete(IAIMPString keyPath);

// Read
// returns E_FAIL when value is not found
double GetValueAsFloat(IAIMPString keyPath);
int GetValueAsInt32(IAIMPString keyPath);
long GetValueAsInt64(IAIMPString keyPath);
Expand Down
52 changes: 38 additions & 14 deletions AimpLyrics/Settings/AimpLyricsPluginSettings.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Aimp4.Api;
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

#nullable enable
namespace AimpLyrics.Settings
{
public class AimpLyricsPluginSettings : ILyricsPluginSettings
Expand Down Expand Up @@ -43,20 +43,20 @@ public bool RestoreWindowHeight
set => SetString(Convert.ToString(value));
}

private readonly int _defaultWindowHeight = 600;
public int WindowHeight
private readonly double _defaultWindowHeight = 600;
public double WindowHeight
{
get
{
int height = GetInt32();
double height = GetDouble();

if (height < 50 || height > 3000)
{
WindowHeight = height = _defaultWindowHeight;
}
return height;
}
set => SetInt32(value);
set => SetDouble(value);
}

private IAIMPServiceConfig Config => AimpLyricsPlugin.Core.GetService<IAIMPServiceConfig>();
Expand All @@ -69,21 +69,45 @@ private IAIMPString GetKeyPath(string key)

private string GetString([CallerMemberName] string key = "")
{
string value = null;
var aimpKeyPath = GetKeyPath(key);
var aimpValue = Config.GetValueAsString(aimpKeyPath);
string value = aimpValue.GetData();

aimpKeyPath?.ReleaseComObject();
aimpValue?.ReleaseComObject();
try
{
var aimpValue = Config.GetValueAsString(aimpKeyPath);
value = aimpValue.GetData();
aimpValue?.ReleaseComObject();
}
catch (Exception ex)
{
Trace.WriteLine($"GetString: {ex}");
}
finally
{
aimpKeyPath?.ReleaseComObject();
}

return value;
}

private int GetInt32([CallerMemberName] string key = "")
private double GetDouble([CallerMemberName] string key = "")
{
double value = 0;
var aimpKeyPath = GetKeyPath(key);
int value = Config.GetValueAsInt32(aimpKeyPath);
aimpKeyPath?.ReleaseComObject();

try
{
value = Config.GetValueAsFloat(aimpKeyPath);
}
catch (Exception ex)
{
Trace.WriteLine($"GetDouble: {ex}");
}
finally
{
aimpKeyPath?.ReleaseComObject();
}

return value;
}

Expand All @@ -98,10 +122,10 @@ private void SetString(string value, [CallerMemberName] string key = "")
aimpValue?.ReleaseComObject();
}

private void SetInt32(int value, [CallerMemberName] string key = "")
private void SetDouble(double value, [CallerMemberName] string key = "")
{
var aimpKeyPath = GetKeyPath(key);
Config.SetValueAsInt32(aimpKeyPath, value);
Config.SetValueAsFloat(aimpKeyPath, value);
aimpKeyPath?.ReleaseComObject();
}
}
Expand Down
2 changes: 1 addition & 1 deletion AimpLyrics/Settings/ILyricsPluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public interface ILyricsPluginSettings
{
bool OpenWindowOnInitializing { get; set; }
bool RestoreWindowHeight { get; set; }
int WindowHeight { get; set; }
double WindowHeight { get; set; }
}
}

0 comments on commit e65ebb3

Please sign in to comment.