forked from Siniiitchibo/MatchZy_edit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoManagement.cs
More file actions
140 lines (118 loc) · 6.16 KB
/
DemoManagement.cs
File metadata and controls
140 lines (118 loc) · 6.16 KB
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
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Modules.Cvars;
using System.IO.Compression;
namespace MatchZy
{
public partial class MatchZy
{
public string demoPath = "MatchZy/";
public string demoUploadURL = "";
public string activeDemoFile = "";
public bool isDemoRecording = false;
public void StartDemoRecording()
{
string formattedTime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss").Replace(" ", "_");
string demoFileName = $"{formattedTime}_{liveMatchId}_{Server.MapName}_{matchzyTeam1.teamName.Replace(" ", "_")}_vs_{matchzyTeam2.teamName.Replace(" ", "_")}";
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())
using (var formData = new MultipartFormDataContent())
{
Log($"[UploadDemoAsync] Going to upload demo on {demoUploadURL}. Complete path: {demoPath}");
if (!File.Exists(demoPath))
{
Log($"[UploadDemoAsync ERROR] File not found: {demoPath}");
return;
}
var compressedFilePath = Path.ChangeExtension(demoPath, "zip"); // Change to ".gz" for GZip compression
using (var compressedFileStream = new FileStream(compressedFilePath, FileMode.Create))
using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Create))
{
// Add the .dem file to the zip archive
var zipEntry = zipArchive.CreateEntry(Path.GetFileName(demoPath));
using (var entryStream = zipEntry.Open())
using (var demoFileStream = new FileStream(demoPath, FileMode.Open, FileAccess.Read))
{
await demoFileStream.CopyToAsync(entryStream);
}
}
var compressedFileStreamContent = new StreamContent(new FileStream(compressedFilePath, FileMode.Open, FileAccess.Read));
compressedFileStreamContent.Headers.Add("Content-Type", "application/zip");
formData.Add(compressedFileStreamContent, "file", Path.GetFileName(compressedFilePath));
formData.Headers.Add("MatchZy-FileName", Path.GetFileName(compressedFilePath));
formData.Headers.Add("MatchZy-MatchId", matchId.ToString());
formData.Headers.Add("MatchZy-MapNumber", mapNumber.ToString());
var response = await httpClient.PostAsync(demoUploadURL, formData).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
Log($"[UploadDemoAsync] File upload successful for matchId: {matchId} mapNumber: {mapNumber} fileName: {Path.GetFileName(compressedFilePath)}.");
}
else
{
Log($"[UploadDemoAsync ERROR] Failed to upload file. Status code: {response.StatusCode}");
}
// Clean up: Delete the temporary compressed file
File.Delete(compressedFilePath);
}
}
catch (Exception e)
{
Log($"[UploadDemoAsync FATAL] An error occurred: {e.Message}");
}
}
}
}