diff --git a/addons/launchy/plugin.gd b/addons/launchy/plugin.gd index 12d5729..4371c0b 100644 --- a/addons/launchy/plugin.gd +++ b/addons/launchy/plugin.gd @@ -9,6 +9,7 @@ var interface = get_editor_interface() var editor_settings = interface.get_editor_settings() var current_object #currently selected object in the editor var settings = editor_settings.get_setting('editors/external/associations') #Dict +var custom_type_cache: Dictionary = {} const MENU_LABEL = "Launchy: Edit Associations..." @@ -70,7 +71,7 @@ func handles(object): #When choosing the application to launch, we should probably #go through the entire set of keys to check for if an exact class is found. for type in settings.keys(): - if object.is_class(type): + if object.is_class(type) or get_object_type(object) == type: return true return false @@ -81,8 +82,8 @@ func make_visible(visible): if visible == true: if current_object is Resource: b.res = current_object #Set the launch exe to the most specific class we can get. - if settings.has(current_object.get_class()): - b.exe = settings[current_object.get_class()] + if settings.has(get_object_type(current_object)): + b.exe = settings[get_object_type(current_object)] else: #Exact match not found. Search for a base class. #TODO: Maybe walk the inheretance tree recursively up to find the # subclass which matches the closest to current_object type @@ -90,6 +91,32 @@ func make_visible(visible): if current_object.is_class(type): b.exe = settings[type] +func get_object_type(object: Object): + + var script: Script = object.get_script() + if script != null: + + # Check cache for this object type + if script.resource_path in custom_type_cache: + return custom_type_cache[script.resource_path] + + if script is GDScript: + + # Parse source code to find class_name + for line in script.source_code.split("\n"): + if line.begins_with("class_name "): + custom_type_cache[script.resource_path] = line.trim_prefix("class_name ").strip_edges() + return custom_type_cache[script.resource_path] + + # If script has no class_name, return file name instead + var name: String = script.resource_path.get_basename().get_file() + if name in settings: + custom_type_cache[script.resource_path] = name + return name + + else: + return object.get_class() + func launchConfigPopup(param=null): if param is bool: c.popup_exclusive = param