Skip to content

Commit 4f392c9

Browse files
committed
Custom core loading
1 parent 2fe0ec6 commit 4f392c9

2 files changed

Lines changed: 126 additions & 6 deletions

File tree

pkg/android/phoenix/src/com/retroarch/browser/mainmenu/MainMenuActivity.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,24 @@ public void finalStartup()
126126
retro.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
127127
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
128128

129+
// Check if we received intent extras from external launcher
130+
Intent originalIntent = getIntent();
131+
String romPath = originalIntent.getStringExtra("ROM");
132+
String corePath = originalIntent.getStringExtra("LIBRETRO");
133+
String configPath = originalIntent.getStringExtra("CONFIGFILE");
134+
String imePath = originalIntent.getStringExtra("IME");
135+
String dataDirPath = originalIntent.getStringExtra("DATADIR");
136+
String dataSourcePath = originalIntent.getStringExtra("APK");
137+
138+
// Use provided values or defaults
129139
startRetroActivity(
130140
retro,
131-
null,
132-
prefs.getString("libretro_path", getApplicationInfo().dataDir + "/cores/"),
133-
UserPreferences.getDefaultConfigPath(this),
134-
Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD),
135-
getApplicationInfo().dataDir,
136-
getApplicationInfo().sourceDir);
141+
romPath,
142+
corePath != null ? corePath : prefs.getString("libretro_path", getApplicationInfo().dataDir + "/cores/"),
143+
configPath != null ? configPath : UserPreferences.getDefaultConfigPath(this),
144+
imePath != null ? imePath : Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD),
145+
dataDirPath != null ? dataDirPath : getApplicationInfo().dataDir,
146+
dataSourcePath != null ? dataSourcePath : getApplicationInfo().sourceDir);
137147
}
138148

139149
startActivity(retro);

pkg/android/phoenix/src/com/retroarch/browser/retroactivity/RetroActivityFuture.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414
import android.os.Message;
1515
import com.retroarch.browser.preferences.util.ConfigFile;
1616
import com.retroarch.browser.preferences.util.UserPreferences;
17+
import com.retroarch.browser.mainmenu.MainMenuActivity;
1718
import java.lang.reflect.InvocationTargetException;
1819
import java.lang.reflect.Method;
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import java.util.List;
25+
import java.util.ArrayList;
26+
import android.content.pm.PackageManager;
27+
import android.Manifest;
1928

2029
public final class RetroActivityFuture extends RetroActivityCamera {
2130

@@ -62,10 +71,111 @@ public void onCreate(Bundle savedInstanceState) {
6271
isRunning = true;
6372
mDecorView = getWindow().getDecorView();
6473

74+
// Check if we need permissions before proceeding
75+
if (!checkPermissions()) {
76+
// Redirect to MainMenuActivity to get permissions
77+
Intent mainMenuIntent = new Intent(this, MainMenuActivity.class);
78+
mainMenuIntent.putExtras(getIntent());
79+
startActivity(mainMenuIntent);
80+
finish();
81+
return;
82+
}
83+
84+
// Handle core sideloading if external core path provided
85+
handleCoreSideloading();
86+
6587
// If QUITFOCUS parameter is provided then enable that Retroarch quits when focus is lost
6688
quitfocus = getIntent().hasExtra("QUITFOCUS");
6789
}
6890

91+
private boolean checkPermissions() {
92+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
93+
List<String> permissionsNeeded = new ArrayList<String>();
94+
95+
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
96+
permissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
97+
}
98+
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
99+
permissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
100+
}
101+
102+
return permissionsNeeded.isEmpty();
103+
}
104+
return true;
105+
}
106+
107+
private void handleCoreSideloading() {
108+
String corePath = getIntent().getStringExtra("LIBRETRO");
109+
if (corePath == null || corePath.isEmpty()) {
110+
return;
111+
}
112+
113+
File coreFile = new File(corePath);
114+
if (!coreFile.exists()) {
115+
Log.w("RetroActivityFuture", "Core file does not exist: " + corePath);
116+
return;
117+
}
118+
119+
// Get the cores directory
120+
String coresDir = getApplicationInfo().dataDir + "/cores/";
121+
File coresDirFile = new File(coresDir);
122+
123+
// Create cores directory if it doesn't exist
124+
if (!coresDirFile.exists()) {
125+
if (!coresDirFile.mkdirs()) {
126+
Log.e("RetroActivityFuture", "Failed to create cores directory: " + coresDir);
127+
return;
128+
}
129+
}
130+
131+
// Check if core is outside RetroArch cores folder - if so, sideload it
132+
if (!corePath.startsWith(coresDir)) {
133+
String coreFileName = coreFile.getName();
134+
File destinationFile = new File(coresDir, coreFileName);
135+
136+
try {
137+
copyFile(coreFile, destinationFile);
138+
Log.i("RetroActivityFuture", "Sideloaded core from " + corePath + " to " + destinationFile.getAbsolutePath());
139+
140+
// Update intent to point to the new location
141+
getIntent().putExtra("LIBRETRO", destinationFile.getAbsolutePath());
142+
} catch (IOException e) {
143+
Log.e("RetroActivityFuture", "Failed to sideload core: " + e.getMessage());
144+
}
145+
}
146+
}
147+
148+
private void copyFile(File source, File destination) throws IOException {
149+
FileInputStream inStream = null;
150+
FileOutputStream outStream = null;
151+
152+
try {
153+
inStream = new FileInputStream(source);
154+
outStream = new FileOutputStream(destination);
155+
156+
byte[] buffer = new byte[8192];
157+
int length;
158+
while ((length = inStream.read(buffer)) > 0) {
159+
outStream.write(buffer, 0, length);
160+
}
161+
} finally {
162+
if (inStream != null) {
163+
try {
164+
inStream.close();
165+
} catch (IOException e) {
166+
// Ignore
167+
}
168+
}
169+
if (outStream != null) {
170+
try {
171+
outStream.close();
172+
} catch (IOException e) {
173+
// Ignore
174+
}
175+
}
176+
}
177+
}
178+
69179
@Override
70180
public void onNewIntent(Intent intent) {
71181
super.onNewIntent(intent);

0 commit comments

Comments
 (0)