config.d 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 char parseConf(string filename, string type)
  10. {
  11. try
  12. {
  13. auto file = File(filename);
  14. auto config = file.byLineCopy();
  15. // Create a mapping of types to their corresponding prefixes
  16. auto typeMap = [
  17. "sound": "SOUND:",
  18. "backward": "BACKWARD:",
  19. "forward": "FORWARD:",
  20. "right": "RIGHT:",
  21. "left": "LEFT:",
  22. "dialog": "DIALOG:",
  23. "opmenu": "OPMENU:"
  24. ];
  25. // Check if the provided type exists in the map
  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 button = trimmedLine[prefix.length .. $].strip();
  35. debug debugWriteln("Value for ", type, ": ", button);
  36. return button.front.to!char;
  37. }
  38. }
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. try
  44. {
  45. debugWriteln("Error parsing config: " ~ e.msg);
  46. }
  47. catch (Exception e)
  48. {
  49. }
  50. }
  51. return 'E';
  52. }
  53. SystemSettings loadSettingsFromConfigFile()
  54. {
  55. return SystemSettings(
  56. parseConf("conf/settings.conf", "right"),
  57. parseConf("conf/settings.conf", "left"),
  58. parseConf("conf/settings.conf", "backward"),
  59. parseConf("conf/settings.conf", "forward"),
  60. parseConf("conf/settings.conf", "dialog"),
  61. parseConf("conf/settings.conf", "opmenu"),
  62. parseConf("conf/settings.conf", "sound")
  63. );
  64. }