Skip to content

Commit

Permalink
Add support for slot 2 Memory Expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelvcaetano committed Jun 27, 2024
1 parent ec8a89e commit 0a3ee93
Show file tree
Hide file tree
Showing 72 changed files with 521 additions and 649 deletions.
34 changes: 32 additions & 2 deletions app/src/main/cpp/MelonDSAndroidJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unistd.h>
#include <cstdlib>
#include <MelonDS.h>
#include <RomGbaSlotConfig.h>
#include <InputAndroid.h>
#include <android/asset_manager_jni.h>
#include "UriFileHandler.h"
Expand All @@ -18,7 +19,14 @@

#define MAX_CHEAT_SIZE (2*64)

enum GbaSlotType {
NONE = 0,
GBA_ROM = 1,
MEMORY_EXPANSION = 2,
};

void* emulate(void*);
MelonDSAndroid::RomGbaSlotConfig* buildGbaSlotConfig(GbaSlotType slotType, const char* romPath, const char* savePath);

pthread_t emuThread;
pthread_mutex_t emuThreadMutex;
Expand Down Expand Up @@ -186,15 +194,17 @@ Java_me_magnum_melonds_MelonEmulator_getRichPresenceStatus(JNIEnv* env, jobject
}

JNIEXPORT jint JNICALL
Java_me_magnum_melonds_MelonEmulator_loadRomInternal(JNIEnv* env, jobject thiz, jstring romPath, jstring sramPath, jboolean loadGbaRom, jstring gbaRomPath, jstring gbaSramPath)
Java_me_magnum_melonds_MelonEmulator_loadRomInternal(JNIEnv* env, jobject thiz, jstring romPath, jstring sramPath, jint gbaSlotType, jstring gbaRomPath, jstring gbaSramPath)
{
jboolean isCopy = JNI_FALSE;
const char* rom = romPath == nullptr ? nullptr : env->GetStringUTFChars(romPath, &isCopy);
const char* sram = sramPath == nullptr ? nullptr : env->GetStringUTFChars(sramPath, &isCopy);
const char* gbaRom = gbaRomPath == nullptr ? nullptr : env->GetStringUTFChars(gbaRomPath, &isCopy);
const char* gbaSram = gbaSramPath == nullptr ? nullptr : env->GetStringUTFChars(gbaSramPath, &isCopy);

int result = MelonDSAndroid::loadRom(const_cast<char*>(rom), const_cast<char*>(sram), loadGbaRom, const_cast<char*>(gbaRom), const_cast<char*>(gbaSram));
MelonDSAndroid::RomGbaSlotConfig* gbaSlotConfig = buildGbaSlotConfig((GbaSlotType) gbaSlotType, gbaRom, gbaSram);
int result = MelonDSAndroid::loadRom(const_cast<char*>(rom), const_cast<char*>(sram), gbaSlotConfig);
delete gbaSlotConfig;

if (isCopy == JNI_TRUE) {
if (romPath) env->ReleaseStringUTFChars(romPath, rom);
Expand Down Expand Up @@ -467,6 +477,26 @@ Java_me_magnum_melonds_MelonEmulator_updateEmulatorConfiguration(JNIEnv* env, jo
}
}

MelonDSAndroid::RomGbaSlotConfig* buildGbaSlotConfig(GbaSlotType slotType, const char* romPath, const char* savePath)
{
if (slotType == GbaSlotType::GBA_ROM)
{
MelonDSAndroid::RomGbaSlotConfigGbaRom* gbaSlotConfigGbaRom = new MelonDSAndroid::RomGbaSlotConfigGbaRom {
.romPath = std::string(romPath),
.savePath = std::string(savePath)
};
return (MelonDSAndroid::RomGbaSlotConfig*) gbaSlotConfigGbaRom;
}
else if (slotType == GbaSlotType::MEMORY_EXPANSION)
{
return (MelonDSAndroid::RomGbaSlotConfig*) new MelonDSAndroid::RomGbaSlotConfigMemoryExpansion;
}
else
{
return (MelonDSAndroid::RomGbaSlotConfig*) new MelonDSAndroid::RomGbaSlotConfigNone;
}
}

double getCurrentMillis() {
timespec now;
clock_gettime(CLOCK_REALTIME, &now);
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/me/magnum/melonds/MelonEmulator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ object MelonEmulator {
DSI_NAND_BAD
}

enum class GbaSlotType {
NONE,
GBA_ROM,
MEMORY_EXPANSION,
}

external fun setupEmulator(emulatorConfiguration: EmulatorConfiguration, assetManager: AssetManager?, dsiCameraSource: DSiCameraSource?, retroAchievementsCallback: RetroAchievementsCallback, textureBuffer: ByteBuffer)

external fun setupCheats(cheats: Array<Cheat>)
Expand All @@ -47,8 +53,8 @@ object MelonEmulator {

external fun getRichPresenceStatus(): String?

fun loadRom(romUri: Uri, sramUri: Uri, loadGbaRom: Boolean, gbaRomUri: Uri?, gbaSramUri: Uri?): LoadResult {
val loadResult = loadRomInternal(romUri.toString(), sramUri.toString(), loadGbaRom, gbaRomUri?.toString(), gbaSramUri?.toString())
fun loadRom(romUri: Uri, sramUri: Uri, gbaSlotType: GbaSlotType, gbaRomUri: Uri?, gbaSramUri: Uri?): LoadResult {
val loadResult = loadRomInternal(romUri.toString(), sramUri.toString(), gbaSlotType.ordinal, gbaRomUri?.toString(), gbaSramUri?.toString())
return when (loadResult) {
0 -> LoadResult.SUCCESS
1 -> LoadResult.SUCCESS_GBA_FAILED
Expand All @@ -63,7 +69,7 @@ object MelonEmulator {
return FirmwareLoadResult.entries[loadResult]
}

private external fun loadRomInternal(romPath: String, sramPath: String, loadGbaRom: Boolean, gbaRomPath: String?, gbaSramPath: String?): Int
private external fun loadRomInternal(romPath: String, sramPath: String, gbaSlotType: Int, gbaRomPath: String?, gbaSramPath: String?): Int

private external fun bootFirmwareInternal(): Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.net.Uri
import io.reactivex.Single
import me.magnum.melonds.common.uridelegates.UriHandler
import me.magnum.melonds.domain.model.*
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.rom.config.RomConfig
import me.magnum.melonds.extensions.isBlank
import me.magnum.melonds.extensions.nameWithoutExtension
import me.magnum.melonds.impl.NdsRomCache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import android.graphics.Bitmap
import android.net.Uri
import io.reactivex.Single
import me.magnum.melonds.common.uridelegates.UriHandler
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.RomConfig
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.rom.config.RomConfig
import me.magnum.melonds.domain.model.RomInfo
import me.magnum.melonds.domain.model.RomMetadata
import me.magnum.melonds.extensions.isBlank
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package me.magnum.melonds.common.romprocessors
import android.graphics.Bitmap
import android.net.Uri
import io.reactivex.Single
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.RomInfo

interface RomFileProcessor {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/me/magnum/melonds/di/MigrationModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ object MigrationModule {
registerMigration(Migration21to22(context, gson, uriHandler))
registerMigration(Migration24to25(genericJsonArrayMigrationHelper, context))
registerMigration(Migration25to26(genericJsonArrayMigrationHelper))
registerMigration(Migration30to31(genericJsonArrayMigrationHelper))
}
}
}
15 changes: 0 additions & 15 deletions app/src/main/java/me/magnum/melonds/domain/model/RomConfig.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.magnum.melonds.domain.model
package me.magnum.melonds.domain.model.rom

import android.net.Uri
import me.magnum.melonds.domain.model.rom.config.RomConfig
import java.util.*

data class Rom(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package me.magnum.melonds.domain.model.rom.config

import java.util.*

data class RomConfig(
var runtimeConsoleType: RuntimeConsoleType = RuntimeConsoleType.DEFAULT,
var runtimeMicSource: RuntimeMicSource = RuntimeMicSource.DEFAULT,
var layoutId: UUID? = null,
val gbaSlotConfig: RomGbaSlotConfig = RomGbaSlotConfig.None,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.magnum.melonds.domain.model.rom.config

import android.net.Uri

sealed class RomGbaSlotConfig {
data object None : RomGbaSlotConfig()
data class GbaRom(val romPath: Uri?, val savePath: Uri?) : RomGbaSlotConfig()
data object MemoryExpansion : RomGbaSlotConfig()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package me.magnum.melonds.domain.model
package me.magnum.melonds.domain.model.rom.config

import me.magnum.melonds.domain.model.ConsoleType

enum class RuntimeConsoleType(val targetConsoleType: ConsoleType?) : RuntimeEnum<RuntimeConsoleType, ConsoleType> {
DEFAULT(null),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.magnum.melonds.domain.model
package me.magnum.melonds.domain.model.rom.config

interface RuntimeEnum<T, U> {
fun getDefault(): T
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package me.magnum.melonds.domain.model
package me.magnum.melonds.domain.model.rom.config

import me.magnum.melonds.domain.model.MicSource

enum class RuntimeMicSource(val micSource: MicSource?) : RuntimeEnum<RuntimeMicSource, MicSource> {
DEFAULT(null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package me.magnum.melonds.domain.repositories
import android.net.Uri
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.RomConfig
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.rom.config.RomConfig
import me.magnum.melonds.domain.model.RomScanningStatus
import java.util.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package me.magnum.melonds.domain.repositories

import android.graphics.Bitmap
import android.net.Uri
import io.reactivex.Single
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.SaveStateSlot

interface SaveStatesRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.reactivex.Observable
import kotlinx.coroutines.flow.Flow
import me.magnum.melonds.domain.model.*
import me.magnum.melonds.domain.model.camera.DSiCameraSourceType
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.ui.Theme
import java.util.*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.net.Uri
import kotlinx.coroutines.flow.Flow
import me.magnum.melonds.domain.model.Cheat
import me.magnum.melonds.domain.model.ConsoleType
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.emulator.FirmwareLaunchResult
import me.magnum.melonds.domain.model.emulator.RomLaunchResult
import me.magnum.melonds.domain.model.retroachievements.GameAchievementData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import me.magnum.melonds.common.romprocessors.RomFileProcessorFactory
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.RomConfig
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.rom.config.RomConfig
import me.magnum.melonds.domain.model.RomScanningStatus
import me.magnum.melonds.domain.repositories.RomsRepository
import me.magnum.melonds.domain.repositories.SettingsRepository
import me.magnum.melonds.extensions.addTo
import me.magnum.melonds.impl.dtos.RomDto
import me.magnum.melonds.impl.dtos.rom.RomDto
import me.magnum.melonds.utils.FileUtils
import me.magnum.melonds.utils.SubjectSharedFlow
import java.io.File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.graphics.Bitmap
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import me.magnum.melonds.common.uridelegates.UriHandler
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.SaveStateSlot
import me.magnum.melonds.domain.repositories.SaveStatesRepository
import me.magnum.melonds.domain.repositories.SettingsRepository
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/me/magnum/melonds/impl/NdsRomCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.reactivex.Observable
import io.reactivex.subjects.PublishSubject
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.SizeUnit
import me.magnum.melonds.domain.repositories.SettingsRepository
import java.io.File
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import androidx.documentfile.provider.DocumentFile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import me.magnum.melonds.common.romprocessors.RomFileProcessorFactory
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import java.io.File
import java.util.*
import java.util.concurrent.locks.ReentrantLock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.graphics.Bitmap
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import com.squareup.picasso.Picasso
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.SaveStateSlot
import java.io.File

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import me.magnum.melonds.domain.model.LayoutConfiguration
import me.magnum.melonds.domain.model.MacAddress
import me.magnum.melonds.domain.model.MicSource
import me.magnum.melonds.domain.model.RendererConfiguration
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import me.magnum.melonds.domain.model.RomIconFiltering
import me.magnum.melonds.domain.model.SaveStateLocation
import me.magnum.melonds.domain.model.SizeUnit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package me.magnum.melonds.impl.dtos
package me.magnum.melonds.impl.dtos.rom

import android.net.Uri
import com.google.gson.annotations.SerializedName
import me.magnum.melonds.domain.model.RomConfig
import me.magnum.melonds.domain.model.RuntimeConsoleType
import me.magnum.melonds.domain.model.RuntimeMicSource
import java.util.*
import me.magnum.melonds.domain.model.rom.config.RomConfig
import me.magnum.melonds.domain.model.rom.config.RuntimeConsoleType
import me.magnum.melonds.domain.model.rom.config.RuntimeMicSource
import java.util.UUID

data class RomConfigDto(
@SerializedName("runtimeConsoleType")
Expand All @@ -14,12 +13,8 @@ data class RomConfigDto(
val runtimeMicSource: RuntimeMicSource,
@SerializedName("layoutId")
val layoutId: String?,
@SerializedName("loadGbaCart")
val loadGbaCart: Boolean,
@SerializedName("gbaCartPath")
val gbaCartPath: String?,
@SerializedName("gbaSavePath")
val gbaSavePath: String?,
@SerializedName("gbaSlotConfig")
val gbaSlotConfig: RomGbaSlotConfigDto,
) {

companion object {
Expand All @@ -28,9 +23,7 @@ data class RomConfigDto(
romConfig.runtimeConsoleType,
romConfig.runtimeMicSource,
romConfig.layoutId?.toString(),
romConfig.loadGbaCart,
romConfig.gbaCartPath?.toString(),
romConfig.gbaSavePath?.toString(),
RomGbaSlotConfigDto.fromModel(romConfig.gbaSlotConfig),
)
}
}
Expand All @@ -40,9 +33,7 @@ data class RomConfigDto(
runtimeConsoleType,
runtimeMicSource,
layoutId?.let { UUID.fromString(it) },
loadGbaCart,
gbaCartPath?.let { Uri.parse(it) },
gbaSavePath?.let { Uri.parse(it) },
gbaSlotConfig.toModel(),
)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package me.magnum.melonds.impl.dtos
package me.magnum.melonds.impl.dtos.rom

import android.net.Uri
import com.google.gson.annotations.SerializedName
import me.magnum.melonds.domain.model.Rom
import me.magnum.melonds.domain.model.rom.Rom
import java.util.*

data class RomDto(
Expand Down
Loading

0 comments on commit 0a3ee93

Please sign in to comment.