2using System.Collections;
3using System.Collections.Generic;
5using System.Reflection;
18 IEnumerator<T> IEnumerable<T>.GetEnumerator() =>
new FilteredComponentsEnumerator<T>(
m_entity);
20 IEnumerator IEnumerable.GetEnumerator() =>
new FilteredComponentsEnumerator<T>(
m_entity);
87 throw new InvalidOperationException(
"ValuesDictionary was not created from EntityTemplate.");
91 List<KeyValuePair<int, Component>> list = [];
97 bool isOptional = item.GetValue<
bool>(
"IsOptional");
98 string className = item.GetValue<
string>(
"Class");
99 int loadOrder = item.GetValue<
int>(
"LoadOrder");
104#pragma warning disable IL2072
105 obj = Activator.CreateInstance(type);
106#pragma warning restore IL2072
108 catch (TargetInvocationException ex) {
109 if (ex.InnerException is not
null) {
110 throw ex.InnerException;
115 throw new InvalidOperationException(
116 $
"Type \"{className}\" cannot be used as a component because it does not inherit from Component class."
119 component.Initialize(
this, item);
120 bool isModComponent = type.Namespace !=
"Game";
122 int adjustedLoadOrder = isModComponent ? loadOrder + 10000 : loadOrder;
123 list.Add(
new KeyValuePair<int, Component>(adjustedLoadOrder, component));
128 list.Sort((x, y) => x.Key - y.Key);
129 m_components =
new List<Component>(list.Select(x => x.Value));
134 if (type.GetTypeInfo().IsAssignableFrom(component.GetType().GetTypeInfo())
140 if (
string.IsNullOrEmpty(name)) {
141 throw new Exception($
"Required component {type.FullName} does not exist in entity.");
143 throw new Exception($
"Required component {type.FullName} with name \"{name}\" does not exist in entity.");
150 if (
string.IsNullOrEmpty(name)) {
151 throw new ArgumentNullException(nameof(name));
160 throw new Exception($
"Required component {name} does not exist in entity.");
176 if (newComponent.GetType().GetTypeInfo().IsAssignableFrom(oldComponent.GetType().GetTypeInfo())) {
190 GC.SuppressFinalize(
this);
195 List<Entity> list =
null;
199 list.AddRange(ownedEntities);
209 catch (Exception innerException) {
210 throw new InvalidOperationException($
"Error loading component {component.GetType().FullName}.", innerException);
218 component.
Save(valuesDictionary2, entityToIdMap);
219 if (valuesDictionary2.
Count > 0) {
static Type FindType(string typeName, bool skipSystemAssemblies, bool throwIfNotFound)
virtual void InheritFromComponent(Component baseComponent)
ValuesDictionary ValuesDictionary
virtual void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
virtual void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual IEnumerable< Entity > GetOwnedEntities()
Entity(Project project, ValuesDictionary valuesDictionary, int id) static Action< Entity, List< KeyValuePair< int, Component > > > EntityComponentsInitialized
void ReplaceComponent(Component oldComponent, Component newComponent)
void FireEntityRemovedEvent()
List< Entity > InternalGetOwnedEntities()
EventHandler EntityRemoved
FilteredComponentsEnumerable< T > FindComponents< T >()
Component FindComponent(string name, bool throwOnError)
void FireEntityAddedEvent()
Entity(Project project, ValuesDictionary valuesDictionary)
List< Component > Components
ValuesDictionary m_valuesDictionary
void InternalLoadEntity(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
ValuesDictionary ValuesDictionary
void InternalSaveEntity(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
List< Component > m_components
void RemoveComponent(Component component)
Component FindComponent(Type type, string name, bool throwOnError)
DatabaseObjectType MemberComponentTemplateType
DatabaseObjectType EntityTemplateType
virtual GameDatabase GameDatabase
IEnumerable< object > Values
DatabaseObject DatabaseObject
FilteredComponentsEnumerable(Entity entity)
FilteredComponentsEnumerator< T > GetEnumerator()
FilteredComponentsEnumerator(Entity entity)