@@ -0,0 +1,95 @@
// SPDX-License-Identifier: GPL-2.0-or-later

package org.dolphinemu.dolphinemu.features.riivolution.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import org.dolphinemu.dolphinemu.features.riivolution.model.RiivolutionPatches;
import org.dolphinemu.dolphinemu.ui.DividerItemDecoration;
import org.dolphinemu.dolphinemu.utils.DirectoryInitialization;

public class RiivolutionBootActivity extends AppCompatActivity
{
private static final String ARG_GAME_PATH = "game_path";
private static final String ARG_GAME_ID = "game_id";
private static final String ARG_REVISION = "revision";
private static final String ARG_DISC_NUMBER = "disc_number";

private RiivolutionPatches mPatches;

public static void launch(Context context, String gamePath, String gameId, int revision,
int discNumber)
{
Intent launcher = new Intent(context, RiivolutionBootActivity.class);
launcher.putExtra(ARG_GAME_PATH, gamePath);
launcher.putExtra(ARG_GAME_ID, gameId);
launcher.putExtra(ARG_REVISION, revision);
launcher.putExtra(ARG_DISC_NUMBER, discNumber);
context.startActivity(launcher);
}

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_riivolution_boot);

Intent intent = getIntent();

String path = getIntent().getStringExtra(ARG_GAME_PATH);
String gameId = intent.getStringExtra(ARG_GAME_ID);
int revision = intent.getIntExtra(ARG_REVISION, -1);
int discNumber = intent.getIntExtra(ARG_DISC_NUMBER, -1);

TextView textSdRoot = findViewById(R.id.text_sd_root);
String riivolutionPath = DirectoryInitialization.getUserDirectory() + "/Load/Riivolution";
textSdRoot.setText(getString(R.string.riivolution_sd_root, riivolutionPath));

Button buttonStart = findViewById(R.id.button_start);
buttonStart.setOnClickListener((v) ->
{
if (mPatches != null)
mPatches.saveConfig();

EmulationActivity.launch(this, path, true);
});

new Thread(() ->
{
RiivolutionPatches patches = new RiivolutionPatches(gameId, revision, discNumber);
patches.loadConfig();
runOnUiThread(() -> populateList(patches));
}).start();
}

@Override
protected void onStop()
{
super.onStop();

if (mPatches != null)
mPatches.saveConfig();
}

private void populateList(RiivolutionPatches patches)
{
mPatches = patches;

RecyclerView recyclerView = findViewById(R.id.recycler_view);

recyclerView.setAdapter(new RiivolutionAdapter(this, patches));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-2.0-or-later

package org.dolphinemu.dolphinemu.features.riivolution.ui;

public class RiivolutionItem
{
public final int mDiscIndex;
public final int mSectionIndex;
public final int mOptionIndex;

/**
* Constructor for a disc.
*/
public RiivolutionItem(int discIndex)
{
mDiscIndex = discIndex;
mSectionIndex = -1;
mOptionIndex = -1;
}

/**
* Constructor for a section.
*/
public RiivolutionItem(int discIndex, int sectionIndex)
{
mDiscIndex = discIndex;
mSectionIndex = sectionIndex;
mOptionIndex = -1;
}

/**
* Constructor for an option.
*/
public RiivolutionItem(int discIndex, int sectionIndex, int optionIndex)
{
mDiscIndex = discIndex;
mSectionIndex = sectionIndex;
mOptionIndex = optionIndex;
}
}
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: GPL-2.0-or-later

package org.dolphinemu.dolphinemu.features.riivolution.ui;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.features.riivolution.model.RiivolutionPatches;

public class RiivolutionViewHolder extends RecyclerView.ViewHolder
implements AdapterView.OnItemSelectedListener
{
public static final int TYPE_HEADER = 0;
public static final int TYPE_OPTION = 1;

private final TextView mTextView;
private final Spinner mSpinner;

private RiivolutionPatches mPatches;
private RiivolutionItem mItem;

public RiivolutionViewHolder(@NonNull View itemView)
{
super(itemView);

mTextView = itemView.findViewById(R.id.text_name);
mSpinner = itemView.findViewById(R.id.spinner_choice);
}

public void bind(Context context, RiivolutionPatches patches, RiivolutionItem item)
{
String text;
if (item.mOptionIndex != -1)
text = patches.getOptionName(item.mDiscIndex, item.mSectionIndex, item.mOptionIndex);
else if (item.mSectionIndex != -1)
text = patches.getSectionName(item.mDiscIndex, item.mSectionIndex);
else
text = patches.getDiscName(item.mDiscIndex);
mTextView.setText(text);

if (item.mOptionIndex != -1)
{
mPatches = patches;
mItem = item;

ArrayAdapter<String> adapter = new ArrayAdapter<>(context,
R.layout.list_item_riivolution_header);

int choiceCount = patches.getChoiceCount(mItem.mDiscIndex, mItem.mSectionIndex,
mItem.mOptionIndex);
adapter.add(context.getString(R.string.riivolution_disabled));
for (int i = 0; i < choiceCount; i++)
{
adapter.add(patches.getChoiceName(mItem.mDiscIndex, mItem.mSectionIndex, mItem.mOptionIndex,
i));
}

mSpinner.setAdapter(adapter);
mSpinner.setSelection(patches.getSelectedChoice(mItem.mDiscIndex, mItem.mSectionIndex,
mItem.mOptionIndex));
mSpinner.setOnItemSelectedListener(this);
}
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
mPatches.setSelectedChoice(mItem.mDiscIndex, mItem.mSectionIndex, mItem.mOptionIndex, position);
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{
}
}
Expand Up @@ -29,19 +29,22 @@
public final class EmulationFragment extends Fragment implements SurfaceHolder.Callback
{
private static final String KEY_GAMEPATHS = "gamepaths";
private static final String KEY_RIIVOLUTION = "riivolution";

private InputOverlay mInputOverlay;

private String[] mGamePaths;
private boolean mRiivolution;
private boolean mRunWhenSurfaceIsValid;
private boolean mLoadPreviousTemporaryState;

private EmulationActivity activity;

public static EmulationFragment newInstance(String[] gamePaths)
public static EmulationFragment newInstance(String[] gamePaths, boolean riivolution)
{
Bundle args = new Bundle();
args.putStringArray(KEY_GAMEPATHS, gamePaths);
args.putBoolean(KEY_RIIVOLUTION, riivolution);

EmulationFragment fragment = new EmulationFragment();
fragment.setArguments(args);
Expand Down Expand Up @@ -76,6 +79,7 @@ public void onCreate(Bundle savedInstanceState)
setRetainInstance(true);

mGamePaths = getArguments().getStringArray(KEY_GAMEPATHS);
mRiivolution = getArguments().getBoolean(KEY_RIIVOLUTION);
}

/**
Expand Down Expand Up @@ -267,12 +271,12 @@ private void runWithValidSurface()
if (mLoadPreviousTemporaryState)
{
Log.debug("[EmulationFragment] Starting emulation thread from previous state.");
NativeLibrary.Run(mGamePaths, getTemporaryStateFilePath(), true);
NativeLibrary.Run(mGamePaths, mRiivolution, getTemporaryStateFilePath(), true);
}
else
{
Log.debug("[EmulationFragment] Starting emulation thread.");
NativeLibrary.Run(mGamePaths);
NativeLibrary.Run(mGamePaths, mRiivolution);
}
EmulationActivity.stopIgnoringLaunchRequests();
}, "NativeEmulation");
Expand Down
Expand Up @@ -216,7 +216,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent result)
case MainPresenter.REQUEST_GAME_FILE:
FileBrowserHelper.runAfterExtensionCheck(this, uri,
FileBrowserHelper.GAME_LIKE_EXTENSIONS,
() -> EmulationActivity.launch(this, result.getData().toString()));
() -> EmulationActivity.launch(this, result.getData().toString(), false));
break;

case MainPresenter.REQUEST_WAD_FILE:
Expand Down
Expand Up @@ -153,7 +153,7 @@ void setupUI()

// Start the emulation activity and send the path of the clicked ISO to it.
String[] paths = GameFileCacheService.findSecondDiscAndGetPaths(holder.gameFile);
EmulationActivity.launch(TvMainActivity.this, paths);
EmulationActivity.launch(TvMainActivity.this, paths, false);
}
});
}
Expand Down Expand Up @@ -255,7 +255,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent result)
case MainPresenter.REQUEST_GAME_FILE:
FileBrowserHelper.runAfterExtensionCheck(this, uri,
FileBrowserHelper.GAME_LIKE_EXTENSIONS,
() -> EmulationActivity.launch(this, result.getData().toString()));
() -> EmulationActivity.launch(this, result.getData().toString(), false));
break;

case MainPresenter.REQUEST_WAD_FILE:
Expand Down
Expand Up @@ -51,7 +51,7 @@ public static void HandleInit(FragmentActivity parent)
if (start_files != null && start_files.length > 0)
{
// Start the emulation activity, send the ISO passed in and finish the main activity
EmulationActivity.launch(parent, start_files);
EmulationActivity.launch(parent, start_files, false);
parent.finish();
}
}
Expand Down
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/text_sd_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
tools:text="@string/riivolution_sd_root"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/divider" />

<View
android:id="@+id/divider"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#1F000000"
android:layout_marginHorizontal="@dimen/spacing_large"
android:layout_marginVertical="@dimen/spacing_small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/text_sd_root"
app:layout_constraintBottom_toTopOf="@id/scroll_view" />

<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/divider"
app:layout_constraintBottom_toTopOf="@id/button_start">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp" />

</ScrollView>

<Button
android:id="@+id/button_start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_large"
android:text="@string/riivolution_start"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/scroll_view"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_name"
tools:text="Example Section"
android:paddingHorizontal="@dimen/spacing_large"
android:paddingVertical="@dimen/spacing_medlarge" />
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/text_name"
tools:text="Example Option"
android:layout_marginHorizontal="@dimen/spacing_large"
android:layout_marginVertical="@dimen/spacing_medlarge"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/spinner_choice"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/spinner_choice"
app:layout_constraintStart_toEndOf="@id/text_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
8 changes: 7 additions & 1 deletion Source/Android/app/src/main/res/values/strings.xml
Expand Up @@ -399,8 +399,9 @@

<!-- Game Properties Screen -->
<string name="properties_details">Details</string>
<string name="properties_convert">Convert File</string>
<string name="properties_start_with_riivolution">Start with Riivolution Patches</string>
<string name="properties_set_default_iso">Set as Default ISO</string>
<string name="properties_convert">Convert File</string>
<string name="properties_edit_game_settings">Edit Game Settings</string>
<string name="properties_edit_cheats">Edit Cheats</string>
<string name="properties_clear_game_settings">Clear Game Settings and Cheats</string>
Expand Down Expand Up @@ -480,6 +481,11 @@ and a few other programs. It can efficiently compress encrypted Wii data, but no
It can efficiently compress both junk data and encrypted Wii data.
</string>

<!-- Riivolution Boot Screen -->
<string name="riivolution_sd_root">SD Root: %1$s</string>
<string name="riivolution_disabled">Disabled</string>
<string name="riivolution_start">Start</string>

<!-- Emulation Menu -->
<string name="pause_emulation">Pause Emulation</string>
<string name="unpause_emulation">Unpause Emulation</string>
Expand Down
21 changes: 21 additions & 0 deletions Source/Android/jni/AndroidCommon/IDCache.cpp
Expand Up @@ -70,6 +70,9 @@ static jclass s_patch_cheat_class;
static jfieldID s_patch_cheat_pointer;
static jmethodID s_patch_cheat_constructor;

static jclass s_riivolution_patches_class;
static jfieldID s_riivolution_patches_pointer;

namespace IDCache
{
JNIEnv* GetEnvForThread()
Expand Down Expand Up @@ -325,6 +328,16 @@ jmethodID GetPatchCheatConstructor()
return s_patch_cheat_constructor;
}

jclass GetRiivolutionPatchesClass()
{
return s_riivolution_patches_class;
}

jfieldID GetRiivolutionPatchesPointer()
{
return s_riivolution_patches_pointer;
}

} // namespace IDCache

extern "C" {
Expand Down Expand Up @@ -454,6 +467,13 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved)
s_patch_cheat_constructor = env->GetMethodID(patch_cheat_class, "<init>", "(J)V");
env->DeleteLocalRef(patch_cheat_class);

const jclass riivolution_patches_class =
env->FindClass("org/dolphinemu/dolphinemu/features/riivolution/model/RiivolutionPatches");
s_riivolution_patches_class =
reinterpret_cast<jclass>(env->NewGlobalRef(riivolution_patches_class));
s_riivolution_patches_pointer = env->GetFieldID(riivolution_patches_class, "mPointer", "J");
env->DeleteLocalRef(riivolution_patches_class);

return JNI_VERSION;
}

Expand All @@ -477,5 +497,6 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved)
env->DeleteGlobalRef(s_ar_cheat_class);
env->DeleteGlobalRef(s_gecko_cheat_class);
env->DeleteGlobalRef(s_patch_cheat_class);
env->DeleteGlobalRef(s_riivolution_patches_class);
}
}
3 changes: 3 additions & 0 deletions Source/Android/jni/AndroidCommon/IDCache.h
Expand Up @@ -69,4 +69,7 @@ jclass GetPatchCheatClass();
jfieldID GetPatchCheatPointer();
jmethodID GetPatchCheatConstructor();

jclass GetRiivolutionPatchesClass();
jfieldID GetRiivolutionPatchesPointer();

} // namespace IDCache
1 change: 1 addition & 0 deletions Source/Android/jni/CMakeLists.txt
Expand Up @@ -10,6 +10,7 @@ add_library(main SHARED
GameList/GameFileCache.cpp
IniFile.cpp
MainAndroid.cpp
RiivolutionPatches.cpp
WiiUtils.cpp
)

Expand Down
27 changes: 20 additions & 7 deletions Source/Android/jni/MainAndroid.cpp
Expand Up @@ -48,6 +48,7 @@

#include "DiscIO/Blob.h"
#include "DiscIO/Enums.h"
#include "DiscIO/RiivolutionParser.h"
#include "DiscIO/ScrubbedBlob.h"
#include "DiscIO/Volume.h"

Expand Down Expand Up @@ -547,7 +548,7 @@ static float GetRenderSurfaceScale(JNIEnv* env)
return env->CallStaticFloatMethod(native_library_class, get_render_surface_scale_method);
}

static void Run(JNIEnv* env, const std::vector<std::string>& paths,
static void Run(JNIEnv* env, const std::vector<std::string>& paths, bool riivolution,
const std::optional<std::string>& savestate_path = {},
bool delete_savestate = false)
{
Expand All @@ -564,6 +565,16 @@ static void Run(JNIEnv* env, const std::vector<std::string>& paths,
if (boot)
boot->delete_savestate = delete_savestate;

if (riivolution && std::holds_alternative<BootParameters::Disc>(boot->parameters))
{
const std::string& riivolution_dir = File::GetUserPath(D_RIIVOLUTION_IDX);
const DiscIO::Volume& volume = *std::get<BootParameters::Disc>(boot->parameters).volume;

AddRiivolutionPatches(boot.get(), DiscIO::Riivolution::GenerateRiivolutionPatchesFromConfig(
riivolution_dir, volume.GetGameID(), volume.GetRevision(),
volume.GetDiscNumber()));
}

WindowSystemInfo wsi(WindowSystemType::Android, nullptr, s_surf, s_surf);
wsi.render_surface_scale = GetRenderSurfaceScale(env);

Expand Down Expand Up @@ -616,17 +627,19 @@ static void Run(JNIEnv* env, const std::vector<std::string>& paths,
IDCache::GetFinishEmulationActivity());
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Run___3Ljava_lang_String_2(
JNIEnv* env, jclass, jobjectArray jPaths)
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Run___3Ljava_lang_String_2Z(
JNIEnv* env, jclass, jobjectArray jPaths, jboolean jRiivolution)
{
Run(env, JStringArrayToVector(env, jPaths));
Run(env, JStringArrayToVector(env, jPaths), jRiivolution);
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_NativeLibrary_Run___3Ljava_lang_String_2Ljava_lang_String_2Z(
JNIEnv* env, jclass, jobjectArray jPaths, jstring jSavestate, jboolean jDeleteSavestate)
Java_org_dolphinemu_dolphinemu_NativeLibrary_Run___3Ljava_lang_String_2ZLjava_lang_String_2Z(
JNIEnv* env, jclass, jobjectArray jPaths, jboolean jRiivolution, jstring jSavestate,
jboolean jDeleteSavestate)
{
Run(env, JStringArrayToVector(env, jPaths), GetJString(env, jSavestate), jDeleteSavestate);
Run(env, JStringArrayToVector(env, jPaths), jRiivolution, GetJString(env, jSavestate),
jDeleteSavestate);
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_ChangeDisc(JNIEnv* env, jclass,
Expand Down
193 changes: 193 additions & 0 deletions Source/Android/jni/RiivolutionPatches.cpp
@@ -0,0 +1,193 @@
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <string>
#include <string_view>
#include <vector>

#include <fmt/format.h>
#include <jni.h>

#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/StringUtil.h"
#include "DiscIO/RiivolutionParser.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"

static std::vector<DiscIO::Riivolution::Disc>* GetPointer(JNIEnv* env, jobject obj)
{
return reinterpret_cast<std::vector<DiscIO::Riivolution::Disc>*>(
env->GetLongField(obj, IDCache::GetRiivolutionPatchesPointer()));
}

static std::vector<DiscIO::Riivolution::Disc>& GetReference(JNIEnv* env, jobject obj)
{
return *GetPointer(env, obj);
}

extern "C" {

JNIEXPORT jlong JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_initialize(JNIEnv* env,
jclass obj)
{
return reinterpret_cast<jlong>(new std::vector<DiscIO::Riivolution::Disc>);
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_finalize(JNIEnv* env,
jobject obj)
{
delete GetPointer(env, obj);
}

JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getDiscCount(
JNIEnv* env, jobject obj)
{
return GetReference(env, obj).size();
}

JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getDiscName(
JNIEnv* env, jobject obj, jint disc_index)
{
std::string filename, extension;
SplitPath(GetReference(env, obj)[disc_index].m_xml_path, nullptr, &filename, &extension);
return ToJString(env, filename + extension);
}

JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getSectionCount(
JNIEnv* env, jobject obj, jint disc_index)
{
return GetReference(env, obj)[disc_index].m_sections.size();
}

JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getSectionName(
JNIEnv* env, jobject obj, jint disc_index, jint section_index)
{
return ToJString(env, GetReference(env, obj)[disc_index].m_sections[section_index].m_name);
}

JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getOptionCount(
JNIEnv* env, jobject obj, jint disc_index, jint section_index)
{
return GetReference(env, obj)[disc_index].m_sections[section_index].m_options.size();
}

JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getOptionName(
JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index)
{
return ToJString(
env,
GetReference(env, obj)[disc_index].m_sections[section_index].m_options[option_index].m_name);
}

JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getChoiceCount(
JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index)
{
return GetReference(env, obj)[disc_index]
.m_sections[section_index]
.m_options[option_index]
.m_choices.size();
}

JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getChoiceName(
JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index,
jint choice_index)
{
return ToJString(env, GetReference(env, obj)[disc_index]
.m_sections[section_index]
.m_options[option_index]
.m_choices[choice_index]
.m_name);
}

JNIEXPORT jint JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_getSelectedChoice(
JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index)
{
return GetReference(env, obj)[disc_index]
.m_sections[section_index]
.m_options[option_index]
.m_selected_choice;
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_setSelectedChoiceImpl(
JNIEnv* env, jobject obj, jint disc_index, jint section_index, jint option_index,
jint choice_index)
{
GetReference(env, obj)[disc_index]
.m_sections[section_index]
.m_options[option_index]
.m_selected_choice = choice_index;
}

static std::optional<DiscIO::Riivolution::Config> LoadConfigXML(const std::string& root_directory,
std::string_view game_id)
{
// The way Riivolution stores settings only makes sense for standard game IDs.
if (!(game_id.size() == 4 || game_id.size() == 6))
return std::nullopt;

return DiscIO::Riivolution::ParseConfigFile(
fmt::format("{}/riivolution/config/{}.xml", root_directory, game_id.substr(0, 4)));
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_loadConfigImpl(
JNIEnv* env, jobject obj, jstring j_game_id, jint revision, jint disc_number)
{
const std::string game_id = GetJString(env, j_game_id);
auto& discs = GetReference(env, obj);

const std::string& riivolution_dir = File::GetUserPath(D_RIIVOLUTION_IDX);
const auto config = LoadConfigXML(riivolution_dir, game_id);

discs.clear();
for (const std::string& path : Common::DoFileSearch({riivolution_dir + "riivolution"}, {".xml"}))
{
auto parsed = DiscIO::Riivolution::ParseFile(path);
if (!parsed || !parsed->IsValidForGame(game_id, revision, disc_number))
continue;
if (config)
DiscIO::Riivolution::ApplyConfigDefaults(&*parsed, *config);
discs.emplace_back(std::move(*parsed));
}
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_riivolution_model_RiivolutionPatches_saveConfigImpl(
JNIEnv* env, jobject obj, jstring j_game_id)
{
const std::string game_id = GetJString(env, j_game_id);
if (!(game_id.size() == 4 || game_id.size() == 6))
return;

DiscIO::Riivolution::Config config;
for (const auto& disc : GetReference(env, obj))
{
for (const auto& section : disc.m_sections)
{
for (const auto& option : section.m_options)
{
std::string id = option.m_id.empty() ? (section.m_name + option.m_name) : option.m_id;
config.m_options.emplace_back(
DiscIO::Riivolution::ConfigOption{std::move(id), option.m_selected_choice});
}
}
}

const std::string& root = File::GetUserPath(D_RIIVOLUTION_IDX);
DiscIO::Riivolution::WriteConfigFile(
fmt::format("{}/riivolution/config/{}.xml", root, game_id.substr(0, 4)), config);
}
}
43 changes: 39 additions & 4 deletions Source/Core/DiscIO/RiivolutionParser.cpp
Expand Up @@ -9,8 +9,10 @@
#include <string_view>
#include <vector>

#include <fmt/format.h>
#include <pugixml.hpp>

#include "Common/FileSearch.h"
#include "Common/FileUtil.h"
#include "Common/IOFile.h"
#include "Common/StringUtil.h"
Expand Down Expand Up @@ -354,7 +356,7 @@ std::vector<Patch> GenerateRiivolutionPatchesFromGameModDescriptor(
{
for (auto& option : section.m_options)
{
const auto* info = [&]() -> const DiscIO::GameModDescriptorRiivolutionPatchOption* {
const auto* info = [&]() -> const GameModDescriptorRiivolutionPatchOption* {
for (const auto& o : patch_info.options)
{
if (o.section_name == section.m_name)
Expand All @@ -374,14 +376,47 @@ std::vector<Patch> GenerateRiivolutionPatchesFromGameModDescriptor(

for (auto& p : parsed->GeneratePatches(game_id))
{
p.m_file_data_loader = std::make_shared<DiscIO::Riivolution::FileDataLoaderHostFS>(
patch_info.root, parsed->m_xml_path, p.m_root);
p.m_file_data_loader =
std::make_shared<FileDataLoaderHostFS>(patch_info.root, parsed->m_xml_path, p.m_root);
result.emplace_back(std::move(p));
}
}
return result;
}

std::vector<Patch> GenerateRiivolutionPatchesFromConfig(const std::string root_directory,
const std::string& game_id,
std::optional<u16> revision,
std::optional<u8> disc_number)
{
std::vector<Patch> result;

// The way Riivolution stores settings only makes sense for standard game IDs.
if (!(game_id.size() == 4 || game_id.size() == 6))
return result;

const std::optional<Config> config = ParseConfigFile(
fmt::format("{}/riivolution/config/{}.xml", root_directory, game_id.substr(0, 4)));

for (const std::string& path : Common::DoFileSearch({root_directory + "riivolution"}, {".xml"}))
{
std::optional<Disc> parsed = ParseFile(path);
if (!parsed || !parsed->IsValidForGame(game_id, revision, disc_number))
continue;
if (config)
ApplyConfigDefaults(&*parsed, *config);

for (auto& patch : parsed->GeneratePatches(game_id))
{
patch.m_file_data_loader =
std::make_shared<FileDataLoaderHostFS>(root_directory, parsed->m_xml_path, patch.m_root);
result.emplace_back(std::move(patch));
}
}

return result;
}

std::optional<Config> ParseConfigFile(const std::string& filename)
{
::File::IOFile f(filename, "rb");
Expand Down Expand Up @@ -460,7 +495,7 @@ void ApplyConfigDefaults(Disc* disc, const Config& config)
{
for (const auto& config_option : config.m_options)
{
auto* matching_option = [&]() -> DiscIO::Riivolution::Option* {
auto* matching_option = [&]() -> Option* {
for (auto& section : disc->m_sections)
{
for (auto& option : section.m_options)
Expand Down
5 changes: 4 additions & 1 deletion Source/Core/DiscIO/RiivolutionParser.h
Expand Up @@ -143,7 +143,6 @@ struct Memory
std::vector<u8> m_original;

// If true, this memory patch is an ocarina-style patch.
// TODO: I'm unsure what this means exactly, need to check some examples...
bool m_ocarina = false;

// If true, the offset is not known, and instead we should search for the m_original bytes in
Expand Down Expand Up @@ -223,6 +222,10 @@ std::optional<Disc> ParseString(std::string_view xml, std::string xml_path);
std::vector<Patch> GenerateRiivolutionPatchesFromGameModDescriptor(
const GameModDescriptorRiivolution& descriptor, const std::string& game_id,
std::optional<u16> revision, std::optional<u8> disc_number);
std::vector<Patch> GenerateRiivolutionPatchesFromConfig(const std::string root_directory,
const std::string& game_id,
std::optional<u16> revision,
std::optional<u8> disc_number);
std::optional<Config> ParseConfigFile(const std::string& filename);
std::optional<Config> ParseConfigString(std::string_view xml);
std::string WriteConfigString(const Config& config);
Expand Down