コード例 #1
0
        /// <summary>
        /// Saves meta info for cached files such as Source, ContentType and Expiry date
        /// </summary>
        /// <param name="cacheFilePath">Full path to the cached (image) file.</param>
        /// <param name="sourceLocation">External source url.</param>
        /// <param name="response">Response object from the external server contining information we want to store in meta data.</param>
        private async Task <AssetMeta> SaveMeta(string metaFilePath, Uri sourceLocation, HttpResponseMessage response)
        {
            if (response != null)
            {
                var meta = new AssetMeta()
                {
                    SourceUrl    = sourceLocation.AbsoluteUri,
                    ContentType  = response.Content.Headers.ContentType?.MediaType,
                    ResponseCode = (int)response.StatusCode,
                    RequestTime  = DateTimeOffset.UtcNow
                                   //Expires = response.Content.Headers?.Expires //unfortunately expires does not always comply with standards (see below)
                };

                IEnumerable <string> expires;
                if (response.Content.Headers.TryGetValues("Expires", out expires))
                {
                    DateTimeOffset expiryDate;
                    var            dateStr = expires.FirstOrDefault()?.Replace("UTC", "GMT"); //Flickr date is not compliant to ISO 8601
                    if (DateTimeOffset.TryParse(dateStr, out expiryDate))
                    {
                        meta.Expires = expiryDate;
                    }
                }

                using (var fileStream = new FileStream(metaFilePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096, true))
                    using (var writer = new StreamWriter(fileStream))
                        await writer.WriteAsync(JsonConvert.SerializeObject(meta));

                return(meta);
            }
            return(null);
        }
コード例 #2
0
        public async ValueTask <Sprite> Load(AssetMeta meta)
        {
            if (null == meta)
            {
                throw new ArgumentNullException(nameof(meta));
            }
            if (null == meta.Properties)
            {
                throw new ArgumentException("properties missing", nameof(AssetMeta.Properties));
            }

            if (!meta.Properties.TryGetValue("width", out var tmp) ||
                !int.TryParse(tmp.ToString(), out var width))
            {
                throw new ArgumentException("invalid width", nameof(AssetMeta.Properties));
            }

            if (!meta.Properties.TryGetValue("height", out tmp) ||
                !int.TryParse(tmp.ToString(), out var height))
            {
                throw new ArgumentException("invalid height", nameof(AssetMeta.Properties));
            }

            var bounds = new Rectangle(0, 0, width, height);

            var elementRef = new ElementReference(Guid.NewGuid().ToString());

            return(new Sprite(meta.Path, elementRef, bounds, meta.Path));
        }
コード例 #3
0
        public async ValueTask <AnimationCollection> Load(AssetMeta meta)
        {
            var dto = await _httpClient.GetFromJsonAsync <AnimationsFile>(meta.Path);

            var asset = dto.ToAsset();

            return(asset);
        }
コード例 #4
0
        public ValueTask <Sound> Load(AssetMeta meta)
        {
            var type  = meta.Properties["type"].ToString();
            var name  = meta.Properties["name"].ToString();
            var sound = new Sound(meta.Path, name, type);

            return(ValueTask.FromResult(sound));
        }
コード例 #5
0
        public async ValueTask <MapObjects> Load(AssetMeta meta)
        {
            var dtos = await _httpClient.GetFromJsonAsync <MapObjectRaw[]>(meta.Path);

            var mapObjects = dtos.Select(d => d.ToModel());

            return(new MapObjects(meta.Path, mapObjects));
        }
コード例 #6
0
        public async ValueTask <SpriteSheet> Load(AssetMeta meta)
        {
            var dto = await _httpClient.GetFromJsonAsync <SpriteSheetDTO>(meta.Path);

            var elementRef = new ElementReference(Guid.NewGuid().ToString());

            var sprites = dto.sprites
                          .Select(s => new SpriteBase(s.name, elementRef, new Rectangle(s.x, s.y, s.width, s.height)))
                          .ToArray();

            return(new SpriteSheet(meta.Path, elementRef, dto.imagePath, sprites));
        }
コード例 #7
0
        private AssetMeta GetOrNewMeta(string asset)
        {
            var       id = HashAssetID(asset);
            AssetMeta meta;

            if (!mAssets.TryGetValue(id, out meta))
            {
                meta        = new AssetMeta(asset);
                mAssets[id] = meta;
            }
            return(meta);
        }
コード例 #8
0
        public async ValueTask <TA> Load <TA>(AssetMeta meta) where TA : IAsset
        {
            if (meta == null)
            {
                throw new ArgumentNullException(nameof(meta));
            }

            var loader = _assetLoaderFactory.Get <TA>();
            var asset  = await loader.Load(meta);

            if (null == asset)
            {
                throw new TypeLoadException($"unable to load asset type '{typeof(TA)}' from path '{meta.Path}'");
            }

            _assets.AddOrUpdate(meta.Path, k => asset, (k, v) => asset);
            return(asset);
        }
コード例 #9
0
        // 加载资源
        private bool LoadAsset <T>(AssetMeta meta, AssetHandler <T> handler, ErrorHandler errorhandler) where T : Object
        {
            if (meta.assetInstence != null)
            {
                if (handler != null)
                {
                    handler(meta.assetInstence as T);
                }
                return(false);
            }
            bool load = false;

            if (meta.abData != null && meta.abData.assetBundle == null)
            {
                load |= LoadAB <AssetBundle>(meta.abData.name, meta.abData, null, null);
            }
            var assethandler = GetHandler(meta.Identify);

            if (assethandler == null)
            {
                assethandler = new AssetObtain <T>(meta);
                mHandlers.AddLast(assethandler);
                mLoaders++;
                load = true;
                assethandler.DoComplete(FinishAndLoadNextAsset);
            }
            if (handler != null)
            {
                assethandler.RegistHandler(handler);
            }
            if (errorhandler != null)
            {
                assethandler.RegistErrorHandler(errorhandler);
            }
            return(load);
        }
コード例 #10
0
 public AssetObtain(AssetMeta meta)
 {
     mMeta = meta;
 }