Set up Jni and JniGen and did a successfully test
This commit is contained in:
102
lib/aniyomi_bridge.dart
Normal file
102
lib/aniyomi_bridge.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart' show rootBundle;
|
||||
import 'package:jni/jni.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/janiyomibridge.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jsanime.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:archive/archive.dart';
|
||||
|
||||
class AniyomiBridge {
|
||||
|
||||
static final _aniyomiBridgeDir = "aniyomibridge";
|
||||
static final _packageAssetsDir = "packages/k3vinb5_aniyomi_bridge/assets";
|
||||
|
||||
Future<void> initJvm() async{
|
||||
Directory supportDirectory = await getApplicationSupportDirectory();
|
||||
_loadJreIfNeeded(supportDirectory);
|
||||
_loadJarIfNeeded(supportDirectory);
|
||||
Jni.spawn(
|
||||
dylibDir: _getDylibDir(supportDirectory),
|
||||
classPath: _getClassPath(supportDirectory)
|
||||
);
|
||||
JAniyomiBridge.init();
|
||||
JAniyomiBridge jAniyomiBridge = JAniyomiBridge();
|
||||
jAniyomiBridge.loadExtension(JString.fromString("https://gitea.k3vinb5.dev/Backups/kohi-den-extensions/raw/branch/main/apk/aniyomi-en.allanime-v14.37.apk"));
|
||||
JList<JObject?>? searchResults = jAniyomiBridge.getSearchResults(JString.fromString("Naruto"), 1, JString.fromString("allanime"));
|
||||
print("Search results size: ${searchResults?.length}");
|
||||
if (searchResults != null) {
|
||||
for (JObject obj in searchResults.cast()) {
|
||||
print(obj.toString());
|
||||
JSAnime jsAnime = obj.as<JSAnime>(JSAnime.type);
|
||||
print(jsAnime.getTitle().toDartString());
|
||||
print(jsAnime.getUrl().toDartString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
String _getDylibDir(Directory supportDirectory) {
|
||||
String basePath = path.join(supportDirectory.absolute.path, _aniyomiBridgeDir);
|
||||
if (Platform.isLinux) {
|
||||
return path.join(basePath, "jre", "linux", "customjre", "lib", "server");
|
||||
} else if (Platform.isMacOS) {
|
||||
return path.join(basePath, "jre", "macos", "customjre", "lib", "server");
|
||||
} else if (Platform.isWindows) {
|
||||
return path.join(basePath, "jre", "windows", "customjre", "lib", "server");
|
||||
} else {
|
||||
throw UnsupportedError("Unsupported platform");
|
||||
}
|
||||
}
|
||||
|
||||
List<String> _getClassPath(Directory supportDirectory) {
|
||||
return [path.join(supportDirectory.absolute.path, _aniyomiBridgeDir, "aniyomibridge-core.jar")];
|
||||
}
|
||||
|
||||
Future<void> _loadJarIfNeeded(Directory supportDirectory) async {
|
||||
String aniyomiBridgeCorePath = path.join(supportDirectory.path, _aniyomiBridgeDir, "aniyomibridge-core.jar");
|
||||
File aniyomiBridgeCore = File(aniyomiBridgeCorePath);
|
||||
if (!(await aniyomiBridgeCore.exists())) {
|
||||
copyAssetToFile("$_packageAssetsDir/aniyomibridge-core.jar", aniyomiBridgeCorePath);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadJreIfNeeded(Directory supportDirectory) async {
|
||||
String zippedJrePath = path.join(supportDirectory.path, _aniyomiBridgeDir, "jre.zip");
|
||||
File zippedJre = File(zippedJrePath);
|
||||
if (!(await zippedJre.exists())) {
|
||||
await copyAssetToFile("$_packageAssetsDir/jre.zip", zippedJrePath);
|
||||
}
|
||||
Directory jreDir = Directory(path.join(supportDirectory.path, _aniyomiBridgeDir, "jre"));
|
||||
if (!(await jreDir.exists())) {
|
||||
final bytes = await zippedJre.readAsBytes();
|
||||
final jreArchive = ZipDecoder().decodeBytes(bytes);
|
||||
await _unzipFileToDir(jreArchive, "jre", jreDir.path);
|
||||
zippedJre.deleteSync(recursive: false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _unzipFileToDir(Archive archive, String archiveName, String outDirPath) async{
|
||||
outDirPath = outDirPath.replaceAll("/$archiveName", "");
|
||||
for (final file in archive) {
|
||||
final filename = file.name;
|
||||
if (file.isFile) {
|
||||
final data = file.content as List<int>;
|
||||
final outFile = File(path.join(outDirPath, filename));
|
||||
await outFile.parent.create(recursive: true);
|
||||
await outFile.writeAsBytes(data);
|
||||
} else {
|
||||
await Directory(path.join(outDirPath, filename)).create(recursive: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<File> copyAssetToFile(String assetPath, String outPath) async {
|
||||
final byteData = await rootBundle.load(assetPath);
|
||||
final buffer = byteData.buffer.asUint8List();
|
||||
final file = File(outPath);
|
||||
await file.parent.create(recursive: true);
|
||||
await file.writeAsBytes(buffer, flush: true);
|
||||
return file;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user