RiivolutionPatches.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2021 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <string>
  4. #include <string_view>
  5. #include <vector>
  6. #include <fmt/format.h>
  7. #include <jni.h>
  8. #include "Common/FileSearch.h"
  9. #include "Common/FileUtil.h"
  10. #include "Common/StringUtil.h"
  11. #include "DiscIO/RiivolutionParser.h"
  12. #include "jni/AndroidCommon/AndroidCommon.h"
  13. #include "jni/AndroidCommon/IDCache.h"
  14. static std::vector<DiscIO::Riivolution::Disc>* GetPointer(JNIEnv* env, jobject obj)
  15. {
  16. return reinterpret_cast<std::vector<DiscIO::Riivolution::Disc>*>(
  17. env->GetLongField(obj, IDCache::GetRiivolutionPatchesPointer()));
  18. }
  19. static std::vector<DiscIO::Riivolution::Disc>& GetReference(JNIEnv* env, jobject obj)
  20. {
  21. return *GetPointer(env, obj);
  22. }
  23. extern "C" {
  24. JNIEXPORT jlong JNICALL
  25. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_initialize(JNIEnv* env,
  26. jclass obj)
  27. {
  28. return reinterpret_cast<jlong>(new std::vector<DiscIO::Riivolution::Disc>);
  29. }
  30. JNIEXPORT void JNICALL
  31. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_finalize(JNIEnv* env,
  32. jobject obj)
  33. {
  34. delete GetPointer(env, obj);
  35. }
  36. JNIEXPORT jint JNICALL
  37. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getDiscCount(
  38. JNIEnv* env, jobject obj)
  39. {
  40. return GetReference(env, obj).size();
  41. }
  42. JNIEXPORT jstring JNICALL
  43. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getDiscName(
  44. JNIEnv* env, jobject obj, jint disc_index)
  45. {
  46. std::string filename, extension;
  47. SplitPath(GetReference(env, obj)[disc_index].m_xml_path, nullptr, &filename, &extension);
  48. return ToJString(env, filename + extension);
  49. }
  50. JNIEXPORT jint JNICALL
  51. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getSectionCount(
  52. JNIEnv* env, jobject obj, jint disc_index)
  53. {
  54. return GetReference(env, obj)[disc_index].m_sections.size();
  55. }
  56. JNIEXPORT jstring JNICALL
  57. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getSectionName(
  58. JNIEnv* env, jobject obj, jint disc_index, jint section_index)
  59. {
  60. return ToJString(env, GetReference(env, obj)[disc_index].m_sections[section_index].m_name);
  61. }
  62. JNIEXPORT jint JNICALL
  63. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getOptionCount(
  64. JNIEnv* env, jobject obj, jint disc_index, jint section_index)
  65. {
  66. return GetReference(env, obj)[disc_index].m_sections[section_index].m_options.size();
  67. }
  68. JNIEXPORT jstring JNICALL
  69. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getOptionName(
  70. JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index)
  71. {
  72. return ToJString(
  73. env,
  74. GetReference(env, obj)[disc_index].m_sections[section_index].m_options[option_index].m_name);
  75. }
  76. JNIEXPORT jint JNICALL
  77. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getChoiceCount(
  78. JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index)
  79. {
  80. return GetReference(env, obj)[disc_index]
  81. .m_sections[section_index]
  82. .m_options[option_index]
  83. .m_choices.size();
  84. }
  85. JNIEXPORT jstring JNICALL
  86. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getChoiceName(
  87. JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index,
  88. jint choice_index)
  89. {
  90. return ToJString(env, GetReference(env, obj)[disc_index]
  91. .m_sections[section_index]
  92. .m_options[option_index]
  93. .m_choices[choice_index]
  94. .m_name);
  95. }
  96. JNIEXPORT jint JNICALL
  97. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getSelectedChoice(
  98. JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index)
  99. {
  100. return GetReference(env, obj)[disc_index]
  101. .m_sections[section_index]
  102. .m_options[option_index]
  103. .m_selected_choice;
  104. }
  105. JNIEXPORT void JNICALL
  106. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_setSelectedChoiceImpl(
  107. JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index,
  108. jint choice_index)
  109. {
  110. GetReference(env, obj)[disc_index]
  111. .m_sections[section_index]
  112. .m_options[option_index]
  113. .m_selected_choice = choice_index;
  114. }
  115. static std::optional<DiscIO::Riivolution::Config> LoadConfigXML(const std::string& root_directory,
  116. std::string_view game_id)
  117. {
  118. // The way Riivolution stores settings only makes sense for standard game IDs.
  119. if (!(game_id.size() == 4 || game_id.size() == 6))
  120. return std::nullopt;
  121. return DiscIO::Riivolution::ParseConfigFile(
  122. fmt::format("{}/riivolution/config/{}.xml", root_directory, game_id.substr(0, 4)));
  123. }
  124. JNIEXPORT void JNICALL
  125. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_loadConfigImpl(
  126. JNIEnv* env, jobject obj, jstring j_game_id, jint revision, jint disc_number)
  127. {
  128. const std::string game_id = GetJString(env, j_game_id);
  129. auto& discs = GetReference(env, obj);
  130. const std::string& riivolution_dir = File::GetUserPath(D_RIIVOLUTION_IDX);
  131. const auto config = LoadConfigXML(riivolution_dir, game_id);
  132. discs.clear();
  133. for (const std::string& path : Common::DoFileSearch({riivolution_dir + "riivolution"}, {".xml"}))
  134. {
  135. auto parsed = DiscIO::Riivolution::ParseFile(path);
  136. if (!parsed || !parsed->IsValidForGame(game_id, revision, disc_number))
  137. continue;
  138. if (config)
  139. DiscIO::Riivolution::ApplyConfigDefaults(&*parsed, *config);
  140. discs.emplace_back(std::move(*parsed));
  141. }
  142. }
  143. JNIEXPORT void JNICALL
  144. Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_saveConfigImpl(
  145. JNIEnv* env, jobject obj, jstring j_game_id)
  146. {
  147. const std::string game_id = GetJString(env, j_game_id);
  148. if (!(game_id.size() == 4 || game_id.size() == 6))
  149. return;
  150. DiscIO::Riivolution::Config config;
  151. for (const auto& disc : GetReference(env, obj))
  152. {
  153. for (const auto& section : disc.m_sections)
  154. {
  155. for (const auto& option : section.m_options)
  156. {
  157. std::string id = option.m_id.empty() ? (section.m_name + option.m_name) : option.m_id;
  158. config.m_options.emplace_back(
  159. DiscIO::Riivolution::ConfigOption{std::move(id), option.m_selected_choice});
  160. }
  161. }
  162. }
  163. const std::string& root = File::GetUserPath(D_RIIVOLUTION_IDX);
  164. DiscIO::Riivolution::WriteConfigFile(
  165. fmt::format("{}/riivolution/config/{}.xml", root, game_id.substr(0, 4)), config);
  166. }
  167. }