Angius
Angius
Explore posts from servers
CC#
Created by Angius on 5/28/2024 in #help
Getting route data from a WebAPI without Swagger
The way I'm currently generating TS clients, is I use NSwag to get the OpenAPI spec, then I have a tool written in JS that parses that json and generates TS clients. I was thinking of eliminating some steps from that, though. The way I see it, I should be able to make a console app that references my WebAPI project, and somehow get the route data from there. All nicely typed and all. Bypassing Swagger completely. Can't figure out how, though
15 replies
CC#
Created by Angius on 4/24/2024 in #help
Random temperature curve generation too biased
No description
16 replies
CC#
Created by Angius on 4/4/2024 in #help
✅ SkiaSharp canvas scale not scaling
I have a method that takes an int[,] array with ones and zeroes. Based on that, I want to create an image, that is optionally resized and blurred. Creating the image at 1:1 scale works, blurring it works as well, but for the love of me I cannot get it to scale...
byte[] CreateImage(int[,] arr, float? scale = null, float? blur = null)
{
var wSize = arr.GetLength(0);
var hSize = arr.GetLength(1);

var bmp = new SKBitmap(wSize, hSize);
using (var canvas = new SKCanvas(bmp))
{
for (var w = 0; w < wSize; w++)
{
for (var h = 0; h < hSize; h++)
{
canvas.DrawPoint(w, h, arr[w, h] == 0 ? SKColors.White : SKColors.Black);
}
}

if (scale is {} s)
{
canvas.Scale(s);
}
}

if (blur is {} b)
{
var newBmp = new SKBitmap((int)(wSize * scale ?? 1), (int)(hSize * scale ?? 1));
using var canvas = new SKCanvas(newBmp);
using var paint = new SKPaint();

paint.ImageFilter = SKImageFilter.CreateBlur(b, b);
canvas.DrawImage(SKImage.FromBitmap(bmp), 0, 0, paint);

using var blurredImage = SKImage.FromBitmap(newBmp);
return blurredImage.Encode().ToArray();
}

using var image = SKImage.FromBitmap(bmp);
return image.Encode().ToArray();
}
byte[] CreateImage(int[,] arr, float? scale = null, float? blur = null)
{
var wSize = arr.GetLength(0);
var hSize = arr.GetLength(1);

var bmp = new SKBitmap(wSize, hSize);
using (var canvas = new SKCanvas(bmp))
{
for (var w = 0; w < wSize; w++)
{
for (var h = 0; h < hSize; h++)
{
canvas.DrawPoint(w, h, arr[w, h] == 0 ? SKColors.White : SKColors.Black);
}
}

if (scale is {} s)
{
canvas.Scale(s);
}
}

if (blur is {} b)
{
var newBmp = new SKBitmap((int)(wSize * scale ?? 1), (int)(hSize * scale ?? 1));
using var canvas = new SKCanvas(newBmp);
using var paint = new SKPaint();

paint.ImageFilter = SKImageFilter.CreateBlur(b, b);
canvas.DrawImage(SKImage.FromBitmap(bmp), 0, 0, paint);

using var blurredImage = SKImage.FromBitmap(newBmp);
return blurredImage.Encode().ToArray();
}

using var image = SKImage.FromBitmap(bmp);
return image.Encode().ToArray();
}
5 replies
CC#
Created by Angius on 2/9/2024 in #help
✅ Hex color converter breaks on 3-digit and 4-digit values
No description
5 replies
CC#
Created by Angius on 1/17/2024 in #help
Is there any way to *conditionally* set a property with EF `.ExecuteUpdateAsync()`?
Right now, I'm doing
var thing = await _ctx.Things.FirstOrDefaultAsync(t => t.Id == req.Id);
if (thing.Name != req.Name) thing.Name = req.Name;
if (thing.Count != req.Count) thing.Count = req.Count;
await _ctx.SaveChangesAsync();
var thing = await _ctx.Things.FirstOrDefaultAsync(t => t.Id == req.Id);
if (thing.Name != req.Name) thing.Name = req.Name;
if (thing.Count != req.Count) thing.Count = req.Count;
await _ctx.SaveChangesAsync();
but I'm not sure how would I do something similar, as in, something that avoids setting x.Name = x.Name, with the Execute method. Something like
var rows = await _ctx.Things.Where(t => t.Id == req.Id)
.ExecuteUpdateAsync(props => props
.SetPropertyIf(t => t.Name != req.Name, t => t.Name, req.Name)
.SetPropertyIf(t => t.Count != req.Count, t => t.Count, req.Count))
var rows = await _ctx.Things.Where(t => t.Id == req.Id)
.ExecuteUpdateAsync(props => props
.SetPropertyIf(t => t.Name != req.Name, t => t.Name, req.Name)
.SetPropertyIf(t => t.Count != req.Count, t => t.Count, req.Count))
but no such thing seems to exist.
20 replies
CC#
Created by Angius on 7/17/2023 in #help
❔ Azure DevOps repo remote unpack failed
I have a Git repo set up, it's an UE5 project which means large binaries, and Azure repos have unlimited size, so that's why not Github. I'm trying to push the project over SSH, but I'm getting some weird errors
❯ git push -u origin --all
Enter passphrase for key [key]:
Enumerating objects: 2462, done.
Counting objects: 100% (2462/2462), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2433/2433), done.
remote: The archive entry was compressed using an unsupported compression method.
send-pack: unexpected disconnect while reading sideband packet
fatal: sha1 file '<stdout>' write error: Broken pipe
error: remote unpack failed: error The archive entry was compressed using an unsupported compression method.
fatal: the remote end hung up unexpectedly
❯ git push -u origin --all
Enter passphrase for key [key]:
Enumerating objects: 2462, done.
Counting objects: 100% (2462/2462), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2433/2433), done.
remote: The archive entry was compressed using an unsupported compression method.
send-pack: unexpected disconnect while reading sideband packet
fatal: sha1 file '<stdout>' write error: Broken pipe
error: remote unpack failed: error The archive entry was compressed using an unsupported compression method.
fatal: the remote end hung up unexpectedly
23 replies
CC#
Created by Angius on 6/25/2023 in #help
❔ Code from the new Gundam episode
11 replies
CC#
Created by Angius on 6/21/2023 in #help
❔ Windows - system restore on PC shutdown
Not a C# question, I know, but I've no idea where else to turn to lol Students at my school do all sorts of stuff with the PCs. Weird wallpapers, meatspin in autostart, weird bat files, installing Roblox, all kinds of stuff like that. Not everything can be prevented with just an admin password, AFAIK. Back in college, they had some system set up that would roll back the PC on shutdown (or was it startup?) to a set restore point. So all the software and stuff was still there, but all the settings would be rolled back, and all the newly-installed stuff would be removed. I'd like to use something like that in this school as well, but for the love of me I cannot remember the name of the soft
12 replies
CC#
Created by Angius on 5/21/2023 in #help
❔ Properly including XML templates in a class library
I'm writing a library for generating .epub files. Since they're just glorified zips with XML files inside, I'll need to generate that XML somehow. The easiest way is to just have some templates with the XML and placeholders, and run a .Replace() on them or use some templating library. Question is, how do I store those templates properly? Using triple-quoted string constants would be the easiest, but some of the XML is quite large. I could just store loose files and load them with File.ReadAllTextAsync(), but not sure how well that'd work in the class lib context. Not to mention, I'd like those templates to be available at all times, I'd rather avoid reading them from the filesystem each time I want to generate an epub. Using DI somehow is an idea, I could read the templates once and inject them as a singleton or something, but how do I even make DI work in a class lib, not knowing if the user of the library even uses DI in the first place? I'd consider source generators and just reading files on compile time and adding them to some Template const in each class that needs it, but source generators can't read files... Any ideas would be welcome
21 replies
CC#
Created by Angius on 4/22/2023 in #help
✅ Avalonia — stop children from triggering parent events
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
Orientation="Horizontal"
Background="Black"
Opacity="0.7" PointerPressed="InputElement_OnPointerPressed">
<Button Command="{Binding NewWindowCommand}">S</Button>
<Button Command="{Binding MainViewCommand}">Home</Button>
</StackPanel>
<StackPanel Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3"
Orientation="Horizontal"
Background="Black"
Opacity="0.7" PointerPressed="InputElement_OnPointerPressed">
<Button Command="{Binding NewWindowCommand}">S</Button>
<Button Command="{Binding MainViewCommand}">Home</Button>
</StackPanel>
with the following codebehind
private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
BeginMoveDrag(e);
}
private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
BeginMoveDrag(e);
}
makes it so that clicking on the buttons doesn't trigger their commands, but rather the click gets captured by the parent and the window gets dragged
24 replies
CC#
Created by Angius on 2/16/2023 in #help
❔ Ownership-based permission system...?
So, in my ASP.NET 7 app I have a system of user clubs. Each club can have a founder and members, and each member can also be a mod for that community. The roles of a founder and moderator are represented as an enum on the join table. Now, I want to restrict some stuff to given roles. Like, only the founder can edit the settings of the club, and only the moderators or founders can kick or ban users. And you need to be a member to post there, etc. Currently, I just add one more .Where() to each query that checks for the given role, but maybe there's some better solution? Some middleware or something that'd let me just slap [Authorize(PolicyNames.ClubFounder)] onto a controller and be done with it?
13 replies
CC#
Created by Angius on 1/29/2023 in #help
❔ ✅ Rider is out of its mind about `async` code
7 replies
CC#
Created by Angius on 1/1/2023 in #help
❔ Return gets ignored for some godforsaken reason
82 replies
CC#
Created by Angius on 11/7/2022 in #help
Banned Symbols Analyzer partial ban [Answered]
I currently ban DbContext.Set<T>() methods in my project using https://github.com/dotnet/roslyn-analyzers/tree/main/src/Microsoft.CodeAnalysis.BannedApiAnalyzers
M:Microsoft.EntityFrameworkCore.DbContext.Set``1
M:Microsoft.EntityFrameworkCore.DbContext.Set``1(System.String)
M:Microsoft.EntityFrameworkCore.DbContext.Set``1
M:Microsoft.EntityFrameworkCore.DbContext.Set``1(System.String)
but I'd like to ban them only partially. Meaning, I want to disallow concrete types, but allow generic parameters. For example
public void Foo<T>()
{
_ctx.Set<T>(); // allowed
_ctx.Set<Product>(); // disallowed
}
public void Foo<T>()
{
_ctx.Set<T>(); // allowed
_ctx.Set<Product>(); // disallowed
}
is that even possible?
7 replies