mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-07 19:27:25 +01:00
Allow multiple sources in each extension source apk (#761)
* Allow multiple sources in each extension source apk Minor code cleanup * Add runtime defined sources functionality * Undo extensions library major version number bump
This commit is contained in:
parent
8df3080e0d
commit
b7b83305b2
@ -0,0 +1,12 @@
|
|||||||
|
package eu.kanade.tachiyomi.source
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A factory for creating sources at runtime.
|
||||||
|
*/
|
||||||
|
interface SourceFactory {
|
||||||
|
/**
|
||||||
|
* Create a new copy of the sources
|
||||||
|
* @return The created sources
|
||||||
|
*/
|
||||||
|
fun createSources(): List<Source>
|
||||||
|
}
|
@ -74,7 +74,7 @@ open class SourceManager(private val context: Context) {
|
|||||||
val map = file.inputStream().use { yaml.loadAs(it, Map::class.java) }
|
val map = file.inputStream().use { yaml.loadAs(it, Map::class.java) }
|
||||||
sources.add(YamlHttpSource(map))
|
sources.add(YamlHttpSource(map))
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e("Error loading source from file. Bad format?")
|
Timber.e("Error loading source from file. Bad format?", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,25 +95,29 @@ open class SourceManager(private val context: Context) {
|
|||||||
val extName = pkgManager.getApplicationLabel(appInfo).toString()
|
val extName = pkgManager.getApplicationLabel(appInfo).toString()
|
||||||
.substringAfter("Tachiyomi: ")
|
.substringAfter("Tachiyomi: ")
|
||||||
val version = pkgInfo.versionName
|
val version = pkgInfo.versionName
|
||||||
var sourceClass = appInfo.metaData.getString(METADATA_SOURCE_CLASS)
|
val sourceClasses = appInfo.metaData.getString(METADATA_SOURCE_CLASS)
|
||||||
if (sourceClass.startsWith(".")) {
|
.split(";")
|
||||||
sourceClass = pkgInfo.packageName + sourceClass
|
.map {
|
||||||
|
val sourceClass = it.trim()
|
||||||
|
if(sourceClass.startsWith("."))
|
||||||
|
pkgInfo.packageName + sourceClass
|
||||||
|
else
|
||||||
|
sourceClass
|
||||||
}
|
}
|
||||||
|
|
||||||
val extension = Extension(extName, appInfo, version, sourceClass)
|
val extension = Extension(extName, appInfo, version, sourceClasses)
|
||||||
try {
|
try {
|
||||||
val instance = loadExtension(extension)
|
sources += loadExtension(extension)
|
||||||
sources.add(instance)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Timber.e("Extension load error: $extName. Reason: ${e.message}")
|
Timber.e("Extension load error: $extName.", e)
|
||||||
} catch (e: LinkageError) {
|
} catch (e: LinkageError) {
|
||||||
Timber.e("Extension load error: $extName. Reason: ${e.message}")
|
Timber.e("Extension load error: $extName.", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sources
|
return sources
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadExtension(ext: Extension): Source {
|
private fun loadExtension(ext: Extension): List<Source> {
|
||||||
// Validate lib version
|
// Validate lib version
|
||||||
val majorLibVersion = ext.version.substringBefore('.').toInt()
|
val majorLibVersion = ext.version.substringBefore('.').toInt()
|
||||||
if (majorLibVersion < LIB_VERSION_MIN || majorLibVersion > LIB_VERSION_MAX) {
|
if (majorLibVersion < LIB_VERSION_MIN || majorLibVersion > LIB_VERSION_MAX) {
|
||||||
@ -122,13 +126,20 @@ open class SourceManager(private val context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val classLoader = PathClassLoader(ext.appInfo.sourceDir, null, context.classLoader)
|
val classLoader = PathClassLoader(ext.appInfo.sourceDir, null, context.classLoader)
|
||||||
return Class.forName(ext.sourceClass, false, classLoader).newInstance() as Source
|
return ext.sourceClasses.flatMap {
|
||||||
|
val obj = Class.forName(it, false, classLoader).newInstance()
|
||||||
|
when(obj) {
|
||||||
|
is Source -> listOf(obj)
|
||||||
|
is SourceFactory -> obj.createSources()
|
||||||
|
else -> throw Exception("Unknown source class type!")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Extension(val name: String,
|
class Extension(val name: String,
|
||||||
val appInfo: ApplicationInfo,
|
val appInfo: ApplicationInfo,
|
||||||
val version: String,
|
val version: String,
|
||||||
val sourceClass: String)
|
val sourceClasses: List<String>)
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
const val EXTENSION_FEATURE = "tachiyomi.extension"
|
const val EXTENSION_FEATURE = "tachiyomi.extension"
|
||||||
|
Loading…
Reference in New Issue
Block a user