예제 #1
0
        protected override void LoadDynamic()
        {
            try
            {
                HashSet <Object> rawEntities = GetBackingDataHashSet();
                Dictionary <long, BaseObject> internalDataCopy = new Dictionary <long, BaseObject>(GetInternalData());

                //Update the main data mapping
                foreach (Object entity in rawEntities)
                {
                    try
                    {
                        long entityId = BaseEntity.GetEntityId(entity);
                        if (entityId == 0)
                        {
                            continue;
                        }

                        if (!IsValidEntity(entity))
                        {
                            continue;
                        }

                        MyObjectBuilder_EntityBase baseEntity = BaseEntity.GetObjectBuilder(entity);
                        if (baseEntity == null)
                        {
                            continue;
                        }
                        if (!EntityRegistry.Instance.ContainsGameType(baseEntity.TypeId))
                        {
                            continue;
                        }

                        //If the original data already contains an entry for this, skip creation and just update values
                        if (GetInternalData().ContainsKey(entityId))
                        {
                            BaseEntity matchingEntity = (BaseEntity)GetEntry(entityId);
                            if (matchingEntity == null || matchingEntity.IsDisposed)
                            {
                                continue;
                            }

                            matchingEntity.BackingObject = entity;
                            matchingEntity.ObjectBuilder = baseEntity;
                        }
                        else
                        {
                            BaseEntity newEntity = null;

                            //Get the matching API type from the registry
                            Type apiType = EntityRegistry.Instance.GetAPIType(baseEntity.TypeId);

                            //Create a new API entity
                            newEntity = (BaseEntity)Activator.CreateInstance(apiType, new object[] { baseEntity, entity });

                            if (newEntity != null)
                            {
                                AddEntry(newEntity.EntityId, newEntity);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }

                //Cleanup old entities
                foreach (var entry in internalDataCopy)
                {
                    try
                    {
                        if (!rawEntities.Contains(entry.Value.BackingObject))
                        {
                            DeleteEntry(entry.Value);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
예제 #2
0
        protected override void LoadDynamic()
        {
            try
            {
                Dictionary <Object, MyObjectBuilder_Base> objectBuilderList = GetObjectBuilderMap();
                HashSet <Object> rawEntities = GetBackingDataHashSet();
                Dictionary <long, BaseObject> internalDataCopy = new Dictionary <long, BaseObject>(GetInternalData());

                if (objectBuilderList.Count != rawEntities.Count)
                {
                    if (SandboxGameAssemblyWrapper.IsDebugging)
                    {
                        LogManager.APILog.WriteLine("SectorObjectManager - Mismatch between raw entities and object builders");
                    }
                    return;
                }

                //Update the main data mapping
                foreach (Object entity in rawEntities)
                {
                    try
                    {
                        long entityId = BaseEntity.GetEntityId(entity);
                        if (entityId == 0)
                        {
                            continue;
                        }

                        if (!IsValidEntity(entity))
                        {
                            continue;
                        }

                        if (!objectBuilderList.ContainsKey(entity))
                        {
                            continue;
                        }

                        MyObjectBuilder_EntityBase baseEntity = (MyObjectBuilder_EntityBase)objectBuilderList[entity];
                        if (baseEntity == null)
                        {
                            continue;
                        }

                        //If the original data already contains an entry for this, skip creation and just update values
                        if (GetInternalData().ContainsKey(entityId))
                        {
                            BaseEntity matchingEntity = (BaseEntity)GetEntry(entityId);
                            if (matchingEntity == null || matchingEntity.IsDisposed)
                            {
                                continue;
                            }

                            //Update the base entity (not the same as BackingObject which is the internal object)
                            matchingEntity.ObjectBuilder = baseEntity;
                        }
                        else
                        {
                            BaseEntity newEntity = null;

                            //Get the matching API type from the registry
                            Type apiType = EntityRegistry.Instance.Registry[baseEntity.TypeId];

                            //Create a new API entity
                            newEntity = (BaseEntity)Activator.CreateInstance(apiType, new object[] { baseEntity, entity });

                            if (newEntity != null)
                            {
                                AddEntry(newEntity.EntityId, newEntity);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }

                //Cleanup old entities
                foreach (var entry in internalDataCopy)
                {
                    try
                    {
                        if (!rawEntities.Contains(entry.Value.BackingObject))
                        {
                            DeleteEntry(entry.Value);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
            }
        }