Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Dec 20, 2023
1 parent dee562d commit 30e49a7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
@@ -1,64 +1,67 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace VContainer.Internal
{
public class OpenGenericInstanceProvider : IInstanceProvider
{
class TypeParametersEqualityComparer : IEqualityComparer<Type[]>
{
public bool Equals(Type[] x, Type[] y)
{
if (x == null || y == null) return x == y;
if (x.Length != y.Length) return false;

for (var i = 0; i < x.Length; i++)
{
if (x[i] != y[i]) return false;
}
return true;
}

public int GetHashCode(Type[] typeParameters)
{
var hash = 5381;
foreach (var typeParameter in typeParameters)
{
hash = ((hash << 5) + hash) ^ typeParameter.GetHashCode();
}
return hash;
}
}

readonly Lifetime lifetime;
readonly Type implementationType;
readonly IReadOnlyList<IInjectParameter> customParameters;

readonly ConcurrentDictionary<int, Registration> registrations;
readonly ConcurrentDictionary<Type, int> typeParametersHashes;
readonly ConcurrentDictionary<Type[], Registration> constructedRegistrations = new ConcurrentDictionary<Type[], Registration>(new TypeParametersEqualityComparer());
readonly Func<Type[], Registration> createRegistrationFunc;

public OpenGenericInstanceProvider(Type implementationType, Lifetime lifetime,
List<IInjectParameter> injectParameters)
public OpenGenericInstanceProvider(Type implementationType, Lifetime lifetime, List<IInjectParameter> injectParameters)
{
this.implementationType = implementationType;
this.lifetime = lifetime;
customParameters = injectParameters;
typeParametersHashes = new ConcurrentDictionary<Type, int>();
registrations = new ConcurrentDictionary<int, Registration>();
createRegistrationFunc = CreateRegistration;
}

public Registration GetClosedRegistration(Type closedInterfaceType, Type[] typeParameters)
{
var typeParametersHash = typeParametersHashes.GetOrAdd(closedInterfaceType, (_, arg) =>
((IStructuralEquatable)arg).GetHashCode(EqualityComparer<Type>.Default), typeParameters);

var registrationArgs = new RegistrationArguments
{
ImplementationType = implementationType,
Lifetime = lifetime,
CustomParameters = customParameters,
TypeParameters = typeParameters
};

return registrations.GetOrAdd(typeParametersHash, (_, args) => CreateRegistration(args), registrationArgs);
return constructedRegistrations.GetOrAdd(typeParameters, createRegistrationFunc);
}

private static Registration CreateRegistration(RegistrationArguments args)
Registration CreateRegistration(Type[] typeParameters)
{
var newType = args.ImplementationType.MakeGenericType(args.TypeParameters);
var newType = implementationType.MakeGenericType(typeParameters);
var injector = InjectorCache.GetOrBuild(newType);
var spawner = new InstanceProvider(injector, args.CustomParameters);
return new Registration(newType, args.Lifetime, new List<Type>(1) { newType }, spawner);
var spawner = new InstanceProvider(injector, customParameters);
return new Registration(newType, lifetime, new List<Type>(1) { newType }, spawner);
}

public object SpawnInstance(IObjectResolver resolver)
{
throw new InvalidOperationException();
}

private struct RegistrationArguments
{
public Type ImplementationType;
public Lifetime Lifetime;
public IReadOnlyList<IInjectParameter> CustomParameters;
public Type[] TypeParameters;
}
}
}
}
33 changes: 11 additions & 22 deletions VContainer/Packages/packages-lock.json
@@ -1,21 +1,5 @@
{
"dependencies": {
"com.cysharp.runtimeunittesttoolkit": {
"version": "https://github.com/Cysharp/RuntimeUnitTestToolkit.git?path=RuntimeUnitTestToolkit/Assets/RuntimeUnitTestToolkit",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "1a9d2bee8d37f8e5317b7efe91e70c977c679fd6"
},
"com.unity.ai.navigation": {
"version": "1.1.4",
"depth": 0,
"source": "registry",
"dependencies": {
"com.unity.modules.ai": "1.0.0"
},
"url": "https://packages.unity.com"
},
"com.unity.ext.nunit": {
"version": "1.0.6",
"depth": 1,
Expand Down Expand Up @@ -61,12 +45,6 @@
"com.unity.modules.imgui": "1.0.0"
}
},
"com.unity.modules.ai": {
"version": "1.0.0",
"depth": 1,
"source": "builtin",
"dependencies": {}
},
"com.unity.modules.animation": {
"version": "1.0.0",
"depth": 0,
Expand Down Expand Up @@ -131,6 +109,17 @@
"version": "1.0.0",
"depth": 0,
"source": "builtin",
"dependencies": {
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.imgui": "1.0.0",
"com.unity.modules.jsonserialize": "1.0.0",
"com.unity.modules.uielementsnative": "1.0.0"
}
},
"com.unity.modules.uielementsnative": {
"version": "1.0.0",
"depth": 1,
"source": "builtin",
"dependencies": {
"com.unity.modules.ui": "1.0.0",
"com.unity.modules.imgui": "1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions VContainer/ProjectSettings/ProjectVersion.txt
@@ -1,2 +1,2 @@
m_EditorVersion: 2022.3.5f1
m_EditorVersionWithRevision: 2022.3.5f1 (9674261d40ee)
m_EditorVersion: 2021.3.30f1
m_EditorVersionWithRevision: 2021.3.30f1 (b4360d7cdac4)

0 comments on commit 30e49a7

Please sign in to comment.