예제 #1
0
        public void Load(params string[] storageCapsuleIDs)
        {
            using (ActiveRefHandler = new SaveableReferenceIdHandler())
            {
                RefreshCachedData(storageCapsuleIDs);

                foreach (var capsuleToStorage in _cachedStorageCapsules)
                {
                    if (storageCapsuleIDs != null && storageCapsuleIDs.Length > 0 && Array.IndexOf(storageCapsuleIDs, capsuleToStorage.Key.ID) < 0)
                    {
                        continue;
                    }

                    List <ISaveable> _allLoadedReferences   = new List <ISaveable>();
                    List <string>    _allLoadedReferenceIds = new List <string>();

                    Action <string> referenceRequestedEventAction = (id) =>
                    {
                        if (_allLoadedReferenceIds.Contains(id))
                        {
                            return;
                        }

                        _allLoadedReferenceIds.Add(id);

                        if (!capsuleToStorage.Value.TryGetValue(id, out StorageDictionary storage))
                        {
                            storage = new StorageDictionary(capsuleToStorage.Key.ID, this);
                        }

                        if (id == ROOT_SAVE_DATA_CAPSULE_REFERENCE_ID)
                        {
                            capsuleToStorage.Key.Load(storage);
                            _allLoadedReferences.Add(capsuleToStorage.Key);
                        }
                        else if (storage.LoadValue(STORAGE_REFERENCE_TYPE_ID_ULONG_KEY, out ulong classTypeId))
                        {
                            ISaveable referenceInstance = _storageObjectFactory.LoadSaveableObject(classTypeId, storage);
                            ActiveRefHandler.SetReferenceReady(referenceInstance, id);
                            _allLoadedReferences.Add(referenceInstance);
                        }
                        else if (storage.LoadValue(STORAGE_REFERENCE_TYPE_STRING_KEY, out string classTypeFullName))
                        {
                            Type      referenceType       = Type.GetType(classTypeFullName);
                            bool      methodLoadInterface = typeof(ISaveableLoad).IsAssignableFrom(referenceType);
                            ISaveable referenceInstance   = (methodLoadInterface ? Activator.CreateInstance(referenceType) : Activator.CreateInstance(referenceType, storage)) as ISaveable;
                            ActiveRefHandler.SetReferenceReady(referenceInstance, id);

                            if (methodLoadInterface)
                            {
                                ((ISaveableLoad)referenceInstance).Load(storage);
                            }

                            _allLoadedReferences.Add(referenceInstance);
                        }
                        else
                        {
                            Debug.LogErrorFormat("UNABLE TO LOAD REFERENCE ID {0}'s CLASS TYPE NAME", id);
                        }
                    };

                    ActiveRefHandler.ReferenceRequestedEvent += referenceRequestedEventAction;
                    referenceRequestedEventAction(ROOT_SAVE_DATA_CAPSULE_REFERENCE_ID);
                    ActiveRefHandler.LoadRemainingAsNull();
                    ActiveRefHandler.ReferenceRequestedEvent -= referenceRequestedEventAction;

                    for (int i = _allLoadedReferences.Count - 1; i >= 0; i--)
                    {
                        _allLoadedReferences[i].LoadingCompleted();
                    }

                    _allLoadedReferences   = null;
                    _allLoadedReferenceIds = null;
                }
            }
        }