com.xml.guard.model.CustomStringMapping.kt Maven / Gradle / Ivy
package com.xml.guard.model
import com.xml.guard.entensions.GuardExtension
import com.xml.guard.utils.getRandomString
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
class CustomStringMapping(private val guardExtension: GuardExtension) {
private val customStringMap by lazy { guardExtension.customStringMap }
internal val customStringMapping by lazy { mutableMapOf() }
/**
* 混淆自定义字符
*/
fun obfuscateCustomString(): Map {
return customStringMap.mapValues { (rawName, isLowercase) ->
obfuscateCustomString(rawName, isLowercase)
}
}
/**
* 将映射写入文件
*/
fun writeMappingToFile(mappingFile: File) {
val writer = BufferedWriter(FileWriter(mappingFile, false))
for ((key, value) in customStringMapping) {
writer.write(String.format("%s -> %s\n", key, value))
}
writer.flush()
writer.close()
}
private fun obfuscateCustomString(rawName: String, isLowercase: Boolean): String {
var obfuscateName = customStringMapping[rawName]
if (obfuscateName == null) {
val value = MappingParser.checkObfuscate(rawName, isLowercase = isLowercase)
obfuscateName = value.ifBlank {
getRandomString {
val result = if (isLowercase) it.lowercase() else it
MappingParser.verifyObfuscateName(result)
}
}
customStringMapping[rawName] = obfuscateName
}
return obfuscateName
}
}