Esempio n. 1
0
        public object Load(string path, ResourceLoadContext context)
        {
            context.AttemptingSyncLoad = true;
            context.ResourceLoader     = this;

            //determine type
            context.ResourceType = DetermineResourceType(path, context);

            //attempt to load with importers
            if (context.ResourceImporter != null)
            {
                try
                {
                    return(context.ResourceImporter.LoadResource(path, context.ResourceType, context));
                }
                catch (Exception e)
                {
                    Debug.LogError($"[ResourceLoader] Exception in asset importer {context.ResourceImporter.GetType().Name}");
                    Debug.LogException(e);
                    throw e;
                }
            }

            //attempt to load builtin
            return(LoadFromFileBuiltin(path, context.ResourceType));
        }
Esempio n. 2
0
        public async Task <T> LoadFromFileAsync <T>(string path, ResourceLoadContext context)
        {
            foreach (var importer in Importers)
            {
                try
                {
                    if (importer.CanLoadResource(path, context))
                    {
                        return((T)await importer.LoadResourceAsync(path, typeof(T), context));
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError($"[ResourceLoader] Exception in asset importer {importer.GetType().Name}");
                    Debug.LogException(e);
                }
            }

            byte[] bytes = null;
            await Task.Run(() => {
                bytes = File.ReadAllBytes(path);
            });

            T resource = await LoadFromBytesAsync <T>(bytes, context);

            if (resource is UnityEngine.Object obj)
            {
                obj.name = Path.GetFileNameWithoutExtension(path);
            }

            return(resource);
        }
Esempio n. 3
0
        public static async Task <object> LoadSpriteAsync(string path, ResourceLoadContext context, SpriteData sd)
        {
            string fullTexturePath = Path.Combine(Path.GetDirectoryName(path), sd.TexturePath);

            Texture2D texture;

            if (sd.TextureIsResource)
            {
                texture = context.ResourceManager.GetResource <Texture2D>(path, false);
            }
            else
            {
                texture = (Texture2D)await context.ResourceLoader.LoadAsync(fullTexturePath, new ResourceLoadContext()
                {
                    AttemptingSyncLoad = false, ResourceLoader = context.ResourceLoader, ResourceManager = context.ResourceManager, ResourceType = typeof(Texture2D)
                });
            }

            if (!sd.Rect.HasValue)
            {
                sd.Rect = new Rect(0, 0, texture.width, texture.height);
            }

            await Task.Yield();

            Sprite sprite = Sprite.Create(texture, sd.Rect.Value, sd.Pivot, sd.PixelsPerUnit, 0, SpriteMeshType.FullRect);

            sprite.name = Path.GetFileNameWithoutExtension(path);

            return(sprite);
        }
Esempio n. 4
0
        private async Task <T> LoadFromBytesAsync <T>(byte[] data, ResourceLoadContext context)
        {
            //TODO actual async implementations

            await Task.Yield();

            return((T)LoadFromBytesBuiltin(data, typeof(T)));
        }
        public Type GetResourceType(string path, ResourceLoadContext context)
        {
            if (Path.GetExtension(path).Equals(".ogg", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(path).Equals(".mp3", StringComparison.OrdinalIgnoreCase))
            {
                return(typeof(AudioClip));
            }

            throw new NotSupportedException();
        }
Esempio n. 6
0
        /// <summary>
        /// Determines the resource type from file type
        /// </summary>
        /// <returns>The type to be used for a resourcehandle, or null on failure</returns>
        public Type DetermineResourceType(string path, ResourceLoadContext context)
        {
            foreach (var importer in Importers)
            {
                try
                {
                    if (importer.CanLoadResource(path, context) && (!context.AttemptingSyncLoad || importer.CanLoadSync))
                    {
                        context.ResourceImporter = importer;
                        return(importer.GetResourceType(path, context)); //note that malicious or malformed importers can scribble context
                    }
                }
                catch (Exception e)
                {
                    Debug.LogError($"[ResourceLoader] Exception in asset importer {importer.GetType().Name}");
                    Debug.LogException(e);
                }
            }

            string extension = Path.GetExtension(path);

            if (!string.IsNullOrEmpty(extension))
            {
                extension = extension.TrimStart('.').ToLower(CultureInfo.InvariantCulture);
                switch (extension)
                {
                case "txt":
                case "htm":
                case "html":
                case "xml":
                case "json":
                case "csv":
                case "yaml":
                case "fnt":
                    return(typeof(TextAsset));

                case "jpg":
                case "jpeg":
                case "png":
                case "tga":
                case "bmp":
                    return(typeof(Texture2D));

                case "wav":
                case "wave":
                case "mp3":
                case "ogg":
                case "oga":
                case "m4a":
                case "flac":
                    return(typeof(AudioClip));
                }
            }

            return(null);
        }
        public bool CanLoadResource(string path, ResourceLoadContext context)
        {
            if (Path.GetExtension(path).Equals(".ogg", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            if (Path.GetExtension(path).Equals(".mp3", StringComparison.OrdinalIgnoreCase))
            {
                return(true);
            }

            return(false);
        }
Esempio n. 8
0
        public async Task <ResourceHandle> AddResourceFromFileAsync(string targetPath, string filePath, ResourcePriority priority)
        {
            ResourceLoadContext context = new ResourceLoadContext()
            {
                AttemptingSyncLoad = false, ResourceManager = this, ResourceLoader = ResourceLoader, TargetPath = targetPath
            };
            var resource = await ResourceLoader.LoadAsync(filePath, context);

            var ro         = RetrieveResourceObject(targetPath);
            var handleType = typeof(FileResourceHandle <>).MakeGenericType(context.ResourceType);
            var rh         = (ResourceHandle)Activator.CreateInstance(handleType, resource, filePath, priority, context);

            ro.AddResourceHandle(rh, resource.GetType());
            return(rh);
        }
Esempio n. 9
0
        public bool CanLoadResource(string path, ResourceLoadContext context)
        {
            if (Path.GetExtension(path).Equals(".jasset", StringComparison.OrdinalIgnoreCase))
            {
                JObject jo = JObject.Parse(File.ReadAllText(path));
                if (!jo.IsNullOrEmpty())
                {
                    string typeName = jo["$assetType"].ToString();
                    if (typeName == "Sprite")
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Esempio n. 10
0
        //replacement for LoadFromFile, probably
        public object LoadTyped(string path, ResourceLoadContext context)
        {
            //context may include type and importer information, use that if possible

            context.AttemptingSyncLoad = true;
            context.ResourceLoader     = this;

            if (context.ResourceImporter == null && context.ResourceType != null)
            {
                Debug.LogWarning($"[ResourceLoader] LoadTyped was given a context with type but no importer"); //should this be a warning?
            }
            if (context.ResourceImporter != null && !context.ResourceImporter.CanLoadSync)
            {
                Debug.LogWarning($"[ResourceLoader] LoadTyped was given an importer that can't load synchronously!");
                context.ResourceImporter = null;
            }

            if (context.ResourceImporter == null)
            {
                context.ResourceType = DetermineResourceType(path, context); //this will also get our importer
            }
            else if (context.ResourceType == null)
            {
                context.ResourceType = context.ResourceImporter.GetResourceType(path, context);
            }

            //attempt to load with importers
            if (context.ResourceImporter != null)
            {
                try
                {
                    return(context.ResourceImporter.LoadResource(path, context.ResourceType, context));
                }
                catch (Exception e)
                {
                    Debug.LogError($"[ResourceLoader] Exception in asset importer {context.ResourceImporter.GetType().Name}");
                    Debug.LogException(e);
                    throw e;
                }
            }

            //attempt to load builtin
            return(LoadFromFileBuiltin(path, context.ResourceType));
        }
        public async Task <object> LoadResourceAsync(string path, Type target, ResourceLoadContext context)
        {
            var aType = AudioType.UNKNOWN;

            if (Path.GetExtension(path).Equals(".ogg", StringComparison.OrdinalIgnoreCase))
            {
                aType = AudioType.OGGVORBIS;
            }
            else if (Path.GetExtension(path).Equals(".mp3", StringComparison.OrdinalIgnoreCase))
            {
                aType = AudioType.MPEG;
            }

            using (var request = UnityWebRequestMultimedia.GetAudioClip($"file://{path}", aType))
            {
                var operation = request.SendWebRequest();

                //dumb AF but fine for now
                while (!operation.isDone)
                {
                    await Task.Delay(60);
                }

                if (request.isNetworkError || request.isHttpError)
                {
                    throw new Exception(request.error);
                }
                else
                {
                    AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
                    clip.name = Path.GetFileNameWithoutExtension(path);
                    return(clip);
                }
            }

            throw new NotImplementedException();
        }
Esempio n. 12
0
 private T LoadFromBytes <T>(byte[] data, ResourceLoadContext context)
 {
     return((T)LoadFromBytesBuiltin(data, typeof(T)));
 }
Esempio n. 13
0
        public async Task <object> LoadResourceAsync(string path, Type target, ResourceLoadContext context)
        {
            SpriteData sd = ReadSpriteData(path);

            return(await LoadSpriteAsync(path, context, sd));
        }
Esempio n. 14
0
        public object LoadResource(string path, Type target, ResourceLoadContext context)
        {
            SpriteData sd = ReadSpriteData(path);

            return(LoadSprite(path, context, sd));
        }
Esempio n. 15
0
 public Type GetResourceType(string path, ResourceLoadContext context)
 {
     return(typeof(Sprite));
 }
 public object LoadResource(string path, Type target, ResourceLoadContext context)
 {
     throw new NotImplementedException();
 }
Esempio n. 17
0
 public FileResourceHandle(T resource, string path, ResourcePriority priority, ResourceLoadContext initialLoadContext) : this(resource, path, priority)
 {
     Importer = initialLoadContext.ResourceImporter;
 }