UiDynamicContentDatabase.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "UiDynamicContentDatabase.h"
  9. #include <ISystem.h>
  10. #include <AzCore/JSON/reader.h>
  11. #include <AzCore/JSON/error/en.h>
  12. #include <AzCore/IO/FileIO.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <AzFramework/Archive/IArchive.h>
  15. namespace LyShineExamples
  16. {
  17. UiDynamicContentDatabase::UiDynamicContentDatabase()
  18. {
  19. memset(m_documentParsed, 0, sizeof(m_documentParsed));
  20. UiDynamicContentDatabaseBus::Handler::BusConnect();
  21. }
  22. UiDynamicContentDatabase::~UiDynamicContentDatabase()
  23. {
  24. UiDynamicContentDatabaseBus::Handler::BusDisconnect();
  25. }
  26. int UiDynamicContentDatabase::GetNumColors(ColorType colorType)
  27. {
  28. if (!m_documentParsed[colorType])
  29. {
  30. return 0;
  31. }
  32. const rapidjson::Value& colors = m_document[colorType]["colors"];
  33. return colors.Size();
  34. }
  35. AZ::Color UiDynamicContentDatabase::GetColor(ColorType colorType, int index)
  36. {
  37. AZ::Color color(0.0f, 0.0f, 0.0f, 1.0f);
  38. if (!m_documentParsed[colorType])
  39. {
  40. return color;
  41. }
  42. if (index < GetNumColors(colorType))
  43. {
  44. const rapidjson::Value& jsonColors = m_document[colorType]["colors"];
  45. const rapidjson::Value& jsonColor = jsonColors[index]["color"];
  46. color.Set(jsonColor[0].GetInt() / 255.0f, jsonColor[1].GetInt() / 255.0f, jsonColor[2].GetInt() / 255.0f, 1.0f);
  47. }
  48. return color;
  49. }
  50. AZStd::string UiDynamicContentDatabase::GetColorName(ColorType colorType, int index)
  51. {
  52. AZStd::string colorName;
  53. if (!m_documentParsed[colorType])
  54. {
  55. return colorName;
  56. }
  57. if (index < GetNumColors(colorType))
  58. {
  59. const rapidjson::Value& colors = m_document[colorType]["colors"];
  60. const rapidjson::Value& name = colors[index]["name"];
  61. colorName = name.GetString();
  62. }
  63. return colorName;
  64. }
  65. AZStd::string UiDynamicContentDatabase::GetColorPrice(ColorType colorType, int index)
  66. {
  67. AZStd::string colorPrice;
  68. if (!m_documentParsed[colorType])
  69. {
  70. return "";
  71. }
  72. if (index < GetNumColors(colorType))
  73. {
  74. const rapidjson::Value& colors = m_document[colorType]["colors"];
  75. const rapidjson::Value& price = colors[index]["price"];
  76. colorPrice = price.GetString();
  77. }
  78. return colorPrice;
  79. }
  80. void UiDynamicContentDatabase::Refresh(ColorType colorType, const AZStd::string& filePath)
  81. {
  82. AZ::IO::HandleType readHandle = gEnv->pCryPak->FOpen(filePath.c_str(), "rt");
  83. if (readHandle == AZ::IO::InvalidHandle)
  84. {
  85. return;
  86. }
  87. size_t fileSize = gEnv->pCryPak->FGetSize(readHandle);
  88. if (fileSize > 0)
  89. {
  90. AZStd::string fileBuf;
  91. fileBuf.resize(fileSize);
  92. gEnv->pCryPak->FRead(fileBuf.data(), fileSize, readHandle);
  93. m_documentParsed[colorType] = false;
  94. rapidjson::ParseResult parseResult = m_document[colorType].Parse(fileBuf.data());
  95. if (!parseResult)
  96. {
  97. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  98. "Failed to parse content due to '%s' at offset %zd.\n",
  99. rapidjson::GetParseError_En(parseResult.Code()), parseResult.Offset());
  100. }
  101. else if (!m_document[colorType].IsObject())
  102. {
  103. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  104. "Expected an object at the root.");
  105. }
  106. else
  107. {
  108. m_documentParsed[colorType] = true;
  109. }
  110. }
  111. gEnv->pCryPak->FClose(readHandle);
  112. }
  113. void UiDynamicContentDatabase::Reflect(AZ::ReflectContext* context)
  114. {
  115. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  116. if (behaviorContext)
  117. {
  118. behaviorContext->Enum<(int)UiDynamicContentDatabaseInterface::ColorType::FreeColors>("eUiDynamicContentDBColorType_Free")
  119. ->Enum<(int)UiDynamicContentDatabaseInterface::ColorType::PaidColors>("eUiDynamicContentDBColorType_Paid");
  120. behaviorContext->EBus<UiDynamicContentDatabaseBus>("UiDynamicContentDatabaseBus")
  121. ->Event("GetNumColors", &UiDynamicContentDatabaseBus::Events::GetNumColors)
  122. ->Event("GetColor", &UiDynamicContentDatabaseBus::Events::GetColor)
  123. ->Event("GetColorName", &UiDynamicContentDatabaseBus::Events::GetColorName)
  124. ->Event("GetColorPrice", &UiDynamicContentDatabaseBus::Events::GetColorPrice)
  125. ->Event("Refresh", &UiDynamicContentDatabaseBus::Events::Refresh);
  126. }
  127. }
  128. } // namespace LYGame