Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue 29 refactor: issue 24 #30

Merged
merged 4 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions Classes/Recorders/LibObsRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ public override async void Start() {
}
else if (formattedMsg == "[game-capture: 'gameplay'] capture stopped") {
signalGCHookSuccess = false;

//TODO: Read video recording lenght from file or in another way.
RecordingService.lastVideoDuration = RecordingService.recordingElapsed;
}
}
}
Expand Down Expand Up @@ -301,6 +298,7 @@ private IntPtr GetVideoEncoder(string encoder) {
}

private void GetAvailableEncoders() {
SettingsService.LoadSettings(); //Hacky fix for weird first launch issue, should be investigated further.
UIntPtr idx = UIntPtr.Zero;
string id = "";
List<string> availableEncoders = new();
Expand Down Expand Up @@ -360,7 +358,7 @@ public override async Task<bool> StopRecording() {

Logger.WriteLine(string.Format("Session recording saved to {0}", videoSavePath));
Logger.WriteLine(string.Format("LibObs stopped recording {0} {1} [{2}]", session.Pid, session.GameTitle, bnum_allocs()));

RecordingService.lastVideoDuration = GetVideoDuration(videoSavePath);
try {
var t = await Task.Run(() => GetAllVideos(WebMessage.videoSortSettings.game, WebMessage.videoSortSettings.sortBy));
WebMessage.SendMessage(t);
Expand All @@ -370,7 +368,7 @@ public override async Task<bool> StopRecording() {
}

//Adding bookmarks
KeyboardHookService.Stop("/" + MakeValidFolderNameSimple(session.GameTitle) + "/" + videoNameTimeStamp + "-ses.mp4");
BookmarkService.ApplyBookmarkToSavedVideo("/" + MakeValidFolderNameSimple(session.GameTitle) + "/" + videoNameTimeStamp + "-ses.mp4");

return true;
}
Expand Down
31 changes: 2 additions & 29 deletions Classes/Services/BookmarkService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ namespace RePlays.Classes.Services
{
internal static class BookmarkService
{
private static Keys bookmarkKey = Keys.F8;
static List<int> bookmarks = new List<int>();
static int latestBookmarkKeyPress = 0;
static List<int> bookmarks = new();
static int latestBookmarkKeyPress;

public static void AddBookmark()
{
Expand All @@ -36,32 +35,6 @@ public static void ApplyBookmarkToSavedVideo(string videoName)
bookmarks.Clear();
}

public static void SetBookmarkKeyFromSettings()
{
//Get bookmark key
string[] keybind;
SettingsService.Settings.keybindings.TryGetValue("CreateBookmark", out keybind);
for (int i = 0; i < keybind.Length; i++)
{
Keys key = Keys.None;
Enum.TryParse(keybind[i], out key);
if (i == 0) bookmarkKey = key;
else bookmarkKey = key;

//TODO: Make it possible to use multiple keys
//else bookmarkKey |= key;
}
}

[DllImport("user32.dll")]
static public extern short GetKeyState(Keys nVirtKey);

public static bool IsPressingBookmark()
{
int state = GetKeyState(bookmarkKey);
if (state > 1 || state < -1) return true;
return false;
}

}
}
119 changes: 78 additions & 41 deletions Classes/Services/HotkeyService.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,92 @@
using RePlays.Utils;
using NHotkey.WindowsForms;
using System.Windows.Forms;
using RePlays.Services;
using RePlays.Utils;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using RePlays.Classes.Services.Hotkeys;

namespace RePlays.Services {
public static class HotkeyService {
public static string EditId = null;
namespace RePlays.Services
{
public static class HotkeyService
{
public delegate IntPtr CallbackDelegate(int Code, IntPtr W, IntPtr L);

public static void Start() {
foreach (var keybind in SettingsService.Settings.keybindings) {
RegisterHotkey(keybind.Key, keybind.Value);
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct KBDLLHookStruct
{
public Int32 vkCode;
public Int32 scanCode;
public Int32 flags;
public Int32 time;
public Int32 dwExtraInfo;
}

public static void Stop() {
foreach (var keybind in SettingsService.Settings.keybindings) {
HotkeyManager.Current.Remove(keybind.Key);
}
[DllImport("user32", CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr SetWindowsHookEx(int idHook, CallbackDelegate lpfn, int hInstance, int threadId);

[DllImport("user32", CallingConvention = CallingConvention.StdCall)]
private static extern bool UnhookWindowsHookEx(IntPtr idHook);

[DllImport("user32", CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
private static extern int GetCurrentThreadId();
public static string EditId = null;
private static List<Hotkey> _hotkeys = new();
private static IntPtr HookID;
static CallbackDelegate TheHookCB = null;

public static void Start()
{
//Create hotkeys
_hotkeys.Add(new BookmarkHotkey());
_hotkeys.Add(new RecordingHotkey());
//Create hook
TheHookCB = KeybHookProc;
HookID = SetWindowsHookEx(13, TheHookCB, 0, 0);
Logger.WriteLine("Loaded KeyboardHook...");
}

public static void RegisterHotkey(string name, string[] keys) {
Keys keybind = Keys.None;
public static void Stop()
{
_hotkeys.Clear();
UnhookWindowsHookEx(HookID);
Logger.WriteLine("Unloaded KeyboardHook...");
}

for (int i = 0; i < keys.Length; i++) {
Keys key = Keys.None;
Enum.TryParse(keys[i], out key);
private static IntPtr KeybHookProc(int Code, IntPtr W, IntPtr L)
{
if (Code < 0)
return CallNextHookEx(HookID, Code, W, L);

if(i == 0) keybind = key;
else keybind |= key;
try
{
KeyEvents kEvent = (KeyEvents)W;
if (kEvent == KeyEvents.KeyDown)
{
Keys vkCode = (Keys)Marshal.ReadInt32(L);
vkCode |= Control.ModifierKeys;
foreach (Hotkey h in _hotkeys)
{
if (vkCode == h.Keybind) h.Action();
}
}
}
catch (Exception e)
{
Logger.WriteLine("Error getting current keypress: " + e.ToString());
}

HotkeyManager.Current.AddOrReplace(name, keybind, (s, e) => {
switch (name) {
case "StartStopRecording": {
if (!RecordingService.IsRecording) {
Logger.WriteLine("Manual Start Recording");
RecordingService.StartRecording();
}
else {
Logger.WriteLine("Manual Stop Recording");
RecordingService.StopRecording();
}
}
break;
default:
Logger.WriteLine($"No hotkey event match for {name}");
break;
}
e.Handled = true;
});
Logger.WriteLine($"Registered Hotkey: {name} / [{string.Join(" | ", keys)}]");
return CallNextHookEx(HookID, Code, W, L);
}


public enum KeyEvents
{
KeyDown = 0x0100,
KeyUp = 0x0101
}
}
}
25 changes: 25 additions & 0 deletions Classes/Services/Hotkeys/BookmarkHotkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RePlays.Services;

namespace RePlays.Classes.Services.Hotkeys
{
public class BookmarkHotkey : Hotkey
{
public override void Action()
{
if (RecordingService.IsRecording) BookmarkService.AddBookmark();
}

protected override void SetKeybind()
{
string[] keybind;
SettingsService.Settings.keybindings.TryGetValue("CreateBookmark", out keybind);
_keybind = ParseKeys(keybind);
}
}
}
40 changes: 40 additions & 0 deletions Classes/Services/Hotkeys/Hotkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Web.WebView2.Core;

namespace RePlays.Classes.Services.Hotkeys
{
public abstract class Hotkey
{
protected Keys _keybind;
public Keys Keybind => _keybind;

protected Hotkey()
{
SetKeybind();
}

public static Keys ParseKeys(string[] keys)
{
Keys keybind = Keys.None;

for (int i = 0; i < keys.Length; i++)
{
Keys key;
Enum.TryParse(keys[i], out key);
keybind |= key;
}

return keybind;
}

public abstract void Action();

protected abstract void SetKeybind();
}
}
25 changes: 25 additions & 0 deletions Classes/Services/Hotkeys/RecordingHotkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RePlays.Services;

namespace RePlays.Classes.Services.Hotkeys
{
public class RecordingHotkey : Hotkey
{
public override void Action()
{
if (RecordingService.IsRecording) RecordingService.StopRecording();
else RecordingService.StartRecording();
}

protected override void SetKeybind()
{
string[] keybind;
SettingsService.Settings.keybindings.TryGetValue("StartStopRecording", out keybind);
_keybind = ParseKeys(keybind);
}
}
}
90 changes: 0 additions & 90 deletions Classes/Services/KeyboardHookService.cs

This file was deleted.

Loading