main.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 'use strict';
  2. const path = require('path');
  3. const electron = require('electron');
  4. const exec = require('child_process').exec;
  5. let fs = require("fs");
  6. let _lastUuid ;//最后打开的预制节点,记录当前打开层的uuid
  7. module.exports =
  8. {
  9. load () {
  10. // 上次加载事件未释放
  11. if(global._simpleCodeMain){
  12. this.unload.bind(global._simpleCodeMain)()
  13. }
  14. // 执行扩展逻辑
  15. this.initExtend();
  16. this.runExtendFunc("onLoad",this);
  17. try{
  18. this.changeConfig();
  19. } catch (exception) {
  20. Editor.error("配置插件config.js出错:,",exception);
  21. }
  22. global._simpleCodeMain = this;
  23. },
  24. // 2.4.4 发现保存后不会刷新
  25. unload () {
  26. delete global._simpleCodeMain
  27. this.scripts.forEach((obj)=>
  28. {
  29. for(let name in obj.messages)
  30. {
  31. let state = electron.ipcMain.removeListener( name.indexOf(':') == -1 ? "simple-code:"+name : name,obj.messages[name] ) ;
  32. }
  33. try {
  34. if(obj.onDestroy){
  35. obj.onDestroy()
  36. }
  37. } catch (error) {
  38. Editor.error(error);
  39. }
  40. })
  41. },
  42. changeConfig(){
  43. // let packageJson = JSON.parse( fs.readFileSync(Editor.url("packages://simple-code/package.json")) );
  44. // let cfg = eval( fs.readFileSync(Editor.url("packages://simple-code/config.js")).toString() );
  45. // let menuCfg = cfg["main-menu"]
  46. // let menuCfgOld = packageJson["main-menu"];
  47. // let isNeedSave = false;
  48. // for (let key in menuCfg)
  49. // {
  50. // let v = menuCfg[key];
  51. // if (menuCfgOld[key] == null || v.accelerator != menuCfgOld[key].accelerator || v.message != menuCfgOld[key].message)
  52. // {
  53. // isNeedSave = true;
  54. // break;
  55. // }
  56. // }
  57. // if (isNeedSave){
  58. // packageJson["main-menu"] = menuCfg;
  59. // Editor.log("替换编辑器插件快捷方式",Editor.url("packages://simple-code/package.json"),JSON.stringify( packageJson , null, "\t"))
  60. // fs.writeFile(Editor.url("packages://simple-code/package.json"),JSON.stringify( packageJson , null, "\t"), 'utf-8');
  61. // }
  62. },
  63. // 读取扩展逻辑文件
  64. initExtend()
  65. {
  66. const fe = Editor.require('packages://simple-code/tools/tools.js');
  67. this.scripts = [];
  68. let fileList = fe.getDirAllFiles(Editor.url("packages://simple-code/extensions"),[]);
  69. fileList.forEach((v)=>
  70. {
  71. if(v.substr(v.lastIndexOf(path.sep)+1) == "main_ex.js")
  72. {
  73. let obj = require(v);
  74. this.scripts.push(obj);
  75. for(let name in obj.messages)
  76. {
  77. obj.messages[name] = obj.messages[name].bind(obj)
  78. electron.ipcMain.on(name.indexOf(':') == -1 ? "simple-code:"+name : name,obj.messages[name]);
  79. }
  80. }
  81. })
  82. },
  83. // 运行扩展文件的方法
  84. runExtendFunc(funcName,...args){
  85. this.scripts.forEach((obj)=>{
  86. if (obj[funcName])
  87. {
  88. obj[funcName](...args);
  89. }
  90. })
  91. },
  92. // register your ipc messages here
  93. messages: {
  94. 'loadWidgetToCode'(){
  95. Editor.Ipc.sendToPanel('simple-code', 'loadWidgetToCode');
  96. },
  97. 'open' () {
  98. // open entry panel registered in package.json
  99. Editor.Panel.open('simple-code');
  100. },
  101. 'openPreview' () {
  102. // open entry panel registered in package.json
  103. Editor.Panel.open('simple-code.preview');
  104. },
  105. 'openNodeFileByOutside' () {
  106. // send ipc message to panel
  107. Editor.Scene.callSceneScript('simple-code', 'open-file-by-outside' ,"", (err, event)=>{
  108. } );
  109. },
  110. 'openNodeFile' () {
  111. // send ipc message to panel
  112. Editor.Panel.open('simple-code');
  113. Editor.Ipc.sendToPanel('simple-code', 'custom-cmd',{cmd:"openFile"});
  114. },
  115. 'findFileAndOpen' () {
  116. Editor.Panel.open('simple-code');
  117. Editor.Ipc.sendToPanel('simple-code', 'custom-cmd',{cmd:"findFileAndOpen"});
  118. },
  119. 'findFileGoto' () {
  120. Editor.Panel.open('simple-code');
  121. Editor.Ipc.sendToPanel('simple-code', 'custom-cmd',{cmd:"findFileGoto"});
  122. },
  123. 'simple-code:selectNode'(){
  124. Editor.Scene.callSceneScript('simple-code', 'select-node' ,{});
  125. },
  126. 'uuidToUrl'(event,a){
  127. if (event.reply) {
  128. //if no error, the first argument should be null
  129. if(a.uuids)
  130. {
  131. let arrUrl = []
  132. a.uuids.forEach((uuid,i)=>{
  133. arrUrl.push(Editor.assetdb.uuidToUrl(uuid))
  134. })
  135. event.reply(null, {urls:arrUrl});
  136. }
  137. }
  138. },
  139. 'getPrefabUuid'(event,a){
  140. if (event.reply) {
  141. event.reply(null, _lastUuid);
  142. }
  143. },
  144. 'setting'(){
  145. Editor.Panel.open('simple-code');
  146. Editor.Ipc.sendToPanel('simple-code', 'custom-cmd',{cmd:"setting"});
  147. },
  148. 'openConfig'(){
  149. // 打开配置
  150. const config = Editor.require('packages://simple-code/config.js');
  151. Editor.Ipc.sendToPanel('simple-code', 'open-code-file',config.getUserConfigPath(Editor.url('packages://simple-code/editor_config.js')));
  152. },
  153. 'openKeyMap'(){
  154. // 打开配置
  155. Editor.Ipc.sendToPanel('simple-code', 'open-code-file',Editor.url("packages://simple-code/keyMap.js"));
  156. },
  157. 'openConfigHitn'(){
  158. // 打开目录
  159. Editor.Ipc.sendToPanel('simple-code', 'open-code-file',Editor.url("packages://simple-code/template/hint_text.txt"));
  160. },
  161. 'openConfigExtendDir'(){
  162. // 打开目录
  163. exec( (Editor.isWin32 ? "start " : "open ")+Editor.url("packages://simple-code/extensions") )
  164. },
  165. // 联系作者
  166. 'contactAuthor'(){
  167. let url = 'https://qm.qq.com/cgi-bin/qm/qr?k=uha480KkJZa0P0rh_Pmrt8OkzQ6QIBqX&jump_from=webapi';
  168. exec(Editor.isWin32 ? "cmd /c start "+url : "open "+url);
  169. },
  170. 'scene:enter-prefab-edit-mode' (event,uuid) {
  171. _lastUuid = uuid;
  172. },
  173. 'refresh-preview'(){
  174. Editor.Ipc.sendToPanel('simple-code.preview','refresh-preview');
  175. },
  176. },
  177. };