-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUtility.gd
273 lines (231 loc) · 6.41 KB
/
Utility.gd
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
class_name Utility
extends Object
##
## This file contains the translator constants
## Developed by Johannes Witt and Hugo Locurcio
## Placed into the Public Domain
##
## Contains virtual functions that are automatically parsed as such
const VIRTUAL_FUNCTIONS = [
# Object
"_init",
"_get",
"_set",
"_get_property_list",
"_notification",
"_to_string",
# Node
"_ready",
"_process",
"_physics_process",
"_input",
"_unhandled_input",
"_unhandled_key_input",
"_enter_tree",
"_exit_tree",
"_get_configuration_warning",
# Control
"_clips_input",
"_get_minimum_size",
"_gui_input",
"_make_custom_tooltip",
]
## Contains all GDScript math operators
const MATH_OPERATORS = [
"+",
"-",
"*",
"/",
"%",
]
## Contains all GDScript assignment operators
const ASSIGNMENT_OPERATORS = [
"=",
"+=",
"-=",
"*=",
"/=",
"%=",
"&=",
"|=",
]
## Contains all GDScript comparison operators
const COMPARISON_OPERATORS = [
"<",
">",
"==",
"!=",
">=",
"<=",
]
## Contains all bitwise operators
const BITWISE_OPERATORS = [
"~",
"<<",
">>",
"&",
"^",
"|",
]
## Contains all method remaps (i.e. for methods using a namespace)
const REMAP_METHODS = {
"assert": "Debug.Assert",
"print": "GD.Print",
"prints": "GD.PrintS",
"printt": "GD.PrintT",
"load": "GD.Load",
"preload": "GD.Load",
"abs": "Mathf.Abs",
"acos": "Mathf.Acos",
"asin": "Mathf.Asin",
"atan": "Mathf.Atan",
"atan2": "Mathf.Atan2",
"min": "Mathf.Min",
"ord": "char.GetNumericValue",
"push_error": "GD.PushError",
"push_warning": "GD.PushWarning",
}
## Contains all keyword remaps that are parsed as variables
const REMAP_VARIABLES = {
"self": "this",
"event": "@event",
}
## Contains the needed usings, when using the specified method
const METHOD_USINGS = {
"assert": "System.Diagnostics",
}
## Contains the builtin classes
## value is how it should be handled
## null -> normal
## String -> value is used 1:1 for empty constructor
## [String, String] -> 0: type, 1: not empty
##
const BUILTIN_CLASSES = {
"String": null,
"Vector2": null,
"Rect2": null,
"Vector3": null,
"Transform2D": "Transform2D.Identity",
"Plane": null,
"Quat": "Quat.Identity",
"AABB": null,
"Basis": "Basis.Identity",
"Transform": null,
"Color": null,
"NodePath": null,
"RID": null,
"Array": ["Godot.Collections.Array", "Godot.Collections.Array{%s}"],
"Dictionary": ["Godot.Collections.Dictionary", "Godot.Collections.Dictionary{%s}"],
# Maybe convert those to typesafe Godot Arrays?
"PackedByteArray": ["byte[]", "byte[] {%s}"],
"PackedInt32Array": ["int[]", "int[] {%s}"],
"PackedInt64Array": ["Int64[]", "Int64[] {%s}"],
"PackedFloat32Array": ["float[]", "float[] {%s}"],
"PackedFloat64Array": ["double[], double[] {%s}"],
"PackedVector2Array": ["Vector2[]", "Vector2[] {%s}"],
"PackedVector3Array": ["Vector3[]", "Vector3[] {%s}"],
"PackedColorArray": ["Color[]", "Color[] {%s}"],
}
### Case Converter ##
## Converts string to PascalCase
##
## @param string
## @param keep_frist_ if true, the first underscore is preserved
static func pascal(string: String, keep_first_ := false) -> String:
var result = ""
var strings := string.split(" ")
for s in strings:
if s.empty():
result += " "
continue
var first = s[0] == '_'
var s2 = s.capitalize()
result += ("_" if first && keep_first_ else "") + s2.replace(" ", "") + " "
result.erase(result.length() - 1, 1)
return result
## Converts string to camelCase
##
## @param string
## @param keep_first_ if true, the first underscrore is preserved
static func camelCase(string: String, keep_first_ := false) -> String:
var first = string[0] == '_'
string = string.capitalize().replace(" ", "")
string[0] = string[0].to_lower()
return ("_" if first && keep_first_ else "") + string
# Utilities
static func find_not_in_string(string: String, character: String, start_offset := 0) -> int:
var quote := -1
for i in string.length():
if string[i] == '"':
if quote == -1:
quote = i
else:
quote = -1
continue
if i < start_offset:
continue
var pos = string.find(character, i - character.length())
if pos != -1 && pos <= i && quote == -1:
return i
return -1
## Returns the assignment expression as
## [left side, operator, right side]
static func split_math(string: String) -> Array:
for op in MATH_OPERATORS:
var pos = string.find(op)
if pos != -1:
return [string.substr(0, pos).strip_edges(), op, string.substr(pos + op.length())]
assert(false) # Here is no math operation to be split
return [string, "", ""]
## Returns the assignment expression as
## [left side, operator, right side]
static func split_assignment(string: String) -> Array:
for op in ASSIGNMENT_OPERATORS:
var pos = string.find(op)
if pos != -1:
return [string.substr(0, pos).strip_edges(), op, string.substr(pos + op.length())]
assert(false)
return [string, "", ""]
## Returns the bitwise expression as
## [left side, operator, right side]
static func split_bitwise(string: String) -> Array:
for op in BITWISE_OPERATORS:
var pos = string.find(op)
if pos != -1:
return [string.substr(0, pos).strip_edges(), op, string.substr(pos + op.length())]
assert(false)
return [string, "", ""]
## Returns the comparison expression as
## [left side, operator, right side]
static func split_comparison(string: String) -> Array:
for op in COMPARISON_OPERATORS:
var pos = string.find(op)
if pos != -1:
return [string.substr(0, pos).strip_edges(), op, string.substr(pos + op.length())]
assert(false)
return [string, "", ""]
## Returns the var converted if declared in a local block
## Returns empty array if none found
static func get_var_in_local_vars(string: String, lsv) -> Array:
for indent in lsv:
if lsv[indent].has(string):
return lsv[indent][string]
return []
## Returns variable name from declaration
static func get_var_name_from_d(string: String, is_const := false) -> String:
var offset = 5 if is_const else 3
var end = -1
if string.count(":=") > 0:
end = string.find(":=") - offset
elif find_not_in_string(string, ":") > 0:
end = find_not_in_string(string, ":") - offset
elif string.count("=") > 0:
end = string.find("=") - offset
return string.substr(offset, end).strip_edges()
## Returns true if method is remapped in C# (i.e. using a namespace)
static func is_remapped_method(method: String) -> bool:
return method in REMAP_METHODS
## Returns the C# variant of the the method
static func get_remapped_method(method: String) -> String:
assert(is_remapped_method(method))
return REMAP_METHODS[method]