Skip to content

Commit

Permalink
fix: PrefsBounds/PrefsBoundsInt is not sync
Browse files Browse the repository at this point in the history
  • Loading branch information
fuqunaga committed Apr 24, 2023
1 parent 56e9e0c commit a082365
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Assets/Tests/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Tests/Editor/PrefsGUISyncForMirror.Tests.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "PrefsGUISyncForMirror.Tests.Editor",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"PrefsGUISyncForMirror"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions Assets/Tests/Editor/TestBytesConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Xml.Serialization;
using NUnit.Framework;
using UnityEngine;

namespace PrefsGUI.Sync.Tests.Editor
{
public class TestBytesConverter
{

[TestCaseSource(nameof(GetCsharpPrimitives), Category = "C# Primitives")]
[TestCaseSource(nameof(GetUnityPrimitives), Category = "Unity Primitives")]
public void TestPrimitives<T>(T value)
{
var bytes = Array.Empty<byte>();
BytesConverter.ValueToBytes(value, ref bytes);
var valueConverted = BytesConverter.BytesToValue<T>(bytes);

if (typeof(T).IsValueType)
{
Assert.AreEqual(value, valueConverted);
}
else
{
var fields = typeof(T).GetFields();
var properties = typeof(T).GetProperties();

foreach (var field in fields)
{
var expected = field.GetValue(value);
var actual = field.GetValue(valueConverted);

Assert.AreEqual(expected, actual, $"field: {field.Name}, expected: {expected}, actual: {actual}");
}

foreach (var property in properties)
{
var expected = property.GetValue(value);
var actual = property.GetValue(valueConverted);

Assert.AreEqual(expected, actual, $"property: {property.Name}, expected: {expected}, actual: {actual}");
}
}
}

private static TestCaseData[] GetCsharpPrimitives()
{
return new TestCaseData[]
{
new(true),
new((byte)1),
new((sbyte)1),
new((char)1),
new((double)1d),
new((float)1f),
new((int)1),
new((uint)1),
new((long)1),
new((ulong)1),
new((short)1),
new((ushort)1),
};
}

private static TestCaseData[] GetUnityPrimitives()
{
return new TestCaseData[]
{
new(Vector2.one),
new(Vector2Int.one),
new(Vector3.one),
new(Vector3Int.one),
new(Vector4.one),
new(Quaternion.identity),
new(Color.white),
new(new Color32(1, 1, 1, 1)),
new(new Rect(1f, 1f, 1f, 1f)),
new(new RectInt(1, 1, 1, 1)),
new(new RectOffset(1, 1, 1, 1)),
new(Matrix4x4.identity),
new(new Plane(Vector3.one, 1f)),
new(new Bounds(Vector3.one, Vector3.one)),
new(new BoundsInt(Vector3Int.one, Vector3Int.one))
};
}
}
}
11 changes: 11 additions & 0 deletions Assets/Tests/Editor/TestBytesConverter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Tests/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "PrefsGUISyncForMirror.Tests",
"name": "PrefsGUISyncForMirror.Tests.Runtime",
"rootNamespace": "",
"references": [
"GUID:436548315e6ca5b499a1008921425bfb",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace PrefsGUI.Sync
{
public static class BytesConverter
public static partial class BytesConverter
{
public static void ValueToBytes<T>(T value, ref byte[] bytes)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using Mirror;
using UnityEngine;

namespace PrefsGUI.Sync
{
public static partial class BytesConverter
{
static BytesConverter()
{
RegisterWriterReaderRectInt();
RegisterWriterReaderRectOffset();
RegisterWriterReaderBounds();
RegisterWriterReaderBoundsInt();
}

private static void RegisterWriterReaderRectInt()
{
RegisterWriterReader(
(writer, value) =>
{
writer.Write(value.x);
writer.Write(value.y);
writer.Write(value.width);
writer.Write(value.height);
},
(reader) => new RectInt
{
x = reader.ReadInt(),
y = reader.ReadInt(),
width = reader.ReadInt(),
height = reader.ReadInt()
});
}


private static void RegisterWriterReaderRectOffset()
{
RegisterWriterReader(
(writer, value) =>
{
writer.Write(value.left);
writer.Write(value.right);
writer.Write(value.top);
writer.Write(value.bottom);
},
(reader) => new RectOffset
{
left = reader.ReadInt(),
right = reader.ReadInt(),
top = reader.ReadInt(),
bottom = reader.ReadInt()
});
}


private static void RegisterWriterReaderBounds()
{
RegisterWriterReader(
(writer, bounds) =>
{
writer.Write(bounds.center);
writer.Write(bounds.extents);
},
(reader) => new Bounds
{
center = reader.ReadVector3(),
extents = reader.ReadVector3()
});
}

private static void RegisterWriterReaderBoundsInt()
{
RegisterWriterReader(
(writer, bounds) =>
{
writer.Write(bounds.position);
writer.Write(bounds.size);
},
(reader) => new BoundsInt
{
position = reader.ReadVector3Int(),
size = reader.ReadVector3Int()
});
}





private static void RegisterWriterReader<T>(Action<NetworkWriter, T> writeFunc, Func<NetworkReader, T> readFunc)
{
Writer<T>.write ??= writeFunc;
Reader<T>.read ??= readFunc;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a082365

Please sign in to comment.