public void AddToQueue(JobInfo jobInfo, string replayPath, string outputPath) { queue.Add(Tuple.Create(jobInfo, replayPath, outputPath)); }
public async Task ReplayConverterWithAddons(CommandContext ctx, params string[] addons) { try { await ctx.TriggerTypingAsync(); JobInfo addonsJobInfo = addons != null?JobInfo.CreateFromStrings((byte)addons.Length, addons) : JobInfo.NoAddons; currentChannel = ctx.Channel; if (ctx.Message.Attachments.Count != 0) { if (ctx.Message.Attachments.First().FileSize > 300000) { await ctx.RespondAsync("File is too big."); return; } using (WebClient wwwClient = new WebClient()) { wwwClient.DownloadFile(ctx.Message.Attachments.First().Url, ctx.Message.Attachments.First().FileName); } try { FileInfo replay = new FileInfo(ctx.Message.Attachments.First().FileName); string replayID = Path.GetRandomFileName() + ".lmp"; byte[] fileBytes = File.ReadAllBytes(replay.FullName).ToArray(); string addonPath; string version; if (fileBytes[12] == 201) { addonPath = $"/root/.srb2/.srb21/addons/"; version = "2.1"; } else if (fileBytes[12] == 202) { addonPath = $"/root/.srb2/addons"; version = "2.2"; } else { await ctx.RespondAsync("File not playable on 2.2 or 2.1. Is it a valid replay?"); return; } File.Move(replay.Name, $"/root/.srb2/replaystogif/{replayID}"); string confirmationMessage = $"Processing {version} replay sent by {ctx.Member.Username}"; if (addons != null) { for (int i = 0; i < addons.Length; i++) { if (version == "2.1") { if (!File.Exists($"{addonPath}/{addons[i]}")) { await ctx.RespondAsync("Addon does not exist on the server."); File.Delete($"/root/.srb2/replaystogif/{replayID}"); return; } } else { if (!File.Exists($"{addonPath}/Levels/{addons[i]}") && !File.Exists($"{addonPath}/Characters/{addons[i]}")) { await ctx.RespondAsync("Addon does not exist on the server."); File.Delete($"/root/.srb2/replaystogif/{replayID}"); return; } } } confirmationMessage += " with addon(s) " + string.Join(" ", addons); } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { await ctx.RespondAsync(confirmationMessage); Program.convertQueue.Add(ctx); } Program.replayEvents.AddToQueue(addonsJobInfo, $"/root/.srb2/replaystogif/{replayID}", "/var/www/html/gifs/torename.gif"); } catch (Exception e) { await ctx.RespondAsync("Error: " + e.Message); // await ctx.RespondAsync(e.StackTrace); // await ctx.RespondAsync(e.Source); // await ctx.RespondAsync(e.InnerException.Message); } } else { await ctx.RespondAsync("No file attached."); } } catch (Exception e) { await ctx.RespondAsync("Error: " + e.Message); // await ctx.RespondAsync(e.StackTrace); // await ctx.RespondAsync(e.Source); // await ctx.RespondAsync(e.InnerException.Message); } }
private void RecordReplay(JobInfo replayInfo, string replayPath, string outputPath) { FileInfo[] gifInfos = GifDir.GetFiles().OrderBy(file => file.LastWriteTime).ToArray(); PreviousGifFiles = new string[gifInfos.Length]; for (int i = 0; i < gifInfos.Length; i++) { PreviousGifFiles[i] = gifInfos[i].Name; } try { byte[] bytes = File.ReadAllBytes(replayPath); if (bytes[12] == 202) { ExecutableFolder = ExecutableFolder22; DataFolder = DataFolder22; LogFilePath = LogFilePath22; } else { ExecutableFolder = ExecutableFolder21; DataFolder = DataFolder21; LogFilePath = LogFilePath21; } File.WriteAllBytes(Path.Combine(DataFolder, "replay", "downloaded.lmp"), bytes); string addons = ""; if (replayInfo.HasAddons) { addons = "-file "; List <string> charactersDirectoryFiles = new List <string>(Directory.GetFiles("/root/.srb2/addons/Characters") .Select(Path.GetFileName)); List <string> levelsDirectoryFiles = new List <string>(Directory.GetFiles("/root/.srb2/addons/Levels") .Select(Path.GetFileName)); foreach (var addonfname in replayInfo.AddonsFileNames) { string addonName = System.Text.Encoding.ASCII.GetString(addonfname); var charMatch = charactersDirectoryFiles.Where(stringToCheck => stringToCheck == addonName); if (charMatch.Any()) { addons += "Characters/"; } ; var levelMatch = levelsDirectoryFiles.Where(stringToCheck => stringToCheck == addonName); if (levelMatch.Any()) { addons += "Levels/"; } ; addons += addonName + ' '; } } GameProcess.StartInfo.Arguments = "./reptovid -home /root -playdemo replay/downloaded.lmp " + addons + "-- :1"; GameProcess.StartInfo.FileName = "xinit"; GameProcess.StartInfo.WorkingDirectory = ExecutableFolder; GameProcess.Start(); bool nomap = false; string errorMessage = ""; do { Thread.Sleep(2000); string logFile = File.ReadAllText(LogFilePath); if (logFile.Contains("I_Error()")) { nomap = true; File.WriteAllText(LogFilePath, "a"); try { GameProcess.Kill(true); } catch { } } if (logFile.Contains("Process killed by signal: ")) { errorMessage = logFile.Split("Process killed by signal: ")[1]; File.WriteAllText(LogFilePath, "a"); try { GameProcess.Kill(true); } catch { } } }while (!GameProcess.HasExited); gifInfos = GifDir.GetFiles().OrderBy(file => file.LastWriteTime).ToArray(); CurrentGifFiles = new string[gifInfos.Length]; for (int i = 0; i < gifInfos.Length; i++) { CurrentGifFiles[i] = gifInfos[i].Name; } if (!CurrentGifFiles.SequenceEqual(PreviousGifFiles)) { string gifPath = gifInfos.Last().FullName; if (!string.IsNullOrEmpty(errorMessage)) { InvokeEventAndCleanup(ReplayStatus.GameError, replayPath, gifPath, outputPath, errorMessage); } InvokeEventAndCleanup(ReplayStatus.Success, replayPath, gifPath, outputPath); } else { if (nomap) { InvokeEventAndCleanup(ReplayStatus.NoMap, replayPath); } else { InvokeEventAndCleanup(ReplayStatus.BadDemo, replayPath); } } } catch (Exception e) { Console.WriteLine(e.Message); InvokeEventAndCleanup(ReplayStatus.UnhandledException, replayPath); } }