Skip to content

Commit

Permalink
功能[Turnip驱动]: 允许自定义Turnip驱动
Browse files Browse the repository at this point in the history
  • Loading branch information
Vera-Firefly committed Oct 31, 2024
1 parent bb93e0f commit 1a3088a
Show file tree
Hide file tree
Showing 15 changed files with 424 additions and 14 deletions.
1 change: 1 addition & 0 deletions app_pojavlauncher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ dependencies {
implementation 'androidx.drawerlayout:drawerlayout:1.2.0'
implementation 'androidx.viewpager2:viewpager2:1.1.0-beta01'
implementation 'androidx.annotation:annotation:1.5.0'
implementation 'androidx.documentfile:documentfile:1.0.1'

implementation "androidx.constraintlayout:constraintlayout:2.1.4"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.firefly.ui.prefs;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.preference.ListPreference;

import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;
import com.firefly.utils.TurnipUtils;

import java.util.Arrays;
import java.util.List;

public class ChooseTurnipListPref extends ListPreference {
private List<String> defaultLibs;
private OnPreferenceChangeListener preferenceChangeListener;
private View.OnClickListener confirmButtonListener;

public ChooseTurnipListPref(Context context, AttributeSet attrs) {
super(context, attrs);
loadDefaultLibs(context);
}

private void loadDefaultLibs(Context context) {
defaultLibs = Arrays.asList(context.getResources().getStringArray(R.array.turnip_values));
}

@Override
protected void onClick() {
showDialog();
}

private void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle(getDialogTitle());

CharSequence[] entriesCharSequence = getEntries();
String[] entries = new String[entriesCharSequence.length];
for (int i = 0; i < entriesCharSequence.length; i++) {
entries[i] = entriesCharSequence[i].toString();
}

builder.setItems(entries, (dialog, which) -> {
String newValue = getEntryValues()[which].toString();
if (preferenceChangeListener != null) {
if (preferenceChangeListener.onPreferenceChange(this, newValue)) {
setValue(newValue);
}
} else {
setValue(newValue);
}
dialog.dismiss();
});

LinearLayout layout = new LinearLayout(getContext());
layout.setOrientation(LinearLayout.VERTICAL);

Button createButton = new Button(getContext());
createButton.setText(R.string.pgw_settings_custom_turnip_creat);
createButton.setOnClickListener(view -> {
if (confirmButtonListener != null) {
confirmButtonListener.onClick(view);
}
});
layout.addView(createButton);
builder.setView(layout);

AlertDialog dialog = builder.create();
dialog.show();

ListView listView = dialog.getListView();
listView.setOnItemLongClickListener((adapterView, view, position, id) -> {
String selectedVersion = getEntryValues()[position].toString();
if (defaultLibs.contains(selectedVersion)) {
Toast.makeText(getContext(), R.string.preference_rendererexp_mesa_delete_defaultlib, Toast.LENGTH_SHORT).show();
} else {
showDeleteConfirmationDialog(selectedVersion);
}
dialog.dismiss();
return true;
});
}

@Override
public void setOnPreferenceChangeListener(OnPreferenceChangeListener listener) {
this.preferenceChangeListener = listener;
super.setOnPreferenceChangeListener(listener);
}

public void setConfirmButton(String buttonText, View.OnClickListener listener) {
this.confirmButtonListener = listener;
}

private void showDeleteConfirmationDialog(String version) {
new AlertDialog.Builder(getContext())
.setTitle(R.string.pgw_settings_ctu_delete_title)
.setMessage(getContext().getString(R.string.pgw_settings_ctu_delete_message, version))
.setPositiveButton(R.string.alertdialog_done, (dialog, which) -> {
boolean success = TurnipUtils.INSTANCE.deleteTurnipDriver(version);
if (success) {
Toast.makeText(getContext(), R.string.preference_rendererexp_mesa_deleted, Toast.LENGTH_SHORT).show();
setEntriesAndValues();
} else {
Toast.makeText(getContext(), R.string.preference_rendererexp_mesa_delete_fail, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.alertdialog_cancel, null)
.show();
}

private void setEntriesAndValues() {
Tools.IListAndArry array = Tools.getCompatibleCTurnipDriver(getContext());
setEntries(array.getArray());
setEntryValues(array.getList().toArray(new String[0]));
String currentValue = getValue();
if (!array.getList().contains(currentValue)) {
setValueIndex(0);
}
}
}
89 changes: 89 additions & 0 deletions app_pojavlauncher/src/main/java/com/firefly/utils/TurnipUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.firefly.utils;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;

import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TurnipUtils {

public static final TurnipUtils INSTANCE = new TurnipUtils();
private final File turnipDir;

private TurnipUtils() {
this.turnipDir = new File(Tools.TURNIP_DIR);
if (!turnipDir.exists() && !turnipDir.mkdirs()) {
throw new RuntimeException("Failed to create Turnip directory");
}
}

public List<String> getTurnipDriverList() {
List<String> list = new ArrayList<>();
File[] files = turnipDir.listFiles();
for (File file : files) {
if (file.isDirectory() && new File(file.getAbsolutePath() + "/libvulkan_freedreno.so").exists()) {
list.add(file.getName());
}
}
return list;
}

public String getTurnipDriver(String version) {
return Tools.TURNIP_DIR + "/" + version;
}

public boolean saveTurnipDriver(Context context, Uri fileUri, String folderName) {
try {
File targetDir = new File(turnipDir, folderName);
if (!targetDir.exists() && !targetDir.mkdirs()) {
return false;
}

File targetFile = new File(targetDir, "libvulkan_freedreno.so");

try (InputStream inputStream = context.getContentResolver().openInputStream(fileUri);
OutputStream outputStream = new FileOutputStream(targetFile)) {
if (inputStream == null) return false;

byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean deleteTurnipDriver(String version) {
File libDir = new File(turnipDir, version);
if (libDir.exists()) {
return deleteDirectory(libDir);
}
return false;
}

private boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
return directoryToBeDeleted.delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ private void runCraft(String versionId, JMinecraftVersionList.Version version) t
if (Tools.LOADER_OVERRIDE == null) {
Tools.LOADER_OVERRIDE = LauncherPreferences.PREF_LOCAL_LOADER_OVERRIDE;
}
if (Tools.TURNIP_LIBS == null) {
Tools.TURNIP_LIBS = LauncherPreferences.PREF_TURNIP_LIBS;
}
if (!Tools.checkRendererCompatible(this, Tools.LOCAL_RENDERER)) {
Tools.RenderersList renderersList = Tools.getCompatibleRenderers(this);
String firstCompatibleRenderer = renderersList.rendererIds.get(0);
Expand Down
46 changes: 45 additions & 1 deletion app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.firefly.utils.TurnipUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.movtery.ui.subassembly.customprofilepath.ProfilePathHome;
Expand Down Expand Up @@ -109,6 +111,7 @@ public final class Tools {
public static String NATIVE_LIB_DIR;
public static String DIR_DATA; //Initialized later to get context
public static String MESA_DIR;
public static String TURNIP_DIR;
public static File DIR_CACHE;
public static File FILE_PROFILE_PATH;
public static String MULTIRT_HOME;
Expand All @@ -129,11 +132,13 @@ public final class Tools {

public static String DRIVER_MODEL = null;
public static String MESA_LIBS = null;
public static String TURNIP_LIBS = null;
public static String LOADER_OVERRIDE = null;

private static CDriverModelList sCompatibleCDriverModel;
private static CMesaLibList sCompatibleCMesaLibs;
private static CMesaLDOList sCompatibleCMesaLDO;
private static CTurnipDriverList sCompatibleCTurnipDriver;
private static RenderersList sCompatibleRenderers;

private static File getPojavStorageRoot(Context ctx) {
Expand Down Expand Up @@ -165,6 +170,7 @@ public static void initContextConstants(Context ctx) {
DIR_CACHE = ctx.getCacheDir();
DIR_DATA = ctx.getFilesDir().getParent();
MESA_DIR = DIR_DATA + "/mesa";
TURNIP_DIR = DIR_DATA + "/turnip";
FILE_PROFILE_PATH = new File(Tools.DIR_DATA, "/profile_path.json");
MULTIRT_HOME = DIR_DATA + "/runtimes";
DIR_GAME_HOME = getPojavStorageRoot(ctx).getAbsolutePath();
Expand Down Expand Up @@ -1290,7 +1296,6 @@ public String[] getArray() {
}

public static CMesaLibList getCompatibleCMesaLib(Context context) {
// if (sCompatibleCMesaLibs != null) return sCompatibleCMesaLibs;
Resources resources = context.getResources();
String[] defaultCMesaLib = resources.getStringArray(R.array.osmesa_values);
String[] defaultCMesaLibNames = resources.getStringArray(R.array.osmesa_library);
Expand Down Expand Up @@ -1406,6 +1411,45 @@ public static CMesaLDOList getCompatibleCMesaLDO(Context context) {
return sCompatibleCMesaLDO;
}

public static class CTurnipDriverList implements IListAndArry {
public final List<String> CTurnipDriverIds;
public final String[] CTurnipDriver;

public CTurnipDriverList(List<String> CTurnipDriverIds, String[] CTurnipDriver) {
this.CTurnipDriverIds = CTurnipDriverIds;
this.CTurnipDriver = CTurnipDriver;
}

@Override
public List<String> getList() {
return CTurnipDriverIds;
}

@Override
public String[] getArray() {
return CTurnipDriver;
}
}

public static CTurnipDriverList getCompatibleCTurnipDriver(Context context) {
Resources resources = context.getResources();
String[] defaultCTurnipDriver = resources.getStringArray(R.array.turnip_values);
String[] defaultCTurnipDriverNames = resources.getStringArray(R.array.turnip_files);
List<String> CTurnipDriverIds = new ArrayList<>(defaultCTurnipDriver.length);
List<String> CTurnipDriverNames = new ArrayList<>(defaultCTurnipDriverNames.length);
for (int i = 0; i < defaultCTurnipDriver.length; i++) {
CTurnipDriverIds.add(defaultCTurnipDriver[i]);
CTurnipDriverNames.add(defaultCTurnipDriverNames[i]);
}
List<String> addTurnipList = TurnipUtils.INSTANCE.getTurnipDriverList();
for (String item : addTurnipList) {
CTurnipDriverIds.add(item);
CTurnipDriverNames.add(item);
}
sCompatibleCTurnipDriver = new CTurnipDriverList(CTurnipDriverIds, CTurnipDriverNames.toArray(new String[0]));
return sCompatibleCTurnipDriver;
}

@SuppressLint("DefaultLocale")
public static String formatFileSize(long bytes) {
if (bytes <= 0) return "0 B";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class LauncherPreferences {
public static SharedPreferences DEFAULT_PREF;
public static String PREF_RENDERER = "opengles2";
public static String PREF_MESA_LIB = "default";
public static String PREF_TURNIP_LIBS = "default";
public static String PREF_DRIVER_MODEL = "driver_zink";
public static String PREF_LOCAL_LOADER_OVERRIDE = "kgsl";

Expand Down Expand Up @@ -149,6 +150,7 @@ public static void loadPreferences(Context ctx) {

PREF_EXP_SETUP = DEFAULT_PREF.getBoolean("ExperimentalSetup", false);
PREF_MESA_LIB = DEFAULT_PREF.getString("CMesaLibrary", "default");
PREF_TURNIP_LIBS = DEFAULT_PREF.getString("chooseTurnipDriver", "default");
PREF_DRIVER_MODEL = DEFAULT_PREF.getString("CDriverModels", "driver_zink");
PREF_LOCAL_LOADER_OVERRIDE = DEFAULT_PREF.getString("ChooseMldo", "kgsl");

Expand Down
Loading

0 comments on commit 1a3088a

Please sign in to comment.