device_options.cpp 4.8 KB

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