Skip to content

Commit

Permalink
- Added SM exe setup window
Browse files Browse the repository at this point in the history
- App.xaml.cs: Replaced Environment.Exit with Shutdown
- Removed JetBrains annotations
- SMATaskbarIcon: Fixed null issue when right clicking the tray icon
- NativeDataCfg.json: Updated versions number
- InjectLib: Better exception handling
- Several minor additions, bug fixes, cleanup
  • Loading branch information
alexis- committed Feb 8, 2020
1 parent dc9e202 commit 5d2293f
Show file tree
Hide file tree
Showing 74 changed files with 1,384 additions and 1,891 deletions.
89 changes: 0 additions & 89 deletions SuperMemoAssistant.sln

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions SuperMemoAssistant.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,5 @@ public static $name$ Instance { get; } = new $name$();</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=hkcu/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Hotkey/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=marshalled/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mems/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Plugin_0027s/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
116 changes: 99 additions & 17 deletions src/AppHosts/SuperMemoAssistant/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
// DEALINGS IN THE SOFTWARE.
//
//
// Created On: 2020/01/22 09:58
// Modified On: 2020/01/22 17:31
// Modified On: 2020/02/03 16:41
// Modified By: Alexis

#endregion
Expand All @@ -36,14 +35,17 @@
using System.Windows;
using Anotar.Serilog;
using CommandLine;
using Forge.Forms;
using Hardcodet.Wpf.TaskbarNotification;
using SuperMemoAssistant.Exceptions;
using SuperMemoAssistant.Interop;
using SuperMemoAssistant.Interop.SuperMemo.Core;
using SuperMemoAssistant.PluginHost;
using SuperMemoAssistant.Services.UI.Extensions;
using SuperMemoAssistant.SMA.Configs;
using SuperMemoAssistant.SMA.Utils;
using SuperMemoAssistant.Sys.IO;
using SuperMemoAssistant.Sys.IO.Devices;
using SuperMemoFinderUtil = SuperMemoAssistant.SMA.Utils.SuperMemoFinder;

namespace SuperMemoAssistant
{
Expand All @@ -65,8 +67,10 @@ protected override void OnExit(ExitEventArgs e)
{
_taskbarIcon?.Dispose();

SuperMemoAssistant.SMA.Core.Logger?.Shutdown();
SMA.Core.Logger?.Shutdown();
#pragma warning disable CS0436 // Type conflicts with imported type
ModuleInitializer.SentryInstance?.Dispose();
#pragma warning restore CS0436 // Type conflicts with imported type

base.OnExit(e);
}
Expand All @@ -85,27 +89,54 @@ private async void Application_Startup(object o,
await LoadApp(parsed.Value);

else
Environment.Exit(HostConst.ExitParameters);
Shutdown(HostConst.ExitParameters);
}

private async Task LoadApp(SMAParameters args)
{
_taskbarIcon = (TaskbarIcon)FindResource("TbIcon");

//
// Make sure assemblies are available, and SMA is installed in "%AppData%\SuperMemoAssistant"
if (CheckAssemblies(out var errMsg) == false || CheckSMALocation(out errMsg) == false)
{
LogTo.Warning(errMsg);
await Show.Window().For(new Alert(errMsg, "Error"));
await errMsg.ErrorMsgBox();

Shutdown();
Shutdown(1);
return;
}

//
// Load system configs
if (await LoadConfigs(out var nativeDataCfg, out var startupCfg) == false)
return;

//
// Make sure SuperMemo exe path is correct. Prompt user to input the path otherwise.
if (ShouldFindSuperMemo(startupCfg, nativeDataCfg))
{
var smFinder = new SuperMemoFinder(nativeDataCfg, startupCfg);
smFinder.ShowDialog();

if (smFinder.DialogResult == null || smFinder.DialogResult == false)
{
LogTo.Warning("No valid SM executable file path defined. SMA cannot run.");

Shutdown(1);
return;
}
}

//
// (Optional) Start the debug Key logger (logs key strokes with modifiers, e.g. ctrl, alt, ..)
if (args.KeyLogger)
SuperMemoAssistant.SMA.Core.KeyboardHotKey.MainCallback = LogHotKeys;
SMA.Core.KeyboardHotKey.MainCallback = LogHotKeys;

//
// Determine which collection to open
SMCollection smCollection = null;
var selectionWdw = new CollectionSelectionWindow();
var selectionWdw = new CollectionSelectionWindow(startupCfg);

// Try to open command line collection, if one was passed
if (args.CollectionKnoPath != null && selectionWdw.ValidateSuperMemoPath())
Expand All @@ -124,18 +155,15 @@ private async Task LoadApp(SMAParameters args)
smCollection = selectionWdw.Collection;
}

// If a collection was selected, start SMA
//
// If a collection was defined, start SMA
if (smCollection != null)
{
SuperMemoAssistant.SMA.Core.SMA.OnSMStoppedEvent += Instance_OnSMStoppedEvent;
SMA.Core.SMA.OnSMStoppedEvent += Instance_OnSMStoppedEvent;

if (await SuperMemoAssistant.SMA.Core.SMA.Start(smCollection).ConfigureAwait(true) == false)
if (await SMA.Core.SMA.Start(nativeDataCfg, startupCfg, smCollection).ConfigureAwait(true) == false)
{
await Show.Window().For(
new Alert(
$"SMA failed to start. Please check the logs in '{SMAFileSystem.LogDir.FullPath}' for details.",
"Error")
);
await $"SMA failed to start. Please check the logs in '{SMAFileSystem.LogDir.FullPath}' for details.".ErrorMsgBox();
Shutdown();
}
}
Expand All @@ -151,6 +179,16 @@ private Task Instance_OnSMStoppedEvent(object sender,
return Dispatcher.InvokeAsync(Shutdown).Task;
}

private bool ShouldFindSuperMemo(StartupCfg startupCfg, NativeDataCfg nativeDataCfg)
{
return string.IsNullOrWhiteSpace(startupCfg.SMBinPath)
|| SuperMemoFinderUtil.CheckSuperMemoExecutable(
nativeDataCfg,
startupCfg.SMBinPath,
out _,
out _) == false;
}

private bool CheckSMALocation(out string error)
{
var smaExeFile = new FilePath(Assembly.GetExecutingAssembly().Location);
Expand Down Expand Up @@ -179,6 +217,50 @@ private void LogHotKeys(HotKey hk)
LogTo.Debug($"Key pressed: {hk}");
}

private Task<bool> LoadConfigs(out NativeDataCfg nativeDataCfg, out StartupCfg startupCfg)
{
nativeDataCfg = LoadNativeDataConfig().Result;
startupCfg = LoadStartupConfig().Result;

if (nativeDataCfg == null || startupCfg == null)
{
Shutdown(1);
return Task.FromResult(false);
}

return Task.FromResult(true);
}

private async Task<StartupCfg> LoadStartupConfig()
{
try
{
return await SMA.Core.Configuration.Load<StartupCfg>()
.ConfigureAwait(false) ?? new StartupCfg();
}
catch (SMAException)
{
await "Failed to open StartupCfg.json. Make sure file is unlocked and try again.".ErrorMsgBox();

return null;
}
}

private async Task<NativeDataCfg> LoadNativeDataConfig()
{
try
{
return await SMA.Core.Configuration.Load<NativeDataCfg>(SMAFileSystem.AppRootDir)
.ConfigureAwait(false);
}
catch (SMAException)
{
await "Failed to load native data config file.".ErrorMsgBox();

return null;
}
}

#endregion
}
}
18 changes: 17 additions & 1 deletion src/AppHosts/SuperMemoAssistant/CollectionSelectionWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
KeyDown="Window_KeyDown">
<Window.Resources>
<ResourceDictionary>

<CollectionViewSource x:Key='SortedCollections' Source="{Binding SavedCollections}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="LastOpen" Direction="Descending" />
Expand Down Expand Up @@ -72,6 +71,23 @@
<EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.Style>
<Style TargetType="{x:Type ListBox}">
<Style.Triggers>
<!-- ReSharper disable once Xaml.BindingWithContextNotResolved -->
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}"
Value="0">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBlock>No collection yet :-( Add one by pressing the "Browse" button.</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.Style>
</ListBox>

<Grid Grid.Row="1">
Expand Down
42 changes: 15 additions & 27 deletions src/AppHosts/SuperMemoAssistant/CollectionSelectionWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
using Forge.Forms;
using MahApps.Metro.Controls;
using Microsoft.Win32;
using SuperMemoAssistant.Exceptions;
using SuperMemoAssistant.Extensions;
using SuperMemoAssistant.Interop.SuperMemo.Core;
using SuperMemoAssistant.SMA.Configs;
Expand All @@ -53,7 +52,7 @@ public partial class CollectionSelectionWindow : MetroWindow
{
#region Properties & Fields - Non-Public

private readonly StartupCfg _config;
private readonly StartupCfg _startupCfg;

#endregion

Expand All @@ -62,21 +61,10 @@ public partial class CollectionSelectionWindow : MetroWindow

#region Constructors

public CollectionSelectionWindow()
public CollectionSelectionWindow(StartupCfg startupCfg)
{
try
{
_config = SuperMemoAssistant.SMA.Core.Configuration.Load<StartupCfg>().Result ?? new StartupCfg();
}
catch (SMAException)
{
Forge.Forms.Show.Window().For(new Alert("Failed to open StartupCfg.json. Make sure file is unlocked and try again.", "Error"));

Environment.Exit(1);
return;
}

SavedCollections = _config.Collections;
_startupCfg = startupCfg;
SavedCollections = startupCfg.Collections;

InitializeComponent();

Expand Down Expand Up @@ -107,11 +95,11 @@ public CollectionSelectionWindow()

public bool ValidateSuperMemoPath()
{
if (new FilePath(_config.SMBinPath).Exists() == false)
if (new FilePath(_startupCfg.SMBinPath).Exists() == false)
{
Forge.Forms.Show.Window().For(
new Alert(
$"Invalid file path for sm executable file: '{_config.SMBinPath}' could not be found.",
$"Invalid file path for sm executable file: '{_startupCfg.SMBinPath}' could not be found.",
"Error")
);
return false;
Expand Down Expand Up @@ -162,7 +150,7 @@ private void DeleteCollection(SMCollection collection)

private void SaveConfig()
{
SuperMemoAssistant.SMA.Core.Configuration.Save<StartupCfg>(_config).Wait();
SuperMemoAssistant.SMA.Core.Configuration.Save<StartupCfg>(_startupCfg).Wait();
}

private void OpenSelectedCollection()
Expand Down Expand Up @@ -205,13 +193,13 @@ private void btnBrowse_Click(object sender,
if (filePath != null)
{
var newCollection = CreateCollection(filePath);
var duplicate = _config.Collections.FirstOrDefault(c => c == newCollection);
var duplicate = _startupCfg.Collections.FirstOrDefault(c => c == newCollection);

if (duplicate != null)
duplicate.LastOpen = DateTime.Now;

else
_config.Collections.Add(newCollection);
_startupCfg.Collections.Add(newCollection);

SaveConfig();

Expand All @@ -220,6 +208,11 @@ private void btnBrowse_Click(object sender,
}
}

private void ListBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
OpenSelectedCollection();
}

private void btnOpen_Click(object sender,
RoutedEventArgs e)
{
Expand All @@ -229,7 +222,7 @@ private void btnOpen_Click(object sender,
private void BtnOptions_Click(object sender,
RoutedEventArgs e)
{
Forge.Forms.Show.Window().For<StartupCfg>(_config).Wait();
Forge.Forms.Show.Window().For<StartupCfg>(_startupCfg).Wait();

SaveConfig();
}
Expand All @@ -251,11 +244,6 @@ private void CollectionSelectionWindow_Loaded(object sender,
lbCollections.SelectFirstItem();
}

private void ListBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
OpenSelectedCollection();
}

#endregion
}
}
1 change: 1 addition & 0 deletions src/AppHosts/SuperMemoAssistant/FodyWeavers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Anotar.Serilog />
<ModuleInit />
<PropertyChanged />
</Weavers>
29 changes: 29 additions & 0 deletions src/AppHosts/SuperMemoAssistant/FodyWeavers.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@
<xs:element name="Weavers">
<xs:complexType>
<xs:all>
<xs:element name="PropertyChanged" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:attribute name="InjectOnPropertyNameChanged" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to control if the On_PropertyName_Changed feature is enabled.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="EventInvokerNames" type="xs:string">
<xs:annotation>
<xs:documentation>Used to change the name of the method that fires the notify event. This is a string that accepts multiple values in a comma separated form.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="CheckForEquality" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to control if equality checks should be inserted. If false, equality checking will be disabled for the project.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="CheckForEqualityUsingBaseEquals" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to control if equality checks should use the Equals method resolved from the base class.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="UseStaticEqualsFromBase" type="xs:boolean">
<xs:annotation>
<xs:documentation>Used to control if equality checks should use the static Equals method resolved from the base class.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="ModuleInit" minOccurs="0" maxOccurs="1" type="xs:anyType" />
<xs:element name="Anotar.Serilog" minOccurs="0" maxOccurs="1" type="xs:anyType" />
</xs:all>
Expand Down
4 changes: 2 additions & 2 deletions src/AppHosts/SuperMemoAssistant/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.2.17")]
[assembly: AssemblyFileVersion("2.0.2.17")]
[assembly: AssemblyVersion("2.0.2.67")]
[assembly: AssemblyFileVersion("2.0.2.67")]

[assembly: AssemblyInformationalVersion("2.0.2")]
Loading

0 comments on commit 5d2293f

Please sign in to comment.