protected void InternalAddCubeBlock( ) { if ( _cubeBlockToAddRemove == null ) return; try { MyObjectBuilder_CubeBlock objectBuilder = _cubeBlockToAddRemove.ObjectBuilder; MyCubeBlockDefinition blockDef = MyDefinitionManager.Static.GetCubeBlockDefinition( objectBuilder ); NetworkManager.BroadcastAddCubeBlock( _cubeBlockToAddRemove ); Object result = InvokeEntityMethod( BackingObject, CubeGridAddCubeBlockMethod, new object[ ] { objectBuilder, true, blockDef } ); _cubeBlockToAddRemove.BackingObject = result; } catch ( Exception ex ) { ApplicationLog.BaseLog.Error( ex ); } _cubeBlockToAddRemove = null; }
protected void InternalRemoveCubeBlock( ) { if ( _cubeBlockToAddRemove == null ) return; //NOTE - We don't broadcast the removal because the game internals take care of that by broadcasting the removal delta lists every frame update InvokeEntityMethod( BackingObject, CubeGridRemoveCubeBlockMethod, new object[ ] { _cubeBlockToAddRemove.BackingObject, Type.Missing } ); _cubeBlockToAddRemove = null; }
public void OnCubeBlockDeleted( CubeBlockEntity entity ) { }
public void DeleteCubeBlock( CubeBlockEntity cubeBlock ) { _cubeBlockToAddRemove = cubeBlock; MySandboxGame.Static.Invoke( InternalRemoveCubeBlock ); }
public bool IsDefinitionMatch( CubeBlockEntity cubeToCheck, int recurseDepth = 0 ) { if ( cubeToCheck == null ) return false; if ( cubeToCheck.IsDisposed ) return false; if ( cubeToCheck.Parent == null ) return false; if ( cubeToCheck.Parent.IsDisposed ) return false; if ( cubeToCheck.Parent.IsLoading ) return false; if ( recurseDepth < 0 || recurseDepth > 2 ) return false; bool isMatch = false; Vector3I cubePos = cubeToCheck.Position; Type cubeType = cubeToCheck.GetType( ); Dictionary<Vector3I, StructureEntry> structureDef = GetMultiblockDefinition( ); StructureEntry anchorDef = structureDef[ Vector3I.Zero ]; //Check if this block isn't the anchor type if ( cubeType != anchorDef.type ) { //Check if this block is anywhere in the definition bool foundMatch = false; foreach ( StructureEntry entry in structureDef.Values ) { if ( entry.type == cubeType ) { foundMatch = true; break; } } if ( !foundMatch ) return false; //Recursively search through possible anchor blocks foreach ( Vector3I key in structureDef.Keys ) { StructureEntry entry = structureDef[ key ]; if ( cubeType == entry.type ) { Vector3I possibleAnchorPos = cubePos - key; CubeBlockEntity posCubeBlock = cubeToCheck.Parent.GetCubeBlock( possibleAnchorPos ); isMatch = IsDefinitionMatch( posCubeBlock, recurseDepth + 1 ); if ( isMatch ) break; } } } else { isMatch = true; foreach ( Vector3I key in structureDef.Keys ) { StructureEntry entry = structureDef[ key ]; Vector3I defPos = cubePos + key; CubeBlockEntity posCubeBlock = cubeToCheck.Parent.GetCubeBlock( defPos ); if ( posCubeBlock == null ) { isMatch = false; break; } //Compare the block type if ( !entry.type.IsAssignableFrom( posCubeBlock.GetType( ) ) ) { isMatch = false; break; } //Compare the block color, if set if ( entry.color != null && entry.color.ColorToHSV( ) != Vector3.Zero ) { if ( entry.color.ColorToHSV( ) != (Vector3)posCubeBlock.ColorMaskHSV ) { isMatch = false; break; } } //Compare the block orientation, if enabled if ( entry.useOrientation ) { if ( entry.orientation.Forward != posCubeBlock.BlockOrientation.Forward || entry.orientation.Up != posCubeBlock.BlockOrientation.Up ) { isMatch = false; break; } } //Compare the block subtype, if enabled if ( entry.useSubTypeName ) { string cleanEntrySubType = entry.subTypeName.ToLower( ); string cleanBlockSubType = posCubeBlock.Id.SubtypeName.ToLower( ); if ( cleanEntrySubType != cleanBlockSubType ) { isMatch = false; break; } } } if ( isMatch ) m_anchor = cubeToCheck; } if ( isMatch && ExtenderOptions.IsDebugging && recurseDepth == 0 ) { //LogManager.APILog.WriteLine("Found multiblock match in cube grid '" + cubeToCheck.Parent.Name + "' anchored at " + ((Vector3I)m_anchor.Min).ToString()); } return isMatch; }
public void OnCubeBlockCreated( CubeBlockEntity entity ) { }
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); } }
public void AddCubeBlock(CubeBlockEntity cubeBlock) { m_cubeBlockToAdd = cubeBlock; Action action = InternalAddCubeBlock; SandboxGameAssemblyWrapper.Instance.EnqueueMainGameAction(action); }
public void BroadcastCubeBlockFactionData(CubeBlockEntity cubeBlock) { try { BaseObject.InvokeEntityMethod(m_netManager, CubeGridNetManagerBroadcastCubeBlockFactionDataMethod, new object[] { m_cubeGrid.BackingObject, cubeBlock.ActualObject, cubeBlock.Owner, cubeBlock.ShareMode }); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }
public void DeleteCubeBlock( CubeBlockEntity cubeBlock ) { _cubeBlockToAddRemove = cubeBlock; Action action = InternalRemoveCubeBlock; SandboxGameAssemblyWrapper.Instance.EnqueueMainGameAction( action ); }
public void BroadcastCubeBlockBuildIntegrityValues(CubeBlockEntity cubeBlock) { try { Type someEnum = CubeGridEntity.InternalType.GetNestedType(CubeGridIntegrityChangeEnumClass); Array someEnumValues = someEnum.GetEnumValues(); Object enumValue = someEnumValues.GetValue(0); BaseObject.InvokeEntityMethod(m_netManager, CubeGridNetManagerBroadcastCubeBlockBuildIntegrityValuesMethod, new object[] { cubeBlock.BackingObject, enumValue, 0L }); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }
public void BroadcastAddCubeBlock(CubeBlockEntity cubeBlock) { try { Type packedStructType = CubeGridEntity.InternalType.GetNestedType(CubeGridEntity.CubeGridPackedCubeBlockClass); Object packedStruct = Activator.CreateInstance(packedStructType); MyCubeBlockDefinition def = MyDefinitionManager.Static.GetCubeBlockDefinition(cubeBlock.ObjectBuilder); //Set def id BaseObject.SetEntityFieldValue(packedStruct, "35E024D9E3B721592FB9B6FC1A1E239A", (DefinitionIdBlit)def.Id); //Set position BaseObject.SetEntityFieldValue(packedStruct, "5C3938C9B8CED1D0057CCF12F04329AB", (Vector3I)cubeBlock.Min); //Set block size BaseObject.SetEntityFieldValue(packedStruct, "0DDB53EB9299ECC9826DF9A47E5E4F38", new Vector3UByte(def.Size)); //Set block margins BaseObject.SetEntityFieldValue(packedStruct, "4045ED59A8C93DE0B41218EF2E947E55", new Vector3B(0, 0, 0)); BaseObject.SetEntityFieldValue(packedStruct, "096897446D5BD5243D3D6E5C53CE1772", new Vector3B(0, 0, 0)); //Set block margin scale BaseObject.SetEntityFieldValue(packedStruct, "E28B9725868E18B339D1E0594EF14444", new Vector3B(0, 0, 0)); //Set orientation Quaternion rot; cubeBlock.BlockOrientation.GetQuaternion(out rot); BaseObject.SetEntityFieldValue(packedStruct, "F1AAFF5C8F200592F313BC7E02140A38", Base6Directions.GetForward(rot)); BaseObject.SetEntityFieldValue(packedStruct, "E80AA7B84131E39F9F88209A109EED59", Base6Directions.GetUp(rot)); //Set color BaseObject.SetEntityFieldValue(packedStruct, "556976F2528411FF5F95FC75DC13FEED", ColorExtensions.PackHSVToUint(cubeBlock.ColorMaskHSV)); object[] parameters = { packedStruct, new HashSet<Vector3UByte>(), cubeBlock.EntityId, MyRandom.Instance.CreateRandomSeed() }; BaseObject.InvokeEntityMethod(m_netManager, CubeGridNetManagerBroadcastAddCubeBlockMethod, parameters); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }
protected void InternalAddCubeBlock() { if (m_cubeBlockToAdd == null) return; //TODO - Find out how to broadcast this InvokeEntityMethod(BackingObject, CubeGridAddCubeBlockMethod, new object[] { m_cubeBlockToAdd.ObjectBuilder, false }); m_cubeBlockToAdd = null; }
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 ( KeyValuePair<long, BaseObject> 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 ); } }
public void UpdateCubeBlock(CubeGridEntity parent, CubeBlockEntity cubeBlock) { ApplicationLog.BaseLog.Info( "WCF Service - Received cube block entity '{0}' with id '{1}'", cubeBlock.Name, cubeBlock.EntityId ); foreach (CubeGridEntity cubeGrid in GetSectorCubeGridEntities()) { if (cubeGrid.EntityId == parent.EntityId) { //Find the matching block by either the entity id or the position of the block in the grid foreach (CubeBlockEntity baseBlock in cubeGrid.CubeBlocks) { //If entity id is defined but there is a mismatch, skip this block if (baseBlock.EntityId != 0 && baseBlock.EntityId != cubeBlock.EntityId) continue; //If entity is is NOT defined but there is a position mismatch, skip this block if (baseBlock.EntityId == 0 && baseBlock.Position != cubeBlock.Position) continue; //Copy over the deserialized dummy cube block to the actual cube block Type type = baseBlock.GetType(); PropertyInfo[] properties = type.GetProperties(); foreach (PropertyInfo property in properties) { try { //Check if the property can be publicly set if (!property.CanWrite) continue; if (property.GetSetMethod() == null) continue; //Check if the property has the [DataMember] attribute object[] dataMemberAttributes = property.GetCustomAttributes(typeof(DataMemberAttribute), true); if (dataMemberAttributes == null || dataMemberAttributes.Length == 0) continue; Object newValue = property.GetValue(cubeBlock, new object[] { }); if (newValue == null) continue; Object oldValue = property.GetValue(baseBlock, new object[] { }); if (newValue.Equals(oldValue)) continue; property.SetValue(baseBlock, newValue, new object[] { }); } catch (Exception) { //Do nothing } } break; } break; } } }
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("CubeBlockManager - Mismatch between raw entities and object builders"); m_resourceLock.ReleaseExclusive(); return; } //Update the main data mapping foreach (Object entity in rawEntities) { try { if (!IsValidEntity(entity)) continue; if (!objectBuilderList.ContainsKey(entity)) continue; MyObjectBuilder_CubeBlock baseEntity = (MyObjectBuilder_CubeBlock)objectBuilderList[entity]; 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 (GetInternalData().ContainsKey(packedBlockCoordinates)) { CubeBlockEntity matchingCubeBlock = (CubeBlockEntity)GetEntry(packedBlockCoordinates); if (matchingCubeBlock.IsDisposed) continue; //Update the base entity (not the same as BackingObject which is the internal object) matchingCubeBlock.ObjectBuilder = baseEntity; } else { CubeBlockEntity newCubeBlock = null; if (BlockRegistry.Instance.Registry.ContainsKey(baseEntity.TypeId)) { //Get the matching API type from the registry Type apiType = BlockRegistry.Instance.Registry[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 = false; } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }
public void BroadcastAddCubeBlock(CubeBlockEntity cubeBlock) { try { //TODO - Get this working. This code was a valiant effort but still isn't working >.< Type packedStructType = CubeGridEntity.InternalType.GetNestedType(CubeGridEntity.CubeGridPackedCubeBlockClass); Object packedStruct = Activator.CreateInstance(packedStructType); //Set def id FieldInfo field = BaseObject.GetEntityField(packedStruct, "35E024D9E3B721592FB9B6FC1A1E239A"); field.SetValue(packedStruct, (DefinitionIdBlit)MyDefinitionManager.Static.GetCubeBlockDefinition(cubeBlock.ObjectBuilder).Id); //Set position field = BaseObject.GetEntityField(packedStruct, "5C3938C9B8CED1D0057CCF12F04329AB"); field.SetValue(packedStruct, (Vector3I)cubeBlock.Min); //Set orientation Quaternion rot; cubeBlock.BlockOrientation.GetQuaternion(out rot); field = BaseObject.GetEntityField(packedStruct, "F1AAFF5C8F200592F313BC7E02140A38"); field.SetValue(packedStruct, Base6Directions.GetForward(rot)); field = BaseObject.GetEntityField(packedStruct, "E80AA7B84131E39F9F88209A109EED59"); field.SetValue(packedStruct, Base6Directions.GetUp(rot)); //Set color field = BaseObject.GetEntityField(packedStruct, "556976F2528411FF5F95FC75DC13FEED"); field.SetValue(packedStruct, ColorExtensions.PackHSVToUint(cubeBlock.ColorMaskHSV)); object[] parameters = { cubeBlock.EntityId, packedStruct }; Type[] typeArgs = { typeof(long), packedStructType.MakeByRefType() }; BaseObject.InvokeEntityMethod(m_netManager, CubeGridNetManagerBroadcastAddCubeBlockMethod, parameters, typeArgs); } catch (Exception ex) { LogManager.ErrorLog.WriteLine(ex); } }