config.d 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.abstraction;
  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. "title": "TITLE:",
  17. "icon": "ICON:",
  18. "dialog_end_indicator": "DIALOG_END_INDICATOR:",
  19. "dialog_box": "DIALOG_BOX:",
  20. "choice_box": "CHOICE_BOX:",
  21. "fallback_font": "FALLBACK_FONT:",
  22. "default_screen_width": "DEFAULT_SCREEN_WIDTH:",
  23. "default_screen_height": "DEFAULT_SCREEN_HEIGHT:",
  24. "default_fullscreen": "DEFAULT_FULLSCREEN:"
  25. ];
  26. if (type in typeMap)
  27. {
  28. auto prefix = typeMap[type];
  29. foreach (line; config)
  30. {
  31. auto trimmedLine = strip(line);
  32. if (trimmedLine.startsWith(prefix))
  33. {
  34. auto value = trimmedLine[prefix.length .. $].strip();
  35. debug debugWriteln("Value for ", type, ": ", value);
  36. return value;
  37. }
  38. }
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. debugWriteln(e.msg);
  44. }
  45. return "";
  46. }
  47. SystemSettings loadSettingsFromConfigFile(string confName) {
  48. return SystemSettings(
  49. parseConf("script", confName),
  50. parseConf("title", confName),
  51. parseConf("icon", confName),
  52. parseConf("dialog_end_indicator", confName),
  53. parseConf("dialog_box", confName),
  54. parseConf("choice_box", confName),
  55. parseConf("fallback_font", confName),
  56. parseConf("default_screen_width", confName).to!int,
  57. parseConf("default_screen_height", confName).to!int,
  58. parseConf("default_fullscreen", confName).to!bool
  59. );
  60. }