Refactored the bridge to use load the JVM in a separate Isolate that receives messages in a queue executing them, this way it doesn't block the UI thread
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:isolate';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:jni/jni.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jni_isolate.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jni_isolate_message.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/models/extension.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jvideo.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/janiyomibridge.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jsanime.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jsepisode.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jsmanga.dart';
|
||||
@@ -13,168 +17,205 @@ import 'package:k3vinb5_aniyomi_bridge/jmodels/jschapter.dart';
|
||||
import 'package:k3vinb5_aniyomi_bridge/jmodels/jpage.dart';
|
||||
|
||||
class AniyomiBridge {
|
||||
static const String _aniyomiBridgeDir = "aniyomibridge";
|
||||
// static consts
|
||||
static const String _aniyomiBridgeAnimeExtensionsDir = "animeExtensions";
|
||||
static const String _aniyomiBridgeMangaExtensionsDir = "mangaExtensions";
|
||||
static const String _aniyomiBridgeDir = "aniyomibridge";
|
||||
static const String _aniyomiBridgeJarName = "aniyomibridge-core.jar";
|
||||
static const String _packageAssetsDir =
|
||||
"packages/k3vinb5_aniyomi_bridge/assets";
|
||||
static late final JAniyomiBridge _jAniyomiBridge;
|
||||
static late final String _supportDirectoryPath;
|
||||
bool _isReady = false;
|
||||
|
||||
// static variables
|
||||
static bool _isReady = false;
|
||||
static late final JniIsolate _jniIsolate;
|
||||
static late final ReceivePort _jniReceiverPort;
|
||||
static late final SendPort _jniSenderPort;
|
||||
|
||||
// instance variables
|
||||
late final String _supportDirectoryPath;
|
||||
final Random _random = Random();
|
||||
final Map<int, Completer> _pendingRequests = {};
|
||||
|
||||
|
||||
AniyomiBridge() {
|
||||
_initJvm();
|
||||
}
|
||||
|
||||
Future<void> _initJvm() async {
|
||||
if (_isReady) {
|
||||
return;
|
||||
}
|
||||
Directory supportDirectory = await getApplicationSupportDirectory();
|
||||
_supportDirectoryPath = supportDirectory.path;
|
||||
await _loadJarIfNeeded(supportDirectory);
|
||||
Jni.spawnIfNotExists(
|
||||
dylibDir: _getDylibDir(supportDirectory),
|
||||
classPath: _getClassPath(supportDirectory),
|
||||
Jni.setDylibDir(dylibDir: _getDylibDir(supportDirectory));
|
||||
|
||||
_jniIsolate = JniIsolate();
|
||||
final readyPort = ReceivePort();
|
||||
_jniReceiverPort = ReceivePort();
|
||||
(bool, SendPort) response = await _jniIsolate.initJniIsolate(
|
||||
readyPort,
|
||||
_jniReceiverPort,
|
||||
supportDirectory,
|
||||
_getDylibDir(supportDirectory),
|
||||
_getClassPath(supportDirectory),
|
||||
);
|
||||
JAniyomiBridge.init();
|
||||
_jAniyomiBridge = JAniyomiBridge();
|
||||
_isReady = true;
|
||||
_isReady = response.$1;
|
||||
_jniSenderPort = response.$2;
|
||||
_jniReceiverPort.listen((message) {
|
||||
if (message is JniIsolateError) {
|
||||
final id = message.id;
|
||||
final completer = _pendingRequests.remove(id);
|
||||
if (completer == null) return;
|
||||
completer.completeError(message.error);
|
||||
} else if (message is JniIsolateResponse) {
|
||||
final id = message.id;
|
||||
final completer = _pendingRequests.remove(id);
|
||||
if (completer == null) return;
|
||||
if (message.results == null) {
|
||||
completer.complete();
|
||||
return;
|
||||
}
|
||||
completer.complete(message.results);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool isReady() {
|
||||
return _isReady;
|
||||
}
|
||||
|
||||
List<JSAnime> getAnimeSearchResults(String query, int page, String source) {
|
||||
JList<JObject?>? searchResults = _jAniyomiBridge.getAnimeSearchResults(
|
||||
JString.fromString(query),
|
||||
page,
|
||||
JString.fromString(source),
|
||||
);
|
||||
if (searchResults == null) {
|
||||
return [];
|
||||
}
|
||||
return searchResults
|
||||
.cast<JObject?>()
|
||||
.where(_jObjIsNotNull)
|
||||
.map((jObj) => jObj!.as<JSAnime>(JSAnime.type))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<JSManga> getMangaSearchResults(String query, int page, String source) {
|
||||
JList<JObject?>? searchResults = _jAniyomiBridge.getMangaSearchResults(
|
||||
JString.fromString(query),
|
||||
page,
|
||||
JString.fromString(source),
|
||||
);
|
||||
if (searchResults == null) {
|
||||
return [];
|
||||
}
|
||||
return searchResults
|
||||
.cast<JObject?>()
|
||||
.where(_jObjIsNotNull)
|
||||
.map((jObj) => jObj!.as<JSManga>(JSManga.type))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<JSEpisode> getEpisodeList(JSAnime sAnime, String source) {
|
||||
JList<JObject?>? episodeList = _jAniyomiBridge.getEpisodeList(
|
||||
sAnime,
|
||||
JString.fromString(source),
|
||||
);
|
||||
if (episodeList == null) {
|
||||
return [];
|
||||
}
|
||||
return episodeList
|
||||
.cast<JObject?>()
|
||||
.where(_jObjIsNotNull)
|
||||
.map((jObj) => jObj!.as<JSEpisode>(JSEpisode.type))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<JSChapter> getChapterList(JSManga sManga, String source) {
|
||||
JList<JObject?>? episodeList = _jAniyomiBridge.getEpisodeList(
|
||||
sManga,
|
||||
JString.fromString(source),
|
||||
);
|
||||
if (episodeList == null) {
|
||||
return [];
|
||||
}
|
||||
return episodeList
|
||||
.cast<JObject?>()
|
||||
.where(_jObjIsNotNull)
|
||||
.map((jObj) => jObj!.as<JSChapter>(JSChapter.type))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<JVideo> getVideoList(JSEpisode sEpisode, String source) {
|
||||
JList<JObject?>? videoList = _jAniyomiBridge.getVideoList(
|
||||
sEpisode,
|
||||
JString.fromString(source),
|
||||
);
|
||||
if (videoList == null) {
|
||||
return [];
|
||||
}
|
||||
return videoList
|
||||
.cast<JObject?>()
|
||||
.where(_jObjIsNotNull)
|
||||
.map((jObj) => jObj!.as<JVideo>(JVideo.type))
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<JPage> getPageList(JSChapter sChapter, String source) {
|
||||
JList<JObject?>? videoList = _jAniyomiBridge.getVideoList(
|
||||
sChapter,
|
||||
JString.fromString(source),
|
||||
);
|
||||
if (videoList == null) {
|
||||
return [];
|
||||
}
|
||||
return videoList
|
||||
.cast<JObject?>()
|
||||
.where(_jObjIsNotNull)
|
||||
.map((jObj) => jObj!.as<JPage>(JPage.type))
|
||||
.toList();
|
||||
}
|
||||
|
||||
void loadAnimeExtension(String extensionUrl) {
|
||||
_jAniyomiBridge.loadAnimeExtension(
|
||||
JString.fromString(extensionUrl),
|
||||
JString.fromString(
|
||||
path.join(_supportDirectoryPath, _aniyomiBridgeAnimeExtensionsDir),
|
||||
Future<List<JSAnime>> getAnimeSearchResults(String query, int page, String source) async {
|
||||
return _sendJniIsolateMessage<List<JSAnime>>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.getAnimeSearchResults,
|
||||
args: {
|
||||
'query': query,
|
||||
'page': page,
|
||||
'source': source,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void unloadAnimeExtension(String extensionName, String extensionVersion) {
|
||||
_jAniyomiBridge.unloadAnimeExtension(
|
||||
JString.fromString(extensionName),
|
||||
JString.fromString(extensionVersion),
|
||||
JString.fromString(
|
||||
path.join(_supportDirectoryPath, _aniyomiBridgeAnimeExtensionsDir),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
void loadMangaExtension(String extensionUrl) {
|
||||
_jAniyomiBridge.loadMangaExtension(
|
||||
JString.fromString(extensionUrl),
|
||||
JString.fromString(
|
||||
path.join(_supportDirectoryPath, _aniyomiBridgeMangaExtensionsDir),
|
||||
Future<List<JSManga>> getMangaSearchResults(String query, int page, String source) async {
|
||||
return _sendJniIsolateMessage<List<JSManga>>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.getMangaSearchResults,
|
||||
args: {
|
||||
'query': query,
|
||||
'page': page,
|
||||
'source': source,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void unloadMangaExtension(String extensionName, String extensionVersion) {
|
||||
_jAniyomiBridge.unloadMangaExtension(
|
||||
JString.fromString(extensionName),
|
||||
JString.fromString(extensionVersion),
|
||||
JString.fromString(
|
||||
path.join(_supportDirectoryPath, _aniyomiBridgeMangaExtensionsDir),
|
||||
)
|
||||
Future<List<JSEpisode>> getEpisodeList(JSAnime sAnime, String source) async {
|
||||
return _sendJniIsolateMessage<List<JSEpisode>>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.getEpisodeList,
|
||||
args: {
|
||||
'sAnime': sAnime,
|
||||
'source': source,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Set<Extension>> getInstalledAnimeExtensions() async{
|
||||
Future<List<JSChapter>> getChapterList(JSManga sManga, String source) async {
|
||||
return _sendJniIsolateMessage<List<JSChapter>>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.getChapterList,
|
||||
args: {
|
||||
'sManga': sManga,
|
||||
'source': source,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<JVideo>> getVideoList(JSEpisode sEpisode, String source) async {
|
||||
return _sendJniIsolateMessage<List<JVideo>>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.getVideoList,
|
||||
args: {
|
||||
'sEpisode': sEpisode,
|
||||
'source': source,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<List<JPage>> getPageList(JSChapter sChapter, String source) async {
|
||||
return _sendJniIsolateMessage<List<JPage>>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.getPageList,
|
||||
args: {
|
||||
'sChapter': sChapter,
|
||||
'source': source,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> loadAnimeExtension(String extensionUrl) async {
|
||||
return _sendJniIsolateMessage<void>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.loadAnimeExtension,
|
||||
args: {
|
||||
'extensionUrl': extensionUrl,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> unloadAnimeExtension(String extensionName, String extensionVersion) async {
|
||||
return _sendJniIsolateMessage<void>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.unloadAnimeExtension,
|
||||
args: {
|
||||
'extensionName': extensionName,
|
||||
'extensionVersion': extensionVersion,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> loadMangaExtension(String extensionUrl) async {
|
||||
return _sendJniIsolateMessage<void>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.loadMangaExtension,
|
||||
args: {
|
||||
'extensionUrl': extensionUrl,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> unloadMangaExtension(String extensionName, String extensionVersion) async {
|
||||
return _sendJniIsolateMessage<void>(
|
||||
JniIsolateMessage(
|
||||
id: _random.nextInt(1 << 32),
|
||||
type: JniIsolateMessageType.unloadMangaExtension,
|
||||
args: {
|
||||
'extensionName': extensionName,
|
||||
'extensionVersion': extensionVersion,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Set<Extension>> getInstalledAnimeExtensions() async {
|
||||
Directory animeExtDir = Directory(
|
||||
path.join(_supportDirectoryPath, _aniyomiBridgeAnimeExtensionsDir),
|
||||
);
|
||||
@@ -193,7 +234,7 @@ class AniyomiBridge {
|
||||
return extensions;
|
||||
}
|
||||
|
||||
Future<Set<Extension>> getInstalledMangaExtensions() async{
|
||||
Future<Set<Extension>> getInstalledMangaExtensions() async {
|
||||
Directory mangaExtDir = Directory(
|
||||
path.join(_supportDirectoryPath, _aniyomiBridgeMangaExtensionsDir),
|
||||
);
|
||||
@@ -212,33 +253,37 @@ class AniyomiBridge {
|
||||
return extensions;
|
||||
}
|
||||
|
||||
List<String>? getLoadedAnimeExtensions() {
|
||||
JList<JString?>? loadedExtensions = _jAniyomiBridge
|
||||
.getAnimeLoadedExtensions();
|
||||
if (loadedExtensions == null) {
|
||||
return null;
|
||||
}
|
||||
return loadedExtensions
|
||||
.cast<JString>()
|
||||
.map((jStr) => jStr.toDartString())
|
||||
.toList();
|
||||
}
|
||||
// List<String>? getLoadedAnimeExtensions() {
|
||||
// JList<JString?>? loadedExtensions = _jAniyomiBridge
|
||||
// .getAnimeLoadedExtensions();
|
||||
// if (loadedExtensions == null) {
|
||||
// return null;
|
||||
// }
|
||||
// return loadedExtensions
|
||||
// .cast<JString>()
|
||||
// .map((jStr) => jStr.toDartString())
|
||||
// .toList();
|
||||
// }
|
||||
//
|
||||
// List<String>? getLoadedMangaExtensions() {
|
||||
// JList<JString?>? loadedExtensions = _jAniyomiBridge
|
||||
// .getMangaLoadedExtensions();
|
||||
// if (loadedExtensions == null) {
|
||||
// return null;
|
||||
// }
|
||||
// return loadedExtensions
|
||||
// .cast<JString>()
|
||||
// .map((jStr) => jStr.toDartString())
|
||||
// .toList();
|
||||
// }
|
||||
|
||||
List<String>? getLoadedMangaExtensions() {
|
||||
JList<JString?>? loadedExtensions = _jAniyomiBridge
|
||||
.getMangaLoadedExtensions();
|
||||
if (loadedExtensions == null) {
|
||||
return null;
|
||||
}
|
||||
return loadedExtensions
|
||||
.cast<JString>()
|
||||
.map((jStr) => jStr.toDartString())
|
||||
.toList();
|
||||
Future<T> _sendJniIsolateMessage<T>(JniIsolateMessage message) {
|
||||
final completer = Completer<T>();
|
||||
_pendingRequests[message.id] = completer;
|
||||
_jniSenderPort.send(message);
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
bool Function(JObject? jObj) get _jObjIsNotNull =>
|
||||
(jObj) => jObj != null;
|
||||
|
||||
String _getDylibDir(Directory supportDirectory) {
|
||||
String executablePath = File(Platform.resolvedExecutable).parent.path;
|
||||
if (Platform.isLinux) {
|
||||
|
||||
Reference in New Issue
Block a user