Skip to content

Commit df9a4b4

Browse files
committed
feat: Added Vector X Byte structs and Organized
1 parent 1aa5a33 commit df9a4b4

21 files changed

+798
-6
lines changed

Assets/Mirror/Core/Tools/CustomStructs.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

Assets/Mirror/Core/Tools/CustomStructs/Half.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#pragma warning disable CS0659 // 'Vector2Byte' overrides Object.Equals(object o) but does not override Object.GetHashCode()
2+
#pragma warning disable CS0661 // 'Vector2Byte' defines operator == or operator != but does not override Object.GetHashCode()
3+
4+
// Vector2Byte by mischa (based on game engine project)
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Mirror
9+
{
10+
public struct Vector2Byte
11+
{
12+
public byte x;
13+
public byte y;
14+
15+
public static readonly Vector2Byte zero = new Vector2Byte(0, 0);
16+
public static readonly Vector2Byte one = new Vector2Byte(1, 1);
17+
public static readonly Vector2Byte left = new Vector2Byte(0, 1);
18+
public static readonly Vector2Byte right = new Vector2Byte(1, 0);
19+
public static readonly Vector2Byte up = new Vector2Byte(0, 1);
20+
public static readonly Vector2Byte down = new Vector2Byte(0, 0);
21+
22+
// constructor /////////////////////////////////////////////////////////
23+
public Vector2Byte(byte x, byte y)
24+
{
25+
this.x = x;
26+
this.y = y;
27+
}
28+
29+
// operators ///////////////////////////////////////////////////////////
30+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31+
public static Vector2Byte operator +(Vector2Byte a, Vector2Byte b) =>
32+
new Vector2Byte((byte)(a.x + b.x), (byte)(a.y + b.y));
33+
34+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
public static Vector2Byte operator -(Vector2Byte a, Vector2Byte b) =>
36+
new Vector2Byte((byte)(a.x - b.x), (byte)(a.y - b.y));
37+
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39+
public static Vector2Byte operator *(Vector2Byte a, byte n) =>
40+
new Vector2Byte((byte)(a.x * n), (byte)(a.y * n));
41+
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public static Vector2Byte operator *(byte n, Vector2Byte a) =>
44+
new Vector2Byte((byte)(a.x * n), (byte)(a.y * n));
45+
46+
// == returns true if exactly equal
47+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
48+
public static bool operator ==(Vector2Byte a, Vector2Byte b) =>
49+
a.x == b.x && a.y == b.y;
50+
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
public static bool operator !=(Vector2Byte a, Vector2Byte b) => !(a == b);
53+
54+
// [i] component index. useful for iterating all components etc.
55+
public byte this[int index]
56+
{
57+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
58+
get
59+
{
60+
switch (index)
61+
{
62+
case 0: return x;
63+
case 1: return y;
64+
default: throw new IndexOutOfRangeException($"Vector2Byte[{index}] out of range.");
65+
}
66+
}
67+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
68+
set
69+
{
70+
switch (index)
71+
{
72+
case 0:
73+
x = value;
74+
break;
75+
case 1:
76+
y = value;
77+
break;
78+
default: throw new IndexOutOfRangeException($"Vector2Byte[{index}] out of range.");
79+
}
80+
}
81+
}
82+
83+
// instance functions //////////////////////////////////////////////////
84+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
85+
public override string ToString() => $"({x} {y})";
86+
87+
// equality ////////////////////////////////////////////////////////////
88+
// implement Equals & HashCode explicitly for performance.
89+
// calling .Equals (instead of "==") checks for exact equality.
90+
// (API compatibility)
91+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92+
public bool Equals(Vector2Byte other) =>
93+
x == other.x && y == other.y;
94+
95+
// Equals(object) can reuse Equals(Vector2Byte)
96+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97+
public override bool Equals(object other) =>
98+
other is Vector2Byte vector2 && Equals(vector2);
99+
100+
#if UNITY_2021_3_OR_NEWER
101+
// Unity 2019/2020 don't have HashCode.Combine yet.
102+
// this is only to avoid reflection. without defining, it works too.
103+
// default generated by rider
104+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
105+
public override int GetHashCode() => HashCode.Combine(x, y);
106+
#endif
107+
}
108+
}

Assets/Mirror/Core/Tools/CustomStructs/Vector2Byte.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#pragma warning disable CS0659 // 'Vector2SByte' overrides Object.Equals(object o) but does not override Object.GetHashCode()
2+
#pragma warning disable CS0661 // 'Vector2SByte' defines operator == or operator != but does not override Object.GetHashCode()
3+
4+
// Vector2SByte by mischa (based on game engine project)
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Mirror
9+
{
10+
public struct Vector2SByte
11+
{
12+
public sbyte x;
13+
public sbyte y;
14+
15+
public static readonly Vector2SByte zero = new Vector2SByte(0, 0);
16+
public static readonly Vector2SByte one = new Vector2SByte(1, 1);
17+
public static readonly Vector2SByte left = new Vector2SByte(-1, 0);
18+
public static readonly Vector2SByte right = new Vector2SByte(1, 0);
19+
public static readonly Vector2SByte up = new Vector2SByte(0, 1);
20+
public static readonly Vector2SByte down = new Vector2SByte(0, -1);
21+
22+
// constructor /////////////////////////////////////////////////////////
23+
public Vector2SByte(sbyte x, sbyte y)
24+
{
25+
this.x = x;
26+
this.y = y;
27+
}
28+
29+
// operators ///////////////////////////////////////////////////////////
30+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31+
public static Vector2SByte operator +(Vector2SByte a, Vector2SByte b) =>
32+
new Vector2SByte((sbyte)(a.x + b.x), (sbyte)(a.y + b.y));
33+
34+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
public static Vector2SByte operator -(Vector2SByte a, Vector2SByte b) =>
36+
new Vector2SByte((sbyte)(a.x - b.x), (sbyte)(a.y - b.y));
37+
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39+
public static Vector2SByte operator -(Vector2SByte v) =>
40+
new Vector2SByte((sbyte)-v.x, (sbyte)-v.y);
41+
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public static Vector2SByte operator *(Vector2SByte a, sbyte n) =>
44+
new Vector2SByte((sbyte)(a.x * n), (sbyte)(a.y * n));
45+
46+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47+
public static Vector2SByte operator *(sbyte n, Vector2SByte a) =>
48+
new Vector2SByte((sbyte)(a.x * n), (sbyte)(a.y * n));
49+
50+
// == returns true if exactly equal
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
public static bool operator ==(Vector2SByte a, Vector2SByte b) =>
53+
a.x == b.x && a.y == b.y;
54+
55+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
56+
public static bool operator !=(Vector2SByte a, Vector2SByte b) => !(a == b);
57+
58+
// [i] component index. useful for iterating all components etc.
59+
public sbyte this[int index]
60+
{
61+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
62+
get
63+
{
64+
switch (index)
65+
{
66+
case 0: return x;
67+
case 1: return y;
68+
default: throw new IndexOutOfRangeException($"Vector2SByte[{index}] out of range.");
69+
}
70+
}
71+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
72+
set
73+
{
74+
switch (index)
75+
{
76+
case 0:
77+
x = value;
78+
break;
79+
case 1:
80+
y = value;
81+
break;
82+
default: throw new IndexOutOfRangeException($"Vector2SByte[{index}] out of range.");
83+
}
84+
}
85+
}
86+
87+
// instance functions //////////////////////////////////////////////////
88+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
89+
public override string ToString() => $"({x} {y})";
90+
91+
// equality ////////////////////////////////////////////////////////////
92+
// implement Equals & HashCode explicitly for performance.
93+
// calling .Equals (instead of "==") checks for exact equality.
94+
// (API compatibility)
95+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
96+
public bool Equals(Vector2SByte other) =>
97+
x == other.x && y == other.y;
98+
99+
// Equals(object) can reuse Equals(Vector2SByte)
100+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
101+
public override bool Equals(object other) =>
102+
other is Vector2SByte vector2 && Equals(vector2);
103+
104+
#if UNITY_2021_3_OR_NEWER
105+
// Unity 2019/2020 don't have HashCode.Combine yet.
106+
// this is only to avoid reflection. without defining, it works too.
107+
// default generated by rider
108+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109+
public override int GetHashCode() => HashCode.Combine(x, y);
110+
#endif
111+
}
112+
}

Assets/Mirror/Core/Tools/CustomStructs/Vector2SByte.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#pragma warning disable CS0659 // 'Vector3Byte' overrides Object.Equals(object o) but does not override Object.GetHashCode()
2+
#pragma warning disable CS0661 // 'Vector3Byte' defines operator == or operator != but does not override Object.GetHashCode()
3+
4+
// Vector3Byte by mischa (based on game engine project)
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Mirror
9+
{
10+
public struct Vector3Byte
11+
{
12+
public byte x;
13+
public byte y;
14+
public byte z;
15+
16+
public static readonly Vector3Byte zero = new Vector3Byte(0, 0, 0);
17+
public static readonly Vector3Byte one = new Vector3Byte(1, 1, 1);
18+
public static readonly Vector3Byte forward = new Vector3Byte(0, 0, 1);
19+
public static readonly Vector3Byte back = new Vector3Byte(0, 0, 0);
20+
public static readonly Vector3Byte left = new Vector3Byte(0, 0, 0);
21+
public static readonly Vector3Byte right = new Vector3Byte(1, 0, 0);
22+
public static readonly Vector3Byte up = new Vector3Byte(0, 1, 0);
23+
public static readonly Vector3Byte down = new Vector3Byte(0, 0, 0);
24+
25+
// constructor /////////////////////////////////////////////////////////
26+
public Vector3Byte(byte x, byte y, byte z)
27+
{
28+
this.x = x;
29+
this.y = y;
30+
this.z = z;
31+
}
32+
33+
// operators ///////////////////////////////////////////////////////////
34+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
public static Vector3Byte operator +(Vector3Byte a, Vector3Byte b) =>
36+
new Vector3Byte((byte)(a.x + b.x), (byte)(a.y + b.y), (byte)(a.z + b.z));
37+
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39+
public static Vector3Byte operator -(Vector3Byte a, Vector3Byte b) =>
40+
new Vector3Byte((byte)(a.x - b.x), (byte)(a.y - b.y), (byte)(a.z - b.z));
41+
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public static Vector3Byte operator *(Vector3Byte a, byte n) =>
44+
new Vector3Byte((byte)(a.x * n), (byte)(a.y * n), (byte)(a.z * n));
45+
46+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
47+
public static Vector3Byte operator *(byte n, Vector3Byte a) =>
48+
new Vector3Byte((byte)(a.x * n), (byte)(a.y * n), (byte)(a.z * n));
49+
50+
// == returns true if exactly equal
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
public static bool operator ==(Vector3Byte a, Vector3Byte b) =>
53+
a.x == b.x &&
54+
a.y == b.y &&
55+
a.z == b.z;
56+
57+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
58+
public static bool operator !=(Vector3Byte a, Vector3Byte b) => !(a == b);
59+
60+
// [i] component index. useful for iterating all components etc.
61+
public byte this[int index]
62+
{
63+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
64+
get
65+
{
66+
switch (index)
67+
{
68+
case 0: return x;
69+
case 1: return y;
70+
case 2: return z;
71+
default: throw new IndexOutOfRangeException($"Vector3Byte[{index}] out of range.");
72+
}
73+
}
74+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
75+
set
76+
{
77+
switch (index)
78+
{
79+
case 0:
80+
x = value;
81+
break;
82+
case 1:
83+
y = value;
84+
break;
85+
case 2:
86+
z = value;
87+
break;
88+
default: throw new IndexOutOfRangeException($"Vector3Byte[{index}] out of range.");
89+
}
90+
}
91+
}
92+
93+
// instance functions //////////////////////////////////////////////////
94+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
95+
public override string ToString() => $"({x} {y} {z})";
96+
97+
// equality ////////////////////////////////////////////////////////////
98+
// implement Equals & HashCode explicitly for performance.
99+
// calling .Equals (instead of "==") checks for exact equality.
100+
// (API compatibility)
101+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
102+
public bool Equals(Vector3Byte other) =>
103+
x == other.x && y == other.y && z == other.z;
104+
105+
// Equals(object) can reuse Equals(Vector3Byte)
106+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107+
public override bool Equals(object other) =>
108+
other is Vector3Byte vector3 && Equals(vector3);
109+
110+
#if UNITY_2021_3_OR_NEWER
111+
// Unity 2019/2020 don't have HashCode.Combine yet.
112+
// this is only to avoid reflection. without defining, it works too.
113+
// default generated by rider
114+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
115+
public override int GetHashCode() => HashCode.Combine(x, y, z);
116+
#endif
117+
}
118+
}

0 commit comments

Comments
 (0)