コード例 #1
0
        public void StoreMetadata(AssetType type, string path, IAssetMetadata metadata)
        {
            var metaDataPath = GetMetadataFilePath(type, path);

            var data = JsonConvert.SerializeObject(metadata);
            File.WriteAllText(metaDataPath, data);
        }
コード例 #2
0
        public void StoreMetadata(AssetType type, string path, IAssetMetadata metadata)
        {
            var metaDataPath = GetMetadataFilePath(type, path);

            var data = JsonConvert.SerializeObject(metadata);

            File.WriteAllText(metaDataPath, data);
        }
コード例 #3
0
 public ScenarioAsset(string path, IAssetMetadata metadata, AssetWithImageCachedData cachedData)
     : base(path, metadata, cachedData, AssetType.Scenario)
 {
     if (cachedData == null)
     {
         throw new ArgumentNullException(nameof(cachedData));
     }
     _cachedData = cachedData;
 }
コード例 #4
0
        protected Asset(string path, IAssetMetadata metadata, IAssetCachedData cachedData, AssetType type)
        {
            if (path == null) throw new ArgumentNullException(nameof(path));
            if (cachedData == null) throw new ArgumentNullException(nameof(cachedData));

            InstallationPath = path;
            _metadata = metadata;
            _cachedData = cachedData;
            Type = type;
        }
コード例 #5
0
ファイル: Asset.cs プロジェクト: calintx3/calin
        protected Asset(string path, IAssetMetadata metadata, IAssetCachedData cachedData, AssetType type)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (cachedData == null)
            {
                throw new ArgumentNullException(nameof(cachedData));
            }

            InstallationPath = path;
            _metadata        = metadata;
            _cachedData      = cachedData;
            Type             = type;
        }
コード例 #6
0
        public async Task<IAssetCachedData> GetData(AssetType type, IAssetMetadata metadata, string path)
        {
            var metaType = GetCachedDataType(type);
            var metaDataPath = GetCachedDataFilePath(type, path);

            if (!File.Exists(metaDataPath))
            {
                var newInstance = await GenerateCachableData(type, metadata, path);

                StoreData(type, path, newInstance);
                return newInstance;
            }

            var data = File.ReadAllText(metaDataPath);
            return JsonConvert.DeserializeObject(data, metaType) as IAssetCachedData;
        }
コード例 #7
0
        public async Task <IAssetCachedData> GetData(AssetType type, IAssetMetadata metadata, string path)
        {
            var metaType     = GetCachedDataType(type);
            var metaDataPath = GetCachedDataFilePath(type, path);

            if (!File.Exists(metaDataPath))
            {
                var newInstance = await GenerateCachableData(type, metadata, path);

                StoreData(type, path, newInstance);
                return(newInstance);
            }

            var data = File.ReadAllText(metaDataPath);

            return(JsonConvert.DeserializeObject(data, metaType) as IAssetCachedData);
        }
コード例 #8
0
 public BlueprintAsset(string path, IAssetMetadata metadata, IAssetCachedData cachedData)
     : base(path, metadata, cachedData, AssetType.Blueprint)
 {
 }
コード例 #9
0
 public SavegameAsset(string path, IAssetMetadata metadata, AssetWithImageCachedData cachedData)
     : base(path, metadata, cachedData, AssetType.Savegame)
 {
     if (cachedData == null) throw new ArgumentNullException(nameof(cachedData));
     _cachedData = cachedData;
 }
コード例 #10
0
 public BlueprintAsset(string path, IAssetMetadata metadata, IAssetCachedData cachedData)
     : base(path, metadata, cachedData, AssetType.Blueprint)
 {
 }
コード例 #11
0
        private async Task <IAssetCachedData> GenerateCachableData(AssetType type, IAssetMetadata metadata, string path)
        {
            try
            {
                switch (type)
                {
                case AssetType.Blueprint:
                    var blueprint = BlueprintConverter.DeserializeFromFile(path);
                    return(new AssetCachedData
                    {
                        Name = blueprint.Header.Name
                    });

                case AssetType.Mod:
                    if (metadata?.Id == null)
                    {
                        return(new AssetWithImageCachedData());
                    }

                    var asset = await _website.API.GetAsset(metadata.Id);

                    using (var memoryStream = new MemoryStream())
                    {
                        try
                        {
                            using (var image = await asset.Image.Data.Get())
                            {
                                if (image == null)
                                {
                                    return(new AssetWithImageCachedData
                                    {
                                        ImageBase64 = null
                                    });
                                }

                                image.Save(memoryStream, ImageFormat.Png);
                                return(new AssetWithImageCachedData
                                {
                                    ImageBase64 = Convert.ToBase64String(memoryStream.ToArray())
                                });
                            }
                        }
                        catch (Exception e)
                        {
                            _log.WriteLine("Failed to load image!");
                            _log.WriteException(e);
                            return(new AssetWithImageCachedData());
                        }
                    }

                case AssetType.Savegame:
                    var savegame = SavegameConverter.DeserializeFromFile(path);

                    using (var stream = new MemoryStream())
                    {
                        using (var thumbnail = savegame.Screenshot)
                        {
                            thumbnail.Save(stream, ImageFormat.Png);
                        }

                        return(new AssetWithImageCachedData
                        {
                            Name = savegame.Header.Name,
                            ImageBase64 = Convert.ToBase64String(stream.ToArray())
                        });
                    }

                default:
                    throw new Exception("Unsupported type");
                }
            }
            catch (Exception e)
            {
                _log.WriteException(e);
                return(new AssetCachedData
                {
                    Name = "Failed to open image for " + path
                });
            }
        }
コード例 #12
0
        private async Task<IAssetCachedData> GenerateCachableData(AssetType type, IAssetMetadata metadata, string path)
        {
            try
            {
                switch (type)
                {
                    case AssetType.Blueprint:
                        var blueprint = BlueprintConverter.DeserializeFromFile(path);
                        return new AssetCachedData
                        {
                            Name = blueprint.Header.Name
                        };
                    case AssetType.Mod:
                        if (metadata?.Id == null)
                            return new AssetWithImageCachedData();
                        
                        var asset = await _website.API.GetAsset(metadata.Id);
                        using (var memoryStream = new MemoryStream())
                        {
                            try
                            {
                                using (var image = await asset.Image.Data.Get())
                                {
                                    if (image == null)
                                    {
                                        return new AssetWithImageCachedData
                                        {
                                            ImageBase64 = null
                                        };
                                    }
                                    
                                    image.Save(memoryStream, ImageFormat.Png);
                                    return new AssetWithImageCachedData
                                    {
                                        ImageBase64 = Convert.ToBase64String(memoryStream.ToArray())
                                    };
                                }
                            }
                            catch (Exception e)
                            {
                                _log.WriteLine("Failed to load image!");
                                _log.WriteException(e);
                                return new AssetWithImageCachedData();
                            }
                        }
                    case AssetType.Savegame:
                        var savegame = SavegameConverter.DeserializeFromFile(path);

                        using (var stream = new MemoryStream())
                        {
                            using (var thumbnail = savegame.Screenshot)
                            {
                                thumbnail.Save(stream, ImageFormat.Png);
                            }

                            return new AssetWithImageCachedData
                            {
                                Name = savegame.Header.Name,
                                ImageBase64 = Convert.ToBase64String(stream.ToArray())
                            };
                        }
                    default:
                        throw new Exception("Unsupported type");
                }
            }
            catch (Exception e)
            {
                _log.WriteException(e);
                return new AssetCachedData
                {
                    Name = "Failed to open image for " + path
                };
            }
        }