TypeRegistry.cpp 3.8 KB

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