public static ObservableCollection <InventoryEditorModel> GetInventory(this MyObjectBuilder_ComponentContainer componentContainer, MyCubeBlockDefinition definition = null) { var inventoryEditors = new ObservableCollection <InventoryEditorModel>(); if (componentContainer != null) { var inventoryBase = componentContainer.Components.FirstOrDefault(e => e.TypeId == "MyInventoryBase"); if (inventoryBase != null) { var singleInventory = inventoryBase.Component as MyObjectBuilder_Inventory; if (singleInventory != null) { var iem = ParseInventory(singleInventory, definition); if (iem != null) { inventoryEditors.Add(iem); } } var aggregate = inventoryBase.Component as MyObjectBuilder_InventoryAggregate; if (aggregate != null) { foreach (var field in aggregate.Inventories) { var iem = ParseInventory(field as MyObjectBuilder_Inventory, definition); if (iem != null) { inventoryEditors.Add(iem); } } } } } return(inventoryEditors); }
private static void PlaceEdge(EdgePlacerConfig cfg, Vector3D[] segments) { MyEntity holderEntity; MyEntities.TryGetEntityById(cfg.EntityPlacing, out holderEntity); var holderPlayer = holderEntity != null?MyAPIGateway.Players.GetPlayerControllingEntity(holderEntity) : null; var def = DefinitionFor(cfg.Placed); if (def == null) { MyEventContext.ValidationFailed(); return; } #region Validation if (!MyEventContext.Current.IsLocallyInvoked) { if (holderEntity == null || holderPlayer == null || MyEventContext.Current.Sender.Value != holderPlayer.SteamUserId) { MyEventContext.ValidationFailed(); return; } if (MyAreaPermissionSystem.Static != null) { foreach (var pos in segments) { if (MyAreaPermissionSystem.Static.HasPermission(holderPlayer.IdentityId, pos, MyPermissionsConstants.Build)) { continue; } holderPlayer.ShowNotification("You cannot build here", 2000, null, new Vector4(1, 0, 0, 1)); MyEventContext.ValidationFailed(); return; } } var validPlacedType = false; foreach (var item in holderEntity.GetInventory(MyCharacterConstants.MainInventory).Items) { if (item == null) { continue; } var itemDef = MyDefinitionManager.Get <MyInventoryItemDefinition>(item.DefinitionId) as MyHandItemDefinition; if (itemDef == null) { continue; } foreach (var behaviorDef in itemDef.Behaviors) { var placeDef = behaviorDef as EdgePlacerBehaviorDefinition; if (placeDef == null || placeDef.Placed != cfg.Placed) { continue; } validPlacedType = true; break; } if (validPlacedType) { break; } } if (!validPlacedType) { MyEventContext.ValidationFailed(); MySession.Static.Log.Warning( $"{holderPlayer} tried to place {cfg.Placed}, but has no item that can place it"); return; } var layer = MySession.Static.Components.Get <BendyController>().GetOrCreateLayer(def.Layer); var annotated = AnnotateNodes(layer, segments); var tmp = new List <string>(); if (!ValidatePath(def, layer, annotated, tmp)) { holderPlayer.ShowNotification(string.Join("\n", tmp)); MyEventContext.ValidationFailed(); return; } } #endregion var graph = MySession.Static.Components.Get <BendyController>().GetOrCreateLayer(def.Layer); for (var i = 1; i < segments.Length; i++) { var nextNode = graph.GetOrCreateNode(segments[i - 1]); var prevNode = graph.GetOrCreateNode(segments[i]); if (graph.GetEdge(prevNode, nextNode) != null) { continue; } var obContainer = new MyObjectBuilder_ComponentContainer(); var worldMatrix = MatrixD.CreateWorld((prevNode.Position + nextNode.Position) / 2, Vector3D.Normalize(nextNode.Position - prevNode.Position), Vector3D.Normalize(nextNode.Up + prevNode.Up)); var worldMatrixInv = MatrixD.Invert(worldMatrix); ((ICollection <MyObjectBuilder_EntityComponent>)obContainer.Components).Add( new MyObjectBuilder_BendyComponent() { Overrides = new[] { new MyObjectBuilder_BendyComponent.NodePose { Index = 0, Position = (Vector3)Vector3D.Transform(prevNode.Position, worldMatrixInv), Up = (Vector3)Vector3D.Transform(prevNode.Up, worldMatrixInv) }, new MyObjectBuilder_BendyComponent.NodePose { Index = 1, Position = (Vector3)Vector3D.Transform(nextNode.Position, worldMatrixInv), Up = (Vector3)Vector3D.Transform(nextNode.Up, worldMatrixInv) } } }); var entOb = new MyObjectBuilder_EntityBase() { EntityDefinitionId = (MyDefinitionId)cfg.Placed, PersistentFlags = MyPersistentEntityFlags2.InScene, PositionAndOrientation = new MyPositionAndOrientation(worldMatrix), SubtypeName = cfg.Placed.SubtypeId, ComponentContainer = obContainer }; var entity = MyAPIGateway.Entities.CreateFromObjectBuilderAndAdd(entOb); if (holderPlayer != null && holderPlayer.IsCreative()) { entity.Components.Get <ConstructableComponent>()?.InstallFromCreative(); ConstructableComponentDefinition.CcComponent test; int test2; entity.Components.Get <ConstructableComponent>() ?.IncreaseIntegrity(1e9f, out test, out test2); } entity.Components.Get <BendyPhysicsComponent>()?.DestroyEnvItems(); EntityAdded?.Invoke(holderEntity, holderPlayer, entity); } }
/// <summary> /// Tries to retrieve entity definition of the entity owning this container, check if the definition has some DefaultComponents, /// tries to retrieve these components definitions, create these components instances and add them /// /// TODO: This should be ideally a behavior of the MyEntityComponentContainer when it is initialized (deserialized).. or by the factory, for now, this is an extension method /// </summary> public static void InitComponents(this MyComponentContainer container, MyObjectBuilderType type, MyStringHash subtypeName, MyObjectBuilder_ComponentContainer builder) { if (MyDefinitionManager.Static != null) { MyContainerDefinition definition = null; bool IsFirstInit = builder == null; if (TryGetContainerDefinition(type, subtypeName, out definition)) { container.Init(definition); if (definition.DefaultComponents != null) { foreach (var component in definition.DefaultComponents) { MyComponentDefinitionBase componentDefinition = null; MyObjectBuilder_ComponentBase componentBuilder = FindComponentBuilder(component, builder); bool IsComponentSerialized = componentBuilder != null; Type componentType = null; MyComponentBase componentInstance = null; var componentSubtype = subtypeName; if (component.SubtypeId.HasValue) { componentSubtype = component.SubtypeId.Value; } // Create component instance if (TryGetComponentDefinition(component.BuilderType, componentSubtype, out componentDefinition)) { componentInstance = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId); componentInstance.Init(componentDefinition); } else if (component.IsValid()) { if (!component.BuilderType.IsNull) { componentInstance = MyComponentFactory.CreateInstanceByTypeId(component.BuilderType); } else { componentInstance = MyComponentFactory.CreateInstanceByType(component.InstanceType); } } // Check component type from attributes. if (componentInstance != null) { var componentTypeFromAttr = MyComponentTypeFactory.GetComponentType(componentInstance.GetType()); if (componentTypeFromAttr != null) { componentType = componentTypeFromAttr; } else { if (componentDefinition != null) { System.Diagnostics.Debug.Fail("Unknown component type - component type attribute not specified for component class: " + componentInstance.GetType()); } } } //TODO: this should be avoided! Component type MUST be set via MyComponentType attribute. if (componentType == null && componentInstance != null) { componentType = componentInstance.GetType(); } // If everything passed, go on.. if (componentInstance != null && componentType != null) { bool componentShouldBeAdded = IsFirstInit || IsComponentSerialized || component.ForceCreate; if (componentShouldBeAdded) { if (componentBuilder != null) { componentInstance.Deserialize(componentBuilder); } // Add only fully initialized component.. container.Add(componentType, componentInstance); } } else { System.Diagnostics.Debug.Fail("Component instance wasn't created or it's base type couldn't been determined!"); } } } else { System.Diagnostics.Debug.Fail("Got definition for container, but DefaultComponents is null!"); } } // This may rewrite already once deserialized data, but will also add missing components in definition container.Deserialize(builder); } else { System.Diagnostics.Debug.Fail("Trying to init enabled components on entity, but definition manager is null"); } }
public static MyObjectBuilder_ComponentBase FindComponentBuilder(MyContainerDefinition.DefaultComponent component, MyObjectBuilder_ComponentContainer builder) { MyObjectBuilder_ComponentBase componentBuilder = null; if (builder != null && component.IsValid()) { MyObjectBuilderType componentObType = null; if (!component.BuilderType.IsNull) { var componentData = builder.Components.Find(x => x.Component.TypeId == component.BuilderType); if (componentData != null) { componentBuilder = componentData.Component; } } } return(componentBuilder); }
public static void InitComponents(this MyComponentContainer container, MyObjectBuilderType type, MyStringHash subtypeName, MyObjectBuilder_ComponentContainer builder) { if (MyDefinitionManager.Static != null) { MyContainerDefinition definition = null; bool flag = ReferenceEquals(builder, null); if (TryGetContainerDefinition(type, subtypeName, out definition)) { container.Init(definition); if (definition.DefaultComponents != null) { foreach (MyContainerDefinition.DefaultComponent component in definition.DefaultComponents) { MyComponentDefinitionBase componentDefinition = null; MyObjectBuilder_ComponentBase base3 = FindComponentBuilder(component, builder); bool flag2 = base3 != null; Type type2 = null; MyComponentBase base4 = null; MyStringHash hash = subtypeName; if (component.SubtypeId != null) { hash = component.SubtypeId.Value; } if (TryGetComponentDefinition(component.BuilderType, hash, out componentDefinition)) { base4 = MyComponentFactory.CreateInstanceByTypeId(componentDefinition.Id.TypeId); base4.Init(componentDefinition); } else if (component.IsValid()) { base4 = component.BuilderType.IsNull ? MyComponentFactory.CreateInstanceByType(component.InstanceType) : MyComponentFactory.CreateInstanceByTypeId(component.BuilderType); } if (base4 != null) { Type componentType = MyComponentTypeFactory.GetComponentType(base4.GetType()); if (componentType != null) { type2 = componentType; } else { MyComponentDefinitionBase base1 = componentDefinition; } } if ((type2 == null) && (base4 != null)) { type2 = base4.GetType(); } if (((base4 != null) && (type2 != null)) && ((flag | flag2) || component.ForceCreate)) { if (base3 != null) { base4.Deserialize(base3); } container.Add(type2, base4); } } } } container.Deserialize(builder); } }
public static MyObjectBuilder_ComponentBase FindComponentBuilder(MyContainerDefinition.DefaultComponent component, MyObjectBuilder_ComponentContainer builder) { MyObjectBuilder_ComponentBase base2 = null; if ((builder != null) && component.IsValid()) { null; if (!component.BuilderType.IsNull) { MyObjectBuilder_ComponentContainer.ComponentData data = builder.Components.Find(x => x.Component.TypeId == component.BuilderType); if (data != null) { base2 = data.Component; } } } return(base2); }