シリアライゼーション + エンコーディングの形式だと思います。
基本的に、さまざまな秘密の値を含むバイト配列を生成する必要があります (これは、ほとんどのバイナリ プロトコルの記述方法です)。次に、各バイトを対応する文字 (またはそれ以上) にエンコードして、出力 "文字列" を読み取れるようにします。Python でデータをバイト配列にシリアル化する方法のサンプルを次に示します。
プレイヤー情報
player_name = "Robert"
player_stats = {
"health": 200,
"mana": 150,
"has_sword": True,
"has_arrows": False,
"has_shovel": True,
"dungeon1_complete": True
}
シリアライゼーション情報の設定
# If these can be greater than 255, they must be stored across multiple bytes - extra work
byte_attribs = ["health", "mana"]
bit_attribs = ["has_sword", "has_arrows", "has_shovel", "dungeon1_complete"]
player_name_max_length = 7
byte_attrib_offset = player_name_max_length
bit_attrib_offset = byte_attrib_offset + len(byte_attribs)
secret_storage = bytearray(bit_attrib_offset + len(bit_attribs))
assert(len(player_name) <= player_name_max_length)
シリアル化を実行する
# Serialize Player Name
secret_storage[:player_name_max_length] = player_name.rjust(player_name_max_length)
# Serialize attributes:
for idx, attrib in enumerate(byte_attribs):
secret_storage[byte_attrib_offset + idx] = player_stats[attrib]
for idx, attrib in enumerate(bit_attribs):
byte_offset = idx // 8 # attribs 0-7 go in byte 0, etc.
bit_offset = idx % 8
# Bit manipulation examples: http://wiki.python.org/moin/BitManipulation
current_byte = bit_attrib_offset + byte_offset
if player_stats[attrib]:
mask = 1 << bit_offset
secret_storage[current_byte] = secret_storage[current_byte] | mask
else:
mask = ~(1 << bit_offset)
secret_storage[current_byte] = secret_storage[current_byte] & mask
個々の値を取得する
print "Storage array encoded as ascii:", secret_storage
# Access name:
print "Name:", secret_storage[:player_name_max_length].lstrip()
# >>> Name: Robert
# Access byte values:
attrib_idx = byte_attribs.index("mana")
print "Mana level:", secret_storage[byte_attrib_offset + attrib_idx]
# >>> Mana level: 150
# Access bit flags:
attrib_idx = bit_attribs.index("dungeon1_complete")
print "Completed Dungeon 1:", bool(secret_storage[bit_attrib_offset + (attrib_idx // 8)] & (1 << attrib_idx % 8))
# >>> Completed Dungeon 1: True