device_options.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "core/command_line.h"
  7. #include "core/filesystem/path.h"
  8. #include "core/strings/dynamic_string.inl"
  9. #include "device/device_options.h"
  10. #include <stdlib.h>
  11. namespace crown
  12. {
  13. static void help(const char* msg = NULL)
  14. {
  15. printf(
  16. "The Flexible Game Engine\n"
  17. "Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.\n"
  18. "License: https://github.com/dbartolini/crown/blob/master/LICENSE\n"
  19. "\n"
  20. "Usage:\n"
  21. " crown [options]\n"
  22. "\n"
  23. "Options:\n"
  24. " -h --help Display this help.\n"
  25. " -v --version Display engine version.\n"
  26. " --source-dir <path> Specify the <path> of the project's source data.\n"
  27. " --data-dir <path> Specify the <path> where to put the compiled data.\n"
  28. " --map-source-dir <name> <path> Mount <path>/<name> at <source-dir>/<name>.\n"
  29. " --boot-dir <prefix> Use <prefix>/boot.config to boot the engine.\n"
  30. " --compile Compile the project's source data.\n"
  31. " --platform <platform> Specify the target <platform> for data compilation.\n"
  32. " linux\n"
  33. " windows\n"
  34. " android\n"
  35. " --continue Run the engine after the data has been compiled.\n"
  36. " --console-port <port> Set port of the console server.\n"
  37. " --wait-console Wait for a console connection before booting the engine.\n"
  38. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  39. " --server Run the engine in server mode.\n"
  40. "\n"
  41. "Complete documentation available at https://dbartolini.github.io/crown/html/v" CROWN_VERSION "\n"
  42. );
  43. if (msg)
  44. printf("Error: %s\n", msg);
  45. }
  46. DeviceOptions::DeviceOptions(Allocator& a, int argc, const char** argv)
  47. : _argc(argc)
  48. , _argv(argv)
  49. , _source_dir(a)
  50. , _map_source_dir_name(NULL)
  51. , _map_source_dir_prefix(a)
  52. , _data_dir(a)
  53. , _boot_dir(NULL)
  54. , _platform(NULL)
  55. , _lua_string(a)
  56. , _wait_console(false)
  57. , _do_compile(false)
  58. , _do_continue(false)
  59. , _server(false)
  60. , _parent_window(0)
  61. , _console_port(CROWN_DEFAULT_CONSOLE_PORT)
  62. , _window_x(0)
  63. , _window_y(0)
  64. , _window_width(CROWN_DEFAULT_WINDOW_WIDTH)
  65. , _window_height(CROWN_DEFAULT_WINDOW_HEIGHT)
  66. {
  67. }
  68. DeviceOptions::~DeviceOptions()
  69. {
  70. }
  71. int DeviceOptions::parse(bool* quit)
  72. {
  73. CommandLine cl(_argc, _argv);
  74. if (cl.has_option("help", 'h'))
  75. {
  76. help();
  77. *quit = true;
  78. return EXIT_SUCCESS;
  79. }
  80. if (cl.has_option("version", 'v'))
  81. {
  82. printf("Crown " CROWN_VERSION "\n");
  83. *quit = true;
  84. return EXIT_SUCCESS;
  85. }
  86. path::reduce(_source_dir, cl.get_parameter(0, "source-dir"));
  87. path::reduce(_data_dir, cl.get_parameter(0, "data-dir"));
  88. _map_source_dir_name = cl.get_parameter(0, "map-source-dir");
  89. if (_map_source_dir_name)
  90. {
  91. path::reduce(_map_source_dir_prefix, cl.get_parameter(1, "map-source-dir"));
  92. if (_map_source_dir_prefix.empty())
  93. {
  94. help("Mapped source directory must be specified.");
  95. return EXIT_FAILURE;
  96. }
  97. }
  98. _do_compile = cl.has_option("compile");
  99. if (_do_compile)
  100. {
  101. _platform = cl.get_parameter(0, "platform");
  102. // Compile for platform the executable is built for.
  103. if (_platform == NULL)
  104. _platform = CROWN_PLATFORM_NAME;
  105. if (true
  106. && strcmp(_platform, "android") != 0
  107. && strcmp(_platform, "linux") != 0
  108. && strcmp(_platform, "windows") != 0
  109. )
  110. {
  111. help("Cannot compile for the given platform.");
  112. return EXIT_FAILURE;
  113. }
  114. if (_source_dir.empty())
  115. {
  116. help("Source dir must be specified.");
  117. return EXIT_FAILURE;
  118. }
  119. if (_data_dir.empty())
  120. {
  121. _data_dir += _source_dir;
  122. _data_dir += '_';
  123. _data_dir += _platform;
  124. }
  125. }
  126. _server = cl.has_option("server");
  127. if (_server)
  128. {
  129. if (_source_dir.empty())
  130. {
  131. help("Source dir must be specified.");
  132. return EXIT_FAILURE;
  133. }
  134. }
  135. if (!_data_dir.empty())
  136. {
  137. if (!path::is_absolute(_data_dir.c_str()))
  138. {
  139. help("Data dir must be absolute.");
  140. return EXIT_FAILURE;
  141. }
  142. }
  143. if (!_source_dir.empty())
  144. {
  145. if (!path::is_absolute(_source_dir.c_str()))
  146. {
  147. help("Source dir must be absolute.");
  148. return EXIT_FAILURE;
  149. }
  150. }
  151. if (!_map_source_dir_prefix.empty())
  152. {
  153. if (!path::is_absolute(_map_source_dir_prefix.c_str()))
  154. {
  155. help("Mapped source dir must be absolute.");
  156. return EXIT_FAILURE;
  157. }
  158. }
  159. _do_continue = cl.has_option("continue");
  160. if (_do_continue)
  161. {
  162. if (strcmp(_platform, CROWN_PLATFORM_NAME) != 0)
  163. {
  164. help("Host platform can not run from the compiled data. Consider removing --continue option.");
  165. return EXIT_FAILURE;
  166. }
  167. }
  168. _boot_dir = cl.get_parameter(0, "boot-dir");
  169. if (_boot_dir)
  170. {
  171. if (!path::is_relative(_boot_dir))
  172. {
  173. help("Boot dir must be relative.");
  174. return EXIT_FAILURE;
  175. }
  176. }
  177. _wait_console = cl.has_option("wait-console");
  178. const char* parent = cl.get_parameter(0, "parent-window");
  179. if (parent)
  180. {
  181. if (sscanf(parent, "%u", &_parent_window) != 1)
  182. {
  183. help("Parent window is invalid.");
  184. return EXIT_FAILURE;
  185. }
  186. }
  187. const char* port = cl.get_parameter(0, "console-port");
  188. if (port)
  189. {
  190. if (sscanf(port, "%hu", &_console_port) != 1)
  191. {
  192. help("Console port is invalid.");
  193. return EXIT_FAILURE;
  194. }
  195. }
  196. const char* ls = cl.get_parameter(0, "lua-string");
  197. if (ls)
  198. _lua_string = ls;
  199. return EXIT_SUCCESS;
  200. }
  201. } // namespace crown