Skip to content

Commit

Permalink
1. Improve compatibility
Browse files Browse the repository at this point in the history
2. Fix install logic
  • Loading branch information
ZaneYork committed Jul 11, 2023
1 parent a23388e commit dc9ded2
Show file tree
Hide file tree
Showing 24 changed files with 182 additions and 29 deletions.
3 changes: 1 addition & 2 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
minSdkVersion 21
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 30
versionCode 76
versionName "3.18.3.2"
versionCode 77
versionName "3.18.3.3"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
Expand Down
Binary file modified app/src/main/assets/apk/StardewModdingAPI.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion app/src/main/assets/mods_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
},
{
"assetPath":"mods/console-commands.zip",
"Name": "Console Commands",
"Name": "ConsoleCommands",
"UniqueID": "SMAPI.ConsoleCommands"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package androidx.documentfile.provider;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.text.TextUtils;
import android.util.Log;


import java.util.ArrayList;
import java.util.List;

public class DocumentUtils {
public interface IFileFilter {
boolean accept(String name);
}
private static TreeDocumentFile findFile(Context context, TreeDocumentFile file, String name) {
final ContentResolver resolver = context.getContentResolver();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(file.getUri(), DocumentsContract.getDocumentId(file.getUri()));
Cursor c = null;
try {
c = resolver.query(childrenUri, new String[] {DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME}, null, null, null);
while (c.moveToNext()) {
final String documentName = c.getString(1);
if (TextUtils.equals(name, documentName)) {
final String documentId = c.getString(0);
final Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(file.getUri(), documentId);
return new TreeDocumentFile(file, context, documentUri);
}
}
} catch (Exception e) {
Log.w("DocumentUtils", "Failed query: " + e);
} finally {
if (c != null) {
c.close();
}
}
return null;
}
public static DocumentFile findFile(Context context, DocumentFile documentFile, String name) {
if (documentFile instanceof TreeDocumentFile)
return findFile(context, (TreeDocumentFile)documentFile, name);
return documentFile.findFile(name);
}
private static List<DocumentFile> filterFiles(Context context, TreeDocumentFile file, IFileFilter filter) {
ContentResolver resolver = context.getContentResolver();
Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(file.getUri(), DocumentsContract.getDocumentId(file.getUri()));
List<DocumentFile> filtered = new ArrayList<>();
Cursor c = null;
try {
c = resolver.query(childrenUri, new String[] {DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME}, null, null, null);
while (c.moveToNext()) {
String documentName = c.getString(1);
String documentId = c.getString(0);
Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(file.getUri(), documentId);
TreeDocumentFile child = new TreeDocumentFile(file, context, documentUri);
if (child.isDirectory())
filtered.addAll(filterFiles(context, child, filter));
else if (filter.accept(documentName))
filtered.add(child);
}
} catch (Exception e) {
Log.w("DocumentUtils", "Failed query: " + e);
} finally {
if (c != null) {
c.close();
}
}
return filtered;
}
public static List<DocumentFile> filterFiles(Context context, DocumentFile documentFile, IFileFilter filter) {
if (documentFile instanceof TreeDocumentFile)
return filterFiles(context, (TreeDocumentFile)documentFile, filter);
List<DocumentFile> filtered = new ArrayList<>();
DocumentFile[] files = documentFile.listFiles();
if (files != null) {
for (DocumentFile file : files) {
if (filter.accept(file.getName()))
filtered.add(file);
}
}
return filtered;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public Tuple2<String, String[]> extract(int advancedStage) {
* 将指定APK文件重新打包,添加SMAPI,修改AndroidManifest.xml,同时验证版本是否正确
*
* @param apkPath APK文件路径
* @param second
* @param targetFile 目标文件
* @param isAdvanced 是否高级模式
* @return 是否成功打包
Expand Down
17 changes: 8 additions & 9 deletions app/src/main/java/com/zane/smapiinstaller/logic/CommonLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import androidx.annotation.RequiresApi;
import androidx.documentfile.provider.DocumentFile;
import androidx.documentfile.provider.DocumentUtils;

import com.afollestad.materialdialogs.MaterialDialog;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -275,27 +276,25 @@ public static boolean unpackSmapiFiles(Activity context, String apkPath, boolean
if (CommonLogic.checkDataRootPermission(context)) {
Uri targetDirUri = pathToTreeUri(Constants.TARGET_DATA_FILE_URI);
DocumentFile documentFile = DocumentFile.fromTreeUri(context, targetDirUri);
for (DocumentFile file : documentFile.listFiles()) {
if (file.getName().equals("files")) {
copyDocument(context, new File(basePath, "smapi-internal"), file);
copyDocument(context, new File(basePath, "Mods"), file);
}
DocumentFile filesDoc = DocumentUtils.findFile(context, documentFile, "files");
if(filesDoc != null) {
copyDocument(context, new File(basePath, "smapi-internal"), filesDoc);
}
}
return true;
}

private static void copyDocument(Activity context, File src, DocumentFile dest) {
public static void copyDocument(Activity context, File src, DocumentFile dest) {
if (src.isDirectory()) {
DocumentFile documentFile = dest.findFile(src.getName());
DocumentFile documentFile = DocumentUtils.findFile(context, dest, src.getName());
if (documentFile == null) {
documentFile = dest.createDirectory(src.getName());
}
for (File file : src.listFiles()) {
copyDocument(context, file, documentFile);
}
} else {
DocumentFile documentFile = dest.findFile(src.getName());
DocumentFile documentFile = DocumentUtils.findFile(context, dest, src.getName());
if (documentFile == null) {
documentFile = dest.createFile("application/x-binary", src.getName());
}
Expand Down Expand Up @@ -512,7 +511,7 @@ public static void openPermissionSetting(Activity activity) {
}
}

public static boolean checkDataRootPermission(Activity context) {
public static boolean checkDataRootPermission(Context context) {
File pathFrom = new File(FileUtils.getStadewValleyBasePath(), "Android/data/" + Constants.TARGET_PACKAGE_NAME + "/files/");
if (!pathFrom.exists()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
Expand Down Expand Up @@ -45,6 +46,8 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;

import androidx.documentfile.provider.DocumentFile;
import androidx.documentfile.provider.DocumentUtils;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

Expand Down Expand Up @@ -169,6 +172,7 @@ public boolean installDefaultMods() {
}
File modFolder = new File(FileUtils.getStadewValleyBasePath(), Constants.MOD_PATH);
ImmutableListMultimap<String, ModManifestEntry> installedModMap = Multimaps.index(findAllInstalledMods(), ModManifestEntry::getUniqueID);
List<File> unpackedMods = new ArrayList<>();
for (ModManifestEntry mod : modManifestEntries) {
if (installedModMap.containsKey(mod.getUniqueID())) {
ImmutableList<ModManifestEntry> installedMods = installedModMap.get(mod.getUniqueID());
Expand Down Expand Up @@ -202,7 +206,9 @@ public boolean installDefaultMods() {
}
if (installedMods.size() > 0) {
try {
ZipUtil.unpack(context.getAssets().open(mod.getAssetPath()), new File(installedMods.get(0).getAssetPath()), (name) -> StringUtils.removeStart(name, mod.getName() + "/"));
File targetFile = new File(installedMods.get(0).getAssetPath());
ZipUtil.unpack(context.getAssets().open(mod.getAssetPath()), targetFile, (name) -> StringUtils.removeStart(name, mod.getName() + "/"));
unpackedMods.add(targetFile);
} catch (IOException e) {
Log.e(TAG, "Install Mod Error", e);
}
Expand All @@ -211,10 +217,25 @@ public boolean installDefaultMods() {
}
try {
ZipUtil.unpack(context.getAssets().open(mod.getAssetPath()), modFolder);
unpackedMods.add(new File(modFolder, mod.getName()));
} catch (IOException e) {
Log.e(TAG, "Install Mod Error", e);
}
}
if (CommonLogic.checkDataRootPermission(context)) {
Uri targetDirUri = CommonLogic.pathToTreeUri(Constants.TARGET_DATA_FILE_URI);
DocumentFile documentFile = DocumentFile.fromTreeUri(context, targetDirUri);
if(documentFile != null) {
DocumentFile filesDoc = DocumentUtils.findFile(context, documentFile, "files");
DocumentFile modsDoc = DocumentUtils.findFile(context, filesDoc, "Mods");
if (modsDoc == null) {
modsDoc = filesDoc.createDirectory("Mods");
}
for (File mod : unpackedMods) {
CommonLogic.copyDocument(context, mod, modsDoc);
}
}
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ private void joinQQ() {
case 1:
CommonLogic.doOnNonNull(this.getContext(), (context) -> CommonLogic.openUrl(context, baseUrl + "kshK7BavcS2jXZ6exDvezc18ksLB8YsM"));
break;
default:
case 2:
CommonLogic.doOnNonNull(this.getContext(), (context) -> CommonLogic.openUrl(context, baseUrl + "zqsWYGBuAxPx0n9RI_ONs-7NA1Mm48QY"));
break;
case 3:
CommonLogic.doOnNonNull(this.getContext(), (context) -> CommonLogic.openUrl(context, baseUrl + "uYnxVTCGlWuLbeb3XA3mDXoO0tlYhy3J"));
break;
default:
CommonLogic.doOnNonNull(this.getContext(), (context) -> CommonLogic.openUrl(context, "https://s.zaneyork.cn:8443/s/qc"));
break;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public View onCreateView(@NonNull LayoutInflater inflater,
binding.layoutAdvInstall.setVisibility(View.VISIBLE);
}
try {
String firstLine = Files.asCharSource(new File(FileUtils.getStadewValleyBasePath(), Constants.LOG_PATH), StandardCharsets.UTF_8).readFirstLine();
String firstLine = Files.asCharSource(FileUtils.docOverlayFetch(context, Constants.LOG_PATH), StandardCharsets.UTF_8).readFirstLine();
if (StringUtils.isNoneBlank(firstLine)) {
String versionString = RegExUtils.removePattern(firstLine, "\\[.+\\]\\s+");
versionString = RegExUtils.removePattern(versionString, "\\s+with.+");
Expand Down Expand Up @@ -158,7 +158,6 @@ private void installLogic(boolean isAdv) {
return;
}
ModAssetsManager modAssetsManager = new ModAssetsManager(binding.getRoot());
DialogUtils.setProgressDialogState(binding.getRoot(), dialog, R.string.unpacking_smapi_files, 6);
modAssetsManager.installDefaultMods();

DialogUtils.setProgressDialogState(binding.getRoot(), dialog, R.string.signing_package, null);
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/com/zane/smapiinstaller/utils/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.zane.smapiinstaller.utils;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import androidx.documentfile.provider.DocumentFile;
import androidx.documentfile.provider.DocumentUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
Expand All @@ -12,6 +17,8 @@
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.hjq.language.MultiLanguages;
import com.zane.smapiinstaller.constant.Constants;
import com.zane.smapiinstaller.logic.CommonLogic;

import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -22,6 +29,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand Down Expand Up @@ -327,4 +335,34 @@ public static List<String> listAll(String basePath, Predicate<File> filter) {
File::getAbsolutePath)
);
}

public static File docOverlayFetch(Context context, String relativePath) {
relativePath = relativePath.replace("StardewValley/", "");
if (CommonLogic.checkDataRootPermission(context)) {
Uri targetDirUri = CommonLogic.pathToTreeUri(Constants.TARGET_DATA_FILE_URI);
DocumentFile documentFile = DocumentFile.fromTreeUri(context, targetDirUri);
DocumentFile filesDoc = DocumentUtils.findFile(context, documentFile, "files");
if(filesDoc != null) {
String[] split = relativePath.split("/");
DocumentFile currentDoc = filesDoc;
for (String path : split) {
currentDoc = DocumentUtils.findFile(context, currentDoc, path);
if(currentDoc == null) {
break;
}
}
if(currentDoc != null && currentDoc.isFile()) {
try (InputStream inputStream = context.getContentResolver().openInputStream(currentDoc.getUri())) {
File tempFile = File.createTempFile(currentDoc.getName(), null);
tempFile.deleteOnExit();
FileUtils.copy(inputStream, tempFile);
return tempFile;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
return new File(FileUtils.getStadewValleyBasePath(), relativePath);
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="settings_verbose_logging">Registro detallado</string>
<string name="signing_package">Firmando paquete deinstalación</string>
<string name="smapi_game_name">SMAPI Stardew Valley</string>
<string name="smapi_version">Versión SMAPI: 3.18.3.2</string>
<string name="smapi_version">Versión SMAPI: 3.18.3.3</string>
<string name="text_install_tip1">Nota: Requiere la versión del juego 1.5.6.39 o superior</string>
<string name="text_install_tip2">El cuerpo del juego debe instalarse durante la actualización o instalación</string>
<string name="unpacking_smapi_files">Desempacando</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="settings_verbose_logging">Journalisation détaillée</string>
<string name="signing_package">Signature</string>
<string name="smapi_game_name">SMAPI Stardew Valley</string>
<string name="smapi_version">Version SMAPI: 3.18.3.2</string>
<string name="smapi_version">Version SMAPI: 3.18.3.3</string>
<string name="text_install_tip1">Remarques: La version du jeu 1.5.6.39 ou ultérieure est requise.</string>
<string name="text_install_tip2">Le jeu de base est requis lors de la mise à jour / installation.</string>
<string name="unpacking_smapi_files">Déballage</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="settings_verbose_logging">Catatan Terperinci</string>
<string name="signing_package">Menandatangani</string>
<string name="smapi_game_name">SMAPI Stardew Valley</string>
<string name="smapi_version">Versi SMAPI: 3.18.3.2</string>
<string name="smapi_version">Versi SMAPI: 3.18.3.3</string>
<string name="text_install_tip1">Catatan: Dibutuhkan Stardew Valley versi 1.5.6.39 atau yang lebih baru.</string>
<string name="text_install_tip2">Permainan dasar diperlukan saat memperbarui/menginstal.</string>
<string name="unpacking_smapi_files">Membongkar</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ko-rKR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="settings_verbose_logging">자세한 로그</string>
<string name="signing_package">설치 패키지 서명</string>
<string name="smapi_game_name">SMAPI Stardew Valley</string>
<string name="smapi_version">SMAPI버전: 3.18.3.2</string>
<string name="smapi_version">SMAPI버전: 3.18.3.3</string>
<string name="text_install_tip1">참고 : 게임 버전 1.5.6.39 이상이 필요합니다</string>
<string name="text_install_tip2">업데이트 또는 설치 중에 게임 본체를 설치해야합니다</string>
<string name="unpacking_smapi_files">포장 풀기</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="settings_verbose_logging">Log detalhado</string>
<string name="signing_package">Assinatura</string>
<string name="smapi_game_name">SMAPI Stardew Valley</string>
<string name="smapi_version">Versão SMAPI: 3.18.3.2</string>
<string name="smapi_version">Versão SMAPI: 3.18.3.3</string>
<string name="text_install_tip1">Notas: É necessária a versão do jogo 1.5.6.39 ou posterior.</string>
<string name="text_install_tip2">O jogo base é necessário ao atualizar / instalar.</string>
<string name="unpacking_smapi_files">Desembalar</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-th/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,7 @@
<item>860453392</item>
<item>1078428449</item>
<item>980882763</item>
<item>1156021254</item>
<item>QQ频道</item>
</string-array>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-th/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<string name="settings_verbose_logging">บันทึกอย่างละเอียด</string>
<string name="signing_package">กำลังลงทะเบียนแอป</string>
<string name="smapi_game_name">SMAPI Stardew Valley</string>
<string name="smapi_version">เวอร์ชั่น SMAPI: 3.18.3.2</string>
<string name="smapi_version">เวอร์ชั่น SMAPI: 3.18.3.3</string>
<string name="text_install_tip1">หมายเหตุ: ต้องการเกมเวอร์ชั่น 1.5.6.39 หรือใหม่กว่า</string>
<string name="text_install_tip2">ต้องการเกมหลักเมื่อทำการอัปเดต / ติดตั้ง</string>
<string name="unpacking_smapi_files">กำลังแกะกล่อง</string>
Expand Down
Loading

0 comments on commit dc9ded2

Please sign in to comment.