-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathconvertFormat.py
More file actions
executable file
·127 lines (104 loc) · 4.23 KB
/
Copy pathconvertFormat.py
File metadata and controls
executable file
·127 lines (104 loc) · 4.23 KB
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
#!/usr/bin/env python3
import argparse as AP
import json
from pathlib import Path
from typing import TypedDict
from collections import defaultdict
class OldBiomeEntry(TypedDict):
color: int
name: str | None
cave: bool | None
class OldStructureEntry(TypedDict):
icon: str | None
name: str | None
showByDefault: bool | None
class NewBiomeEntry(TypedDict):
r: int
g: int
b: int
name: str | None
class NewBiomeEntry(TypedDict):
icon: str
name: str | None
def write_json(data: dict, path: Path, root: Path, force: bool, prefix: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
if path.exists() and not force:
print(f'{prefix}\x1b[1;31mSkipping, because {path.relative_to(root)} already exists!\x1b[0m')
return
elif path.exists():
print(f'{prefix}\x1b[1;33mOverwriting existing {path.relative_to(root)}\x1b[0m')
else:
print(f'{prefix}\x1b[35mWriting {path.relative_to(root)}')
path.write_text(json.dumps(data, indent=2) + '\n', encoding='utf-8')
def main() -> int:
parser = AP.ArgumentParser(description='Convert from the old pack format to the new format')
parser.add_argument('dir', type=Path, help='The path to the root datapack dir')
parser.add_argument('-f', '--force', action='store_true', help='Overwrite existing files')
args = parser.parse_args()
root: Path = args.dir.resolve()
if root.name != 'data' or not root.exists() or not root.is_dir():
print(f'Invalid root data pack dir {root} (The path name must be an existing `data` directory).')
return 1
print('\x1b[1;32mProcessing biomes:\x1b[0m')
biome_colors_data: dict[str, NewBiomeEntry] = {}
cave_biome_list: list[str] = []
for structure_root in root.glob('**/biome_preview/'):
print(f' - \x1b[1mFound biome root: {structure_root.relative_to(root)}\x1b[0m')
for biome_raw in structure_root.glob('**/*.json'):
print(f' - Processing: {biome_raw.relative_to(root)}')
data: dict[str, OldBiomeEntry] = json.loads(biome_raw.read_text(encoding='utf-8'))
for k, v in data.items():
color = v['color']
biome_colors_data[k] = {
'r': (color >> 16) & 0xFF,
'g': (color >> 8) & 0xFF,
'b': (color >> 0) & 0xFF,
}
if 'name' in v:
biome_colors_data[k]['name'] = v['name']
if 'cave' in v and v['cave']:
cave_biome_list += [k]
# Write data
write_json(biome_colors_data, root / 'c' / 'worldgen' / 'biome_colors.json', root, args.force, ' - ')
# Write cave tags
write_json(
{
'replace': False,
'values': cave_biome_list
},
root / 'c' / 'tags' / 'worldgen' / 'biome' / 'is_cave.json',
root,
args.force,
' - ',
)
print('\x1b[1;32mProcessing structures:\x1b[0m')
structure_data: dict[str, NewBiomeEntry] = defaultdict(dict)
display_by_default_list: list[str] = []
for structure_root in root.glob('**/structure_preview/'):
print(f' - \x1b[1mFound structure root: {structure_root.relative_to(root)}\x1b[0m')
for structure_raw in structure_root.glob('**/*.json'):
print(f' - Processing: {structure_raw.relative_to(root)}')
data: dict[str, OldStructureEntry] = json.loads(structure_raw.read_text(encoding='utf-8'))
for k, v in data.items():
if 'icon' in v:
structure_data[k]['texture'] = v['icon']
if 'name' in v:
structure_data[k]['name'] = v['name']
if 'showByDefault' in v and v['showByDefault']:
display_by_default_list += [k]
# Write data
write_json(structure_data, root / 'c' / 'worldgen' / 'structure_icons.json', root, args.force, ' - ')
# Write cave tags
write_json(
{
'replace': False,
'values': display_by_default_list
},
root / 'c' / 'tags' / 'worldgen' / 'structure' / 'display_on_map_by_default.json',
root,
args.force,
' - ',
)
return 0
if __name__ == '__main__':
raise SystemExit(main())