Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sabihoshi committed Oct 16, 2021
1 parent 8cf87c2 commit f3deb52
Show file tree
Hide file tree
Showing 20 changed files with 568 additions and 559 deletions.
50 changes: 25 additions & 25 deletions GenshinLyreMidiPlayer.Data/GenshinLyreMidiPlayer.Data.csproj
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Melanchall.DryWetMidi" Version="5.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="5.0.11" />
<PackageReference Include="Stylet" Version="1.3.6" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Melanchall.DryWetMidi" Version="5.2.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="5.0.11" />
<PackageReference Include="Stylet" Version="1.3.6" />
</ItemGroup>

<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
<AutoGen>True</AutoGen>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
<AutoGen>True</AutoGen>
</Compile>
</ItemGroup>

<ItemGroup>
<None Update="Properties\Settings.settings">
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
<Generator>PublicSettingsSingleFileGenerator</Generator>
</None>
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
<Generator>PublicSettingsSingleFileGenerator</Generator>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
namespace GenshinLyreMidiPlayer.Data.Notification
{
public class PlayTimerNotification
{
}
public class PlayTimerNotification { }
}
4 changes: 2 additions & 2 deletions GenshinLyreMidiPlayer.Data/Properties/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace GenshinLyreMidiPlayer.Data.Properties
// The SettingsSaving event is raised before the setting values are saved.
public sealed partial class Settings
{
protected override void OnPropertyChanged(object sender, PropertyChangedEventArgs e) => Save();

protected override void OnSettingsLoaded(object sender, SettingsLoadedEventArgs e)
{
if (Default.UpgradeRequired)
Expand All @@ -19,7 +21,5 @@ protected override void OnSettingsLoaded(object sender, SettingsLoadedEventArgs
Default.Save();
}
}

protected override void OnPropertyChanged(object sender, PropertyChangedEventArgs e) => Save();
}
}
125 changes: 65 additions & 60 deletions GenshinLyreMidiPlayer.Data/Properties/Settings.settings
Original file line number Diff line number Diff line change
@@ -1,62 +1,67 @@
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="GenshinLyreMidiPlayer.Data.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="SqliteConnection" Type="(Connection string)" Scope="Application">
<DesignTimeValue Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;SerializableConnectionString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;ConnectionString&gt;history.db&lt;/ConnectionString&gt;
&lt;ProviderName /&gt;
&lt;/SerializableConnectionString&gt;</DesignTimeValue>
<Value Profile="(Default)">history.db</Value>
</Setting>
<Setting Name="LicenseUri" Type="(Web Service URL)" Scope="Application">
<Value Profile="(Default)">https://github.com/sabihoshi/GenshinLyreMidiPlayer/blob/main/LICENSE.md</Value>
</Setting>
<Setting Name="ThirdPartyLicenseUri" Type="(Web Service URL)" Scope="Application">
<Value Profile="(Default)">https://github.com/sabihoshi/GenshinLyreMidiPlayer/blob/main/THIRD-PARTY-NOTICES.md</Value>
</Setting>
<Setting Name="AutoCheckUpdates" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="IncludeBetaUpdates" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="HoldNotes" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="TransposeNotes" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="MergeNotes" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="UseSpeakers" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="KeyOffset" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="AppTheme" Type="System.Int32" Scope="User">
<Value Profile="(Default)">-1</Value>
</Setting>
<Setting Name="SelectedLayout" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="SelectedSpeed" Type="System.Int32" Scope="User">
<Value Profile="(Default)">3</Value>
</Setting>
<Setting Name="SelectedTransition" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="MergeMilliseconds" Type="System.UInt32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="GenshinLocation" Type="System.String" Scope="User">
<Value Profile="(Default)">GenshinImpact.exe</Value>
</Setting>
</Settings>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)"
GeneratedClassNamespace="GenshinLyreMidiPlayer.Data.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="SqliteConnection" Type="(Connection string)" Scope="Application">
<DesignTimeValue Profile="(Default)">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;SerializableConnectionString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;ConnectionString&gt;history.db&lt;/ConnectionString&gt;
&lt;ProviderName /&gt;
&lt;/SerializableConnectionString&gt;
</DesignTimeValue>
<Value Profile="(Default)">history.db</Value>
</Setting>
<Setting Name="LicenseUri" Type="(Web Service URL)" Scope="Application">
<Value Profile="(Default)">https://github.com/sabihoshi/GenshinLyreMidiPlayer/blob/main/LICENSE.md</Value>
</Setting>
<Setting Name="ThirdPartyLicenseUri" Type="(Web Service URL)" Scope="Application">
<Value Profile="(Default)">
https://github.com/sabihoshi/GenshinLyreMidiPlayer/blob/main/THIRD-PARTY-NOTICES.md
</Value>
</Setting>
<Setting Name="AutoCheckUpdates" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="IncludeBetaUpdates" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="HoldNotes" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="TransposeNotes" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="MergeNotes" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="UseSpeakers" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="KeyOffset" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="AppTheme" Type="System.Int32" Scope="User">
<Value Profile="(Default)">-1</Value>
</Setting>
<Setting Name="SelectedLayout" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="SelectedSpeed" Type="System.Int32" Scope="User">
<Value Profile="(Default)">3</Value>
</Setting>
<Setting Name="SelectedTransition" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="MergeMilliseconds" Type="System.UInt32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
<Setting Name="UpgradeRequired" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="GenshinLocation" Type="System.String" Scope="User">
<Value Profile="(Default)">GenshinImpact.exe</Value>
</Setting>
</Settings>
</SettingsFile>
40 changes: 20 additions & 20 deletions GenshinLyreMidiPlayer.WPF/Core/LyrePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public enum Transpose
83 // B5
};

public static bool TryGetKey(this Layout layout, int noteId, out VirtualKeyCode key)
{
var keys = GetLayout(layout);
return TryGetKey(keys, noteId, out key);
}

public static bool TryGetKey(this IEnumerable<VirtualKeyCode> keys, int noteId, out VirtualKeyCode key)
{
var keyIndex = LyreNotes.IndexOf(noteId);
key = keys.ElementAtOrDefault(keyIndex);

return keyIndex != -1;
}

public static int TransposeNote(int noteId,
Transpose direction = Transpose.Ignore)
{
Expand All @@ -69,9 +83,11 @@ public static int TransposeNote(int noteId,
}
}

public static void PlayNote(int noteId, Layout selectedLayout)
public static void InteractNote(int noteId, Layout selectedLayout,
Func<VirtualKeyCode, IKeyboardSimulator> action)
{
InteractNote(noteId, selectedLayout, Input.Keyboard.KeyPress);
if (selectedLayout.TryGetKey(noteId, out var key))
action.Invoke(key);
}

public static void NoteDown(int noteId, Layout selectedLayout)
Expand All @@ -84,25 +100,9 @@ public static void NoteUp(int noteId, Layout selectedLayout)
InteractNote(noteId, selectedLayout, Input.Keyboard.KeyUp);
}

public static void InteractNote(int noteId, Layout selectedLayout,
Func<VirtualKeyCode, IKeyboardSimulator> action)
{
if (selectedLayout.TryGetKey(noteId, out var key))
action.Invoke(key);
}

public static bool TryGetKey(this Layout layout, int noteId, out VirtualKeyCode key)
{
var keys = GetLayout(layout);
return TryGetKey(keys, noteId, out key);
}

public static bool TryGetKey(this IEnumerable<VirtualKeyCode> keys, int noteId, out VirtualKeyCode key)
public static void PlayNote(int noteId, Layout selectedLayout)
{
var keyIndex = LyreNotes.IndexOf(noteId);
key = keys.ElementAtOrDefault(keyIndex);

return keyIndex != -1;
InteractNote(noteId, selectedLayout, Input.Keyboard.KeyPress);
}
}
}
32 changes: 16 additions & 16 deletions GenshinLyreMidiPlayer.WPF/Core/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ public static class WindowHelper
private static string GenshinProcessName
=> Path.GetFileNameWithoutExtension(Settings.Default.GenshinLocation)!;

private static IntPtr? FindWindowByProcessName(string processName)
{
var process = Process.GetProcessesByName(processName);
return process.FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero)?.MainWindowHandle;
}

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);

public static bool EnsureGameOnTop()
{
var genshinWindow = FindWindowByProcessName(GenshinProcessName);
Expand All @@ -41,17 +29,29 @@ public static bool EnsureGameOnTop()
return GetForegroundWindow().Equals(genshinWindow);
}

public static bool IsGameFocused()
{
var genshinWindow = FindWindowByProcessName(GenshinProcessName);
return genshinWindow != null &&
IsWindowFocused((IntPtr) genshinWindow);
}

private static bool IsWindowFocused(IntPtr windowPtr)
{
var hWnd = GetForegroundWindow();
return hWnd.Equals(windowPtr);
}

public static bool IsGameFocused()
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();

private static IntPtr? FindWindowByProcessName(string processName)
{
var genshinWindow = FindWindowByProcessName(GenshinProcessName);
return genshinWindow != null &&
IsWindowFocused((IntPtr) genshinWindow);
var process = Process.GetProcessesByName(processName);
return process.FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero)?.MainWindowHandle;
}

[DllImport("user32.dll")]
private static extern void SwitchToThisWindow(IntPtr hWnd, bool fUnknown);
}
}
2 changes: 1 addition & 1 deletion GenshinLyreMidiPlayer.WPF/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<PropertyChanged />
<PropertyChanged />
</Weavers>
Loading

0 comments on commit f3deb52

Please sign in to comment.