This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathharvester.py
314 lines (246 loc) · 10.2 KB
/
harvester.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import os
import shutil
import json
import pprint
import tkinter as tk
from tkinter import filedialog
from unittest import skip
from jsonc_decoder import JSONCDecoder
import math
def strip_entities(data, filename, relative_path):
# Setup components list
components = []
# Gather raw components and groups
components_raw = data.get("minecraft:entity", {}).get("components", {})
component_groups = data.get(
"minecraft:entity", {}).get("component_groups", {})
# Bring components into a list
for component in components_raw.items():
if component not in components:
components.append(component)
# Bring component groups into a list
for group_name in component_groups:
group = component_groups.get(group_name)
for component in group.items():
if component not in components:
components.append(component)
marked_components = []
for component in components:
marked_components.append(
{
"entity": filename,
"path": relative_path,
"id": component[0],
"component": component[1]
}
)
return marked_components
# Handle a single json file, return tuples of k/v pairs for stripped components
def strip_items(data, filename, relative_path):
# Setup components list
components = []
# Gather raw components and groups
components_raw = data.get("minecraft:item", {}).get("components", {})
# Bring components into a list
for component in components_raw.items():
if component not in components:
components.append(component)
marked_components = []
for component in components:
marked_components.append(
{
"entity": filename,
"path": relative_path,
"id": component[0],
"component": component[1]
}
)
return marked_components
def strip_biomes(data, filename, relative_path):
# Setup components list
components = []
# Gather raw components and groups
components_raw = data.get("minecraft:biome", {}).get("components", {})
# Bring components into a list
for component in components_raw.items():
if component not in components:
components.append(component)
marked_components = []
for component in components:
marked_components.append(
{
"entity": filename,
"path": relative_path,
"id": component[0],
"component": component[1]
}
)
return marked_components
# Handle a single json file, return tuples of k/v pairs for stripped components
def strip_features(data, filename, relative_path):
# Setup components list
components = []
# Gather raw components and groups
components_raw = data.get("minecraft:entity", {}).get("components", {})
component_groups = data.get(
"minecraft:entity", {}).get("component_groups", {})
# Bring components into a list
for component in components_raw.items():
if component not in components:
components.append(component)
# Bring component groups into a list
for group_name in component_groups:
group = component_groups.get(group_name)
for component in group.items():
if component not in components:
components.append(component)
marked_components = []
for component in components:
marked_components.append(
{
"entity": filename,
"path": relative_path,
"id": component[0],
"component": component[1]
}
)
return marked_components
# Handle a single json file, return tuples of k/v pairs for stripped components
def strip_feature_rules(data, filename, relative_path):
# Setup components list
components = []
# Gather raw components and groups
components_raw = data.get(
"minecraft:feature_rules", {}).get("conditions", {})
# Bring components into a list
for component in components_raw.items():
if component not in components:
components.append(component)
marked_components = []
for component in components:
marked_components.append(
{
"entity": filename,
"path": relative_path,
"id": component[0],
"component": component[1]
}
)
return marked_components
# Handle a single json file, return tuples of k/v pairs for stripped components
def strip_spawn_rules(data, filename, relative_path):
# Setup components list
components = []
# Bring components into a list
for components_raw in data.get("minecraft:spawn_rules", {}).get("conditions", {}):
for component in components_raw.items():
if component not in components:
components.append(component)
marked_components = []
for component in components:
marked_components.append(
{
"entity": filename,
"path": relative_path,
"id": component[0],
"component": component[1]
}
)
return marked_components
def print_to_file(bp_path, folder, stable, outfile, title, strip_func):
inpath = os.path.join(bp_path, folder)
components = []
# Defines maximum amount of files where examples are tooken from:
counter_lim = 8
# Maybe add it to function arguments?
# Loop over filename
if os.path.exists(inpath):
filenames = os.listdir(inpath)
for filename in filenames:
# We only care about json files.
if(filename.endswith(".json")):
with open(os.path.join(inpath, filename)) as json_file:
data = json.load(json_file, cls=JSONCDecoder)
relative_path = os.path.join(folder, filename)
components.extend(strip_func(data, filename, relative_path))
else:
print(f'Warning! Input path {inpath} does not exist.')
components = sorted(components, key=lambda i: i['id'])
outfile.write("---\n")
outfile.write("title: {}\n".format(title))
outfile.write("category: Documentation\n")
outfile.write("---\n\n")
# Prep outfile:
outfile.write(
f"This documentation is stripped from the vanilla files using an [automated script](https://github.com/Bedrock-OSS/bedrock-harvester). If there is an issue, you can tell us about it in [Bedrock OSS](https://discord.gg/XjV87YN) Discord server. Please not that examples are shown from not more than {counter_lim} files to keep the page fast to load.\n\n"
)
# Handle components:
counter = 0
current = ""
current_entity = ""
for component in components:
docs_url = get_docs_link(
component["path"], True, stable).replace("\\", "/")
# Reset current, so we can create headers:
if(component["id"] != current):
counter = 0
if current != "":
outfile.write("</Spoiler>" + "\n\n")
current = component["id"]
outfile.write("## " + current.split("minecraft:")
[1] + "\n\n")
# Add links to bedrock.dev for entity components
if "vanilla-usage-components" in str(outfile):
dot_dev_link = "https://bedrock.dev/docs/stable/Entities#" + \
current.replace(":", "%3A")
outfile.write(
"<small>[View docs](" + dot_dev_link + ")</small>\n\n")
outfile.write('<Spoiler title="Show">\n\n')
# Reset component for smaller headers.
counter += 1
if(component["entity"] != current_entity and counter <= counter_lim):
current_entity = component["entity"]
outfile.write("#### " + component["entity"].replace(
".json", "") + "\n\n" + "<small>[View file](" + docs_url + ")</small>\n\n")
if counter <= counter_lim:
outfile.write("<CodeHeader></CodeHeader>" + "\n\n" + "```json\n\"" +
component["id"] + "\": " + json.dumps(component["component"], indent=4) + "\n```\n\n")
outfile.write("</Spoiler>")
def get_docs_link(f: str, bp: bool, stable: bool) -> str:
url = "https://github.com/bedrock-dot-dev/packs/tree/master/"
url = url + ("stable" if stable else "beta") + "/" + \
("behavior" if bp else "resource") + "/"
return url + f
def harvest(bp_path: str, is_stable: bool):
output_path = os.path.join(os.getcwd(), "output")
print("Entities!")
path = os.path.join(output_path, "vanilla-usage-components.md")
with open(path, 'w') as output_file:
print_to_file(bp_path, 'entities', is_stable,
output_file, "Vanilla Usage Components", strip_entities)
print("Items!")
with open(os.path.join(output_path, "vanilla-usage-items.md"), 'w') as output_file:
print_to_file(bp_path, 'items', is_stable,
output_file, "Vanilla Usage Items", strip_items)
print("Biomes!")
with open(os.path.join(output_path, "biomes.md"), 'w') as output_file:
print_to_file(bp_path, 'biomes', is_stable,
output_file, "Vanilla Usage Biomes", strip_biomes)
print("Features!")
with open(os.path.join(output_path, "features.md"), 'w') as output_file:
print_to_file(bp_path, 'features', is_stable,
output_file, "Vanilla Usage Features", strip_features)
print("Feature Rules!")
with open(os.path.join(output_path, "feature_rules.md"), 'w') as output_file:
print_to_file(bp_path, 'feature_rules', is_stable,
output_file, "Vanilla Usage Feature Rules", strip_feature_rules)
print("Spawn Rules!")
with open(os.path.join(output_path, "vanilla-usage-spawn-rules.md"), 'w') as output_file:
print_to_file(bp_path, 'spawn_rules', is_stable,
output_file, "Vanilla Usage Spawn Rules", strip_spawn_rules)
if __name__ == "__main__":
root = tk.Tk()
root.withdraw()
print("Please select behavior pack path.")
bp_path = filedialog.askdirectory()
harvest(bp_path)