Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
EntityReference.cs
浏览该文件的文档.
1using System.Globalization;
3
4namespace Game {
5 public struct EntityReference {
12
14
15 public string m_entityReference;
16
17 public string m_componentReference;
18
19 public string ReferenceString {
20 get {
21 if (m_referenceType == ReferenceType.Null) {
22 return "null:";
23 }
24 if (m_referenceType == ReferenceType.Local) {
25 return $"local:{m_componentReference}";
26 }
27 if (m_referenceType == ReferenceType.ByEntityId) {
28 return $"id:{m_entityReference}:{m_componentReference}";
29 }
30 if (m_referenceType == ReferenceType.ByEntityName) {
31 return $"name:{m_entityReference}:{m_componentReference}";
32 }
33 throw new Exception("Unknown entity reference type.");
34 }
35 }
36
37 public static EntityReference Null => default;
38
39 public Entity GetEntity(Entity localEntity, IdToEntityMap idToEntityMap, bool throwIfNotFound) {
40 Entity entity;
41 if (m_referenceType == ReferenceType.Null) {
42 entity = null;
43 }
44 else if (m_referenceType == ReferenceType.Local) {
45 entity = localEntity;
46 }
47 else if (m_referenceType == ReferenceType.ByEntityId) {
48 int id = int.Parse(m_entityReference, CultureInfo.InvariantCulture);
49 entity = localEntity.Project.FindEntity(id);
50 }
51 else {
52 if (m_referenceType != ReferenceType.ByEntityName) {
53 throw new Exception("Unknown entity reference type.");
54 }
55 entity = localEntity.Project.FindSubsystem<SubsystemNames>(true).FindEntityByName(m_entityReference);
56 }
57 if (entity != null) {
58 return entity;
59 }
60 if (throwIfNotFound) {
61 throw new Exception($"Required entity \"{ReferenceString}\" not found.");
62 }
63 return null;
64 }
65
66 public T GetComponent<T>(Entity localEntity, IdToEntityMap idToEntityMap, bool throwIfNotFound) where T : class {
67 Entity entity = GetEntity(localEntity, idToEntityMap, throwIfNotFound);
68 if (entity == null) {
69 return null;
70 }
71 return entity.FindComponent<T>(m_componentReference, throwIfNotFound);
72 }
73
74 public bool IsNullOrEmpty() {
75 if (m_referenceType != 0
76 && (m_referenceType != ReferenceType.Local || !string.IsNullOrEmpty(m_componentReference))
77 && (m_referenceType != ReferenceType.ByEntityId || !(m_entityReference == "0"))) {
78 if (m_referenceType == ReferenceType.ByEntityName) {
79 return string.IsNullOrEmpty(m_entityReference);
80 }
81 return false;
82 }
83 return true;
84 }
85
86 public static EntityReference Local(Component component) {
87 EntityReference result = default;
88 result.m_referenceType = ReferenceType.Local;
89 result.m_componentReference = component != null ? component.ValuesDictionary.DatabaseObject.Name : string.Empty;
90 return result;
91 }
92
93 public static EntityReference FromId(Component component) {
94 if (component?.Entity == null) {
95 return default;
96 }
97 EntityReference result = FromId(component.Entity);
98 result.m_componentReference = component != null ? component.ValuesDictionary.DatabaseObject.Name : string.Empty;
99 return result;
100 }
101
102 public static EntityReference FromId(Entity entity) {
103 int num = entity.Id;
104 EntityReference result = default;
105 result.m_referenceType = ReferenceType.ByEntityId;
106 result.m_entityReference = num.ToString(CultureInfo.InvariantCulture);
107 result.m_componentReference = string.Empty;
108 return result;
109 }
110
111 public static EntityReference FromId(Entity entity, EntityToIdMap entityToIdMap) => FromId(entity);
112
113 public static EntityReference FromId(Component component, EntityToIdMap entityToIdMap) => FromId(component);
114
115 public static EntityReference FromName(Component component) {
116 string entityReference = component != null ? component.Entity.FindComponent<ComponentName>(null, true).Name : string.Empty;
117 EntityReference result = default;
118 result.m_referenceType = ReferenceType.ByEntityName;
119 result.m_entityReference = entityReference;
120 result.m_componentReference = component != null ? component.ValuesDictionary.DatabaseObject.Name : string.Empty;
121 return result;
122 }
123
124 public static EntityReference FromName(Entity entity) {
125 string entityReference = entity != null ? entity.FindComponent<ComponentName>(null, true).Name : string.Empty;
126 EntityReference result = default;
127 result.m_referenceType = ReferenceType.ByEntityName;
128 result.m_entityReference = entityReference;
129 result.m_componentReference = string.Empty;
130 return result;
131 }
132
133 public static EntityReference FromReferenceString(string referenceString) {
134 EntityReference result = default;
135 if (string.IsNullOrEmpty(referenceString)) {
136 result.m_referenceType = ReferenceType.Null;
137 result.m_entityReference = string.Empty;
138 result.m_componentReference = string.Empty;
139 }
140 else {
141 string[] array = referenceString.Split(':');
142 if (array.Length == 1) {
143 result.m_referenceType = ReferenceType.Local;
144 result.m_entityReference = string.Empty;
145 result.m_componentReference = array[0];
146 }
147 else {
148 if (array.Length != 2
149 && array.Length != 3) {
150 throw new Exception("Invalid entity reference. Too many tokens.");
151 }
152 if (array[0] == "null"
153 && array.Length == 2) {
154 result.m_referenceType = ReferenceType.Null;
155 result.m_entityReference = string.Empty;
156 result.m_componentReference = string.Empty;
157 }
158 else if (array[0] == "local"
159 && array.Length == 2) {
160 result.m_referenceType = ReferenceType.Local;
161 result.m_componentReference = array[1];
162 }
163 else if (array[0] == "id") {
164 result.m_referenceType = ReferenceType.ByEntityId;
165 result.m_entityReference = array[1];
166 result.m_componentReference = array.Length == 3 ? array[2] : string.Empty;
167 }
168 else {
169 if (!(array[0] == "name")) {
170 throw new Exception("Unknown entity reference type.");
171 }
172 result.m_referenceType = ReferenceType.ByEntityId;
173 result.m_entityReference = array[1];
174 result.m_componentReference = array.Length == 3 ? array[2] : string.Empty;
175 }
176 }
177 }
178 return result;
179 }
180 }
181}
Component FindComponent(Type type, string name, bool throwOnError)
virtual Entity FindEntity(int EntityID)
virtual Subsystem FindSubsystem(Type type, string name, bool throwOnError)
static EntityReference FromName(Entity entity)
static EntityReference Null
static EntityReference FromId(Component component)
static EntityReference FromReferenceString(string referenceString)
static EntityReference FromId(Entity entity)
static EntityReference FromId(Entity entity, EntityToIdMap entityToIdMap)
Entity GetEntity(Entity localEntity, IdToEntityMap idToEntityMap, bool throwIfNotFound)
static EntityReference FromName(Component component)
static EntityReference FromId(Component component, EntityToIdMap entityToIdMap)
T GetComponent< T >(Entity localEntity, IdToEntityMap idToEntityMap, bool throwIfNotFound)
static EntityReference Local(Component component)