コード例 #1
0
        public void Setup()
        {
            var harmony = new Harmony($"{nameof(DependencyInjectionTests)}.{nameof(Setup)}");

            harmony.Patch(SymbolExtensions.GetMethodInfo(() => FSIOHelper.GetConfigPath()),
                          prefix: new HarmonyMethod(DelegateHelper.GetMethodInfo(MockedGetConfigPath)));
            harmony.Patch(SymbolExtensions2.GetPropertyInfo(() => TWCommon.ConfigName).GetMethod,
                          prefix: new HarmonyMethod(DelegateHelper.GetMethodInfo(MockedGetConfigName)));
            var engineUtilitiesType = Type.GetType("TaleWorlds.Engine.Utilities, TaleWorlds.Engine", false);

            harmony.Patch(engineUtilitiesType?.GetMethod("GetModulesNames", BindingFlags.Public | BindingFlags.Static),
                          prefix: new HarmonyMethod(DelegateHelper.GetMethodInfo(MockedGetModuleNames)));

            var subModule        = new ButterLibSubModule();
            var subModuleWrapper = new MBSubModuleBaseWrapper(subModule);

            subModuleWrapper.SubModuleLoad();
            ExceptionHandlerSubSystem.Instance?.Disable();

            var services = ButterLibSubModule.Instance !.GetServices() !;

            services.AddScoped <CampaignDescriptor, CampaignDescriptorImplementation>();
            services.AddSingleton <ICampaignDescriptorStatic, CampaignDescriptorStaticImplementation>();
            services.AddScoped(typeof(DistanceMatrix <>), typeof(DistanceMatrixImplementation <>));
            services.AddSingleton <IDistanceMatrixStatic, DistanceMatrixStaticImplementation>();
            services.AddSingleton <ICampaignExtensions, CampaignExtensionsImplementation>();
            subModuleWrapper.BeforeInitialModuleScreenSetAsRoot();
        }
コード例 #2
0
        public void DistanceMatrix_Test()
        {
            using var harmony = new HarmonyDisposable($"{nameof(DependencyInjectionTests)}.{nameof(DistanceMatrix_Test)}");
            harmony.Patch(SymbolExtensions2.GetPropertyInfo(() => Settlement.All).GetMethod,
                          prefix: new HarmonyMethod(DelegateHelper.GetMethodInfo(MockedGetSettlementAll)));


            var serviceProvider = ButterLibSubModule.Instance !.GetServiceProvider() !;
            var scope           = serviceProvider.CreateScope();

            var distanceMatrix = scope.ServiceProvider.GetRequiredService <DistanceMatrix <Settlement> >();

            Assert.NotNull(distanceMatrix);
            Assert.True(distanceMatrix.GetType().GetGenericTypeDefinition() == typeof(DistanceMatrixImplementation <>));
            Assert.AreEqual(distanceMatrix.GetType().GenericTypeArguments, new[] { typeof(Settlement) });


            var distanceMatrix1 = Bannerlord.ButterLib.DistanceMatrix.DistanceMatrix.Create <Settlement>() !;

            Assert.NotNull(distanceMatrix1);
            Assert.True(distanceMatrix1.GetType().GetGenericTypeDefinition() == typeof(DistanceMatrixImplementation <>));
            Assert.AreEqual(distanceMatrix1.GetType().GenericTypeArguments, new[] { typeof(Settlement) });

            var distanceMatrix2 = Bannerlord.ButterLib.DistanceMatrix.DistanceMatrix <Settlement> .Create() !;

            Assert.NotNull(distanceMatrix2);
            Assert.True(distanceMatrix2.GetType().GetGenericTypeDefinition() == typeof(DistanceMatrixImplementation <>));
            Assert.AreEqual(distanceMatrix2.GetType().GenericTypeArguments, new[] { typeof(Settlement) });
        }
コード例 #3
0
        private static bool MockedGetSettlementAll(ref MBReadOnlyList <Settlement> __result)
        {
            var settlement = (Settlement)FormatterServices.GetUninitializedObject(typeof(Settlement));
            var village    = (Village)FormatterServices.GetSafeUninitializedObject(typeof(Village));

            SymbolExtensions2.GetPropertyInfo((Settlement s) => s.Id).SetValue(settlement, new MBGUID(1));
            SymbolExtensions2.GetPropertyInfo((Settlement s) => s.IsInitialized).SetValue(settlement, true);
            SymbolExtensions2.GetFieldInfo((Settlement s) => s.Village).SetValue(settlement, village);

            __result = new MBReadOnlyList <Settlement>(new List <Settlement> {
                settlement
            });
            return(false);
        }
コード例 #4
0
 public static PropertyInfo?GetPropertyInfo(LambdaExpression expression)
 => SymbolExtensions3.GetPropertyInfo(expression);
コード例 #5
0
 public static PropertyInfo?GetPropertyInfo <T, TResult>(Expression <Func <T, TResult> > expression)
 => SymbolExtensions3.GetPropertyInfo <T, TResult>(expression);
コード例 #6
0
 public static FieldInfo?GetFieldInfo(LambdaExpression expression)
 => SymbolExtensions3.GetFieldInfo(expression);
コード例 #7
0
 public static FieldInfo?GetFieldInfo <T, TResult>(Expression <Func <T, TResult> > expression)
 => SymbolExtensions3.GetFieldInfo <T, TResult>(expression);
コード例 #8
0
 public static ConstructorInfo?GetConstructorInfo(LambdaExpression expression)
 => SymbolExtensions3.GetConstructorInfo(expression);
コード例 #9
0
 public static ConstructorInfo?GetConstructorInfo <T, TResult>(Expression <Func <T, TResult> > expression)
 => SymbolExtensions3.GetConstructorInfo <T, TResult>(expression);