| 1 |
# ------------------------------------------------------------------------------ |
| 2 |
# Utility class to hold the local and built in methods separately. Add all local |
| 3 |
# methods FIRST, then add built ins. |
| 4 |
# ------------------------------------------------------------------------------ |
| 5 |
class ScriptMethods: |
| 6 |
# List of methods that should not be overloaded when they are not defined |
| 7 |
# in the class being doubled. These either break things if they are |
| 8 |
# overloaded or do not have a "super" equivalent so we can't just pass |
| 9 |
# through. |
| 10 |
var _blacklist = [ |
| 11 |
'has_method', |
| 12 |
'get_script', |
| 13 |
'get', |
| 14 |
'_notification', |
| 15 |
'get_path', |
| 16 |
'_enter_tree', |
| 17 |
'_exit_tree', |
| 18 |
'_process', |
| 19 |
'_draw', |
| 20 |
'_physics_process', |
| 21 |
'_input', |
| 22 |
'_unhandled_input', |
| 23 |
'_unhandled_key_input', |
| 24 |
'_set', |
| 25 |
'_get', # probably |
| 26 |
'emit_signal', # can't handle extra parameters to be sent with signal. |
| 27 |
'draw_mesh', # issue with one parameter, value is `Null((..), (..), (..))`` |
| 28 |
'_to_string', # nonexistant function ._to_string |
| 29 |
'_get_minimum_size', # Nonexistent function _get_minimum_size |
| 30 |
] |
| 31 |
|
| 32 |
# These methods should not be included in the double. |
| 33 |
var _skip = [ |
| 34 |
# There is an init in the template. There is also no real reason |
| 35 |
# to include this method since it will always be called, it has no |
| 36 |
# return value, and you cannot prevent super from being called. |
| 37 |
'_init' |
| 38 |
] |
| 39 |
|
| 40 |
var built_ins = [] |
| 41 |
var local_methods = [] |
| 42 |
var _method_names = [] |
| 43 |
|
| 44 |
func is_blacklisted(method_meta): |
| 45 |
return _blacklist.find(method_meta.name) != -1 |
| 46 |
|
| 47 |
func _add_name_if_does_not_have(method_name): |
| 48 |
if(_skip.has(method_name)): |
| 49 |
return false |
| 50 |
var should_add = _method_names.find(method_name) == -1 |
| 51 |
if(should_add): |
| 52 |
_method_names.append(method_name) |
| 53 |
return should_add |
| 54 |
|
| 55 |
func add_built_in_method(method_meta): |
| 56 |
var did_add = _add_name_if_does_not_have(method_meta.name) |
| 57 |
if(did_add and !is_blacklisted(method_meta)): |
| 58 |
built_ins.append(method_meta) |
| 59 |
|
| 60 |
func add_local_method(method_meta): |
| 61 |
var did_add = _add_name_if_does_not_have(method_meta.name) |
| 62 |
if(did_add): |
| 63 |
local_methods.append(method_meta) |
| 64 |
|
| 65 |
func to_s(): |
| 66 |
var text = "Locals\n" |
| 67 |
for i in range(local_methods.size()): |
| 68 |
text += str(" ", local_methods[i].name, "\n") |
| 69 |
text += "Built-Ins\n" |
| 70 |
for i in range(built_ins.size()): |
| 71 |
text += str(" ", built_ins[i].name, "\n") |
| 72 |
return text |
| 73 |
|
| 74 |
# ------------------------------------------------------------------------------ |
| 75 |
# Helper class to deal with objects and inner classes. |
| 76 |
# ------------------------------------------------------------------------------ |
| 77 |
class ObjectInfo: |
| 78 |
var _path = null |
| 79 |
var _subpaths = [] |
| 80 |
var _utils = load('res://addons/gut/utils.gd').get_instance() |
| 81 |
var _method_strategy = null |
| 82 |
var make_partial_double = false |
| 83 |
var scene_path = null |
| 84 |
var _native_class = null |
| 85 |
var _native_class_name = null |
| 86 |
|
| 87 |
func _init(path, subpath=null): |
| 88 |
_path = path |
| 89 |
if(subpath != null): |
| 90 |
_subpaths = _utils.split_string(subpath, '/') |
| 91 |
|
| 92 |
# Returns an instance of the class/inner class |
| 93 |
func instantiate(): |
| 94 |
var to_return = null |
| 95 |
if(is_native()): |
| 96 |
to_return = _native_class.new() |
| 97 |
else: |
| 98 |
to_return = get_loaded_class().new() |
| 99 |
return to_return |
| 100 |
|
| 101 |
# Can't call it get_class because that is reserved so it gets this ugly name. |
| 102 |
# Loads up the class and then any inner classes to give back a reference to |
| 103 |
# the desired Inner class (if there is any) |
| 104 |
func get_loaded_class(): |
| 105 |
var LoadedClass = load(_path) |
| 106 |
for i in range(_subpaths.size()): |
| 107 |
LoadedClass = LoadedClass.get(_subpaths[i]) |
| 108 |
return LoadedClass |
| 109 |
|
| 110 |
func to_s(): |
| 111 |
return str(_path, '[', get_subpath(), ']') |
| 112 |
|
| 113 |
func get_path(): |
| 114 |
return _path |
| 115 |
|
| 116 |
func get_subpath(): |
| 117 |
return _utils.join_array(_subpaths, '/') |
| 118 |
|
| 119 |
func has_subpath(): |
| 120 |
return _subpaths.size() != 0 |
| 121 |
|
| 122 |
func get_extends_text(): |
| 123 |
var extend = null |
| 124 |
if(is_native()): |
| 125 |
var native = get_native_class_name() |
| 126 |
if(native.begins_with('_')): |
| 127 |
native = native.substr(1) |
| 128 |
extend = str("extends ", native) |
| 129 |
else: |
| 130 |
extend = str("extends '", get_path(), "'") |
| 131 |
|
| 132 |
if(has_subpath()): |
| 133 |
extend += str('.', get_subpath().replace('/', '.')) |
| 134 |
|
| 135 |
return extend |
| 136 |
|
| 137 |
func get_method_strategy(): |
| 138 |
return _method_strategy |
| 139 |
|
| 140 |
func set_method_strategy(method_strategy): |
| 141 |
_method_strategy = method_strategy |
| 142 |
|
| 143 |
func is_native(): |
| 144 |
return _native_class != null |
| 145 |
|
| 146 |
func set_native_class(native_class): |
| 147 |
_native_class = native_class |
| 148 |
var inst = native_class.new() |
| 149 |
_native_class_name = inst.get_class() |
| 150 |
_path = _native_class_name |
| 151 |
if(!inst is Reference): |
| 152 |
inst.free() |
| 153 |
|
| 154 |
func get_native_class_name(): |
| 155 |
return _native_class_name |
| 156 |
|
| 157 |
# ------------------------------------------------------------------------------ |
| 158 |
# Allows for interacting with a file but only creating a string. This was done |
| 159 |
# to ease the transition from files being created for doubles to loading |
| 160 |
# doubles from a string. This allows the files to be created for debugging |
| 161 |
# purposes since reading a file is easier than reading a dumped out string. |
| 162 |
# ------------------------------------------------------------------------------ |
| 163 |
class FileOrString: |
| 164 |
extends File |
| 165 |
|
| 166 |
var _do_file = false |
| 167 |
var _contents = '' |
| 168 |
var _path = null |
| 169 |
|
| 170 |
func open(path, mode): |
| 171 |
_path = path |
| 172 |
if(_do_file): |
| 173 |
return .open(path, mode) |
| 174 |
else: |
| 175 |
return OK |
| 176 |
|
| 177 |
func close(): |
| 178 |
if(_do_file): |
| 179 |
return .close() |
| 180 |
|
| 181 |
func store_string(s): |
| 182 |
if(_do_file): |
| 183 |
.store_string(s) |
| 184 |
_contents += s |
| 185 |
|
| 186 |
func get_contents(): |
| 187 |
return _contents |
| 188 |
|
| 189 |
func get_path(): |
| 190 |
return _path |
| 191 |
|
| 192 |
func load_it(): |
| 193 |
if(_contents != ''): |
| 194 |
var script = GDScript.new() |
| 195 |
script.set_source_code(get_contents()) |
| 196 |
script.reload() |
| 197 |
return script |
| 198 |
else: |
| 199 |
return load(_path) |
| 200 |
|
| 201 |
# ------------------------------------------------------------------------------ |
| 202 |
# A stroke of genius if I do say so. This allows for doubling a scene without |
| 203 |
# having to write any files. By overloading instance we can make whatever |
| 204 |
# we want. |
| 205 |
# ------------------------------------------------------------------------------ |
| 206 |
class PackedSceneDouble: |
| 207 |
extends PackedScene |
| 208 |
var _script = null |
| 209 |
var _scene = null |
| 210 |
|
| 211 |
func set_script_obj(obj): |
| 212 |
_script = obj |
| 213 |
|
| 214 |
func instance(edit_state=0): |
| 215 |
var inst = _scene.instance(edit_state) |
| 216 |
if(_script != null): |
| 217 |
inst.set_script(_script) |
| 218 |
return inst |
| 219 |
|
| 220 |
func load_scene(path): |
| 221 |
_scene = load(path) |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
# ------------------------------------------------------------------------------ |
| 227 |
# START Doubler |
| 228 |
# ------------------------------------------------------------------------------ |
| 229 |
var _utils = load('res://addons/gut/utils.gd').get_instance() |
| 230 |
|
| 231 |
var _ignored_methods = _utils.OneToMany.new() |
| 232 |
var _stubber = _utils.Stubber.new() |
| 233 |
var _lgr = _utils.get_logger() |
| 234 |
var _method_maker = _utils.MethodMaker.new() |
| 235 |
|
| 236 |
var _output_dir = 'user://gut_temp_directory' |
| 237 |
var _double_count = 0 # used in making files names unique |
| 238 |
var _spy = null |
| 239 |
var _gut = null |
| 240 |
var _strategy = null |
| 241 |
var _base_script_text = _utils.get_file_as_text('res://addons/gut/double_templates/script_template.txt') |
| 242 |
var _make_files = false |
| 243 |
|
| 244 |
# These methods all call super implicitly. Stubbing them to call super causes |
| 245 |
# super to be called twice. |
| 246 |
var _non_super_methods = [ |
| 247 |
"_init", |
| 248 |
"_ready", |
| 249 |
"_notification", |
| 250 |
"_enter_world", |
| 251 |
"_exit_world", |
| 252 |
"_process", |
| 253 |
"_physics_process", |
| 254 |
"_exit_tree", |
| 255 |
"_gui_input ", |
| 256 |
] |
| 257 |
|
| 258 |
func _init(strategy=_utils.DOUBLE_STRATEGY.PARTIAL): |
| 259 |
set_logger(_utils.get_logger()) |
| 260 |
_strategy = strategy |
| 261 |
|
| 262 |
# ############### |
| 263 |
# Private |
| 264 |
# ############### |
| 265 |
func _get_indented_line(indents, text): |
| 266 |
var to_return = '' |
| 267 |
for _i in range(indents): |
| 268 |
to_return += "\t" |
| 269 |
return str(to_return, text, "\n") |
| 270 |
|
| 271 |
|
| 272 |
func _stub_to_call_super(obj_info, method_name): |
| 273 |
if(_non_super_methods.has(method_name)): |
| 274 |
return |
| 275 |
var path = obj_info.get_path() |
| 276 |
if(obj_info.scene_path != null): |
| 277 |
path = obj_info.scene_path |
| 278 |
var params = _utils.StubParams.new(path, method_name, obj_info.get_subpath()) |
| 279 |
params.to_call_super() |
| 280 |
_stubber.add_stub(params) |
| 281 |
|
| 282 |
func _get_base_script_text(obj_info, override_path): |
| 283 |
var path = obj_info.get_path() |
| 284 |
if(override_path != null): |
| 285 |
path = override_path |
| 286 |
|
| 287 |
var stubber_id = -1 |
| 288 |
if(_stubber != null): |
| 289 |
stubber_id = _stubber.get_instance_id() |
| 290 |
|
| 291 |
var spy_id = -1 |
| 292 |
if(_spy != null): |
| 293 |
spy_id = _spy.get_instance_id() |
| 294 |
|
| 295 |
var gut_id = -1 |
| 296 |
if(_gut != null): |
| 297 |
gut_id = _gut.get_instance_id() |
| 298 |
|
| 299 |
var values = { |
| 300 |
"path":path, |
| 301 |
"subpath":obj_info.get_subpath(), |
| 302 |
"stubber_id":stubber_id, |
| 303 |
"spy_id":spy_id, |
| 304 |
"extends":obj_info.get_extends_text(), |
| 305 |
"gut_id":gut_id |
| 306 |
} |
| 307 |
|
| 308 |
return _base_script_text.format(values) |
| 309 |
|
| 310 |
func _write_file(obj_info, dest_path, override_path=null): |
| 311 |
var base_script = _get_base_script_text(obj_info, override_path) |
| 312 |
var script_methods = _get_methods(obj_info) |
| 313 |
|
| 314 |
var f = FileOrString.new() |
| 315 |
f._do_file = _make_files |
| 316 |
var f_result = f.open(dest_path, f.WRITE) |
| 317 |
|
| 318 |
if(f_result != OK): |
| 319 |
_lgr.error(str('Error creating file ', dest_path)) |
| 320 |
_lgr.error(str('Could not create double for :', obj_info.to_s())) |
| 321 |
return |
| 322 |
|
| 323 |
f.store_string(base_script) |
| 324 |
|
| 325 |
for i in range(script_methods.local_methods.size()): |
| 326 |
if(obj_info.make_partial_double): |
| 327 |
_stub_to_call_super(obj_info, script_methods.local_methods[i].name) |
| 328 |
f.store_string(_get_func_text(script_methods.local_methods[i])) |
| 329 |
|
| 330 |
for i in range(script_methods.built_ins.size()): |
| 331 |
_stub_to_call_super(obj_info, script_methods.built_ins[i].name) |
| 332 |
f.store_string(_get_func_text(script_methods.built_ins[i])) |
| 333 |
|
| 334 |
f.close() |
| 335 |
return f |
| 336 |
|
| 337 |
func _double_scene_and_script(scene_info): |
| 338 |
var to_return = PackedSceneDouble.new() |
| 339 |
to_return.load_scene(scene_info.get_path()) |
| 340 |
|
| 341 |
var inst = load(scene_info.get_path()).instance() |
| 342 |
var script_path = null |
| 343 |
if(inst.get_script()): |
| 344 |
script_path = inst.get_script().get_path() |
| 345 |
inst.free() |
| 346 |
|
| 347 |
if(script_path): |
| 348 |
var oi = ObjectInfo.new(script_path) |
| 349 |
oi.set_method_strategy(scene_info.get_method_strategy()) |
| 350 |
oi.make_partial_double = scene_info.make_partial_double |
| 351 |
oi.scene_path = scene_info.get_path() |
| 352 |
to_return.set_script_obj(_double(oi, scene_info.get_path()).load_it()) |
| 353 |
|
| 354 |
return to_return |
| 355 |
|
| 356 |
func _get_methods(object_info): |
| 357 |
var obj = object_info.instantiate() |
| 358 |
# any method in the script or super script |
| 359 |
var script_methods = ScriptMethods.new() |
| 360 |
var methods = obj.get_method_list() |
| 361 |
if(!(obj is Reference)): |
| 362 |
obj.free() |
| 363 |
|
| 364 |
# first pass is for local methods only |
| 365 |
for i in range(methods.size()): |
| 366 |
# 65 is a magic number for methods in script, though documentation |
| 367 |
# says 64. This picks up local overloads of base class methods too. |
| 368 |
if(methods[i].flags == 65 and !_ignored_methods.has(object_info.get_path(), methods[i]['name'])): |
| 369 |
script_methods.add_local_method(methods[i]) |
| 370 |
|
| 371 |
|
| 372 |
if(object_info.get_method_strategy() == _utils.DOUBLE_STRATEGY.FULL): |
| 373 |
# second pass is for anything not local |
| 374 |
for i in range(methods.size()): |
| 375 |
# 65 is a magic number for methods in script, though documentation |
| 376 |
# says 64. This picks up local overloads of base class methods too. |
| 377 |
if(methods[i].flags != 65 and !_ignored_methods.has(object_info.get_path(), methods[i]['name'])): |
| 378 |
script_methods.add_built_in_method(methods[i]) |
| 379 |
|
| 380 |
return script_methods |
| 381 |
|
| 382 |
func _get_inst_id_ref_str(inst): |
| 383 |
var ref_str = 'null' |
| 384 |
if(inst): |
| 385 |
ref_str = str('instance_from_id(', inst.get_instance_id(),')') |
| 386 |
return ref_str |
| 387 |
|
| 388 |
func _get_func_text(method_hash): |
| 389 |
return _method_maker.get_function_text(method_hash) + "\n" |
| 390 |
|
| 391 |
# returns the path to write the double file to |
| 392 |
func _get_temp_path(object_info): |
| 393 |
var file_name = null |
| 394 |
var extension = null |
| 395 |
if(object_info.is_native()): |
| 396 |
file_name = object_info.get_native_class_name() |
| 397 |
extension = 'gd' |
| 398 |
else: |
| 399 |
file_name = object_info.get_path().get_file().get_basename() |
| 400 |
extension = object_info.get_path().get_extension() |
| 401 |
|
| 402 |
if(object_info.has_subpath()): |
| 403 |
file_name += '__' + object_info.get_subpath().replace('/', '__') |
| 404 |
|
| 405 |
file_name += str('__dbl', _double_count, '__.', extension) |
| 406 |
|
| 407 |
var to_return = _output_dir.plus_file(file_name) |
| 408 |
return to_return |
| 409 |
|
| 410 |
func _load_double(fileOrString): |
| 411 |
return fileOrString.load_it() |
| 412 |
|
| 413 |
func _double(obj_info, override_path=null): |
| 414 |
var temp_path = _get_temp_path(obj_info) |
| 415 |
var result = _write_file(obj_info, temp_path, override_path) |
| 416 |
_double_count += 1 |
| 417 |
return result |
| 418 |
|
| 419 |
func _double_script(path, make_partial, strategy): |
| 420 |
var oi = ObjectInfo.new(path) |
| 421 |
oi.make_partial_double = make_partial |
| 422 |
oi.set_method_strategy(strategy) |
| 423 |
return _double(oi).load_it() |
| 424 |
|
| 425 |
func _double_inner(path, subpath, make_partial, strategy): |
| 426 |
var oi = ObjectInfo.new(path, subpath) |
| 427 |
oi.set_method_strategy(strategy) |
| 428 |
oi.make_partial_double = make_partial |
| 429 |
return _double(oi).load_it() |
| 430 |
|
| 431 |
func _double_scene(path, make_partial, strategy): |
| 432 |
var oi = ObjectInfo.new(path) |
| 433 |
oi.set_method_strategy(strategy) |
| 434 |
oi.make_partial_double = make_partial |
| 435 |
return _double_scene_and_script(oi) |
| 436 |
|
| 437 |
func _double_gdnative(native_class, make_partial, strategy): |
| 438 |
var oi = ObjectInfo.new(null) |
| 439 |
oi.set_native_class(native_class) |
| 440 |
oi.set_method_strategy(strategy) |
| 441 |
oi.make_partial_double = make_partial |
| 442 |
return _double(oi).load_it() |
| 443 |
|
| 444 |
# ############### |
| 445 |
# Public |
| 446 |
# ############### |
| 447 |
func get_output_dir(): |
| 448 |
return _output_dir |
| 449 |
|
| 450 |
func set_output_dir(output_dir): |
| 451 |
if(output_dir != null): |
| 452 |
_output_dir = output_dir |
| 453 |
if(_make_files): |
| 454 |
var d = Directory.new() |
| 455 |
d.make_dir_recursive(output_dir) |
| 456 |
|
| 457 |
func get_spy(): |
| 458 |
return _spy |
| 459 |
|
| 460 |
func set_spy(spy): |
| 461 |
_spy = spy |
| 462 |
|
| 463 |
func get_stubber(): |
| 464 |
return _stubber |
| 465 |
|
| 466 |
func set_stubber(stubber): |
| 467 |
_stubber = stubber |
| 468 |
|
| 469 |
func get_logger(): |
| 470 |
return _lgr |
| 471 |
|
| 472 |
func set_logger(logger): |
| 473 |
_lgr = logger |
| 474 |
_method_maker.set_logger(logger) |
| 475 |
|
| 476 |
func get_strategy(): |
| 477 |
return _strategy |
| 478 |
|
| 479 |
func set_strategy(strategy): |
| 480 |
_strategy = strategy |
| 481 |
|
| 482 |
func get_gut(): |
| 483 |
return _gut |
| 484 |
|
| 485 |
func set_gut(gut): |
| 486 |
_gut = gut |
| 487 |
|
| 488 |
func partial_double_scene(path, strategy=_strategy): |
| 489 |
return _double_scene(path, true, strategy) |
| 490 |
|
| 491 |
# double a scene |
| 492 |
func double_scene(path, strategy=_strategy): |
| 493 |
return _double_scene(path, false, strategy) |
| 494 |
|
| 495 |
# double a script/object |
| 496 |
func double(path, strategy=_strategy): |
| 497 |
return _double_script(path, false, strategy) |
| 498 |
|
| 499 |
func partial_double(path, strategy=_strategy): |
| 500 |
return _double_script(path, true, strategy) |
| 501 |
|
| 502 |
func partial_double_inner(path, subpath, strategy=_strategy): |
| 503 |
return _double_inner(path, subpath, true, strategy) |
| 504 |
|
| 505 |
# double an inner class in a script |
| 506 |
func double_inner(path, subpath, strategy=_strategy): |
| 507 |
return _double_inner(path, subpath, false, strategy) |
| 508 |
|
| 509 |
# must always use FULL strategy since this is a native class and you won't get |
| 510 |
# any methods if you don't use FULL |
| 511 |
func double_gdnative(native_class): |
| 512 |
return _double_gdnative(native_class, false, _utils.DOUBLE_STRATEGY.FULL) |
| 513 |
|
| 514 |
# must always use FULL strategy since this is a native class and you won't get |
| 515 |
# any methods if you don't use FULL |
| 516 |
func partial_double_gdnative(native_class): |
| 517 |
return _double_gdnative(native_class, true, _utils.DOUBLE_STRATEGY.FULL) |
| 518 |
|
| 519 |
func clear_output_directory(): |
| 520 |
if(!_make_files): |
| 521 |
return false |
| 522 |
|
| 523 |
var did = false |
| 524 |
if(_output_dir.find('user://') == 0): |
| 525 |
var d = Directory.new() |
| 526 |
var result = d.open(_output_dir) |
| 527 |
# BIG GOTCHA HERE. If it cannot open the dir w/ erro 31, then the |
| 528 |
# directory becomes res:// and things go on normally and gut clears out |
| 529 |
# out res:// which is SUPER BAD. |
| 530 |
if(result == OK): |
| 531 |
d.list_dir_begin(true) |
| 532 |
var f = d.get_next() |
| 533 |
while(f != ''): |
| 534 |
d.remove(f) |
| 535 |
f = d.get_next() |
| 536 |
did = true |
| 537 |
return did |
| 538 |
|
| 539 |
func delete_output_directory(): |
| 540 |
var did = clear_output_directory() |
| 541 |
if(did): |
| 542 |
var d = Directory.new() |
| 543 |
d.remove(_output_dir) |
| 544 |
|
| 545 |
func add_ignored_method(path, method_name): |
| 546 |
_ignored_methods.add(path, method_name) |
| 547 |
|
| 548 |
func get_ignored_methods(): |
| 549 |
return _ignored_methods |
| 550 |
|
| 551 |
func get_make_files(): |
| 552 |
return _make_files |
| 553 |
|
| 554 |
func set_make_files(make_files): |
| 555 |
_make_files = make_files |
| 556 |
set_output_dir(_output_dir) |