更新自研插件

This commit is contained in:
2026-10-04 17:19:42 +08:00
parent dfd8636476
commit b99952d3c9
4 changed files with 189 additions and 30 deletions
@@ -18,6 +18,15 @@ const KEY_MASK := ROOT + "collision_mask"
const KEY_EXTENSIONS := ROOT + "model_extensions"
const KEY_SKIP_DIRS := ROOT + "skip_dirs"
## 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").
@@ -36,7 +45,7 @@ static func project_info() -> Dictionary:
KEY_SHAPE: {
"name": KEY_SHAPE,
"type": TYPE_INT,
"default": 0, # MeshCollider.ShapeType.CONVEX_HULL
"default": 1, # MeshCollider.ShapeType.TRIMESH
"hint": PROPERTY_HINT_ENUM,
"hint_string": "Convex Hull,Trimesh,Box,Sphere,Capsule,Cylinder",
},
@@ -106,6 +115,9 @@ static func _info(key: String) -> Dictionary:
## 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:
@@ -117,6 +129,26 @@ static func install() -> void:
# 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.")
% MeshCollider.SHAPE_TYPE_NAMES[wanted])
# 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"]))