forked from Drakulix/GameReplay.Mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mod.cs
722 lines (623 loc) · 23.6 KB
/
Mod.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Diagnostics;
using Mono.Cecil;
using UnityEngine;
using ScrollsModLoader.Interfaces;
using System.ComponentModel;
using System.Net;
using System.Text.RegularExpressions;
using JsonFx.Json;
namespace GameReplay.Mod
{
public class Mod : BaseMod, ICommListener, IListCallback, IOkStringCancelCallback, IOkCallback
{
private Recorder recorder = null;
private Player player;
private UIListPopup recordListPopup;
List<Item> recordList = new List<Item>();
private Record selectedRecord;
private Settings sttngs;
private String recordFolder;
private bool spectating = false;
private BattleMode bm = null;
private BattleModeUI bmui = null;
private bool loadedguiSkin = false;
private GUISkin guiSkin;
private FieldInfo currentEffectField=typeof(BattleMode).GetField("currentEffect", BindingFlags.NonPublic | BindingFlags.Instance);
private FieldInfo frameRectField = typeof(ProfileMenu).GetField("frameRect", BindingFlags.Instance | BindingFlags.NonPublic);
private FieldInfo showEditField = typeof(ProfileMenu).GetField("showEdit", BindingFlags.Instance | BindingFlags.NonPublic);
//private MethodInfo getButtonRectMethod= typeof(ProfileMenu).GetMethod("getButtonRect", BindingFlags.NonPublic | BindingFlags.Instance);
//private FieldInfo guiSkinField=typeof(ProfileMenu).GetField("guiSkin", BindingFlags.Instance | BindingFlags.NonPublic);
private FieldInfo usernameStyleField=typeof(ProfileMenu).GetField("usernameStyle", BindingFlags.Instance | BindingFlags.NonPublic);
public Mod()
{
this.loadGuiSkin();
//recordFolder = this.OwnFolder() + Path.DirectorySeparatorChar + "Records";
string homePath = (Environment.OSVersion.Platform == PlatformID.Unix ||Environment.OSVersion.Platform == PlatformID.MacOSX)? Environment.GetEnvironmentVariable("HOME") : Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
recordFolder = homePath + Path.DirectorySeparatorChar + "ScrollsRecords";
if (!Directory.Exists(recordFolder + Path.DirectorySeparatorChar))
{
Directory.CreateDirectory(recordFolder + Path.DirectorySeparatorChar);
}
player = new Player(recordFolder);
sttngs = new Settings(recordFolder);
try {
App.Communicator.addListener(this);
} catch {}
new Thread (new ThreadStart (parseRecords)).Start ();
Console.WriteLine("loaded Recorder");
}
private void loadGuiSkin()
{
this.guiSkin = (GUISkin)ResourceManager.Load("_GUISkins/Lobby");
}
public static string GetName()
{
return "GameReplay";
}
public static int GetVersion()
{
return 133;
}
public void handleMessage(Message msg)
{
try {
if (msg is BattleRedirectMessage)
{
this.spectating = false;
recorder = new Recorder(recordFolder, this, this.sttngs);
Console.WriteLine("Recorder started: " + recorder);
}
} catch {}
//try
//{
if (msg is SpectateRedirectMessage)
{
this.spectating = true;
recorder = new Recorder(recordFolder, this, this.sttngs);
Console.WriteLine("Recorder started: " + recorder + " (Spectator)");
}
//}
//catch { }
}
public void onConnect(OnConnectData ocd)
{
return;
}
public String getRecordFolder()
{
return recordFolder;
}
public Player getPlayer()
{
return player;
}
public static MethodDefinition[] GetHooks(TypeDefinitionCollection scrollsTypes, int version)
{
try {
MethodDefinition[] defs = new MethodDefinition[] {
scrollsTypes["ProfileMenu"].Methods.GetMethod("Start")[0],
scrollsTypes["ProfileMenu"].Methods.GetMethod("getButtonRect", new Type[]{typeof(float)}),
scrollsTypes["ProfileMenu"].Methods.GetMethod("drawEditButton")[0],
scrollsTypes["BattleMode"].Methods.GetMethod("Start")[0],
scrollsTypes["BattleMode"].Methods.GetMethod("effectDone")[0],
scrollsTypes["BattleMode"].Methods.GetMethod("handleHandUpdate", new Type[]{typeof(EMHandUpdate)}),
scrollsTypes["BattleModeUI"].Methods.GetMethod("Start")[0],
scrollsTypes["SettingsMenu"].Methods.GetMethod("OnGUI")[0],
scrollsTypes["Communicator"].Methods.GetMethod("joinLobby", new Type[]{typeof(bool)}),
};
List<MethodDefinition> list = new List<MethodDefinition>(defs);
list.AddRange(Player.GetPlayerHooks(scrollsTypes, version));
return list.ToArray();
} catch {
return new MethodDefinition[] {};
}
}
public override bool WantsToReplace (InvocationInfo info)
{
if (info.targetMethod.Equals ("getButtonRect")) {
foreach (StackFrame frame in info.stackTrace.GetFrames()) {
if (frame.GetMethod ().Name.Contains ("BeforeInvoke")) {
break;
}
if (frame.GetMethod ().Name.Contains ("drawEditButton")) {
return true;
}
}
}
return player.WantsToReplace(info);
}
public override void ReplaceMethod (InvocationInfo info, out object returnValue) {
if (info.target is ProfileMenu && info.targetMethod.Equals("getButtonRect"))
{
returnValue = (Rect)typeof(ProfileMenu).GetMethod("getButtonRect", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(info.target, new object[] { 2f });
}
else
{
player.ReplaceMethod(info, out returnValue);
}
}
public override void BeforeInvoke(InvocationInfo info)
{
if (info.target is ProfileMenu && info.targetMethod.Equals("Start") && App.SceneValues.profilePage.isMe())
{
//list them
//recordList.Clear();
recordListPopup = new GameObject("Record List").AddComponent<UIListPopup>();
recordListPopup.transform.parent = ((ProfileMenu)info.target).transform;
Rect frame = (Rect)frameRectField.GetValue(info.target);
recordListPopup.Init(new Rect(frame.center.x - frame.width / 2.0f, frame.center.y - frame.height / 2.0f + 32.0f, frame.width, frame.height - (float)Screen.height * 0.055f * 3.0f), false, true, recordList, this, null, null, false, true, false, true, null, true, false);
recordListPopup.enabled = true;
recordListPopup.SetOpacity(1f);
}
if (info.target is ProfileMenu && info.targetMethod.Equals("drawEditButton"))
{
Rect rect = (Rect)frameRectField.GetValue(info.target);
//LobbyMenu.drawShadowText (new Rect(rect.center.x-(float)Screen.width/8.0f/2.0f, rect.center.y-rect.height/2.0f-(float)Screen.height*0.055f*3.0f-40.0f, (float)Screen.width/8.0f, 35.0f), "Match History", Color.white);
if ((bool)showEditField.GetValue(info.target))
{
recordListPopup.enabled = false;
recordListPopup.SetOpacity(0f);
}
else
{
if (!this.loadedguiSkin)
{
this.loadGuiSkin();
this.loadedguiSkin = true;
}
new ScrollsFrame(new Rect(rect.center.x - rect.width / 2.0f - 20.0f, rect.center.y - rect.height / 2.0f, rect.width + 40.0f, rect.height - (float)Screen.height * 0.055f - 20.0f)).AddNinePatch(ScrollsFrame.Border.DARK_CURVED, NinePatch.Patches.CENTER).Draw();
recordListPopup.enabled = true;
recordListPopup.SetOpacity(1f);
GUIStyle labelSkin = (GUIStyle)usernameStyleField.GetValue(info.target);
labelSkin.fontSize = 32;
GUI.Label(new Rect(rect.center.x - (float)Screen.width / 6.0f / 2.0f, rect.center.y - rect.height / 2.0f - 40.0f, (float)Screen.width / 6.0f, 35.0f), "Match History", labelSkin);
if (LobbyMenu.drawButton((Rect)this.sttngs.getButtonRect(0), "Load Replay", this.guiSkin ))// getButtonRectMethod.Invoke(info.target, new object[] { 0 }) , (GUISkin)guiSkinField.GetValue(info.target)
{
LoadReplay();
}
}
}
if (info.target is BattleMode && info.targetMethod.Equals("effectDone"))
{
EffectMessage currentEffect = ((EffectMessage)currentEffectField.GetValue(info.target));
if (currentEffect != null && currentEffect.type == "TurnBegin" && currentEffect.getRawText() != null && currentEffect.getRawText().Contains("{\"TurnBegin\":") && recorder != null && recorder.recording == true)
{
//Console.WriteLine("turnbegin end of: " + currentEffect.getRawText()); //{"TurnBegin":
recorder.turnBeginEnds();
}
}
if (!(info.target is ProfileMenu))
player.BeforeInvoke(info);
}
public override void AfterInvoke(InvocationInfo info, ref object returnValue)
{
if (info.target is SettingsMenu && info.targetMethod.Equals("OnGUI"))
{
if (!this.loadedguiSkin)
{
this.loadGuiSkin();
this.loadedguiSkin = true;
}
string label = "ask after match";
if (!this.sttngs.alwayssave) label = "never ask";
if (LobbyMenu.drawButton((Rect)this.sttngs.getButtonRect(1), label, this.guiSkin))//(GUISkin)guiSkinField.GetValue(info.target)
{
this.sttngs.alwayssave = !this.sttngs.alwayssave;
this.sttngs.saveSettings();
}
}
if (info.target is Communicator && info.targetMethod.Equals("joinLobby") && recorder != null && recorder.recording == true)
{
recorder.stoprecording();
}
if (info.target is BattleMode && info.targetMethod.Equals("Start"))
{
player.setbm(info.target as BattleMode);
}
if (info.target is BattleMode && info.targetMethod.Equals("Start") && this.player.playing)
{
App.ChatUI.Show(false);
App.ChatUI.SetMode(OnlineState.SPECTATE);
App.ChatUI.SetLocked(false, (float)Screen.height * 0.25f);
App.Communicator.setEnabled(true, true);
App.ChatUI.SetEnabled(true);
Console.WriteLine("playing + battlemodestart");
}
if (info.target is BattleMode && info.targetMethod.Equals("handleHandUpdate") && this.player.playing)
{
EMHandUpdate m = (EMHandUpdate)info.arguments[0];
BattleMode dis = (BattleMode)info.target;
//test
FieldInfo bmActiveColor = typeof(BattleMode).GetField("activeColor", BindingFlags.Instance | BindingFlags.NonPublic);
//MethodInfo getPlayer = typeof(BattleMode).GetMethod("getPlayer", BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo getPlayer = typeof(BattleMode).GetMethod("getPlayer", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(TileColor) }, null);
FieldInfo bmHandManager = typeof(BattleMode).GetField("handManager", BindingFlags.Instance | BindingFlags.NonPublic);
TileColor activeColorr = ((TileColor)bmActiveColor.GetValue(dis));
BMPlayer playerr = (BMPlayer)getPlayer.Invoke(dis, new object[] { activeColorr });
if (m.profileId == playerr.profileId)
{
ResourceGroup availableResources = ((BattleMode)info.target).battleUI.GetResources(dis.isLeftColor(activeColorr)).availableResources;
HandManager handManager = ((HandManager)bmHandManager.GetValue(dis));
handManager.SetHand(m.cards, availableResources, m.profileId);
}
}
if (info.target is BattleMode && info.targetMethod.Equals("Start") && recorder != null && recorder.recording == true)
{
Console.WriteLine("## set Bm");
recorder.setBm(info.target as BattleMode);
if (this.spectating)
{
recorder.recordSpectator();
Console.WriteLine("## (Spectator)");
}
}
if (info.target is BattleModeUI && info.targetMethod.Equals("Start") && recorder != null && recorder.recording == true)
{
Console.WriteLine("## set Bmui");
recorder.setBmUI(info.target as BattleModeUI);
}
if (!(info.target is BattleMode) || (info.target is BattleMode && (!info.targetMethod.Equals("Start") && !info.targetMethod.Equals("effectDone"))) || (info.target is Communicator && !info.targetMethod.Equals("joinLobby")))
{
player.AfterInvoke(info, ref returnValue);
}
}
public void ButtonClicked(UIListPopup popup, ECardListButton button)
{
return;
}
public void ButtonClicked(UIListPopup popup, ECardListButton button, List<Item> selectedCards)
{
return;
}
public void ItemButtonClicked(UIListPopup popup, Item card)
{
return;
}
public void ItemClicked(UIListPopup popup, Item card)
{
//player.LaunchReplay (((Record)card).fileName());
selectedRecord = (Record)card;
//App.Popups.ShowMultibutton(this, "Replayer", card.getDesc(), new string[] { "Play", "Share", "Delete" });
App.Popups.ShowMultibutton(this, "Replayer", card.getDesc(), new GUIContent[]
{
new GUIContent("Play"),
new GUIContent("Share"),
new GUIContent("Delete")
});
}
public void ItemHovered(UIListPopup popup, Item card)
{
return;
}
public void LoadReplay()
{
//App.Popups.ShowMultibutton(this, "Load", "Load", new string[] { "From File", "From Link" });
App.Popups.ShowMultibutton(this, "Load", "Load", new GUIContent[]
{
new GUIContent("From File"),
new GUIContent("From Link")
});
}
public void PopupOk(string popupType, string choice)
{
if (popupType.Equals("savedeck"))
{
Console.WriteLine("Loading replay " + choice);
// try a few regexes until a match is found
List<RegexPattern> patterns = new List<RegexPattern>();
patterns.Add(new RegexPattern("^(http://)?(www\\.)?scrollsguide\\.com/replays/r/([0-9]+)$", 3)); // full replay url in browser
patterns.Add(new RegexPattern("^([0-9]+)$", 1)); // just the replay number
patterns.Add(new RegexPattern("^http://a\\.scrollsguide\\.com/replay/download/([0-9]+)(\\?true)?$", 1)); // full replay url with or without ?true
bool hasMatch = false;
for (int i = 0; i < patterns.Count && !hasMatch; i++)
{
RegexPattern pattern = patterns[i];
Match m = Regex.Match(choice, pattern.getPattern());
if (m.Success)
{
Console.WriteLine("Regex match: " + m.Groups[pattern.getMatchNum()].Value);
hasMatch = true;
ReplayDownloader rd = new ReplayDownloader(this);
rd.startDownload(Convert.ToInt32(m.Groups[pattern.getMatchNum()].Value));
}
else
{
Console.WriteLine("Can't match replay regex for " + pattern.getPattern());
}
}
if (!hasMatch)// is not a valid replay url
{
App.Popups.ShowOk(this, "fail", "Invalid url", "That is not a valid url, replay can't be loaded.", "Ok");
}
}
else
{
if (choice.Equals ("Play")) {
player.LaunchReplay (selectedRecord.fileName ());
} else if (choice.Equals ("Share")) {
new ReplayUploader (this, selectedRecord);
} else if (choice.Equals ("Delete")) {
File.Delete (selectedRecord.fileName ());
recordList.Remove(selectedRecord);
recordListPopup.SetItemList (recordList);
}
else if (choice.Equals("From File"))
{
String path = modAPI.FileOpenDialog ();
player.LaunchReplay (path);
}
else if (choice.Equals("From Link"))
{
//App.Popups.ShowTextInput(this, "", "Browse replays on http://www.scrollsguide.com/replays", "savedeck", "Load replay", "Paste the url of the replay:", "Load");
App.Popups.ShowTextEntry(this, "savedeck", "Load replay", "Paste the url of the replay:", "Load", "Cancel", "");
}
}
}
public void PopupCancel(string popupType)
{
return;
}
// method for single OKButton popup
public void PopupOk(string popupType)
{
return;
}
public void parseRecords() {
foreach (String file in Directory.GetFiles(recordFolder))
{
if (file.EndsWith("sgr"))
{
parseRecord (file);
}
}
}
public void parseRecord(String file) {
Console.WriteLine("read record-file: " + file);
JsonMessageSplitter jsonms = new JsonMessageSplitter();
String log = File.ReadAllText(file);
jsonms.feed(log);
jsonms.runParsing();
String line = jsonms.getNextMessage();
bool searching = true;
String player1name = null;
String player2name = null;
//String enemyId = null;
String deckName = null;
ResourceType type = ResourceType.NONE;
while (line != null && searching)
{
try
{
Message msg = MessageFactory.create(MessageFactory.getMessageName(line), line);
if (msg is GameInfoMessage)
{
GameInfoMessage gim = (GameInfoMessage) msg;
if (gim.white.Equals(App.MyProfile.ProfileInfo.name))
{
player1name = gim.white;
player2name = gim.black;
}
else
{
player1name = gim.black;
player2name = gim.white;
}
deckName = ""; //deckname isnt in gameinfomessage anymore
}
if (msg is ActiveResourcesMessage)
{
type = (msg as ActiveResourcesMessage).types[0];
}
if (player2name != null && type != ResourceType.NONE)
{
searching = false;
}
}
catch
{
}
jsonms.runParsing();
line = jsonms.getNextMessage();
}
recordList.Add(new Record(File.GetCreationTime(file).ToShortDateString() + " - " + File.GetCreationTime(file).ToShortTimeString(),player1name + " vs " + player2name + " - " + deckName, /*enemyId,*/ file, type));
if (recordListPopup != null)
recordListPopup.SetItemList (recordList);
}
}
internal class RegexPattern
{
private String pattern;
private int matchNum;
public RegexPattern(String pattern, int matchNum)
{
this.pattern = pattern;
this.matchNum = matchNum;
}
public String getPattern()
{
return this.pattern;
}
public int getMatchNum()
{
return this.matchNum;
}
}
internal class Record : Item
{
private String Title;
private String Description;
private String filename;
//private String enemyId;
private ResourceType resource;
public Record(String title, String desc, /*String enemyId,*/ String filename, ResourceType resource)
{
this.Title = title;
this.Description = desc;
this.filename = filename;
this.resource = resource;
//this.enemyId = enemyId;
}
public bool selectable()
{
return true;
}
public Texture getImage()
{
switch (resource)
{
case ResourceType.GROWTH:
return ResourceManager.LoadTexture("BattleUI/battlegui_icon_growth");
case ResourceType.ENERGY:
return ResourceManager.LoadTexture("BattleUI/battlegui_icon_energy");
case ResourceType.ORDER:
return ResourceManager.LoadTexture("BattleUI/battlegui_icon_order");
case ResourceType.DECAY:
return ResourceManager.LoadTexture("BattleUI/battlegui_icon_decay");
default:
return null;
}
}
public String getName()
{
return Title;
}
public String getDesc()
{
return Description;
}
public String fileName()
{
return filename;
}
public long getId()
{
return Convert.ToInt64(Path.GetFileNameWithoutExtension(filename));
}
}
internal class ReplayUploader
{
private Mod m;
private Record toUpload;
public ReplayUploader(Mod m, Record r)
{
this.m = m;
this.toUpload = r;
Thread uploadThread = new Thread(new ThreadStart(Upload));
uploadThread.Start();
}
private void Upload()
{
if (canShare())
{
NameValueCollection postParams = getPostParams();
App.Popups.ShowInfo("Sharing", "Sharing your replay...");
ResultMessage result = Extensions.HttpUploadFile("http://a.scrollsguide.com/replay/upload",
toUpload.fileName(), "replay", "scr/replay", postParams);
App.Popups.KillCurrentPopup();
if (result.msg.Equals("success"))
{
App.Popups.ShowOk(m, "fail", "Replay shared", "Your replay has been shared. See it on scrollsguide.com/replays!", "Ok");
}
else
{
App.Popups.ShowOk(m, "fail", "Replay not shared", "There was an error sharing your replay: " + result.data, "Ok");
}
}
else
{
App.Popups.ShowOk(m, "fail", "Replay not shared", "This replay was already shared.", "Ok");
}
}
private NameValueCollection getPostParams()
{
NameValueCollection nvc = new NameValueCollection();
nvc.Add("from", App.MyProfile.ProfileInfo.name); // player's username
nvc.Add("gid", Convert.ToString(toUpload.getId())); // game id
nvc.Add("mtime", Convert.ToString(Extensions.ToUnixTimestamp(File.GetCreationTimeUtc(toUpload.fileName())))); // creation time
return nvc;
}
private bool canShare()
{
String html;
try {
html = new WebClient().DownloadString("http://a.scrollsguide.com/replay/canshare/" + toUpload.getId());
} catch (WebException) {
return false;
}
JsonReader r = new JsonReader();
ResultMessage rm = r.Read(html, System.Type.GetType("ResultMessage")) as ResultMessage;
return rm.data.Equals("yes");
}
}
internal class ReplayDownloader
{
private WebClient wc = new WebClient();
private Mod callback = null;
private String saveDirectory;
private String saveLocation;
public ReplayDownloader(Mod p)
{
this.callback = p;
saveDirectory = p.getRecordFolder() + Path.DirectorySeparatorChar + "downloads" + Path.DirectorySeparatorChar;
if (!Directory.Exists(saveDirectory))
{
Directory.CreateDirectory(saveDirectory);
}
}
public void startDownload(long gameId)
{
saveLocation = saveDirectory + gameId + ".sgr";
if (File.Exists(saveLocation))
{
Console.WriteLine("Using cached replay file");
callback.getPlayer().LaunchReplay(saveLocation);
}
else
{
Console.WriteLine("Downloading replay from internet");
try {
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
wc.DownloadFileAsync(new Uri("http://a.scrollsguide.com/replay/download/" + gameId + "?true"), saveLocation);
} catch (WebException) {
return;
}
}
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
}
void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
Console.WriteLine("Done downloading :)");
String[] keys = wc.ResponseHeaders.AllKeys;
String contentType = "";
for (int i = 0; i < keys.Length; i++)
{
Console.WriteLine(keys[i]);
if (keys[i].Equals("Content-Type"))
{
contentType = wc.ResponseHeaders.Get(i);
}
}
Console.WriteLine("Content-Type: " + contentType);
System.Diagnostics.Debug.WriteLine("Done downloading the replay :)");
if (contentType.Equals("scr/replay")) // only acceptable header, other headers are errors
{
callback.getPlayer().LaunchReplay(saveLocation);
}
else
{
File.Delete(saveLocation);
App.Popups.ShowOk(callback, "fail", "Failed to download replay", "That's not a valid replay ID.", "Ok");
}
}
}
}