示例#1
0
        public void UnloadAsset(AssetId assetId)
        {
            if (!_registeredAssets.TryGetValue(assetId, out var registeredAsset))
            {
                throw new AssetNotRegisteredException(assetId);
            }

            if (registeredAsset.IsLoaded)
            {
                Debug.Assert(registeredAsset.AssetInstance != null, "registeredAsset.AssetInstance != null");
                _assetsIds.Remove(registeredAsset.AssetInstance);
                registeredAsset.Unload();
            }
            else
            {
                Log.Debug($"Asset is not loaded. Skipping asset unload. Asset info: {registeredAsset.AssetInfo}");
            }
        }
示例#2
0
        /// <summary>
        ///     Loads <see cref="AssetData" /> from specified stream.
        /// </summary>
        /// <param name="stream">Stream containing asset data.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"><c>AssetType</c> property is null or <c>ContentFormat</c> property is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///     <c>ContentFormat</c> property value is none of available formats
        ///     <see cref="AssetContentFormat" />.
        /// </exception>
        public static AssetData Load(Stream stream)
        {
            using var jsonDocument = JsonDocument.Parse(stream);
            var rootElement         = jsonDocument.RootElement;
            var assetId             = new AssetId(rootElement.GetProperty("AssetId").GetGuid());
            var assetTypeString     = rootElement.GetProperty("AssetType").GetString() ?? throw new InvalidOperationException("AssetType cannot be null.");
            var assetType           = new AssetType(assetTypeString);
            var contentFormatString = rootElement.GetProperty("ContentFormat").GetString() ??
                                      throw new InvalidOperationException("ContentFormat cannot be null.");
            var     contentFormat   = Enum.Parse <AssetContentFormat>(contentFormatString);
            var     contentProperty = rootElement.GetProperty("Content");
            Content content         = contentFormat switch
            {
                AssetContentFormat.Binary => new BinaryContent(contentProperty),
                AssetContentFormat.String => new StringContent(contentProperty),
                AssetContentFormat.Json => new JsonContent(contentProperty),
                _ => throw new ArgumentOutOfRangeException("ContentFormat", contentFormat, "Unknown content format.")
            };

            return(new AssetData(assetId, assetType, content));
        }
示例#3
0
        public TAsset GetAsset <TAsset>(AssetId assetId)
        {
            if (!_registeredAssets.TryGetValue(assetId, out var registeredAsset))
            {
                throw new AssetNotRegisteredException(assetId, typeof(TAsset));
            }
            if (registeredAsset.AssetClassType != typeof(TAsset))
            {
                throw new AssetNotRegisteredException(assetId, typeof(TAsset));
            }

            if (!registeredAsset.IsLoaded)
            {
                Log.Debug($"Asset not yet loaded, will be loaded now. Asset info: {registeredAsset.AssetInfo}");
                registeredAsset.Load();
                Debug.Assert(registeredAsset.AssetInstance != null, "registeredAsset.AssetInstance != null");
                _assetsIds.Add(registeredAsset.AssetInstance, assetId);
            }

            Debug.Assert(registeredAsset.AssetInstance != null, "registeredAsset.AssetInstance != null");
            return((TAsset)registeredAsset.AssetInstance);
        }
示例#4
0
 /// <summary>
 ///     Creates new instance of <see cref="AssetData" /> with specified <see cref="Assets.AssetId" />,
 ///     <see cref="Assets.AssetType" /> and content in JSON format.
 /// </summary>
 /// <param name="assetId"><see cref="Assets.AssetId" /> of <see cref="AssetData" />.</param>
 /// <param name="assetType"><see cref="Assets.AssetType" /> of <see cref="AssetData" />.</param>
 /// <param name="content">
 ///     Instance of <typeparamref name="T" /> to be serialized as JSON content of
 ///     <see cref="AssetData" />.
 /// </param>
 /// <returns>New instance of <see cref="AssetData" /> with <see cref="string" /> content.</returns>
 public static AssetData CreateWithJsonContent <T>(AssetId assetId, AssetType assetType, T content) where T : notnull =>
 new AssetData(assetId, assetType, new JsonContent(content));
示例#5
0
 /// <summary>
 ///     Creates new instance of <see cref="AssetData" /> with specified <see cref="Assets.AssetId" />,
 ///     <see cref="Assets.AssetType" /> and content in <see cref="string" /> format.
 /// </summary>
 /// <param name="assetId"><see cref="Assets.AssetId" /> of <see cref="AssetData" />.</param>
 /// <param name="assetType"><see cref="Assets.AssetType" /> of <see cref="AssetData" />.</param>
 /// <param name="content"><see cref="string" /> to be used as content of <see cref="AssetData" />.</param>
 /// <returns>New instance of <see cref="AssetData" /> with <see cref="string" /> content.</returns>
 public static AssetData CreateWithStringContent(AssetId assetId, AssetType assetType, string content) =>
 new AssetData(assetId, assetType, new StringContent(content));
示例#6
0
 /// <summary>
 ///     Creates new instance of <see cref="AssetData" /> with specified <see cref="Assets.AssetId" />,
 ///     <see cref="Assets.AssetType" /> and content in binary format.
 /// </summary>
 /// <param name="assetId"><see cref="Assets.AssetId" /> of <see cref="AssetData" />.</param>
 /// <param name="assetType"><see cref="Assets.AssetType" /> of <see cref="AssetData" />.</param>
 /// <param name="content"><see cref="Stream" /> to read from the binary content of <see cref="AssetData" />.</param>
 /// <returns>New instance of <see cref="AssetData" /> with binary content.</returns>
 public static AssetData CreateWithBinaryContent(AssetId assetId, AssetType assetType, Stream content) =>
 new AssetData(assetId, assetType, new BinaryContent(content));
示例#7
0
 private AssetData(AssetId assetId, AssetType assetType, Content content)
 {
     AssetId   = assetId;
     AssetType = assetType;
     _content  = content;
 }
示例#8
0
 public AssetNotRegisteredException(AssetId assetId, Type assetType) : base(
         $"Asset of class type {assetType.FullName} with id {assetId} was not registered in an asset store.")
 {
     AssetId   = assetId;
     AssetType = assetType;
 }
示例#9
0
 public AssetNotRegisteredException(AssetId assetId) : base(
         $"Asset with id {assetId} was not registered in an asset store.")
 {
     AssetId = assetId;
 }
示例#10
0
 /// <inheritdoc />
 public bool Equals(AssetInfo other) => AssetId.Equals(other.AssetId) && AssetType.Equals(other.AssetType) && AssetFilePath == other.AssetFilePath;
示例#11
0
 /// <summary>
 ///     Creates new instance of <see cref="AssetInfo" />.
 /// </summary>
 /// <param name="assetId">Id of asset.</param>
 /// <param name="assetType">Type of asset.</param>
 /// <param name="assetFilePath">Path to asset file.</param>
 public AssetInfo(AssetId assetId, AssetType assetType, string assetFilePath)
 {
     AssetId        = assetId;
     AssetType      = assetType;
     _assetFilePath = assetFilePath ?? throw new ArgumentNullException(nameof(assetFilePath));
 }