-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.py
167 lines (131 loc) · 4.21 KB
/
bootstrap.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
import json
import os
import urllib.request
import subprocess
from os import path
# 1.18.2 server direct download link
SERVER_URL = 'https://launcher.mojang.com/v1/objects/c8f83c5655308435b3dcf03c06d9fe8740a77469/server.jar'
# setup constants
BOOTSTRAP_DIR = path.abspath('./bootstrap')
SERVER_PATH = path.join(BOOTSTRAP_DIR, 'server.jar')
REPORTS_DIR = path.join(BOOTSTRAP_DIR, 'reports')
GEN_HEADER_PATH = path.join('mc-proto', 'minecraft', 'generated.h')
GEN_SOURCE_PATH = path.join('mc-proto', 'minecraft', 'generated.cpp')
FILE_HEADER_COMMENT = \
"""
// generated by bootstrap.py
// do not modify
"""
# setting up all data for bootstrap
if not path.exists(BOOTSTRAP_DIR):
os.mkdir(BOOTSTRAP_DIR)
if not path.exists(SERVER_PATH):
print('Downloading server jar...')
urllib.request.urlretrieve(SERVER_URL, SERVER_PATH)
# generating raw data
if not path.exists(REPORTS_DIR):
os.mkdir(REPORTS_DIR)
if len(os.listdir(REPORTS_DIR)) == 0:
print('Generating raw data...')
subprocess.run(
f'java "-DbundlerMainClass=net.minecraft.data.Main" -jar {SERVER_PATH} -reports -all --output="{BOOTSTRAP_DIR}"',
cwd=BOOTSTRAP_DIR
)
# generating type code from reports
if path.exists(GEN_HEADER_PATH):
os.remove(GEN_HEADER_PATH)
if path.exists(GEN_SOURCE_PATH):
os.remove(GEN_SOURCE_PATH)
header_content = ''
source_content = ''
# parsing reports
registriesjson = json.load(open(path.join(REPORTS_DIR, 'registries.json')))
blockregistry = {key: v['protocol_id'] for key, v in registriesjson['minecraft:block']['entries'].items()}
blockjson = json.load(open(path.join(REPORTS_DIR, 'blocks.json')))
# generating data
block_type_enum_name = 'BlockTypes'
block_states_enum_name = 'BlockStates'
block_keys_strs = []
block_types_enum_data = []
block_states_enum_data = []
block_state_to_type_map_content = []
block_type_to_string_map_content = []
block_entries = 0
for key, block in blockjson.items():
enum_name = str(key[len('minecraft:'):]).upper()
block_keys_strs.append(f'char* __BLOCK_KEY_{enum_name} = (char*)"{key}";')
block_type_to_string_map_content.append(
f'{{ {block_type_enum_name}::{enum_name}, __BLOCK_KEY_{enum_name} }}'
)
block_types_enum_data.append(f'{enum_name} = {blockregistry[key]}')
for i in range(len(block['states'])):
block_entries += 1
gen_enum_name = enum_name
if i > 0:
gen_enum_name += f'_{i}'
state = block['states'][i]
block_states_enum_data.append(f'{gen_enum_name} = {state["id"]}')
block_state_to_type_map_content.append(
f'{{ ({block_states_enum_name}){state["id"]}, ({block_type_enum_name}){blockregistry[key]} }}'
)
nl = '\n'
nlcomma = ',\n'
header_content += \
f"""
#define NUM_GLOBAL_BLOCK_ENTRIES {len(blockregistry)}
#define NUM_GLOBAL_BLOCK_STATE_ENTRIES {block_entries}
enum class {block_type_enum_name}
{{
{nlcomma.join(block_types_enum_data)}
}};
enum class {block_states_enum_name} {{
{nlcomma.join(block_states_enum_data)}
}};
extern {block_type_enum_name} block_type_from_state({block_states_enum_name} state);
extern std::string block_type_to_string({block_type_enum_name} type);
"""
source_content += \
f"""
{nl.join(block_keys_strs)}
std::map<{block_states_enum_name}, {block_type_enum_name}> BLOCK_STATE_TO_TYPE_MAP = {{ {nlcomma.join(block_state_to_type_map_content)} }};
std::map<{block_type_enum_name}, char*> BLOCK_TYPE_TO_STRING_MAP = {{ {nlcomma.join(block_type_to_string_map_content)} }};
{block_type_enum_name} block_type_from_state({block_states_enum_name} state)
{{
return BLOCK_STATE_TO_TYPE_MAP[state];
}}
std::string block_type_to_string({block_type_enum_name} type)
{{
return std::string(BLOCK_TYPE_TO_STRING_MAP[type]);
}}
"""
# write generated data
hfile = open(GEN_HEADER_PATH, 'w')
sfile = open(GEN_SOURCE_PATH, 'w')
hfile.write(f'''
#pragma once
{FILE_HEADER_COMMENT}
#include <string>
namespace minecraft
{{
{header_content}
}}
''')
sfile.write(f'''
{FILE_HEADER_COMMENT}
#include "generated.h"
#include <map>
namespace
{{
struct __cmp_chr_ptr
{{
bool operator()(const char* a, const char* b) const
{{
return strcmp(a, b) < 0;
}}
}};
}}
namespace minecraft
{{
{source_content}
}}
''')