Merge canonical CultMath serialization and the Geometry vector collapse (P4)
Every public CultMath value type serializes: MessagePack in the default resolver chain (byte-identical to Aetheria's and StreamPixels' copies across 653 values and all 115 float fields of the real Aetheria.cc) and System.Text.Json converters (STJ-parity number handling, math types as dictionary keys). CultVec2/3 and CultRect are gone with bytes unchanged. The cultlib Unity package depends on cultmath; the release gate refuses until CultMath 0.3.0 carries every type. Three Soul passes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
69bd45ff14
21 changed files with 1625 additions and 300 deletions
|
|
@ -57,7 +57,7 @@ public static class CultDocumentMessagePackSerialization
|
|||
{
|
||||
return MessagePackSerializerOptions.Standard
|
||||
.WithResolver(CompositeResolver.Create(
|
||||
consumerResolvers.Append(CultDocumentResolver.Instance).Append(StandardResolver.Instance).ToArray()))
|
||||
consumerResolvers.Append(CultDocumentResolver.Instance).Append(CultMathResolver.Instance).Append(StandardResolver.Instance).ToArray()))
|
||||
.WithSecurity(CultMessagePackSecurity.Instance);
|
||||
}
|
||||
|
||||
|
|
|
|||
516
src/GameCult.Caching.MessagePack/CultMathJson.cs
Normal file
516
src/GameCult.Caching.MessagePack/CultMathJson.cs
Normal file
|
|
@ -0,0 +1,516 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using CultMath;
|
||||
using Random = CultMath.Random;
|
||||
|
||||
namespace GameCult.Caching.MessagePack;
|
||||
|
||||
/// <summary>
|
||||
/// The canonical System.Text.Json shape of every public CultMath value type, the JSON twin of
|
||||
/// <see cref="CultMathResolver"/>. Vectors, quaternions and colors are objects keyed by their
|
||||
/// field names (<c>{"x":1,"y":2}</c>), matrices are arrays of row objects, <c>rect</c> is
|
||||
/// <c>{"min":..,"max":..}</c>, and <c>CultCellular</c> is <c>{"nearest":..,"edge":..,"id":..}</c>.
|
||||
/// Property names are fixed: <c>PropertyNamingPolicy</c> does not rename them, and
|
||||
/// <c>PropertyNameCaseInsensitive</c> makes reading case-insensitive.
|
||||
/// <para>
|
||||
/// Reading skips unknown properties (<c>UnmappedMemberHandling.Disallow</c> refuses them) and
|
||||
/// leaves missing ones at zero (a missing <c>Color32.a</c> is 255). A repeated property replaces
|
||||
/// the whole earlier value, never merging component-wise, and is refused when
|
||||
/// <c>AllowDuplicateProperties</c> is false. A wrong token, a number outside its component type,
|
||||
/// or a matrix that is not an array of exactly its row count throws
|
||||
/// <see cref="JsonException"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Numbers follow <see cref="JsonSerializerOptions.NumberHandling"/> as System.Text.Json's own
|
||||
/// float does. NaN and the infinities are the strings <c>"NaN"</c>, <c>"Infinity"</c>,
|
||||
/// <c>"-Infinity"</c>: written when <c>AllowNamedFloatingPointLiterals</c> or
|
||||
/// <c>WriteAsString</c> is set (otherwise <see cref="JsonException"/>), read when
|
||||
/// <c>AllowNamedFloatingPointLiterals</c> or <c>AllowReadingFromString</c> is set.
|
||||
/// <c>AllowReadingFromString</c> reads quoted numbers without surrounding whitespace, and
|
||||
/// <c>WriteAsString</c> writes quoted numbers. Two intended divergences: a bare number beyond
|
||||
/// the component's range (<c>1e40</c> as a float) is refused where System.Text.Json reads
|
||||
/// infinity, and refusals on write are <see cref="JsonException"/> where it throws
|
||||
/// <see cref="ArgumentException"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Every type is also a dictionary key: the flattened components joined by commas in
|
||||
/// declaration order, invariant culture, shortest round-trip floats (<c>"1,2"</c>,
|
||||
/// <c>"true,false"</c>, matrices row by row, <c>rect</c> as <c>"minX,minY,maxX,maxY"</c>).
|
||||
/// Only that exact spelling reads back, so two JSON keys never collapse into one dictionary key.
|
||||
/// </para>
|
||||
/// Register with <see cref="AddCultMathConverters"/>; JSON options are the consumer's own.
|
||||
/// </summary>
|
||||
public static class CultMathJson
|
||||
{
|
||||
/// <summary>The CultMath types that have a converter, for coverage checks.</summary>
|
||||
public static IReadOnlyCollection<Type> ConvertedTypes => Converters.Keys;
|
||||
|
||||
/// <summary>Adds the CultMath converters to <paramref name="options"/> and returns it.</summary>
|
||||
public static JsonSerializerOptions AddCultMathConverters(this JsonSerializerOptions options)
|
||||
{
|
||||
if (options == null) throw new ArgumentNullException(nameof(options));
|
||||
foreach (var converter in Converters.Values) options.Converters.Add(converter);
|
||||
return options;
|
||||
}
|
||||
|
||||
private static bool LiteralsWrite(JsonSerializerOptions o) =>
|
||||
(o.NumberHandling & (JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.WriteAsString)) != 0;
|
||||
|
||||
private static bool LiteralsRead(JsonSerializerOptions o) =>
|
||||
(o.NumberHandling & (JsonNumberHandling.AllowNamedFloatingPointLiterals | JsonNumberHandling.AllowReadingFromString)) != 0;
|
||||
|
||||
private static bool FromString(JsonSerializerOptions o) => (o.NumberHandling & JsonNumberHandling.AllowReadingFromString) != 0;
|
||||
private static bool AsString(JsonSerializerOptions o) => (o.NumberHandling & JsonNumberHandling.WriteAsString) != 0;
|
||||
|
||||
// AllowDuplicateProperties arrived after the System.Text.Json this library compiles against.
|
||||
private static readonly PropertyInfo? AllowDuplicateProperties = typeof(JsonSerializerOptions).GetProperty("AllowDuplicateProperties");
|
||||
|
||||
private static bool DuplicatesAllowed(JsonSerializerOptions o) => AllowDuplicateProperties == null || (bool)AllowDuplicateProperties.GetValue(o)!;
|
||||
|
||||
private const NumberStyles FloatStyle = NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent;
|
||||
|
||||
// One component kind. Every CultMath type is a flat list of one kind, so a Scalar plus a Node tree describes it.
|
||||
private abstract class Scalar<S> where S : unmanaged
|
||||
{
|
||||
public abstract void Write(Utf8JsonWriter w, S value, JsonSerializerOptions o);
|
||||
public abstract S Read(ref Utf8JsonReader r, JsonSerializerOptions o);
|
||||
|
||||
/// <summary>The canonical key spelling, whatever the options allow.</summary>
|
||||
public abstract string Spelling(S value);
|
||||
|
||||
/// <summary>The key spelling, refusing what the options would refuse to write.</summary>
|
||||
public virtual string Text(S value, JsonSerializerOptions o) => Spelling(value);
|
||||
|
||||
public abstract S Parse(string text, JsonSerializerOptions o);
|
||||
|
||||
/// <summary>Reads a key component; keys are always text, so AllowReadingFromString does not apply.</summary>
|
||||
public virtual S ParseKey(string text, JsonSerializerOptions o) => Parse(text, o);
|
||||
}
|
||||
|
||||
private abstract class FloatingScalar<S> : Scalar<S> where S : unmanaged
|
||||
{
|
||||
protected static bool IsLiteral(string s) => s == "NaN" || s == "Infinity" || s == "-Infinity";
|
||||
protected abstract bool IsFinite(S value);
|
||||
protected abstract string Literal(S value);
|
||||
protected abstract S FromLiteral(string literal);
|
||||
protected abstract bool TryNumber(string text, out S value);
|
||||
protected abstract bool TryToken(ref Utf8JsonReader r, out S value);
|
||||
protected abstract void WriteNumber(Utf8JsonWriter w, S value);
|
||||
protected abstract string Number(S value);
|
||||
|
||||
public override void Write(Utf8JsonWriter w, S v, JsonSerializerOptions o)
|
||||
{
|
||||
if (!IsFinite(v))
|
||||
{
|
||||
if (!LiteralsWrite(o)) throw new JsonException($"A non-finite {typeof(S).Name} needs JsonNumberHandling.AllowNamedFloatingPointLiterals or WriteAsString.");
|
||||
w.WriteStringValue(Literal(v));
|
||||
}
|
||||
else if (AsString(o)) w.WriteStringValue(Number(v));
|
||||
else WriteNumber(w, v);
|
||||
}
|
||||
|
||||
public override S Read(ref Utf8JsonReader r, JsonSerializerOptions o)
|
||||
{
|
||||
if (r.TokenType == JsonTokenType.Number)
|
||||
{
|
||||
if (!TryToken(ref r, out var v) || !IsFinite(v)) throw new JsonException($"The number is outside the {typeof(S).Name} range.");
|
||||
return v;
|
||||
}
|
||||
|
||||
if (r.TokenType == JsonTokenType.String) return Parse(r.GetString()!, o);
|
||||
throw new JsonException($"Expected a number, found {r.TokenType}.");
|
||||
}
|
||||
|
||||
public override string Spelling(S v) => Number(v);
|
||||
|
||||
public override string Text(S v, JsonSerializerOptions o)
|
||||
{
|
||||
if (!IsFinite(v) && !LiteralsWrite(o)) throw new JsonException($"A non-finite {typeof(S).Name} needs JsonNumberHandling.AllowNamedFloatingPointLiterals or WriteAsString.");
|
||||
return Spelling(v);
|
||||
}
|
||||
|
||||
public override S Parse(string text, JsonSerializerOptions o)
|
||||
{
|
||||
if (!IsLiteral(text) && !FromString(o)) throw new JsonException($"Expected a number, found the string '{text}'.");
|
||||
return ParseKey(text, o);
|
||||
}
|
||||
|
||||
public override S ParseKey(string text, JsonSerializerOptions o)
|
||||
{
|
||||
if (IsLiteral(text))
|
||||
{
|
||||
if (!LiteralsRead(o)) throw new JsonException($"'{text}' needs JsonNumberHandling.AllowNamedFloatingPointLiterals or AllowReadingFromString.");
|
||||
return FromLiteral(text);
|
||||
}
|
||||
|
||||
if (TryNumber(text, out var v) && IsFinite(v)) return v;
|
||||
throw new JsonException($"A {typeof(S).Name} cannot be read from '{text}'.");
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FloatScalar : FloatingScalar<float>
|
||||
{
|
||||
public static readonly FloatScalar Instance = new();
|
||||
protected override bool IsFinite(float v) => float.IsFinite(v);
|
||||
protected override string Literal(float v) => float.IsNaN(v) ? "NaN" : v > 0f ? "Infinity" : "-Infinity";
|
||||
protected override float FromLiteral(string s) => s == "NaN" ? float.NaN : s == "Infinity" ? float.PositiveInfinity : float.NegativeInfinity;
|
||||
protected override bool TryNumber(string s, out float v) => float.TryParse(s, FloatStyle, CultureInfo.InvariantCulture, out v);
|
||||
protected override bool TryToken(ref Utf8JsonReader r, out float v) => r.TryGetSingle(out v);
|
||||
protected override void WriteNumber(Utf8JsonWriter w, float v) => w.WriteNumberValue(v);
|
||||
protected override string Number(float v) => v.ToString("R", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private sealed class DoubleScalar : FloatingScalar<double>
|
||||
{
|
||||
public static readonly DoubleScalar Instance = new();
|
||||
protected override bool IsFinite(double v) => double.IsFinite(v);
|
||||
protected override string Literal(double v) => double.IsNaN(v) ? "NaN" : v > 0d ? "Infinity" : "-Infinity";
|
||||
protected override double FromLiteral(string s) => s == "NaN" ? double.NaN : s == "Infinity" ? double.PositiveInfinity : double.NegativeInfinity;
|
||||
protected override bool TryNumber(string s, out double v) => double.TryParse(s, FloatStyle, CultureInfo.InvariantCulture, out v);
|
||||
protected override bool TryToken(ref Utf8JsonReader r, out double v) => r.TryGetDouble(out v);
|
||||
protected override void WriteNumber(Utf8JsonWriter w, double v) => w.WriteNumberValue(v);
|
||||
protected override string Number(double v) => v.ToString("R", CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private abstract class IntegerScalar<S> : Scalar<S> where S : unmanaged
|
||||
{
|
||||
protected abstract bool TryToken(ref Utf8JsonReader r, out S value);
|
||||
protected abstract bool TryText(string text, out S value);
|
||||
protected abstract void WriteNumber(Utf8JsonWriter w, S value);
|
||||
|
||||
public override void Write(Utf8JsonWriter w, S v, JsonSerializerOptions o)
|
||||
{
|
||||
if (AsString(o)) w.WriteStringValue(Spelling(v)); else WriteNumber(w, v);
|
||||
}
|
||||
|
||||
public override S Read(ref Utf8JsonReader r, JsonSerializerOptions o)
|
||||
{
|
||||
if (r.TokenType == JsonTokenType.Number)
|
||||
{
|
||||
if (!TryToken(ref r, out var v)) throw new JsonException($"The number is not a {typeof(S).Name}.");
|
||||
return v;
|
||||
}
|
||||
|
||||
if (r.TokenType == JsonTokenType.String && FromString(o)) return Parse(r.GetString()!, o);
|
||||
throw new JsonException($"Expected an integer, found {r.TokenType}.");
|
||||
}
|
||||
|
||||
public override S Parse(string text, JsonSerializerOptions o) =>
|
||||
TryText(text, out var v) ? v : throw new JsonException($"A {typeof(S).Name} cannot be read from '{text}'.");
|
||||
}
|
||||
|
||||
private sealed class IntScalar : IntegerScalar<int>
|
||||
{
|
||||
public static readonly IntScalar Instance = new();
|
||||
protected override bool TryToken(ref Utf8JsonReader r, out int v) => r.TryGetInt32(out v);
|
||||
protected override bool TryText(string s, out int v) => int.TryParse(s, NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture, out v);
|
||||
protected override void WriteNumber(Utf8JsonWriter w, int v) => w.WriteNumberValue(v);
|
||||
public override string Spelling(int v) => v.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private sealed class UIntScalar : IntegerScalar<uint>
|
||||
{
|
||||
public static readonly UIntScalar Instance = new();
|
||||
protected override bool TryToken(ref Utf8JsonReader r, out uint v) => r.TryGetUInt32(out v);
|
||||
protected override bool TryText(string s, out uint v) => uint.TryParse(s, NumberStyles.None, CultureInfo.InvariantCulture, out v);
|
||||
protected override void WriteNumber(Utf8JsonWriter w, uint v) => w.WriteNumberValue(v);
|
||||
public override string Spelling(uint v) => v.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private sealed class ByteScalar : IntegerScalar<byte>
|
||||
{
|
||||
public static readonly ByteScalar Instance = new();
|
||||
protected override bool TryToken(ref Utf8JsonReader r, out byte v) => r.TryGetByte(out v);
|
||||
protected override bool TryText(string s, out byte v) => byte.TryParse(s, NumberStyles.None, CultureInfo.InvariantCulture, out v);
|
||||
protected override void WriteNumber(Utf8JsonWriter w, byte v) => w.WriteNumberValue(v);
|
||||
public override string Spelling(byte v) => v.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private sealed class BoolScalar : Scalar<bool>
|
||||
{
|
||||
public static readonly BoolScalar Instance = new();
|
||||
|
||||
public override void Write(Utf8JsonWriter w, bool v, JsonSerializerOptions o) => w.WriteBooleanValue(v);
|
||||
|
||||
public override bool Read(ref Utf8JsonReader r, JsonSerializerOptions o) =>
|
||||
r.TokenType == JsonTokenType.True ? true
|
||||
: r.TokenType == JsonTokenType.False ? false
|
||||
: throw new JsonException($"Expected true or false, found {r.TokenType}.");
|
||||
|
||||
public override string Spelling(bool v) => v ? "true" : "false";
|
||||
|
||||
public override bool Parse(string text, JsonSerializerOptions o) =>
|
||||
text == "true" ? true : text == "false" ? false : throw new JsonException($"A bool cannot be read from '{text}'.");
|
||||
}
|
||||
|
||||
// The JSON structure over a flat component list: leaves consume one component each, in order.
|
||||
// Read fills `flat` (pre-loaded with `defaults`), so a missing property keeps its default.
|
||||
private abstract class Node<S> where S : unmanaged
|
||||
{
|
||||
public int Count { get; protected set; }
|
||||
public abstract void Write(Utf8JsonWriter w, ReadOnlySpan<S> flat, Scalar<S> scalar, JsonSerializerOptions o);
|
||||
public abstract void Read(ref Utf8JsonReader r, scoped Span<S> flat, scoped ReadOnlySpan<S> defaults, Scalar<S> scalar, JsonSerializerOptions o);
|
||||
}
|
||||
|
||||
private sealed class Leaf<S> : Node<S> where S : unmanaged
|
||||
{
|
||||
public Leaf() { Count = 1; }
|
||||
|
||||
public override void Write(Utf8JsonWriter w, ReadOnlySpan<S> flat, Scalar<S> scalar, JsonSerializerOptions o) => scalar.Write(w, flat[0], o);
|
||||
|
||||
public override void Read(ref Utf8JsonReader r, scoped Span<S> flat, scoped ReadOnlySpan<S> defaults, Scalar<S> scalar, JsonSerializerOptions o) => flat[0] = scalar.Read(ref r, o);
|
||||
}
|
||||
|
||||
private sealed class Fields<S> : Node<S> where S : unmanaged
|
||||
{
|
||||
private readonly string[] names;
|
||||
private readonly byte[][] utf8Names;
|
||||
private readonly JsonEncodedText[] encoded;
|
||||
private readonly Node<S>[] children;
|
||||
private readonly int[] offsets;
|
||||
|
||||
public Fields(params (string Name, Node<S> Child)[] fields)
|
||||
{
|
||||
names = new string[fields.Length];
|
||||
utf8Names = new byte[fields.Length][];
|
||||
encoded = new JsonEncodedText[fields.Length];
|
||||
children = new Node<S>[fields.Length];
|
||||
offsets = new int[fields.Length];
|
||||
for (var i = 0; i < fields.Length; i++)
|
||||
{
|
||||
names[i] = fields[i].Name;
|
||||
utf8Names[i] = Encoding.UTF8.GetBytes(fields[i].Name);
|
||||
encoded[i] = JsonEncodedText.Encode(fields[i].Name);
|
||||
children[i] = fields[i].Child;
|
||||
offsets[i] = Count;
|
||||
Count += fields[i].Child.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter w, ReadOnlySpan<S> flat, Scalar<S> scalar, JsonSerializerOptions o)
|
||||
{
|
||||
w.WriteStartObject();
|
||||
for (var i = 0; i < children.Length; i++)
|
||||
{
|
||||
w.WritePropertyName(encoded[i]);
|
||||
children[i].Write(w, flat.Slice(offsets[i], children[i].Count), scalar, o);
|
||||
}
|
||||
|
||||
w.WriteEndObject();
|
||||
}
|
||||
|
||||
public override void Read(ref Utf8JsonReader r, scoped Span<S> flat, scoped ReadOnlySpan<S> defaults, Scalar<S> scalar, JsonSerializerOptions o)
|
||||
{
|
||||
if (r.TokenType != JsonTokenType.StartObject) throw new JsonException($"Expected a JSON object, found {r.TokenType}.");
|
||||
var ignoreCase = o.PropertyNameCaseInsensitive;
|
||||
var seen = 0;
|
||||
while (r.Read() && r.TokenType == JsonTokenType.PropertyName)
|
||||
{
|
||||
var index = IndexOf(ref r, ignoreCase);
|
||||
r.Read();
|
||||
if (index < 0)
|
||||
{
|
||||
if (o.UnmappedMemberHandling == JsonUnmappedMemberHandling.Disallow)
|
||||
throw new JsonException("The JSON object has a property that is not a component.");
|
||||
r.Skip();
|
||||
continue;
|
||||
}
|
||||
|
||||
var slice = flat.Slice(offsets[index], children[index].Count);
|
||||
var bit = 1 << index;
|
||||
if ((seen & bit) != 0)
|
||||
{
|
||||
if (!DuplicatesAllowed(o)) throw new JsonException($"The property '{names[index]}' appears more than once.");
|
||||
defaults.Slice(offsets[index], slice.Length).CopyTo(slice);
|
||||
}
|
||||
|
||||
seen |= bit;
|
||||
children[index].Read(ref r, slice, defaults.Slice(offsets[index], slice.Length), scalar, o);
|
||||
}
|
||||
}
|
||||
|
||||
private int IndexOf(ref Utf8JsonReader r, bool ignoreCase)
|
||||
{
|
||||
if (!ignoreCase)
|
||||
{
|
||||
for (var i = 0; i < utf8Names.Length; i++)
|
||||
if (r.ValueTextEquals(utf8Names[i])) return i;
|
||||
return -1;
|
||||
}
|
||||
|
||||
var name = r.GetString()!;
|
||||
for (var i = 0; i < names.Length; i++)
|
||||
if (string.Equals(names[i], name, StringComparison.OrdinalIgnoreCase)) return i;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Rows<S> : Node<S> where S : unmanaged
|
||||
{
|
||||
private readonly Node<S> row;
|
||||
private readonly int rows;
|
||||
|
||||
public Rows(int rows, Node<S> row)
|
||||
{
|
||||
this.rows = rows;
|
||||
this.row = row;
|
||||
Count = rows * row.Count;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter w, ReadOnlySpan<S> flat, Scalar<S> scalar, JsonSerializerOptions o)
|
||||
{
|
||||
w.WriteStartArray();
|
||||
for (var i = 0; i < rows; i++) row.Write(w, flat.Slice(i * row.Count, row.Count), scalar, o);
|
||||
w.WriteEndArray();
|
||||
}
|
||||
|
||||
public override void Read(ref Utf8JsonReader r, scoped Span<S> flat, scoped ReadOnlySpan<S> defaults, Scalar<S> scalar, JsonSerializerOptions o)
|
||||
{
|
||||
if (r.TokenType != JsonTokenType.StartArray) throw new JsonException($"Expected a JSON array of {rows} rows, found {r.TokenType}.");
|
||||
for (var i = 0; i < rows; i++)
|
||||
{
|
||||
if (!r.Read() || r.TokenType == JsonTokenType.EndArray) throw new JsonException($"Expected {rows} rows.");
|
||||
row.Read(ref r, flat.Slice(i * row.Count, row.Count), defaults.Slice(i * row.Count, row.Count), scalar, o);
|
||||
}
|
||||
|
||||
if (!r.Read() || r.TokenType != JsonTokenType.EndArray) throw new JsonException($"Expected {rows} rows.");
|
||||
}
|
||||
}
|
||||
|
||||
private delegate T Make<T, S>(ReadOnlySpan<S> components) where S : unmanaged;
|
||||
private delegate void Flatten<T, S>(T value, Span<S> components) where S : unmanaged;
|
||||
|
||||
private sealed class Shape<T, S> : JsonConverter<T> where S : unmanaged
|
||||
{
|
||||
private readonly Scalar<S> scalar;
|
||||
private readonly Node<S> root;
|
||||
private readonly S[] defaults;
|
||||
private readonly Make<T, S> make;
|
||||
private readonly Flatten<T, S> flatten;
|
||||
|
||||
public Shape(Scalar<S> scalar, Node<S> root, Make<T, S> make, Flatten<T, S> flatten, S[]? initial)
|
||||
{
|
||||
this.scalar = scalar;
|
||||
this.root = root;
|
||||
this.make = make;
|
||||
this.flatten = flatten;
|
||||
defaults = initial ?? new S[root.Count];
|
||||
}
|
||||
|
||||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
Span<S> flat = stackalloc S[root.Count];
|
||||
defaults.CopyTo(flat);
|
||||
root.Read(ref reader, flat, defaults, scalar, options);
|
||||
return make(flat);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
Span<S> flat = stackalloc S[root.Count];
|
||||
flatten(value, flat);
|
||||
root.Write(writer, flat, scalar, options);
|
||||
}
|
||||
|
||||
public override T ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var text = reader.GetString() ?? throw new JsonException("A dictionary key cannot be null.");
|
||||
var parts = text.Split(',');
|
||||
if (parts.Length != root.Count) throw new JsonException($"A {typeof(T).Name} key has {root.Count} comma-separated components.");
|
||||
Span<S> flat = stackalloc S[root.Count];
|
||||
for (var i = 0; i < parts.Length; i++) flat[i] = scalar.ParseKey(parts[i], options);
|
||||
var value = make(flat);
|
||||
if (Spell(flat) != text) throw new JsonException($"'{text}' is not the canonical spelling of a {typeof(T).Name} key.");
|
||||
return value;
|
||||
}
|
||||
|
||||
public override void WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
|
||||
{
|
||||
Span<S> flat = stackalloc S[root.Count];
|
||||
flatten(value, flat);
|
||||
for (var i = 0; i < flat.Length; i++) scalar.Text(flat[i], options);
|
||||
writer.WritePropertyName(Spell(flat));
|
||||
}
|
||||
|
||||
private string Spell(ReadOnlySpan<S> flat)
|
||||
{
|
||||
var parts = new string[flat.Length];
|
||||
for (var i = 0; i < parts.Length; i++) parts[i] = scalar.Spelling(flat[i]);
|
||||
return string.Join(",", parts);
|
||||
}
|
||||
}
|
||||
|
||||
private static Node<S> Val<S>() where S : unmanaged => new Leaf<S>();
|
||||
|
||||
private static Node<S> Xy<S>() where S : unmanaged => new Fields<S>(("x", Val<S>()), ("y", Val<S>()));
|
||||
private static Node<S> Xyz<S>() where S : unmanaged => new Fields<S>(("x", Val<S>()), ("y", Val<S>()), ("z", Val<S>()));
|
||||
private static Node<S> Xyzw<S>() where S : unmanaged => new Fields<S>(("x", Val<S>()), ("y", Val<S>()), ("z", Val<S>()), ("w", Val<S>()));
|
||||
|
||||
private static KeyValuePair<Type, JsonConverter> Add<T, S>(
|
||||
Scalar<S> scalar, Node<S> root, Make<T, S> make, Flatten<T, S> flatten, S[]? initial = null) where S : unmanaged =>
|
||||
new(typeof(T), new Shape<T, S>(scalar, root, make, flatten, initial));
|
||||
|
||||
private static readonly Dictionary<Type, JsonConverter> Converters = Build();
|
||||
|
||||
private static Dictionary<Type, JsonConverter> Build()
|
||||
{
|
||||
var f = FloatScalar.Instance;
|
||||
var shapes = new[]
|
||||
{
|
||||
Add<float2, float>(f, Xy<float>(), a => new float2(a[0], a[1]), (v, a) => { a[0] = v.x; a[1] = v.y; }),
|
||||
Add<float3, float>(f, Xyz<float>(), a => new float3(a[0], a[1], a[2]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; }),
|
||||
Add<float4, float>(f, Xyzw<float>(), a => new float4(a[0], a[1], a[2], a[3]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; a[3] = v.w; }),
|
||||
Add<quaternion, float>(f, Xyzw<float>(), a => new quaternion(a[0], a[1], a[2], a[3]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; a[3] = v.w; }),
|
||||
Add<double2, double>(DoubleScalar.Instance, Xy<double>(), a => new double2(a[0], a[1]), (v, a) => { a[0] = v.x; a[1] = v.y; }),
|
||||
Add<double3, double>(DoubleScalar.Instance, Xyz<double>(), a => new double3(a[0], a[1], a[2]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; }),
|
||||
Add<int2, int>(IntScalar.Instance, Xy<int>(), a => new int2(a[0], a[1]), (v, a) => { a[0] = v.x; a[1] = v.y; }),
|
||||
Add<int3, int>(IntScalar.Instance, Xyz<int>(), a => new int3(a[0], a[1], a[2]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; }),
|
||||
Add<int4, int>(IntScalar.Instance, Xyzw<int>(), a => new int4(a[0], a[1], a[2], a[3]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; a[3] = v.w; }),
|
||||
Add<bool2, bool>(BoolScalar.Instance, Xy<bool>(), a => new bool2(a[0], a[1]), (v, a) => { a[0] = v.x; a[1] = v.y; }),
|
||||
Add<bool3, bool>(BoolScalar.Instance, Xyz<bool>(), a => new bool3(a[0], a[1], a[2]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; }),
|
||||
Add<bool4, bool>(BoolScalar.Instance, Xyzw<bool>(), a => new bool4(a[0], a[1], a[2], a[3]), (v, a) => { a[0] = v.x; a[1] = v.y; a[2] = v.z; a[3] = v.w; }),
|
||||
Add<Color32, byte>(ByteScalar.Instance,
|
||||
new Fields<byte>(("r", Val<byte>()), ("g", Val<byte>()), ("b", Val<byte>()), ("a", Val<byte>())),
|
||||
a => new Color32(a[0], a[1], a[2], a[3]), (v, a) => { a[0] = v.r; a[1] = v.g; a[2] = v.b; a[3] = v.a; },
|
||||
initial: new byte[] { 0, 0, 0, 255 }),
|
||||
Add<Random, uint>(UIntScalar.Instance, new Fields<uint>(("state", Val<uint>())),
|
||||
a => new Random { state = a[0] }, (v, a) => a[0] = v.state),
|
||||
|
||||
// Compositions: matrices are arrays of row objects, rect is {min, max}, CultCellular is {nearest, edge, id}.
|
||||
Add<float2x2, float>(f, new Rows<float>(2, Xy<float>()),
|
||||
a => new float2x2(a[0], a[1], a[2], a[3]),
|
||||
(v, a) => { a[0] = v[0].x; a[1] = v[0].y; a[2] = v[1].x; a[3] = v[1].y; }),
|
||||
Add<float3x3, float>(f, new Rows<float>(3, Xyz<float>()),
|
||||
a => new float3x3(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]),
|
||||
(v, a) =>
|
||||
{
|
||||
for (var row = 0; row < 3; row++)
|
||||
{
|
||||
a[row * 3] = v[row].x;
|
||||
a[(row * 3) + 1] = v[row].y;
|
||||
a[(row * 3) + 2] = v[row].z;
|
||||
}
|
||||
}),
|
||||
Add<rect, float>(f, new Fields<float>(("min", Xy<float>()), ("max", Xy<float>())),
|
||||
a => new rect(a[0], a[1], a[2], a[3]),
|
||||
(v, a) => { a[0] = v.min.x; a[1] = v.min.y; a[2] = v.max.x; a[3] = v.max.y; }),
|
||||
Add<CultCellular, float>(f, new Fields<float>(("nearest", Xyzw<float>()), ("edge", Xyzw<float>()), ("id", Val<float>())),
|
||||
a => new CultCellular(new float4(a[0], a[1], a[2], a[3]), new float4(a[4], a[5], a[6], a[7]), a[8]),
|
||||
(v, a) =>
|
||||
{
|
||||
a[0] = v.nearest.x; a[1] = v.nearest.y; a[2] = v.nearest.z; a[3] = v.nearest.w;
|
||||
a[4] = v.edge.x; a[5] = v.edge.y; a[6] = v.edge.z; a[7] = v.edge.w;
|
||||
a[8] = v.id;
|
||||
}),
|
||||
};
|
||||
|
||||
var map = new Dictionary<Type, JsonConverter>();
|
||||
foreach (var shape in shapes) map.Add(shape.Key, shape.Value);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
230
src/GameCult.Caching.MessagePack/CultMathResolver.cs
Normal file
230
src/GameCult.Caching.MessagePack/CultMathResolver.cs
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CultMath;
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
using Random = CultMath.Random;
|
||||
|
||||
namespace GameCult.Caching.MessagePack;
|
||||
|
||||
/// <summary>
|
||||
/// The canonical MessagePack encoding of every public CultMath value type. Each type is an
|
||||
/// array of its components in declaration order: vectors and quaternions are flat scalar
|
||||
/// arrays, matrices are arrays of row vectors, and <c>rect</c> is <c>[min, max]</c>. Decoding
|
||||
/// skips trailing elements and leaves missing ones at zero (a missing Color32 alpha is 255); nil is refused. The resolver is
|
||||
/// part of the default <see cref="CultDocumentMessagePackSerialization"/> chain, so a consumer
|
||||
/// registers nothing.
|
||||
/// </summary>
|
||||
public sealed class CultMathResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly CultMathResolver Instance = new();
|
||||
private CultMathResolver() { }
|
||||
|
||||
/// <summary>The CultMath types this resolver formats, for coverage checks.</summary>
|
||||
public static IReadOnlyCollection<Type> FormattedTypes => Formatters.Keys;
|
||||
|
||||
public IMessagePackFormatter<T>? GetFormatter<T>() => Cache<T>.Formatter;
|
||||
|
||||
private static class Cache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T>? Formatter =
|
||||
Formatters.TryGetValue(typeof(T), out var formatter) ? (IMessagePackFormatter<T>)formatter : null;
|
||||
}
|
||||
|
||||
private delegate void WriteFn<T>(ref MessagePackWriter writer, T value);
|
||||
private delegate T ReadFn<T>(ref MessagePackReader reader);
|
||||
|
||||
private sealed class ArrayShape<T> : IMessagePackFormatter<T>
|
||||
{
|
||||
private readonly WriteFn<T> write;
|
||||
private readonly ReadFn<T> read;
|
||||
|
||||
public ArrayShape(WriteFn<T> write, ReadFn<T> read)
|
||||
{
|
||||
this.write = write;
|
||||
this.read = read;
|
||||
}
|
||||
|
||||
public void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options) => write(ref writer, value);
|
||||
|
||||
public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
return read(ref reader);
|
||||
}
|
||||
}
|
||||
|
||||
private static KeyValuePair<Type, object> Shape<T>(WriteFn<T> write, ReadFn<T> read) =>
|
||||
new(typeof(T), new ArrayShape<T>(write, read));
|
||||
|
||||
// Each reader takes the first dst.Length elements and skips the rest.
|
||||
private static void Singles(ref MessagePackReader r, scoped Span<float> dst)
|
||||
{
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < dst.Length) dst[i] = r.ReadSingle(); else r.Skip();
|
||||
}
|
||||
|
||||
private static void Doubles(ref MessagePackReader r, scoped Span<double> dst)
|
||||
{
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < dst.Length) dst[i] = r.ReadDouble(); else r.Skip();
|
||||
}
|
||||
|
||||
private static void Ints(ref MessagePackReader r, scoped Span<int> dst)
|
||||
{
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < dst.Length) dst[i] = r.ReadInt32(); else r.Skip();
|
||||
}
|
||||
|
||||
private static void Bools(ref MessagePackReader r, scoped Span<bool> dst)
|
||||
{
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < dst.Length) dst[i] = r.ReadBoolean(); else r.Skip();
|
||||
}
|
||||
|
||||
private static void Bytes(ref MessagePackReader r, scoped Span<byte> dst)
|
||||
{
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < dst.Length) dst[i] = r.ReadByte(); else r.Skip();
|
||||
}
|
||||
|
||||
private static void Put(ref MessagePackWriter w, scoped ReadOnlySpan<float> v)
|
||||
{
|
||||
w.WriteArrayHeader(v.Length);
|
||||
foreach (var e in v) w.Write(e);
|
||||
}
|
||||
|
||||
private static void Put(ref MessagePackWriter w, scoped ReadOnlySpan<double> v)
|
||||
{
|
||||
w.WriteArrayHeader(v.Length);
|
||||
foreach (var e in v) w.Write(e);
|
||||
}
|
||||
|
||||
private static void Put(ref MessagePackWriter w, scoped ReadOnlySpan<int> v)
|
||||
{
|
||||
w.WriteArrayHeader(v.Length);
|
||||
foreach (var e in v) w.Write(e);
|
||||
}
|
||||
|
||||
private static void Put(ref MessagePackWriter w, scoped ReadOnlySpan<bool> v)
|
||||
{
|
||||
w.WriteArrayHeader(v.Length);
|
||||
foreach (var e in v) w.Write(e);
|
||||
}
|
||||
|
||||
private static void Put(ref MessagePackWriter w, scoped ReadOnlySpan<byte> v)
|
||||
{
|
||||
w.WriteArrayHeader(v.Length);
|
||||
foreach (var e in v) w.Write(e);
|
||||
}
|
||||
|
||||
private static void PutFloat2(ref MessagePackWriter w, float2 v) => Put(ref w, stackalloc float[] { v.x, v.y });
|
||||
private static void PutFloat3(ref MessagePackWriter w, float3 v) => Put(ref w, stackalloc float[] { v.x, v.y, v.z });
|
||||
private static void PutFloat4(ref MessagePackWriter w, float4 v) => Put(ref w, stackalloc float[] { v.x, v.y, v.z, v.w });
|
||||
|
||||
private static float2 GetFloat2(ref MessagePackReader r)
|
||||
{
|
||||
Span<float> s = stackalloc float[2];
|
||||
Singles(ref r, s);
|
||||
return new float2(s[0], s[1]);
|
||||
}
|
||||
|
||||
private static float3 GetFloat3(ref MessagePackReader r)
|
||||
{
|
||||
Span<float> s = stackalloc float[3];
|
||||
Singles(ref r, s);
|
||||
return new float3(s[0], s[1], s[2]);
|
||||
}
|
||||
|
||||
private static float4 GetFloat4(ref MessagePackReader r)
|
||||
{
|
||||
Span<float> s = stackalloc float[4];
|
||||
Singles(ref r, s);
|
||||
return new float4(s[0], s[1], s[2], s[3]);
|
||||
}
|
||||
|
||||
private static readonly Dictionary<Type, object> Formatters = Build();
|
||||
|
||||
private static Dictionary<Type, object> Build()
|
||||
{
|
||||
var shapes = new[]
|
||||
{
|
||||
Shape<float2>(PutFloat2, GetFloat2),
|
||||
Shape<float3>(PutFloat3, GetFloat3),
|
||||
Shape<float4>(PutFloat4, GetFloat4),
|
||||
Shape<double2>((ref MessagePackWriter w, double2 v) => Put(ref w, stackalloc double[] { v.x, v.y }),
|
||||
(ref MessagePackReader r) => { Span<double> s = stackalloc double[2]; Doubles(ref r, s); return new double2(s[0], s[1]); }),
|
||||
Shape<double3>((ref MessagePackWriter w, double3 v) => Put(ref w, stackalloc double[] { v.x, v.y, v.z }),
|
||||
(ref MessagePackReader r) => { Span<double> s = stackalloc double[3]; Doubles(ref r, s); return new double3(s[0], s[1], s[2]); }),
|
||||
Shape<int2>((ref MessagePackWriter w, int2 v) => Put(ref w, stackalloc int[] { v.x, v.y }),
|
||||
(ref MessagePackReader r) => { Span<int> s = stackalloc int[2]; Ints(ref r, s); return new int2(s[0], s[1]); }),
|
||||
Shape<int3>((ref MessagePackWriter w, int3 v) => Put(ref w, stackalloc int[] { v.x, v.y, v.z }),
|
||||
(ref MessagePackReader r) => { Span<int> s = stackalloc int[3]; Ints(ref r, s); return new int3(s[0], s[1], s[2]); }),
|
||||
Shape<int4>((ref MessagePackWriter w, int4 v) => Put(ref w, stackalloc int[] { v.x, v.y, v.z, v.w }),
|
||||
(ref MessagePackReader r) => { Span<int> s = stackalloc int[4]; Ints(ref r, s); return new int4(s[0], s[1], s[2], s[3]); }),
|
||||
Shape<bool2>((ref MessagePackWriter w, bool2 v) => Put(ref w, stackalloc bool[] { v.x, v.y }),
|
||||
(ref MessagePackReader r) => { Span<bool> s = stackalloc bool[2]; Bools(ref r, s); return new bool2(s[0], s[1]); }),
|
||||
Shape<bool3>((ref MessagePackWriter w, bool3 v) => Put(ref w, stackalloc bool[] { v.x, v.y, v.z }),
|
||||
(ref MessagePackReader r) => { Span<bool> s = stackalloc bool[3]; Bools(ref r, s); return new bool3(s[0], s[1], s[2]); }),
|
||||
Shape<bool4>((ref MessagePackWriter w, bool4 v) => Put(ref w, stackalloc bool[] { v.x, v.y, v.z, v.w }),
|
||||
(ref MessagePackReader r) => { Span<bool> s = stackalloc bool[4]; Bools(ref r, s); return new bool4(s[0], s[1], s[2], s[3]); }),
|
||||
Shape<quaternion>((ref MessagePackWriter w, quaternion v) => Put(ref w, stackalloc float[] { v.x, v.y, v.z, v.w }),
|
||||
(ref MessagePackReader r) => { Span<float> s = stackalloc float[4]; Singles(ref r, s); return new quaternion(s[0], s[1], s[2], s[3]); }),
|
||||
Shape<Color32>((ref MessagePackWriter w, Color32 v) => Put(ref w, stackalloc byte[] { v.r, v.g, v.b, v.a }),
|
||||
(ref MessagePackReader r) => { Span<byte> s = stackalloc byte[4]; s[3] = 255; Bytes(ref r, s); return new Color32(s[0], s[1], s[2], s[3]); }),
|
||||
Shape<Random>((ref MessagePackWriter w, Random v) => { w.WriteArrayHeader(1); w.Write(v.state); },
|
||||
(ref MessagePackReader r) =>
|
||||
{
|
||||
var value = default(Random);
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i == 0) value.state = r.ReadUInt32(); else r.Skip();
|
||||
return value;
|
||||
}),
|
||||
|
||||
// Compositions: matrices are rows, rect is [min, max], CultCellular is [nearest, edge, id].
|
||||
Shape<float2x2>((ref MessagePackWriter w, float2x2 v) => { w.WriteArrayHeader(2); PutFloat2(ref w, v[0]); PutFloat2(ref w, v[1]); },
|
||||
(ref MessagePackReader r) =>
|
||||
{
|
||||
var rows = new float2[2];
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < 2) rows[i] = GetFloat2(ref r); else r.Skip();
|
||||
return new float2x2(rows[0], rows[1]);
|
||||
}),
|
||||
Shape<float3x3>((ref MessagePackWriter w, float3x3 v) => { w.WriteArrayHeader(3); PutFloat3(ref w, v[0]); PutFloat3(ref w, v[1]); PutFloat3(ref w, v[2]); },
|
||||
(ref MessagePackReader r) =>
|
||||
{
|
||||
var rows = new float3[3];
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < 3) rows[i] = GetFloat3(ref r); else r.Skip();
|
||||
return new float3x3(rows[0], rows[1], rows[2]);
|
||||
}),
|
||||
Shape<rect>((ref MessagePackWriter w, rect v) => { w.WriteArrayHeader(2); PutFloat2(ref w, v.min); PutFloat2(ref w, v.max); },
|
||||
(ref MessagePackReader r) =>
|
||||
{
|
||||
var corners = new float2[2];
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
if (i < 2) corners[i] = GetFloat2(ref r); else r.Skip();
|
||||
return new rect(corners[0], corners[1]);
|
||||
}),
|
||||
Shape<CultCellular>((ref MessagePackWriter w, CultCellular v) => { w.WriteArrayHeader(3); PutFloat4(ref w, v.nearest); PutFloat4(ref w, v.edge); w.Write(v.id); },
|
||||
(ref MessagePackReader r) =>
|
||||
{
|
||||
var value = default(CultCellular);
|
||||
for (int i = 0, n = r.ReadArrayHeader(); i < n; i++)
|
||||
{
|
||||
switch (i)
|
||||
{
|
||||
case 0: value.nearest = GetFloat4(ref r); break;
|
||||
case 1: value.edge = GetFloat4(ref r); break;
|
||||
case 2: value.id = r.ReadSingle(); break;
|
||||
default: r.Skip(); break;
|
||||
}
|
||||
}
|
||||
return value;
|
||||
}),
|
||||
};
|
||||
|
||||
var map = new Dictionary<Type, object>();
|
||||
foreach (var shape in shapes) map.Add(shape.Key, shape.Value);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
|
@ -6,7 +6,9 @@
|
|||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\GameCult.Caching\GameCult.Caching.csproj" />
|
||||
<ProjectReference Include="..\..\packages\cultmath\src\CultMath\CultMath.csproj" />
|
||||
<PackageReference Include="MessagePack" Version="3.1.7" />
|
||||
<PackageReference Include="System.Text.Json" Version="9.0.10" />
|
||||
<PackageReference Include="MessagePackAnalyzer" Version="3.1.7" PrivateAssets="all" />
|
||||
<InternalsVisibleTo Include="GameCult.Caching.Tests" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ format for the attribute-first `GameCult.Caching` stack.
|
|||
- `CultDocumentMessagePackSerialization`
|
||||
- `CultDocumentResolver`
|
||||
- `CultRecordRefFormatter<T>`
|
||||
- `CultMathResolver`: the canonical component-array encoding of every public CultMath value type, part of the default resolver chain
|
||||
|
||||
Cult document payloads serialize through `MessagePackSerializer` with the
|
||||
options of the document's assembly (`CultDocumentMessagePackSerialization.OptionsFor`),
|
||||
|
|
@ -56,3 +57,42 @@ await cache.PullAllBackingStoresAsync();
|
|||
persisted through hand-written MessagePack array layouts in this package.
|
||||
- The raw CultNet document lane should treat these payload bytes as already
|
||||
blessed, not decode and re-encode them for sport.
|
||||
|
||||
## CultMath Value Shapes
|
||||
|
||||
Every public CultMath value type has one MessagePack shape (`CultMathResolver`,
|
||||
in the default chain) and one JSON shape (`CultMathJson`, added to a consumer's
|
||||
options with `new JsonSerializerOptions().AddCultMathConverters()`; CultLib owns
|
||||
no JSON options of its own). MessagePack decoding skips extra elements, leaves
|
||||
missing ones at zero (`Color32` alpha at 255) and refuses nil. JSON reading
|
||||
skips unknown properties (`UnmappedMemberHandling.Disallow` refuses them), leaves
|
||||
missing ones at zero, honours `PropertyNameCaseInsensitive`, lets a repeated
|
||||
property replace the whole earlier value (refused when `AllowDuplicateProperties`
|
||||
is false), and throws `JsonException` for a wrong token or an out-of-range number.
|
||||
Numbers follow `JsonSerializerOptions.NumberHandling` as System.Text.Json's own
|
||||
float does: `"NaN"`, `"Infinity"` and `"-Infinity"` are written under
|
||||
`AllowNamedFloatingPointLiterals` or `WriteAsString` and read under
|
||||
`AllowNamedFloatingPointLiterals` or `AllowReadingFromString`; quoted numbers read
|
||||
under `AllowReadingFromString` with no surrounding whitespace.
|
||||
Every type is also a dictionary key: its flattened components joined by commas,
|
||||
invariant culture (`"1,2"`, `"true,false"`, matrices row by row, `rect` as
|
||||
`"minX,minY,maxX,maxY"`), and only that exact spelling reads back. Two intended
|
||||
divergences from System.Text.Json: a bare number beyond a component's range
|
||||
(`1e40` as a float) is refused where System.Text.Json reads infinity, and a
|
||||
refusal on write is a `JsonException` where System.Text.Json throws
|
||||
`ArgumentException`. A consumer resolver or converter
|
||||
registered ahead of these wins.
|
||||
|
||||
| Type | MessagePack | JSON |
|
||||
| --- | --- | --- |
|
||||
| `float2`, `double2`, `int2`, `bool2` | `[x, y]` | `{"x":..,"y":..}` |
|
||||
| `float3`, `double3`, `int3`, `bool3` | `[x, y, z]` | `{"x":..,"y":..,"z":..}` |
|
||||
| `float4`, `int4`, `bool4`, `quaternion` | `[x, y, z, w]` | `{"x":..,"y":..,"z":..,"w":..}` |
|
||||
| `Color32` | `[r, g, b, a]` as uint8 | `{"r":..,"g":..,"b":..,"a":..}` |
|
||||
| `Random` | `[state]` | `{"state":..}` |
|
||||
| `float2x2`, `float3x3` | array of row vectors (row-major) | array of row objects |
|
||||
| `rect` | `[min, max]` of `float2` | `{"min":{..},"max":{..}}` |
|
||||
| `CultCellular` | `[nearest, edge, id]` | `{"nearest":{..},"edge":{..},"id":..}` |
|
||||
|
||||
Floats are MessagePack float32 (`0xCA`), doubles float64 (`0xCB`), integers the
|
||||
shortest MessagePack integer.
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ namespace GameCult.Geometry
|
|||
public static CultGeometryTriangleMesh Extract(
|
||||
float[,,] samples,
|
||||
float isoValue = 0f,
|
||||
CultVec3 origin = default,
|
||||
float3 origin = default,
|
||||
float cellSize = 1f)
|
||||
{
|
||||
if (samples == null) throw new ArgumentNullException(nameof(samples));
|
||||
|
|
@ -56,7 +56,6 @@ namespace GameCult.Geometry
|
|||
var positions = new List<float>();
|
||||
var normals = new List<float>();
|
||||
var indices = new List<uint>();
|
||||
var originValue = new float3(origin.X, origin.Y, origin.Z);
|
||||
var cornerPositions = new float3[8];
|
||||
var cornerValues = new float[8];
|
||||
var tetraPositions = new float3[4];
|
||||
|
|
@ -66,7 +65,7 @@ namespace GameCult.Geometry
|
|||
for (var y = 0; y < samples.GetLength(1) - 1; y++)
|
||||
for (var z = 0; z < samples.GetLength(2) - 1; z++)
|
||||
{
|
||||
var cubeOrigin = originValue + new float3(x, y, z) * cellSize;
|
||||
var cubeOrigin = origin + new float3(x, y, z) * cellSize;
|
||||
for (var corner = 0; corner < CubeCorners.Length; corner++)
|
||||
{
|
||||
var offset = CubeCorners[corner];
|
||||
|
|
|
|||
|
|
@ -1,197 +1,10 @@
|
|||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
using CultMath;
|
||||
using MessagePack;
|
||||
|
||||
namespace GameCult.Geometry
|
||||
{
|
||||
/// <summary>
|
||||
/// Deterministic two-dimensional float vector for cross-runtime geometry and query contracts.
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public readonly struct CultVec2 : IEquatable<CultVec2>
|
||||
{
|
||||
/// <summary>Creates a two-dimensional vector.</summary>
|
||||
public CultVec2(float x, float y)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
|
||||
/// <summary>Gets the x component.</summary>
|
||||
[Key(0)]
|
||||
public float X { get; }
|
||||
|
||||
/// <summary>Gets the y component.</summary>
|
||||
[Key(1)]
|
||||
public float Y { get; }
|
||||
|
||||
/// <summary>Gets the zero vector.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public static CultVec2 Zero => new(0f, 0f);
|
||||
|
||||
/// <summary>Gets the squared vector length.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public float LengthSquared => (X * X) + (Y * Y);
|
||||
|
||||
/// <summary>Returns whether this vector is equal to another vector.</summary>
|
||||
public bool Equals(CultVec2 other) => X.Equals(other.X) && Y.Equals(other.Y);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj) => obj is CultVec2 other && Equals(other);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode() => HashCode.Combine(X, Y);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"({X}, {Y})";
|
||||
|
||||
public static CultVec2 operator +(CultVec2 left, CultVec2 right) => new(left.X + right.X, left.Y + right.Y);
|
||||
public static CultVec2 operator -(CultVec2 left, CultVec2 right) => new(left.X - right.X, left.Y - right.Y);
|
||||
public static CultVec2 operator *(CultVec2 value, float scalar) => new(value.X * scalar, value.Y * scalar);
|
||||
public static CultVec2 operator /(CultVec2 value, float scalar) => new(value.X / scalar, value.Y / scalar);
|
||||
public static bool operator ==(CultVec2 left, CultVec2 right) => left.Equals(right);
|
||||
public static bool operator !=(CultVec2 left, CultVec2 right) => !left.Equals(right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deterministic three-dimensional float vector for cross-runtime geometry and query contracts.
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public readonly struct CultVec3 : IEquatable<CultVec3>
|
||||
{
|
||||
/// <summary>Creates a three-dimensional vector.</summary>
|
||||
public CultVec3(float x, float y, float z)
|
||||
{
|
||||
X = x;
|
||||
Y = y;
|
||||
Z = z;
|
||||
}
|
||||
|
||||
/// <summary>Gets the x component.</summary>
|
||||
[Key(0)]
|
||||
public float X { get; }
|
||||
|
||||
/// <summary>Gets the y component.</summary>
|
||||
[Key(1)]
|
||||
public float Y { get; }
|
||||
|
||||
/// <summary>Gets the z component.</summary>
|
||||
[Key(2)]
|
||||
public float Z { get; }
|
||||
|
||||
/// <summary>Gets the zero vector.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public static CultVec3 Zero => new(0f, 0f, 0f);
|
||||
|
||||
/// <summary>Gets the XY plane projection.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public CultVec2 Xy => new(X, Y);
|
||||
|
||||
/// <summary>Gets the XZ plane projection for Unity compatibility adapters.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public CultVec2 Xz => new(X, Z);
|
||||
|
||||
/// <summary>Gets the squared vector length.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public float LengthSquared => (X * X) + (Y * Y) + (Z * Z);
|
||||
|
||||
/// <summary>Returns whether this vector is equal to another vector.</summary>
|
||||
public bool Equals(CultVec3 other) => X.Equals(other.X) && Y.Equals(other.Y) && Z.Equals(other.Z);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj) => obj is CultVec3 other && Equals(other);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode() => HashCode.Combine(X, Y, Z);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"({X}, {Y}, {Z})";
|
||||
|
||||
public static CultVec3 operator +(CultVec3 left, CultVec3 right) => new(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
|
||||
public static CultVec3 operator -(CultVec3 left, CultVec3 right) => new(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
|
||||
public static CultVec3 operator *(CultVec3 value, float scalar) => new(value.X * scalar, value.Y * scalar, value.Z * scalar);
|
||||
public static CultVec3 operator /(CultVec3 value, float scalar) => new(value.X / scalar, value.Y / scalar, value.Z / scalar);
|
||||
public static bool operator ==(CultVec3 left, CultVec3 right) => left.Equals(right);
|
||||
public static bool operator !=(CultVec3 left, CultVec3 right) => !left.Equals(right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Axis-aligned two-dimensional rectangle represented by canonical min and max corners.
|
||||
/// </summary>
|
||||
[MessagePackObject]
|
||||
public readonly struct CultRect : IEquatable<CultRect>
|
||||
{
|
||||
/// <summary>Creates a rectangle and canonicalizes min/max corners.</summary>
|
||||
public CultRect(CultVec2 min, CultVec2 max)
|
||||
{
|
||||
Min = new CultVec2(Math.Min(min.X, max.X), Math.Min(min.Y, max.Y));
|
||||
Max = new CultVec2(Math.Max(min.X, max.X), Math.Max(min.Y, max.Y));
|
||||
}
|
||||
|
||||
/// <summary>Creates a rectangle and canonicalizes min/max corners.</summary>
|
||||
public CultRect(float minX, float minY, float maxX, float maxY)
|
||||
: this(new CultVec2(minX, minY), new CultVec2(maxX, maxY))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Gets the canonical minimum corner.</summary>
|
||||
[Key(0)]
|
||||
public CultVec2 Min { get; }
|
||||
|
||||
/// <summary>Gets the canonical maximum corner.</summary>
|
||||
[Key(1)]
|
||||
public CultVec2 Max { get; }
|
||||
|
||||
/// <summary>Gets the rectangle size.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public CultVec2 Size => Max - Min;
|
||||
|
||||
/// <summary>Gets the rectangle center.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public CultVec2 Center => (Min + Max) * 0.5f;
|
||||
|
||||
/// <summary>Returns whether the point lies inside or on the rectangle edge.</summary>
|
||||
public bool Contains(CultVec2 point)
|
||||
{
|
||||
return point.X >= Min.X &&
|
||||
point.X <= Max.X &&
|
||||
point.Y >= Min.Y &&
|
||||
point.Y <= Max.Y;
|
||||
}
|
||||
|
||||
/// <summary>Returns whether this rectangle intersects another rectangle.</summary>
|
||||
public bool Intersects(CultRect other)
|
||||
{
|
||||
return Min.X <= other.Max.X &&
|
||||
Max.X >= other.Min.X &&
|
||||
Min.Y <= other.Max.Y &&
|
||||
Max.Y >= other.Min.Y;
|
||||
}
|
||||
|
||||
/// <summary>Returns whether this rectangle is equal to another rectangle.</summary>
|
||||
public bool Equals(CultRect other) => Min == other.Min && Max == other.Max;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj) => obj is CultRect other && Equals(other);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int GetHashCode() => HashCode.Combine(Min, Max);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[{Min}..{Max}]";
|
||||
|
||||
public static bool operator ==(CultRect left, CultRect right) => left.Equals(right);
|
||||
public static bool operator !=(CultRect left, CultRect right) => !left.Equals(right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Two-dimensional circle for spatial queries.
|
||||
/// </summary>
|
||||
|
|
@ -199,7 +12,8 @@ namespace GameCult.Geometry
|
|||
public readonly struct CultCircle : IEquatable<CultCircle>
|
||||
{
|
||||
/// <summary>Creates a circle.</summary>
|
||||
public CultCircle(CultVec2 center, float radius)
|
||||
[JsonConstructor]
|
||||
public CultCircle(float2 center, float radius)
|
||||
{
|
||||
if (radius < 0f)
|
||||
{
|
||||
|
|
@ -212,7 +26,7 @@ namespace GameCult.Geometry
|
|||
|
||||
/// <summary>Gets the circle center.</summary>
|
||||
[Key(0)]
|
||||
public CultVec2 Center { get; }
|
||||
public float2 Center { get; }
|
||||
|
||||
/// <summary>Gets the circle radius.</summary>
|
||||
[Key(1)]
|
||||
|
|
@ -221,29 +35,29 @@ namespace GameCult.Geometry
|
|||
/// <summary>Gets the circle bounds.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public CultRect Bounds => new(
|
||||
Center.X - Radius,
|
||||
Center.Y - Radius,
|
||||
Center.X + Radius,
|
||||
Center.Y + Radius);
|
||||
public rect Bounds => new(
|
||||
Center.x - Radius,
|
||||
Center.y - Radius,
|
||||
Center.x + Radius,
|
||||
Center.y + Radius);
|
||||
|
||||
/// <summary>Returns whether the point lies inside or on the circle edge.</summary>
|
||||
public bool Contains(CultVec2 point)
|
||||
public bool Contains(float2 point)
|
||||
{
|
||||
var delta = point - Center;
|
||||
return delta.LengthSquared <= Radius * Radius;
|
||||
return math.lengthsq(delta) <= Radius * Radius;
|
||||
}
|
||||
|
||||
/// <summary>Returns whether this circle intersects a rectangle.</summary>
|
||||
public bool Intersects(CultRect rect)
|
||||
public bool Intersects(rect rect)
|
||||
{
|
||||
var clampedX = Math.Max(rect.Min.X, Math.Min(Center.X, rect.Max.X));
|
||||
var clampedY = Math.Max(rect.Min.Y, Math.Min(Center.Y, rect.Max.Y));
|
||||
return Contains(new CultVec2(clampedX, clampedY));
|
||||
var clampedX = Math.Max(rect.min.x, Math.Min(Center.x, rect.max.x));
|
||||
var clampedY = Math.Max(rect.min.y, Math.Min(Center.y, rect.max.y));
|
||||
return Contains(new float2(clampedX, clampedY));
|
||||
}
|
||||
|
||||
/// <summary>Returns whether this circle is equal to another circle.</summary>
|
||||
public bool Equals(CultCircle other) => Center == other.Center && Radius.Equals(other.Radius);
|
||||
public bool Equals(CultCircle other) => Center.Equals(other.Center) && Radius.Equals(other.Radius);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj) => obj is CultCircle other && Equals(other);
|
||||
|
|
@ -262,7 +76,8 @@ namespace GameCult.Geometry
|
|||
public readonly struct CultSphere : IEquatable<CultSphere>
|
||||
{
|
||||
/// <summary>Creates a sphere.</summary>
|
||||
public CultSphere(CultVec3 center, float radius)
|
||||
[JsonConstructor]
|
||||
public CultSphere(float3 center, float radius)
|
||||
{
|
||||
if (radius < 0f)
|
||||
{
|
||||
|
|
@ -275,7 +90,7 @@ namespace GameCult.Geometry
|
|||
|
||||
/// <summary>Gets the sphere center.</summary>
|
||||
[Key(0)]
|
||||
public CultVec3 Center { get; }
|
||||
public float3 Center { get; }
|
||||
|
||||
/// <summary>Gets the sphere radius.</summary>
|
||||
[Key(1)]
|
||||
|
|
@ -284,17 +99,17 @@ namespace GameCult.Geometry
|
|||
/// <summary>Gets the XY circle projection.</summary>
|
||||
[IgnoreMember]
|
||||
[JsonIgnore]
|
||||
public CultCircle XyCircle => new(Center.Xy, Radius);
|
||||
public CultCircle XyCircle => new(Center.xy, Radius);
|
||||
|
||||
/// <summary>Returns whether the point lies inside or on the sphere surface.</summary>
|
||||
public bool Contains(CultVec3 point)
|
||||
public bool Contains(float3 point)
|
||||
{
|
||||
var delta = point - Center;
|
||||
return delta.LengthSquared <= Radius * Radius;
|
||||
return math.lengthsq(delta) <= Radius * Radius;
|
||||
}
|
||||
|
||||
/// <summary>Returns whether this sphere is equal to another sphere.</summary>
|
||||
public bool Equals(CultSphere other) => Center == other.Center && Radius.Equals(other.Radius);
|
||||
public bool Equals(CultSphere other) => Center.Equals(other.Center) && Radius.Equals(other.Radius);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object? obj) => obj is CultSphere other && Equals(other);
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ namespace GameCult.Geometry
|
|||
public static CultGeometryQuadMesh Extract(
|
||||
float[,,] samples,
|
||||
float isoValue = 0f,
|
||||
CultVec3 origin = default,
|
||||
float3 origin = default,
|
||||
float cellSize = 1f)
|
||||
{
|
||||
if (samples == null) throw new ArgumentNullException(nameof(samples));
|
||||
|
|
@ -66,7 +66,7 @@ namespace GameCult.Geometry
|
|||
throw new ArgumentException("The isovalue must be finite.", nameof(isoValue));
|
||||
}
|
||||
|
||||
if (!float.IsFinite(origin.X) || !float.IsFinite(origin.Y) || !float.IsFinite(origin.Z))
|
||||
if (!float.IsFinite(origin.x) || !float.IsFinite(origin.y) || !float.IsFinite(origin.z))
|
||||
{
|
||||
throw new ArgumentException("The origin must be finite.", nameof(origin));
|
||||
}
|
||||
|
|
@ -80,7 +80,6 @@ namespace GameCult.Geometry
|
|||
var sizeY = samples.GetLength(1);
|
||||
var sizeZ = samples.GetLength(2);
|
||||
var cellCounts = new[] { sizeX - 1, sizeY - 1, sizeZ - 1 };
|
||||
var originValue = new float3(origin.X, origin.Y, origin.Z);
|
||||
|
||||
var vertexIndex = new Dictionary<(int, int, int), uint>();
|
||||
var positions = new List<float>();
|
||||
|
|
@ -90,7 +89,7 @@ namespace GameCult.Geometry
|
|||
for (var cz = 0; cz < cellCounts[2]; cz++)
|
||||
{
|
||||
var cellCoord = new[] { cx, cy, cz };
|
||||
var vertex = CellVertex(samples, cellCoord, isoValue, originValue, cellSize);
|
||||
var vertex = CellVertex(samples, cellCoord, isoValue, origin, cellSize);
|
||||
if (vertex == null) continue;
|
||||
|
||||
vertexIndex[(cx, cy, cz)] = (uint)(positions.Count / 3);
|
||||
|
|
|
|||
|
|
@ -31,12 +31,9 @@ The package is deliberately substrate-shaped:
|
|||
## Value Primitives
|
||||
|
||||
The small value layer is for cross-runtime query contracts, physics probes,
|
||||
native slices, and UI surfaces:
|
||||
native slices, and UI surfaces. Vectors and rectangles are CultMath's `float2`,
|
||||
`float3` and `rect`, and `GameCult.Caching.MessagePack` serializes them.
|
||||
|
||||
- `CultVec2` and `CultVec3`: whole-vector values for positions, velocities,
|
||||
accelerations, and query inputs. SoA layouts may store them efficiently, but
|
||||
the semantic API does not force callers to split x/y/z into unrelated fields.
|
||||
- `CultRect`: canonical XY viewport/query rectangle stored as min/max corners.
|
||||
- `CultCircle`: 2D influence/query brush with rect intersection helpers.
|
||||
- `CultSphere`: 3D query primitive with whole-vector center and XY projection.
|
||||
|
||||
|
|
@ -85,8 +82,8 @@ cache miss; that is split-brain geometry.
|
|||
|
||||
## Documents
|
||||
|
||||
- `CultVec2`, `CultVec3`, `CultRect`, `CultCircle`, `CultSphere`: shared value
|
||||
primitives for query and view contracts.
|
||||
- `CultCircle`, `CultSphere`: shared value primitives for query and view
|
||||
contracts.
|
||||
- `CultGeometryDomainDocument`: one hierarchical domain tree, suitable for
|
||||
Ragnarok/Fensalir-style feature DSL output.
|
||||
- `CultGeometryBuildRequest`: one LOD/frustum/budget request for workers.
|
||||
|
|
|
|||
|
|
@ -116,12 +116,12 @@ var aetheria = verse.Use(AetheriaRuntime.Schema);
|
|||
var viewport = await aetheria.Zone(zoneId)
|
||||
.Objects()
|
||||
.VisibleTo(controlledUnits)
|
||||
.Within(CultRect.FromBounds(-400, -250, 400, 250))
|
||||
.Within(new rect(-400f, -250f, 400f, 250f))
|
||||
.QueryAsync();
|
||||
|
||||
await aetheria.Entity(ravenId)
|
||||
.Pilot()
|
||||
.MoveAsync(CultVec2.Right, claim: "pilot-control");
|
||||
.MoveAsync(new float2(1f, 0f), claim: "pilot-control");
|
||||
```
|
||||
|
||||
Current seed:
|
||||
|
|
@ -134,7 +134,7 @@ var verse = await CultMesh.ConnectVerseAsync(
|
|||
new[] { new CultMeshAuthorityClaim("pilot-control", shardId: "zone:raven") });
|
||||
|
||||
var aetheria = verse.Use(context => new AetheriaGeneratedFacade(context));
|
||||
await aetheria.Entity(ravenId).Pilot.MoveAsync(CultVec2.Right);
|
||||
await aetheria.Entity(ravenId).Pilot.MoveAsync(new float2(1f, 0f));
|
||||
```
|
||||
|
||||
```ts
|
||||
|
|
@ -177,7 +177,7 @@ let viewport = aetheria
|
|||
.zone(zone_id)
|
||||
.objects()
|
||||
.visible_to(&controlled_units)
|
||||
.within(CultRect::from_bounds(-400.0, -250.0, 400.0, 250.0))
|
||||
.within(rect::new(-400.0, -250.0, 400.0, 250.0))
|
||||
.query()
|
||||
.await?;
|
||||
```
|
||||
|
|
|
|||
|
|
@ -407,8 +407,8 @@ var view = new CultMeshNativeSliceViewDescriptor(
|
|||
rowCount,
|
||||
new[]
|
||||
{
|
||||
CultMeshNativeSliceColumn.For<CultVec2>("position"),
|
||||
CultMeshNativeSliceColumn.For<CultVec2>("velocity")
|
||||
CultMeshNativeSliceColumn.For<float2>("position"),
|
||||
CultMeshNativeSliceColumn.For<float2>("velocity")
|
||||
},
|
||||
new CultMeshRouteHint(CultMeshLocalityKind.SharedMemory));
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in a new issue