|
5 | 5 | using CounterStrikeSharp.API.Modules.Commands;
|
6 | 6 | using CounterStrikeSharp.API.Modules.Entities.Constants;
|
7 | 7 | using CounterStrikeSharp.API.Modules.Utils;
|
| 8 | +using RetakesAllocatorCore.Config; |
8 | 9 | using RetakesAllocatorCore;
|
9 | 10 |
|
10 | 11 | namespace RetakesAllocator;
|
@@ -201,4 +202,91 @@ public static bool IsWindows()
|
201 | 202 | }
|
202 | 203 |
|
203 | 204 | public static bool IsVip(CCSPlayerController player) => AdminManager.PlayerHasPermissions(player, "@css/vip");
|
| 205 | + |
| 206 | + public static async Task DownloadMissingFiles() |
| 207 | + { |
| 208 | + string baseFolderPath = Configs.Shared.Module!; |
| 209 | + |
| 210 | + string gamedataFileName = "gamedata/RetakesAllocator_gamedata.json"; |
| 211 | + string gamedataGithubUrl = "https://raw.githubusercontent.com/yonilerner/cs2-retakes-allocator/main/Resources/RetakesAllocator_gamedata.json"; |
| 212 | + string gamedataFilePath = Path.Combine(baseFolderPath, gamedataFileName); |
| 213 | + string gamedataDirectoryPath = Path.GetDirectoryName(gamedataFilePath)!; |
| 214 | + await CheckAndDownloadFile(gamedataFilePath, gamedataGithubUrl, gamedataDirectoryPath); |
| 215 | + } |
| 216 | + |
| 217 | + public static async Task<bool> CheckAndDownloadFile(string filePath, string githubUrl, string directoryPath) |
| 218 | + { |
| 219 | + if (!File.Exists(filePath)) |
| 220 | + { |
| 221 | + if (!Directory.Exists(directoryPath)) |
| 222 | + { |
| 223 | + Directory.CreateDirectory(directoryPath); |
| 224 | + } |
| 225 | + await DownloadFileFromGithub(githubUrl, filePath); |
| 226 | + return true; |
| 227 | + } |
| 228 | + else |
| 229 | + { |
| 230 | + if (Configs.GetConfigData().AutoUpdateSignatures) |
| 231 | + { |
| 232 | + bool isFileDifferent = await IsFileDifferent(filePath, githubUrl); |
| 233 | + if (isFileDifferent) |
| 234 | + { |
| 235 | + File.Delete(filePath); |
| 236 | + await DownloadFileFromGithub(githubUrl, filePath); |
| 237 | + return true; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + } |
| 242 | + |
| 243 | + return false; |
| 244 | + } |
| 245 | + |
| 246 | + |
| 247 | + public static async Task<bool> IsFileDifferent(string localFilePath, string githubUrl) |
| 248 | + { |
| 249 | + try |
| 250 | + { |
| 251 | + byte[] localFileBytes = await File.ReadAllBytesAsync(localFilePath); |
| 252 | + string localFileHash = GetFileHash(localFileBytes); |
| 253 | + |
| 254 | + using (HttpClient client = new HttpClient()) |
| 255 | + { |
| 256 | + byte[] githubFileBytes = await client.GetByteArrayAsync(githubUrl); |
| 257 | + string githubFileHash = GetFileHash(githubFileBytes); |
| 258 | + return localFileHash != githubFileHash; |
| 259 | + } |
| 260 | + } |
| 261 | + catch (Exception ex) |
| 262 | + { |
| 263 | + Log.Debug($"Error comparing files: {ex.Message}"); |
| 264 | + return false; |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + public static string GetFileHash(byte[] fileBytes) |
| 269 | + { |
| 270 | + using (var md5 = System.Security.Cryptography.MD5.Create()) |
| 271 | + { |
| 272 | + byte[] hashBytes = md5.ComputeHash(fileBytes); |
| 273 | + return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant(); |
| 274 | + } |
| 275 | + } |
| 276 | + |
| 277 | + public static async Task DownloadFileFromGithub(string url, string destinationPath) |
| 278 | + { |
| 279 | + using (HttpClient client = new HttpClient()) |
| 280 | + { |
| 281 | + try |
| 282 | + { |
| 283 | + byte[] fileBytes = await client.GetByteArrayAsync(url); |
| 284 | + await File.WriteAllBytesAsync(destinationPath, fileBytes); |
| 285 | + } |
| 286 | + catch (Exception ex) |
| 287 | + { |
| 288 | + Log.Debug($"Error downloading file: {ex.Message}"); |
| 289 | + } |
| 290 | + } |
| 291 | + } |
204 | 292 | }
|
0 commit comments