/
MovieConversionExtensions.cs
332 lines (273 loc) · 7.91 KB
/
MovieConversionExtensions.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.IO;
using System.Linq;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Cores.Nintendo.Gameboy;
using BizHawk.Emulation.Cores.Sega.MasterSystem;
using BizHawk.Emulation.Common.IEmulatorExtensions;
using BizHawk.Emulation.Cores.Consoles.Sega.gpgx;
namespace BizHawk.Client.Common.MovieConversionExtensions
{
public static class MovieConversionExtensions
{
public static TasMovie ToTasMovie(this IMovie old, bool copy = false)
{
string newFilename = old.Filename + "." + TasMovie.Extension;
if (File.Exists(newFilename))
{
int fileNum = 1;
bool fileConflict = true;
while (fileConflict)
{
if (File.Exists(newFilename))
{
newFilename = old.Filename + " (" + fileNum + ")" + "." + TasMovie.Extension;
fileNum++;
}
else
{
fileConflict = false;
}
}
}
var tas = new TasMovie(newFilename, old.StartsFromSavestate);
tas.TasStateManager.MountWriteAccess();
for (var i = 0; i < old.InputLogLength; i++)
{
var input = old.GetInputState(i);
tas.AppendFrame(input);
}
if (!copy)
{
old.Truncate(0); // Trying to minimize ram usage
}
tas.HeaderEntries.Clear();
foreach (var kvp in old.HeaderEntries)
{
tas.HeaderEntries[kvp.Key] = kvp.Value;
}
tas.SyncSettingsJson = old.SyncSettingsJson;
tas.Comments.Clear();
foreach (var comment in old.Comments)
{
tas.Comments.Add(comment);
}
tas.Subtitles.Clear();
foreach (var sub in old.Subtitles)
{
tas.Subtitles.Add(sub);
}
tas.TextSavestate = old.TextSavestate;
tas.BinarySavestate = old.BinarySavestate;
tas.SaveRam = old.SaveRam;
return tas;
}
public static Bk2Movie ToBk2(this IMovie old, bool copy = false, bool backup = false)
{
var bk2 = new Bk2Movie(old.Filename.Replace(old.PreferredExtension, Bk2Movie.Extension));
for (var i = 0; i < old.InputLogLength; i++)
{
var input = old.GetInputState(i);
bk2.AppendFrame(input);
}
if (!copy)
{
old.Truncate(0); // Trying to minimize ram usage
}
bk2.HeaderEntries.Clear();
foreach (var kvp in old.HeaderEntries)
{
bk2.HeaderEntries[kvp.Key] = kvp.Value;
}
bk2.SyncSettingsJson = old.SyncSettingsJson;
bk2.Comments.Clear();
foreach (var comment in old.Comments)
{
bk2.Comments.Add(comment);
}
bk2.Subtitles.Clear();
foreach (var sub in old.Subtitles)
{
bk2.Subtitles.Add(sub);
}
bk2.TextSavestate = old.TextSavestate;
bk2.BinarySavestate = old.BinarySavestate;
bk2.SaveRam = old.SaveRam;
if (!backup)
bk2.Save();
return bk2;
}
public static TasMovie ConvertToSavestateAnchoredMovie(this TasMovie old, int frame, byte[] savestate)
{
string newFilename = old.Filename + "." + TasMovie.Extension;
if (File.Exists(newFilename))
{
int fileNum = 1;
bool fileConflict = true;
while (fileConflict)
{
if (File.Exists(newFilename))
{
newFilename = old.Filename + " (" + fileNum + ")" + "." + TasMovie.Extension;
fileNum++;
}
else
{
fileConflict = false;
}
}
}
TasMovie tas = new TasMovie(newFilename, true);
tas.BinarySavestate = savestate;
tas.ClearLagLog();
var entries = old.GetLogEntries();
tas.CopyLog(entries.Skip(frame));
tas.CopyVerificationLog(old.VerificationLog);
tas.CopyVerificationLog(entries.Take(frame));
// States can't be easily moved over, because they contain the frame number.
// TODO? I'm not sure how this would be done.
tas.TasStateManager.MountWriteAccess();
old.TasStateManager.Clear();
// Lag Log
tas.TasLagLog.FromLagLog(old.TasLagLog);
tas.TasLagLog.StartFromFrame(frame);
tas.HeaderEntries.Clear();
foreach (var kvp in old.HeaderEntries)
{
tas.HeaderEntries[kvp.Key] = kvp.Value;
}
tas.StartsFromSavestate = true;
tas.SyncSettingsJson = old.SyncSettingsJson;
tas.Comments.Clear();
foreach (string comment in old.Comments)
{
tas.Comments.Add(comment);
}
tas.Subtitles.Clear();
foreach (Subtitle sub in old.Subtitles)
{
tas.Subtitles.Add(sub);
}
foreach (TasMovieMarker marker in old.Markers)
{
if (marker.Frame > frame)
tas.Markers.Add(new TasMovieMarker(marker.Frame - frame, marker.Message));
}
tas.TasStateManager.Settings = old.TasStateManager.Settings;
tas.Save();
return tas;
}
public static TasMovie ConvertToSaveRamAnchoredMovie(this TasMovie old, byte[] saveRam)
{
string newFilename = old.Filename + "." + TasMovie.Extension;
if (File.Exists(newFilename))
{
int fileNum = 1;
bool fileConflict = true;
while (fileConflict)
{
if (File.Exists(newFilename))
{
newFilename = old.Filename + " (" + fileNum + ")" + "." + TasMovie.Extension;
fileNum++;
}
else
{
fileConflict = false;
}
}
}
TasMovie tas = new TasMovie(newFilename, true);
tas.SaveRam = saveRam;
tas.TasStateManager.Clear();
tas.ClearLagLog();
var entries = old.GetLogEntries();
tas.CopyVerificationLog(old.VerificationLog);
tas.CopyVerificationLog(entries);
tas.HeaderEntries.Clear();
foreach (var kvp in old.HeaderEntries)
{
tas.HeaderEntries[kvp.Key] = kvp.Value;
}
tas.StartsFromSaveRam = true;
tas.StartsFromSavestate = false;
tas.SyncSettingsJson = old.SyncSettingsJson;
tas.Comments.Clear();
foreach (string comment in old.Comments)
{
tas.Comments.Add(comment);
}
tas.Subtitles.Clear();
foreach (Subtitle sub in old.Subtitles)
{
tas.Subtitles.Add(sub);
}
tas.TasStateManager.Settings = old.TasStateManager.Settings;
tas.Save();
return tas;
}
// TODO: This doesn't really belong here, but not sure where to put it
public static void PopulateWithDefaultHeaderValues(this IMovie movie, string author = null)
{
movie.Author = author ?? Global.Config.DefaultAuthor;
movie.EmulatorVersion = VersionInfo.GetEmuVersion();
movie.SystemID = Global.Emulator.SystemId;
var settable = new SettingsAdapter(Global.Emulator);
if (settable.HasSyncSettings)
{
movie.SyncSettingsJson = ConfigService.SaveWithType(settable.GetSyncSettings());
}
if (Global.Game != null)
{
movie.GameName = PathManager.FilesystemSafeName(Global.Game);
movie.Hash = Global.Game.Hash;
if (Global.Game.FirmwareHash != null)
{
movie.FirmwareHash = Global.Game.FirmwareHash;
}
}
else
{
movie.GameName = "NULL";
}
if (Global.Emulator.BoardName != null)
{
movie.BoardName = Global.Emulator.BoardName;
}
if (Global.Emulator.HasRegions())
{
var region = Global.Emulator.AsRegionable().Region;
if (region == Emulation.Common.DisplayType.PAL)
{
movie.HeaderEntries.Add(HeaderKeys.PAL, "1");
}
}
if (Global.FirmwareManager.RecentlyServed.Any())
{
foreach (var firmware in Global.FirmwareManager.RecentlyServed)
{
var key = firmware.SystemId + "_Firmware_" + firmware.FirmwareId;
if (!movie.HeaderEntries.ContainsKey(key))
{
movie.HeaderEntries.Add(key, firmware.Hash);
}
}
}
if (Global.Emulator is Gameboy && (Global.Emulator as Gameboy).IsCGBMode())
{
movie.HeaderEntries.Add("IsCGBMode", "1");
}
if (Global.Emulator is SMS && (Global.Emulator as SMS).IsSG1000)
{
movie.HeaderEntries.Add("IsSGMode", "1");
}
if (Global.Emulator is GPGX && (Global.Emulator as GPGX).IsSegaCD)
{
movie.HeaderEntries.Add("IsSegaCDMode", "1");
}
movie.Core = ((CoreAttributes)Attribute
.GetCustomAttribute(Global.Emulator.GetType(), typeof(CoreAttributes)))
.CoreName;
}
}
}