Skip to content

Instantly share code, notes, and snippets.

View gustavopsantos's full-sized avatar
🦖

Gustavo Santos gustavopsantos

🦖
View GitHub Profile
using System;
using System.Linq;
public static class TypeExtensions
{
public static string GetSlimAssemblyQualifiedName(this Type type)
{
if (type.IsGenericType)
{
var names = string.Join("],[", type.GenericTypeArguments.Select(t => t.GetSlimAssemblyQualifiedName()));
@gustavopsantos
gustavopsantos / PlayerLoopExtensions
Created October 2, 2021 21:06
unity related class to ease the use of PlayerLoop API
public static class PlayerLoopExtensions
{
public static void AddSubSystem<TParent, TInner>(Action action)
{
var playerLoop = PlayerLoop.GetCurrentPlayerLoop();
ref var parentSystemRef = ref FindSystem<TParent>(playerLoop);
Array.Resize(ref parentSystemRef.subSystemList, parentSystemRef.subSystemList.Length + 1);
var subSystem = new PlayerLoopSystem { type = typeof(TInner), updateDelegate = action.Invoke };
parentSystemRef.subSystemList[parentSystemRef.subSystemList.Length - 1] = subSystem;
PlayerLoop.SetPlayerLoop(playerLoop);
@gustavopsantos
gustavopsantos / EmitCompatibilityCheck.cs
Created March 30, 2022 20:20
EmitCompatibilityCheck
using System.Reflection;
using System.Reflection.Emit;
public static class EmitCompatibilityCheck
{
public static bool Check()
{
try
{
var assemblyName = new AssemblyName(nameof(EmitCompatibilityCheck));
using System;
using System.Threading;
namespace Raptor.Game.Shared
{
public class Clock
{
private int _tick;
private bool _stop;
private readonly Thread _thread;
@gustavopsantos
gustavopsantos / RuntimeInitializeOnLoadMethodExecutionOrder.md
Last active September 22, 2022 13:26
RuntimeInitializeOnLoadMethod execution order
Class Static Ctor
↓
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)][RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)][RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
@gustavopsantos
gustavopsantos / Mod
Created November 4, 2022 13:15
Mod/Cycle
public static float Mod(float a, float b)
{
float c = a % b;
if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
c += b;
}
return c;
}
public static class CameraExtensions
{
public static Bounds WorldToScreenBounds(this Camera camera, Bounds bounds)
{
var center = bounds.center;
var extents = bounds.extents;
Span<Vector3> worldSpaceCorners = stackalloc Vector3[8];
worldSpaceCorners[0] = new Vector3(center.x + extents.x, center.y + extents.y, center.z + extents.z);
worldSpaceCorners[1] = new Vector3(center.x + extents.x, center.y + extents.y, center.z - extents.z);
using System;
using System.Collections.Generic;
using UnityEngine;
public static class GetBoundsAtScreenSpace
{
private static readonly List<Vector3> _localSpaceVerticesBuffer = new List<Vector3>();
public static Bounds Get(MeshFilter meshFilter, Camera camera)
{
@gustavopsantos
gustavopsantos / InjectedPropertyInfo.cs
Created February 7, 2023 13:07
InjectedPropertyInfo
internal sealed class InjectedPropertyInfo
{
public readonly Type PropertyType;
public readonly Action<object, object> Setter;
public InjectedPropertyInfo(PropertyInfo propertyInfo)
{
PropertyType = propertyInfo.PropertyType;
Setter = GenerateSetter(propertyInfo);
}
@gustavopsantos
gustavopsantos / InjectedMethodInfo.cs
Created February 7, 2023 13:08
InjectedMethodInfo
internal sealed class InjectedMethodInfo
{
public readonly MethodInfo MethodInfo;
public readonly ParameterInfo[] Parameters;
public readonly Action<object, object[]> Call;
public InjectedMethodInfo(MethodInfo methodInfo)
{
MethodInfo = methodInfo;
Parameters = methodInfo.GetParameters();