Skip to content

Instantly share code, notes, and snippets.

View AldeRoberge's full-sized avatar
💛
.

Alexandre D. Roberge AldeRoberge

💛
.
View GitHub Profile
@AldeRoberge
AldeRoberge / Hosted-Service-DI.md
Created November 18, 2024 15:15
Different instance C# .NET dependency injection DI register hosted service singleton

Using .NET Dependency Injection (DI) for Shared Instances

When using .NET Dependency Injection (DI), if you register a HostedService and also register it as a Singleton, the consuming classes might not get the same instance because the DI container creates separate instances for each registration by default.

services.AddHostedService<Database>();
services.AddSingleton<IDatabase, Database>();

To fix this, you need to ensure the same instance is shared between the IHostedService and the IDatabase. Here’s how you can do it:

@AldeRoberge
AldeRoberge / CopyToUnity.csproj
Created November 14, 2024 19:26
Copy DLLs into Unity Library folder
<!-- To following is useful if you need to copy built .DLLs into Unity. -->
<!-- Adapt this script depending on where you want your DLLs copied. -->
<!-- You might need to manually add dependencies (other DLLs) inside of the folder. -->
<!-- You can check out NuGet For Unity for this, or simply build the libs with .NET Framework 2.1 and import them. -->
<!-- -->
<!-- Example -->
<!-- -->
<!-- ADG -->
<!-- ├── ADG.FMOD -->
@AldeRoberge
AldeRoberge / LoggingConfig.cs
Last active November 15, 2024 21:26
The Logging config I use for all my apps
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
using Serilog.Exceptions;
using Serilog.Sinks.SystemConsole.Themes;
namespace ADG.Library.Logging
{
public static class LoggingConfig
{
@AldeRoberge
AldeRoberge / android-google-play.yaml
Created June 20, 2024 05:27
GameCI Build GitHub Actions for Android (fails after 50 minutes)
name: Build Game for Android and publish to Google Play
on:
push:
branches:
- main
env:
UNITY_LICENSE: ${{ secrets.UNITY_LICENSE }}
UNITY_EMAIL: ${{ secrets.UNITY_EMAIL }}
@AldeRoberge
AldeRoberge / AddBackground.cs
Created June 18, 2024 15:29
Adds a (white) background to png images and export them to jpeg.
using SkiaSharp;
namespace AGXSpriteProcessor
{
// Loads all images in a directory, adds a white background, and saves them to a new directory as .jpeg
public static class AddBackground
{
private const string InputDirectory = @"C:\Users\Alde\Desktop\Wik";
private const string OutputDirectory = @"C:\Users\Alde\Desktop\Wik\Output";
@AldeRoberge
AldeRoberge / TeleportKinematicCharacterController.cs
Created March 31, 2024 15:53
Allows to teleport a Kinematic Character Controller
// Get the ExampleCharacterController
if (_kccMotor.CharacterController is not ExampleCharacterController exampleCharacterController)
{
Debug.LogError("ExampleCharacterController is null");
return;
}
// Disable
exampleCharacterController.enabled = false;
@AldeRoberge
AldeRoberge / SpriteImageProcessor.cs
Created March 6, 2024 22:52
Converts a sprite (16x16 pixels) into a RotMG-like sprite (with outline and shadow)
using System;
using System.Diagnostics;
using SkiaSharp;
namespace AGES.Utils.Runtime.SpriteImageProcessor
{
public class SpriteImageProcessor
{
public SpriteImageProcessor()
{
@AldeRoberge
AldeRoberge / ProjectWideSearch.ps1
Created February 15, 2024 19:15
Find broken managed references in Unity caused by [SerializeReference], projectwide search for the GUIDs
# The provided script is designed to be useful for identifying broken references caused by the [SerializeReference] attribute in Unity. In Unity, when creating a prefab for a script in a scene, old values may persist, potentially disrupting the SerializeReference mechanism and leading to errors during scene opening or building. The script helps locate and identify broken .asset (YAML) references in the Unity project.
param (
[string]$searchDirectory = "C:\Users\Alde\Documents\GitHub\Wikwemot-AR\Wikwemot-AR\Assets",
[string]$searchString = "6945820829875175427"
)
function Search-FilesForString {
param (
[string]$directory,
@AldeRoberge
AldeRoberge / UnityConsoleRedirect.cs
Last active November 15, 2024 20:53
Redirects console output (Console.WriteLine) to Unity (Debug.Log). Keep in mind that this will probably add some internal logging of Unity to your Editor console.
using System;
using System.IO;
using System.Text;
using UnityEngine;
namespace Generator.Scripts.Runtime.Utilities.Logging
{
/// <summary>
/// Redirects console output (Console.WriteLine) to Unity (Debug.Log)
/// </summary>
@AldeRoberge
AldeRoberge / ChatInputHistory.cs
Created January 8, 2024 04:31
ChatInputHistory using player prefs for Unity, not perfect but does the job
using System.Collections.Generic;
using UnityEngine;
namespace AGES.Networking.AGES.Networking.Runtime.Client.Scripts.UI.Chat
{
public class ChatInputHistory : MonoBehaviour
{
// Keeps a record of previous player chat input and allows the player to scroll through it
private readonly List<string> chatHistory = new();