Example #1
0
        static void StatsForEnum <T>(IAssetManager assets, Dictionary <AssetId, int> dict) where T : unmanaged, Enum
        {
            foreach (var enumValue in Enum.GetValues(typeof(T)).OfType <T>())
            {
                var id      = AssetId.From(enumValue);
                var info    = assets.GetAssetInfo(id);
                var texture = assets.LoadTexture(id) as IReadOnlyTexture <byte>;
                if (texture == null)
                {
                    continue;
                }

                var palleteId = new PaletteId(AssetType.Palette, info.Get(AssetProperty.PaletteId, 0));
                var palette   = assets.LoadPalette(palleteId);

                var frames        = texture.Regions.Count;
                var uniqueColours = new HashSet <byte>();
                foreach (var colour in texture.PixelData)
                {
                    uniqueColours.Add(colour);
                }

                var lcm = BlitUtil.CalculatePalettePeriod(uniqueColours, palette);
                dict[id] = (int)ApiUtil.Lcm(frames, lcm);
            }
        }
Example #2
0
        public static IList <ExportedImageInfo> ExportImage(AssetId assetId, IAssetManager assets, string directory, DumpFormats formats, Func <int, int, bool> frameFilter = null)
        {
            var filenames = new List <ExportedImageInfo>();
            var config    = assets.GetAssetInfo(assetId);
            var palette   = assets.LoadPalette((Base.Palette)(config?.Get <int?>("PaletteHint", null) ?? (int)Base.Palette.Inventory));
            var texture   = assets.LoadTexture(assetId);

            if (texture == null)
            {
                return(filenames);
            }

            if (texture is TrueColorTexture trueColor)
            {
                var path  = Path.Combine(directory, $"{assetId.Id}_{assetId}");
                var image = trueColor.ToImage();
                Save(image, path, formats, filenames);
            }
            else if (texture is VeldridEightBitTexture tilemap && (
                         assetId.Type == AssetType.Font ||
                         assetId.Type == AssetType.TilesetGraphics ||
                         assetId.Type == AssetType.AutomapGraphics))
            {
                if (palette == null)
                {
                    CoreUtil.LogError($"Could not load palette for {assetId}");
                    return(filenames);
                }

                var colors        = tilemap.DistinctColors(null);
                int palettePeriod = palette.CalculatePeriod(colors);

                for (int palFrame = 0; palFrame < palettePeriod; palFrame++)
                {
                    if (frameFilter != null && !frameFilter(0, palFrame))
                    {
                        continue;
                    }
                    var path  = Path.Combine(directory, $"{assetId.Id}_{palFrame}_{assetId}");
                    var image = tilemap.ToImage(palette.GetPaletteAtTime(palFrame));
                    Save(image, path, formats, filenames);
                }
            }
Example #3
0
        public IList <ExportedImageInfo> ExportImage(
            AssetId assetId,
            IAssetManager assets,
            string directory,
            DumpFormats formats,
            Func <int, int, bool> frameFilter = null)
        {
            var           filenames = new List <ExportedImageInfo>();
            var           config    = assets.GetAssetInfo(assetId);
            AlbionPalette palette;

            if (config != null)
            {
                var rawPaletteId = config.Get(AssetProperty.PaletteId, 0);
                var paletteId    = new PaletteId(AssetType.Palette, rawPaletteId);
                palette = assets.LoadPalette(paletteId);
            }
            else
            {
                palette = assets.LoadPalette(Base.Palette.Inventory);
            }

            var texture = assets.LoadTexture(assetId);

            if (texture == null)
            {
                return(filenames);
            }

            if (texture is IReadOnlyTexture <uint> trueColor)
            {
                var path  = Path.Combine(directory, $"{assetId.Id}_{assetId}");
                var image = ImageSharpUtil.ToImageSharp(trueColor.GetLayerBuffer(0));
                Save(image, path, formats, filenames);
            }
            else if (texture is IReadOnlyTexture <byte> tilemap && (
                         assetId.Type == AssetType.Font ||
                         assetId.Type == AssetType.TilesetGraphics ||
                         assetId.Type == AssetType.AutomapGraphics))
            {
                if (palette == null)
                {
                    Error($"Could not load palette for {assetId}");
                    return(filenames);
                }

                var colors        = BlitUtil.DistinctColors(tilemap.PixelData);
                int palettePeriod = palette.CalculatePeriod(colors);

                for (int palFrame = 0; palFrame < palettePeriod; palFrame++)
                {
                    if (frameFilter != null && !frameFilter(0, palFrame))
                    {
                        continue;
                    }
                    var path  = Path.Combine(directory, $"{assetId.Id}_{palFrame}_{assetId}");
                    var image = ImageSharpUtil.ToImageSharp(tilemap.GetLayerBuffer(0), palette.GetPaletteAtTime(palFrame));
                    Save(image, path, formats, filenames);
                }
            }
            else if (texture is IReadOnlyTexture <byte> eightBit)
            {
                for (int subId = 0; subId < eightBit.Regions.Count; subId++)
                {
                    if (palette == null)
                    {
                        Error($"Could not load palette for {assetId}");
                        break;
                    }

                    var colors        = BlitUtil.DistinctColors(eightBit.GetRegionBuffer(subId));
                    int palettePeriod = palette.CalculatePeriod(colors);

                    for (int palFrame = 0; palFrame < palettePeriod; palFrame++)
                    {
                        if (frameFilter != null && !frameFilter(subId, palFrame))
                        {
                            continue;
                        }
                        var path  = Path.Combine(directory, $"{assetId.Id}_{subId}_{palFrame}_{assetId}");
                        var image = ImageSharpUtil.ToImageSharp(eightBit.GetRegionBuffer(subId), palette.GetPaletteAtTime(palFrame));
                        Save(image, path, formats, filenames);
                    }
                }
            }
            else
            {
                var path = Path.Combine(directory, $"{assetId.Id}_{assetId}.png.todo");
                File.WriteAllText(path, "");
                return(null);
            }

            return(filenames);
        }