43 lines
1.1 KiB
GDScript
43 lines
1.1 KiB
GDScript
class_name ResourceStateRecord
|
|
extends RefCounted
|
|
|
|
const SCHEMA_VERSION := 1
|
|
|
|
var data: Dictionary
|
|
|
|
func _init(record_data: Dictionary = {}) -> void:
|
|
data = record_data.duplicate(true)
|
|
|
|
static func capture(node: ResourceNode) -> ResourceStateRecord:
|
|
return ResourceStateRecord.new({
|
|
"schema_version": SCHEMA_VERSION,
|
|
"node_id": String(node.node_id),
|
|
"amount_remaining": node.amount_remaining,
|
|
"reserved_by": node.reserved_by,
|
|
"enabled": node.enabled
|
|
})
|
|
|
|
static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
|
|
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
|
|
return null
|
|
if not record_data.has_all([
|
|
"node_id", "amount_remaining", "reserved_by", "enabled"
|
|
]):
|
|
return null
|
|
if String(record_data["node_id"]).is_empty():
|
|
return null
|
|
return ResourceStateRecord.new(record_data)
|
|
|
|
func restore(node: ResourceNode) -> bool:
|
|
if node == null or node.node_id != StringName(data["node_id"]):
|
|
return false
|
|
node.restore_persistent_state(
|
|
float(data["amount_remaining"]),
|
|
int(data["reserved_by"]),
|
|
bool(data["enabled"])
|
|
)
|
|
return true
|
|
|
|
func to_dictionary() -> Dictionary:
|
|
return data.duplicate(true)
|