Beispiel #1
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            var logger = new ILPostProcessorLogger(new List <DiagnosticMessage>());

            logger.Debug($"process {compiledAssembly.Name}({string.Join("|", compiledAssembly.References.Select(Path.GetFileName).Where(f => !new [] {"System", "Mono", "mscorlib", "netstandard", "Microsoft", "Unity", "UnityEngine"}.Any(f.StartsWith)))})");

            using var resolver           = new PostProcessorAssemblyResolver(compiledAssembly.References);
            using var assemblyDefinition = compiledAssembly.LoadAssembly(resolver);

            var modified = false;

            var nodes = assemblyDefinition.MainModule.GetAllTypes()
                        .Where(type => type.IsClass && !type.IsAbstract && type.TypeImplements(typeof(INodeData)))
                        .ToArray()
            ;

            var methods = nodes.SelectMany(FetchNodeDataMethods)
                          .Where(method => method != null && !method.CustomAttributes.FindAccessorAttributes().Any())
            ;

            foreach (var method in methods)
            {
                var attributes = method.GenerateAccessorAttributes();
                if (!attributes.Any())
                {
                    continue;
                }

                modified = true;
                method.CustomAttributes.AddRange(attributes);
            }

            return(modified ? assemblyDefinition.Write(logger.Messages) : new ILPostProcessResult(null, logger.Messages));
        }
Beispiel #2
0
        public static ILPostProcessorLogger CreateLogger(this AssemblyDefinition assembly)
        {
            var logger = new ILPostProcessorLogger(new List <DiagnosticMessage>());

            // var loggerAttributes = assembly.GetAttributesOf<GenericSerializeReferenceLoggerAttribute>();
            // if (loggerAttributes.Any()) logger.LogLevel = (LogLevel)loggerAttributes.First().ConstructorArguments[0].Value;
            return(logger);
        }
Beispiel #3
0
        public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
        {
            var logger = new ILPostProcessorLogger(new List <DiagnosticMessage>());

            logger.Debug($"process {compiledAssembly.Name}({string.Join("|", compiledAssembly.References.Select(Path.GetFileName).Where(f => !new [] {"System", "Mono", "mscorlib", "netstandard", "Microsoft", "Unity", "UnityEngine"}.Any(f.StartsWith)))}) [{_CODE_GENERATOR.Value.Count}]");
            using var assemblyDefinition = LoadAssemblyDefinition(compiledAssembly);

            var modified = false;

            foreach (var generator in _CODE_GENERATOR.Value)
            {
                try
                {
                    generator.Logger = logger;
                    var m = generator.Generate(assemblyDefinition);
                    modified = modified || m;
                }
                catch (Exception ex)
                {
                    logger.Error(ex.Message);
                    break;
                }
            }

            if (!modified)
            {
                return(new ILPostProcessResult(null, logger.Messages));
            }

            var pe  = new MemoryStream();
            var pdb = new MemoryStream();
            var writerParameters = new WriterParameters
            {
                SymbolWriterProvider = new PortablePdbWriterProvider()
                , SymbolStream       = pdb
                , WriteSymbols       = true
            };

            assemblyDefinition.Write(pe, writerParameters);
            return(new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), logger.Messages));
        }