private static List <string> GetDepDlls(UnityPlugin plugin) { var dlls = new List <string>(); if (plugin != null) { if (plugin.pluginState == UnityPlugin.PluginState.convert) { if (plugin.convertedPath.convertedDLLPath != null && File.Exists(plugin.convertedPath.convertedDLLPath)) { dlls.Add(plugin.convertedPath.convertedDLLPath); } } else { if (plugin.stubPath.stubDLLPath != null && File.Exists(plugin.stubPath.stubDLLPath)) { dlls.Add(plugin.stubPath.stubDLLPath); } if (plugin.stubPath.stubRefDLLPath != null && File.Exists(plugin.stubPath.stubRefDLLPath)) { dlls.Add(plugin.stubPath.stubRefDLLPath); } } } var unityDllpath = UnityPluginUtil.GetUnityEngineStub().stubPath.stubDLLPath; if (File.Exists(unityDllpath)) { dlls.Add(unityDllpath); } return(dlls); }
private static List <string> GetDepDlls(UnityPluginConfig pluginConfig) { var dlls = new List <string>(); pluginConfig.unityPlugins .Append(UnityPluginUtil.GetUnityEngineStub()) .Where(p => p.enable).ToList() .ForEach(plugin => { if (plugin.pluginState == UnityPlugin.PluginState.convert) { if (plugin.convertedPath.convertedDLLPath != null && File.Exists(plugin.convertedPath.convertedDLLPath)) { dlls.Add(plugin.convertedPath.convertedDLLPath); } } else { if (plugin.stubPath.stubDLLPath != null && File.Exists(plugin.stubPath.stubDLLPath)) { dlls.Add(plugin.stubPath.stubDLLPath); } } }); return(dlls); }
private static void GeneratePluginStubDLL(UnityPlugin config) { if (!config.enable) { return; } if (references == null) { references = DirectoryBuilder.RegisterDirectory("references", new DirectoryStructure("References~")); } var sources = new List <string>() { config.stubPath.stubCSPath }; // Add refCS stub file var refCS = config.stubPath.stubRefCSPath; if (refCS != null && File.Exists(refCS)) { sources.Add(refCS); } var os = File.Create(config.stubPath.stubDLLPath); // var refs = UnityPluginUtil.GetLib(config, true, true); var refs = new List <string>() { references["Bridge"]["Bridge.dll"], UnityPluginUtil.GetUnityEngineStub().stubPath.stubDLLPath }; if (refs == null || refs.Count == 0) { return; } EmitResult result; try { result = DLLProc.BuildDLL(config.pluginName + "-stub", os, sources, config.defineMacros, refs, true); } finally { os.Close(); } if (result == null || !result.Success) { throw new Exception(); } }
public static List <string> GetBridgeExcludes(ProjectExportConfig projectConfig, UnityPluginConfig pluginConfig) { var projectExcludes = GetProjectExcludes(projectConfig); // Debug.Log(projectExcludes.Join(";")); var pluginsExcludes = new List <string>(); var pluginsRoot = new List <UnityEngine.Object>(); var pluginsStubCs = new List <string>(); pluginConfig.unityPlugins.ForEach(p => { // 忽略插件生成的stub.cs if (p.stubPath.stubCSPath != null && File.Exists(p.stubPath.stubCSPath)) { pluginsStubCs.Add(p.stubPath.stubCSPath.PathToAssets()); } // 忽略插件生成的ref.cs if (p.stubPath.stubRefCSPath != null && File.Exists(p.stubPath.stubRefCSPath)) { pluginsStubCs.Add(p.stubPath.stubRefCSPath.PathToAssets()); } // 忽略插件源码 if (p.enable) { pluginsRoot.Add(p.pluginPath.pluginRoot); } }); // unity stub var unityStub = UnityPluginUtil.GetUnityEngineStub(); pluginsStubCs.Add(unityStub.stubPath.stubCSPath.PathToAssets()); var pluginsFiles = UnityPluginUtil.GetPluginFilesFromConfig(pluginsRoot); if (pluginsFiles != null) { pluginsExcludes.AddRange(pluginsFiles); // Debug.Log(pluginsExcludes.Join(";")); } pluginsExcludes.AddRange(pluginsStubCs); // Debug.Log(pluginsStubCs.Join(";")); projectExcludes.AddRange(pluginsExcludes); return(projectExcludes); }
private static void ExportPlugins(UnityPlugin single = null) { var logger = new Bridge.Translator.Logging.Logger("plugins_export_logger", true, LoggerLevel.None, false, new MyLoggerWriter(), new Bridge.Translator.Logging.FileLoggerWriter()); var plugins = ConfigManager.configEntry.unityPluginConfig.unityPlugins; // 处理只转换一个插件的情况 if (single != null) { // if (single.pluginState == UnityPlugin.PluginState.stub) { // single.stubConfig.generateJSTemplate = true; // } plugins = new List <UnityPlugin>() { single }; } plugins .Where(p => p.enable) .ToList() .ForEach(plugin => { // clear temp RemoveTempCodeDir(); var bridgeOptions = new BridgeOptions(); if (plugin.pluginState == UnityPlugin.PluginState.convert) { plugin.convertedPath = new UnityPlugin.ConvertedPath(plugin); ConfigureBridgeOptionsForConvertPlugin(ref bridgeOptions, plugin); ProcessBridge(bridgeOptions, logger, // Pre Process (processor) => { // add plugin outside logger.Info("Add Plugin..."); if (single != null) { processor.Translator.Plugins.AddPlugin(new BridgePlugin(single.pluginName)); } else { processor.Translator.Plugins.AddPlugin(new BridgePlugin()); } logger.Info("Add Plugin...[done]"); // clean js output DirectoryUtil.DeleteDirectory(plugin.convertedPath.convertedJSPath); { // remove all dll2 var distDir = references["Bridge"].FullPath; Directory.EnumerateFiles(distDir, "*.dll2", SearchOption.AllDirectories) .ToList() .ForEach(f => { File.Delete(f); }); } { // copy all plugins dlls into references path var dlls = GetDepDlls(plugin); var distDir = references["Bridge"].FullPath; dlls.ForEach(dll => { var fileName = Path.GetFileNameWithoutExtension(dll); File.Copy(dll, Path.Combine(distDir, fileName + ".dll2"), true); }); } }, // Post Process (processor) => { // caching binding scripts var dir = build["Temp"]["bindings"]; var bindingScripts = Directory.EnumerateFiles(dir, "*.js", SearchOption.AllDirectories); var cacheDir = build["BindingsCache"].FullPath; if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } foreach (var bs in bindingScripts) { var fileName = Path.GetFileName(bs); File.Copy(bs, Path.Combine(cacheDir, fileName), true); } // move files to plugin js output var tmpPath = build["Temp"].FullPath; var projDir = plugin.convertedPath.convertedJSPath; DirectoryUtil.CopyDirectory(tmpPath, projDir); DirectoryUtil.DeleteDirectory(tmpPath); // move dll to plugin dll output var projDllSrc = build["Output"][plugin.pluginName + ".dll"]; var projDllDist = plugin.convertedPath.convertedDLLPath; File.Copy(projDllSrc, projDllDist, true); Debug.Log("<" + plugin.pluginName + "> convertion finish."); } ); } else if (plugin.pluginState == UnityPlugin.PluginState.stub) { // generate cs stub if (plugin.stubConfig.generateStub) { plugin.stubPath = new UnityPlugin.StubPath(plugin); GenerateStubProc.GeneratePluginStub(plugin); Debug.Log("Generate Stub <" + plugin.pluginName + ">"); // 自动生成桩代码只需要一次 plugin.stubConfig.generateStub = false; } // generate js stub template if (plugin.stubConfig.generateJSTemplate) { ConfigureBridgeOptionsForStubPlugin(ref bridgeOptions, plugin); ProcessBridge(bridgeOptions, logger, // Pre Process (processor) => { // add plugin outside if (single != null) { processor.Translator.Plugins.AddPlugin(new BridgePlugin(single.pluginName + "-stub")); } else { processor.Translator.Plugins.AddPlugin(new BridgePlugin()); } // clean js output DirectoryUtil.DeleteDirectory(plugin.stubPath.stubJSPath); { // remove all dll2 var distDir = references["Bridge"].FullPath; Directory.EnumerateFiles(distDir, "*.dll2", SearchOption.AllDirectories) .ToList() .ForEach(f => { File.Delete(f); }); } { // copy unity stub dll var dll = UnityPluginUtil.GetUnityEngineStub().stubPath.stubDLLPath; var fileName = Path.GetFileNameWithoutExtension(dll); var distDir = references["Bridge"].FullPath; File.Copy(dll, Path.Combine(distDir, fileName + ".dll2"), true); } }, // Post Process (processor) => { // caching binding scripts var dir = build["Temp"]["bindings"]; var bindingScripts = Directory.EnumerateFiles(dir, "*.js", SearchOption.AllDirectories); var cacheDir = build["BindingsCache"].FullPath; if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } foreach (var bs in bindingScripts) { var fileName = Path.GetFileName(bs); File.Copy(bs, Path.Combine(cacheDir, fileName), true); } if (single != null) { single.stubConfig.generateJSTemplate = false; } // move files to plugin js output var tmpPath = build["Temp"].FullPath; var stubJSDir = plugin.stubPath.stubJSPath; DirectoryUtil.CopyDirectory(tmpPath, stubJSDir); DirectoryUtil.DeleteDirectory(tmpPath); Debug.Log("<" + plugin.pluginName + "> stub template convertion finish."); } ); } } EditorUtility.DisplayProgressBar("Exporting", "...", 0.0f); }); }
public static void GeneUnityStub(bool overwrite = true) { var unity = UnityPluginUtil.GetUnityEngineStub(); if (!overwrite && File.Exists(unity.stubPath.stubCSPath)) { Debug.LogWarning("UnityEngine Stub Exist."); return; } if (unity.stubConfig.generateStub) { // UnityStubBuilder.Build(unity.stubPath.stubCSPath); // 这里改用反射 // UnityStubBuilder.cs 需要被单独作为c#源文件放出,编DLL时不能耦合 // Type.GetType("WeChat.UnityStubBuilder").GetMethod("Build").Invoke(null, new object[] { unity.stubPath.stubCSPath }); // 这里改用回调,DLL里调用反射拿不到C#源文件里定义的类,有空再查 if (genUnityStubCallback != null) { genUnityStubCallback.Invoke(unity.stubPath.stubCSPath); } } // header var allRefContent = string.Format(topHeader, unity.pluginName); string content = ""; using (StreamReader sr = new StreamReader(unity.stubPath.stubCSPath)) { content = sr.ReadToEnd(); } if (content == null || content == "" || content.Length == 0) { return; } using (var sw = new StreamWriter(unity.stubPath.stubCSPath, false)) { sw.Write(allRefContent); sw.Write(content); } var sources = new List <string>() { unity.stubPath.stubCSPath }; var dir = new DirectoryInfo(Path.GetDirectoryName(unity.stubPath.stubDLLPath)); if (!dir.Exists) { dir.Create(); } var os = File.Create(unity.stubPath.stubDLLPath); if (references == null) { references = DirectoryBuilder.RegisterDirectory("references", new DirectoryStructure("References~")); } var refs = new List <string>() { references["Bridge"]["Bridge.dll"] }; EmitResult result; try { result = DLLProc.BuildDLL("UnityEngine-stub", os, sources, unity.defineMacros, refs, true); } finally { os.Close(); } if (result == null || !result.Success) { throw new Exception(); } Debug.Log("Generate Stub <UnityEngine>"); }