config.d 1.8 KB

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