forked from Giza/Broken-Sword-SotT-Reforged
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0fc1016
Showing
4 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import struct | ||
import csv | ||
|
||
def extract_texts(binary_file, csv_file): | ||
""" | ||
Извлекает тексты из бинарного файла и записывает их в CSV-файл. | ||
Args: | ||
binary_file (str): Путь к бинарному файлу. | ||
csv_file (str): Путь к CSV-файлу для записи результатов. | ||
""" | ||
|
||
with open(binary_file, "rb") as bin_file, open(csv_file, "w", newline="", encoding="utf-8") as csv_file: | ||
# Пропускаем первые 8 байт | ||
bin_file.seek(8) | ||
|
||
# Считываем количество текстов | ||
text_count = struct.unpack("<I", bin_file.read(4))[0] | ||
#print(text_count) | ||
|
||
# Создаем объект CSV-писателя | ||
writer = csv.writer(csv_file) | ||
|
||
# Записываем заголовок CSV-файла | ||
writer.writerow(["id_текста", "текст"]) | ||
|
||
# Считываем информацию о текстах | ||
for _ in range(text_count): | ||
|
||
# Считываем ID и оффсет текста | ||
text_id, text_offset = struct.unpack("<II", bin_file.read(8)) | ||
|
||
current_offset = bin_file.tell() | ||
|
||
# Перемещаемся к началу текста | ||
bin_file.seek(text_offset) | ||
|
||
# Считываем текст | ||
text = "" | ||
byte = bin_file.read(1) | ||
while byte != b"\x00": # Читаем до нулевого байта | ||
text += byte.decode("latin-1") | ||
byte = bin_file.read(1) | ||
|
||
# Записываем информацию в CSV-файл | ||
writer.writerow([text_id, text]) | ||
|
||
bin_file.seek(current_offset) | ||
|
||
|
||
if __name__ == "__main__": | ||
# Замените эти значения на ваши пути к файлам | ||
binary_file_path = "file_3735885291.dat" | ||
csv_file_path = "extracted_texts.csv" | ||
|
||
extract_texts(binary_file_path, csv_file_path) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import struct | ||
import csv | ||
|
||
def pack_texts(csv_file, binary_file): | ||
""" | ||
Упаковывает тексты из CSV-файла в бинарный файл. | ||
Args: | ||
csv_file (str): Путь к CSV-файлу с текстами. | ||
binary_file (str): Путь к бинарному файлу для записи. | ||
""" | ||
|
||
with open(csv_file, "r", newline="", encoding="utf-8") as csv_file, open(binary_file, "wb") as bin_file: | ||
bin_file.write(b"\x54\x45\x58\x54") | ||
# Записываем первые 8 байт (зарезервированные) | ||
bin_file.write(b"\x00" * 4) | ||
|
||
# Считываем данные из CSV | ||
reader = csv.reader(csv_file) | ||
|
||
# Записываем количество текстов | ||
text_count = len(list(reader)) | ||
bin_file.write(struct.pack("<I", text_count-1)) | ||
|
||
# Возвращаемся к началу файла | ||
csv_file.seek(0) | ||
next(reader) # Пропускаем заголовок | ||
|
||
text_list = [] | ||
|
||
# Текущий оффсет в бинарном файле | ||
current_offset = bin_file.tell() | ||
file_offset=(text_count-1)*8+12 | ||
# Записываем тексты | ||
for row in reader: | ||
text_id, text, text_tl = row | ||
|
||
# Записываем ID и оффсет текста | ||
bin_file.write(struct.pack("<II", int(text_id), file_offset)) | ||
|
||
# Записываем текст с нулевым байтом в конце | ||
texts = text_tl.encode("utf-8")+b"\x00" | ||
text_list.append(texts) | ||
|
||
# Обновляем текущий оффсет | ||
file_offset += len(texts) | ||
|
||
for i, text in enumerate(text_list): | ||
bin_file.write(text) | ||
|
||
if __name__ == "__main__": | ||
# Замените эти значения на ваши пути к файлам | ||
csv_file_path = "extracted_texts.csv" | ||
binary_file_path = "file_3735885291.dat" | ||
|
||
pack_texts(csv_file_path, binary_file_path) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import struct | ||
import zlib | ||
import os | ||
|
||
def pack_vt7a(input_dir, archive_path): | ||
# Получаем список файлов в директории | ||
files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f))] | ||
num_files = len(files) | ||
|
||
# Создаем заголовок | ||
signature = b"VT7A" | ||
version = 2 | ||
unknown = b"\x5E\x0B\x99\x8F" | ||
header = struct.pack("<4sI4sI", signature, version, unknown, num_files) | ||
|
||
# Открываем архив для записи | ||
with open(archive_path, 'wb') as archive_file: | ||
# Записываем заголовок | ||
archive_file.write(header) | ||
|
||
# Список для хранения сжатых данных | ||
compressed_data_list = [] | ||
|
||
# Обрабатываем каждый файл | ||
file_offset = len(header)*8 | ||
for i, file_name in enumerate(files): | ||
file_number = int(file_name.split("_")[1].split(".")[0]) | ||
file_path = os.path.join(input_dir, file_name) | ||
|
||
# Считываем файл | ||
with open(file_path, 'rb') as file_to_pack: | ||
data = file_to_pack.read() | ||
compressed_data = zlib.compress(data) # Сжатие файла | ||
compressed_length = len(compressed_data) | ||
decompressed_length = len(data) | ||
|
||
# Записываем информацию о файле | ||
file_info = struct.pack("<4I", file_number, file_offset, decompressed_length, compressed_length) | ||
|
||
archive_file.write(file_info) | ||
|
||
# Записываем данные файла | ||
compressed_data_list.append(compressed_data) | ||
|
||
# Обновляем смещение для следующего файла | ||
file_offset += len(file_info)-16 + compressed_length | ||
for i, compressed_data in enumerate(compressed_data_list): | ||
archive_file.write(compressed_data) | ||
# Пример использования | ||
pack_vt7a("text", "text.vt7a") |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import struct | ||
import zlib | ||
import os | ||
|
||
def unpack_vt7a(archive_path, output_dir): | ||
# Открываем архив для чтения | ||
with open(archive_path, 'rb') as archive_file: | ||
# Читаем заголовок | ||
header = archive_file.read(16) | ||
signature, version, unknown, num_files = struct.unpack("<4sI4sI", header) | ||
|
||
current_offset = archive_file.tell() | ||
# Обрабатываем информацию о каждом файле | ||
for _ in range(num_files): | ||
|
||
archive_file.seek(current_offset) | ||
# Читаем информацию о файле из каталога | ||
file_info = archive_file.read(16) | ||
|
||
unknown, file_offset, decompressed_length, compressed_length = struct.unpack("<4I", file_info) | ||
|
||
current_offset = archive_file.tell() | ||
# Перемещаем указатель к данным файла | ||
archive_file.seek(file_offset) | ||
|
||
# Читаем данные файла | ||
data = archive_file.read(compressed_length) | ||
|
||
# Разархивируем файл, если он сжат | ||
if compressed_length > 0: | ||
data = zlib.decompress(data) | ||
|
||
# Сохраняем файл | ||
file_name = f"file_{unknown}.dat" # Пример имени файла | ||
with open(os.path.join(output_dir, file_name), 'wb') as output_file: | ||
output_file.write(data) | ||
|
||
|
||
# Пример использования | ||
unpack_vt7a("text.vt7a", "text") |