-
Notifications
You must be signed in to change notification settings - Fork 1
/
webp_db_fixer.py
75 lines (62 loc) · 2.79 KB
/
webp_db_fixer.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
# This script is hardcoded for BG:DiA! You need to change it for other worlds
# You need to convert the pngs, jp(e)gs to webps (both need to be present) before running this
# It expects to sit one folder above the module, takes the world's root folder as argument
# Partly stolen from https://stackoverflow.com/a/38514560
import glob
import os
import pathlib
import posixpath
import sys
def fix_db(db, replacement):
input_filepath = pathlib.Path("bgdia/data/", db + '2')
output_filepath = pathlib.Path("bgdia/data/", db + '3')
with open(input_filepath, "r", encoding="utf8", newline='\n') as input_db:
with open(output_filepath, "w", encoding="utf8", newline='\n') as output_db:
data = input_db.read()
for original, webp in replacement.items():
data = data.replace(original, webp)
# catching Tokenwildcards
data = data.replace("*.png", "*.webp")
data = data.replace("*.jpg", "*.webp")
data = data.replace("*.jpeg", "*.webp")
output_db.write(data)
def find_pngs(path, replacement):
# find all .pngs (while traversing subdirectories)
for file in glob.glob(path + '/**/*.png', recursive=True):
# convert to linux path
file = file.replace(os.sep, posixpath.sep)
# slice the worlds root folder off
file = file.split('/', 1)[-1]
# create dictonary with the path to the png as key and the path to the webp as value
replacement[file] = file.replace('.png', '.webp')
def find_jpgs(path, replacement):
for file in glob.glob(path + '/**/*.jpg', recursive=True):
file = file.replace(os.sep, posixpath.sep)
file = file.split('/', 1)[-1]
replacement[file] = file.replace('.jpg', '.webp')
# jpegs aswell
for file in glob.glob(path + '/**/*.jpeg', recursive=True):
file = file.replace(os.sep, posixpath.sep)
file = file.split('/', 1)[-1]
replacement[file] = file.replace('.jpeg', '.webp')
def fix(path):
replacement = {}
find_pngs(path, replacement)
find_jpgs(path, replacement)
fix_db('actors.db', replacement)
fix_db('items.db', replacement)
fix_db('journal.db', replacement)
fix_db('scenes.db', replacement)
fix_db('tables.db', replacement)
print('Fixed DBs')
remove_duplicates(replacement)
def remove_duplicates(replacement):
for png, webp in replacement.items():
# add the worlds root folder back, so they're actually valid paths (!!!BGDIA HARDCODED)
duplicated_png = pathlib.Path("bgdia/", png)
duplicated_png.unlink()
if __name__ == "__main__":
if sys.argv[1]:
fix(sys.argv[1])
else:
print("Please pass the path to check as parameter to the script")