gdharness 0.12.16 → 0.12.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/build/cli.js +11 -2
  2. package/build/godot/addons/auto_reload/auto_reload.gd +9 -2
  3. package/build/godot/addons/gdharness_editor/bridge_client.gd +11 -5
  4. package/build/godot/addons/gdharness_editor/plugin.gd +17 -4
  5. package/build/godot/addons/gdharness_editor/reading.gd +64 -0
  6. package/build/godot/addons/gdharness_editor/reading.gd.uid +1 -0
  7. package/build/godot/addons/gdharness_editor/tools/animation_tools.gd +30 -12
  8. package/build/godot/addons/gdharness_editor/tools/class_tools.gd +5 -2
  9. package/build/godot/addons/gdharness_editor/tools/play_tools.gd +3 -1
  10. package/build/godot/addons/gdharness_editor/tools/resource_tools.gd +50 -27
  11. package/build/godot/addons/gdharness_editor/tools/scene_tools.gd +92 -42
  12. package/build/godot/addons/gdharness_runtime/reading.gd +64 -0
  13. package/build/godot/addons/gdharness_runtime/reading.gd.uid +1 -0
  14. package/build/godot/addons/gdharness_runtime/runtime_autoload.gd +24 -10
  15. package/build/godot/addons/gdharness_runtime/runtime_capture.gd +7 -5
  16. package/build/godot/addons/gdharness_runtime/runtime_input.gd +24 -22
  17. package/build/godot/addons/gdharness_runtime/runtime_queries.gd +16 -13
  18. package/build/godot/addons/gdharness_runtime/runtime_values.gd +24 -8
  19. package/build/godot/addons/gdharness_runtime/runtime_waits.gd +7 -4
  20. package/build/godot/operations/audio_buses.gd +7 -6
  21. package/build/godot/operations/class_cache.gd +14 -9
  22. package/build/godot/operations/classdb_queries.gd +4 -3
  23. package/build/godot/operations/dependencies.gd +19 -16
  24. package/build/godot/operations/file_walk.gd +4 -2
  25. package/build/godot/operations/gdscript_analysis.gd +2 -3
  26. package/build/godot/operations/gdscript_authoring.gd +20 -8
  27. package/build/godot/operations/godot_operations.gd +7 -2
  28. package/build/godot/operations/import_pipeline.gd +5 -4
  29. package/build/godot/operations/input_actions.gd +13 -9
  30. package/build/godot/operations/patterns.gd +22 -0
  31. package/build/godot/operations/plugins.gd +10 -6
  32. package/build/godot/operations/project_config.gd +3 -2
  33. package/build/godot/operations/reading.gd +66 -0
  34. package/build/godot/operations/serialisation.gd +41 -9
  35. package/build/index.js +8 -1
  36. package/package.json +1 -1
@@ -3,6 +3,7 @@ extends RefCounted
3
3
  ## What the server asks about the running tree: its shape, one node, the nodes matching a
4
4
  ## question, where one is on screen, a property set, a method called, the metrics.
5
5
 
6
+ const Read = preload("reading.gd")
6
7
  const Values = preload("runtime_values.gd")
7
8
 
8
9
  ## The most nodes one find answers with, unless asked for fewer: enough for any real query and
@@ -31,8 +32,8 @@ func _init(host: Node, values: Values) -> void:
31
32
 
32
33
  func get_tree(params: Dictionary) -> Dictionary:
33
34
  var root_path: String = str(params.get("root", "/root"))
34
- var max_depth: int = int(params.get("depth", 3))
35
- var include_properties: bool = bool(params.get("include_properties", false))
35
+ var max_depth: int = Read.as_int(params.get("depth", 3), 3)
36
+ var include_properties: bool = Read.as_bool(params.get("include_properties", false))
36
37
 
37
38
  var reached: Dictionary = Values.node_at(_host.get_tree().root, root_path)
38
39
  if reached.has("message"):
@@ -65,8 +66,8 @@ func find_nodes(params: Dictionary) -> Dictionary:
65
66
  for filter: String in FILTERS:
66
67
  wanted[filter] = str(params.get(filter, ""))
67
68
  var wanted_property: String = str(params.get("property", ""))
68
- var include_hidden: bool = bool(params.get("include_hidden", true))
69
- var limit: int = clampi(int(params.get("limit", FIND_LIMIT)), 1, FIND_LIMIT_CEILING)
69
+ var include_hidden: bool = Read.as_bool(params.get("include_hidden", true), true)
70
+ var limit: int = clampi(Read.as_int(params.get("limit", FIND_LIMIT), FIND_LIMIT), 1, FIND_LIMIT_CEILING)
70
71
 
71
72
  if not _anything_asked(wanted):
72
73
  return {"type": "error", "message": "find_nodes needs at least one of " + ", ".join(FILTERS)}
@@ -110,7 +111,7 @@ func find_nodes(params: Dictionary) -> Dictionary:
110
111
  pending.push_front(children[index])
111
112
 
112
113
  var answer: Dictionary = {"type": "nodes", "count": found.size(), "truncated": truncated, "nodes": found}
113
- var notes: PackedStringArray = []
114
+ var notes: Array[String] = []
114
115
  # Nothing found is the one answer that cannot be told apart from having asked the wrong
115
116
  # question, and a name written without a wildcard is the way an agent writes "contains".
116
117
  # Said only when it changes the answer, so a genuine nothing stays a plain nothing.
@@ -262,8 +263,8 @@ func _matches_apart_from_name(node: Node, wanted: Dictionary[String, String]) ->
262
263
  ## whose interesting half is below the cut says so in a number rather than in a flag.
263
264
  func read_text(params: Dictionary) -> Dictionary:
264
265
  var root_path: String = str(params.get("root", "/root"))
265
- var include_hidden: bool = bool(params.get("include_hidden", false))
266
- var limit: int = clampi(int(params.get("limit", READ_LIMIT)), 1, READ_LIMIT)
266
+ var include_hidden: bool = Read.as_bool(params.get("include_hidden", false))
267
+ var limit: int = clampi(Read.as_int(params.get("limit", READ_LIMIT), READ_LIMIT), 1, READ_LIMIT)
267
268
 
268
269
  var reached: Dictionary = Values.node_at(_host.get_tree().root, root_path)
269
270
  if reached.has("message"):
@@ -275,7 +276,7 @@ func read_text(params: Dictionary) -> Dictionary:
275
276
  # player is being asked to answer below them reads exactly like a dialog that is not there,
276
277
  # and a bare `truncated: true` was looked straight past twice before the limit was suspected.
277
278
  # A number is what sends somebody back.
278
- var lines: PackedStringArray = PackedStringArray()
279
+ var lines: Array[String] = []
279
280
  var left_out: int = _read_into(root, include_hidden, limit, lines)
280
281
  return {
281
282
  "type": "text",
@@ -297,7 +298,7 @@ func read_text(params: Dictionary) -> Dictionary:
297
298
  ## until it is opened, and reading a closed menu would put every item on the screen.
298
299
  ## Fills [param into] up to [param most] lines and answers with how many further lines the rest of
299
300
  ## the subtree says, which is what the caller is told rather than left to infer.
300
- func _read_into(node: Node, include_hidden: bool, most: int, into: PackedStringArray) -> int:
301
+ func _read_into(node: Node, include_hidden: bool, most: int, into: Array[String]) -> int:
301
302
  if not include_hidden and not _drawn(node):
302
303
  return 0
303
304
  var left_out: int = 0
@@ -336,7 +337,7 @@ static func _drawn(node: Node) -> bool:
336
337
  ## the three of them come to disagree about a SpinBox.
337
338
  static func said_by(node: Node) -> String:
338
339
  for property: Dictionary in node.get_property_list():
339
- if str(property.get("name", "")) == "text" and int(property.get("type", 0)) == TYPE_STRING:
340
+ if str(property.get("name", "")) == "text" and Read.as_int(property.get("type", 0)) == TYPE_STRING:
340
341
  return str(node.get("text")).strip_edges()
341
342
  return ""
342
343
 
@@ -374,7 +375,8 @@ func get_rect(params: Dictionary) -> Dictionary:
374
375
  "window": _values.serialize(window_position),
375
376
  }
376
377
  if node is Node3D:
377
- return _in_the_frame(node_path, node)
378
+ var spatial: Node3D = node
379
+ return _in_the_frame(node_path, spatial)
378
380
  return {
379
381
  "type": "error", "message": "%s is a %s, which has no place on screen" % [node_path, node.get_class()]
380
382
  }
@@ -573,7 +575,8 @@ static func _can_read(holder: Variant, named: String) -> bool:
573
575
  var map: Dictionary = holder
574
576
  return map.has(named) or map.has(StringName(named))
575
577
  if holder is Object:
576
- return _has_property(holder, named)
578
+ var object: Object = holder
579
+ return _has_property(object, named)
577
580
  return false
578
581
 
579
582
 
@@ -638,7 +641,7 @@ static func _nothing_there(holder: Variant, named: String, called: String) -> St
638
641
  var keys: Array = map.keys()
639
642
  # Eight of them, because a map keyed by something unexpected is told by the first few and a
640
643
  # map of two hundred would bury the sentence saying which key was missing.
641
- var some: PackedStringArray = []
644
+ var some: Array[String] = []
642
645
  for key: Variant in keys.slice(0, 8):
643
646
  some.append(str(key))
644
647
  var rest: String = ""
@@ -3,6 +3,8 @@ extends RefCounted
3
3
  ## What crosses the wire between the game and the server, in both directions: Godot values
4
4
  ## as JSON-safe dictionaries, and JSON back into the values a property or parameter wants.
5
5
 
6
+ const Read = preload("reading.gd")
7
+
6
8
  # What each Godot type becomes on the wire, keyed on typeof() rather than written as a chain of
7
9
  # `is` tests whose order has to be trusted: Resource had to be tested before Object, or every
8
10
  # resource came back as a bare class name with its path dropped.
@@ -190,17 +192,30 @@ func deserialize(value: Variant) -> Variant:
190
192
 
191
193
  match fields["_type"]:
192
194
  "Vector2":
193
- return Vector2(fields.get("x", 0), fields.get("y", 0))
195
+ return Vector2(Read.as_float(fields.get("x", 0)), Read.as_float(fields.get("y", 0)))
194
196
  "Vector3":
195
- return Vector3(fields.get("x", 0), fields.get("y", 0), fields.get("z", 0))
197
+ return Vector3(
198
+ Read.as_float(fields.get("x", 0)),
199
+ Read.as_float(fields.get("y", 0)),
200
+ Read.as_float(fields.get("z", 0))
201
+ )
196
202
  "Vector2i":
197
- return Vector2i(fields.get("x", 0), fields.get("y", 0))
203
+ return Vector2i(Read.as_int(fields.get("x", 0)), Read.as_int(fields.get("y", 0)))
198
204
  "Vector3i":
199
- return Vector3i(fields.get("x", 0), fields.get("y", 0), fields.get("z", 0))
205
+ return Vector3i(
206
+ Read.as_int(fields.get("x", 0)),
207
+ Read.as_int(fields.get("y", 0)),
208
+ Read.as_int(fields.get("z", 0))
209
+ )
200
210
  "Color":
201
- return Color(fields.get("r", 0), fields.get("g", 0), fields.get("b", 0), fields.get("a", 1))
211
+ return Color(
212
+ Read.as_float(fields.get("r", 0)),
213
+ Read.as_float(fields.get("g", 0)),
214
+ Read.as_float(fields.get("b", 0)),
215
+ Read.as_float(fields.get("a", 1), 1.0)
216
+ )
202
217
  "NodePath":
203
- return NodePath(fields.get("path", ""))
218
+ return NodePath(str(fields.get("path", "")))
204
219
  return value
205
220
 
206
221
 
@@ -214,7 +229,7 @@ func parameter_type(object: Object, method: String, index: int) -> int:
214
229
  if index < 0 or index >= params.size():
215
230
  return TYPE_NIL
216
231
  var parameter: Dictionary = params[index]
217
- return int(parameter.get("type", TYPE_NIL))
232
+ return Read.as_int(parameter.get("type", TYPE_NIL), TYPE_NIL)
218
233
  return TYPE_NIL
219
234
 
220
235
 
@@ -231,7 +246,8 @@ func fitted(value: Variant, type: int) -> Variant:
231
246
  return rebuilt
232
247
 
233
248
  if rebuilt is String and type != TYPE_STRING:
234
- var parsed: Variant = JSON.parse_string(rebuilt)
249
+ var text: String = rebuilt
250
+ var parsed: Variant = JSON.parse_string(text)
235
251
  if typeof(parsed) != TYPE_NIL and typeof(parsed) != TYPE_STRING:
236
252
  rebuilt = parsed
237
253
 
@@ -4,6 +4,7 @@ extends RefCounted
4
4
  ## happened, or when the time ran out, so the caller never sleeps for a guessed length.
5
5
 
6
6
  const Queries = preload("runtime_queries.gd")
7
+ const Read = preload("reading.gd")
7
8
  const Values = preload("runtime_values.gd")
8
9
 
9
10
  ## The longest one wait may last, whatever the request says: past this the server has long
@@ -34,7 +35,7 @@ static func _out_of_range(named: String, asked: int, least: int, most: int) -> D
34
35
 
35
36
 
36
37
  func wait_frames(params: Dictionary) -> Dictionary:
37
- var frames: int = int(params.get("frames", 1))
38
+ var frames: int = Read.as_int(params.get("frames", 1), 1)
38
39
  if frames < 1 or frames > MOST_FRAMES:
39
40
  return _out_of_range("frames", frames, 1, MOST_FRAMES)
40
41
  var started: int = Time.get_ticks_msec()
@@ -47,7 +48,7 @@ func wait_frames(params: Dictionary) -> Dictionary:
47
48
  func wait_signal(params: Dictionary) -> Dictionary:
48
49
  var node_path: String = str(params.get("path", ""))
49
50
  var signal_name: String = str(params.get("signal", ""))
50
- var timeout_ms: int = int(params.get("timeout_ms", 5000))
51
+ var timeout_ms: int = Read.as_int(params.get("timeout_ms", 5000), 5000)
51
52
  if timeout_ms < 1 or timeout_ms > CEILING_MSEC:
52
53
  return _out_of_range("timeout_ms", timeout_ms, 1, CEILING_MSEC)
53
54
  if node_path.is_empty() or signal_name.is_empty():
@@ -63,7 +64,9 @@ func wait_signal(params: Dictionary) -> Dictionary:
63
64
  var catcher: SignalCatcher = SignalCatcher.new()
64
65
  catcher.arity = _signal_arity(node, signal_name)
65
66
  var callable: Callable = catcher._on_fired
66
- node.connect(signal_name, callable, CONNECT_ONE_SHOT)
67
+ var listening: Error = node.connect(signal_name, callable, CONNECT_ONE_SHOT)
68
+ if listening != OK:
69
+ return {"type": "error", "message": "Could not listen to signal: " + signal_name}
67
70
  var started: int = Time.get_ticks_msec()
68
71
  while not catcher.fired and Time.get_ticks_msec() - started < timeout_ms:
69
72
  await _host.get_tree().process_frame
@@ -86,7 +89,7 @@ func wait_until(params: Dictionary) -> Dictionary:
86
89
  var node_path: String = str(params.get("path", ""))
87
90
  var property: String = str(params.get("property", ""))
88
91
  var says: String = str(params.get("says", ""))
89
- var timeout_ms: int = int(params.get("timeout_ms", 5000))
92
+ var timeout_ms: int = Read.as_int(params.get("timeout_ms", 5000), 5000)
90
93
  if timeout_ms < 1 or timeout_ms > CEILING_MSEC:
91
94
  return _out_of_range("timeout_ms", timeout_ms, 1, CEILING_MSEC)
92
95
  if node_path.is_empty():
@@ -3,6 +3,7 @@ extends RefCounted
3
3
  # The bus layout is a project resource the engine loads at startup and this process holds in
4
4
  # memory, so every change is written back to that file or it is gone with the process.
5
5
 
6
+ const Read = preload("reading.gd")
6
7
  const Log = preload("logger.gd")
7
8
 
8
9
  var _log: Log
@@ -16,7 +17,7 @@ func create_audio_bus(params: Dictionary) -> Dictionary:
16
17
  var bus_name: String = str(params.get("bus_name", ""))
17
18
  if bus_name.is_empty():
18
19
  return _log.failure("bus_name is required")
19
- var parent_idx: int = int(params.get("parent_bus_index", 0))
20
+ var parent_idx: int = Read.as_int(params.get("parent_bus_index", 0))
20
21
  if parent_idx < 0 or parent_idx >= AudioServer.bus_count:
21
22
  return _log.failure("No bus at index " + str(parent_idx))
22
23
 
@@ -40,10 +41,10 @@ func get_audio_buses(_params: Dictionary) -> Dictionary:
40
41
 
41
42
 
42
43
  func set_audio_bus_effect(params: Dictionary) -> Dictionary:
43
- var bus_idx: int = int(params.get("bus_index", 0))
44
- var effect_idx: int = int(params.get("effect_index", 0))
44
+ var bus_idx: int = Read.as_int(params.get("bus_index", 0))
45
+ var effect_idx: int = Read.as_int(params.get("effect_index", 0))
45
46
  var effect_type: String = str(params.get("effect_type", ""))
46
- var enabled: bool = bool(params.get("enabled", true))
47
+ var enabled: bool = Read.as_bool(params.get("enabled", true), true)
47
48
  if bus_idx < 0 or bus_idx >= AudioServer.bus_count:
48
49
  return _log.failure("No bus at index " + str(bus_idx))
49
50
 
@@ -65,8 +66,8 @@ func set_audio_bus_effect(params: Dictionary) -> Dictionary:
65
66
 
66
67
 
67
68
  func set_audio_bus_volume(params: Dictionary) -> Dictionary:
68
- var bus_idx: int = int(params.get("bus_index", 0))
69
- var volume_db: float = float(params.get("volume_db", 0.0))
69
+ var bus_idx: int = Read.as_int(params.get("bus_index", 0))
70
+ var volume_db: float = Read.as_float(params.get("volume_db", 0.0))
70
71
  if bus_idx < 0 or bus_idx >= AudioServer.bus_count:
71
72
  return _log.failure("No bus at index " + str(bus_idx))
72
73
 
@@ -5,6 +5,8 @@ extends RefCounted
5
5
  # A game started from a stale editor then cannot resolve any class_name written since, and the
6
6
  # engine reads the same file headless, so the list is rebuilt here from the scripts themselves.
7
7
 
8
+ const Patterns = preload("patterns.gd")
9
+ const Read = preload("reading.gd")
8
10
  const FileWalk = preload("file_walk.gd")
9
11
  const Log = preload("logger.gd")
10
12
 
@@ -33,7 +35,11 @@ func refresh_class_cache(_params: Dictionary) -> Dictionary:
33
35
  var after: Dictionary = _entries_by_class(entries)
34
36
  var config: ConfigFile = ConfigFile.new()
35
37
  config.set_value("", "list", entries)
36
- DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(CACHE_PATH.get_base_dir()))
38
+ var made: Error = DirAccess.make_dir_recursive_absolute(
39
+ ProjectSettings.globalize_path(CACHE_PATH.get_base_dir())
40
+ )
41
+ if made != OK and made != ERR_ALREADY_EXISTS:
42
+ return _log.failure("Could not make " + CACHE_PATH.get_base_dir() + ": " + error_string(made))
37
43
  var err: Error = config.save(CACHE_PATH)
38
44
  if err != OK:
39
45
  return _log.failure("Failed to write " + CACHE_PATH + ": " + error_string(err))
@@ -80,8 +86,8 @@ func _entry_for(path: String, skipped: Array[Dictionary]) -> Dictionary:
80
86
  "base": StringName(base),
81
87
  "class": StringName(declared),
82
88
  "icon": str(header.get("icon", "")),
83
- "is_abstract": bool(header.get("abstract", false)),
84
- "is_tool": bool(header.get("tool", false)),
89
+ "is_abstract": Read.as_bool(header.get("abstract", false)),
90
+ "is_tool": Read.as_bool(header.get("tool", false)),
85
91
  "language": StringName("GDScript"),
86
92
  "path": path,
87
93
  }
@@ -123,12 +129,11 @@ func _header_of(path: String) -> Dictionary:
123
129
  var file: FileAccess = FileAccess.open(path, FileAccess.READ)
124
130
  if not file:
125
131
  return header
126
- var annotation: RegEx = RegEx.new()
127
- annotation.compile('^@([a-z_]+)(?:\\(\\s*(?:"([^"]*)")?[^)]*\\))?\\s*')
128
- var class_line: RegEx = RegEx.new()
129
- class_line.compile("^class_name\\s+([A-Za-z_][A-Za-z0-9_]*)(?:\\s+extends\\s+(\\S+))?")
130
- var extends_line: RegEx = RegEx.new()
131
- extends_line.compile("^extends\\s+(\\S+)")
132
+ var annotation: RegEx = Patterns.compiled('^@([a-z_]+)(?:\\(\\s*(?:"([^"]*)")?[^)]*\\))?\\s*')
133
+ var class_line: RegEx = Patterns.compiled(
134
+ "^class_name\\s+([A-Za-z_][A-Za-z0-9_]*)(?:\\s+extends\\s+(\\S+))?"
135
+ )
136
+ var extends_line: RegEx = Patterns.compiled("^extends\\s+(\\S+)")
132
137
  while not file.eof_reached():
133
138
  var rest: String = file.get_line().strip_edges()
134
139
  # Annotations may share a line with what they annotate, as in `@abstract class_name X`.
@@ -1,5 +1,6 @@
1
1
  extends RefCounted
2
2
 
3
+ const Read = preload("reading.gd")
3
4
  const Log = preload("logger.gd")
4
5
 
5
6
  # The base class each category name stands for.
@@ -27,7 +28,7 @@ func _init(p_log: Log) -> void:
27
28
  func query_classes(params: Dictionary) -> Dictionary:
28
29
  var filter: String = str(params.get("filter", ""))
29
30
  var category: String = str(params.get("category", ""))
30
- var instantiable_only: bool = bool(params.get("instantiable_only", false))
31
+ var instantiable_only: bool = Read.as_bool(params.get("instantiable_only", false))
31
32
 
32
33
  _log.info(
33
34
  (
@@ -82,7 +83,7 @@ func query_classes(params: Dictionary) -> Dictionary:
82
83
  # Query detailed info about a specific class from ClassDB
83
84
  func query_class_info(params: Dictionary) -> Dictionary:
84
85
  var class_name_str: String = str(params.get("class_name", ""))
85
- var include_inherited: bool = bool(params.get("include_inherited", false))
86
+ var include_inherited: bool = Read.as_bool(params.get("include_inherited", false))
86
87
 
87
88
  _log.info(
88
89
  "Querying class info for: " + class_name_str + " (include_inherited: " + str(include_inherited) + ")"
@@ -220,7 +221,7 @@ func _properties_of(class_name_str: String, include_inherited: bool) -> Array[Di
220
221
 
221
222
  for p: Dictionary in ClassDB.class_get_property_list(class_name_str, not include_inherited):
222
223
  # A category, group or subgroup is an editor heading rather than a property.
223
- var usage: int = int(p.get("usage", 0))
224
+ var usage: int = Read.as_int(p.get("usage", 0))
224
225
  if usage & PROPERTY_USAGE_CATEGORY or usage & PROPERTY_USAGE_GROUP or usage & PROPERTY_USAGE_SUBGROUP:
225
226
  continue
226
227
  properties.append(
@@ -1,5 +1,7 @@
1
1
  extends RefCounted
2
2
 
3
+ const Patterns = preload("patterns.gd")
4
+ const Read = preload("reading.gd")
3
5
  const FileWalk = preload("file_walk.gd")
4
6
  const Log = preload("logger.gd")
5
7
 
@@ -39,9 +41,9 @@ func _init(p_log: Log) -> void:
39
41
  func get_dependencies(params: Dictionary) -> Dictionary:
40
42
  var resource_path: String = str(params.get("resource_path", ""))
41
43
  # No depth, or a depth of zero or less, means the whole chain; the walk stops at cycles.
42
- var depth: int = int(params.get("depth", 0))
44
+ var depth: int = Read.as_int(params.get("depth", 0))
43
45
  var max_depth: int = depth if depth > 0 else 1000
44
- var include_built_in: bool = bool(params.get("include_builtin", false))
46
+ var include_built_in: bool = Read.as_bool(params.get("include_builtin", false))
45
47
 
46
48
  _log.info(
47
49
  (
@@ -83,7 +85,10 @@ func get_dependencies(params: Dictionary) -> Dictionary:
83
85
 
84
86
  var dep_count: int = 0
85
87
  for key: String in dependencies:
86
- dep_count += _count_recursive(dependencies[key])
88
+ # Through a typed local: what comes out of a Dictionary is Variant, and a project that
89
+ # errors on handing one to a typed parameter will not compile this script at all.
90
+ var walked: Array[Dictionary] = dependencies[key]
91
+ dep_count += _count_recursive(walked)
87
92
 
88
93
  return {
89
94
  "dependencies": dependencies,
@@ -111,12 +116,10 @@ func find_resource_usages(params: Dictionary) -> Dictionary:
111
116
  _log.info("Finding usages of: " + resource_path)
112
117
 
113
118
  var class_name_declared: String = _declared_class_name(resource_path)
114
- var by_path: RegEx = RegEx.new()
115
- by_path.compile('"(res://)?' + _regex_escaped(resource_path.substr(6)) + '"')
119
+ var by_path: RegEx = Patterns.compiled('"(res://)?' + _regex_escaped(resource_path.substr(6)) + '"')
116
120
  var by_class: RegEx = null
117
121
  if not class_name_declared.is_empty():
118
- by_class = RegEx.new()
119
- by_class.compile("\\b" + _regex_escaped(class_name_declared) + "\\b")
122
+ by_class = Patterns.compiled("\\b" + _regex_escaped(class_name_declared) + "\\b")
120
123
 
121
124
  var all_files: Array[String] = []
122
125
  for ext: Variant in file_types:
@@ -146,7 +149,7 @@ func find_resource_usages(params: Dictionary) -> Dictionary:
146
149
  if kind.is_empty():
147
150
  continue
148
151
  references.append({"line": i + 1, "kind": kind, "text": line.strip_edges()})
149
- by_kind[kind] = int(by_kind.get(kind, 0)) + 1
152
+ by_kind[kind] = Read.as_int(by_kind.get(kind, 0)) + 1
150
153
 
151
154
  if not references.is_empty():
152
155
  usages.append({"file": file_path, "references": references})
@@ -176,8 +179,7 @@ func _declared_class_name(path: String) -> String:
176
179
  var file: FileAccess = FileAccess.open(path, FileAccess.READ)
177
180
  if not file:
178
181
  return ""
179
- var declaration: RegEx = RegEx.new()
180
- declaration.compile("^class_name\\s+([A-Za-z_][A-Za-z0-9_]*)")
182
+ var declaration: RegEx = Patterns.compiled("^class_name\\s+([A-Za-z_][A-Za-z0-9_]*)")
181
183
  while not file.eof_reached():
182
184
  var found: RegExMatch = declaration.search(file.get_line())
183
185
  if found != null:
@@ -214,7 +216,8 @@ func _count_recursive(deps: Array[Dictionary]) -> int:
214
216
  var count: int = deps.size()
215
217
  for dep: Dictionary in deps:
216
218
  if dep.has("dependencies"):
217
- count += _count_recursive(dep["dependencies"])
219
+ var deeper: Array[Dictionary] = dep["dependencies"]
220
+ count += _count_recursive(deeper)
218
221
  return count
219
222
 
220
223
 
@@ -243,8 +246,7 @@ func _analyze_resource(path: String, current_depth: int, walk: DependencyWalk) -
243
246
  file.close()
244
247
 
245
248
  for pattern: String in REFERENCE_PATTERNS:
246
- var regex: RegEx = RegEx.new()
247
- regex.compile(pattern)
249
+ var regex: RegEx = Patterns.compiled(pattern)
248
250
  for m: RegExMatch in regex.search_all(content):
249
251
  var dep_path: String = _referenced_path(m.get_string())
250
252
 
@@ -282,12 +284,13 @@ func _analyze_resource(path: String, current_depth: int, walk: DependencyWalk) -
282
284
 
283
285
  # The path inside a preload(), load() or ext_resource match; a bare res:// match is the path.
284
286
  func _referenced_path(matched: String) -> String:
285
- var inner: RegEx = RegEx.new()
287
+ var expression: String = ""
286
288
  if "preload" in matched or "load" in matched:
287
- inner.compile('"([^"]+)"')
289
+ expression = '"([^"]+)"'
288
290
  elif "ext_resource" in matched:
289
- inner.compile('path="([^"]+)"')
291
+ expression = 'path="([^"]+)"'
290
292
  else:
291
293
  return matched
294
+ var inner: RegEx = Patterns.compiled(expression)
292
295
  var inner_match: RegExMatch = inner.search(matched)
293
296
  return inner_match.get_string(1) if inner_match else matched
@@ -24,7 +24,8 @@ func find_files(path: String, extension: String) -> Array[String]:
24
24
  var dir: DirAccess = DirAccess.open(path)
25
25
 
26
26
  if dir:
27
- dir.list_dir_begin()
27
+ if dir.list_dir_begin() != OK:
28
+ return files
28
29
  var file_name: String = dir.get_next()
29
30
 
30
31
  while file_name != "":
@@ -47,7 +48,8 @@ func find_files_with_extensions(path: String, extensions: Array) -> Array[String
47
48
  var dir: DirAccess = DirAccess.open(path)
48
49
 
49
50
  if dir:
50
- dir.list_dir_begin()
51
+ if dir.list_dir_begin() != OK:
52
+ return files
51
53
  var file_name: String = dir.get_next()
52
54
 
53
55
  while file_name != "":
@@ -1,5 +1,6 @@
1
1
  extends RefCounted
2
2
 
3
+ const Patterns = preload("patterns.gd")
3
4
  const Log = preload("logger.gd")
4
5
 
5
6
  var _log: Log
@@ -278,9 +279,7 @@ func _parse_param(param_text: String) -> Dictionary:
278
279
 
279
280
  func _extract_dependencies(line: String) -> Array[String]:
280
281
  var deps: Array[String] = []
281
- var regex: RegEx = RegEx.new()
282
-
283
- regex.compile("(?:preload|load)\\s*\\(\\s*[\"']([^\"']+)[\"']\\s*\\)")
282
+ var regex: RegEx = Patterns.compiled("(?:preload|load)\\s*\\(\\s*[\"']([^\"']+)[\"']\\s*\\)")
284
283
 
285
284
  for m: RegExMatch in regex.search_all(line):
286
285
  deps.append(m.get_string(1))
@@ -1,5 +1,6 @@
1
1
  extends RefCounted
2
2
 
3
+ const Read = preload("reading.gd")
3
4
  const Log = preload("logger.gd")
4
5
 
5
6
  var _log: Log
@@ -58,8 +59,10 @@ func create_gdscript(params: Dictionary) -> Dictionary:
58
59
  if not file:
59
60
  return _log.failure("Failed to create script file: " + full_script_path)
60
61
 
61
- file.store_string(script_content)
62
+ var stored: bool = file.store_string(script_content)
62
63
  file.close()
64
+ if not stored:
65
+ return _log.failure("Failed to write to script file: " + full_script_path)
63
66
 
64
67
  # Whether the engine accepts what was written, parsed under this project's own warning
65
68
  # settings: a script that does not load is a script the caller wants to hear about now,
@@ -132,8 +135,10 @@ func modify_gdscript(params: Dictionary) -> Dictionary:
132
135
  if not file:
133
136
  return _log.failure("Failed to write to script file: " + full_script_path)
134
137
 
135
- file.store_string("\n".join(lines))
138
+ var rewrote: bool = file.store_string("\n".join(lines))
136
139
  file.close()
140
+ if not rewrote:
141
+ return _log.failure("Failed to write to script file: " + full_script_path)
137
142
 
138
143
  return {
139
144
  "success": true,
@@ -143,14 +148,15 @@ func modify_gdscript(params: Dictionary) -> Dictionary:
143
148
  }
144
149
 
145
150
 
146
- # Inserts the declaration and answers with its one-based line number.
151
+ # Inserts the declaration and answers with its one-based line number, or 0 for one that could not
152
+ # be placed, which is a number no line has.
147
153
  func _add_variable(lines: Array[String], mod: Dictionary) -> int:
148
154
  var var_name: String = str(mod.get("name", ""))
149
155
  var var_type: String = str(mod.get("varType", ""))
150
156
  var default_value: String = str(mod.get("defaultValue", ""))
151
- var is_export: bool = bool(mod.get("isExport", false))
157
+ var is_export: bool = Read.as_bool(mod.get("isExport", false))
152
158
  var export_hint: String = str(mod.get("exportHint", ""))
153
- var is_onready: bool = bool(mod.get("isOnready", false))
159
+ var is_onready: bool = Read.as_bool(mod.get("isOnready", false))
154
160
 
155
161
  var var_line: String = ""
156
162
 
@@ -179,7 +185,9 @@ func _add_variable(lines: Array[String], mod: Dictionary) -> int:
179
185
  var_line += ": Variant"
180
186
 
181
187
  var insert_line: int = _variable_insertion_point(lines)
182
- lines.insert(insert_line, var_line)
188
+ if lines.insert(insert_line, var_line) != OK:
189
+ push_error("gdharness: could not place a declaration at line " + str(insert_line))
190
+ return 0
183
191
  return insert_line + 1
184
192
 
185
193
 
@@ -208,7 +216,9 @@ func _add_signal(lines: Array[String], mod: Dictionary) -> int:
208
216
  signal_line += "(" + signal_params + ")"
209
217
 
210
218
  var insert_line: int = _signal_insertion_point(lines)
211
- lines.insert(insert_line, signal_line)
219
+ if lines.insert(insert_line, signal_line) != OK:
220
+ push_error("gdharness: could not place a signal at line " + str(insert_line))
221
+ return 0
212
222
  return insert_line + 1
213
223
 
214
224
 
@@ -234,7 +244,9 @@ func _add_function(lines: Array[String], mod: Dictionary) -> int:
234
244
  var insert_line: int = _function_insertion_point(lines, position)
235
245
 
236
246
  for i: int in range(func_lines.size() - 1, -1, -1):
237
- lines.insert(insert_line, func_lines[i])
247
+ if lines.insert(insert_line, func_lines[i]) != OK:
248
+ push_error("gdharness: could not place a function at line " + str(insert_line))
249
+ return 0
238
250
 
239
251
  return insert_line + 1
240
252
 
@@ -48,10 +48,15 @@ func _init() -> void:
48
48
  _log.debug("All arguments: " + str(args))
49
49
 
50
50
  var operation: String = args[script_index + 2]
51
- var params: Variant = _read_params(args[params_index])
52
- if not params is Dictionary:
51
+ var read: Variant = _read_params(args[params_index])
52
+ if not read is Dictionary:
53
53
  quit(1)
54
54
  return
55
+ # Through a typed local rather than straight into the call. The check above proves the type to
56
+ # a reader and not to the analyser, so the call handed a Variant to a Dictionary parameter, and
57
+ # a project holding unsafe_call_argument at error level then refused to compile this script at
58
+ # all. It is compiled under the target project's warning levels, not under this package's.
59
+ var params: Dictionary = read
55
60
 
56
61
  _log.info("Executing operation: " + operation)
57
62
 
@@ -1,5 +1,6 @@
1
1
  extends RefCounted
2
2
 
3
+ const Read = preload("reading.gd")
3
4
  const FileWalk = preload("file_walk.gd")
4
5
  const Log = preload("logger.gd")
5
6
 
@@ -18,7 +19,7 @@ func _init(p_log: Log) -> void:
18
19
  # Get import status for resources
19
20
  func get_import_status(params: Dictionary) -> Dictionary:
20
21
  var resource_path: String = str(params.get("resource_path", ""))
21
- var include_up_to_date: bool = bool(params.get("include_up_to_date", false))
22
+ var include_up_to_date: bool = Read.as_bool(params.get("include_up_to_date", false))
22
23
 
23
24
  _log.info(
24
25
  (
@@ -90,7 +91,7 @@ func set_import_options(params: Dictionary) -> Dictionary:
90
91
  resource_path = "res://" + resource_path
91
92
 
92
93
  var options: Dictionary = params.get("options", {})
93
- var do_reimport: bool = bool(params.get("reimport", true))
94
+ var do_reimport: bool = Read.as_bool(params.get("reimport", true), true)
94
95
 
95
96
  _log.info("Setting import options for: " + resource_path)
96
97
 
@@ -131,7 +132,7 @@ func set_import_options(params: Dictionary) -> Dictionary:
131
132
  # Reimport a resource or all resources
132
133
  func reimport_resource(params: Dictionary) -> Dictionary:
133
134
  var resource_path: String = str(params.get("resource_path", ""))
134
- var force: bool = bool(params.get("force", false))
135
+ var force: bool = Read.as_bool(params.get("force", false))
135
136
 
136
137
  _log.info(
137
138
  (
@@ -208,7 +209,7 @@ func list_export_presets(_params: Dictionary) -> Dictionary:
208
209
  # Validate project for export
209
210
  func validate_project(params: Dictionary) -> Dictionary:
210
211
  var preset_name: String = str(params.get("preset", ""))
211
- var include_suggestions: bool = bool(params.get("include_suggestions", true))
212
+ var include_suggestions: bool = Read.as_bool(params.get("include_suggestions", true), true)
212
213
 
213
214
  _log.info("Validating project" + (" for preset: " + preset_name if not preset_name.is_empty() else ""))
214
215