JsonUtils.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "JsonUtils.h"
  2. #include <Console.h>
  3. #include <Datei.h>
  4. #include "Game.h"
  5. int countAllJsonsInDirectory(Framework::Text path)
  6. {
  7. int count = 0;
  8. if (path.hatAt(path.getLength() - 1, "/")
  9. || path.hatAt(path.getLength() - 1, "\\"))
  10. {
  11. path.remove(path.getLength() - 1, path.getLength());
  12. }
  13. Framework::Datei dir(path);
  14. if (dir.istOrdner())
  15. {
  16. Framework::RCArray<Framework::Text>* list = dir.getDateiListe();
  17. for (Framework::Text* name : *list)
  18. {
  19. Framework::Text nextPath = path + "/" + *name;
  20. count += countAllJsonsInDirectory(nextPath);
  21. }
  22. list->release();
  23. }
  24. else if (path.hatAt(path.getLength() - 5, ".json") && dir.existiert())
  25. {
  26. count++;
  27. }
  28. return count;
  29. }
  30. void internalLoadAllJsonsFromDirectory(Framework::Text path,
  31. std::function<void(
  32. Framework::JSON::JSONValue* zValue, Framework::Text path)> action,
  33. Framework::ConsoleProgressBar* progress)
  34. {
  35. if (path.hatAt(path.getLength() - 1, "/")
  36. || path.hatAt(path.getLength() - 1, "\\"))
  37. {
  38. path.remove(path.getLength() - 1, path.getLength());
  39. }
  40. Framework::Datei dir(path);
  41. if (dir.istOrdner())
  42. {
  43. Framework::RCArray<Framework::Text>* list = dir.getDateiListe();
  44. for (Framework::Text* name : *list)
  45. {
  46. Framework::Text nextPath = path + "/" + *name;
  47. internalLoadAllJsonsFromDirectory(nextPath, action, progress);
  48. }
  49. list->release();
  50. }
  51. else if (path.hatAt(path.getLength() - 5, ".json") && dir.existiert())
  52. {
  53. Framework::JSON::JSONValue* value
  54. = Framework::JSON::loadJSONFromFile(path);
  55. if (value)
  56. {
  57. action(value, path);
  58. value->release();
  59. }
  60. progress->setProgress(progress->getProgress() + 1);
  61. progress->triggerUpdate();
  62. }
  63. }
  64. void loadAllJsonsFromDirectory(Framework::Text path,
  65. std::function<void(
  66. Framework::JSON::JSONValue* zValue, Framework::Text path)> action)
  67. {
  68. Framework::ConsoleProgressBar* progressBar
  69. = new Framework::ConsoleProgressBar();
  70. Game::consoleHandler->addContent(
  71. progressBar, Framework::ConsoleContentPosition::Top);
  72. progressBar->triggerUpdate();
  73. progressBar->setMaxProgress(countAllJsonsInDirectory(path));
  74. progressBar->triggerUpdate();
  75. internalLoadAllJsonsFromDirectory(path, action, progressBar);
  76. progressBar->setMaxWidth(0);
  77. progressBar->triggerUpdate();
  78. Game::consoleHandler->removeContent(progressBar);
  79. }