-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.gd
More file actions
62 lines (52 loc) · 1.69 KB
/
converter.gd
File metadata and controls
62 lines (52 loc) · 1.69 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
extends Node2D
var dictionary = {
"1": {"id": 1, "name": "item 1", "modifiers": { muffin = 123}},
"2": {"id": 2, "name": "item 2", "modifiers": { muffin = 123}},
"3": {"id": 3, "name": "item 3", "modifiers": { muffin = 123}},
}
func _ready():
convert_dict_to_crystal_class(dictionary, "NodeTypes")
func convert_dict_to_crystal_class(data, class__name):
var property_list = {}
var property_list_s = PoolStringArray()
var parameter_list_s = PoolStringArray()
var hash_list_s = PoolStringArray()
for key in data:
var obj = data[key]
var parameter_values_s = PoolStringArray()
for key2 in obj:
var value = obj[key2]
if value is Dictionary:
# We must do recursion here, but i don't know how :/
property_list[key2] = "%s : Hash(String, Int32)" % key2
# Conform to Crystal's Hash
parameter_values_s.append("%s" % to_json(value).replace(":", "=>"))
else:
#Extract keys
property_list[key2] = "%s = \"\" " % key2
parameter_values_s.append("\"%s\"" % value)
hash_list_s.append( "NodeTypes[\"%s\"] = NodeTypesClass.new(%s)" % [key, parameter_values_s.join(",")] )
for key in property_list:
property_list_s.append("property %s" % property_list[key])
parameter_list_s.append("@%s" % key)
var text = """
class %sClass
%s
def initialize(%s) end
end
%s
%s
""" % [
class__name,
property_list_s.join("\n "),
parameter_list_s.join(","),
"%s = Hash(String, %sClass).new" % [class__name, class__name],
hash_list_s.join("\n")
]
save_file("%s.cr" % class__name, text)
print("Conversion Complete!\n-----------\n%s\n-----------" % text)
func save_file(the_path, content):
var filet = File.new()
filet.open(the_path, File.WRITE)
filet.store_string(content)
filet.close()