-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
190 lines (164 loc) · 6.82 KB
/
MainForm.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
using MetadataRetriever;
using NOLFAutoRecorder.Automation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsInput;
using WindowsInput.Native;
namespace NOLFAutoRecorder
{
public partial class MainForm : Form
{
IntPtr emulatorWindow = IntPtr.Zero;
IntPtr emulatorViewPortWindow = IntPtr.Zero;
private InputImpersonator inputImpersonator;
readonly string pcsx2ExePath = Properties.Settings.Default.PCSX2ExePath;
const string nolfPs2IsoPath = @"C:\Jeux\ISOs\NOLF.ISO";
readonly string fmediaPath = Properties.Settings.Default.FMediaExePath;
const string fmediaStartArgs = "--record --dev-loopback=0 --globcmd=listen -o ";
const string fmediaStopArgs = "--globcmd=stop";
readonly string fmediaWorkDir = Properties.Settings.Default.TempRecordingsWorkDir;
List<Action> endOfRecordSessionActions = new List<Action>();
DateTime recordEndTime = DateTime.MinValue;
Process pcsx2Process;
public MainForm()
{
InitializeComponent();
ISOModifier.UndoModifications();
endOfRecordSessionActions.Add(StopRecorder);
endOfRecordSessionActions.Add(() => StopProcess(pcsx2Process));
this.Shown += MainForm_Shown;
Application.ApplicationExit += OnApplicationExit;
}
private void MainForm_Shown(object sender, EventArgs e)
{
RecordBerlinSceneTwo();
}
private void RecordBerlinSceneTwo()
{
int currentSceneStartVoiceId = 10560; //always the same ID
int numberOfVoicesAvailable = 6; //number of lines in the dialogue used
int currentBatchStartId = 10560; //loop over it until the voice ID no longer belongs to the Scene
var missionLoadAction = new Action(() =>
{
WriteLog("Loading Berlin Scene Five...", ToolTipIcon.Warning);
this.inputImpersonator.LoadBerlinSceneFive();
});
var triggerAction = new Action(() =>
{
WriteLog("Activating Berlin Scene Five dialogue...", ToolTipIcon.Warning);
this.inputImpersonator.TriggerBerlinSceneFive();
});
RecordVoiceSet(currentSceneStartVoiceId, numberOfVoicesAvailable, currentBatchStartId, missionLoadAction, triggerAction);
//The ID are linear between 10520 and 10600, so we can simply loop over it with a for,
// but other scenes will need a different loop
//for (int i = currentBatchStartId; i <= currentSceneLastVoiceId; i++)
//{
// WaitSeconds(SoundInfo.GetSoundLengthOfFiles(i, currentSceneLastVoiceId - currentBatchStartId));
//}
//loop disabled as "Wait Seconds" waits too much...
// Modifying the currentBatchStartId for each record session in the source code for now.
}
private void RecordVoiceSet(int startVoiceIdOfScene, int numberOfVoicesAvailable, int currentBatchStartId, Action missionLoadAction, Action activationAction)
{
ISOModifier.PrepareNextBatch(startVoiceIdOfScene, currentBatchStartId, numberOfVoicesAvailable);
pcsx2Process = StartPcsx2();
pcsx2Process.WaitForInputIdle();
emulatorWindow = pcsx2Process.MainWindowHandle;
//Wait for the emulator to load the main screen...
WaitSeconds(12);
emulatorViewPortWindow = WndFinder.SearchForWindow("wxWindowNR", "Slot");
this.inputImpersonator = new InputImpersonator(emulatorViewPortWindow);
this.inputImpersonator.SelectFrenchLanguage();
missionLoadAction.Invoke();
StartRecorder(currentBatchStartId, numberOfVoicesAvailable);
WindowReorganizer.SetActiveWindow(emulatorViewPortWindow);
activationAction.Invoke();
}
public static void WaitSeconds(double seconds)
{
var whenEmulatorReady = DateTime.Now.AddSeconds(seconds);
while (DateTime.Now < whenEmulatorReady)
{
Application.DoEvents();
}
}
private void OnApplicationExit(object sender, EventArgs e)
{
Parallel.ForEach(endOfRecordSessionActions, action =>
{
action.Invoke();
});
}
void WriteLog(string message, ToolTipIcon icon)
{
this.LogTextBox.AppendText(string.Format("{0} : {1}{2}", icon.ToString(), message, Environment.NewLine));
}
bool LoadQuickSave()
{
if (emulatorViewPortWindow == null)
{
return false;
}
WindowReorganizer.SetActiveWindow(emulatorViewPortWindow);
new InputSimulator().Keyboard.KeyPress(VirtualKeyCode.F3);
return true;
}
Process StartPcsx2()
{
var psi = new ProcessStartInfo
{
Arguments = nolfPs2IsoPath,
WorkingDirectory = Path.GetDirectoryName(pcsx2ExePath),
FileName = pcsx2ExePath
};
WriteLog("Emulator started", ToolTipIcon.Info);
return Process.Start(psi);
}
void StartRecorder(int currentBatchStartId, int numberOfVoicesAvailable)
{
var psi = new ProcessStartInfo
{
Arguments = fmediaStartArgs + GetTempFileName(currentBatchStartId, numberOfVoicesAvailable),
WorkingDirectory = fmediaWorkDir,
FileName = fmediaPath,
WindowStyle = ProcessWindowStyle.Minimized
};
Process.Start(psi);
WriteLog("Recording process started", ToolTipIcon.Info);
}
string GetTempFileName(int currentBatchStartId, int numberOfVoicesAvailable)
{
string tempFileName = Path.Combine(fmediaWorkDir, string.Format("{0}_{1}.wav", currentBatchStartId, SoundInfo.GetVoiceIdAtOffsetFromVoiceId(currentBatchStartId, numberOfVoicesAvailable)));
if(File.Exists(tempFileName))
{
File.Delete(tempFileName);
}
return tempFileName;
}
void StopRecorder()
{
Process.Start(fmediaPath, fmediaStopArgs);
WriteLog("Recording process stopped", ToolTipIcon.Info);
}
void StopProcess(Process procToStop)
{
try
{
if (procToStop == null)
{
return;
}
procToStop.Kill();
WriteLog("Emulator stopped", ToolTipIcon.Info);
}
catch
{
}
}
}
}