Exemple #1
0
        public override void Load(IResourceCache cache, ResourcePath path)
        {
            if (!cache.TryContentFileRead(path, out var stream))
            {
                throw new FileNotFoundException("Content file does not exist for font");
            }

            using (stream)
            {
                FontFaceHandle = IoCManager.Resolve <IFontManagerInternal>().Load(stream);
            }
        }
Exemple #2
0
        public override void Load(IResourceCache cache, ResourcePath path)
        {
            if (!cache.TryContentFileRead(path, out var stream))
            {
                throw new FileNotFoundException("Content file does not exist for texture");
            }

            // Primarily for tracking down iCCP sRGB errors in the image files.
            Logger.DebugS("res.tex", $"Loading texture {path}.");

            var loadParameters = _tryLoadTextureParameters(cache, path) ?? TextureLoadParameters.Default;

            var manager = IoCManager.Resolve <IClyde>();

            Texture = manager.LoadTextureFromPNGStream(stream, path.ToString(), loadParameters);
        }
Exemple #3
0
        private static TextureLoadParameters?_tryLoadTextureParameters(IResourceCache cache, ResourcePath path)
        {
            var metaPath = path.WithName(path.Filename + ".yml");

            if (cache.TryContentFileRead(metaPath, out var stream))
            {
                YamlDocument yamlData;
                using (var reader = new StreamReader(stream, EncodingHelpers.UTF8))
                {
                    var yamlStream = new YamlStream();
                    yamlStream.Load(reader);
                    if (yamlStream.Documents.Count == 0)
                    {
                        return(null);
                    }

                    yamlData = yamlStream.Documents[0];
                }

                return(TextureLoadParameters.FromYaml((YamlMappingNode)yamlData.RootNode));
            }
            return(null);
        }
Exemple #4
0
        public override void Load(IResourceCache cache, ResourcePath path)
        {
            if (!cache.TryContentFileRead(path, out var stream))
            {
                throw new FileNotFoundException("Content file does not exist for texture");
            }

            // Primarily for tracking down iCCP sRGB errors in the image files.
            Logger.DebugS("res.tex", $"Loading texture {path}.");

            var loadParameters = _tryLoadTextureParameters(cache, path) ?? TextureLoadParameters.Default;

            var manager = IoCManager.Resolve <IClyde>();

            using var image = Image.Load <Rgba32>(stream);

            Texture = manager.LoadTextureFromImage(image, path.ToString(), loadParameters);

            if (cache is IResourceCacheInternal cacheInternal)
            {
                cacheInternal.TextureLoaded(new TextureLoadedEventArgs(path, image, this));
            }
        }
        public async void LoadParallax()
        {
            if (!_configurationManager.GetCVar <bool>("parallax.enabled"))
            {
                return;
            }

            Stream    configStream = null;
            string    contents;
            TomlTable table;

            try
            {
                // Load normal config into memory
                if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out configStream))
                {
                    Logger.ErrorS("parallax", "Parallax config not found.");
                    return;
                }

                using (var reader = new StreamReader(configStream, EncodingHelpers.UTF8))
                {
                    contents = reader.ReadToEnd();
                }

                if (_resourceCache.UserData.Exists(ParallaxConfigOld))
                {
                    bool match;
                    using (var data = _resourceCache.UserData.Open(ParallaxConfigOld, FileMode.Open))
                        using (var reader = new StreamReader(data, EncodingHelpers.UTF8))
                        {
                            match = reader.ReadToEnd() == contents;
                        }

                    if (match)
                    {
                        using (var stream = _resourceCache.UserData.Open(ParallaxPath, FileMode.Open))
                        {
                            ParallaxTexture = Texture.LoadFromPNGStream(stream, "Parallax");
                        }

                        OnTextureLoaded?.Invoke(ParallaxTexture);
                        return;
                    }
                }

                table = Toml.ReadString(contents);
            }
            finally
            {
                configStream?.Dispose();
            }

            var sawmill = _logManager.GetSawmill("parallax");
            // Generate the parallax in the thread pool.
            var image = await Task.Run(() => ParallaxGenerator.GenerateParallax(table, new Size(1920, 1080), sawmill));

            // And load it in the main thread for safety reasons.
            ParallaxTexture = Texture.LoadFromImage(image, "Parallax");

            // Store it and CRC so further game starts don't need to regenerate it.
            using (var stream = _resourceCache.UserData.Open(ParallaxPath, FileMode.Create))
            {
                image.SaveAsPng(stream);
            }

            using (var stream = _resourceCache.UserData.Open(ParallaxConfigOld, FileMode.Create))
                using (var writer = new StreamWriter(stream, EncodingHelpers.UTF8))
                {
                    writer.Write(contents);
                }

            OnTextureLoaded?.Invoke(ParallaxTexture);
        }
        public async void LoadParallax()
        {
            if (!_configurationManager.GetCVar <bool>("parallax.enabled"))
            {
                return;
            }

            var       debugParallax = _configurationManager.GetCVar <bool>("parallax.debug");
            string    contents;
            TomlTable table;

            // Load normal config into memory
            if (!_resourceCache.TryContentFileRead(ParallaxConfigPath, out var configStream))
            {
                Logger.ErrorS("parallax", "Parallax config not found.");
                return;
            }

            using (configStream)
            {
                using (var reader = new StreamReader(configStream, EncodingHelpers.UTF8))
                {
                    contents = reader.ReadToEnd();
                }

                if (!debugParallax && _resourceCache.UserData.Exists(ParallaxConfigOld))
                {
                    var match = _resourceCache.UserData.ReadAllText(ParallaxConfigOld) == contents;

                    if (match)
                    {
                        using (var stream = _resourceCache.UserData.OpenRead(ParallaxPath))
                        {
                            ParallaxTexture = Texture.LoadFromPNGStream(stream, "Parallax");
                        }

                        OnTextureLoaded?.Invoke(ParallaxTexture);
                        return;
                    }
                }

                table = Toml.ReadString(contents);
            }

            List <Image <Rgba32> > debugImages = null;

            if (debugParallax)
            {
                debugImages = new List <Image <Rgba32> >();
            }

            var sawmill = _logManager.GetSawmill("parallax");
            // Generate the parallax in the thread pool.
            var image = await Task.Run(() =>
                                       ParallaxGenerator.GenerateParallax(table, new Size(1920, 1080), sawmill, debugImages));

            // And load it in the main thread for safety reasons.
            ParallaxTexture = Texture.LoadFromImage(image, "Parallax");

            // Store it and CRC so further game starts don't need to regenerate it.
            using (var stream = _resourceCache.UserData.Create(ParallaxPath))
            {
                image.SaveAsPng(stream);
            }

            if (debugParallax)
            {
                var i = 0;
                foreach (var debugImage in debugImages)
                {
                    using (var stream = _resourceCache.UserData.Create(new ResourcePath($"/parallax_debug_{i}.png")))
                    {
                        debugImage.SaveAsPng(stream);
                    }

                    i += 1;
                }
            }

            image.Dispose();

            using (var stream = _resourceCache.UserData.Create(ParallaxConfigOld))
                using (var writer = new StreamWriter(stream, EncodingHelpers.UTF8))
                {
                    writer.Write(contents);
                }

            OnTextureLoaded?.Invoke(ParallaxTexture);
        }