Перейти к содержимому

Typed addon state без выдумывания новых save types

✅ Source verified 🧱 API layering pattern

Public API может дать typed convenience, не требуя от underlying game save container отдельного native representation для каждого C# type.

Political World показывает это на bool и float.

Public contract:

GetKingdomBool(...)
SetKingdomBool(...)

Implementation:

false = 0
true = 1

Используется уже существующий collision-safe integer data path.

Public contract:

GetKingdomFloat(...)
SetKingdomFloat(...)

Implementation:

float
→ invariant round-trip text
→ addon-private string storage

Read:

float.TryParse(
value,
NumberStyles.Float,
CultureInfo.InvariantCulture,
out parsed
)

Write:

value.ToString("R", CultureInfo.InvariantCulture)

Без explicit culture float может сериализоваться по-разному в зависимости от locale:

1.25
1,25

Save format не должен менять синтаксис только потому, что пользователь переключил language.

Round-trip format нужен, чтобы parsed value можно было восстановить без лишней потери precision.

Addon видит:

bool enabled
float taxRate

Persistence layer:

int
string

Это хорошее разделение.

Internal representation потом можно изменить, сохранив typed public contract.

Если написать:

SetKingdomString(..., "tax_rate", someFloat.ToString())

format/culture уже становятся вашей проблемой.

Если framework даёт:

SetKingdomFloat(...)

лучше использовать его.

Stable public API должен предоставлять semantic types, даже если storage построен на меньшем наборе primitive representations.