@tool class_name MeshColliderSettings extends RefCounted ## Configuration for Mesh Collider. ## ## Everything lives in `project.godot` under `mesh_collider/*`, so it is ## per-project and shows up in **Project Settings → Mesh Collider**. const ROOT := "mesh_collider/" ## Names mirrored from `MeshCollider`'s enums, kept here as plain data. ## ## The settings script deliberately does NOT reference the `MeshCollider` class: ## annotating a return type as `MeshCollider.ShapeType` made GDScript fail to ## parse the plugin with "Could not resolve external class member" when the ## settings script was preloaded. Keeping the dependency one-way (plugin -> ## settings, plugin -> core, settings -> nothing) removes that whole class of ## resolution-order problem. ## ## KEEP IN STEP with the enum order in mesh_collider.gd. const SHAPE_COUNT := 6 const BODY_COUNT := 4 const SHAPE_NAMES: PackedStringArray = [ "convex_hull", "trimesh", "box", "sphere", "capsule", "cylinder", ] const DEFAULT_MODEL_EXTENSIONS: PackedStringArray = [ "fbx", "glb", "gltf", "dae", "obj", "blend", "md2", "md3", "vrm", ] const KEY_ASK := ROOT + "ask_before_adding" const KEY_AUTO := ROOT + "auto_add_collision" const KEY_SHAPE := ROOT + "collision_shape" const KEY_BODY := ROOT + "collision_body" const KEY_SKIP_EXISTING := ROOT + "skip_meshes_with_collision" const KEY_LAYER := ROOT + "collision_layer" const KEY_MASK := ROOT + "collision_mask" const KEY_EXTENSIONS := ROOT + "model_extensions" ## The shape this plugin shipped as its default before 2.0 finalised. ## ## `install()` only writes a default when the setting does not exist yet, so a ## project that already ran an earlier build keeps the old value forever - which ## is exactly what happened when this default moved from Convex Hull to Trimesh. ## A stored value still equal to this legacy default is treated as "untouched" ## and migrated. const LEGACY_SHAPE_DEFAULT := 0 # MeshCollider.ShapeType.CONVEX_HULL ## NOTE: a function rather than a `const` dictionary on purpose. GDScript ## requires a const dictionary's *values* to be constant expressions, so a ## PackedStringArray cannot be built inside one ("isn't a constant expression"). ## ## The enum-backed defaults below are written as literal indices for the same ## reason applied one level out: they must not depend on another script's enum ## resolving. Keep them in step with MeshCollider's enum order. ## ## `hint_string` is the one piece of text an entry carries, and its meaning ## depends on the type: an enum uses it for its option list, an array for its ## element type, and a bool for a short description. Only the first two are ## visible - Project Settings renders a bool as a bare checkbox labelled 启用 and ## has no slot for a per-setting description, so what each setting does is ## documented in README.md instead. static func project_info() -> Dictionary: return { KEY_ASK: { "name": KEY_ASK, "type": TYPE_BOOL, "default": true, "hint_string": "Ask before adding collision to a dragged-in model", }, KEY_AUTO: { "name": KEY_AUTO, "type": TYPE_BOOL, "default": false, "hint_string": "Add collision to every pending model found by a scan", }, KEY_SHAPE: { "name": KEY_SHAPE, "type": TYPE_INT, "default": 1, # MeshCollider.ShapeType.TRIMESH "hint": PROPERTY_HINT_ENUM, "hint_string": "Convex Hull,Trimesh,Box,Sphere,Capsule,Cylinder", }, KEY_BODY: { "name": KEY_BODY, "type": TYPE_INT, "default": 0, # MeshCollider.BodyType.STATIC "hint": PROPERTY_HINT_ENUM, "hint_string": "StaticBody3D,RigidBody3D,Area3D,CharacterBody3D", }, KEY_SKIP_EXISTING: { "name": KEY_SKIP_EXISTING, "type": TYPE_BOOL, "default": false, "hint_string": "Skip meshes already under a physics body", }, KEY_LAYER: { "name": KEY_LAYER, "type": TYPE_INT, "default": 1, "hint": PROPERTY_HINT_LAYERS_3D_PHYSICS, }, KEY_MASK: { "name": KEY_MASK, "type": TYPE_INT, "default": 1, "hint": PROPERTY_HINT_LAYERS_3D_PHYSICS, }, KEY_EXTENSIONS: { "name": KEY_EXTENSIONS, "type": TYPE_PACKED_STRING_ARRAY, "default": PackedStringArray([ "fbx", "glb", "gltf", "dae", "obj", "blend", "md2", "md3", "vrm", ]), "hint": PROPERTY_HINT_ARRAY_TYPE, "hint_string": "String", }, } ## Cached copy of `project_info()`. The dictionary is rebuilt from scratch on ## every call, and the getters below are called on every scan, so keeping one ## instance avoids pointless allocation. static var _cached_info: Dictionary = {} static func _all_info() -> Dictionary: if _cached_info.is_empty(): _cached_info = project_info() return _cached_info static func _info(key: String) -> Dictionary: var all := _all_info() if all.has(key): return all[key] return {"name": key, "type": TYPE_NIL, "default": null} ## Register every setting with ProjectSettings, so the values appear in the UI ## and carry a usable default even before the user touches anything. ## ## Existing values are never overwritten, with one narrow exception: the shape ## default this plugin changed after shipping, migrated below. static func install() -> void: var all := _all_info() for key in all: var info: Dictionary = all[key] if not ProjectSettings.has_setting(key): ProjectSettings.set_setting(key, info["default"]) ProjectSettings.add_property_info(info) # Record the default as the initial value, so an untouched setting is not # reported as a modified project setting. ProjectSettings.set_initial_value(key, info["default"]) # `install()` only writes a default when the setting is absent, so a project # that already ran an earlier build would keep the old default forever. Only # a value still equal to that old default is treated as untouched and # migrated; a genuinely chosen value is left alone. var stored := int(ProjectSettings.get_setting(KEY_SHAPE, all[KEY_SHAPE]["default"])) var wanted := int(all[KEY_SHAPE]["default"]) if stored != LEGACY_SHAPE_DEFAULT or wanted == LEGACY_SHAPE_DEFAULT: return ProjectSettings.set_setting(KEY_SHAPE, wanted) print(("[MeshCollider] collision_shape was still the old default " + "(Convex Hull) - updated to '%s'. Set it back in Project Settings " + "> mesh_collider if that was intentional.") % SHAPE_NAMES[clampi(wanted, 0, SHAPE_NAMES.size() - 1)]) # Only the migration is persisted. Writing on every install would spill all of # the plugin's defaults into project.godot for no benefit. var err := ProjectSettings.save() if err != OK: push_warning("[MeshCollider] could not save project settings (err %d)" % err) static func get_bool(key: String) -> bool: return bool(ProjectSettings.get_setting(key, _info(key)["default"])) static func get_int(key: String) -> int: return int(ProjectSettings.get_setting(key, _info(key)["default"])) static func get_strings(key: String) -> PackedStringArray: var value = ProjectSettings.get_setting(key, _info(key)["default"]) if value is PackedStringArray: return value var out := PackedStringArray() if value is Array: for v in value: out.append(String(v)) return out static func set_value(key: String, value) -> void: ProjectSettings.set_setting(key, value) ## The configured shape type, clamped to the valid enum range. ## ## Returns a plain int rather than `MeshCollider.ShapeType` on purpose; see ## SHAPE_NAMES. Callers use it as an enum value. static func shape_type() -> int: return clampi(get_int(KEY_SHAPE), 0, SHAPE_COUNT - 1) ## The configured body type, clamped to the valid enum range. static func body_type() -> int: return clampi(get_int(KEY_BODY), 0, BODY_COUNT - 1) ## True when `path` looks like a model, according to the configured extensions. static func is_configured_model(path: String) -> bool: if path.is_empty(): return false var ext := path.get_extension().to_lower() if ext.is_empty(): return false var configured := get_strings(KEY_EXTENSIONS) if configured.is_empty(): configured = DEFAULT_MODEL_EXTENSIONS for e in configured: if String(e).to_lower().trim_prefix(".") == ext: return true return false