Initialize ID for PF2e imported monsters to prevent SQLite exceptions
This commit is contained in:
@@ -17,6 +17,7 @@ public class ImportConfig {
|
||||
ImportSource.ImportType.OPEN5E_API,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null
|
||||
));
|
||||
|
||||
@@ -28,7 +29,20 @@ public class ImportConfig {
|
||||
ImportSource.ImportType.GIT_ARCHIVE,
|
||||
"https://github.com/foundryvtt/pf2e/archive/refs/heads/master.zip",
|
||||
"com.majinnaibu.monstercards.importers.Pf2eImporter",
|
||||
".json"
|
||||
".json",
|
||||
"packs/pf2e"
|
||||
));
|
||||
|
||||
SOURCES.add(new ImportSource(
|
||||
"sf2e_foundry",
|
||||
"Starfinder 2e Foundry VTT",
|
||||
"foundryvtt",
|
||||
"https://github.com/foundryvtt/pf2e",
|
||||
ImportSource.ImportType.GIT_ARCHIVE,
|
||||
"https://github.com/foundryvtt/pf2e/archive/refs/heads/master.zip",
|
||||
"com.majinnaibu.monstercards.importers.Pf2eImporter",
|
||||
".json",
|
||||
"packs/sf2e"
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.majinnaibu.monstercards.importers;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
@@ -31,19 +31,30 @@ public class GitRepoImporterService {
|
||||
public static int importFromGitRepository(Context context, ImportSource source, BooleanSupplier isCancelled) throws Exception {
|
||||
int totalImported = 0;
|
||||
|
||||
File tempZip = File.createTempFile("repo_download", ".zip", context.getCacheDir());
|
||||
File downloadsDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
|
||||
if (!downloadsDir.exists()) {
|
||||
downloadsDir.mkdirs();
|
||||
}
|
||||
|
||||
File tempZip = null;
|
||||
File[] existingZips = downloadsDir.listFiles((dir, name) -> name.startsWith(source.id + "_download") && name.endsWith(".zip"));
|
||||
if (existingZips != null && existingZips.length > 0) {
|
||||
tempZip = existingZips[0];
|
||||
} else {
|
||||
tempZip = new File(downloadsDir, source.id + "_download_" + System.currentTimeMillis() + ".zip");
|
||||
downloadZip(source.downloadUrl, tempZip, isCancelled);
|
||||
}
|
||||
|
||||
File extractDir = new File(context.getCacheDir(), "repo_extracted_" + System.currentTimeMillis());
|
||||
|
||||
try {
|
||||
downloadZip(source.downloadUrl, tempZip, isCancelled);
|
||||
|
||||
if (isCancelled.getAsBoolean()) return 0;
|
||||
|
||||
if (!extractDir.exists()) {
|
||||
extractDir.mkdirs();
|
||||
}
|
||||
|
||||
unzipAndFilter(tempZip, extractDir, source.fileExtension, isCancelled);
|
||||
unzipAndFilter(tempZip, extractDir, source.fileExtension, source.subfolder, isCancelled);
|
||||
|
||||
if (isCancelled.getAsBoolean()) return 0;
|
||||
|
||||
@@ -58,7 +69,8 @@ public class GitRepoImporterService {
|
||||
totalImported = processDirectory(extractDir, importer, repository, isCancelled);
|
||||
|
||||
} finally {
|
||||
deleteFileOrDir(tempZip);
|
||||
// We no longer delete tempZip since the user wants to keep the downloaded zip
|
||||
// deleteFileOrDir(tempZip);
|
||||
deleteFileOrDir(extractDir);
|
||||
}
|
||||
|
||||
@@ -97,14 +109,15 @@ public class GitRepoImporterService {
|
||||
}
|
||||
}
|
||||
|
||||
private static void unzipAndFilter(File zipFile, File extractDir, String filterExtension, BooleanSupplier isCancelled) throws IOException {
|
||||
private static void unzipAndFilter(File zipFile, File extractDir, String filterExtension, String subfolder, BooleanSupplier isCancelled) throws IOException {
|
||||
try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {
|
||||
ZipEntry entry;
|
||||
while ((entry = zis.getNextEntry()) != null) {
|
||||
if (isCancelled.getAsBoolean()) break;
|
||||
if (!entry.isDirectory()) {
|
||||
String name = entry.getName();
|
||||
if (filterExtension == null || name.endsWith(filterExtension)) {
|
||||
boolean inSubfolder = subfolder == null || subfolder.isEmpty() || name.contains("/" + subfolder + "/") || name.startsWith(subfolder + "/");
|
||||
if (inSubfolder && (filterExtension == null || name.endsWith(filterExtension))) {
|
||||
File outFile = new File(extractDir, new File(name).getName());
|
||||
if (outFile.exists()) {
|
||||
outFile = new File(extractDir, System.currentTimeMillis() + "_" + new File(name).getName());
|
||||
|
||||
@@ -16,6 +16,7 @@ import com.majinnaibu.monstercards.models.Skill;
|
||||
import com.majinnaibu.monstercards.models.Trait;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Pf2eImporter implements EntityImporter<Monster> {
|
||||
@Override
|
||||
@@ -43,6 +44,7 @@ public class Pf2eImporter implements EntityImporter<Monster> {
|
||||
JsonObject system = root.has("system") ? root.getAsJsonObject("system") : new JsonObject();
|
||||
|
||||
Monster monster = new Monster();
|
||||
monster.id = UUID.randomUUID();
|
||||
|
||||
// Name
|
||||
if (root.has("name") && !root.get("name").isJsonNull()) {
|
||||
|
||||
@@ -16,8 +16,9 @@ public class ImportSource {
|
||||
public String downloadUrl;
|
||||
public String importerClassName;
|
||||
public String fileExtension;
|
||||
public String subfolder;
|
||||
|
||||
public ImportSource(String id, String projectName, String creatorName, String creatorPageLink, ImportType importType, String downloadUrl, String importerClassName, String fileExtension) {
|
||||
public ImportSource(String id, String projectName, String creatorName, String creatorPageLink, ImportType importType, String downloadUrl, String importerClassName, String fileExtension, String subfolder) {
|
||||
this.id = id;
|
||||
this.projectName = projectName;
|
||||
this.creatorName = creatorName;
|
||||
@@ -26,5 +27,6 @@ public class ImportSource {
|
||||
this.downloadUrl = downloadUrl;
|
||||
this.importerClassName = importerClassName;
|
||||
this.fileExtension = fileExtension;
|
||||
this.subfolder = subfolder;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user