miggor-StreamGraph/addons/no-obs-ws/Utility/EnumGen.gd
Lera Elvoé b55a462945 Add OBS and Twitch nodes. Improve UX significantly. Rework groups from the ground up with a new instancing feature. Open to the public. (#18)
After months of work and over a hundred commits on this repo alone (not to mention the old, half-working repos on GitHub), StreamGraph is finally ready to be shown to the public, even if in an incomplete state.

This PR is a culmination of numerous design discussions, re-writes, and hours spent by both @Eroax and myself.

Reviewed-on: https://codeberg.org/Eroax/StreamGraph/pulls/18
Co-authored-by: Lera Elvoé <yagich@poto.cafe>
Co-committed-by: Lera Elvoé <yagich@poto.cafe>
2023-12-15 21:44:25 +00:00

64 lines
1.9 KiB
GDScript

static func generate_enums(protocol_json_path: String, output_to_path: String) -> void:
var protocol := FileAccess.open(protocol_json_path, FileAccess.READ).get_as_text()
var protocol_json: Dictionary = JSON.parse_string(protocol)
var res := "# This file is automatically generated, please do not change it. If you wish to edit it, check /addons/deckobsws/Utility/EnumGen.gd\n\n"
for e in protocol_json.enums:
# if all are deprecated, don't make the enum
var deprecated_count: int
for enumlet in e.enumIdentifiers:
if enumlet.deprecated:
deprecated_count += 1
if deprecated_count == e.enumIdentifiers.size():
continue
res += "enum %s {\n" % e.enumType
for enumlet in e.enumIdentifiers:
var enumlet_value: int
match typeof(enumlet.enumValue):
TYPE_FLOAT:
enumlet_value = int(enumlet.enumValue)
TYPE_STRING when "<<" in enumlet.enumValue:
enumlet_value = tokenize_lbitshift(enumlet.enumValue)
TYPE_STRING when "|" in enumlet.enumValue:
var token: String = (enumlet.enumValue as String)\
.trim_prefix("(")\
.trim_suffix(")")
var split := Array(token.split("|")).map(
func(x: String):
return x.strip_edges()
)
var calc: int
for enum_partial in e.enumIdentifiers:
if enum_partial.enumIdentifier not in split:
continue
calc |= tokenize_lbitshift(enum_partial.enumValue)
enumlet_value = calc
TYPE_STRING:
enumlet_value = int(enumlet.enumValue)
res += "\t%s = %s,\n" % [
(enumlet.enumIdentifier as String).to_snake_case().to_upper(),
enumlet_value
]
res += "}\n\n\n"
var result_file := FileAccess.open(output_to_path, FileAccess.WRITE)
result_file.store_string(res.strip_edges() + "\n")
static func tokenize_lbitshift(s: String) -> int:
var tokens := Array(s\
.trim_prefix("(")\
.trim_suffix(")")\
.split("<<")).map(
func(x: String):
return int(x)
)
return tokens[0] << tokens [1]