Skip to content

Commit

Permalink
feat: make some studio messages are processed on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
DemoJameson committed Apr 2, 2022
1 parent 66be271 commit 4fdebe3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -16,11 +17,13 @@
namespace TAS.Communication;

public sealed class StudioCommunicationClient : StudioCommunicationBase {
public static StudioCommunicationClient Instance { get; private set; }
private static readonly ConcurrentQueue<Action> mainThreadActions = new();

private byte[] lastBindingsData = new byte[0];
private readonly List<Thread> threads = new();
private StudioCommunicationClient() { }
private StudioCommunicationClient(string target) : base(target) { }
public static StudioCommunicationClient Instance { get; private set; }

[Load]
private static void Load() {
Expand Down Expand Up @@ -81,6 +84,18 @@ public static StudioCommunicationClient RunExternal(string target) {
return client;
}

private static void AddWaitingAction(Action action) {
mainThreadActions.Enqueue(action);
}

public static void ExecuteWaitingActions() {
while (mainThreadActions.TryDequeue(out Action action)) {
if (Initialized) {
action.Invoke();
}
}
}

#region Read

protected override void ReadData(Message message) {
Expand Down Expand Up @@ -238,7 +253,7 @@ private void ProcessSendPath(byte[] data) {
path = path.Substring(2, path.Length - 2).Replace("\\", "/");
}

InputController.StudioTasFilePath = path;
AddWaitingAction(() => InputController.StudioTasFilePath = path);
}

private void ProcessHotkeyPressed(byte[] data) {
Expand Down
5 changes: 2 additions & 3 deletions CelesteTAS-EverestInterop/Source/TAS/Input/InputController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -14,14 +13,14 @@
namespace TAS.Input;

public class InputController {
private static readonly ConcurrentDictionary<string, FileSystemWatcher> watchers = new();
private static readonly Dictionary<string, FileSystemWatcher> watchers = new();
private static string studioTasFilePath = string.Empty;

public readonly SortedDictionary<int, List<Command>> Commands = new();
public readonly SortedDictionary<int, FastForward> FastForwards = new();
public readonly SortedDictionary<int, FastForward> FastForwardComments = new();
public readonly List<InputFrame> Inputs = new();
private readonly ConcurrentDictionary<string, byte> UsedFiles = new();
private readonly Dictionary<string, byte> UsedFiles = new();

private string checksum;
private int initializationFrameCount;
Expand Down
1 change: 1 addition & 0 deletions CelesteTAS-EverestInterop/Source/TAS/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static Manager() {

public static void Update() {
LastStates = States;
StudioCommunicationClient.ExecuteWaitingActions();
Hotkeys.Update();
Savestates.HandleSaveStates();
HandleFrameRates();
Expand Down

0 comments on commit 4fdebe3

Please sign in to comment.