Ejemplo n.º 1
0
        public InventoryEntity(MyObjectBuilder_Inventory definition)
            : base(definition)
        {
            m_itemManager = new InventoryItemManager(this);

            List<InventoryItemEntity> itemList = new List<InventoryItemEntity>();
            foreach (MyObjectBuilder_InventoryItem item in definition.Items)
            {
                InventoryItemEntity newItem = new InventoryItemEntity(item);
                newItem.Container = this;
                itemList.Add(newItem);
            }
            m_itemManager.Load(itemList);
        }
Ejemplo n.º 2
0
        public InventoryItemEntity NewEntry()
        {
            MyObjectBuilder_InventoryItem defaults   = new MyObjectBuilder_InventoryItem();
            SerializableDefinitionId      itemTypeId = new SerializableDefinitionId(typeof(MyObjectBuilder_Ore), "Stone");

            defaults.PhysicalContent = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(itemTypeId);
            defaults.Amount          = 1;

            InventoryItemEntity newItem = new InventoryItemEntity(defaults);

            newItem.ItemId = NextItemId;
            m_itemManager.NewEntry <InventoryItemEntity>(newItem);

            NextItemId = NextItemId + 1;

            RefreshInventory();

            return(newItem);
        }
Ejemplo n.º 3
0
        public bool NewEntry(InventoryItemEntity source)
        {
            /*
             * m_itemManager.AddEntry<InventoryItemEntity>(NextItemId, source);
             *
             * NextItemId = NextItemId + 1;
             * //TODO - Figure out the right way to add new items
             * //Just updating an item amount doesn't seem to work right
             * UpdateItemAmount(source, source.Amount * 2);
             *
             * RefreshInventory();
             */

            if (BackingObject != null)
            {
                SandboxGameAssemblyWrapper.Instance.GameAction(new Action(delegate()
                {
                    IMyInventory inventory = (IMyInventory)BackingObject;
                    inventory.AddItems((MyFixedPoint)source.Amount, source.PhysicalContent);
                }));
            }

            return(true);
        }
Ejemplo n.º 4
0
        public void UpdateItemAmount(InventoryItemEntity item, Decimal newAmount)
        {
            m_itemToUpdate = item;
            m_oldItemAmount = (Decimal)item.Amount;
            m_newItemAmount = newAmount;

            Action action = InternalUpdateItemAmount;
            SandboxGameAssemblyWrapper.Instance.EnqueueMainGameAction(action);
        }
Ejemplo n.º 5
0
 public void RefreshInventory()
 {
     try
     {
         if (BackingObject != null)
         {
             //Update the base entity
             MethodInfo getObjectBuilder = BackingObject.GetType().GetMethod(InventoryGetObjectBuilderMethod);
             MyObjectBuilder_Inventory inventory = (MyObjectBuilder_Inventory)getObjectBuilder.Invoke(BackingObject, new object[] { });
             ObjectBuilder = inventory;
         }
         else
         {
             //Update the item manager
             MyObjectBuilder_Inventory inventory = (MyObjectBuilder_Inventory)ObjectBuilder;
             List<InventoryItemEntity> itemList = new List<InventoryItemEntity>();
             foreach (MyObjectBuilder_InventoryItem item in inventory.Items)
             {
                 InventoryItemEntity newItem = new InventoryItemEntity(item);
                 newItem.Container = this;
                 itemList.Add(newItem);
             }
             m_itemManager.Load(itemList);
         }
     }
     catch (Exception ex)
     {
         LogManager.ErrorLog.WriteLine(ex);
     }
 }
Ejemplo n.º 6
0
        public bool NewEntry(InventoryItemEntity source)
        {
            m_itemManager.AddEntry<InventoryItemEntity>(NextItemId, source);

            //TODO - Figure out the right way to add new items
            //Just updating an item amount doesn't seem to work right
            UpdateItemAmount(source, (Decimal)(source.Amount * 2));

            RefreshInventory();

            return true;
        }
Ejemplo n.º 7
0
        public bool DeleteEntry(InventoryItemEntity source)
        {
            bool result = m_itemManager.DeleteEntry(source);

            RefreshInventory();

            return result;
        }
        protected override void LoadDynamic()
        {
            try
            {
                Dictionary <Object, MyObjectBuilder_Base> objectBuilderList = GetObjectBuilderMap();
                List <Object> rawEntities = GetBackingDataList();
                Dictionary <long, BaseObject> internalDataCopy = new Dictionary <long, BaseObject>(GetInternalData());

                if (objectBuilderList.Count != rawEntities.Count)
                {
                    if (SandboxGameAssemblyWrapper.IsDebugging)
                    {
                        LogManager.APILog.WriteLine("InventoryItemManager - 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_InventoryItem baseEntity = (MyObjectBuilder_InventoryItem)objectBuilderList[entity];
                        if (baseEntity == null)
                        {
                            continue;
                        }

                        long itemId = baseEntity.ItemId;

                        //If the original data already contains an entry for this, skip creation
                        if (GetInternalData().ContainsKey(itemId))
                        {
                            InventoryItemEntity matchingItem = (InventoryItemEntity)GetEntry(itemId);

                            //Update the base entity (not the same as BackingObject which is the internal object)
                            matchingItem.ObjectBuilder = baseEntity;
                        }
                        else
                        {
                            InventoryItemEntity newItemEntity = new InventoryItemEntity(baseEntity, entity);
                            newItemEntity.Container = m_parent;

                            AddEntry(newItemEntity.ItemId, newItemEntity);
                        }
                    }
                    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);
            }
        }
Ejemplo n.º 9
0
        protected override void LoadDynamic()
        {
            try
            {
                Dictionary<Object, MyObjectBuilder_Base> objectBuilderList = GetObjectBuilderMap();
                List<Object> rawEntities = GetBackingDataList();
                Dictionary<long, BaseObject> internalDataCopy = new Dictionary<long, BaseObject>(GetInternalData());

                if (objectBuilderList.Count != rawEntities.Count)
                {
                    if (SandboxGameAssemblyWrapper.IsDebugging)
                        LogManager.APILog.WriteLine("InventoryItemManager - 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_InventoryItem baseEntity = (MyObjectBuilder_InventoryItem)objectBuilderList[entity];
                        if (baseEntity == null)
                            continue;

                        long itemId = baseEntity.ItemId;

                        //If the original data already contains an entry for this, skip creation
                        if (GetInternalData().ContainsKey(itemId))
                        {
                            InventoryItemEntity matchingItem = (InventoryItemEntity)GetEntry(itemId);

                            //Update the base entity (not the same as BackingObject which is the internal object)
                            matchingItem.ObjectBuilder = baseEntity;
                        }
                        else
                        {
                            InventoryItemEntity newItemEntity = new InventoryItemEntity(baseEntity, entity);
                            newItemEntity.Container = m_parent;

                            AddEntry(newItemEntity.ItemId, newItemEntity);
                        }
                    }
                    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);
            }
        }
Ejemplo n.º 10
0
        public bool DeleteEntry( InventoryItemEntity source )
        {
            if ( BackingObject != null )
            {
                SandboxGameAssemblyWrapper.Instance.GameAction( new Action( delegate( )
                {
                    IMyInventory myInventory = (IMyInventory)BackingObject;
                    //myInventory.RemoveItems( source.ItemId );
                } ) );
            }

            return true;

            /*
            bool result = m_itemManager.DeleteEntry(source);
            RefreshInventory();
            return result;
             */
        }
        protected void InternalUpdateItemAmount()
        {
            try
            {
                if (m_itemToUpdate == null)
                    return;

                Decimal delta = m_newItemAmount - m_oldItemAmount;

                MyObjectBuilder_PhysicalObject physicalContent = m_itemToUpdate.ObjectBuilder.PhysicalContent;

                if (delta > 0)
                {
                    Object[] parameters = new object[] {
                        (MyFixedPoint)delta,
                        physicalContent,
                        -1
                    };

                    InvokeEntityMethod(BackingObject, InventoryAddItemAmountMethod, parameters);
                }
                else
                {
                    Type[] argTypes = new Type[3];
                    argTypes[0] = typeof(MyFixedPoint);
                    argTypes[1] = typeof(MyObjectBuilder_PhysicalObject);
                    argTypes[2] = typeof(bool);

                    Object[] parameters = new object[] {
                        (MyFixedPoint)(-delta),
                        physicalContent,
                        Type.Missing
                    };

                    InvokeEntityMethod(BackingObject, InventoryRemoveItemAmountMethod, parameters, argTypes);
                }

                m_itemToUpdate = null;
                m_oldItemAmount = 0;
                m_newItemAmount = 0;
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
Ejemplo n.º 12
0
        protected override void LoadDynamic()
        {
            try
            {
                List <Object> rawEntities = GetBackingDataList();
                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_InventoryItem baseEntity = (MyObjectBuilder_InventoryItem)InventoryItemEntity.InvokeEntityMethod(entity, InventoryItemEntity.InventoryItemGetObjectBuilderMethod);
                        if (baseEntity == null)
                        {
                            continue;
                        }

                        uint entityItemId = InventoryItemEntity.GetInventoryItemId(entity);
                        long itemId       = baseEntity.ItemId;

                        //If the original data already contains an entry for this, skip creation
                        if (internalDataCopy.ContainsKey(itemId))
                        {
                            InventoryItemEntity matchingItem = (InventoryItemEntity)GetEntry(itemId);
                            if (matchingItem == null || matchingItem.IsDisposed)
                            {
                                continue;
                            }

                            matchingItem.BackingObject = entity;
                            matchingItem.ObjectBuilder = baseEntity;
                        }
                        else
                        {
                            InventoryItemEntity newItemEntity = new InventoryItemEntity(baseEntity, entity);
                            newItemEntity.Container = m_parent;

                            AddEntry(newItemEntity.ItemId, newItemEntity);
                        }
                    }
                    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);
            }
        }
		protected override void LoadDynamic()
		{
			try
			{
				List<Object> rawEntities = GetBackingDataList();
				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_InventoryItem baseEntity = (MyObjectBuilder_InventoryItem)InventoryItemEntity.InvokeEntityMethod(entity, InventoryItemEntity.InventoryItemGetObjectBuilderMethod);
						if (baseEntity == null)
							continue;

						uint entityItemId = InventoryItemEntity.GetInventoryItemId(entity);
						long itemId = baseEntity.ItemId;

						//If the original data already contains an entry for this, skip creation
						if (internalDataCopy.ContainsKey(itemId))
						{
							InventoryItemEntity matchingItem = (InventoryItemEntity)GetEntry(itemId);
							if (matchingItem == null || matchingItem.IsDisposed)
								continue;

							matchingItem.BackingObject = entity;
							matchingItem.ObjectBuilder = baseEntity;
						}
						else
						{
							InventoryItemEntity newItemEntity = new InventoryItemEntity(baseEntity, entity);
							newItemEntity.Container = m_parent;

							AddEntry(newItemEntity.ItemId, newItemEntity);
						}
					}
					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);
			}
		}
		public void UpdateItemAmount(InventoryItemEntity item, float newAmount)
		{
			InventoryDelta delta = new InventoryDelta();
			delta.item = item;
			delta.oldAmount = item.Amount;
			delta.newAmount = newAmount;

			m_itemDeltaQueue.Enqueue(delta);

			Action action = InternalUpdateItemAmount;
			SandboxGameAssemblyWrapper.Instance.EnqueueMainGameAction(action);
		}
		public InventoryItemEntity NewEntry()
		{
			MyObjectBuilder_InventoryItem defaults = new MyObjectBuilder_InventoryItem();
			SerializableDefinitionId itemTypeId = new SerializableDefinitionId(typeof(MyObjectBuilder_Ore), "Stone");
			defaults.PhysicalContent = (MyObjectBuilder_PhysicalObject)MyObjectBuilderSerializer.CreateNewObject(itemTypeId);
			defaults.Amount = 1;

			InventoryItemEntity newItem = new InventoryItemEntity(defaults);
			newItem.ItemId = NextItemId;
			m_itemManager.NewEntry<InventoryItemEntity>(newItem);

			NextItemId = NextItemId + 1;

			RefreshInventory();

			return newItem;
		}
Ejemplo n.º 16
0
        protected void InternalUpdateItemAmount()
        {
            try
            {
                if (m_itemToUpdate == null)
                    return;

                Decimal delta = m_newItemAmount - m_oldItemAmount;

                MethodInfo method;
                Object[] parameters;
                if (delta > 0)
                {
                    method = BackingObject.GetType().GetMethod(InventoryAddItemAmountMethod);
                    parameters = new object[] {
                        delta,
                        m_itemToUpdate.ObjectBuilder.PhysicalContent,
                        Type.Missing
                    };
                }
                else
                {
                    Type[] argTypes = new Type[3];
                    argTypes[0] = typeof(Decimal);
                    argTypes[1] = typeof(MyObjectBuilder_PhysicalObject);
                    argTypes[2] = typeof(bool);

                    method = BackingObject.GetType().GetMethod(InventoryRemoveItemAmountMethod, argTypes);
                    parameters = new object[] {
                        -delta,
                        m_itemToUpdate.ObjectBuilder.PhysicalContent,
                        Type.Missing
                    };
                }

                method.Invoke(BackingObject, parameters);

                m_itemToUpdate = null;
                m_oldItemAmount = 0;
                m_newItemAmount = 0;
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
Ejemplo n.º 17
0
        public bool NewEntry( InventoryItemEntity source )
        {
            /*
            m_itemManager.AddEntry<InventoryItemEntity>(NextItemId, source);

            NextItemId = NextItemId + 1;
            //TODO - Figure out the right way to add new items
            //Just updating an item amount doesn't seem to work right
            UpdateItemAmount(source, source.Amount * 2);

            RefreshInventory();
            */

            if ( BackingObject != null )
            {
                SandboxGameAssemblyWrapper.Instance.GameAction( new Action( delegate( )
                    {
                        IMyInventory inventory = (IMyInventory)BackingObject;
                        //inventory.AddItems( (MyFixedPoint)source.Amount, source.PhysicalContent );
                    } ) );
            }

            return true;
        }
Ejemplo n.º 18
0
 public void UpdateItemAmount( InventoryItemEntity item, Decimal newAmount )
 {
     UpdateItemAmount( item, (float)newAmount );
 }
		private void BTN_InventoryItem_Add_Click(object sender, EventArgs e)
		{
			if (CMB_ItemType.SelectedItem == null)
				return;
			if (Amount <= 0.0f)
				return;

			try
			{
				MyObjectBuilder_InventoryItem objectBuilder = MyObjectBuilderSerializer.CreateNewObject<MyObjectBuilder_InventoryItem>();
                objectBuilder.Content = MyObjectBuilderSerializer.CreateNewObject(SelectedType.TypeId, SelectedType.SubtypeId.ToString());
				objectBuilder.Amount = (MyFixedPoint)Amount;
				InventoryItemEntity newItem = new InventoryItemEntity(objectBuilder);

				InventoryContainer.NewEntry(newItem);

				this.Close();
			}
			catch (Exception ex)
			{
				LogManager.ErrorLog.WriteLine(ex);
			}
		}
Ejemplo n.º 20
0
        public void UpdateItemAmount( InventoryItemEntity item, float newAmount )
        {
            if ( BackingObject != null )
            {
                /*
                SandboxGameAssemblyWrapper.Instance.GameAction( new Action( delegate( )
                {
                    IMyInventory myInventory = (IMyInventory)BackingObject;
                    if ( newAmount == 0 )
                        myInventory.RemoveItems( item.ItemId );
                    else if ( newAmount > item.Amount )
                        myInventory.AddItems( (MyFixedPoint)( newAmount - item.Amount ), item.PhysicalContent, (int)item.ItemId );
                    else if ( newAmount < item.Amount )
                        myInventory.RemoveItemsAt( (int)item.ItemId, (MyFixedPoint)( item.Amount - newAmount ), true );
                } ) );
                */
            }

            /*
            InventoryDelta delta = new InventoryDelta();
            delta.item = item;
            delta.oldAmount = item.Amount;
            delta.newAmount = newAmount;

            m_itemDeltaQueue.Enqueue(delta);

            Action action = InternalUpdateItemAmount;
            MySandboxGame.Static.Invoke(action);
             */
        }
Ejemplo n.º 21
0
 public void UpdateItemAmount(InventoryItemEntity item, Decimal newAmount)
 {
     UpdateItemAmount(item, (float)newAmount);
 }
        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 GetBackingDataList())
                {
                    if (!IsValidEntity(entity))
                    {
                        continue;
                    }

                    MyObjectBuilder_InventoryItem baseEntity = (MyObjectBuilder_InventoryItem)InventoryItemEntity.InvokeEntityMethod(entity, InventoryItemEntity.InventoryItemGetObjectBuilderMethod);
                    if (baseEntity == null)
                    {
                        continue;
                    }

                    m_rawDataObjectBuilderList.Add(entity, baseEntity);
                }

                m_rawDataObjectBuilderListResourceLock.ReleaseExclusive();
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
                m_rawDataObjectBuilderListResourceLock.ReleaseExclusive();
            }
        }