config.d 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 auto parseConf(string type)(string filename)
  10. {
  11. try
  12. {
  13. auto file = File(filename);
  14. auto config = file.byLineCopy();
  15. static immutable typeMap = [
  16. "sound": "SOUND:",
  17. "backward": "BACKWARD:",
  18. "forward": "FORWARD:",
  19. "right": "RIGHT:",
  20. "left": "LEFT:",
  21. "dialog": "DIALOG:",
  22. "opmenu": "OPMENU:",
  23. "script": "SCRIPT:"
  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. static if (type == "sound") {
  36. return value.to!int();
  37. } else static if (type == "script") {
  38. return value;
  39. } else {
  40. return value.front.to!char();
  41. }
  42. }
  43. }
  44. }
  45. }
  46. catch (Exception e)
  47. {
  48. debugWriteln(e.msg);
  49. }
  50. static if (type == "sound") {
  51. return 0;
  52. } else static if (type == "script") {
  53. return "";
  54. } else {
  55. return 'E';
  56. }
  57. }
  58. SystemSettings loadSettingsFromConfigFile()
  59. {
  60. return SystemSettings(
  61. parseConf!"sound"("conf/settings.conf"), // int
  62. parseConf!"right"("conf/settings.conf"), // char
  63. parseConf!"left"("conf/settings.conf"), // char
  64. parseConf!"backward"("conf/settings.conf"), // char
  65. parseConf!"forward"("conf/settings.conf"), // char
  66. parseConf!"dialog"("conf/settings.conf"), // char
  67. parseConf!"opmenu"("conf/settings.conf"), // char
  68. parseConf!"script"("conf/settings.conf") // string
  69. );
  70. }