I am studying d2s schema for making D2R MOD,
and found the checksum algorithm of Diablo 2 save file in the below link:
https://github.com/krisives/d2s-format#checksum
(Maybe it is correct because lot of people did cross-check about it)
and I tried to modify some byte datas in d2s file and use below code in python.
Although checksum was updated well, but failed to load this d2s file at D2R.
Is there any problem of below code?
d2s 파일 구조에 대해 잘 아시는 분이 있다면 도와주세요!
import struct def calculate_checksum(data: bytearray) -> int: """ Calculates the checksum for the given bytearray excluding the checksum field itself. Args: data (bytearray): The bytearray representing the file data. Returns: int: The calculated checksum value. """ sum = 0 for i in range(len(data)): ch = data[i] if 12 <= i < 16: ch = 0 sum = (sum << 1) + ch # Ensure sum stays within 32 bits sum &= 0xFFFFFFFF return sum def update_checksum(data: bytearray) -> None: """ Updates the checksum field in the bytearray to reflect the calculated checksum. Args: data (bytearray): The bytearray representing the file data. """ # Calculate the checksum excluding the current checksum bytes checksum = calculate_checksum(data) # Pack checksum into 4 bytes (little-endian) checksum_bytes = struct.pack('<I', checksum) # Update the checksum position (12-15 bytes) checksum_position = 12 data[checksum_position:checksum_position+4] = checksum_bytes def save_file(filename: str, data: bytearray) -> None: """ Saves the bytearray to a file. Args: filename (str): The path to the file. data (bytearray): The bytearray to save. """ with open(filename, 'wb') as file: file.write(data) def main(): filename = 'test.d2s' # Load file into bytearray with open(filename, 'rb') as file: data = bytearray(file.read()) # Update the checksum in the data update_checksum(data) # Save the modified file save_file(filename, data) if __name__ == "__main__": main()
New contributor
SuperRukie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.