Debian Codegraph @

godot/addons/gut/method_maker.gd

raw
1 # This class will generate method declaration lines based on method meta
2 # data. It will create defaults that match the method data.
3 #
4 # --------------------
5 # function meta data
6 # --------------------
7 # name:
8 # flags:
9 # args: [{
10 # (class_name:),
11 # (hint:0),
12 # (hint_string:),
13 # (name:),
14 # (type:4),
15 # (usage:7)
16 # }]
17 # default_args []
18
19 var _utils = load('res://addons/gut/utils.gd').get_instance()
20 var _lgr = _utils.get_logger()
21 const PARAM_PREFIX = 'p_'
22
23 # ------------------------------------------------------
24 # _supported_defaults
25 #
26 # This array contains all the data types that are supported for default values.
27 # If a value is supported it will contain either an empty string or a prefix
28 # that should be used when setting the parameter default value.
29 # For example int, real, bool do not need anything func(p1=1, p2=2.2, p3=false)
30 # but things like Vectors and Colors do since only the parameters to create a
31 # new Vector or Color are included in the metadata.
32 # ------------------------------------------------------
33 # TYPE_NIL = 0 — Variable is of type nil (only applied for null).
34 # TYPE_BOOL = 1 — Variable is of type bool.
35 # TYPE_INT = 2 — Variable is of type int.
36 # TYPE_REAL = 3 — Variable is of type float/real.
37 # TYPE_STRING = 4 — Variable is of type String.
38 # TYPE_VECTOR2 = 5 — Variable is of type Vector2.
39 # TYPE_RECT2 = 6 — Variable is of type Rect2.
40 # TYPE_VECTOR3 = 7 — Variable is of type Vector3.
41 # TYPE_COLOR = 14 — Variable is of type Color.
42 # TYPE_OBJECT = 17 — Variable is of type Object.
43 # TYPE_DICTIONARY = 18 — Variable is of type Dictionary.
44 # TYPE_ARRAY = 19 — Variable is of type Array.
45 # TYPE_VECTOR2_ARRAY = 24 — Variable is of type PoolVector2Array.
46
47
48
49 # TYPE_TRANSFORM2D = 8 — Variable is of type Transform2D.
50 # TYPE_PLANE = 9 — Variable is of type Plane.
51 # TYPE_QUAT = 10 — Variable is of type Quat.
52 # TYPE_AABB = 11 — Variable is of type AABB.
53 # TYPE_BASIS = 12 — Variable is of type Basis.
54 # TYPE_TRANSFORM = 13 — Variable is of type Transform.
55 # TYPE_NODE_PATH = 15 — Variable is of type NodePath.
56 # TYPE_RID = 16 — Variable is of type RID.
57 # TYPE_RAW_ARRAY = 20 — Variable is of type PoolByteArray.
58 # TYPE_INT_ARRAY = 21 — Variable is of type PoolIntArray.
59 # TYPE_REAL_ARRAY = 22 — Variable is of type PoolRealArray.
60 # TYPE_STRING_ARRAY = 23 — Variable is of type PoolStringArray.
61 # TYPE_VECTOR3_ARRAY = 25 — Variable is of type PoolVector3Array.
62 # TYPE_COLOR_ARRAY = 26 — Variable is of type PoolColorArray.
63 # TYPE_MAX = 27 — Marker for end of type constants.
64 # ------------------------------------------------------
65 var _supported_defaults = []
66
67 func _init():
68 for _i in range(TYPE_MAX):
69 _supported_defaults.append(null)
70
71 # These types do not require a prefix for defaults
72 _supported_defaults[TYPE_NIL] = ''
73 _supported_defaults[TYPE_BOOL] = ''
74 _supported_defaults[TYPE_INT] = ''
75 _supported_defaults[TYPE_REAL] = ''
76 _supported_defaults[TYPE_OBJECT] = ''
77 _supported_defaults[TYPE_ARRAY] = ''
78 _supported_defaults[TYPE_STRING] = ''
79 _supported_defaults[TYPE_DICTIONARY] = ''
80 _supported_defaults[TYPE_VECTOR2_ARRAY] = ''
81
82 # These require a prefix for whatever default is provided
83 _supported_defaults[TYPE_VECTOR2] = 'Vector2'
84 _supported_defaults[TYPE_RECT2] = 'Rect2'
85 _supported_defaults[TYPE_VECTOR3] = 'Vector3'
86 _supported_defaults[TYPE_COLOR] = 'Color'
87
88 # ###############
89 # Private
90 # ###############
91 var _func_text = _utils.get_file_as_text('res://addons/gut/double_templates/function_template.txt')
92
93 func _is_supported_default(type_flag):
94 return type_flag >= 0 and type_flag < _supported_defaults.size() and [type_flag] != null
95
96 # Creates a list of parameters with defaults of null unless a default value is
97 # found in the metadata. If a default is found in the meta then it is used if
98 # it is one we know how support.
99 #
100 # If a default is found that we don't know how to handle then this method will
101 # return null.
102 func _get_arg_text(method_meta):
103 var text = ''
104 var args = method_meta.args
105 var defaults = []
106 var has_unsupported_defaults = false
107
108 # fill up the defaults with null defaults for everything that doesn't have
109 # a default in the meta data. default_args is an array of default values
110 # for the last n parameters where n is the size of default_args so we only
111 # add nulls for everything up to the first parameter with a default.
112 for _i in range(args.size() - method_meta.default_args.size()):
113 defaults.append('null')
114
115 # Add meta-data defaults.
116 for i in range(method_meta.default_args.size()):
117 var t = args[defaults.size()]['type']
118 var value = ''
119 if(_is_supported_default(t)):
120 # strings are special, they need quotes around the value
121 if(t == TYPE_STRING):
122 value = str("'", str(method_meta.default_args[i]), "'")
123 # Colors need the parens but things like Vector2 and Rect2 don't
124 elif(t == TYPE_COLOR):
125 value = str(_supported_defaults[t], '(', str(method_meta.default_args[i]), ')')
126 elif(t == TYPE_OBJECT):
127 if(str(method_meta.default_args[i]) == "[Object:null]"):
128 value = str(_supported_defaults[t], 'null')
129 else:
130 value = str(_supported_defaults[t], str(method_meta.default_args[i]).to_lower())
131
132 # Everything else puts the prefix (if one is there) form _supported_defaults
133 # in front. The to_lower is used b/c for some reason the defaults for
134 # null, true, false are all "Null", "True", "False".
135 else:
136 value = str(_supported_defaults[t], str(method_meta.default_args[i]).to_lower())
137 else:
138 _lgr.warn(str(
139 'Unsupported default param type: ',method_meta.name, '-', args[defaults.size()].name, ' ', t, ' = ', method_meta.default_args[i]))
140 value = str('unsupported=',t)
141 has_unsupported_defaults = true
142
143 defaults.append(value)
144
145 # construct the string of parameters
146 for i in range(args.size()):
147 text += str(PARAM_PREFIX, args[i].name, '=', defaults[i])
148 if(i != args.size() -1):
149 text += ', '
150
151 # if we don't know how to make a default then we have to return null b/c
152 # it will cause a runtime error and it's one thing we could return to let
153 # callers know it didn't work.
154 if(has_unsupported_defaults):
155 text = null
156
157 return text
158
159 # ###############
160 # Public
161 # ###############
162
163 # Creates a delceration for a function based off of function metadata. All
164 # types whose defaults are supported will have their values. If a datatype
165 # is not supported and the parameter has a default, a warning message will be
166 # printed and the declaration will return null.
167 func get_function_text(meta):
168 var method_params = _get_arg_text(meta)
169 var text = null
170
171 var param_array = get_spy_call_parameters_text(meta)
172 if(param_array == 'null'):
173 param_array = '[]'
174
175 if(method_params != null):
176 var decleration = str('func ', meta.name, '(', method_params, '):')
177 text = _func_text.format({
178 "func_decleration":decleration,
179 "method_name":meta.name,
180 "param_array":param_array,
181 "super_call":get_super_call_text(meta)
182 })
183 return text
184
185 # creates a call to the function in meta in the super's class.
186 func get_super_call_text(meta):
187 var params = ''
188
189 for i in range(meta.args.size()):
190 params += PARAM_PREFIX + meta.args[i].name
191 if(meta.args.size() > 1 and i != meta.args.size() -1):
192 params += ', '
193 if(meta.name == '_init'):
194 return 'null'
195 else:
196 return str('.', meta.name, '(', params, ')')
197
198 func get_spy_call_parameters_text(meta):
199 var called_with = 'null'
200 if(meta.args.size() > 0):
201 called_with = '['
202 for i in range(meta.args.size()):
203 called_with += str(PARAM_PREFIX, meta.args[i].name)
204 if(i < meta.args.size() - 1):
205 called_with += ', '
206 called_with += ']'
207 return called_with
208
209 func get_logger():
210 return _lgr
211
212 func set_logger(logger):
213 _lgr = logger