Skip to content

Commit

Permalink
updated for 3.14 live
Browse files Browse the repository at this point in the history
The bindings in the streamdeck plugin are automatically updated when changing bindings in Star Citizen options screen.
  • Loading branch information
mhwlng committed Aug 7, 2021
1 parent 55d2e55 commit 340c815
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 134 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

TODO :

* Test multiple language keyboards (only lightly tested 3.13 with US keyboard)

* Automatically update binding in streamdeck plugin when changing binding in Star Citizen options screen (now streamdeck.exe has to be restarted)
* Test multiple language keyboards (only lightly tested 3.14 with US keyboard)

* Add more logging to pluginlog.log

Expand All @@ -18,6 +16,8 @@ TODO :

This plugin gets the key bindings from the Star Citizen game files.

The bindings in the streamdeck plugin are automatically updated when changing bindings in Star Citizen options screen.

**The plugin does not contain any button images or ready made streamdeck profiles.**

Credit goes to https://github.com/SCToolsfactory/SCJMapper-V2 for all the code to get the defaultProfile.xml from the pk4 file etc.
Expand Down
104 changes: 104 additions & 0 deletions starcitizen/FifoExecution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System.Collections.Generic;
using System.Threading;

namespace starcitizen
{
//https://msdn.microsoft.com/en-us/magazine/dd419664.aspx
/*
example :
public static FifoExecution reportjob = new FifoExecution(); // language is changed for thread
reportjob.QueueUserWorkItem(SensorReportEmailCallback, sensorsiteparam);
private static void SensorReportEmailCallback(Object threadContext)
{
SensorReportEmailCallbackParam param = (SensorReportEmailCallbackParam)threadContext;
}
*/
internal class WorkItem
{
public WaitCallback Callback;
public object State;
public ExecutionContext Context;

private static ContextCallback _contextCallback = s =>
{
var item = (WorkItem)s;
item.Callback(item.State);
};

public void Execute()
{
if (Context != null)
ExecutionContext.Run(Context, _contextCallback, this);
else Callback(State);
}
}

public class FifoExecution
{
private Queue<WorkItem> _workItems = new Queue<WorkItem>();
private bool _delegateQueuedOrRunning = false;

public void QueueUserWorkItem(WaitCallback callback, object state)
{
var item = new WorkItem
{
Callback = callback,
State = state,
Context = ExecutionContext.Capture()
};
lock (_workItems)
{
_workItems.Enqueue(item);
if (!_delegateQueuedOrRunning)
{
_delegateQueuedOrRunning = true;
ThreadPool.UnsafeQueueUserWorkItem(ProcessQueuedItems, null);
}
}
}

/*
private void ProcessQueuedItems(object ignored) {
while (true) {
WorkItem item;
lock (_workItems) {
if (_workItems.Count == 0) {
_delegateQueuedOrRunning = false;
break;
}
item = _workItems.Dequeue();
}
try { item.Execute(); }
catch {
ThreadPool.UnsafeQueueUserWorkItem(ProcessQueuedItems,
null);
throw;
}
}
}
*/

private void ProcessQueuedItems(object ignored)
{
WorkItem item;
lock (_workItems)
{
if (_workItems.Count == 0)
{
_delegateQueuedOrRunning = false;
return;
}
item = _workItems.Dequeue();
}
try { item.Execute(); }
finally
{
ThreadPool.UnsafeQueueUserWorkItem(ProcessQueuedItems,
null);
}
}
}
}
121 changes: 109 additions & 12 deletions starcitizen/Program.cs
Original file line number Diff line number Diff line change
@@ -1,42 +1,139 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;
using BarRaider.SdTools;
using p4ktest.SC;
using SCJMapper_V2.SC;

namespace starcitizen
{
class Program
public class KeyBindingFileEvent : EventArgs
{
public static DProfileReader dpReader = new DProfileReader(); // we may read a profile

static void Main(string[] args)
}

public class KeyBindingWatcher : FileSystemWatcher
{
public event EventHandler KeyBindingUpdated;

protected KeyBindingWatcher()
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Init Star Citizen");

}

public KeyBindingWatcher(string path, string fileName)
{
Filter = fileName;
NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite;
Path = path;
}

public virtual void StartWatching()
{
if (EnableRaisingEvents)
{
return;
}

Changed -= UpdateStatus;
Changed += UpdateStatus;

EnableRaisingEvents = true;
}

public virtual void StopWatching()
{
try
{
if (EnableRaisingEvents)
{
Changed -= UpdateStatus;

EnableRaisingEvents = false;
}
}
catch (Exception e)
{
Trace.TraceError($"Error while stopping Status watcher: {e.Message}");
Trace.TraceInformation(e.StackTrace);
}
}

protected void UpdateStatus(object sender, FileSystemEventArgs e)
{
Thread.Sleep(50);

KeyBindingUpdated?.Invoke(this, EventArgs.Empty);
}

SCFiles.Instance.UpdatePack(); // update game files

var profile = SCDefaultProfile.DefaultProfile();
}
class Program
{
public static FifoExecution keywatcherjob = new FifoExecution();

public static KeyBindingWatcher KeyBindingWatcher;

public static DProfileReader dpReader = new DProfileReader();

public static string profile;

public static void HandleKeyBindingEvents(object sender, object evt)
{
Logger.Instance.LogMessage(TracingLevel.INFO, $"Reloading Key Bindings");

keywatcherjob.QueueUserWorkItem(GetKeyBindings, null);
}


private static void GetKeyBindings(Object threadContext)
{
if (KeyBindingWatcher != null)
{
KeyBindingWatcher.StopWatching();
KeyBindingWatcher.Dispose();
KeyBindingWatcher = null;
}

var actionmaps = SCDefaultProfile.ActionMaps();
// load stuff
var actionmaps = SCDefaultProfile.ActionMaps();

dpReader.fromXML(profile);
dpReader = new DProfileReader();

dpReader.fromActionProfile(actionmaps);
dpReader.fromXML(profile);

dpReader.Actions();
dpReader.fromActionProfile(actionmaps);

dpReader.Actions();

dpReader.CreateDropdownTemplate();

dpReader.CreateCsv();

Logger.Instance.LogMessage(TracingLevel.INFO, "monitoring key binding file" );
KeyBindingWatcher = new KeyBindingWatcher(SCPath.SCClientProfilePath, "actionmaps.xml");
KeyBindingWatcher.KeyBindingUpdated += HandleKeyBindingEvents;
KeyBindingWatcher.StartWatching();
}


static void Main(string[] args)
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Init Star Citizen");

try
{
SCFiles.Instance.UpdatePack(); // update game files

dpReader.CreateDropdownTemplate();
profile = SCDefaultProfile.DefaultProfile();

dpReader.CreateCsv();
GetKeyBindings(null);

}
catch (Exception ex)
Expand Down
4 changes: 2 additions & 2 deletions starcitizen/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
// 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("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
Loading

0 comments on commit 340c815

Please sign in to comment.