Created
December 5, 2025 04:58
-
-
Save scturtle/3ac8798853566c4954c7350d49fb0359 to your computer and use it in GitHub Desktop.
pal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import struct | |
| class PalMessages: | |
| def __init__(self, pal_path): | |
| self.pal_path = pal_path | |
| mkf_path = os.path.join(self.pal_path, 'Sss.mkf') | |
| self._index_data = self._read_mkf_subfile(mkf_path, 3) | |
| msg_path = os.path.join(self.pal_path, 'M.msg') | |
| self._message_data = open(msg_path, 'rb').read() | |
| word_path = os.path.join(self.pal_path, 'Word.dat') | |
| self._words_data = open(word_path, 'rb').read() | |
| def _read_mkf_subfile(self, mkf_file, sub_id): | |
| with open(mkf_file, 'rb') as f: | |
| f.seek(0) | |
| f.seek(sub_id * 4) | |
| offset_bytes = f.read(8) | |
| start_offset = struct.unpack('<I', offset_bytes[0:4])[0] | |
| end_offset = struct.unpack('<I', offset_bytes[4:8])[0] | |
| length = end_offset - start_offset | |
| f.seek(start_offset) | |
| return f.read(length) | |
| def get_text(self, index): | |
| pos = index * 4 | |
| start_pos = struct.unpack('<I', self._index_data[pos:pos+4])[0] | |
| end_pos = struct.unpack('<I', self._index_data[pos+4:pos+8])[0] | |
| length = end_pos - start_pos | |
| raw_msg = self._message_data[start_pos:end_pos] | |
| return raw_msg.decode('gbk', errors='replace') | |
| def get_word(self, index): | |
| record_size = 10 | |
| pos = index * record_size | |
| raw_word = self._words_data[pos : pos + record_size] | |
| return raw_word.decode('gbk') | |
| def __getitem__(self, index): | |
| return self.get_text(index) | |
| if __name__ == "__main__": | |
| pal_msg = PalMessages(pal_path=".") | |
| for i in range(13513): | |
| print(f"Text[{i}]: {repr(pal_msg[i])}") | |
| for i in range(565): | |
| print(f"Word[{i}]: {repr(pal_msg.get_word(i))}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment