Esempio n. 1
0
        private IWorld LoadWorld(ZipArchive archive, string worldFileName, SaveLoadMetadata metadata, IReadOnlyDictionary <string, Type> typeAliases, IDictionary <string, IAsset> assets, IReadOnlyList <IResource> generatedResources)
        {
            IWorld world;
            var    worldEntry = archive.GetEntry(worldFileName) ?? throw new Exception(worldFileName + " not found.");

            using (var reader = trwFactory.JsonReader(worldEntry.Open()))
                using (var convertedReader = converterContainer.ConvertWorldReader(reader, metadata.Version, converterContainer.CurrentVersion))
                    using (var context = saveLoadFactory.WorldReadContext(convertedReader, typeAliases))
                    {
                        context.Bag.Add(SaveLoadConstants.AssetDictBagKey, assets);
                        context.Bag.Add(SaveLoadConstants.GeneratedResourcesBagKey, generatedResources);
                        world = context.Read <IWorld>();
                    }

            return(world);
        }
Esempio n. 2
0
        private IDictionary <string, IAsset> LoadAssets(ZipArchive archive, SaveLoadMetadata metadata, IReadOnlyDictionary <string, Type> typeAliases, IFileLoadInfo loadInfo)
        {
            AssetSaveLoadInfo[] assetInfos;
            var assetInfoEntry = archive.GetEntry("assetInfos.json") ?? throw new Exception("assetInfo.json not found.");

            using (var reader = trwFactory.JsonReader(assetInfoEntry.Open()))
                using (var convertedReader = converterContainer.ConvertAssetInfoReader(reader, metadata.Version, converterContainer.CurrentVersion))
                    using (var context = saveLoadFactory.AssetsInfoReadContext(convertedReader, typeAliases))
                        assetInfos = context.Read <AssetSaveLoadInfo[]>();

            var assets           = new Dictionary <string, IAsset>();
            var zipFileSystem    = new ZipFileSystem(archive);
            var actualFileSystem = new ActualFileSystem();

            foreach (var assetInfo in assetInfos)
            {
                var assetLoadInfo = new AssetLoadInfo
                {
                    AssetName     = assetInfo.AssetName,
                    StorageType   = assetInfo.StorageType,
                    ReferencePath = assetInfo.ReferencePath
                };
                switch (assetInfo.StorageType)
                {
                case AssetStorageType.CopyLocal:
                    assetLoadInfo.FileSystem = zipFileSystem;
                    assetLoadInfo.LoadPath   = assetInfo.LocalCopyPath;
                    break;

                case AssetStorageType.ReferenceOriginal:
                    assetLoadInfo.FileSystem = actualFileSystem;
                    assetLoadInfo.LoadPath   = assetInfo.ReferencePath;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                var asset = loadInfo.OnFoundAsset(assetLoadInfo);
                assets.Add(asset.Name, asset);
            }

            return(assets);
        }
Esempio n. 3
0
        private IReadOnlyList <IResource> LoadGeneratedResources(ZipArchive archive, SaveLoadMetadata metadata, IReadOnlyDictionary <string, Type> typeAliases)
        {
            GeneratedResourceSaveLoadInfo[] generatedResourceInfos;
            var generatedResourceInfoEntry = archive.GetEntry("generatedResourceInfos.json") ?? throw new Exception("generatedResourceInfos.json not found.");

            using (var reader = trwFactory.JsonReader(generatedResourceInfoEntry.Open()))
                using (var convertedReader = converterContainer.ConvertGeneratedResourceInfoReader(reader, metadata.Version, converterContainer.CurrentVersion))
                    using (var context = saveLoadFactory.GeneratedResourceInfoReadContext(convertedReader, typeAliases))
                        generatedResourceInfos = context.Read <GeneratedResourceSaveLoadInfo[]>();

            var generatedResources = new List <IResource>();
            var zipFileSystem      = new ZipFileSystem(archive);

            foreach (var info in generatedResourceInfos)
            {
                generatedResources.Add(resourceLoadingService.Load(info.Type, zipFileSystem, info.Path).WithSource(x => new GeneratedResourceSource(x, info.Type)));
            }

            return(generatedResources);
        }
Esempio n. 4
0
        private IReadOnlyDictionary <string, Type> LoadTypeAliases(ZipArchive archive, SaveLoadMetadata metadata)
        {
            var aliasesEntry = archive.GetEntry("typeAliases.json") ?? throw new Exception("typeAliases.json not found.");

            using (var reader = trwFactory.JsonReader(aliasesEntry.Open()))
                using (var convertedReader = converterContainer.ConvertAliasesReader(reader, metadata.Version, converterContainer.CurrentVersion))
                    using (var context = saveLoadFactory.AliasesReadContext(convertedReader))
                    {
                        var rawAliases = context.Read <Dictionary <string, string> >();
                        return(rawAliases.ToDictionary(x => x.Key, x => Type.GetType(x.Value)));
                    }
        }