Initial Commit

This commit is contained in:
2026-03-16 14:38:46 +02:00
commit b8f7327a21
2327 changed files with 253610 additions and 0 deletions
@@ -0,0 +1,274 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ModestTree;
using Zenject.Internal;
namespace Zenject
{
[NoReflectionBaking]
public class InjectContext : IDisposable
{
BindingId _bindingId;
Type _objectType;
InjectContext _parentContext;
object _objectInstance;
string _memberName;
bool _optional;
InjectSources _sourceType;
object _fallBackValue;
object _concreteIdentifier;
DiContainer _container;
public InjectContext()
{
_bindingId = new BindingId();
Reset();
}
public InjectContext(DiContainer container, Type memberType)
: this()
{
Container = container;
MemberType = memberType;
}
public InjectContext(DiContainer container, Type memberType, object identifier)
: this(container, memberType)
{
Identifier = identifier;
}
public InjectContext(DiContainer container, Type memberType, object identifier, bool optional)
: this(container, memberType, identifier)
{
Optional = optional;
}
public void Dispose()
{
ZenPools.DespawnInjectContext(this);
}
public void Reset()
{
_objectType = null;
_parentContext = null;
_objectInstance = null;
_memberName = "";
_optional = false;
_sourceType = InjectSources.Any;
_fallBackValue = null;
_container = null;
_bindingId.Type = null;
_bindingId.Identifier = null;
}
public BindingId BindingId
{
get { return _bindingId; }
}
// The type of the object which is having its members injected
// NOTE: This is null for root calls to Resolve<> or Instantiate<>
public Type ObjectType
{
get { return _objectType; }
set { _objectType = value; }
}
// Parent context that triggered the creation of ObjectType
// This can be used for very complex conditions using parent info such as identifiers, types, etc.
// Note that ParentContext.MemberType is not necessarily the same as ObjectType,
// since the ObjectType could be a derived type from ParentContext.MemberType
public InjectContext ParentContext
{
get { return _parentContext; }
set { _parentContext = value; }
}
// The instance which is having its members injected
// Note that this is null when injecting into the constructor
public object ObjectInstance
{
get { return _objectInstance; }
set { _objectInstance = value; }
}
// Identifier - most of the time this is null
// It will match 'foo' in this example:
// ... In an installer somewhere:
// Container.Bind<Foo>("foo").AsSingle();
// ...
// ... In a constructor:
// public Foo([Inject(Id = "foo") Foo foo)
public object Identifier
{
get { return _bindingId.Identifier; }
set { _bindingId.Identifier = value; }
}
// The constructor parameter name, or field name, or property name
public string MemberName
{
get { return _memberName; }
set { _memberName = value; }
}
// The type of the constructor parameter, field or property
public Type MemberType
{
get { return _bindingId.Type; }
set { _bindingId.Type = value; }
}
// When optional, null is a valid value to be returned
public bool Optional
{
get { return _optional; }
set { _optional = value; }
}
// When set to true, this will only look up dependencies in the local container and will not
// search in parent containers
public InjectSources SourceType
{
get { return _sourceType; }
set { _sourceType = value; }
}
public object ConcreteIdentifier
{
get { return _concreteIdentifier; }
set { _concreteIdentifier = value; }
}
// When optional, this is used to provide the value
public object FallBackValue
{
get { return _fallBackValue; }
set { _fallBackValue = value; }
}
// The container used for this injection
public DiContainer Container
{
get { return _container; }
set { _container = value; }
}
public IEnumerable<InjectContext> ParentContexts
{
get
{
if (ParentContext == null)
{
yield break;
}
yield return ParentContext;
foreach (var context in ParentContext.ParentContexts)
{
yield return context;
}
}
}
public IEnumerable<InjectContext> ParentContextsAndSelf
{
get
{
yield return this;
foreach (var context in ParentContexts)
{
yield return context;
}
}
}
// This will return the types of all the objects that are being injected
// So if you have class Foo which has constructor parameter of type IBar,
// and IBar resolves to Bar, this will be equal to (Bar, Foo)
public IEnumerable<Type> AllObjectTypes
{
get
{
foreach (var context in ParentContextsAndSelf)
{
if (context.ObjectType != null)
{
yield return context.ObjectType;
}
}
}
}
public InjectContext CreateSubContext(Type memberType)
{
return CreateSubContext(memberType, null);
}
public InjectContext CreateSubContext(Type memberType, object identifier)
{
var subContext = new InjectContext();
subContext.ParentContext = this;
subContext.Identifier = identifier;
subContext.MemberType = memberType;
// Clear these
subContext.ConcreteIdentifier = null;
subContext.MemberName = "";
subContext.FallBackValue = null;
// Inherit these ones by default
subContext.ObjectType = ObjectType;
subContext.ObjectInstance = ObjectInstance;
subContext.Optional = Optional;
subContext.SourceType = SourceType;
subContext.Container = Container;
return subContext;
}
public InjectContext Clone()
{
var clone = new InjectContext();
clone.ObjectType = ObjectType;
clone.ParentContext = ParentContext;
clone.ConcreteIdentifier = ConcreteIdentifier;
clone.ObjectInstance = ObjectInstance;
clone.Identifier = Identifier;
clone.MemberType = MemberType;
clone.MemberName = MemberName;
clone.Optional = Optional;
clone.SourceType = SourceType;
clone.FallBackValue = FallBackValue;
clone.Container = Container;
return clone;
}
// This is very useful to print out for debugging purposes
public string GetObjectGraphString()
{
var result = new StringBuilder();
foreach (var context in ParentContextsAndSelf.Reverse())
{
if (context.ObjectType == null)
{
continue;
}
result.AppendLine(context.ObjectType.PrettyName());
}
return result.ToString();
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2005ce29739317b48bcc3c06695da848
timeCreated: 1461708048
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using ModestTree;
namespace Zenject
{
[DebuggerStepThrough]
public struct TypeValuePair
{
public Type Type;
public object Value;
public TypeValuePair(Type type, object value)
{
Type = type;
Value = value;
}
}
[DebuggerStepThrough]
public static class InjectUtil
{
public static List<TypeValuePair> CreateArgList(IEnumerable<object> args)
{
Assert.That(!args.ContainsItem(null),
"Cannot include null values when creating a zenject argument list because zenject has no way of deducing the type from a null value. If you want to allow null, use the Explicit form.");
return args.Select(x => new TypeValuePair(x.GetType(), x)).ToList();
}
public static TypeValuePair CreateTypePair<T>(T param)
{
// Use the most derived type that we can find here
return new TypeValuePair(
param == null ? typeof(T) : param.GetType(), param);
}
public static List<TypeValuePair> CreateArgListExplicit<T>(T param)
{
return new List<TypeValuePair>
{
CreateTypePair(param)
};
}
public static List<TypeValuePair> CreateArgListExplicit<TParam1, TParam2>(TParam1 param1, TParam2 param2)
{
return new List<TypeValuePair>
{
CreateTypePair(param1),
CreateTypePair(param2)
};
}
public static List<TypeValuePair> CreateArgListExplicit<TParam1, TParam2, TParam3>(
TParam1 param1, TParam2 param2, TParam3 param3)
{
return new List<TypeValuePair>
{
CreateTypePair(param1),
CreateTypePair(param2),
CreateTypePair(param3)
};
}
public static List<TypeValuePair> CreateArgListExplicit<TParam1, TParam2, TParam3, TParam4>(
TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4)
{
return new List<TypeValuePair>
{
CreateTypePair(param1),
CreateTypePair(param2),
CreateTypePair(param3),
CreateTypePair(param4)
};
}
public static List<TypeValuePair> CreateArgListExplicit<TParam1, TParam2, TParam3, TParam4, TParam5>(
TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5)
{
return new List<TypeValuePair>
{
CreateTypePair(param1),
CreateTypePair(param2),
CreateTypePair(param3),
CreateTypePair(param4),
CreateTypePair(param5)
};
}
public static List<TypeValuePair> CreateArgListExplicit<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6>(
TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6)
{
return new List<TypeValuePair>
{
CreateTypePair(param1),
CreateTypePair(param2),
CreateTypePair(param3),
CreateTypePair(param4),
CreateTypePair(param5),
CreateTypePair(param6)
};
}
// Find the first match with the given type and remove it from the list
// Return true if it was removed
public static bool PopValueWithType(
List<TypeValuePair> extraArgMap, Type injectedFieldType, out object value)
{
for (int i = 0; i < extraArgMap.Count; i++)
{
var arg = extraArgMap[i];
if (arg.Type.DerivesFromOrEqual(injectedFieldType))
{
value = arg.Value;
extraArgMap.RemoveAt(i);
return true;
}
}
value = injectedFieldType.GetDefaultValue();
return false;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 0ebfddbd9452a9c4cb6c9dfe7a25ea53
timeCreated: 1461708048
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,42 @@
using ModestTree;
namespace Zenject
{
[ZenjectAllowDuringValidation]
[NoReflectionBaking]
public class LazyInject<T> : IValidatable
{
readonly DiContainer _container;
readonly InjectContext _context;
bool _hasValue;
T _value;
public LazyInject(DiContainer container, InjectContext context)
{
Assert.DerivesFromOrEqual<T>(context.MemberType);
_container = container;
_context = context;
}
void IValidatable.Validate()
{
_container.Resolve(_context);
}
public T Value
{
get
{
if (!_hasValue)
{
_value = (T)_container.Resolve(_context);
_hasValue = true;
}
return _value;
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 54883063c774a244c898edbc91b859a0
timeCreated: 1529237555
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: