TypeRegistry.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "TypeRegistry.h"
  2. #include "BlockTypeGeneratorRule.h"
  3. #include "OverworldDimension.h"
  4. #include "TreeTemplate.h"
  5. TypeRegistry::TypeRegistry()
  6. : ReferenceCounter()
  7. {
  8. // register dimensions
  9. registerDimensionGeneratorFactory(new OverworldDimensionFactory());
  10. // register rules
  11. registerGeneratorRuleFactory(new BlockTypeGeneratorRuleFactory());
  12. // register templates
  13. registerGeneratorTemplate(new TreeTemplateFactory());
  14. }
  15. void TypeRegistry::registerGeneratorRuleFactory(GeneratorRuleFactory* factory)
  16. {
  17. generatorRules.add(factory);
  18. }
  19. GeneratorRule* TypeRegistry::createGeneratorRule(
  20. Framework::JSON::JSONValue* zConfig, JExpressionMemory* zMemory)
  21. {
  22. for (GeneratorRuleFactory* f : generatorRules)
  23. {
  24. Framework::JSON::Validator::JSONValidator* validator
  25. = f->getValidator();
  26. if (validator->isValid(zConfig))
  27. {
  28. validator->release();
  29. return f->createRule(zConfig, zMemory);
  30. }
  31. validator->release();
  32. }
  33. return 0;
  34. }
  35. Framework::JSON::Validator::JSONValidator*
  36. TypeRegistry::getGeneratorRuleValidator()
  37. {
  38. auto validator = Framework::JSON::Validator::JSONValidator::buildForOneOf()
  39. ->typeSpecifiedByAttribute("type");
  40. for (GeneratorRuleFactory* f : generatorRules)
  41. {
  42. validator->addAcceptedType(f->getValidator());
  43. }
  44. return validator->finishOneOf();
  45. }
  46. void TypeRegistry::registerDimensionGeneratorFactory(
  47. DimensionGeneratorFactory* factory)
  48. {
  49. dimensionGenerators.add(factory);
  50. }
  51. DimensionGenerator* TypeRegistry::createDimensionGenerator(
  52. Framework::JSON::JSONValue* zConfig, int worldSeed)
  53. {
  54. for (DimensionGeneratorFactory* f : dimensionGenerators)
  55. {
  56. Framework::JSON::Validator::JSONValidator* validator
  57. = f->getConfigValidator();
  58. if (validator->isValid(zConfig))
  59. {
  60. validator->release();
  61. std::cout << "Loaded Config for dimension '" << f->getName()
  62. << "' with id " << f->getDimensionId() << "\n";
  63. return f->createDimensionGenerator(worldSeed, zConfig);
  64. }
  65. validator->release();
  66. }
  67. return 0;
  68. }
  69. Framework::JSON::Validator::JSONValidator*
  70. TypeRegistry::getDimensionGeneratorValidator()
  71. {
  72. auto validator = Framework::JSON::Validator::JSONValidator::buildForOneOf()
  73. ->typeSpecifiedByAttribute("dimension");
  74. for (DimensionGeneratorFactory* f : dimensionGenerators)
  75. {
  76. validator->addAcceptedType(f->getConfigValidator());
  77. }
  78. return validator->finishOneOf();
  79. }
  80. const Framework::RCArray<DimensionGeneratorFactory>&
  81. TypeRegistry::getDimensionGeneratorFactories() const
  82. {
  83. return dimensionGenerators;
  84. }
  85. void TypeRegistry::registerGeneratorTemplate(GeneratorTemplateFactory* factory)
  86. {
  87. generatorTemplates.add(factory);
  88. }
  89. GeneratorTemplate* TypeRegistry::createGeneratorTemplate(
  90. Framework::JSON::JSONValue* zConfig)
  91. {
  92. for (GeneratorTemplateFactory* f : generatorTemplates)
  93. {
  94. Framework::JSON::Validator::JSONValidator* validator
  95. = f->getValidator();
  96. if (validator->isValid(zConfig))
  97. {
  98. validator->release();
  99. return f->createTemplate(zConfig);
  100. }
  101. validator->release();
  102. }
  103. return 0;
  104. }
  105. Framework::JSON::Validator::JSONValidator*
  106. TypeRegistry::getGeneratorTemplateValidator()
  107. {
  108. auto validator = Framework::JSON::Validator::JSONValidator::buildForOneOf()
  109. ->typeSpecifiedByAttribute("type");
  110. for (GeneratorTemplateFactory* f : generatorTemplates)
  111. {
  112. validator->addAcceptedType(f->getValidator());
  113. }
  114. return validator->finishOneOf();
  115. }