fix felica template read

master
wlt233 2 years ago
parent 01926e3081
commit 51dd062bfa

@ -9,7 +9,6 @@ import android.widget.Toast
class EmuCard : HostNfcFService() {
private lateinit var card: FelicaCard
// byte utils
fun byteArrayOfInts(vararg ints: Int) = ByteArray(ints.size) { pos -> ints[pos].toByte() }
fun ByteArray.toHexString(hasSpace: Boolean = true) = this.joinToString("") {
@ -80,7 +79,7 @@ class EmuCard : HostNfcFService() {
Log.d("HCEFService", "onCreate NFCF")
super.onCreate()
val globalVar = this.applicationContext as GlobalVar
card = FelicaCard(globalVar.IDm)
card = FelicaCard(this, globalVar.IDm)
// Toast.makeText(this, "onCreate", Toast.LENGTH_LONG).show()
}

@ -1,58 +1,45 @@
package moe.tqlwsl.aicemu
import android.app.Application
import android.content.Context
import android.util.Log
import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import java.io.BufferedReader
import java.io.File
import java.io.IOException
import java.io.InputStreamReader
class FelicaCard(IDm: String) {
class FelicaCard(context: Context, IDm: String) {
private var cardData = mutableMapOf<String, String>()
private val gson = Gson()
private val TAG = "AICEmu"
private val fileName = "felica_template.json"
init {
// instance = this
// val fileName = "felica_template.json"
// Log.d("HCEFService", "Load Felica Data from assets/$fileName")
// val fileContent = readAssetFile(applicationContext, fileName)
// val mutableBlock = object : TypeToken<MutableMap<String, String>>() {}.type
// try {
// val jsonData = gson.fromJson<MutableMap<String, String>>(fileContent, mutableBlock)
// if (jsonData != null) {
// cardData = jsonData
// val block82 = ByteArray(16)
// val IDmBytes = IDm.decodeHex()
// System.arraycopy(IDmBytes, 0, block82, 0, 16)
// cardData["82"] = block82.toHexString()
// }
val block82 = ByteArray(16)
Log.d("HCEFService","Felica Card $IDm")
val IDmBytes = IDm.decodeHex()
System.arraycopy(IDmBytes, 0, block82, 0, 8)
cardData["82"] = block82.toHexString()
val block = ByteArray(16)
cardData["80"] = block.toHexString()
cardData["86"] = block.toHexString()
cardData["90"] = block.toHexString()
cardData["91"] = block.toHexString()
cardData["00"] = block.toHexString()
Log.d("HCEFService","Felica Card Data")
Log.d("HCEFService","=".repeat(53))
for (en in cardData.entries) {
Log.d("HCEFService","[${en.key}]: ${en.value}")
Log.d(TAG, "Load Felica Data from assets/$fileName")
val fileContent = readAssetFile(context, fileName)
val mutableBlock = object : TypeToken<MutableMap<String, String>>() {}.type
try {
val jsonData = gson.fromJson<MutableMap<String, String>>(fileContent, mutableBlock)
if (jsonData != null) {
cardData = jsonData
val block82 = ByteArray(16)
val IDmBytes = IDm.decodeHex()
System.arraycopy(IDmBytes, 0, block82, 0, 8)
cardData["82"] = block82.toHexString()
}
Log.d(TAG,"[Felica Card Data]")
Log.d(TAG, "=".repeat(53))
for (en in cardData.entries) {
Log.d(TAG,"[${en.key}]: ${en.value}")
}
Log.d(TAG, "=".repeat(53))
} catch (e: IOException) {
Log.e(TAG, "assets/$fileName Read Error")
} catch (e: JsonSyntaxException) {
Log.e(TAG, "assets/$fileName Syntax Error")
}
Log.d("HCEFService","=".repeat(53))
// } catch (e: IOException) {
// Log.e("Error", "File Read Error")
// } catch (e: JsonSyntaxException) {
// Log.e("Error", "File Syntax Error")
// }
}
// utils
@ -76,33 +63,23 @@ class FelicaCard(IDm: String) {
System.arraycopy(blockData, 0, resp, 0, blockData.size)
return resp
}
Log.e("Error", "Invalid Block")
Log.e(TAG, "Invalid Block $idStr")
return null
}
fun writeBlock(id: Byte, data: ByteArray) { }
fun readAssetFile(context: Context, fileName: String): String {
private fun readAssetFile(context: Context, fileName: String): String {
val stringBuilder = StringBuilder()
// 获取AssetManager
val assetManager = context.assets
// 通过AssetManager打开文件
val inputStream = assetManager.open(fileName)
val bufferedReader = BufferedReader(InputStreamReader(inputStream))
// 逐行读取文件内容并将其添加到StringBuilder中
bufferedReader.forEachLine { line ->
stringBuilder.append(line)
stringBuilder.append("\n")
}
// 关闭流资源
bufferedReader.close()
inputStream.close()
return stringBuilder.toString()
}
}

@ -3,5 +3,5 @@ package moe.tqlwsl.aicemu
import android.app.Application
class GlobalVar : Application() {
var IDm: String = ""
var IDm: String = "02fe000000000000"
}
Loading…
Cancel
Save