-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
219 lines (192 loc) · 8.78 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using OBSWebsocketDotNet;
using MemoryUtil.ComponentUtil;
using System.Reflection;
using Microsoft.VisualBasic;
namespace Auto_Recorder
{
class MainProgram
{
public static readonly MainForm MainForm = new MainForm();
private static readonly OBSWebsocket OBS = new OBSWebsocket();
private static readonly GameManager game = new GameManager();
private static readonly System.Timers.Timer update_timer = new System.Timers.Timer();
private static EventHandler OnStartRecording;
private static EventHandler OnStopRecording;
private static void Main()
{
Application.EnableVisualStyles();
if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
{
MessageBox.Show("Another instance of this program is already running.\nOnly one instance of this application is allowed.", "ASRT Auto Recorder", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(0);
}
OnStartRecording += StartRecording;
OnStopRecording += StopRecording;
OBS.WSTimeout = TimeSpan.FromMilliseconds(500);
update_timer.AutoReset = false;
update_timer.Interval = 15;
update_timer.Elapsed += AutoRecordTask;
update_timer.Start();
Application.Run(MainForm);
}
private static void AutoRecordTask(object sender, EventArgs e)
{
// Check if connected to the game
if (!game.IsHooked()) game.HookGameProcess();
// Check again if connected. If not, exit the task
if (!game.IsHooked())
{
OBSMessages.FailedToHookGameProcess();
OBSMessages.NotRecording();
update_timer.Start();
return;
}
// Now try to connect to OBS
if (!OBS.IsConnected) ConnectToOBS();
// And recheck if connected
if (!OBS.IsConnected)
{
OBSMessages.FailedToHookOBS();
OBSMessages.NotRecording();
update_timer.Start();
return;
}
// Now check for OBS auth
CheckOBSAuth();
if (!OBS.IsConnected)
{
OBSMessages.FailedToHookOBS();
OBSMessages.NotRecording();
update_timer.Start();
return;
}
// At this point both OBS and the game should have been hooked
OBSMessages.AutoRecordingEnabled();
// Launch the main update logic
UpdateLogic();
UpdateStrings();
update_timer.Start();
}
private static void UpdateLogic()
{
game.UpdateMemoryWatchers();
switch (OBS.GetStreamingStatus().IsRecording)
{
case true:
if (!game.watchers.LoadState.Current)
{
OnStopRecording?.Invoke(null, EventArgs.Empty);
}
else if (game.watchers.RaceState.Changed && (game.watchers.RaceState.Old == 18 || game.watchers.RaceState.Old == 19 || game.watchers.RaceState.Old == 22))
{
OnStopRecording?.Invoke(null, EventArgs.Empty);
}
else if (game.watchers.RaceState.Current != 9 && game.watchers.RaceState.Current != 11 && game.watchers.RaceState.Old == 9)
{
OnStopRecording?.Invoke(null, EventArgs.Empty);
}
break;
case false:
if (game.watchers.LoadState.Current && game.watchers.RaceState.Current == 9)
{
OnStartRecording?.Invoke(null, EventArgs.Empty);
}
else if (game.watchers.RaceState.Current == 8 && game.watchers.RaceState.Changed)
{
OnStartRecording?.Invoke(null, EventArgs.Empty);
}
break;
}
}
private static void UpdateStrings()
{
switch (OBS.GetStreamingStatus().IsRecording)
{
case true: OBSMessages.Recording(); break;
case false: OBSMessages.NotRecording(); break;
}
}
private static void ConnectToOBS()
{
try { OBS.Connect("ws://127.0.0.1:4444", ""); } catch { }
}
private static void CheckOBSAuth()
{
bool AuthConnected = false;
do
{
try
{
OBS.GetStreamingStatus();
AuthConnected = true;
}
catch
{
var question = Interaction.InputBox("OBS WebSocket requires a password.\n\nPlease inpput the password and click OK\nor click \"Cancel\" to exit the program.", "TSR Auto Recorder", "password");
if (question == "") Environment.Exit(0);
if (!OBS.IsConnected) break;
try { OBS.Authenticate(question, OBS.GetAuthInfo()); } catch (AuthFailureException) { }
}
} while (!AuthConnected);
}
private static void StartRecording(object sender, EventArgs e)
{
if (!OBS.GetRecordingStatus().IsRecording) OBS.StartRecording();
}
private static void StopRecording(object sender, EventArgs e)
{
if (OBS.GetRecordingStatus().IsRecording) OBS.StopRecording();
}
}
static class OBSMessages
{
public static void FailedToHookGameProcess() { MainProgram.MainForm.BeginInvoke((MethodInvoker)delegate () { MainProgram.MainForm.label1.Text = "Auto recording disabled: couldn't connect to the game!"; }); }
public static void FailedToHookOBS() { MainProgram.MainForm.BeginInvoke((MethodInvoker)delegate () { MainProgram.MainForm.label1.Text = "Auto recording disabled: couldn't connect to OBS!"; }); }
public static void AutoRecordingEnabled() { MainProgram.MainForm.BeginInvoke((MethodInvoker)delegate () { MainProgram.MainForm.label1.Text = "Auto recording enabled!"; }); }
public static void NotRecording() { MainProgram.MainForm.BeginInvoke((MethodInvoker)delegate () { MainProgram.MainForm.labelStatus.Text = "Currently not recording"; }); }
public static void Recording() { MainProgram.MainForm.BeginInvoke((MethodInvoker)delegate () { MainProgram.MainForm.labelStatus.Text = "Currently recording!"; }); }
}
class GameManager
{
private Process game;
internal Watchers watchers;
public bool IsHooked()
{
return !(game == null || game.HasExited);
}
public void HookGameProcess()
{
game = Process.GetProcessesByName("ASN_App_PcDx9_Final").OrderByDescending(x => x.StartTime).FirstOrDefault(x => !x.HasExited);
if (game == null) return;
try
{
game.WriteBytes(game.MainModuleWow64Safe().BaseAddress + 0x306686, new byte[] { 0x8B, 0xF1, 0x89, 0x3D, 0xFC, 0x0F, 0xFF, 0x00, 0xEB, 0xCE });
game.WriteBytes(game.MainModuleWow64Safe().BaseAddress + 0x30665C, new byte[] { 0xEB, 0x28 });
watchers = new Watchers(game);
}
catch
{
game = null;
}
}
internal class Watchers : MemoryWatcherList
{
public MemoryWatcher<byte> RaceState { get; }
public MemoryWatcher<bool> LoadState { get; }
public Watchers(Process game)
{
this.RaceState = new MemoryWatcher<byte>(new DeepPointer(game.MainModuleWow64Safe().BaseAddress + 0xBF0FFC)) { FailAction = MemoryWatcher.ReadFailAction.SetZeroOrNull };
this.LoadState = new MemoryWatcher<bool>(new DeepPointer(game.MainModuleWow64Safe().BaseAddress + 0x7CE92C, 0x4, 0x0, 0xD400)) { FailAction = MemoryWatcher.ReadFailAction.SetZeroOrNull };
this.AddRange(this.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(p => !p.GetIndexParameters().Any()).Select(p => p.GetValue(this, null) as MemoryWatcher).Where(p => p != null));
}
}
public void UpdateMemoryWatchers()
{
this.watchers.UpdateAll(game);
}
}
}