-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture-binds.py
164 lines (130 loc) · 4.84 KB
/
texture-binds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
import argparse
import json
import pathlib
import re
import zipfile
import io
from string import printable as printable_characters
from packaging.version import parse as ParseVersion
from packaging.version import Version
from PIL import Image
from PIL import ImageChops
from PIL import ImageEnhance
class HotBar:
def __init__(self):
self.slots = {i: None for i in range(1, 10)}
def bind(self, slot, key):
if not isinstance(slot, int):
raise TypeError
if not slot in range(1, 10):
raise ValueError
if not isinstance(key, str):
raise TypeError
if not len(key) in range(1, 3):
raise ValueError
for char in key:
if not char in printable_characters:
raise ValueError
self.slots[slot] = key
def slot(self, n):
return self.slots[n]
class Asset:
def __init__(self, obj, data):
if not isinstance(obj['path'], str):
raise TypeError
if not isinstance(obj['spacing'], int):
raise TypeError
if not isinstance(obj['x'], int):
raise TypeError
if not isinstance(obj['y'], int):
raise TypeError
if not isinstance(data, io.BytesIO):
raise TypeError
self.path = obj['path']
self.spacing = obj['spacing']
self.x = obj['x']
self.y = obj['y']
self.data = data
parser = argparse.ArgumentParser(prog='texture-binds')
parser.add_argument('path', metavar='.minecraft folder', type=pathlib.Path)
parser.add_argument('--offset', nargs=2, type=int,
metavar=('x', 'y'), default=[1, 1])
parser.add_argument('--opacity', type=float, metavar='float', default=0.5)
args = parser.parse_args()
PACKMCMETA = json.dumps({
"pack": {
"pack_format": 12,
"description": "Your keybinds embedded on inventory GUI and HUD."
}
}, sort_keys=True, indent=4)
with pathlib.Path(__file__).with_name('assets.json').open('r') as f:
ASSETS_JSON = json.load(f)
if not args.path.is_dir():
raise NotADirectoryError(args.path)
OPTIONS_PATH = args.path.joinpath('options.txt')
if not OPTIONS_PATH.is_file():
raise FileNotFoundError(OPTIONS_PATH)
RESOURCEPACK_PATH = args.path.joinpath('resourcepacks/texture-binds.zip')
if not RESOURCEPACK_PATH.parent.is_dir():
raise NotADirectoryError(RESOURCEPACK_PATH)
# find all the jar files in versions, compare them, and use
# the latest version. this will likely break with snapshot jars.
# TODO: add exceptions for snapshot versions.
JAR_PATH = None
for jar in args.path.glob('versions/**/*.jar'):
if (JAR_PATH is None
or ParseVersion(jar.stem) > ParseVersion(JAR_PATH.stem)):
JAR_PATH = jar
if JAR_PATH is None:
raise FileNotFoundError
ASSETS = []
with zipfile.ZipFile(JAR_PATH) as jar:
for asset in ASSETS_JSON:
ASSETS.append(
Asset(asset, io.BytesIO(jar.open(asset['path'], 'r').read())))
ASCII_PNG = io.BytesIO(
jar.open('assets/minecraft/textures/font/ascii.png', 'r').read())
hotbar = HotBar()
with OPTIONS_PATH.open('r') as f:
pattern = re.compile(
r'key_key\.hotbar\.([1-9]):key\.(keyboard|mouse)\.([\w])')
for match in re.finditer(pattern, f.read()):
slot = int(match.group(1))
device = match.group(2)
key = match.group(3)
if device == 'mouse':
key = 'm' + key
hotbar.bind(slot, key)
# ascii sheet is 128x128, each tile is 8x8, character size is 5x8.
# sheet / tile size = 16, therefore 16 columns, 16 rows.
# most characters only take up 5x7, however the characters p and q
# render slightly lower than the others, so 5x8 is used.
ASCII_SPRITES = {}
sheet = Image.open(ASCII_PNG)
for char in printable_characters:
# TODO: add mouse buttons to list
n = ord(char)
x1 = (n % 16) * 8
y1 = (n // 16) * 8
x2 = x1 + 5
y2 = y1 + 8
ASCII_SPRITES[char] = sheet.copy().crop((x1, y1, x2, y2))
RESOURCEPACK_ZIP = io.BytesIO()
with zipfile.ZipFile(RESOURCEPACK_ZIP, 'w') as zip:
with zip.open('pack.mcmeta', 'w') as f:
f.write(PACKMCMETA.encode())
for asset in ASSETS:
modified_asset = io.BytesIO()
texture = Image.open(asset.data).convert('RGBA')
key_overlay = Image.new('RGBA', texture.size)
for slot, key in hotbar.slots.items():
position = (asset.x + args.offset[0] + (slot - 1) * asset.spacing,
asset.y + args.offset[1])
key_overlay.paste(ASCII_SPRITES[key], position, ASCII_SPRITES[key])
ImageChops.screen(texture, ImageEnhance.Brightness(
key_overlay).enhance(args.opacity)).save(modified_asset, 'png')
with zip.open(asset.path, 'w') as f:
f.write(modified_asset.getvalue())
with RESOURCEPACK_PATH.open('wb') as f:
f.write(RESOURCEPACK_ZIP.getvalue())