Unity.Mathematics Converters and Galaxy Upload
This commit is contained in:
parent
1df670351b
commit
433a7b4ae4
24 changed files with 910 additions and 351 deletions
|
|
@ -8,6 +8,8 @@ public class DatabaseCache
|
|||
{
|
||||
public event Action<DatabaseEntry> OnDataUpdateLocal;
|
||||
public event Action<DatabaseEntry> OnDataUpdateRemote;
|
||||
public event Action<DatabaseEntry> OnDataDeleteLocal;
|
||||
|
||||
|
||||
public Action<string> Logger = Console.WriteLine;
|
||||
|
||||
|
|
@ -65,4 +67,15 @@ public class DatabaseCache
|
|||
{
|
||||
return _types[typeof(T)].Values.Cast<T>();
|
||||
}
|
||||
|
||||
public void Delete(DatabaseEntry entry)
|
||||
{
|
||||
_entries.Remove(entry.ID);
|
||||
foreach (var type in _types.Values)
|
||||
{
|
||||
type.Remove(entry.ID);
|
||||
}
|
||||
|
||||
OnDataDeleteLocal?.Invoke(entry);
|
||||
}
|
||||
}
|
||||
147
Assets/Scripts/ServerShared/JsonConverters.cs
Normal file
147
Assets/Scripts/ServerShared/JsonConverters.cs
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class MathJsonConverter : JsonConverter
|
||||
{
|
||||
|
||||
private readonly Dictionary<Type, Action<JsonWriter, object>> _writers;
|
||||
private readonly Dictionary<Type, Func<JArray, object>> _readers;
|
||||
|
||||
public MathJsonConverter()
|
||||
{
|
||||
_writers = GenerateWriters();
|
||||
_readers = GenerateReaders();
|
||||
}
|
||||
|
||||
private Dictionary<Type, Action<JsonWriter, object>> GenerateWriters()
|
||||
{
|
||||
var writers = new Dictionary<Type, Action<JsonWriter, object>>();
|
||||
|
||||
writers[typeof(float2)] = (writer, o) =>
|
||||
{
|
||||
if (!(o is float2 v))
|
||||
throw new JsonReaderException();
|
||||
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(v.x);
|
||||
writer.WriteValue(v.y);
|
||||
writer.WriteEndArray();
|
||||
};
|
||||
|
||||
writers[typeof(float3)] = (writer, o) =>
|
||||
{
|
||||
if (!(o is float3 v))
|
||||
throw new JsonReaderException();
|
||||
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(v.x);
|
||||
writer.WriteValue(v.y);
|
||||
writer.WriteValue(v.z);
|
||||
writer.WriteEndArray();
|
||||
};
|
||||
|
||||
writers[typeof(float4)] = (writer, o) =>
|
||||
{
|
||||
if (!(o is float4 v))
|
||||
throw new JsonReaderException();
|
||||
|
||||
writer.WriteStartArray();
|
||||
writer.WriteValue(v.x);
|
||||
writer.WriteValue(v.y);
|
||||
writer.WriteValue(v.z);
|
||||
writer.WriteValue(v.w);
|
||||
writer.WriteEndArray();
|
||||
};
|
||||
|
||||
return writers;
|
||||
}
|
||||
|
||||
private Dictionary<Type, Func<JArray, object>> GenerateReaders()
|
||||
{
|
||||
var readers = new Dictionary<Type, Func<JArray, object>>();
|
||||
|
||||
readers[typeof(float2)] = array =>
|
||||
{
|
||||
if (array.Count < 2)
|
||||
{
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {typeof(float2)} from json, expected a json array with 2 elements");
|
||||
}
|
||||
|
||||
return float2(
|
||||
array[0].ToObject<float>(),
|
||||
array[1].ToObject<float>());
|
||||
};
|
||||
|
||||
readers[typeof(float3)] = array =>
|
||||
{
|
||||
if (array.Count < 3)
|
||||
{
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {typeof(float3)} from json, expected a json array with 3 elements");
|
||||
}
|
||||
|
||||
return float3(
|
||||
array[0].ToObject<float>(),
|
||||
array[1].ToObject<float>(),
|
||||
array[2].ToObject<float>());
|
||||
};
|
||||
|
||||
readers[typeof(float4)] = array =>
|
||||
{
|
||||
if (array.Count < 4)
|
||||
{
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {typeof(float4)} from json, expected a json array with 4 elements");
|
||||
}
|
||||
|
||||
return float4(
|
||||
array[0].ToObject<float>(),
|
||||
array[1].ToObject<float>(),
|
||||
array[2].ToObject<float>(),
|
||||
array[3].ToObject<float>());
|
||||
};
|
||||
|
||||
return readers;
|
||||
}
|
||||
|
||||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
||||
{
|
||||
if (_writers.TryGetValue(value.GetType(), out var serializeAction))
|
||||
serializeAction(writer, value);
|
||||
else
|
||||
throw new JsonReaderException($"Invalid type for ValueConverter:{value.GetType()}");
|
||||
}
|
||||
|
||||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
||||
{
|
||||
if (_readers.TryGetValue(objectType, out var deserializeFunc))
|
||||
{
|
||||
// Convert token from reader to array for deserialize function
|
||||
var token = JToken.ReadFrom(reader);
|
||||
if (!(token is JArray asArray))
|
||||
throw new JsonReaderException(
|
||||
$"Could not read {objectType} from json, expected a json array but got {token.Type}");
|
||||
|
||||
return deserializeFunc(asArray);
|
||||
}
|
||||
|
||||
throw new JsonReaderException($"Invalid type for ValueConverter:{objectType}");
|
||||
}
|
||||
|
||||
public override bool CanConvert(Type objectType)
|
||||
{
|
||||
//If the dictionary contains the key, the type can be handled by this class
|
||||
return _writers.ContainsKey(objectType);
|
||||
}
|
||||
|
||||
//Enable this JSON Converter for reading as well as writing values
|
||||
public override bool CanRead => true;
|
||||
public override bool CanWrite => true;
|
||||
}
|
||||
11
Assets/Scripts/ServerShared/JsonConverters.cs.meta
Normal file
11
Assets/Scripts/ServerShared/JsonConverters.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fafad9d553198e942a5501ea1472ec9c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
144
Assets/Scripts/ServerShared/MathFormatters.cs
Normal file
144
Assets/Scripts/ServerShared/MathFormatters.cs
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
public class Float2Formatter : MessagePack.Formatters.IMessagePackFormatter<float2>
|
||||
{
|
||||
public void Serialize(ref MessagePackWriter writer, float2 value, MessagePackSerializerOptions options)
|
||||
{
|
||||
writer.WriteArrayHeader(2);
|
||||
writer.Write(value.x);
|
||||
writer.Write(value.y);
|
||||
}
|
||||
|
||||
public float2 Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("typecode is null, struct not supported");
|
||||
}
|
||||
|
||||
var length = reader.ReadArrayHeader();
|
||||
var x = default(float);
|
||||
var y = default(float);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var key = i;
|
||||
switch (key)
|
||||
{
|
||||
case 0:
|
||||
x = reader.ReadSingle();
|
||||
break;
|
||||
case 1:
|
||||
y = reader.ReadSingle();
|
||||
break;
|
||||
default:
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var result = float2(x, y);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Float3Formatter : MessagePack.Formatters.IMessagePackFormatter<float3>
|
||||
{
|
||||
public void Serialize(ref MessagePackWriter writer, float3 value, MessagePackSerializerOptions options)
|
||||
{
|
||||
writer.WriteArrayHeader(3);
|
||||
writer.Write(value.x);
|
||||
writer.Write(value.y);
|
||||
writer.Write(value.z);
|
||||
}
|
||||
|
||||
public float3 Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("typecode is null, struct not supported");
|
||||
}
|
||||
|
||||
var length = reader.ReadArrayHeader();
|
||||
var x = default(float);
|
||||
var y = default(float);
|
||||
var z = default(float);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var key = i;
|
||||
switch (key)
|
||||
{
|
||||
case 0:
|
||||
x = reader.ReadSingle();
|
||||
break;
|
||||
case 1:
|
||||
y = reader.ReadSingle();
|
||||
break;
|
||||
case 2:
|
||||
z = reader.ReadSingle();
|
||||
break;
|
||||
default:
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var result = float3(x, y, z);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public class Float4Formatter : MessagePack.Formatters.IMessagePackFormatter<float4>
|
||||
{
|
||||
public void Serialize(ref MessagePackWriter writer, float4 value, MessagePackSerializerOptions options)
|
||||
{
|
||||
writer.WriteArrayHeader(4);
|
||||
writer.Write(value.x);
|
||||
writer.Write(value.y);
|
||||
writer.Write(value.z);
|
||||
writer.Write(value.w);
|
||||
}
|
||||
|
||||
public float4 Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
|
||||
{
|
||||
if (reader.IsNil)
|
||||
{
|
||||
throw new InvalidOperationException("typecode is null, struct not supported");
|
||||
}
|
||||
|
||||
var length = reader.ReadArrayHeader();
|
||||
var x = default(float);
|
||||
var y = default(float);
|
||||
var z = default(float);
|
||||
var w = default(float);
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
var key = i;
|
||||
switch (key)
|
||||
{
|
||||
case 0:
|
||||
x = reader.ReadSingle();
|
||||
break;
|
||||
case 1:
|
||||
y = reader.ReadSingle();
|
||||
break;
|
||||
case 2:
|
||||
z = reader.ReadSingle();
|
||||
break;
|
||||
case 3:
|
||||
w = reader.ReadSingle();
|
||||
break;
|
||||
default:
|
||||
reader.Skip();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var result = float4(x, y, z, w);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ServerShared/MathFormatters.cs.meta
Normal file
11
Assets/Scripts/ServerShared/MathFormatters.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 02bc8e83a7f1cf54d94ebebcd3272a0d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
73
Assets/Scripts/ServerShared/MathResolver.cs
Normal file
73
Assets/Scripts/ServerShared/MathResolver.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using MessagePack.Formatters;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
|
||||
public class MathResolver : IFormatterResolver
|
||||
{
|
||||
public static readonly MathResolver Instance = new MathResolver();
|
||||
|
||||
private MathResolver()
|
||||
{
|
||||
}
|
||||
|
||||
public IMessagePackFormatter<T> GetFormatter<T>()
|
||||
{
|
||||
return FormatterCache<T>.Formatter;
|
||||
}
|
||||
|
||||
private static class FormatterCache<T>
|
||||
{
|
||||
public static readonly IMessagePackFormatter<T> Formatter;
|
||||
|
||||
static FormatterCache()
|
||||
{
|
||||
Formatter = (IMessagePackFormatter<T>)MathResolverResolverGetFormatterHelper.GetFormatter(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class MathResolverResolverGetFormatterHelper
|
||||
{
|
||||
private static readonly Dictionary<Type, object> FormatterMap = new Dictionary<Type, object>()
|
||||
{
|
||||
// standard
|
||||
{ typeof(float2), new Float2Formatter() },
|
||||
{ typeof(float3), new Float3Formatter() },
|
||||
{ typeof(float4), new Float4Formatter() },
|
||||
{ typeof(float2?), new StaticNullableFormatter<float2>(new Float2Formatter()) },
|
||||
{ typeof(float3?), new StaticNullableFormatter<float3>(new Float3Formatter()) },
|
||||
{ typeof(float4?), new StaticNullableFormatter<float4>(new Float4Formatter()) },
|
||||
|
||||
// standard + array
|
||||
{ typeof(float2[]), new ArrayFormatter<float2>() },
|
||||
{ typeof(float3[]), new ArrayFormatter<float3>() },
|
||||
{ typeof(float4[]), new ArrayFormatter<float4>() },
|
||||
{ typeof(float2?[]), new ArrayFormatter<float2?>() },
|
||||
{ typeof(float3?[]), new ArrayFormatter<float3?>() },
|
||||
{ typeof(float4?[]), new ArrayFormatter<float4?>() },
|
||||
|
||||
// standard + list
|
||||
{ typeof(List<float2>), new ListFormatter<float2>() },
|
||||
{ typeof(List<float3>), new ListFormatter<float3>() },
|
||||
{ typeof(List<float4>), new ListFormatter<float4>() },
|
||||
{ typeof(List<float2?>), new ListFormatter<float2?>() },
|
||||
{ typeof(List<float3?>), new ListFormatter<float3?>() },
|
||||
{ typeof(List<float4?>), new ListFormatter<float4?>() },
|
||||
};
|
||||
|
||||
internal static object GetFormatter(Type t)
|
||||
{
|
||||
object formatter;
|
||||
if (FormatterMap.TryGetValue(t, out formatter))
|
||||
{
|
||||
return formatter;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ServerShared/MathResolver.cs.meta
Normal file
11
Assets/Scripts/ServerShared/MathResolver.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 80366c67f842eaf4dba339ed553e447c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -3,6 +3,7 @@ using System.Collections;
|
|||
using System.Collections.Generic;
|
||||
using MessagePack;
|
||||
using Newtonsoft.Json;
|
||||
using Unity.Mathematics;
|
||||
|
||||
[MessagePackObject]
|
||||
[JsonObject(MemberSerialization.OptIn)]
|
||||
|
|
@ -11,19 +12,19 @@ public class ZoneData : DatabaseEntry, INamedEntry
|
|||
[JsonProperty("name")] [Key(1)]
|
||||
public string Name;
|
||||
|
||||
[JsonProperty("radius")] [Key(2)]
|
||||
public float Radius = 500;
|
||||
[JsonProperty("position")] [Key(2)]
|
||||
public float2 Position = 500;
|
||||
|
||||
[JsonProperty("wormholes")] [Key(4)]
|
||||
[JsonProperty("wormholes")] [Key(3)]
|
||||
public List<Guid> Wormholes = new List<Guid>();
|
||||
|
||||
[JsonProperty("stations")] [Key(3)]
|
||||
[JsonProperty("stations")] [Key(4)]
|
||||
public List<Guid> Stations = new List<Guid>();
|
||||
|
||||
[JsonProperty("planets")] [Key(4)]
|
||||
[JsonProperty("planets")] [Key(5)]
|
||||
public List<Guid> Planets = new List<Guid>();
|
||||
|
||||
[JsonProperty("orbits")] [Key(5)]
|
||||
[JsonProperty("orbits")] [Key(6)]
|
||||
public List<Guid> Orbits = new List<Guid>();
|
||||
|
||||
[IgnoreMember] public string EntryName
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ public class ZoneGenerator
|
|||
// public float GasGiantMass = 2000;
|
||||
// public float RadiusPower = 1.75f;
|
||||
|
||||
public static Planet[] GenerateEntities(GameContext context, ZoneData data, float mass)
|
||||
public static Planet[] GenerateEntities(GameContext context, ZoneData data, float mass, float radius)
|
||||
{
|
||||
var random = new Random();
|
||||
random.InitState(unchecked((uint)data.Name.GetHashCode()));
|
||||
|
|
@ -34,7 +34,7 @@ public class ZoneGenerator
|
|||
Random = random,
|
||||
Context = context,
|
||||
Mass = mass,
|
||||
ChildDistanceMaximum = data.Radius,
|
||||
ChildDistanceMaximum = radius,
|
||||
ChildDistanceMinimum = context.PlanetRadius(mass)
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue