public IImage Image(string path)
 {
     using (var stream = embeddedResourceFiles.FileSystem.OpenRead(path))
         return(imageLoader.TryLoad(stream, out var image, out var error)
             ? image.WithSource(new EmbeddedResourceSource(this, EmbeddedResourceType.Image, path))
             : throw new DataLoadException(error));
 }
        public AssetLoadResultByLoader Load(AssetLoadInfo loadInfo)
        {
            IImage image;
            var    loadPath = loadInfo.LoadPath;
            var    fileData = loadInfo.FileSystem.ReadAllBytes(loadPath);

            using (var stream = new MemoryStream(fileData))
                if (!imageLoader.TryLoad(stream, out image, out var error))
                {
                    return(AssetLoadResultByLoader.Failure("FAILED", error));
                }
            var hash  = AssetHashMd5.FromSingleFile(fileData);
            var asset = new Asset(loadInfo.AssetName, image, loadInfo.StorageType, hash, loadInfo.ReferencePath, Path.GetFileName(loadInfo.ReferencePath));

            return(AssetLoadResultByLoader.Success(asset));
        }
        public bool TryLoad(Type type, IFileSystem fileSystem, string path, out IResource resource)
        {
            if (type != typeof(IImage))
            {
                resource = null;
                return(false);
            }

            using (var stream = fileSystem.OpenRead(path))
            {
                if (imageLoader.TryLoad(stream, out var image, out _))
                {
                    resource = image;
                    return(true);
                }

                // todo: write log
                resource = null;
                return(false);
            }
        }
Example #4
0
        public bool TryLoad(IReadOnlyFileSystem fileSystem, string path, out ISkybox skybox, out string[] imageFileRelativePaths, out ErrorInfo error)
        {
            dynamic mainFile;

            using (var reader = trwFactory.JsonReader(fileSystem.OpenRead(path)))
                mainFile = reader.ReadAsDynamic();
            imageFileRelativePaths = new string[]
            {
                mainFile.Right,
                mainFile.Left,
                mainFile.Top,
                mainFile.Bottom,
                mainFile.Back,
                mainFile.Front
            };
            var folderPath = Path.Combine(Path.GetDirectoryName(path));
            var images     = new IImage[6];

            for (var i = 0; i < 6; i++)
            {
                var relPath = imageFileRelativePaths[i];
                using (var stream = fileSystem.OpenRead(Path.Combine(folderPath, relPath)))
                    if (!imageLoader.TryLoad(stream, out images[i], out error))
                    {
                        skybox = null;
                        return(false);
                    }
            }

            var width = images[0].Size.Width;

            if (images.Any(x => x.Size != new IntSize2(width, width)))
            {
                error  = new ErrorInfo("Skybox images are not of equal size");
                skybox = null;
                return(false);
            }
            skybox = new Skybox(ResourceVolatility.Immutable, width, images);
            return(true);
        }