File size: 1,819 Bytes
ce81a16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace Quantum {
  public class ChecksumVerification : IDisposable {
    private Dictionary<int, ChecksumFile.ChecksumEntry> _checksums;
    private Quantum.CallbackDispatcher _gameCallbacks;
    private bool _verbose;

    public ChecksumVerification(string pathToChecksumFile, Quantum.CallbackDispatcher callbacks, bool verbose = false) {
      _checksums = JsonConvert.DeserializeObject<ChecksumFile>(File.ReadAllText(pathToChecksumFile), ReplayJsonSerializerSettings.GetSettings()).ToDictionary();
      _gameCallbacks = callbacks;
      _gameCallbacks.Subscribe(this, (CallbackSimulateFinished callback) => OnSimulateFinished(callback.Game, callback.Frame));
      _verbose = verbose;
    }

    private void OnSimulateFinished(QuantumGame game, Frame frame) {
      if (frame != null) {
        var f = frame.Number;
        var cs = ChecksumFileHelper.UlongToLong(frame.CalculateChecksum());

        if (_checksums != null) {

          if (_checksums.ContainsKey(f)) {
            Console.Write($"{f,6} {cs,25} ");
            if (cs != _checksums[f].ChecksumAsLong) {
              Console.ForegroundColor = ConsoleColor.Red;
              Console.Write("(failed)");
            } else {
              Console.ForegroundColor = ConsoleColor.Green;
              Console.Write("(verified)");
            }
            Console.Write("\n");
          } else if (_verbose) {
            Console.Write($"{f,6} {cs,25} ");
            Console.Write("(skipped)");
          }
          Console.ForegroundColor = ConsoleColor.Gray;
        }
      }
    }

    public void Dispose() {
      if (_gameCallbacks != null) {
        _gameCallbacks.UnsubscribeListener(this);
        _gameCallbacks = null;
      }
    }
  }
}