mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-04 00:34:26 -05:00 
			
		
		
		
	dev: Updated image library
This commit is contained in:
		@@ -36,6 +36,8 @@ public class AutoPublishService : IExecNoCommand, IReadyExecutor, INService
 | 
			
		||||
            RetryMode = RetryMode.AlwaysFail
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    // todo GUILDS
 | 
			
		||||
 | 
			
		||||
    public async Task OnReadyAsync()
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,7 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
        _rng = new();
 | 
			
		||||
        _client = client;
 | 
			
		||||
        _gss = gss;
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        using var uow = db.GetDbContext();
 | 
			
		||||
        var guildIds = client.Guilds.Select(x => x.Id).ToList();
 | 
			
		||||
        var configs = uow.Set<GuildConfig>()
 | 
			
		||||
@@ -111,13 +111,17 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
    {
 | 
			
		||||
        var curImg = await _images.GetCurrencyImageAsync();
 | 
			
		||||
 | 
			
		||||
        if (curImg is null)
 | 
			
		||||
            return (new MemoryStream(), null);
 | 
			
		||||
 | 
			
		||||
        if (string.IsNullOrWhiteSpace(pass))
 | 
			
		||||
        {
 | 
			
		||||
            // determine the extension
 | 
			
		||||
            using var load = _ = Image.Load(curImg, out var format);
 | 
			
		||||
            using var load = Image.Load(curImg);
 | 
			
		||||
 | 
			
		||||
            var format = load.Metadata.DecodedImageFormat;
 | 
			
		||||
            // return the image
 | 
			
		||||
            return (curImg.ToStream(), format.FileExtensions.FirstOrDefault() ?? "png");
 | 
			
		||||
            return (curImg.ToStream(), format?.FileExtensions.FirstOrDefault() ?? "png");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // get the image stream and extension
 | 
			
		||||
@@ -134,16 +138,17 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
    {
 | 
			
		||||
        // draw lower, it looks better
 | 
			
		||||
        pass = pass.TrimTo(10, true).ToLowerInvariant();
 | 
			
		||||
        using var img = Image.Load<Rgba32>(curImg, out var format);
 | 
			
		||||
        using var img = Image.Load<Rgba32>(curImg);
 | 
			
		||||
        // choose font size based on the image height, so that it's visible
 | 
			
		||||
        var font = _fonts.NotoSans.CreateFont(img.Height / 12.0f, FontStyle.Bold);
 | 
			
		||||
        img.Mutate(x =>
 | 
			
		||||
        {
 | 
			
		||||
            // measure the size of the text to be drawing
 | 
			
		||||
            var size = TextMeasurer.Measure(pass, new TextOptions(font)
 | 
			
		||||
            {
 | 
			
		||||
                Origin = new PointF(0, 0)
 | 
			
		||||
            });
 | 
			
		||||
            var size = TextMeasurer.MeasureSize(pass,
 | 
			
		||||
                new RichTextOptions(font)
 | 
			
		||||
                {
 | 
			
		||||
                    Origin = new PointF(0, 0)
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
            // fill the background with black, add 5 pixels on each side to make it look better
 | 
			
		||||
            x.FillPolygon(Color.ParseHex("00000080"),
 | 
			
		||||
@@ -156,7 +161,8 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
            x.DrawText(pass, font, Color.White, new(0, 0));
 | 
			
		||||
        });
 | 
			
		||||
        // return image as a stream for easy sending
 | 
			
		||||
        return (img.ToStream(format), format.FileExtensions.FirstOrDefault() ?? "png");
 | 
			
		||||
        var format = img.Metadata.DecodedImageFormat;
 | 
			
		||||
        return (img.ToStream(format), format?.FileExtensions.FirstOrDefault() ?? "png");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Task PotentialFlowerGeneration(IUserMessage imsg)
 | 
			
		||||
@@ -256,7 +262,8 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
 | 
			
		||||
                pass = pass?.Trim().TrimTo(10, true).ToUpperInvariant();
 | 
			
		||||
                // gets all plants in this channel with the same password
 | 
			
		||||
                var entries = uow.Set<PlantedCurrency>().AsQueryable()
 | 
			
		||||
                var entries = uow.Set<PlantedCurrency>()
 | 
			
		||||
                                 .AsQueryable()
 | 
			
		||||
                                 .Where(x => x.ChannelId == ch.Id && pass == x.Password)
 | 
			
		||||
                                 .ToList();
 | 
			
		||||
                // sum how much currency that is, and get all of the message ids (so that i can delete them)
 | 
			
		||||
@@ -368,15 +375,16 @@ public class PlantPickService : INService, IExecNoCommand
 | 
			
		||||
        string pass)
 | 
			
		||||
    {
 | 
			
		||||
        await using var uow = _db.GetDbContext();
 | 
			
		||||
        uow.Set<PlantedCurrency>().Add(new()
 | 
			
		||||
        {
 | 
			
		||||
            Amount = amount,
 | 
			
		||||
            GuildId = gid,
 | 
			
		||||
            ChannelId = cid,
 | 
			
		||||
            Password = pass,
 | 
			
		||||
            UserId = uid,
 | 
			
		||||
            MessageId = mid
 | 
			
		||||
        });
 | 
			
		||||
        uow.Set<PlantedCurrency>()
 | 
			
		||||
           .Add(new()
 | 
			
		||||
           {
 | 
			
		||||
               Amount = amount,
 | 
			
		||||
               GuildId = gid,
 | 
			
		||||
               ChannelId = cid,
 | 
			
		||||
               Password = pass,
 | 
			
		||||
               UserId = uid,
 | 
			
		||||
               MessageId = mid
 | 
			
		||||
           });
 | 
			
		||||
        await uow.SaveChangesAsync();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -172,13 +172,13 @@ public partial class Gambling
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var slotBg = await _images.GetSlotBgAsync();
 | 
			
		||||
            var bgImage = Image.Load<Rgba32>(slotBg, out _);
 | 
			
		||||
            var bgImage = Image.Load<Rgba32>(slotBg);
 | 
			
		||||
            var numbers = new int[3];
 | 
			
		||||
            result.Rolls.CopyTo(numbers, 0);
 | 
			
		||||
 | 
			
		||||
            Color fontColor = Config.Slots.CurrencyFontColor;
 | 
			
		||||
 | 
			
		||||
            bgImage.Mutate(x => x.DrawText(new TextOptions(_fonts.DottyFont.CreateFont(65))
 | 
			
		||||
            bgImage.Mutate<Rgba32>(x => x.DrawText(new RichTextOptions(_fonts.DottyFont.CreateFont(65))
 | 
			
		||||
                {
 | 
			
		||||
                    HorizontalAlignment = HorizontalAlignment.Center,
 | 
			
		||||
                    VerticalAlignment = VerticalAlignment.Center,
 | 
			
		||||
@@ -190,7 +190,7 @@ public partial class Gambling
 | 
			
		||||
 | 
			
		||||
            var bottomFont = _fonts.DottyFont.CreateFont(50);
 | 
			
		||||
 | 
			
		||||
            bgImage.Mutate(x => x.DrawText(new TextOptions(bottomFont)
 | 
			
		||||
            bgImage.Mutate(x => x.DrawText(new RichTextOptions(bottomFont)
 | 
			
		||||
                {
 | 
			
		||||
                    HorizontalAlignment = HorizontalAlignment.Center,
 | 
			
		||||
                    VerticalAlignment = VerticalAlignment.Center,
 | 
			
		||||
 
 | 
			
		||||
@@ -90,7 +90,7 @@ public class CryptoService : INService
 | 
			
		||||
 | 
			
		||||
        img.Mutate(x =>
 | 
			
		||||
        {
 | 
			
		||||
            x.DrawLines(color, 2, points);
 | 
			
		||||
            x.DrawLine(color, 2, points);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return img;
 | 
			
		||||
@@ -220,7 +220,7 @@ public class CryptoService : INService
 | 
			
		||||
    {
 | 
			
		||||
        if (page >= 25)
 | 
			
		||||
            page = 24;
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        using var http = _httpFactory.CreateClient();
 | 
			
		||||
 | 
			
		||||
        http.AddFakeHeaders();
 | 
			
		||||
 
 | 
			
		||||
@@ -94,7 +94,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi
 | 
			
		||||
        => image.Mutate(ctx =>
 | 
			
		||||
        {
 | 
			
		||||
            foreach (var data in drawData)
 | 
			
		||||
                ctx.DrawLines(data.IsGreen
 | 
			
		||||
                ctx.DrawLine(data.IsGreen
 | 
			
		||||
                        ? _greenBrush
 | 
			
		||||
                        : _redBrush,
 | 
			
		||||
                    1,
 | 
			
		||||
@@ -128,7 +128,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi
 | 
			
		||||
        {
 | 
			
		||||
            // draw guides
 | 
			
		||||
            foreach (var y in lines)
 | 
			
		||||
                ctx.DrawLines(_lineGuideColor, 1, new PointF(0, y), new PointF(WIDTH, y));
 | 
			
		||||
                ctx.DrawLine(_lineGuideColor, 1, new PointF(0, y), new PointF(WIDTH, y));
 | 
			
		||||
            
 | 
			
		||||
            // // draw min and max price on the chart
 | 
			
		||||
            // ctx.DrawText(min.ToString(CultureInfo.InvariantCulture),
 | 
			
		||||
@@ -156,7 +156,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi
 | 
			
		||||
        
 | 
			
		||||
        image.Mutate(ctx =>
 | 
			
		||||
        {
 | 
			
		||||
            ctx.DrawLines(_sparklineColor, 2, points);
 | 
			
		||||
            ctx.DrawLine(_sparklineColor, 2, points);
 | 
			
		||||
        });
 | 
			
		||||
    
 | 
			
		||||
        return Task.FromResult<ImageData?>(new("png", image.ToStream()));
 | 
			
		||||
@@ -177,7 +177,7 @@ public sealed class ImagesharpStockChartDrawingService : IStockChartDrawingServi
 | 
			
		||||
        var points = GetSparklinePointsInternal(series);
 | 
			
		||||
        image.Mutate(ctx =>
 | 
			
		||||
        {
 | 
			
		||||
            ctx.DrawLines(Color.ParseHex("00FFFFAA"), 1, points);
 | 
			
		||||
            ctx.DrawLine(Color.ParseHex("00FFFFAA"), 1, points);
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        return Task.FromResult<ImageData?>(new("png", image.ToStream()));
 | 
			
		||||
 
 | 
			
		||||
@@ -994,23 +994,23 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
                throw new ArgumentNullException(nameof(bgBytes));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var outlinePen = new Pen(Color.Black, 1f);
 | 
			
		||||
            var outlinePen = new SolidPen(Color.Black, 1f);
 | 
			
		||||
 | 
			
		||||
            using var img = Image.Load<Rgba32>(bgBytes, out var imageFormat);
 | 
			
		||||
            using var img = Image.Load<Rgba32>(bgBytes);
 | 
			
		||||
            if (template.User.Name.Show)
 | 
			
		||||
            {
 | 
			
		||||
                var fontSize = (int)(template.User.Name.FontSize * 0.9);
 | 
			
		||||
                var username = stats.User.ToString();
 | 
			
		||||
                var usernameFont = _fonts.NotoSans.CreateFont(fontSize, FontStyle.Bold);
 | 
			
		||||
 | 
			
		||||
                var size = TextMeasurer.Measure($"@{username}", new(usernameFont));
 | 
			
		||||
                var size = TextMeasurer.MeasureSize($"@{username}", new(usernameFont));
 | 
			
		||||
                var scale = 400f / size.Width;
 | 
			
		||||
                if (scale < 1)
 | 
			
		||||
                    usernameFont = _fonts.NotoSans.CreateFont(template.User.Name.FontSize * scale, FontStyle.Bold);
 | 
			
		||||
 | 
			
		||||
                img.Mutate(x =>
 | 
			
		||||
                {
 | 
			
		||||
                    x.DrawText(new TextOptions(usernameFont)
 | 
			
		||||
                    x.DrawText(new RichTextOptions(usernameFont)
 | 
			
		||||
                        {
 | 
			
		||||
                            HorizontalAlignment = HorizontalAlignment.Left,
 | 
			
		||||
                            VerticalAlignment = VerticalAlignment.Center,
 | 
			
		||||
@@ -1031,7 +1031,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
 | 
			
		||||
                var clubFont = _fonts.NotoSans.CreateFont(template.Club.Name.FontSize, FontStyle.Regular);
 | 
			
		||||
 | 
			
		||||
                img.Mutate(x => x.DrawText(new TextOptions(clubFont)
 | 
			
		||||
                img.Mutate(x => x.DrawText(new RichTextOptions(clubFont)
 | 
			
		||||
                    {
 | 
			
		||||
                        HorizontalAlignment = HorizontalAlignment.Right,
 | 
			
		||||
                        VerticalAlignment = VerticalAlignment.Top,
 | 
			
		||||
@@ -1051,7 +1051,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
                int maxSize)
 | 
			
		||||
            {
 | 
			
		||||
                var font = fontFamily.CreateFont(fontSize, style);
 | 
			
		||||
                var size = TextMeasurer.Measure(text, new(font));
 | 
			
		||||
                var size = TextMeasurer.MeasureSize(text, new(font));
 | 
			
		||||
                var scale = maxSize / size.Width;
 | 
			
		||||
                if (scale < 1)
 | 
			
		||||
                    font = fontFamily.CreateFont(fontSize * scale, style);
 | 
			
		||||
@@ -1114,7 +1114,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
            if (template.User.Xp.Global.Show)
 | 
			
		||||
            {
 | 
			
		||||
                img.Mutate(x => x.DrawText(
 | 
			
		||||
                    new TextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize, FontStyle.Bold))
 | 
			
		||||
                    new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Global.FontSize, FontStyle.Bold))
 | 
			
		||||
                    {
 | 
			
		||||
                        HorizontalAlignment = HorizontalAlignment.Center,
 | 
			
		||||
                        VerticalAlignment = VerticalAlignment.Center,
 | 
			
		||||
@@ -1128,7 +1128,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
            if (template.User.Xp.Guild.Show)
 | 
			
		||||
            {
 | 
			
		||||
                img.Mutate(x => x.DrawText(
 | 
			
		||||
                    new TextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize, FontStyle.Bold))
 | 
			
		||||
                    new RichTextOptions(_fonts.NotoSans.CreateFont(template.User.Xp.Guild.FontSize, FontStyle.Bold))
 | 
			
		||||
                    {
 | 
			
		||||
                        HorizontalAlignment = HorizontalAlignment.Center,
 | 
			
		||||
                        VerticalAlignment = VerticalAlignment.Center,
 | 
			
		||||
@@ -1152,7 +1152,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
                    new(awX, awY)));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            var rankPen = new Pen(Color.White, 1);
 | 
			
		||||
            var rankPen = new SolidPen(Color.White, 1);
 | 
			
		||||
            //ranking
 | 
			
		||||
            if (template.User.GlobalRank.Show)
 | 
			
		||||
            {
 | 
			
		||||
@@ -1166,7 +1166,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
                    68);
 | 
			
		||||
 | 
			
		||||
                img.Mutate(x => x.DrawText(
 | 
			
		||||
                    new TextOptions(globalRankFont)
 | 
			
		||||
                    new RichTextOptions(globalRankFont)
 | 
			
		||||
                    {
 | 
			
		||||
                        Origin = new(template.User.GlobalRank.Pos.X, template.User.GlobalRank.Pos.Y)
 | 
			
		||||
                    },
 | 
			
		||||
@@ -1188,7 +1188,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
                    43);
 | 
			
		||||
 | 
			
		||||
                img.Mutate(x => x.DrawText(
 | 
			
		||||
                    new TextOptions(guildRankFont)
 | 
			
		||||
                    new RichTextOptions(guildRankFont)
 | 
			
		||||
                    {
 | 
			
		||||
                        Origin = new(template.User.GuildRank.Pos.X, template.User.GuildRank.Pos.Y)
 | 
			
		||||
                    },
 | 
			
		||||
@@ -1231,7 +1231,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        using var toDraw = Image.Load(data);
 | 
			
		||||
                        if (toDraw.Size() != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y))
 | 
			
		||||
                        if (toDraw.Size != new Size(template.User.Icon.Size.X, template.User.Icon.Size.Y))
 | 
			
		||||
                            toDraw.Mutate(x => x.Resize(template.User.Icon.Size.X, template.User.Icon.Size.Y));
 | 
			
		||||
 | 
			
		||||
                        img.Mutate(x => x.DrawImage(toDraw,
 | 
			
		||||
@@ -1257,6 +1257,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
            if (outputSize.X != img.Width || outputSize.Y != img.Height)
 | 
			
		||||
                img.Mutate(x => x.Resize(template.OutputSize.X, template.OutputSize.Y));
 | 
			
		||||
 | 
			
		||||
            var imageFormat = img.Metadata.DecodedImageFormat;
 | 
			
		||||
            var output = ((Stream)await img.ToStreamAsync(imageFormat), imageFormat);
 | 
			
		||||
 | 
			
		||||
            return output;
 | 
			
		||||
@@ -1393,7 +1394,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                using var toDraw = Image.Load(data);
 | 
			
		||||
                if (toDraw.Size() != new Size(template.Club.Icon.Size.X, template.Club.Icon.Size.Y))
 | 
			
		||||
                if (toDraw.Size != new Size(template.Club.Icon.Size.X, template.Club.Icon.Size.Y))
 | 
			
		||||
                    toDraw.Mutate(x => x.Resize(template.Club.Icon.Size.X, template.Club.Icon.Size.Y));
 | 
			
		||||
 | 
			
		||||
                img.Mutate(x => x.DrawImage(
 | 
			
		||||
 
 | 
			
		||||
@@ -69,9 +69,9 @@
 | 
			
		||||
        <PackageReference Include="Serilog.Sinks.Console" Version="5.0.1"/>
 | 
			
		||||
        <PackageReference Include="Serilog.Sinks.Seq" Version="7.0.1"/>
 | 
			
		||||
 | 
			
		||||
        <PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta17"/>
 | 
			
		||||
        <PackageReference Include="SixLabors.ImageSharp" Version="2.1.9"/>
 | 
			
		||||
        <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14"/>
 | 
			
		||||
        <PackageReference Include="SixLabors.Fonts" Version="2.0.4" />
 | 
			
		||||
        <PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
 | 
			
		||||
        <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
 | 
			
		||||
        <PackageReference Include="SixLabors.Shapes" Version="1.0.0-beta0009"/>
 | 
			
		||||
        <PackageReference Include="StackExchange.Redis" Version="2.8.0" />
 | 
			
		||||
        <PackageReference Include="YamlDotNet" Version="15.1.4"/>
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@ namespace NadekoBot.Common.JsonConverters;
 | 
			
		||||
public class Rgba32Converter : JsonConverter<Rgba32>
 | 
			
		||||
{
 | 
			
		||||
    public override Rgba32 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 | 
			
		||||
        => Rgba32.ParseHex(reader.GetString());
 | 
			
		||||
        => Rgba32.ParseHex(reader.GetString()!);
 | 
			
		||||
 | 
			
		||||
    public override void Write(Utf8JsonWriter writer, Rgba32 value, JsonSerializerOptions options)
 | 
			
		||||
        => writer.WriteStringValue(value.ToHex());
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,7 @@ public static class Rgba32Extensions
 | 
			
		||||
                using var frame = imgArray[i].Frames.CloneFrame(frameNumber % imgArray[i].Frames.Count);
 | 
			
		||||
                var offset = xOffset;
 | 
			
		||||
                imgFrame.Mutate(x => x.DrawImage(frame, new Point(offset, 0), new GraphicsOptions()));
 | 
			
		||||
                xOffset += imgArray[i].Bounds().Width;
 | 
			
		||||
                xOffset += imgArray[i].Bounds.Width;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user