fix: harden presentation event immutability

This commit is contained in:
goose
2026-10-04 00:56:22 -04:00
parent 0a9b14be26
commit 782fcabc08
3 changed files with 68 additions and 100 deletions
+48 -4
View File
@@ -4,10 +4,38 @@ extends RefCounted
## A display-only, immutable translation of one authoritative GameEvent.
## Getter calls return a fresh source snapshot so presentation code cannot mutate
## the source domain event or this adapter's captured event data.
var _source_event: GameEvent
var _kind: StringName
var _notification: String
var _accent: Color
##
## GDScript's leading underscore is only a convention: declared fields remain
## writable. The backing properties below are virtual so _set can absorb every
## write after construction.
const BACKING_PROPERTIES := [&"_source_event", &"_kind", &"_notification", &"_accent"]
var _backing_values: Dictionary[StringName, Variant] = {}
var _is_initialized := false
var _source_event: GameEvent:
set(value):
_set(&"_source_event", value)
get:
return _captured_source_snapshot()
var _kind: StringName:
set(value):
_set(&"_kind", value)
get:
return _backing_values.get(&"_kind", &"") as StringName
var _notification: String:
set(value):
_set(&"_notification", value)
get:
return _backing_values.get(&"_notification", "") as String
var _accent: Color:
set(value):
_set(&"_accent", value)
get:
return _backing_values.get(&"_accent", Color.WHITE) as Color
var source_event: GameEvent:
get:
@@ -30,6 +58,22 @@ func _init(source: GameEvent, presentation_kind: StringName, text: String = "",
_kind = presentation_kind
_notification = text if not text.is_empty() else source.message
_accent = color
_is_initialized = true
func _set(property: StringName, value: Variant) -> bool:
if property not in BACKING_PROPERTIES:
return false
if not _is_initialized:
_backing_values[property] = value
return true
func _get(property: StringName) -> Variant:
if property in BACKING_PROPERTIES:
return _backing_values.get(property)
return null
func _captured_source_snapshot() -> GameEvent:
return _copy_event(_backing_values.get(&"_source_event") as GameEvent)
static func _copy_event(event: GameEvent) -> GameEvent:
return GameEvent.new(