private static void Execute(ModuleDefinition mainModule, ModuleDefinition l2MModule, ModuleDefinition coreModule)
        {
            var l2MOptimizeAttribute = l2MModule.GetType("LinqPatcher.Attributes", "OptimizeAttribute");
            var typeSystem           = mainModule.TypeSystem;

            var classAnalyzer  = new ClassAnalyzer(mainModule, l2MOptimizeAttribute);
            var methodAnalyzer = new MethodAnalyzer(typeSystem);
            var methodBuilder  = new MethodBuilder(mainModule, coreModule);

            var analyzedClass = classAnalyzer.Analyze();

            foreach (var targetClass in analyzedClass.OptimizeTypes)
            {
                foreach (var method in classAnalyzer.AnalyzeMethod(targetClass))
                {
                    var analyzedMethod = methodAnalyzer.Analyze(method);
                    var methodName     = Guid.NewGuid().ToString("N");

                    methodBuilder.Create(targetClass, methodName, analyzedMethod.ParameterType, analyzedMethod.ReturnType);
                    methodBuilder.Begin();

                    foreach (var linqOperator in analyzedMethod.Operators)
                    {
                        var linq = methodAnalyzer.OperatorFactory(linqOperator, methodBuilder);
                        methodBuilder.AppendOperator(linq);
                    }

                    methodBuilder.BuildOperator();
                    methodBuilder.End();
                    methodBuilder.Replace(method);
                }
            }

            mainModule.Write("Main.dll");
        }
        private static void Execute(ModuleDefinition mainModule, ModuleDefinition l2MModule, ModuleDefinition coreModule)
        {
            var l2MOptimizeAttribute = l2MModule.GetType("LinqPatcher.Attributes", "OptimizeAttribute");

            var classAnalyzer  = new ClassAnalyzer(mainModule, l2MOptimizeAttribute);
            var methodAnalyzer = new MethodAnalyzer(coreModule);
            var methodBuilder  = new MethodBuilder(mainModule, coreModule);

            var analyzedClass = classAnalyzer.Analyze();

            foreach (var optimizeClass in analyzedClass.OptimizeTypes)
            {
                foreach (var method in classAnalyzer.AnalyzeMethod(optimizeClass))
                {
                    var analyzedMethod = methodAnalyzer.Analyze(method);

                    var returnType = mainModule.ImportReference(analyzedMethod.ReturnType);

                    methodBuilder.Create(optimizeClass, MethodHelper.CreateUniqueName, analyzedMethod.ParameterType, returnType);
                    methodBuilder.Begin();

                    foreach (var linqOperator in analyzedMethod.Operators)
                    {
                        var linq = methodAnalyzer.OperatorFactory(linqOperator, methodBuilder);
                        methodBuilder.AppendOperator(linq);
                    }

                    methodBuilder.BuildOperator();
                    methodBuilder.End();
                    methodBuilder.Replace(method);
                }
            }

            mainModule.Write($"{TargetModuleName}.dll");
        }
Beispiel #3
0
 public void HandleResponseRoutesWithNameSetToNullOrEmpty(Type subscriberType)
 {
     Assert.Throws <NetmqRouterException>(() =>
     {
         var subscriber = Activator.CreateInstance(subscriberType);
         ClassAnalyzer.AnalyzeClass(subscriber);
     });
 }
Beispiel #4
0
        public void HandleRouteWithMoreThanOneArgument()
        {
            var subscriber = new ExampleSubscriberRouteWithMoreThanOneArgument();

            Assert.Throws <ConfigurationException>(() =>
            {
                ClassAnalyzer.AnalyzeClass(subscriber);
            });
        }
Beispiel #5
0
        public static IMessageRouter RegisterSubscriber <T>(this IMessageRouter router, T subscriber)
        {
            ClassAnalyzer
            .AnalyzeClass(subscriber)
            .ToList()
            .ForEach(x => router.RegisterSubscriber(x));

            return(router);
        }
Beispiel #6
0
        public string CorrectlyHandleRoutesWithoutRouteResponseAttribute(string incomingROuteName)
        {
            var subscriber = new ExampleSubscriberWithVariousResponseConfiguration();

            return(ClassAnalyzer
                   .AnalyzeClass(subscriber)
                   .First(x => x.Incoming.Name == incomingROuteName)
                   .Outcoming
                   ?.Name);
        }
Beispiel #7
0
        public Type CorrectlyDiscoverOutcomingDataType(string methodName)
        {
            var subscriber = new ExampleSubscriberWithOutcomingRoutes();

            return(ClassAnalyzer
                   .AnalyzeClass(subscriber)
                   .First(x => x.Outcoming.Name == methodName)
                   .Outcoming
                   .DataType);
        }
Beispiel #8
0
        public string Generate(string classAsAString)
        {
            var analyzer       = new ClassAnalyzer();
            var analysisResult = analyzer.Analyze(classAsAString);

            var modelCreator = new BuilderModelCreator();
            var model        = modelCreator.Create(analysisResult);

            var codeGenerator = new BuilderCodeGenerator();

            return(codeGenerator.Generate(model));
        }
Beispiel #9
0
        public void HandleBaseRoute()
        {
            // arrange
            var subscriber = new ClassWithBaseRoute();

            // act
            var result = ClassAnalyzer.AnalyzeClass(subscriber);

            // assert
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual("BaseRoute/IncomingRoute", result[0].Incoming.Name);
            Assert.AreEqual("OutcomingRoute", result[0].Outcoming.Name);
        }
Beispiel #10
0
        public void CorrectlyCallRoute(string routeName, string value)
        {
            // arrange
            var subscriber = new ExampleSubscriberWithCallHandler();
            var routes     = ClassAnalyzer.AnalyzeClass(subscriber);

            // act
            routes
            .First(x => x.Incoming.Name == routeName)
            .Method(value);

            // assert
            Assert.AreEqual(value, subscriber.PassedValue);
        }
        public void Parse_NoErrorIfNoAttributeTestClass_Test()
        {
            const string filecontent = @"
 public class Sample
 {
    [TestMethod]
    public void FooMethod_Descr()
    {
    }
 }";
            var          target      = new ClassAnalyzer(filecontent);

            target.Parse();
            Assert.IsFalse(target.Error.Any());
        }
        public void Parse_NoErrorIfContaintPostfix_Test()
        {
            const string filecontent = @"
 [TestClass]
 public class Sample
 {
    [TestMethod]
    public void FooMethod_Descr_Test()
    {
    }
 }";
            var          target      = new ClassAnalyzer(filecontent);

            target.Parse();
            Assert.IsFalse(target.Error.Any());
        }
Beispiel #13
0
        public void DiscoverOnlyMethodsWithRouteAttribute()
        {
            var subscriber         = new ExampleSubscriberWithIncommingRoutes();
            var incomingRouteNames = ClassAnalyzer
                                     .AnalyzeClass(subscriber)
                                     .Select(x => x.Incoming.Name);

            var expected = new[]
            {
                "GetEvent",
                "GetRaw",
                "GetText",
                "GetObject"
            };

            Assert.AreEqual(expected, incomingRouteNames);
        }
        public void Parse_ErrorIfNotContaintPostfix_Test()
        {
            const string filecontent   = @"
 [TestClass]
 public class Sample
 {
    [TestMethod]
    public void FooMethod()
    {
    }
 }";
            const string expectedError = "FooMethod";
            var          target        = new ClassAnalyzer(filecontent);

            target.Parse();
            Assert.IsTrue(target.Error.Contains(expectedError));
        }
        public void Parse_ErrorIfContaintSymbolsAfterPostfix_Test()
        {
            const string filecontent = @"
 [TestClass]
 public class Sample
 {
    [TestMethod]
    public void FooMethod_Descr_Test2()
    {
    }
 }";
            const string expected    = "FooMethod_Descr_Test2";
            var          target      = new ClassAnalyzer(filecontent);

            target.Parse();
            Assert.IsTrue(target.Error.Contains(expected));
        }
Beispiel #16
0
        public void DiscoverOnlyMethodsWithRouteResponseAttribute()
        {
            var subscriber          = new ExampleSubscriberWithOutcomingRoutes();
            var outcomingRouteNames = ClassAnalyzer
                                      .AnalyzeClass(subscriber)
                                      .Select(x => x.Outcoming.Name)
                                      .ToList();

            var expected = new[]
            {
                "ResponseEvent",
                "ResponseRaw",
                "ResponseText",
                "ResponseObject"
            };

            Assert.AreEqual(expected, outcomingRouteNames);
        }
Beispiel #17
0
            private AnalysisResult Execute()
            {
                var analyzer = new ClassAnalyzer();

                return(analyzer.Analyze(classAsAString));
            }
        private ModInfo ReadFromClassFile(ZipFileInfo file)
        {
            if (file.IsVolumeLabel || file.IsFolder || !file.Name.ToLower().EndsWith(".class"))
            {
                return(null);
            }

            ClassAnalyzer analyzer = null;

            using (Stream stream = file.OpenStream()) analyzer = ClassAnalyzer.Read(stream);
            if (analyzer == null)
            {
                return(null);
            }

            ModInfo info = new ModInfo();

            foreach (JavaAttribute attribute in analyzer.Attributes)
            {
                if (attribute == null)
                {
                    continue;
                }
                if (attribute is JavaRuntimeAnnotations)
                {
                    foreach (JavaAnnotation annotation in ((JavaRuntimeAnnotations)attribute).Annotations)
                    {
                        if (annotation.Name.EndsWith("/fml/common/Mod;"))
                        {
                            if (annotation.Elements.ContainsKey("modid"))
                            {
                                info.ID = (string)annotation.Elements["modid"];
                            }
                            if (annotation.Elements.ContainsKey("name"))
                            {
                                info.Name = (string)annotation.Elements["name"];
                            }
                            if (annotation.Elements.ContainsKey("version"))
                            {
                                info.Version = (string)annotation.Elements["version"];
                            }
                            if (annotation.Elements.ContainsKey("acceptedMinecraftVersions"))
                            {
                                info.MinecraftVersion = VersionTools.ToVersion((string)annotation.Elements["acceptedMinecraftVersions"]);
                            }
                            if (annotation.Elements.ContainsKey("dependencies"))
                            {
                                info.Dependencies = VersionTools.ToVersion((string)annotation.Elements["dependencies"]);
                            }
                        }
                        else if (annotation.Name.EndsWith("/fml/relauncher/IFMLLoadingPlugin$Name;"))
                        {
                            info.Name = (string)annotation.Elements["value"];
                            if (info.ID == null)
                            {
                                info.ID = info.Name;
                            }
                        }
                        else if (annotation.Name.EndsWith("/fml/relauncher/IFMLLoadingPlugin$MCVersion;"))
                        {
                            info.MinecraftVersion = VersionTools.ToVersion((string)annotation.Elements["value"]);
                        }
                    }
                }
            }

            return(info);
        }
Beispiel #19
0
        public void HandleMethodWithRouteResponseAttributeButwithoutBasicRoute()
        {
            var subscriber = new ExampleSubscriberWithOutcomingRouteButWithoutIcoming();

            Assert.Throws <ConfigurationException>(() => { ClassAnalyzer.AnalyzeClass(subscriber); });
        }