# Path: mgz/util.py
# def get_version(game_version, save_version, log_version):
#     """Get version based on version fields."""
#     if game_version == 'VER 9.3':
#         return Version.AOK
#     if game_version == 'VER 9.4':
#         if log_version == 3:
#             return Version.AOC10
#         if log_version == 5 or save_version >= 12.97:
#             return Version.DE
#         if save_version >= 12.36:
#             return Version.HD
#         if log_version == 4:
#             return Version.AOC10C
#         return Version.AOC
#     if game_version == 'VER 9.8':
#         return Version.USERPATCH12
#     if game_version == 'VER 9.9':
#         return Version.USERPATCH13
#     if game_version == 'VER 9.A':
#         return Version.USERPATCH14RC2
#     if game_version in ['VER 9.B', 'VER 9.C', 'VER 9.D']:
#         return Version.USERPATCH14
#     if game_version in ['VER 9.E', 'VER 9.F']:
#         return Version.USERPATCH15
#     if game_version == 'MCP 9.F':
#         return Version.MCP
#     if log_version is not None or game_version != 'VER 9.4':
#         raise ValueError('unsupported version: {}, {}, {}'.format(game_version, save_version, log_version))
# 
# class Version(Enum):
#     """Version enumeration.
# 
#     Using consts from https://github.com/goto-bus-stop/recanalyst/blob/master/src/Model/Version.php
#     for consistency.
#     """
#     AOK = 1
#     AOC = 4
#     AOC10 = 5
#     AOC10C = 8
#     USERPATCH12 = 12
#     USERPATCH13 = 13
#     USERPATCH14 = 11
#     USERPATCH15 = 20
#     DE = 21
#     USERPATCH14RC2 = 22
#     MCP = 30
#     HD = 19


import io
import hashlib
import re
import struct
import uuid
import zlib
from mgz.util import get_version, Version
    anchor = header.read().find(b'\x05\x00' + gaia + b'\x00')
    header.seek(cur + anchor - num_players - 43)
    mod = parse_mod(header, num_players, version)
    players = [parse_player(header, number, num_players) for number in range(num_players)]
    for _ in range(num_players):
        header.read(4)
        entries = unpack('<i', header)
        header.read(5 + (entries * 44))
        points = unpack('<i', header)
        header.read(8 + (points * 32))
    return players, mod


def parse_metadata(header):
    """Parse recorded game metadata."""
    ai, game_speed, owner_id, num_players, cheats = unpack('<I24xf17xhbxb', header)
    if ai > 0:
        raise RuntimeError("don't know how to parse ai")
    return dict(
        speed=game_speed,
        owner_id=owner_id,
        cheats=cheats == 1
    ), num_players


def parse(data):
    """Parse recorded game header."""
    try:
        header = decompress(data)
        version, game, save, log = parse_version(header, data)

# Path: mgz/util.py
# def get_version(game_version, save_version, log_version):
#     """Get version based on version fields."""
#     if game_version == 'VER 9.3':
#         return Version.AOK
#     if game_version == 'VER 9.4':
#         if log_version == 3:
#             return Version.AOC10
#         if log_version == 5 or save_version >= 12.97:
#             return Version.DE
#         if save_version >= 12.36:
#             return Version.HD
#         if log_version == 4:
#             return Version.AOC10C
#         return Version.AOC
#     if game_version == 'VER 9.8':
#         return Version.USERPATCH12
#     if game_version == 'VER 9.9':
#         return Version.USERPATCH13
#     if game_version == 'VER 9.A':
#         return Version.USERPATCH14RC2
#     if game_version in ['VER 9.B', 'VER 9.C', 'VER 9.D']:
#         return Version.USERPATCH14
#     if game_version in ['VER 9.E', 'VER 9.F']:
#         return Version.USERPATCH15
#     if game_version == 'MCP 9.F':
#         return Version.MCP
#     if log_version is not None or game_version != 'VER 9.4':
#         raise ValueError('unsupported version: {}, {}, {}'.format(game_version, save_version, log_version))
# 
# class Version(Enum):
#     """Version enumeration.
# 
#     Using consts from https://github.com/goto-bus-stop/recanalyst/blob/master/src/Model/Version.php
#     for consistency.
#     """
#     AOK = 1
#     AOC = 4
#     AOC10 = 5
#     AOC10C = 8
#     USERPATCH12 = 12
#     USERPATCH13 = 13
#     USERPATCH14 = 11
#     USERPATCH15 = 20
#     DE = 21
#     USERPATCH14RC2 = 22
#     MCP = 30
#     HD = 19


# Path: mgz/fast/header.py
import io
import hashlib
import re
import struct
import uuid
import zlib
from mgz.util import get_version, Version

    anchor = header.read().find(b'\x05\x00' + gaia + b'\x00')
    header.seek(cur + anchor - num_players - 43)
    mod = parse_mod(header, num_players, version)
    players = [parse_player(header, number, num_players) for number in range(num_players)]
    for _ in range(num_players):
        header.read(4)
        entries = unpack('<i', header)
        header.read(5 + (entries * 44))
        points = unpack('<i', header)
        header.read(8 + (points * 32))
    return players, mod


def parse_metadata(header):
    """Parse recorded game metadata."""
    ai, game_speed, owner_id, num_players, cheats = unpack('<I24xf17xhbxb', header)
    if ai > 0:
        raise RuntimeError("don't know how to parse ai")
    return dict(
        speed=game_speed,
        owner_id=owner_id,
        cheats=cheats == 1
    ), num_players


def parse(data):
    """Parse recorded game header."""
    try:
        header = decompress(data)
        version, game, save, log = parse_version(header, data)

        if version not in (Version.USERPATCH15, Version.DE, Version.HD):