-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: PrefsBounds/PrefsBoundsInt is not sync
- Loading branch information
Showing
17 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
Assets/Tests/Editor/PrefsGUISyncForMirror.Tests.Editor.asmdef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
Assets/Tests/Editor/PrefsGUISyncForMirror.Tests.Editor.asmdef.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../Tests/PrefsGUISyncForMirror.Tests.asmdef → ...refsGUISyncForMirror.Tests.Runtime.asmdef
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
...ges/ga.fuquna.prefsguisyncformirror/Runtime/Utility/BytesConverter_RegisterWriteReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
...a.fuquna.prefsguisyncformirror/Runtime/Utility/BytesConverter_RegisterWriteReader.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.