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 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 void GeneratePluginRefStubCs(UnityPlugin plugin) { if (!plugin.enable) { return; } var refs = UnityPluginUtil.GetLib(plugin); if (refs == null || refs.Count == 0) { Debug.Log(plugin.pluginName + ": References C# generating... no references found"); return; } // header var allRefContent = string.Format(topHeader, plugin.pluginName); for (var i = 0; i < refs.Count; i++) { var r = refs[i]; var refName = Path.GetFileNameWithoutExtension(r); Debug.Log(refName); var assembly = Assembly.LoadFile(r); var output = plugin.stubPath.stubRefCSPath; GeneratePluginRefStubCs(plugin.pluginName, output, assembly); var tmp = ""; using (var sr = new StreamReader(output)) { tmp = sr.ReadToEnd(); } // remove #if UNITY_WAGAME // using Bridge; // using System; // at the begining of new ref stub content if (i > 0) { tmp = String.Join("\n", tmp.Split('\n').Skip(4)); } // remove #endif at last line tmp = tmp.Remove(tmp.TrimEnd().LastIndexOf(Environment.NewLine)); // ref header allRefContent += string.Format(refHeader, i, refName); allRefContent += tmp; allRefContent += "\n\n"; } // add #endif at the very last line allRefContent += "#endif"; using (var sw = new StreamWriter(plugin.stubPath.stubRefCSPath, false)) { sw.Write(allRefContent); } }
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 ConfigureBridgeOptionsForConvertPlugin(ref BridgeOptions bridgeOptions, UnityPlugin config) { bridgeOptions.ProjectProperties = new ProjectProperties(); bridgeOptions.BridgeLocation = references["Bridge"]["Bridge.dll"]; bridgeOptions.Lib = build["Output"][config.pluginName + ".dll"]; bridgeOptions.ReferencesPath = references["Bridge"].FullPath; bridgeOptions.ProjectProperties.Configuration = configs["Text"]["bridge.json"].PathToAssets(); bridgeOptions.Folder = Path.GetFullPath("."); bridgeOptions.Sources = UnityPluginUtil.GetSource(config).FilesListToLine(); bridgeOptions.ExcludeSubFolders = UnityPluginUtil.GetExclude(config).FilesListToLine(); bridgeOptions.Rebuild = true; bridgeOptions.Recursive = true; bridgeOptions.Name = config.pluginName; bridgeOptions.OutputLocation = bridgeOptions.Folder; bridgeOptions.DefaultFileName = Path.GetFileNameWithoutExtension(bridgeOptions.Lib); bridgeOptions.ProjectProperties.AssemblyName = bridgeOptions.DefaultFileName; bridgeOptions.ProjectProperties.DefineConstants = config.defineMacros.Join(";"); }
private static void GeneratePluginDLL(UnityPlugin config) { if (!config.enable) { return; } var sources = UnityPluginUtil.GetSource(config); var exc = UnityPluginUtil.GetExclude(config); sources = sources.Except(exc).ToList(); if (sources == null || sources.Count == 0) { return; } var refs = UnityPluginUtil.GetLib(config, true, true); if (refs == null || refs.Count == 0) { return; } // rename dist dll var dist = config.stubPath.stubDLLPath.Replace("-stub.dll", "-internal.dll"); var os = File.Create(dist); EmitResult result; try { // comment this for TMP issue(internal visibility) result = DLLProc.BuildDLL(config.pluginName /* + "-internal" */, os, sources, config.defineMacros, refs, true); } finally { os.Close(); } if (result == null || !result.Success) { throw new Exception(); } }
private static Compilation BuildDLL(ProjectExportConfig config) { // var sources = config.project.AllSources().ToList(); // var references = config.project.AllReferences().ToList(); var sources = ProjectExportUtil.GetProjectSourcesWithoutExcludes(config); var excludes = new List <string>(); excludes.Add("**/*~/**/*.dll"); excludes.Add("**/[Ee]ditor/**/*.dll"); excludes.Add("**/script-export/**/*.dll"); var references = ProjectExportUtil.GetProjectLibs(config, excludes); references.AddRange(UnityPluginUtil.GetUnityEngineLibs()); // references.AddRange(DLLProc.SystemDLLPath()); // references.AddRange(DLLProc.UnityEngineDLLPath()); // references.AddRange(DLLProc.SystemDLLPath()); Debug.Log("Building Assembly for sources[" + sources.Count + "], references[" + references.Count + "]"); return(DLLProc.BuildDLL("WAGameUnityProject", sources, config.defineMacros, references, true)); }
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>"); }
private void OnGUI() { if (s_Styles == null) { s_Styles = new Styles(); } m_ListView.totalRows = m_MsgList.Count; Event current = Event.current; EditorGUILayout.BeginVertical(); GUIContent textContent = new GUIContent(); var list = ListViewGUI.ListView(m_ListView, s_Styles.listBackgroundStyle).GetEnumerator(); while (list.MoveNext()) { var el = (ListViewElement)list.Current; var idx = el.row; var path = m_MsgList[idx]; var name = ""; bool template = false; if (path == "空白模板") { template = true; } else if (Application.platform == RuntimePlatform.WindowsEditor) { name = path.Substring(path.LastIndexOf("\\") + 1); } else { name = path.Substring(path.LastIndexOf("/") + 1); } if (current.type == EventType.MouseDown && current.button == 0 && el.position.Contains(current.mousePosition) && current.clickCount == 2) { if (addedPlugin.Contains(path)) { Debug.Log("已被添加"); } else { UnityPlugin p; if (template) { Debug.Log("创建空白插件配置"); // create random name var time = DateTime.Now.ToFileTimeUtc().ToString(); p = UnityPluginUtil.CreatePlugin(null, "Template_" + time); } else { addedPlugin.Add(path); Debug.Log("第" + idx + "项插件: " + name); var pluginRoot = AssetDatabase.LoadMainAssetAtPath(path.PathToAssets()); p = UnityPluginUtil.CreatePlugin(pluginRoot, name); } Selection.activeObject = p; onAddCallback(p); } } if (current.type == EventType.Repaint) { textContent.text = GetRowText(el); // 交替显示不同背景色 GUIStyle style; if (addedPlugin.Contains(path)) { style = s_Styles.listItemBackgroundFinish; style.Draw(el.position, false, false, true, false); s_Styles.listItem.Draw(el.position, textContent, false, false, true, m_Focus); } else { style = (el.row % 2 != 0) ? s_Styles.listItemBackground2 : s_Styles.listItemBackground; style.Draw(el.position, false, false, m_ListView.row == el.row, false); s_Styles.listItem.Draw(el.position, textContent, false, false, m_ListView.row == el.row, m_Focus); } } } EditorGUILayout.EndVertical(); }