public void GenerateFrom(ManagedCefApiTypes managedTypes)
        {
            var license = string.Join("\r\n", File.ReadAllText(Path.Combine("Settings", "LicenseTemplate.txt"), Encoding.UTF8).Trim().Split('\n').Select(s => "// " + s.Trim()));

            foreach (INamedTypeSymbol symbol in managedTypes.RefCountedClasses)
            {
                if (!symbol.AllInterfaces.Any(t => t.TypeKind == TypeKind.Interface && t.Name.EndsWith("Private")))
                {
                    continue;
                }

                IMethodSymbol[] avoids = symbol.GetMembers().OfType <IMethodSymbol>().Where(m => m.Name.Split('.').Last().StartsWith("Avoid")).ToArray();
                if (avoids.Length == 0)
                {
                    continue;
                }

                string filePath = symbol.Locations[0].SourceTree.FilePath;

                Output = new StreamWriter(Path.Combine(OutputPath, symbol.Name + ".il"), false, Encoding.UTF8);
                try
                {
                    Output.WriteLine(license, nameof(CefGen), filePath.Substring(filePath.IndexOf("Generated")).Replace('\\', '/'));
                    Output.WriteLine();
                    Output.Write(".namespace CefNet");
                    WriteBlockStart(CodeGenBlockType.Namespace);
                    WriteIndent();
                    Output.Write(".class public " + symbol.Name);
                    WriteBlockStart(CodeGenBlockType.Type);
                    Output.WriteLine();
                    foreach (IMethodSymbol method in avoids)
                    {
                        WriteMsilMethod(method, FindTarget(method, symbol));
                        Output.WriteLine();
                    }
                    WriteBlockEnd(CodeGenBlockType.Type);
                    WriteBlockEnd(CodeGenBlockType.Namespace);
                    Output.Flush();
                }
                finally
                {
                    Output.Close();
                    Output = null;
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            string cefPath     = null;
            string outDirPath  = null;
            string projectPath = GetProjectPath();

            bool onlyStdCall = false;

            foreach (string arg in args)
            {
                if (arg.StartsWith("--"))
                {
                    if (arg.StartsWith("--out="))
                    {
                        outDirPath = arg.Substring(6);
                    }
                    if (arg == "--stdcall")
                    {
                        onlyStdCall = true;
                    }
                    continue;
                }
                else if (cefPath == null)
                {
                    cefPath = arg;
                }
            }

            if (string.IsNullOrWhiteSpace(cefPath))
            {
                cefPath = Path.Combine(projectPath, "..", "cef");
            }
            else
            {
                cefPath = cefPath.Trim();
            }
            if (!Directory.Exists(cefPath))
            {
                Console.WriteLine("Could not find the CEF directory.");
                Environment.Exit(-1);
                return;
            }

            if (string.IsNullOrWhiteSpace(outDirPath))
            {
                outDirPath = Path.Combine(projectPath, "..", "CefNet", "Generated");
            }
            else
            {
                outDirPath = outDirPath.Trim();
            }
            if (!Directory.Exists(Path.GetDirectoryName(outDirPath)))
            {
                Console.WriteLine("Could not create the output directory.");
                Environment.Exit(-1);
                return;
            }

            Clean(outDirPath);

            Console.WriteLine("Generate unsafe types...");
            GenerateFromCHeaders(cefPath, outDirPath, onlyStdCall);

            Console.WriteLine("Compile unsafe types...");
            var nativeTypes = new NativeCefApiTypes(outDirPath);

            nativeTypes.Build();

            Console.WriteLine("Generate wrappers...");
            var managedApiBuilder = new ManagedCefApiBuilder(outDirPath);

            managedApiBuilder.Imports.Add("CefNet.CApi");
            managedApiBuilder.Imports.Add("CefNet.Internal");
            managedApiBuilder.EnableCallbackOverrideCheck = true;
            managedApiBuilder.GenerateFrom(nativeTypes);

            Console.WriteLine("Generate glue classes...");
            var cefnetGen = new CefNetCodeGen(outDirPath);

            cefnetGen.Generate();

            Console.WriteLine("Compile wrappers...");
            var managedTypes = new ManagedCefApiTypes(outDirPath);

            managedTypes.Build();

            Console.WriteLine("Generate MSIL for wrappers...");
            var msilGen = new ManagedCefApiMsilCodeGen(outDirPath);

            msilGen.GenerateFrom(managedTypes);

            Console.WriteLine("Complete.");
        }