Skip to content

Commit

Permalink
both read cbl and read cbr are working now
Browse files Browse the repository at this point in the history
  • Loading branch information
walker8088 committed Sep 8, 2024
1 parent d5c7467 commit 40149ea
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 47 deletions.
30 changes: 22 additions & 8 deletions src/cchess/read_cbl.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,36 @@
'''

import struct
from .read_cbr import read_from_cbr_buffer, CODING_PAGE

#from .exception import CChessException
#from .board import ChessBoard

#-----------------------------------------------------#

def read_from_cbl(file_name):

with open(file_name, "rb") as f:
contents = f.read()

magic, _i1, lib_name = struct.unpack("<16s48s512s", contents[:576])
print(magic.decode("GB18030"))
print(lib_name[:19].decode("utf-8"))
bmagic, _i1, book_count, blib = struct.unpack("<16s44si512s", contents[:576])

if magic != b"XQ":
if bmagic != b'CCBridgeLibrary\x00':
return None


lib_info = {}
lib_name = blib.decode(CODING_PAGE)

lib_info['name'] = lib_name
lib_info['games'] = []

index = 101952
count = 0
while index < len(contents):
book_buffer = contents[index:]
game = read_from_cbr_buffer(book_buffer)
lib_info['games'].append(game)
count += 1
index += 4096
print(count+1, game.info['title'])

return lib_info

#read_from_cbl('D:\\01_MyRepos\\cchess\\tests\\data\\1956年全国象棋锦标赛93局.CBL')
77 changes: 40 additions & 37 deletions src/cchess/read_cbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

import sys
import struct

Expand All @@ -23,6 +24,8 @@
from .board import ChessPlayer, ChessBoard
from .game import Game

CODING_PAGE = 'utf-16-le'

#-----------------------------------------------------#
piece_dict = {
#红方
Expand All @@ -44,21 +47,22 @@
}

def _decode_pos(p):
x = p % 9
y = 9 - p // 9
return (x,y)

def _decode_pos2(p_from, p_to):
return (_decode_pos(p_from), _decode_pos(p_to))
return (p%9, 9-p//9)

def cut_bytes_to_str(buff):
#print(buff.hex())
zero_index = buff.find(b'\x00\x00\x00')
#print(zero_index)
return buff[:zero_index].decode(CODING_PAGE)

#-----------------------------------------------------#
class CbrBuffDecoder(object):
def __init__(self, buffer):
def __init__(self, buffer, coding):
self.buffer = buffer
self.index = 0
self.length = len(buffer)

self.coding = coding

def __read(self, size):

start = self.index
Expand All @@ -70,16 +74,10 @@ def __read(self, size):
self.index = stop
return self.buffer[start:stop]

def read_str(self, size, coding="GB18030"):
def read_str(self, size):
buff = self.__read(size)

try:
ret = buff.decode(coding)
except Exception:
ret = None

return ret

return cut_bytes_to_str(buff)

def read_bytes(self, size):
return bytearray(self.__read(size))

Expand Down Expand Up @@ -135,7 +133,7 @@ def __read_steps(buff_decoder, game, parent_move, board):

fench = board.get_fench(move_from)
if not fench:
raise CChessException("bad move at %s %s" % (str(move_from), str(move_to)))
raise CChessException(f"bad move: {board.to_fen()} {move_from}, {move_to}")
good_move = parent_move
else:
_, man_side = fench_to_species(fench)
Expand All @@ -146,7 +144,7 @@ def __read_steps(buff_decoder, game, parent_move, board):
#认为当前走子一方就是合理一方,避免过多走子方检查
curr_move = board.move(move_from, move_to)
curr_move.annote = annote
print(step_info.hex(), curr_move.to_text(), has_var_step, annote_len)
#print(step_info.hex(), curr_move.to_text(), has_var_step, annote_len)
#board.print_board()

if parent_move:
Expand All @@ -155,35 +153,29 @@ def __read_steps(buff_decoder, game, parent_move, board):
game.append_first_move(curr_move)
good_move = curr_move
else:
board.print_board()
print ("bad move at", move_from, move_to)
raise CChessException(f"bad move: {board.to_fen()} {move_from}, {move_to}")
good_move = parent_move

if has_next_move:
__read_steps(buff_decoder, game, good_move, board)

if has_var_step:
print(parent_move.to_text(), 'read var moves')
#print(parent_move.to_text(), 'read var moves')
__read_steps(buff_decoder, game, parent_move, board_bak)


def read_from_cbr(file_name):

with open(file_name, "rb") as f:
contents = f.read()

magic, _i1, title, _i2, move_side, _i3, boards, _i4 = struct.unpack("<16s164s128s1804sB7s90s4s", contents[:2214])
#-----------------------------------------------------#
def read_from_cbr_buffer(contents):

bmagic, _i1, btitle, _i2, move_side, _i3, boards, _i4 = struct.unpack("<16s164s128s1804sB7s90s4s", contents[:2214])

#print(move_side)
#print(title[:10].decode("GBK"))
print(str(magic).rstrip('\x00'))
#if magic != b"CCBridge Record":
# return None
if bmagic != b"CCBridge Record\x00":
return None

game_info = {}
game_info["source"] = "CBR"
game_info['title'] = cut_bytes_to_str(btitle)

buff_decoder = CbrBuffDecoder(contents[2214:])
board = ChessBoard()
if move_side == 1:
board.move_player = ChessPlayer(RED)
Expand All @@ -196,13 +188,24 @@ def read_from_cbr(file_name):
if v in piece_dict:
board.put_fench(piece_dict[v], (x, y))

print(board.to_fen())
board.print_board()
#board.print_board()

buff_decoder = CbrBuffDecoder(contents[2214:], CODING_PAGE)
game_annotation = __read_init_info(buff_decoder)

game = Game(board, game_annotation)
game.info = game_info

__read_steps(buff_decoder, game, None, board)

return game


#-----------------------------------------------------#
def read_from_cbr(file_name):

with open(file_name, "rb") as f:
contents = f.read()

return read_from_cbr_buffer(contents)

Binary file modified tests/test2.cbr
Binary file not shown.
20 changes: 18 additions & 2 deletions tests/test_read_cbr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@

import cchess

game = cchess.read_from_cbr('test.cbr')
lib = cchess.read_from_cbl('D:\\01_MyRepos\\cchess\\tests\\data\\1956年全国象棋锦标赛93局.CBL')

#print(lib['name'])
#for game in lib['games']:
#print('\n=====================================')
#print(game.info['title'])
#game.print_init_board()
#print('=====================================')
#if game.annote:
# print(game.annote)
# print('-------------------------------------')
#game.print_text_moves(steps_per_line = 1, show_annote = True)

'''
game = cchess.read_from_cbr('test2.cbr')
print('\n=====================================')
game.print_init_board()
print('=====================================')
if game.annote:
print(game.annote)
print('-------------------------------------')
game.print_text_moves(steps_per_line = 5, show_annote = False)
game.print_text_moves(steps_per_line = 1, show_annote = True)
print('李3'.encode('utf-16-le').hex())
'''

0 comments on commit 40149ea

Please sign in to comment.