Class AchievementConditionStateCodec
Packs a node's own counter and its children's captured states into the single string an achievement's progress row carries, unpacks it again, and reads the numbers back out of it.
Inherited Members
Namespace: Serenity.Achievements.Application.Conditions
Assembly: Serenity.Achievements.Application.dll
Syntax
public static class AchievementConditionStateCodec
Remarks
A child's state is itself a packed string when that child has children of its own, so the parts have to be escaped rather than merely joined — otherwise a two-level tree's separators would be indistinguishable from its grandchildren's. Backslash escaping keeps the format flat: one pass to write, one to read, at any depth.
Everything read here arrives from a store, which means it may have been hand-edited, written by an older build, or corrupted. Nothing throws and nothing is trusted: an input past MaxPackedStateLength, a truncated escape, a missing part, or a value that is negative or not finite all leave the node at its default rather than at a nonsense count.
Fields
MaxPackedStateLength
The longest packed state that is read at all. A legitimate tree packs a handful of numbers per node against a depth cap of MaxDepth, so anything approaching this is corrupt rather than deep, and unpacking it would only spend memory to arrive at the same defaults.
Declaration
public const int MaxPackedStateLength = 8192
Field Value
| Type | Description |
|---|---|
| int |
Methods
OwnState(IReadOnlyList<string>)
The node's own state within a packed string, or null when there is none.
Declaration
public static string OwnState(IReadOnlyList<string> parts)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<string> | parts | The unpacked parts, own state first. |
Returns
| Type | Description |
|---|---|
| string |
Pack(string, IReadOnlyList<AchievementCondition>)
Packs a node's own state and its children's captured states into one string.
Declaration
public static string Pack(string ownState, IReadOnlyList<AchievementCondition> children)
Parameters
| Type | Name | Description |
|---|---|---|
| string | ownState | The node's own counter, or null when it keeps none. |
| IReadOnlyList<AchievementCondition> | children | The children to capture, in authored order. May be null. |
Returns
| Type | Description |
|---|---|
| string | The packed state. |
RestoreChildren(IReadOnlyList<string>, IReadOnlyList<AchievementCondition>)
Hands each child the part of a packed state that belongs to it.
Declaration
public static void RestoreChildren(IReadOnlyList<string> parts, IReadOnlyList<AchievementCondition> children)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<string> | parts | The unpacked parts, own state first. |
| IReadOnlyList<AchievementCondition> | children | The children to restore, in the order they were packed. May be null. |
Remarks
A part count that does not match the child count is normal rather than exceptional: an author can add or remove a child between two runs. Missing parts leave those children at their defaults, and extra parts are ignored.
TryReadAmount(string, out float)
Reads a stored amount, refusing anything negative or not finite. Infinity and NaN both parse successfully on this runtime, and either one would produce a duration that can never complete or one that is already complete.
Declaration
public static bool TryReadAmount(string state, out float amount)
Parameters
| Type | Name | Description |
|---|---|---|
| string | state | The stored value. |
| float | amount | The amount read, or zero when the value is unusable. |
Returns
| Type | Description |
|---|---|
| bool | True when a usable amount was read. |
TryReadCount(string, out int)
Reads a stored tally, refusing anything that is not a whole count of zero or more.
Declaration
public static bool TryReadCount(string state, out int count)
Parameters
| Type | Name | Description |
|---|---|---|
| string | state | The stored value. |
| int | count | The tally read, or zero when the value is unusable. |
Returns
| Type | Description |
|---|---|
| bool | True when a usable tally was read. |
Unpack(string)
Unpacks a string produced by Pack(string, IReadOnlyList<AchievementCondition>).
Declaration
public static IReadOnlyList<string> Unpack(string state)
Parameters
| Type | Name | Description |
|---|---|---|
| string | state | The packed state. Null, or longer than MaxPackedStateLength, yields no parts at all, which every caller reads as "keep the defaults". |
Returns
| Type | Description |
|---|---|
| IReadOnlyList<string> | The node's own state first, then one entry per child. |
WriteAmount(float)
Writes an amount in a form TryReadAmount(string, out float) reads back exactly.
Declaration
public static string WriteAmount(float amount)
Parameters
| Type | Name | Description |
|---|---|---|
| float | amount | The amount to write. |
Returns
| Type | Description |
|---|---|
| string |
WriteCount(int)
Writes a tally in a form TryReadCount(string, out int) reads back exactly.
Declaration
public static string WriteCount(int count)
Parameters
| Type | Name | Description |
|---|---|---|
| int | count | The tally to write. |
Returns
| Type | Description |
|---|---|
| string |