|
| 1 | +@tool |
| 2 | +class_name AutoloadHandler |
| 3 | +extends RefCounted |
| 4 | + |
| 5 | +## Handles autoload listing, adding, and removing via ProjectSettings. |
| 6 | + |
| 7 | + |
| 8 | +func list_autoloads(_params: Dictionary) -> Dictionary: |
| 9 | + var autoloads: Array[Dictionary] = [] |
| 10 | + for prop in ProjectSettings.get_property_list(): |
| 11 | + var key: String = prop.get("name", "") |
| 12 | + if not key.begins_with("autoload/"): |
| 13 | + continue |
| 14 | + var name := key.substr("autoload/".length()) |
| 15 | + var raw_value: String = ProjectSettings.get_setting(key, "") |
| 16 | + var is_singleton := raw_value.begins_with("*") |
| 17 | + var path := raw_value.substr(1) if is_singleton else raw_value |
| 18 | + autoloads.append({ |
| 19 | + "name": name, |
| 20 | + "path": path, |
| 21 | + "singleton": is_singleton, |
| 22 | + }) |
| 23 | + return {"data": {"autoloads": autoloads, "count": autoloads.size()}} |
| 24 | + |
| 25 | + |
| 26 | +func add_autoload(params: Dictionary) -> Dictionary: |
| 27 | + var name: String = params.get("name", "") |
| 28 | + var path: String = params.get("path", "") |
| 29 | + var singleton: bool = params.get("singleton", true) |
| 30 | + |
| 31 | + if name.is_empty(): |
| 32 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Missing required param: name") |
| 33 | + if path.is_empty(): |
| 34 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Missing required param: path") |
| 35 | + if not path.begins_with("res://"): |
| 36 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Path must start with res:// (got: %s)" % path) |
| 37 | + if not FileAccess.file_exists(path): |
| 38 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "File not found: %s" % path) |
| 39 | + |
| 40 | + var key := "autoload/%s" % name |
| 41 | + if ProjectSettings.has_setting(key): |
| 42 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Autoload '%s' already exists" % name) |
| 43 | + |
| 44 | + var value := ("*" if singleton else "") + path |
| 45 | + ProjectSettings.set_setting(key, value) |
| 46 | + ProjectSettings.set_initial_value(key, "") |
| 47 | + ProjectSettings.set_as_basic(key, true) |
| 48 | + var err := ProjectSettings.save() |
| 49 | + if err != OK: |
| 50 | + ProjectSettings.clear(key) |
| 51 | + return McpErrorCodes.make(McpErrorCodes.INTERNAL_ERROR, "Failed to save project settings (error %d)" % err) |
| 52 | + |
| 53 | + return { |
| 54 | + "data": { |
| 55 | + "name": name, |
| 56 | + "path": path, |
| 57 | + "singleton": singleton, |
| 58 | + "undoable": false, |
| 59 | + "reason": "Autoload changes are saved to project.godot", |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | +func remove_autoload(params: Dictionary) -> Dictionary: |
| 65 | + var name: String = params.get("name", "") |
| 66 | + if name.is_empty(): |
| 67 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Missing required param: name") |
| 68 | + |
| 69 | + var key := "autoload/%s" % name |
| 70 | + if not ProjectSettings.has_setting(key): |
| 71 | + return McpErrorCodes.make(McpErrorCodes.INVALID_PARAMS, "Autoload '%s' not found" % name) |
| 72 | + |
| 73 | + var old_value: String = ProjectSettings.get_setting(key, "") |
| 74 | + ProjectSettings.clear(key) |
| 75 | + var err := ProjectSettings.save() |
| 76 | + if err != OK: |
| 77 | + ProjectSettings.set_setting(key, old_value) |
| 78 | + return McpErrorCodes.make(McpErrorCodes.INTERNAL_ERROR, "Failed to save project settings (error %d)" % err) |
| 79 | + |
| 80 | + return { |
| 81 | + "data": { |
| 82 | + "name": name, |
| 83 | + "removed": true, |
| 84 | + "undoable": false, |
| 85 | + "reason": "Autoload changes are saved to project.godot", |
| 86 | + } |
| 87 | + } |
0 commit comments