launcher.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2012-2026 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #include "core/filesystem/path.h"
  6. #include "core/memory/allocator.h"
  7. #include "core/memory/globals.h"
  8. #include "core/os.h"
  9. #include "core/process.h"
  10. #include "core/strings/dynamic_string.inl"
  11. #include "core/strings/string_view.inl"
  12. #define STB_SPRINTF_IMPLEMENTATION
  13. #define STB_SPRINTF_NOUNALIGNED
  14. #define STB_SPRINTF_NOFLOAT
  15. #include <stb_sprintf.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #if CROWN_PLATFORM_WINDOWS
  20. # define WIN32_LEAN_AND_MEAN
  21. # include <windows.h>
  22. #elif CROWN_PLATFORM_LINUX
  23. # include <unistd.h> // readlink
  24. # include <errno.h>
  25. #endif
  26. using namespace crown;
  27. int main_internal(int argc, char **argv)
  28. {
  29. CE_UNUSED(argc);
  30. char launcher_path[4*4096];
  31. StringView launcher_dir;
  32. StringView editor_dir;
  33. DynamicString editor_cwd(default_allocator());
  34. DynamicString editor_exe(default_allocator());
  35. #if CROWN_PLATFORM_LINUX
  36. ssize_t len = readlink("/proc/self/exe", launcher_path, sizeof(launcher_path) - 1);
  37. if (len == -1) {
  38. printf("readlink: errno %d\n", errno);
  39. return EXIT_FAILURE;
  40. }
  41. launcher_path[len] = '\0';
  42. editor_dir = StringView("platforms/linux64/bin");
  43. os::setenv("UBUNTU_MENUPROXY", "");
  44. #elif CROWN_PLATFORM_WINDOWS
  45. wchar_t buf[MAX_PATH];
  46. DWORD buf_size = (DWORD)sizeof(buf);
  47. DWORD ret = GetModuleFileNameW(NULL, buf, buf_size);
  48. if (ret == 0 || ret >= buf_size) {
  49. printf("GetModuleFileNameW: error\n");
  50. return EXIT_FAILURE;
  51. }
  52. if (WideCharToMultiByte(CP_UTF8, 0, buf, -1, launcher_path, (int)sizeof(launcher_path), NULL, NULL) == 0) {
  53. printf("WideCharToMultiByte: error\n");
  54. return EXIT_FAILURE;
  55. }
  56. editor_dir = StringView("platforms\\windows64\\bin");
  57. #else
  58. #error "Unsupported platform."
  59. #endif
  60. launcher_dir = path::parent_dir(launcher_path);
  61. path::join(editor_cwd, launcher_dir, editor_dir);
  62. path::join(editor_exe, editor_cwd.c_str(), "crown-editor-release" EXE_SUFFIX);
  63. os::setcwd(editor_cwd.c_str());
  64. Process pr;
  65. argv[0] = (char *)editor_exe.c_str();
  66. if (pr.spawn(argv, CROWN_PROCESS_STDOUT_PIPE | CROWN_PROCESS_STDERR_MERGE) != 0) {
  67. printf("Cannot spawn %s\n", argv[0]);
  68. return EXIT_FAILURE;
  69. }
  70. return pr.wait();
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. CE_UNUSED(argc);
  75. memory_globals::init();
  76. int exit_code = main_internal(argc, argv);
  77. memory_globals::shutdown();
  78. return exit_code;
  79. }