Tofaa
Tofaa
CC#
Created by Tofaa on 11/4/2024 in #help
Dictionary<> Keys do not get fetched properly
private readonly Dictionary<NamespacedKey, Texture2D> _textures = new();

internal void Load() {
var path = "assets/textures";
var files = Directory.GetFiles(path);
foreach (var file in files) {
var texture = Raylib.LoadTexture(file);
var id = NamespacedKey.texture(file.Replace("assets/textures\\", "").Replace(".png", ""));
_textures.Add(id, texture);
Console.WriteLine("Loaded texture with id " + id.Full + " Texture Format:" + texture.Format);
}
}

internal void Unload() {
foreach (var texture in _textures) {
Raylib.UnloadTexture(texture.Value);
}
}
private readonly Dictionary<NamespacedKey, Texture2D> _textures = new();

internal void Load() {
var path = "assets/textures";
var files = Directory.GetFiles(path);
foreach (var file in files) {
var texture = Raylib.LoadTexture(file);
var id = NamespacedKey.texture(file.Replace("assets/textures\\", "").Replace(".png", ""));
_textures.Add(id, texture);
Console.WriteLine("Loaded texture with id " + id.Full + " Texture Format:" + texture.Format);
}
}

internal void Unload() {
foreach (var texture in _textures) {
Raylib.UnloadTexture(texture.Value);
}
}
NamespacedKey is just a class that stores 3 strings. I override the GetHashCode() method in it to
public override int GetHashCode()
{
return Domain.GetHashCode() + Key.GetHashCode() + Family.GetHashCode();
}
public override int GetHashCode()
{
return Domain.GetHashCode() + Key.GetHashCode() + Family.GetHashCode();
}
Ive tried different hashcodes incase im just stupid but nothing works
13 replies
CC#
Created by Tofaa on 6/29/2024 in #help
Array resizing issues
I'm so lost, i've been trying to resize my array Here's the code
internal class SingleThreadObjectArray<T> : IObjectArray<T>
{
private T[] array;
private int max;

internal SingleThreadObjectArray(int size)
{
array = new T[size];
max = size;
}

public T? Get(int index)
{
return index < array.Length ? array[index] : default(T);
}

public void Set(int index, T? value)
{
if (index >= array.Length)
{
var newLength = index * 2 + 1;
Array.Resize(ref array, newLength);
}
array[index] = value!;
max = Math.Max(max, index);
}

public void Trim()
{
array.CopyTo(array, max + 1);
}
}
internal class SingleThreadObjectArray<T> : IObjectArray<T>
{
private T[] array;
private int max;

internal SingleThreadObjectArray(int size)
{
array = new T[size];
max = size;
}

public T? Get(int index)
{
return index < array.Length ? array[index] : default(T);
}

public void Set(int index, T? value)
{
if (index >= array.Length)
{
var newLength = index * 2 + 1;
Array.Resize(ref array, newLength);
}
array[index] = value!;
max = Math.Max(max, index);
}

public void Trim()
{
array.CopyTo(array, max + 1);
}
}
However, whenever i try to set a higher index using Set, it throws an index out of bounds exception
26 replies
CC#
Created by Tofaa on 6/23/2024 in #help
Have public static values accessible thru the class that inherited this class
I understand the title is rather silly but i couldnt word it properly to fit a title. I come from a java background, and am currently making something where i require an exposed api interface and also code-generated values inside the interface. For clutter reasons I'd like to seperate the interface and the static methods. In java for example
public sealed interface A extends VariousAs {
}

class AImpl {}

interface VariousAs {

A VALUE_ONE = new AImpl();
A VALUE_TWO = new AImpl();
}
public sealed interface A extends VariousAs {
}

class AImpl {}

interface VariousAs {

A VALUE_ONE = new AImpl();
A VALUE_TWO = new AImpl();
}
In the code above i can easily access A.VALUE_ONE without exposing AImpl and VariousAs, but theres no way (atleast from what i can tell) to replicate such behaviour in c#.
29 replies
CC#
Created by Tofaa on 6/12/2024 in #help
Help using generics and maps
I've been trying to make a basic listener consumer system for a Minecraft server implementation im making in c# from scratch. I come from a java background so c# is fairly similar but im stuck on trying to make a generic map of an unknown type at runtime.
private final Map<Class<? extends ClientPacket>, PacketConsumer>[] listeners = new Map[5];

public <T extends ClientPacket> void setListener(@NotNull ConnectionState state, @NotNull Class<T> packetClass, @NotNull PacketConsumer<T> consumer) {
this.listeners[state.ordinal()].put(packetClass, consumer);
}
@FunctionalInterface
public interface PacketConsumer<T extends ClientPacket> {
void consume(T packet, PlayerConnection connection);
}
private final Map<Class<? extends ClientPacket>, PacketConsumer>[] listeners = new Map[5];

public <T extends ClientPacket> void setListener(@NotNull ConnectionState state, @NotNull Class<T> packetClass, @NotNull PacketConsumer<T> consumer) {
this.listeners[state.ordinal()].put(packetClass, consumer);
}
@FunctionalInterface
public interface PacketConsumer<T extends ClientPacket> {
void consume(T packet, PlayerConnection connection);
}
I'm trying to rewrite this to c#. I have
private readonly ConcurrentDictionary<Type, Action<IClientPacket, Connection>>[] _listeners = [];
public void SetListener<T>(ConnectionState state, Type type, Action<T, Connection> listener) where T : IClientPacket
{
var dict = _listeners[(int)state];
dict[type] = listener; // cannot set this because its not IClientPacket, but rather T
}
private readonly ConcurrentDictionary<Type, Action<IClientPacket, Connection>>[] _listeners = [];
public void SetListener<T>(ConnectionState state, Type type, Action<T, Connection> listener) where T : IClientPacket
{
var dict = _listeners[(int)state];
dict[type] = listener; // cannot set this because its not IClientPacket, but rather T
}
Any help would be fantastic, thank you :D
32 replies
CC#
Created by Tofaa on 8/5/2023 in #help
❔ ✅ Nuget doesnt find any packages
PS C:\Users\tofik\Desktop\Github\Blossom> dotnet add package Raylib-CsLo --version 4.2.0.9 Determining projects to restore... Writing C:\Users\tofik\AppData\Local\Temp\tmp81F6.tmp info : X.509 certificate chain validation will use the default trust store selected by .NET for code signing. info : X.509 certificate chain validation will use the default trust store selected by .NET for timestamping. info : Adding PackageReference for package 'Raylib-CsLo' into project 'C:\Users\tofik\OneDrive\Desktop\Github\Blossom\Blossom.csproj'. info : Restoring packages for C:\Users\tofik\Desktop\Github\Blossom\Blossom.csproj... error: NU1101: Unable to find package Raylib-CsLo. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages error: Package 'Raylib-CsLo' is incompatible with 'all' frameworks in project 'C:\Users\tofik\OneDrive\Desktop\Github\Blossom\Blossom.csproj'. PS C:\Users\tofik\OneDrive\Desktop\Github\Blossom> For some reason it doesnt find anything, VS itself also doesnt find anything but the microsoft packages, same with rider
12 replies
CC#
Created by Tofaa on 8/4/2023 in #help
❔ Rider nuget does not display any packages
2 replies
CC#
Created by Tofaa on 8/2/2023 in #help
❔ ✅ Error MSB4018 : The "ResolvePackageAssets" task failed unexpectedly. When building.
Full Stacktrace: Use build tool: C:\Program Files\dotnet\sdk\8.0.100-preview.5.23303.2\MSBuild.dll CONSOLE: MSBuild version 17.7.0-preview-23281-03+4ce2ff1f8 for .NET CONSOLE: Build started 02.08.2023 19:54:44. CONSOLE: Project "C:\Users\tofik\AppData\Local\Temp\Topocix.proj" on node 1 (default targets). CONSOLE: ControllerTarget: CONSOLE: Run controller from C:\Program Files\JetBrains\Rider\r2r\2023.2.0R\53AFC7C05396A24E65EC4950CAD773F\JetBrains.Platform.MsBuildTask.v17.dll 0>Microsoft.NET.RuntimeIdentifierInference.targets(314,5): Message NETSDK1057 : You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy 0>Microsoft.PackageDependencyResolution.targets(266,5): Error MSB4018 : The "ResolvePackageAssets" task failed unexpectedly. NuGet.Packaging.Core.PackagingException: Unable to find fallback package folder 'C:\Users\tofik\AppData\Roaming\Godot\mono\GodotNuGetFallbackFolder'. at NuGet.Packaging.FallbackPackagePathResolver..ctor(String userPackageFolder, IEnumerable1 fallbackPackageFolders) at Microsoft.NET.Build.Tasks.NuGetPackageResolver.CreateResolver(IEnumerable1 packageFolders) at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter..ctor(ResolvePackageAssets task) at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader.CreateReaderFromDisk(ResolvePackageAssets task, Byte[] settingsHash) at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader..ctor(ResolvePackageAssets task) at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ReadItemGroups() at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ExecuteCore() at Microsoft.NET.Build.Tasks.TaskBase.Execute() at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
28 replies
CC#
Created by Tofaa on 7/29/2023 in #help
❔ ✅ Equivalent of Java's resources folder in c#
I've been dipping my toes in c# for a while but i cant seem to find a proper way of storing files in the app itself, i use rider.
13 replies