-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsteamworks_builders.py
210 lines (175 loc) · 6.75 KB
/
steamworks_builders.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
import json
from re import sub
from io import StringIO
from typing import List, Dict, Tuple
whitelisted_enum_names = [
"EResult",
"ELobbyType",
"ESteamInputType",
"ESteamInputGlyphSize",
"EInputActionOrigin",
"EResult",
"ELobbyDistanceFilter",
"ELobbyComparison",
"EP2PSend",
"EP2PSessionError",
"EUGCMatchingUGCType",
"EUGCQuery",
"EUserUGCList",
"EUserUGCListSortOrder",
"EWorkshopFileType",
"ERemoteStoragePublishedFileVisibility",
"EItemPreviewType",
"EGamepadTextInputMode",
"EFloatingGamepadTextInputMode",
"EGamepadTextInputLineMode",
"EItemUpdateStatus",
"EItemState",
"EChatRoomEnterResponse",
"EChatEntryType",
"EChatMemberStateChange",
]
# Needed because godot can't convert unsigned long long
type_renames = {
"unsigned long long": "uint64_t",
"int32": "int32_t",
"uint64": "uint64_t",
"uint32": "uint32_t",
"AppId_t": "uint32_t",
}
whitelisted_types = [
"InputHandle_t",
"HSteamPipe",
"UGCHandle_t",
"UGCQueryHandle_t",
"UGCUpdateHandle_t",
"PublishedFileId_t",
"HAuthTicket",
]
bitfields = ["ItemState"]
whitelisted_constants = ["k_UGCQueryHandleInvalid", "k_UGCUpdateHandleInvalid"]
whitelisted_structs = ["SteamUGCDetails_t"]
def snake_case(s):
return "_".join(sub("([A-Z][a-z]+)", r" \1", sub("([A-Z]+)", r" \1", s.replace("-", " "))).split()).upper()
class EnumInfo:
enum_name: str
enum_values: List[Tuple[str, str]]
def __init__(self, enum_name: str):
self.enum_name = enum_name
self.enum_values = []
# Returns a dictionary of enum names with their renamed constant values inside
def process_enum_data(enums_data) -> List[EnumInfo]:
enum_infos: List[EnumInfo] = []
for enum in enums_data:
enum_name = enum["enumname"]
if not enum_name in whitelisted_enum_names:
continue
if enum_name.startswith("E"):
enum_name = enum_name[1:]
enum_info: EnumInfo = EnumInfo(enum_name)
for value in enum["values"]:
value_name = value["name"]
if value_name.lower().startswith("k_e"):
value_name = value_name[3:]
value_name = value_name.replace("_", "")
value_name = snake_case(value_name)
if value_name.startswith("P2_P_"):
# Hack to make P2P constants nicer
value_name = "P2P_" + value_name[5:]
enum_info.enum_values.append((value_name, value["value"]))
enum_infos.append(enum_info)
return enum_infos
def generate_enum_definitions(enums: List[EnumInfo]) -> StringIO:
f = StringIO()
for enum in enums:
f.write("\tenum " + enum.enum_name + " {\n")
for value_name, value in enum.enum_values:
f.write("\t\t" + value_name + " = " + value + ",\n")
f.write("\t};\n\n")
return f
def generate_constants(constants: List[Dict]) -> StringIO:
f = StringIO()
for constant in constants:
if constant["constname"] in whitelisted_constants:
const_name = constant["constname"]
const_type = constant["consttype"]
const_val = constant["constval"]
if const_name.startswith("k_"):
if const_name[2].islower():
const_name = const_name[3:]
else:
const_name = const_name[2:]
f.write(f"\tstatic const {const_type} {snake_case(const_name)} = {const_val};\n")
return f
def generate_structs(structs: List[Dict]) -> StringIO:
f = StringIO()
f.write("\t#if defined(LINUXBSD_ENABLED) || defined(MACOS_ENABLED)\n")
f.write("\t#pragma pack( push, 4 )\n")
f.write("\t#else\n")
f.write("\t#pragma pack( push, 8 )\n")
f.write("\t#endif\n\n")
for struct in structs:
if not struct["struct"] in whitelisted_structs:
continue
f.write(f'\tstruct {struct["struct"]} ' + "{\n")
for field in struct["fields"]:
field_type = field["fieldtype"]
spl = field_type.split(" ")
field_type_array = ""
if len(spl) > 1:
field_type_array = spl[1]
field_type = spl[0]
field_type = type_renames.get(field_type, field_type)
if field_type.startswith("E"):
field_type = field_type[1:]
name_start_index = 0
for idx, character in enumerate(field["fieldname"]):
if character.isupper():
name_start_index = idx
break
field_name = snake_case(field["fieldname"][name_start_index:]).lower()
f.write(f"\t\t{field_type} {field_name}{field_type_array};\n")
f.write("};\n\n")
f.write("\t#pragma pack( pop )\n\n")
return f
def generate_constants_file(target, source, env):
json_f = json.load(open(source[0].path, "r"))
enum_infos = process_enum_data(json_f["enums"])
with open(target[0].path, "w") as f:
f.write("#ifndef STEAMWORKS_CONSTANTS_GEN_H\n")
f.write("#define STEAMWORKS_CONSTANTS_GEN_H\n\n")
f.write('#include "core/variant/binder_common.h"\n')
f.write('#include "core/object/object.h"\n\n')
f.write('#include "core/object/class_db.h"\n\n')
f.write("class SteamworksConstants : public Object {\n")
f.write("GDCLASS(SteamworksConstants, Object);\n")
f.write("public:\n")
for typedef in json_f["typedefs"]:
if typedef["typedef"] in whitelisted_types:
typedef_name = typedef["typedef"]
typedef_type = type_renames.get(typedef["type"], typedef["type"])
f.write(f"\ttypedef {typedef_type} {typedef_name};\n")
f.write("\n")
f.write(generate_enum_definitions(enum_infos).getvalue())
f.write("\n")
f.write(generate_constants(json_f["consts"]).getvalue())
f.write("\n")
f.write(generate_structs(json_f["structs"]).getvalue())
f.write("protected:\n")
f.write("\tstatic void _bind_methods() {\n")
for enum in enum_infos:
for value_name, value in enum.enum_values:
if enum.enum_name in bitfields:
f.write(f"\t\tBIND_BITFIELD_FLAG({value_name});\n")
else:
f.write(f"\t\tBIND_ENUM_CONSTANT({value_name});\n")
f.write("\t}\n")
f.write("};\n\n")
for enum in enum_infos:
if enum.enum_name in bitfields:
f.write(f"VARIANT_BITFIELD_CAST(SteamworksConstants::{enum.enum_name});\n")
else:
f.write(f"VARIANT_ENUM_CAST(SteamworksConstants::{enum.enum_name});\n")
f.write("\n")
f.write("using SWC = SteamworksConstants;\n\n")
f.write("#endif // STEAMWORKS_CONSTANTS_GEN_H\n")