111 lines
3.7 KiB
C#
111 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Unity.Mathematics;
|
|
|
|
namespace BITKit
|
|
{
|
|
public static partial class Extensions
|
|
{
|
|
private static readonly ConcurrentDictionary<int,ConcurrentDictionary<int,object>> Services = new();
|
|
public static bool QueryComponents<T>(this IServiceProvider self,out T t1) where T : class
|
|
{
|
|
var id = self.GetHashCode();
|
|
var typeId = typeof(T).GetHashCode();
|
|
if (Services.GetOrCreate(id).TryGetValue(typeId, out var value) is false)
|
|
{
|
|
value = self.GetService<T>();
|
|
Services.GetOrCreate(id).TryAdd(typeId,value);
|
|
}
|
|
if (value is null)
|
|
{
|
|
t1 = null;
|
|
return false;
|
|
}
|
|
t1 = Unsafe.As<T>(value);
|
|
return true;
|
|
}
|
|
public static bool QueryComponents<T,T1> (this IServiceProvider self,out T t1,out T1 t2) where T : class where T1 : class
|
|
{
|
|
t1 = null;
|
|
t2 = null;
|
|
if (QueryComponents(self,out t1) is false) return false;
|
|
if (QueryComponents(self,out t2) is false) return false;
|
|
return true;
|
|
}
|
|
|
|
public static bool QueryComponents<T,T1,T2> (this IServiceProvider self,out T t1,out T1 t2,out T2 t3) where T : class where T1 : class where T2 : class
|
|
{
|
|
t1 = null;
|
|
t2 = null;
|
|
t3 = null;
|
|
if (QueryComponents(self,out t1) is false) return false;
|
|
if (QueryComponents(self,out t2) is false) return false;
|
|
if (QueryComponents(self,out t3) is false) return false;
|
|
return true;
|
|
}
|
|
|
|
public static bool QueryComponents<T,T1,T2,T3> (this IServiceProvider self,out T t1,out T1 t2,out T2 t3,out T3 t4) where T : class where T1 : class where T2 : class where T3 : class
|
|
{
|
|
t1 = null;
|
|
t2 = null;
|
|
t3 = null;
|
|
t4 = null;
|
|
if (QueryComponents(self,out t1) is false) return false;
|
|
if (QueryComponents(self,out t2) is false) return false;
|
|
if (QueryComponents(self,out t3) is false) return false;
|
|
if (QueryComponents(self,out t4) is false) return false;
|
|
return true;
|
|
}
|
|
|
|
public static bool QueryComponent<TBase, T>(this IEnumerable<TBase> self, out T t)
|
|
{
|
|
foreach (var o in self)
|
|
{
|
|
if (o is not T v) continue;
|
|
t = v;
|
|
return true;
|
|
}
|
|
t = default;
|
|
return false;
|
|
}
|
|
public static bool QueryComponent<TBase, T, T1>(this IEnumerable<TBase> self, out T t, out T1 t1)
|
|
{
|
|
t = default;
|
|
t1 = default;
|
|
var count = 0;
|
|
foreach (var o in self)
|
|
{
|
|
if (o is not T v)
|
|
{
|
|
continue;
|
|
}
|
|
t = v;
|
|
count++;
|
|
if (o is not T1 v1) continue;
|
|
t1 = v1;
|
|
count++;
|
|
}
|
|
return count is 2;
|
|
}
|
|
|
|
public static T IsNull<T>(this T t, Action action)
|
|
{
|
|
if (t is null)
|
|
{
|
|
action.Invoke();
|
|
}
|
|
return t;
|
|
}
|
|
public static T IsNotNull<T>(this T t, Action<T> action)
|
|
{
|
|
if (t is not null)
|
|
{
|
|
action.Invoke(t);
|
|
}
|
|
return t;
|
|
}
|
|
}
|
|
} |