-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAnalyzer.cs
More file actions
101 lines (86 loc) · 3.12 KB
/
Analyzer.cs
File metadata and controls
101 lines (86 loc) · 3.12 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
using Heroes.ReplayParser;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Hotsapi.Uploader.Common
{
public class Analyzer : IAnalyzer
{
public int MinimumBuild { get; set; }
private static Logger _log = LogManager.GetCurrentClassLogger();
/// <summary>
/// Analyze replay locally before uploading
/// </summary>
/// <param name="file">Replay file</param>
public Replay Analyze(ReplayFile file)
{
try {
var result = DataParser.ParseReplay(file.Filename, false, ParseOptions.MinimalParsing);
var replay = result.Item2;
var parseResult = result.Item1;
var status = GetPreStatus(replay, parseResult);
if (status != null) {
file.UploadStatus = status.Value;
}
if (parseResult != DataParser.ReplayParseResult.Success) {
return null;
}
file.Fingerprint = GetFingerprint(replay);
return replay;
}
catch (Exception e) {
_log.Warn(e, $"Error analyzing file {file}");
return null;
}
}
public UploadStatus? GetPreStatus(Replay replay, DataParser.ReplayParseResult parseResult)
{
switch (parseResult) {
case DataParser.ReplayParseResult.ComputerPlayerFound:
case DataParser.ReplayParseResult.TryMeMode:
return UploadStatus.AiDetected;
case DataParser.ReplayParseResult.PTRRegion:
return UploadStatus.PtrRegion;
case DataParser.ReplayParseResult.PreAlphaWipe:
return UploadStatus.TooOld;
}
if (parseResult != DataParser.ReplayParseResult.Success) {
return null;
}
if (replay.GameMode == GameMode.Custom) {
return UploadStatus.CustomGame;
}
if (replay.ReplayBuild < MinimumBuild) {
return UploadStatus.TooOld;
}
return null;
}
/// <summary>
/// Get unique hash of replay. Compatible with HotsLogs
/// </summary>
/// <param name="replay"></param>
/// <returns></returns>
public string GetFingerprint(Replay replay)
{
var str = new StringBuilder();
replay.Players.Select(p => p.BattleNetId).OrderBy(x => x).Map(x => str.Append(x.ToString()));
str.Append(replay.RandomValue);
var md5 = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str.ToString()));
var result = new Guid(md5);
return result.ToString();
}
/// <summary>
/// Swaps two bytes in a byte array
/// </summary>
private void SwapBytes(byte[] buf, int i, int j)
{
byte temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}