コード例 #1
0
ファイル: counters.py プロジェクト: patricksurry/jspy-vassal
def decodePiece(type, state):
    # nested decorator structure gets represented as pairs of tab-separated types & states
    # we'll return as a list instead
    types = disconcat(type, '\t')
    states = disconcat(state, '\t')
    if len(types) != len(states) or len(types) > 2:
        raise SyntaxError("Mismatched nested piece definition")

    t, s = types[0], states[0]
    kind, *maybeSpec = disconcat(t, ';', 1)
    spec = maybeSpec[0] if maybeSpec else None
    piece = dict(kind=kind)
    if kind in _pieceDecoders:
        try:
            piece.update(_pieceDecoders[kind](spec, s))
        except:
            print(
                "Failed to decode {:s} with spec='{!s}', state='{!s}'".format(
                    kind, spec, s))
            raise
    else:
        _missingPieceDecoders[kind] = _missingPieceDecoders.setdefault(
            kind, 0) + 1
        piece.update(dict(type=t, state=s))

    result = [piece]
    if len(types) == 2:
        result = decodePiece(types[1], states[1]) + result
    return result
コード例 #2
0
ファイル: gamepiece.py プロジェクト: patricksurry/jspy-vassal
def decodePieceImage(s):
    items = []
    for spec in disconcat(s, ','):
        typ, val = disconcat(spec, ';', 1)
        item = dict(kind=typ)
        if typ not in pieceImageProtos:
            item['spec'] = spec
        else:
            item.update(pieceImageProtos[typ](val))
        items.append(item)
    return items
コード例 #3
0
ファイル: component.py プロジェクト: patricksurry/jspy-vassal
def noteDecoder(s):
    notes = {}
    cmds = disconcat(s, COMMAND_SEPARATOR)
    for cmd in cmds:
        typ, *vs = disconcat(cmd, '\t')
        d = _noteTypes.get(typ)
        assert d, "Uncrecognized note type {:s} in {:s}".format(typ, s)
        note = dict(type=d['type'])
        if vs and vs[0]:
            note.update(seqdict(d['proto'], vs))
        notes[typ] = note
    return notes
コード例 #4
0
ファイル: gamepiece.py プロジェクト: patricksurry/jspy-vassal
def decodePieceLayout(s):
    items = []
    for spec in disconcat(s, ','):
        deriv, base = disconcat(spec, '|')
        typ, val = disconcat(deriv, ';', 1)
        item = dict(kind=typ)
        if typ not in pieceLayoutProtos:
            item['spec'] = spec
        else:
            item.update(pieceLayoutProtos['Item'](base))
            item.update(pieceLayoutProtos[typ](val))
        items.append(item)
    return items
コード例 #5
0
ファイル: component.py プロジェクト: patricksurry/jspy-vassal
def decodeComponent(state):
    cmd = disconcat(state, COMMAND_SEPARATOR)[0]
    id = disconcat(cmd, '\t')[0]
    result = dict(state=state)
    for kind, decoder in _componentDecoders.items():
        if kind in id:
            result = dict(kind=kind)
            try:
                result.update(decoder(state))
            except:
                print('Failed to parse state for {!s}: {:s}'.format(result, state))
                result['state'] = state
            break
    return result
コード例 #6
0
ファイル: translate.py プロジェクト: patricksurry/jspy-vassal
def decodeSave(fname):
    """
    Decode a savedGame which is ESC-separated with backlashed nested separators

    See ./module/GameState.java: getRestoreCommand()

    begin_save
    <versionCmd>  <= not clear if this is serialized?
    <piecesCmd>
    [<restoreComponent>]
    end_save
    """
    with ZipFile(fname).open('savedGame') as f:
        saved = f.read().decode('utf-8')
    content = deobfuscate(saved)
    base = path.splitext(fname)[0]
    with open(base + '.raw', 'w') as f:
        f.write(content)
    begin, *cmds, end = disconcat(content, COMMAND_SEPARATOR)
    assert begin == 'begin_save' and end == 'end_save', "Expected start/end markers in savedGame"
    while cmds:
        # skip empty cmds, not sure that checkVersion even gets serialized?
        if cmds[0]:
            break
        cmds.pop(0)
    assert cmds, "Expected some non-empty commands?!"
    pcs, *comps = cmds
    assert pcs[
        0] == COMMAND_SEPARATOR, 'expected leading separator for restorePieces in {!s}'.format(
            pcs)
    result = dict(
        restorePieces=[
            decodeCommand(cmd) for cmd in disconcat(pcs, COMMAND_SEPARATOR)[1:]
        ],
        components=[decodeComponent(c) for c in comps],
    )
    with open(base + '.json', 'w') as f:
        json.dump(result, f, indent=4)
コード例 #7
0
ファイル: translate.py プロジェクト: patricksurry/jspy-vassal
def decodeCommand(s):
    """
    module/BasicCommandEncoder.java

    encodes commands like below, though so far I only see + (add)
    id's are generally epoch time millis based on the object creation date

    +/id/type/state
    -/id
    D/id/state[/oldstate]
    M/id/mapid/x/y/underid/oldmapid/oldx/oldy/oldunderid/playerid
    """
    (c, id, *elts) = disconcat(s, '/')
    id = maybe(str)(id)
    assert c in _cmds, 'Unknown command {:s}'.format(c)
    cmd = _cmds[c]
    data = dict(id=id)
    if cmd == 'add':
        d = seqdict(['type', 'state'], elts)
        data['piece'] = decodePiece(**d)
    elif cmd == 'remove':
        assert len(elts) == 0, "Got {:d} args for remove, expected 0".format(
            len(elts))
    elif cmd == 'change':
        fields = ['state', 'oldstate']
        assert 1 <= len(elts) <= len(
            fields), "Got {:d} args for change, expected 1-2".format(len(elts))
        data.update(dict(zip(fields, elts)))
    elif cmd == 'move':
        proto = dict(newMapId=str,
                     newX=int,
                     newY=int,
                     newUnderId=str,
                     oldMapId=str,
                     oldX=int,
                     oldY=int,
                     oldUnderId=str,
                     playerId=str)
        data.update(seqdict(proto, elts))
    return {cmd: data}