summaryrefslogtreecommitdiff
path: root/ROUNDS/Photon.Compression.Internal/SyncVarBaseAttribute.cs
blob: f7b3531d7a44d90e6312952b0935c1c958db6a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Photon.Compression.Internal;

[Serializable]
[AttributeUsage(AttributeTargets.Field)]
public abstract class SyncVarBaseAttribute : Attribute
{
	public SyncAs syncAs;

	public KeyRate keyRate;

	public string applyCallback;

	public string snapshotCallback;

	public SetValueTiming setValueTiming = SetValueTiming.AfterCallback;

	public bool interpolate;

	public int bitCount = -1;

	public virtual void Initialize(Type primitiveType)
	{
		if (bitCount <= -1)
		{
			bitCount = GetDefaultBitCount(primitiveType);
		}
	}

	public virtual int GetDefaultBitCount(Type fieldType)
	{
		if (fieldType == typeof(byte) || fieldType == typeof(sbyte))
		{
			return 8;
		}
		if (fieldType == typeof(ushort) || fieldType == typeof(short) || fieldType == typeof(char))
		{
			return 16;
		}
		if (fieldType == typeof(uint) || fieldType == typeof(int) || fieldType == typeof(float))
		{
			return 32;
		}
		if (fieldType == typeof(ulong) || fieldType == typeof(long) || fieldType == typeof(double))
		{
			return 64;
		}
		if (fieldType == typeof(bool))
		{
			return 1;
		}
		if (fieldType == typeof(Vector3))
		{
			return 32;
		}
		if (fieldType == typeof(Vector2))
		{
			return 32;
		}
		return 0;
	}

	public virtual int GetMaxBits(Type fieldType)
	{
		if (fieldType == typeof(byte) || fieldType == typeof(sbyte))
		{
			return 8;
		}
		if (fieldType == typeof(ushort) || fieldType == typeof(short) || fieldType == typeof(char))
		{
			return 16;
		}
		if (fieldType == typeof(uint) || fieldType == typeof(int) || fieldType == typeof(float))
		{
			return 32;
		}
		if (fieldType == typeof(ulong) || fieldType == typeof(long) || fieldType == typeof(double))
		{
			return 64;
		}
		if (fieldType == typeof(bool))
		{
			return 1;
		}
		if (fieldType == typeof(Vector3))
		{
			return 96;
		}
		if (fieldType == typeof(Vector2))
		{
			return 64;
		}
		if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>)))
		{
			Debug.LogWarning("Can't get max bits needed for List<> types, as they are variable. " + fieldType.Name);
			return 2048;
		}
		Debug.LogWarning("Can't get bits needed for unsupported types. " + fieldType.Name);
		return 2048;
	}

	public bool IsKeyframe(int frameId)
	{
		if (keyRate == KeyRate.Every)
		{
			return true;
		}
		if (keyRate == KeyRate.Never)
		{
			return false;
		}
		if (frameId % (int)keyRate == 0)
		{
			return true;
		}
		return false;
	}

	public bool IsForced(int frameId, SerializationFlags writeFlags)
	{
		if (syncAs == SyncAs.Trigger)
		{
			return false;
		}
		if (keyRate == KeyRate.Every)
		{
			return true;
		}
		if ((writeFlags & SerializationFlags.Force) != 0)
		{
			return true;
		}
		if (keyRate == KeyRate.Never && (writeFlags & SerializationFlags.NewConnection) != 0)
		{
			return true;
		}
		if (keyRate != 0 && frameId % (int)keyRate == 0)
		{
			return true;
		}
		return false;
	}

	public bool IsForcedClass<T>(int frameId, T value, T prevValue, SerializationFlags writeFlags) where T : class
	{
		if (syncAs == SyncAs.Trigger)
		{
			Debug.LogError("Reference type " + typeof(T).Name + " cannot be set to SyncAs.Trigger. This PackAttribute setting only applies to structs.");
			return true;
		}
		if (keyRate == KeyRate.Every)
		{
			return true;
		}
		if ((writeFlags & SerializationFlags.Force) != 0)
		{
			return true;
		}
		if (keyRate == KeyRate.Never && (writeFlags & SerializationFlags.NewConnection) != 0)
		{
			return true;
		}
		if (keyRate != 0 && frameId % (int)keyRate == 0)
		{
			return true;
		}
		if (!value.Equals(prevValue))
		{
			return true;
		}
		return false;
	}

	public bool IsForced<T>(int frameId, T value, T prevValue, SerializationFlags writeFlags) where T : struct
	{
		if (syncAs == SyncAs.Trigger)
		{
			return !value.Equals(new T());
		}
		if (keyRate == KeyRate.Every)
		{
			return true;
		}
		if ((writeFlags & SerializationFlags.Force) != 0)
		{
			return true;
		}
		if (keyRate == KeyRate.Never && (writeFlags & SerializationFlags.NewConnection) != 0)
		{
			return true;
		}
		if (keyRate != 0 && frameId % (int)keyRate == 0)
		{
			return true;
		}
		if (!value.Equals(prevValue))
		{
			return true;
		}
		return false;
	}
}