config.d 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. module system.config;
  2. import std.stdio;
  3. import std.file;
  4. import std.string;
  5. import std.range;
  6. import variables;
  7. import std.conv;
  8. import system.debugwriteln;
  9. nothrow string parseConf(string type, string filename) {
  10. try
  11. {
  12. auto file = File(filename);
  13. auto config = file.byLineCopy();
  14. static immutable typeMap = [
  15. "script": "SCRIPT=",
  16. "menu_script": "MENU_SCRIPT=",
  17. "title": "TITLE=",
  18. "icon": "ICON=",
  19. "dialog_end_indicator": "DIALOG_END_INDICATOR=",
  20. "dialog_box": "DIALOG_BOX=",
  21. "fallback_font": "FALLBACK_FONT=",
  22. "default_screen_width": "DEV_SCREEN_WIDTH=",
  23. "default_screen_height": "DEV_SCREEN_HEIGHT=",
  24. "screen_width": "SCREEN_WIDTH=",
  25. "screen_height": "SCREEN_HEIGHT=",
  26. "default_fullscreen": "DEFAULT_FULLSCREEN=",
  27. "render_texture_width": "RENDER_TEXTURE_WIDTH=",
  28. "render_texture_height": "RENDER_TEXTURE_HEIGHT=",
  29. "draw_message_ui": "DRAW_MESSAGE_UI=",
  30. "target_fps": "TARGET_FPS=",
  31. "sound_state": "SOUND_STATE=",
  32. "gamepad_state": "GAMEPAD_STATE=",
  33. ];
  34. if (type in typeMap)
  35. {
  36. auto prefix = typeMap[type];
  37. foreach (line; config)
  38. {
  39. auto trimmedLine = strip(line);
  40. if (trimmedLine.startsWith("//")) {
  41. continue;
  42. }
  43. if (trimmedLine.startsWith(prefix))
  44. {
  45. auto value = trimmedLine[prefix.length .. $].strip();
  46. debug debugWriteln("Value for ", type, ": ", value);
  47. return value;
  48. }
  49. }
  50. }
  51. }
  52. catch (Exception e)
  53. {
  54. debugWriteln(e.msg);
  55. }
  56. return "";
  57. }
  58. SystemSettings loadSettingsFromConfigFile(string confName) {
  59. return SystemSettings(
  60. parseConf("script", confName),
  61. parseConf("menu_script", confName),
  62. parseConf("title", confName),
  63. parseConf("icon", confName),
  64. parseConf("dialog_end_indicator", confName),
  65. parseConf("dialog_box", confName),
  66. parseConf("fallback_font", confName),
  67. parseConf("default_screen_width", confName).to!int,
  68. parseConf("default_screen_height", confName).to!int,
  69. parseConf("screen_width", confName).to!int,
  70. parseConf("screen_height", confName).to!int,
  71. parseConf("default_fullscreen", confName).to!bool,
  72. parseConf("render_texture_width", confName).to!int,
  73. parseConf("render_texture_height", confName).to!int,
  74. parseConf("draw_message_ui", confName).to!bool,
  75. parseConf("target_fps", confName).to!int,
  76. parseConf("sound_state", confName).to!bool,
  77. parseConf("gamepad_state", confName).to!bool,
  78. );
  79. }