예제 #1
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);
        }
예제 #2
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);
        }