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 { if (!IsValidEntity(entity)) { continue; } MyObjectBuilder_CubeBlock baseEntity = (MyObjectBuilder_CubeBlock)CubeBlockEntity.InvokeEntityMethod(entity, CubeBlockEntity.CubeBlockGetObjectBuilderMethod); if (baseEntity == null) { continue; } Vector3I cubePosition = baseEntity.Min; long packedBlockCoordinates = (long)cubePosition.X + (long)cubePosition.Y * 10000 + (long)cubePosition.Z * 100000000; //If the original data already contains an entry for this, skip creation if (internalDataCopy.ContainsKey(packedBlockCoordinates)) { CubeBlockEntity matchingCubeBlock = (CubeBlockEntity)GetEntry(packedBlockCoordinates); if (matchingCubeBlock.IsDisposed) { continue; } matchingCubeBlock.BackingObject = entity; matchingCubeBlock.ObjectBuilder = baseEntity; } else { CubeBlockEntity newCubeBlock = null; if (BlockRegistry.Instance.ContainsGameType(baseEntity.TypeId)) { //Get the matching API type from the registry Type apiType = BlockRegistry.Instance.GetAPIType(baseEntity.TypeId); //Create a new API cube block newCubeBlock = (CubeBlockEntity)Activator.CreateInstance(apiType, new object[] { m_parent, baseEntity, entity }); } if (newCubeBlock == null) { newCubeBlock = new CubeBlockEntity(m_parent, baseEntity, entity); } AddEntry(packedBlockCoordinates, newCubeBlock); } } 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); } } if (GetInternalData().Count > 0 && m_isLoading) { //Trigger an event now that this cube grid has finished loading EntityEventManager.EntityEvent newEvent = new EntityEventManager.EntityEvent(); newEvent.type = EntityEventManager.EntityEventType.OnCubeGridLoaded; newEvent.timestamp = DateTime.Now; newEvent.entity = this.m_parent; newEvent.priority = 1; EntityEventManager.Instance.AddEvent(newEvent); m_isLoading = false; } } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }
protected override void InternalRefreshObjectBuilderMap() { try { if (m_rawDataObjectBuilderListResourceLock.Owned) { return; } if (WorldManager.Instance.IsWorldSaving) { return; } if (WorldManager.Instance.InternalGetResourceLock() == null) { return; } if (WorldManager.Instance.InternalGetResourceLock().Owned) { return; } m_rawDataObjectBuilderListResourceLock.AcquireExclusive(); m_rawDataObjectBuilderList.Clear(); foreach (Object entity in GetBackingDataHashSet()) { if (!IsValidEntity(entity)) { continue; } MyObjectBuilder_CubeBlock baseEntity = (MyObjectBuilder_CubeBlock)CubeBlockEntity.InvokeEntityMethod(entity, CubeBlockEntity.CubeBlockGetObjectBuilderMethod); if (baseEntity == null) { continue; } m_rawDataObjectBuilderList.Add(entity, baseEntity); } m_rawDataObjectBuilderListResourceLock.ReleaseExclusive(); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); m_rawDataObjectBuilderListResourceLock.ReleaseExclusive(); } }