Resource icon

strings_bin_converter 0.72 2006-12-28 15:07:21

Register   or   log in   to get this file
Resource icon

strings_bin_converter 0.72 2006-12-28 15:07:21

Register   or   log in   to get this file

Reviews 3.65 star(s) 20 reviews

import sys
import struct
import os
from pathlib import Path

def convert_file(filepath):
"""
Takes a type 2 .strings.bin file and converts it to a text file
"""
bin_path = Path(filepath + ".strings.bin")
txt_path = Path(filepath)

with open(bin_path, "rb") as f:
style1, = struct.unpack("h", f.read(2))
style2, = struct.unpack("h", f.read(2))

if style1 != 2 or style2 != 2048:
print(f"Skipping {bin_path}: unsupported format")
return

with open(txt_path, "w", encoding="utf-16") as fw:
# Write BOM + initial marker
fw.write("\ufeff\uac00\n")

length, = struct.unpack("i", f.read(4))

for _ in range(length):
strlen, = struct.unpack("h", f.read(2))
tagstr = "".join(
f.read(2).decode("utf-16") for _ in range(strlen)
)

strlen, = struct.unpack("h", f.read(2))
screenstr = ""

for _ in range(strlen):
temp = f.read(2)
e, = struct.unpack("H", temp)
if e == 10:
screenstr += "\\n"
else:
screenstr += temp.decode("utf-16")

fw.write(f"{{{tagstr}}}{screenstr}\n")

print(f"Converted: {bin_path} → {txt_path}")


def main():
if len(sys.argv) < 2:
print("Usage: python strings_bin_converter.py [all | file1 file2 ...]")
return

if sys.argv[1] == "all":
path = Path.cwd()
files = [p.stem.replace(".strings", "")
for p in path.glob("*.strings.bin")]

for i, filepath in enumerate(files, 1):
print(f"Converting {filepath} ({i} of {len(files)})")
convert_file(filepath)
else:
for filepath in sys.argv[1:]:
convert_file(filepath)


if __name__ == "__main__":
main()
Upvote 0
In case you get the following error running [B]_convert_all.bat[/B]

[QUOTE]Traceback (most recent call last):
File "D:\TEST\_strings_bin_converter.py", line 42, in <module>
for filepath in os.listdir(''):
WindowsError: [Error 3] The system cannot find the path specified: ''[/QUOTE]

Just open [B]_strings_bin_converter.py[/B] with text editor and change line [B]for filepath in os.listdir(''):[/B] with [B]for filepath in os.listdir(os.getcwd()):[/B]
Upvote 0
All I got was download.Php
Upvote 0
[CODE]>>> ================================ RESTART ================================
>>>


Traceback (most recent call last):
File "C:\Users\acer\Downloads\strings_bin_converter.py", line 39, in <module>
if sys.argv[1] == 'all':
IndexError: list index out of range[/CODE]

Any idea what might be wrong?
Upvote 0
How do you run this thing? I installed it after installing Python 2.5 and all it was was a small 3KB RAR file without a read me or a convert all_bat (I'm on Windows 8 by the way).
Upvote 0
This doesnt work for me, can some just upload all the converted strings file? for all campaigns (imperial and kingdoms) please
Upvote 0
Tankyou!
Upvote 0
Thanks :thumbsup2
Upvote 0
Thanks Worked fine, You need PYTHON 2.5 You cannot get Old or newer versions. ONLY 2.5
Upvote 0
^ Above solution didn't work, :(
Upvote 0
I finally debugged this for myself. I have python 3.x installed, and since python is a piece of :poop:, they aren't backwards compatible with older scripts. And it doesn't work in THREE different ways.

1) Some syntax is screwed up with on of the comments.
So you have to right-click the strings_bin_converter.py file and open as a txt file with notepad or something, comment out the "[B]print 'Converting file '+filepath+'('+str(i)+' of '+str(len(arr))+')'[/B]" line with a "[B]#[/B]" so that it looks like this:
[B]#print 'Converting file '+filepath+'('+str(i)+' of '+str(len(arr))+')'[/B]
They are just comments so I didn't feel like learning how to fix it, you'll know it worked when you see a bunch of new files pop up in the text directory. :thumbsup2 but if you want to figure it out, its probably something to do with the changes to the 'str' function between python 2 and python 3.


2) [B]os.listdir(''):[/B] Doesn't work the way it did in python 2.x apparently, but thats ok you can copy-paste in the complete directory of your data/text folder, mines in steam:
[B]os.listdir('C:/Program Files (x86)/Steam/steamapps/common/Medieval II Total War/data/text'):[/B]


Next you'll get an error about 'unicode' not being defined.
To fix this replace the instances where you find '[B]unicode[/B]' with '[B]str[/B]'.
[B]fw.write(str(struct.pack('HH',0xFFFE,0xAC00),'UTF-16')+'\r\n')
tagstr += str(f.read(2), 'UTF-16')
screenstr += str(tempstr, 'UTF-16')[/B]


My entire file that seems to work for me:

[SPOILER][CODE]# strings_bin_converter.py (c) 2006 Stefan Reutter (alias alpaca)
# Version 0.72
# A script to convert a number of MTW2 .strings.bin files to their .txt counterparts

import sys
import codecs
import struct
import os

def convertFile(filepath):
"""
Takes a type 2 .strings.bin file and converts it to a text file
"""
f = open(filepath+'.strings.bin', 'rb')
(style1,) = struct.unpack('h',f.read(2))
(style2,) = struct.unpack('h', f.read(2))
if style1 == 2 and style2 == 2048:
fw = codecs.open(filepath, encoding='UTF-16', mode='w')
fw.write(str(struct.pack('HH',0xFFFE,0xAC00),'UTF-16')+'\r\n')
(length,) = struct.unpack('i',f.read(4))
for string in range(length):
(strlen,) = struct.unpack('h', f.read(2))
tagstr = ''
for i in range(strlen):
tagstr += str(f.read(2), 'UTF-16')
(strlen,) = struct.unpack('h', f.read(2))
screenstr = ''
for i in range(strlen):
tempstr = f.read(2)
(e,) = struct.unpack('H',tempstr)
if e == 10:
screenstr += '\\n'
else:
screenstr += str(tempstr, 'UTF-16')
fw.write('{'+tagstr+'}'+screenstr+'\r\n')
fw.close()
f.close()

if sys.argv[1] == 'all':
arr = []
i = 1
#for filepath in os.listdir(''):
for filepath in os.listdir('C:/Program Files (x86)/Steam/steamapps/common/Medieval II Total War/data/text'):
if filepath.find('.strings.bin') > -1:
arr.append(filepath.split('.strings.bin')[0])
for filepath in arr:
#print 'Converting file '+filepath+'('+str(i)+' of '+str(len(arr))+')'
convertFile(filepath)
i+=1
else:
for i in range(len(sys.argv)-1):
path = sys.argv[i+1]
convertFile(path)[/CODE][/SPOILER]
Upvote 0
^I have the exact same problem, any help would be very handy
Upvote 0
I've downloaded python, installed, run the converter... and the .bin files are still .bin files. cmd prompt flashes for a second, vanishes, and nothing changes. Does the change not take place right away, or is there a glitch?
Upvote 0
You need to get Python first for it to work. It says so in the readme
Upvote 0
It does not work!
Upvote 0
This file converter does not work =(
Upvote 0
Unfortunately:
[IMG]http://www.bildites.lv/images/ibiowg57u87amc3hhw8t.jpg[/IMG]
Any advice how to deal with this?
Upvote 0
very nice!
thanks!
Upvote 0
In Clouds across europe there is no such thing as historic_events and vanila folder there in no TEXT folder!
Upvote 0

Site News

Site Polls

  • Axis & Allies

  • Battleship

  • Checkers

  • Chess

  • Clue

  • Go

  • Monopoly

  • Risk

  • Stratego

  • Other


Results are only viewable after voting.
Back
Top Bottom