_entry_preview.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. global.__comx_global_exception_function = function(e, promise){
  2. if(promise) {
  3. console.log('unhandledRejection:', e, promise);
  4. }
  5. else {
  6. // if(e && e.message) {
  7. // console.log('uncaughtException:', e.message);
  8. // }
  9. // else {
  10. console.log('uncaughtException:', e);
  11. // }
  12. }
  13. };
  14. process.on('uncaughtException', (e)=>{
  15. __comx_global_exception_function(e);
  16. });
  17. process.on('unhandledRejection',function(err,promise){
  18. __comx_global_exception_function(err, promise);
  19. });
  20. var fs = require('fs');
  21. var vm = require('vm');
  22. const { execSync } = require('child_process');
  23. initRuntimeEnv();
  24. var dirHome = process.env['COMX_SDK'];
  25. var dirUnit = __dirname + '/';
  26. if(process.platform == 'win32')
  27. {
  28. require('bindings')('js_ext_comx_qtshell-native-win.node');
  29. require('bindings')('js_ext_comx_resource-native-win.node');
  30. require('bindings')('js_ext_comx_mutex-native-win.node');
  31. }
  32. if(process.platform == 'linux')
  33. {
  34. require('bindings')('js_ext_comx_qt-native-linux.node');
  35. require('bindings')('js_ext_comx_resource-native-linux.node');
  36. require('bindings')('js_ext_comx_mutex-native-linux.node');
  37. }
  38. if(process.platform == 'darwin')
  39. {
  40. require('bindings')('js_ext_comx_qt-native-darwin.node');
  41. require('bindings')('js_ext_comx_resource-native-darwin.node');
  42. require('bindings')('js_ext_comx_mutex-native-darwin.node');
  43. }
  44. var require_ = require('require-from-string');
  45. var __ = (_)=>{
  46. (new vm.Script(_, {filename : 'remote'})).runInNewContext({
  47. 'require' : require,
  48. 'process' : process,
  49. 'comx' : comx,
  50. 'exports' :exports,
  51. 'comx_startup' : ()=>{
  52. comx.resource.qt.loadUnit(dirUnit, exports, function(){
  53. if(process.argv.length >= 4)
  54. {
  55. var is_hide = !process.argv[3];
  56. if(is_hide)
  57. {
  58. unit.form[process.argv[2]].ShowEmbedded();
  59. }
  60. else
  61. unit.form[process.argv[2]].ShowPreview();
  62. }
  63. else
  64. {
  65. var is_hide = !process.argv[2];
  66. if(is_hide)
  67. {
  68. unit.form._entry.ShowEmbedded();
  69. }
  70. else
  71. unit.form._entry.ShowPreview();
  72. }
  73. }, (form)=>{
  74. //readyForm
  75. });
  76. }
  77. });
  78. };
  79. var tmpl = fs.readFileSync(dirHome+'template/4e7b90182d3b4e11a7ca74d8fc8c545f');
  80. comx.resource.Initialize(require_, __, tmpl);
  81. if(!comx.resource.hasResource)
  82. {
  83. if(process.argv.length == 4)
  84. {
  85. var tmpl = fs.readFileSync(dirHome+'template/008f111453704f82b9616e9bf92655f0');
  86. comx.resource.LoadKernelRemote(parseInt(process.argv[3]), false, tmpl);
  87. }
  88. else
  89. {
  90. var tmpl = fs.readFileSync(dirHome+'template/ad72fee60eea4a89844aa934b9bc099b');
  91. comx.resource.LoadKernelRemote(getWSAddress(), false, tmpl);
  92. }
  93. }
  94. else
  95. {
  96. comx.resource.qt.loadUnit(dirUnit, exports, function(){
  97. if(process.argv.length >= 4)
  98. {
  99. var is_hide = !process.argv[3];
  100. if(is_hide)
  101. {
  102. unit.form[process.argv[2]].ShowEmbedded();
  103. }
  104. else
  105. unit.form[process.argv[2]].ShowPreview();
  106. }
  107. else
  108. {
  109. var is_hide = !process.argv[2];
  110. if(is_hide)
  111. {
  112. unit.form._entry.ShowEmbedded();
  113. }
  114. else
  115. unit.form._entry.ShowPreview();
  116. }
  117. }, (form)=>{
  118. //readyForm
  119. });
  120. }
  121. /////////////////////////////////////////////////////////////////////////////
  122. // Init Runtime Env
  123. function initRuntimeEnv()
  124. {
  125. process.env.COMX_SDK = getHomeDir().replace(/\\/g, '/');
  126. if(process.platform == 'win32')
  127. {
  128. addPathToEnv(getHomeDir() + 'dependences\\windows\\occt\\7.5\\win64\\vc14\\bin');
  129. addPathToEnv(getHomeDir() + 'dependences\\windows\\vtk\\8.1\\bin');
  130. addPathToEnv(getHomeDir() + 'dependences\\windows\\vtk\\9.3\\bin');
  131. }
  132. var depsDir = getDepsDir();
  133. if(depsDir)
  134. {
  135. process.chdir(depsDir);
  136. }
  137. if(process.platform == 'linux')
  138. {
  139. const packages = ['occt-7.5.0', 'vtk-8.1', 'vtk-9.3'];
  140. const hardcodedPaths = ['/usr/lib/x86_64-linux-gnu/'];
  141. configureLdLibraryPath(packages, hardcodedPaths);
  142. }
  143. }
  144. function getParentDir(dirName)
  145. {
  146. var idx = dirName.lastIndexOf('/');
  147. if(idx == -1)
  148. {
  149. idx = dirName.lastIndexOf('\\');
  150. }
  151. return dirName.substr(0, idx);
  152. }
  153. function getHomeDir()
  154. {
  155. if(fs.existsSync(__dirname + '/07D976FB68304742A31191AA0503DE79'))
  156. {
  157. return __dirname + '/';
  158. }
  159. else
  160. {
  161. return getParentDir(getParentDir(__dirname)) + '/';
  162. }
  163. }
  164. function getDepsDir()
  165. {
  166. if(process.platform == 'win32')
  167. {
  168. return getHomeDir() + 'dependences\\windows\\qt\\5.15.2\\msvc2019_64\\bin\\';
  169. }
  170. if(process.platform == 'linux' || process.platform == 'darwin')
  171. {
  172. return getHomeDir() + 'addon';
  173. }
  174. return false;
  175. }
  176. function getWSAddress()
  177. {
  178. var fpath = getHomeDir() + '/cache/ws.cache';
  179. if(!fs.existsSync(fpath))
  180. {
  181. return "ws://localhost:3000";
  182. }
  183. else
  184. {
  185. return '' + fs.readFileSync(fpath);
  186. }
  187. }
  188. /**
  189. * Adds the specified PATH to the environment variable path.
  190. * @param {string} newPath - New path to add
  191. * @param {boolean} [prepend=true] - Whether to add the PATH to the beginning of the path (default true)
  192. * @returns {void}
  193. */
  194. function addPathToEnv(newPath, prepend = true) {
  195. if (!newPath || typeof newPath !== 'string') {
  196. throw new Error('Invalid path: Path must be a non-empty string.');
  197. }
  198. // Get the current PATH
  199. const currentPath = process.env.PATH || '';
  200. // Check whether the path already exists.
  201. if (currentPath.includes(newPath)) {
  202. console.warn(`Path "${newPath}" is already in PATH.`);
  203. return;
  204. }
  205. // Determine the path separator according to the operating system
  206. const separator = process.platform === 'win32' ? ';' : ':';
  207. // Add a new path
  208. process.env.PATH = prepend
  209. ? `${newPath}${separator}${currentPath}` // Add to the beginning
  210. : `${currentPath}${separator}${newPath}`; // Add to end
  211. //console.log(`Updated PATH: ${process.env.PATH}`);
  212. }
  213. /**
  214. * Extract the library path of pkg-config.
  215. * @param {string} pkgName - Package name of pkg-config (such as "occt-7.5.0")
  216. * @returns {string} - The extracted path
  217. */
  218. function extractLibraryPath(pkgName) {
  219. try {
  220. // Call pkg-config to extract the path.
  221. const command = `pkg-config --libs-only-L ${pkgName} | sed -n 's/^-L//p'`;
  222. const path = execSync(command, { encoding: 'utf-8' }).trim();
  223. return path;
  224. } catch (error) {
  225. console.error(`Error extracting path for ${pkgName}:`, error.message);
  226. return null;
  227. }
  228. }
  229. /**
  230. * Add path to LD_LIBRARY_PATH
  231. * @param {string} path - Path to add
  232. */
  233. function addPathToLdLibraryPath(path) {
  234. if (path && !process.env.LD_LIBRARY_PATH?.includes(path)) {
  235. process.env.LD_LIBRARY_PATH = `${path}:${process.env.LD_LIBRARY_PATH || ''}`;
  236. //console.log(`Added to LD_LIBRARY_PATH: ${path}`);
  237. } else {
  238. //console.log(`Path already in LD_LIBRARY_PATH or invalid: ${path}`);
  239. }
  240. }
  241. /**
  242. * Configure LD_LIBRARY_PATH
  243. * @param {string[]} packages - An array of package names that need to extract paths.
  244. * @param {string[]} hardcodedPaths - Hard-coded path array
  245. */
  246. function configureLdLibraryPath(packages, hardcodedPaths) {
  247. // Add a hard-coded path
  248. hardcodedPaths.forEach(path => {
  249. addPathToLdLibraryPath(path);
  250. });
  251. // Extract and add the package path
  252. packages.forEach(pkg => {
  253. const path = extractLibraryPath(pkg);
  254. if (path) {
  255. addPathToLdLibraryPath(path);
  256. }
  257. });
  258. //console.log('Updated LD_LIBRARY_PATH:', process.env.LD_LIBRARY_PATH);
  259. }