Exemple #1
0
        internal void Persist(IRSRuntimeComponent inComponent, int inFlags, ref RSPersistComponentData outData)
        {
            if (outData == null)
            {
                outData = new RSPersistComponentData();
            }

            outData.ComponentType = IdHash;

            if (m_PersistFields != null)
            {
                Array.Resize(ref outData.NamedValues, m_PersistFields.Count);

                int idx = 0;
                foreach (var persistField in m_PersistFields.Values)
                {
                    string  name  = persistField.Name;
                    RSValue value = persistField.Persist(inComponent);
                    outData.NamedValues[idx++] = new RSNamedValue(name, value);
                }
            }

            if (m_UseCustomDataField)
            {
                ((IRSCustomPersistDataProvider)inComponent).GetCustomPersistData(ref outData.CustomData, inFlags);
            }
        }
Exemple #2
0
        internal void Restore(IRSRuntimeComponent inComponent, RSPersistComponentData inData, RSEnvironment inEnvironment, int inFlags)
        {
            if (inData.NamedValues != null)
            {
                for (int i = 0; i < inData.NamedValues.Length; ++i)
                {
                    string  name  = inData.NamedValues[i].Name;
                    RSValue value = inData.NamedValues[i].Value;

                    RSPersistFieldInfo fieldInfo;
                    m_PersistFields.TryGetValue(name, out fieldInfo);
                    if (fieldInfo != null)
                    {
                        fieldInfo.Restore(inComponent, value, inEnvironment);
                    }
                }
            }

            if (m_UseCustomDataField)
            {
                ((IRSCustomPersistDataProvider)inComponent).RestoreCustomPersistData(inData.CustomData, inFlags, inEnvironment);
            }
        }
Exemple #3
0
        /// <summary>
        /// Restores persistent data to the given runtime entity and its components.
        /// </summary>
        static public bool Restore(RSEnvironment inEnvironment, RSPersistEntityData inData, IRSRuntimeEntity inEntity, IReadOnlyList <IRSRuntimeComponent> inComponents, int inFlags)
        {
            if (inEntity == null)
            {
                return(false);
            }

            using (PooledList <IRSPersistListener> persistListeners = PooledList <IRSPersistListener> .Alloc())
            {
                IRSPersistListener entityPersistListener = inEntity as IRSPersistListener;
                if (entityPersistListener != null)
                {
                    entityPersistListener.OnPreRestore(inEnvironment);
                    persistListeners.Add(entityPersistListener);
                }

                inEntity.SetActiveWithoutNotify(inData.Active);
                inEntity.RuleTable?.Restore(inData.TableData);
                for (int i = 0, length = inData.ComponentData.Length; i < length; ++i)
                {
                    RSPersistComponentData componentData = inData.ComponentData[i];

                    RSComponentInfo componentInfo = inEnvironment.Library.GetComponent(componentData.ComponentType);
                    if (componentInfo == null)
                    {
                        continue;
                    }

                    IRSRuntimeComponent component = inEntity.GetRSComponent(componentInfo.OwnerType);
                    if (component == null)
                    {
                        continue;
                    }

                    if (inComponents != null)
                    {
                        bool bIncluded = false;
                        for (int allCompIdx = 0, allCompLength = inComponents.Count; allCompIdx < allCompLength; ++allCompIdx)
                        {
                            IRSRuntimeComponent includedComponent = inComponents[allCompIdx];
                            if (includedComponent == component)
                            {
                                bIncluded = true;
                                break;
                            }
                        }

                        if (!bIncluded)
                        {
                            continue;
                        }
                    }

                    IRSPersistListener componentListener = component as IRSPersistListener;
                    if (componentListener != null)
                    {
                        componentListener.OnPreRestore(inEnvironment);
                        persistListeners.Add(componentListener);
                    }

                    componentInfo.Restore(component, componentData, inEnvironment, inFlags);
                }

                IRSCustomPersistDataProvider customDataProvider = inEntity as IRSCustomPersistDataProvider;
                if (customDataProvider != null)
                {
                    customDataProvider.RestoreCustomPersistData(inData.CustomData, inFlags, inEnvironment);
                }

                foreach (var listener in persistListeners)
                {
                    listener.OnPostRestore(inEnvironment);
                }
            }

            return(true);
        }
Exemple #4
0
        /// <summary>
        /// Generates persistent data from the given runtime entity and its components.
        /// </summary>
        static public bool Persist(RSLibrary inLibrary, IRSRuntimeEntity inEntity, IReadOnlyList <IRSRuntimeComponent> inComponents, int inFlags, ref RSPersistEntityData outData)
        {
            if (inEntity == null)
            {
                return(false);
            }

            if (outData == null)
            {
                outData = new RSPersistEntityData();
            }

            outData.EntityId = inEntity.Id;
            outData.Active   = inEntity.IsActive();
            inEntity.RuleTable?.Persist(ref outData.TableData);

            using (PooledList <IRSPersistListener> persistListeners = PooledList <IRSPersistListener> .Alloc())
            {
                IRSPersistListener entityPersistListener = inEntity as IRSPersistListener;
                if (entityPersistListener != null)
                {
                    entityPersistListener.OnPrePersist();
                    persistListeners.Add(entityPersistListener);
                }

                using (PooledList <RSPersistComponentData> persistedComponentData = PooledList <RSPersistComponentData> .Alloc())
                {
                    for (int i = 0, length = inComponents.Count; i < length; ++i)
                    {
                        IRSRuntimeComponent component = inComponents[i];
                        if (component == null)
                        {
                            continue;
                        }

                        IRSPersistListener componentListener = component as IRSPersistListener;
                        if (componentListener != null)
                        {
                            componentListener.OnPrePersist();
                            persistListeners.Add(componentListener);
                        }

                        RSComponentInfo componentInfo = inLibrary.GetComponent(component.GetType());
                        if (componentInfo == null)
                        {
                            continue;
                        }

                        RSPersistComponentData compData = outData.FindComponentData(componentInfo.IdHash);
                        componentInfo.Persist(component, inFlags, ref compData);
                        persistedComponentData.Add(compData);
                    }

                    Array.Resize(ref outData.ComponentData, persistedComponentData.Count);
                    for (int i = 0; i < outData.ComponentData.Length; ++i)
                    {
                        outData.ComponentData[i] = persistedComponentData[i];
                    }
                }

                IRSCustomPersistDataProvider customDataProvider = inEntity as IRSCustomPersistDataProvider;
                if (customDataProvider != null)
                {
                    customDataProvider.GetCustomPersistData(ref outData.CustomData, inFlags);
                }

                foreach (var listener in persistListeners)
                {
                    listener.OnPostPersist();
                }
            }

            return(true);
        }