forked from shobhit-pathak/MatchZy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoManagement.cs
179 lines (150 loc) · 7.35 KB
/
DemoManagement.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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Cvars;
using System.IO.Compression;
using System.Net.Http.Json;
using System.Text;
namespace MatchZy
{
public partial class MatchZy
{
public string demoPath = "MatchZy/";
public string demoNameFormat = "{TIME}_{MATCH_ID}_{MAP}_{TEAM1}_vs_{TEAM2}";
public string demoUploadURL = "";
public string demoUploadHeaderKey = "";
public string demoUploadHeaderValue = "";
public string activeDemoFile = "";
public bool isDemoRecording = false;
public void StartDemoRecording()
{
string demoFileName = FormatDemoName();
try
{
string? directoryPath = Path.GetDirectoryName(Path.Join(Server.GameDirectory + "/csgo/", demoPath));
if (directoryPath != null)
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
}
string tempDemoPath = demoPath == "" ? demoFileName : demoPath + demoFileName;
activeDemoFile = tempDemoPath;
Log($"[StartDemoRecoding] Starting demo recording, path: {tempDemoPath}");
Server.ExecuteCommand($"tv_record {tempDemoPath}");
isDemoRecording = true;
}
catch (Exception ex)
{
Log($"[StartDemoRecording - FATAL] Error: {ex.Message}. Starting demo recording with path. Name: {demoFileName}");
// This is to avoid demo loss in any case of exception
Server.ExecuteCommand($"tv_record {demoFileName}");
isDemoRecording = true;
}
}
public void StopDemoRecording(float delay, string activeDemoFile, long liveMatchId, int currentMapNumber)
{
Log($"[StopDemoRecording] Going to stop demorecording in {delay}s");
string demoPath = Path.Join(Server.GameDirectory + "/csgo/", activeDemoFile);
AddTimer(delay, () =>
{
if (isDemoRecording) Server.ExecuteCommand($"tv_stoprecord");
AddTimer(15, () =>
{
Task.Run(async () =>
{
await UploadDemoAsync(demoPath, liveMatchId, currentMapNumber);
});
});
});
}
public int GetTvDelay()
{
bool tvEnable = ConVar.Find("tv_enable")!.GetPrimitiveValue<bool>();
if (!tvEnable) return 0;
bool tvEnable1 = ConVar.Find("tv_enable1")!.GetPrimitiveValue<bool>();
int tvDelay = ConVar.Find("tv_delay")!.GetPrimitiveValue<int>();
if (!tvEnable1) return tvDelay;
int tvDelay1 = ConVar.Find("tv_delay1")!.GetPrimitiveValue<int>();
if (tvDelay < tvDelay1) return tvDelay1;
return tvDelay;
}
public async Task UploadDemoAsync(string? demoPath, long matchId, int mapNumber)
{
if (demoPath == null || demoUploadURL == "")
{
Log($"[UploadDemoAsync] Not able to upload demo, either demoPath or demoUploadURL is not set. demoPath: {demoPath} demoUploadURL: {demoUploadURL}");
}
try
{
using var httpClient = new HttpClient();
Log($"[UploadDemoAsync] Going to upload demo on {demoUploadURL}. Complete path: {demoPath}");
if (!File.Exists(demoPath))
{
Log($"[UploadDemoAsync ERROR] File not found: {demoPath}");
return;
}
using FileStream fileStream = File.OpenRead(demoPath);
byte[] fileContent = new byte[fileStream.Length];
await fileStream.ReadAsync(fileContent, 0, (int)fileStream.Length);
using ByteArrayContent content = new ByteArrayContent(fileContent);
content.Headers.Add("Content-Type", "application/octet-stream");
content.Headers.Add("MatchZy-FileName", Path.GetFileName(demoPath));
content.Headers.Add("MatchZy-MatchId", matchId.ToString());
content.Headers.Add("MatchZy-MapNumber", mapNumber.ToString());
// For Get5 Panel
content.Headers.Add("Get5-FileName", Path.GetFileName(demoPath));
content.Headers.Add("Get5-MatchId", matchId.ToString());
content.Headers.Add("Get5-MapNumber", mapNumber.ToString());
if (!string.IsNullOrEmpty(demoUploadHeaderKey))
{
httpClient.DefaultRequestHeaders.Add(demoUploadHeaderKey, demoUploadHeaderValue);
}
HttpResponseMessage response = await httpClient.PostAsync(demoUploadURL, content);
if (response.IsSuccessStatusCode)
{
Log($"[UploadDemoAsync] File upload successful for matchId: {matchId} mapNumber: {mapNumber} fileName: {Path.GetFileName(demoPath)}.");
}
else
{
Log($"[UploadDemoAsync ERROR] Failed to upload file. Status code: {response.StatusCode} Response: {await response.Content.ReadAsStringAsync()}");
}
}
catch (Exception e)
{
Log($"[UploadDemoAsync FATAL] An error occurred: {e.Message}");
}
}
private string FormatDemoName()
{
string formattedTime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
var demoName = demoNameFormat
.Replace("{TIME}", formattedTime)
.Replace("{MATCH_ID}", $"{liveMatchId}")
.Replace("{MAP}", Server.MapName)
.Replace("{MAPNUMBER}", matchConfig.CurrentMapNumber.ToString())
.Replace("{TEAM1}", matchzyTeam1.teamName)
.Replace("{TEAM2}", matchzyTeam2.teamName)
.Replace(" ", "_");
return $"{demoName}.dem";
}
[ConsoleCommand("get5_demo_upload_header_key", "If defined, a custom HTTP header with this name is added to the HTTP requests for demos")]
[ConsoleCommand("matchzy_demo_upload_header_key", "If defined, a custom HTTP header with this name is added to the HTTP requests for demos")]
public void DemoUploadHeaderKeyCommand(CCSPlayerController? player, CommandInfo command)
{
if (player != null) return;
string header = command.ArgByIndex(1).Trim();
if (header != "") demoUploadHeaderKey = header;
}
[ConsoleCommand("get5_demo_upload_header_value", "If defined, the value of the custom header added to the demos sent over HTTP")]
[ConsoleCommand("matchzy_demo_upload_header_value", "If defined, the value of the custom header added to the demos sent over HTTP")]
public void DemoUploadHeaderValueCommand(CCSPlayerController? player, CommandInfo command)
{
if (player != null) return;
string headerValue = command.ArgByIndex(1).Trim();
if (headerValue != "") demoUploadHeaderValue = headerValue;
}
}
}