-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathReplicatedEntityRegistry.cs
More file actions
199 lines (169 loc) · 5.8 KB
/
ReplicatedEntityRegistry.cs
File metadata and controls
199 lines (169 loc) · 5.8 KB
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
using System;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public abstract class ReplicatedEntityFactory : ScriptableObjectRegistryEntry
{
public abstract Entity Create(EntityManager entityManager, int predictingPlayerId);
}
[CreateAssetMenu(fileName = "ReplicatedEntityRegistry",
menuName = "FPS Sample/ReplicatedEntity/ReplicatedEntityRegistry")]
public class ReplicatedEntityRegistry : RegistryBase
{
[Serializable]
public class Entry
{
// Each entry has either a asset reference or factory. Never both
public WeakAssetReference prefab = new WeakAssetReference();
public ReplicatedEntityFactory factory;
}
public List<Entry> entries = new List<Entry>();
#if UNITY_EDITOR
public int GetId(string guid)
{
if (guid == null || guid == "")
return -1;
for (int i = 0; i < entries.Count; i++)
{
if (entries[i].prefab.guid == guid)
return i;
}
return -1;
}
public int GetId(ReplicatedEntityFactory factory)
{
if (factory == null)
return -1;
for (int i = 0; i < entries.Count; i++)
{
if (entries[i].factory == factory)
return i;
}
return -1;
}
public void ClearAtId(int index)
{
entries[index].prefab.guid = "";
entries[index].factory = null;
EditorUtility.SetDirty(this);
}
public int FindFreeId()
{
for (var i = 0; i < entries.Count; i++)
{
if (entries[i].prefab.guid == "" && entries[i].factory == null)
return i;
}
var index = entries.Count;
entries.Add(new Entry());
return index;
}
public void SetPrefab(int registryId, string guid)
{
GameDebug.Assert(entries[registryId].prefab.guid == "","GUID already set");
GameDebug.Assert(entries[registryId].factory == null,"Factory already set");
entries[registryId].prefab.guid = guid;
EditorUtility.SetDirty(this);
}
public void SetFactory(int registryId, ReplicatedEntityFactory factory)
{
GameDebug.Assert(entries[registryId].prefab.guid == "","GUID already set");
GameDebug.Assert(entries[registryId].factory == null,"Factory already set");
entries[registryId].factory = factory;
EditorUtility.SetDirty(this);
}
public static ReplicatedEntityRegistry GetReplicatedEntityRegistry()
{
var registryGuids = AssetDatabase.FindAssets("t:ReplicatedEntityRegistry");
if (registryGuids == null || registryGuids.Length == 0)
{
GameDebug.LogError("Failed to find ReplicatedEntityRegistry");
return null;
}
if (registryGuids.Length > 1)
{
GameDebug.LogError("There should only be one ReplicatedEntityRegistry in project");
return null;
}
var guid = registryGuids[0];
var registryPath = AssetDatabase.GUIDToAssetPath(guid);
var registry = AssetDatabase.LoadAssetAtPath<ReplicatedEntityRegistry>(registryPath);
return registry;
}
public override void GetSingleAssetGUIDs(List<string> guids, bool serverBuild)
{
foreach (var entry in entries)
{
if (entry.factory != null)
continue;
if (entry.prefab.guid == "")
continue;
guids.Add(entry.prefab.guid);
}
}
public override bool Verify()
{
var verified = true;
for (var i = 0; i < entries.Count; i++)
{
var entry = entries[i];
if (entry.prefab.guid == "" && entry.factory == null)
{
Debug.Log("Entry:" + i + " is free");
continue;
}
if (entry.prefab.guid != "" && entry.factory != null)
{
Debug.Log("Entry:" + i + " registered with both prefab and factory");
verified = false;
continue;
}
if (entry.factory != null)
{
if (entry.factory.registryId != i)
{
Debug.Log(entry.factory + " - Index wrong. Registered as id:" + i + " but registryId is:" +
entry.factory.registryId);
verified = false;
continue;
}
}
else
{
var p = AssetDatabase.GUIDToAssetPath(entry.prefab.guid);
if (p == null || p == "")
{
Debug.Log("Cant find path for guid:" + entry.prefab.guid);
verified = false;
continue;
}
var go = AssetDatabase.LoadAssetAtPath<GameObject>(p);
if (go == null)
{
Debug.Log("Cant load asset for guid:" + entry.prefab.guid + " path:" + p);
verified = false;
continue;
}
var replicatedEntity = go.GetComponent<ReplicatedEntity>();
if (replicatedEntity == null)
{
Debug.Log(go + " has no ReplicatedEntity component");
verified = false;
continue;
}
if (replicatedEntity.registryId != i)
{
Debug.Log(go + "Id wrong. Registered as:" + i + " but registryId is:" +
replicatedEntity.registryId);
verified = false;
continue;
}
}
}
return verified;
}
#endif
}