Exemplo n.º 1
0
        /// <summary>
        /// Initializes the RuntimeTemplate with data from the given ContentTemplate.
        /// </summary>
        public void Initialize(ContentTemplateSerializationFormat template)
        {
            TemplateId = template.TemplateId;
            PrettyName = template.PrettyName;

            foreach (Data.IData data in template.DefaultDataInstances)
            {
                _defaultDataInstances[DataAccessorFactory.GetId(data)] = data;
            }
        }
Exemplo n.º 2
0
        public void Initialize(ContentEntitySerializationFormat format)
        {
            PrettyName = format.PrettyName ?? "";
            UniqueId   = format.UniqueId;

            foreach (DataInstance data in format.Data)
            {
                _data[DataAccessorFactory.GetId(data.CurrentData)] = data;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempts to retrieve a data instance with the given DataAccessor from the list of added
        /// data.
        /// </summary>
        /// <param name="accessor">The DataAccessor to lookup</param>
        /// <returns>A data instance, or null if it cannot be found</returns>
        private Data.IData GetAddedData_unlocked(DataAccessor accessor)
        {
            int id = accessor.Id;

            // TODO: optimize this so we don't have to search through all added data... though
            // this should actually be pretty quick
            for (int i = 0; i < _toAddStage1.Count; ++i)
            {
                int addedId = DataAccessorFactory.GetId(_toAddStage1[i].GetType());
                if (addedId == id)
                {
                    return(_toAddStage1[i]);
                }
            }

            return(null);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Creates a new DataAccessor that accesses the specified Data type.
 /// </summary>
 /// <param name="dataType">The type of Data to retrieve; note that this parameter must be a
 /// subtype of Data</param>
 public DataAccessor(Type dataType)
     : this()
 {
     Id = DataAccessorFactory.GetId(dataType);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Applies data state changes to the entity.
        /// </summary>
        /// <remarks>
        /// This function is not thread-safe; no other API calls can be made to the Entity while
        /// this function is being executed.
        /// </remarks>
        /// <returns>If more data state change updates are needed</returns>
        public void DataStateChangeUpdate()
        {
            // do removals
            {
                for (int i = 0; i < _toRemoveStage1.Count; ++i)
                {
                    int id = _toRemoveStage1[i].Id;
                    _removedLastFrame[id] = _toRemoveStage1[i];
                    // _removedLastFrame[id] is removed in stage2

                    _eventDispatcher.Submit(RemovedDataEvent.Create(this, DataAccessorFactory.GetTypeFromId(id)));
                }

                for (int i = 0; i < _toRemoveStage2.Count; ++i)
                {
                    int id = _toRemoveStage2[i].Id;
                    _removedLastFrame.Remove(id);
                    _data.Remove(id);
                }
                _toRemoveStage2.Clear();

                Utils.Swap(ref _toRemoveStage1, ref _toRemoveStage2);
            }

            // We don't throw an exception immediately. If we are throwing an exception, that means
            // that the Entity is in an invalid state (adding a bad data instance). However, the
            // only way to recover from that invalid state is by having this method terminate.
            // Hence, we only throw an exception at the end of the method.
            Exception exceptionToThrow = null;

            // do additions
            for (int i = 0; i < _toAddStage1.Count; ++i)
            {
                Data.IData added = _toAddStage1[i];
                int        id    = DataAccessorFactory.GetId(added.GetType());

                // make sure we do not readd the same data instance twice
                if (_data.ContainsKey(id))
                {
                    exceptionToThrow = new AlreadyAddedDataException(this, new DataAccessor(added));
                    continue;
                }

                _addedLastFrame[id] = added;

                if (added is Data.IVersioned)
                {
                    Data.IVersioned versioned = (Data.IVersioned)added;

                    _data[id] = new VersionedDataContainer(versioned.Duplicate(), versioned.Duplicate(), versioned.Duplicate());
                }
                else
                {
                    Data.NonVersioned nonVersioned = (Data.NonVersioned)added;
                    _data[id] = new NonVersionedDataContainer(nonVersioned);
                }

                // visualize the initial data
                _eventDispatcher.Submit(AddedDataEvent.Create(this, added.GetType()));
            }

            for (int i = 0; i < _toAddStage2.Count; ++i)
            {
                _addedLastFrame.Remove(new DataAccessor(_toAddStage2[i]).Id);
            }
            _toAddStage2.Clear();

            Utils.Swap(ref _toAddStage1, ref _toAddStage2);

            if (exceptionToThrow != null)
            {
                throw exceptionToThrow;
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Adds a default data instance to the template. The template "owns" the passed data
 /// instance; a copy is not made of it.
 /// </summary>
 /// <remarks>
 /// If the ITemplate is currently being backed by an IGameEngine, this will throw an
 /// InvalidOperationException.
 /// </remarks>
 /// <param name="data">The data instance to copy from.</param>
 public void AddDefaultData(Data.IData data)
 {
     _defaultDataInstances[DataAccessorFactory.GetId(data)] = data;
 }