void RecoverMissing(SmartWebClient client, ChunkInfo ci) { List <int> missingKeys; List <int> missingChunks; CurrentReplay.RecoverMissing(ci.keyFrameId, ci.chunkId, Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"\Temp", GameId + "-" + Region), out missingKeys, out missingChunks); if (missingKeys.Count > 0) { // Missing Keys foreach (int keyid in missingKeys) { // GET THEM try { if (Recording) { DownloadKey(client, keyid); } } catch (Exception ex) { if (ex.Message.Contains("404") && OnFailedToRecord != null && CancelRecordOnFail) { OnFailedToRecord(ex); return; } } } } if (missingChunks.Count > 0) { // Missing Chunks foreach (int chunk in missingChunks) { try { if (Recording) { DownloadChunk(client, chunk); } } catch (Exception ex) { if (ex.Message.Contains("404") && OnFailedToRecord != null) { OnFailedToRecord(ex); } } } } }
void SaveReplay(SmartWebClient client, int ChunkId) { //Sometimes chunk 1 isn't retrieved so get it again... it's like 7 kb so np // Download Chunk 0 DownloadChunk(client, 1); // Download Current Chunk id DownloadChunk(client, ChunkId); if (OnStatusChanged != null) { OnStatusChanged("Saving replay"); } // End Stats string stat = client.DownloadString( String.Format("{0}/consumer/{1}/{2}/{3}/token", Server + "/observer-mode/rest", "endOfGameStats", Region, GameId)); string sum = CurrentReplay.SummonerName; CurrentReplay.ParseStats(Convert.FromBase64String(stat)); CurrentReplay.SummonerName = sum; // Last Chunk Info End string token = client.DownloadString( String.Format("{0}/consumer/{1}/{2}/{3}/0/token", Server + "/observer-mode/rest", "getLastChunkInfo", Region, GameId)); CurrentReplay.ParseLastChunkInfo(token); // Last game meta data token = client.DownloadString( String.Format("{0}/consumer/{1}/{2}/{3}/token", this.Server + "/observer-mode/rest", "getGameMetaData", Region, GameId)); ReplayInfo flags = ReplayInfo.StandardFormat | ReplayInfo.Ghostblade; if (CurrentReplay.IsPBE) { flags |= ReplayInfo.PBE; } if (string.IsNullOrEmpty(CurrentReplay.SummonerName) || CurrentReplay.SummonerName == "Spectator") { flags |= ReplayInfo.Spectator; if (CurrentReplay.GameStats.TeamPlayerParticipantStats.Count > 0) { CurrentReplay.SummonerName = CurrentReplay.GameStats.TeamPlayerParticipantStats[0].SummonerName; } else if (CurrentReplay.GameStats.OtherTeamPlayerParticipantStats.Count > 0) { CurrentReplay.SummonerName = CurrentReplay.GameStats.OtherTeamPlayerParticipantStats[0].SummonerName; } } CurrentReplay.ParseMetaData(token); CurrentReplay.MetaData.clientBackFetchingEnabled = true; CurrentReplay.MetaData.clientBackFetchingFreq = 50; CurrentReplay.MetaData.pendingAvailableChunkInfo.Clear(); CurrentReplay.MetaData.pendingAvailableKeyFrameInfo.Clear(); if (!CurrentReplay.IsPBE) { try { RiotSharp.MatchEndpoint.MatchDetail det = API.GetMatch(RiotTool.PlatformToRegion(Region), GameId); // TODO ADD COMPRESSION FLAG CurrentReplay.PlayerInfos.Map = (byte)det.MapType; CurrentReplay.PlayerInfos.Queue = (byte)det.QueueType; } catch { CurrentReplay.PlayerInfos.Map = 11; CurrentReplay.PlayerInfos.Queue = 4; } } try { CurrentReplay.Save(CurrentReplay.Signer != null, flags); } catch (Exception ex) { if (OnFailedToSave != null) { OnFailedToSave(ex); } } if (OnReplayRecorded != null) { OnReplayRecorded(); } Recording = false; }
public void DownloadKey(SmartWebClient client, DownloadTask task, int attemp) { try { if (Recording) { while (client.IsBusy) { Thread.Sleep(100); } if (OnStatusChanged != null) { OnStatusChanged("Downloading key " + task.Id.ToString()); } if (OnAttemptToDownload != null && attemp != 0) { OnAttemptToDownload(task, attemp); } client.DownloadProgressChanged += new DownloadProgressChangedEventHandler((sender, e) => { if (OnDownloadProgress != null) { OnDownloadProgress(task, e.ProgressPercentage); } }); if (attemp == 0 && !IsBusy(task)) { DownloadTasks.Add(task); } else { Logger.Instance.Log.Warn("Multiple Download Tasks " + task.Id + "[" + task.IsChunk.ToString() + "]"); } client.DownloadDataAsync(new Uri( String.Format("{0}/consumer/{1}/{2}/{3}/{4}/token", Server + "/observer-mode/rest", "getKeyFrame", Region, GameId, task.Id))); byte[] chunk = null; bool failed = false; client.DownloadDataCompleted += new DownloadDataCompletedEventHandler((sender, e) => { EndTask(task); if (!e.Cancelled && e.Error == null) { chunk = e.Result; } else if (e.Error != null && attemp < 5) { failed = true; } }); if (failed) { DownloadKey(client, task, attemp + 1); } while (client.IsBusy) { Thread.Sleep(100); } if (OnDownloadProgress != null) { OnDownloadProgress(task, -1); } if (chunk == null) { throw new Exception("Failed to download replay data Maximum attempt exceeded"); } CurrentReplay.AddKey(chunk, (byte)task.Id); chunk = null; } } catch (Exception ex) { if (ResolveException(ex, task)) { DownloadKey(client, task, attemp + 1); } else if (attemp < 5) { Thread.Sleep(1000); DownloadKey(client, task, attemp + 1); } else { OnFailedToRecord(ex); } //if ((ex is WebException) || ex.Message.Contains("404")) //{ // if (attemp == 3 && OnFailedToRecord != null) // OnFailedToRecord(ex); // else if (attemp < 3) // DownloadKey(client, task, attemp + 1); //} //if (OnFailedToRecord != null) // OnFailedToRecord(ex); } }