_entry.js 6.9 KB

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