2022-12-02 11:10:58 +01:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
using static CommonHelpers.Log;
|
|
|
|
|
|
|
|
|
|
namespace SteamController
|
|
|
|
|
{
|
|
|
|
|
public partial class Context : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public readonly TimeSpan UpdateResetInterval = TimeSpan.FromSeconds(1);
|
|
|
|
|
|
|
|
|
|
Thread? thread;
|
|
|
|
|
private bool threadRunning;
|
|
|
|
|
|
|
|
|
|
public int UpdatesPerSec { get; private set; }
|
|
|
|
|
|
2023-01-05 11:47:18 +01:00
|
|
|
public bool Start(int? startDelayMs = null)
|
2022-12-02 11:10:58 +01:00
|
|
|
{
|
|
|
|
|
if (thread is not null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-01-05 11:47:18 +01:00
|
|
|
UpdatesPerSec = 0;
|
2022-12-02 11:10:58 +01:00
|
|
|
threadRunning = true;
|
|
|
|
|
thread = new Thread(ThreadLoop);
|
2023-01-05 11:47:18 +01:00
|
|
|
thread.Start(startDelayMs);
|
2022-12-02 11:10:58 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-05 11:47:18 +01:00
|
|
|
private void ThreadLoop(object? startDelayMs)
|
2022-12-02 11:10:58 +01:00
|
|
|
{
|
2023-01-05 11:47:18 +01:00
|
|
|
if (startDelayMs is int)
|
|
|
|
|
{
|
|
|
|
|
ThreadSleep((int)startDelayMs);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-02 11:10:58 +01:00
|
|
|
var stopwatch = new Stopwatch();
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
int updates = 0;
|
|
|
|
|
var nextReset = stopwatch.Elapsed.Add(UpdateResetInterval);
|
|
|
|
|
|
2023-01-05 11:47:18 +01:00
|
|
|
X360.Start();
|
2023-02-10 11:05:23 +01:00
|
|
|
DS4.Start();
|
2023-01-05 11:47:18 +01:00
|
|
|
|
2022-12-02 11:10:58 +01:00
|
|
|
while (threadRunning)
|
|
|
|
|
{
|
|
|
|
|
if (nextReset < stopwatch.Elapsed)
|
|
|
|
|
{
|
|
|
|
|
UpdatesPerSec = updates;
|
|
|
|
|
nextReset = stopwatch.Elapsed.Add(UpdateResetInterval);
|
|
|
|
|
updates = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updates++;
|
|
|
|
|
Update();
|
|
|
|
|
Debug();
|
|
|
|
|
|
2022-12-02 11:21:08 +01:00
|
|
|
if (!Enabled || !Steam.Updated)
|
2022-12-02 11:10:58 +01:00
|
|
|
{
|
2023-01-05 11:47:18 +01:00
|
|
|
ThreadSleep(100);
|
2022-12-02 11:10:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-05 11:47:18 +01:00
|
|
|
|
|
|
|
|
X360.Stop();
|
2023-02-10 11:05:23 +01:00
|
|
|
DS4.Stop();
|
2022-12-02 11:10:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
threadRunning = false;
|
|
|
|
|
|
|
|
|
|
if (thread != null)
|
|
|
|
|
{
|
|
|
|
|
thread.Interrupt();
|
|
|
|
|
thread.Join();
|
|
|
|
|
thread = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-05 11:47:18 +01:00
|
|
|
|
|
|
|
|
private bool ThreadSleep(int delayMs)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(delayMs);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (ThreadInterruptedException)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-02 11:10:58 +01:00
|
|
|
}
|
|
|
|
|
}
|