Skip to content

Commit

Permalink
Improve random serial duplicator flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
AbandonedCart committed Sep 19, 2023
1 parent 529a395 commit 9b88170
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 171 deletions.
113 changes: 48 additions & 65 deletions app/src/main/java/com/hiddenramblings/tagmo/BrowserActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import com.hiddenramblings.tagmo.nfctech.Foomiibo
import com.hiddenramblings.tagmo.nfctech.NfcActivity
import com.hiddenramblings.tagmo.nfctech.ScanTag
import com.hiddenramblings.tagmo.nfctech.TagArray
import com.hiddenramblings.tagmo.nfctech.TagArray.withRandomSerials
import com.hiddenramblings.tagmo.nfctech.TagReader
import com.hiddenramblings.tagmo.qrcode.QRCodeScanner
import com.hiddenramblings.tagmo.update.UpdateManager
Expand Down Expand Up @@ -973,19 +974,20 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener,

private fun exportWithRandomSerial(amiiboFile: AmiiboFile?, tagData: ByteArray?, count: Int) {
CoroutineScope(Dispatchers.IO).launch {
val amiiboList = amiiboFile?.withRandomSerials(keyManager, count)
val amiiboList = amiiboFile?.let {
TagArray.getValidatedAmiibo(keyManager, it).withRandomSerials(count, keyManager)
} ?: tagData?.withRandomSerials(count, keyManager)
amiiboList?.forEachIndexed { index, amiiboData ->
try {
val outputData = keyManager.encrypt(amiiboData.array)
writeFlipperFile(index, outputData)
} catch (ex: Exception) {
Debug.warn(ex)
withContext(Dispatchers.Main) {
IconifiedSnackbar(this@BrowserActivity, viewPager).buildSnackbar(
getString(R.string.fail_randomize), Snackbar.LENGTH_SHORT
).show()
}
}
} ?: withContext(Dispatchers.Main) {
IconifiedSnackbar(this@BrowserActivity, viewPager).buildSnackbar(
getString(R.string.fail_randomize), Snackbar.LENGTH_SHORT
).show()
}
}
}
Expand All @@ -1010,12 +1012,14 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener,
var needsReload = false

val fileName = TagArray.decipherFilename(settings?.amiiboManager, tagData, true)
val amiiboList = amiiboFile?.withRandomSerials(keyManager, count)
val amiiboList = amiiboFile?.let {
TagArray.getValidatedAmiibo(keyManager, it).withRandomSerials(count, keyManager)
} ?: tagData?.withRandomSerials(count, keyManager)
amiiboList?.forEachIndexed { index, amiiboData ->
val name = "${fileName}_$index"
try {
val outputData = keyManager.encrypt(amiiboData.array)
val outputFile: AmiiboFile? = if (cached) {
if (cached) {
val path = TagArray.writeBytesToFile(directory, name, outputData)
AmiiboFile(File(path), amiiboData.amiiboID, outputData)
} else {
Expand All @@ -1028,21 +1032,16 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener,
AmiiboFile(File(it), amiiboData.amiiboID, amiiboData.array)
}
}
if (!needsReload) needsReload = null != outputFile
} catch (ex: Exception) {
Debug.warn(ex)
}
}
if (needsReload) {
withContext(Dispatchers.Main) {
onRootFolderChanged(true)
}
} else {
withContext(Dispatchers.Main) {
IconifiedSnackbar(this@BrowserActivity, viewPager).buildSnackbar(
} ?: withContext(Dispatchers.Main) {
IconifiedSnackbar(this@BrowserActivity, viewPager).buildSnackbar(
getString(R.string.fail_randomize), Snackbar.LENGTH_SHORT
).show()
}
).show()
}
withContext(Dispatchers.Main) {
onRootFolderChanged(true)
}
}
}
Expand Down Expand Up @@ -2403,22 +2402,18 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener,
hideFakeSnackbar()
onShowSettingsFragment()
} else {
coroutineScope {
uris.map { uri ->
async(Dispatchers.IO) {
coroutineScope { uris.map { uri -> async(Dispatchers.IO) {
try {
contentResolver.openInputStream(uri)?.use { inputStream ->
try {
contentResolver.openInputStream(uri)?.use { inputStream ->
try {
keyManager.evaluateKey(inputStream)
} catch (ex: Exception) {
onShowSettingsFragment()
}
hideFakeSnackbar()
}
} catch (e: Exception) { Debug.warn(e) }
keyManager.evaluateKey(inputStream)
} catch (ex: Exception) {
onShowSettingsFragment()
}
hideFakeSnackbar()
}
}.awaitAll()
}
} catch (e: Exception) { Debug.warn(e) }
} }.awaitAll() }
if (keyManager.isKeyMissing) {
onShowSettingsFragment()
hideFakeSnackbar()
Expand All @@ -2442,30 +2437,22 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener,
onShowSettingsFragment()
}
} else {
coroutineScope {
directories.map {
async(Dispatchers.IO) {
if (it.isDirectory) locateKeyFilesRecursive(it, false)
}
}.awaitAll()
}
coroutineScope { directories.map { file -> async(Dispatchers.IO) {
if (file.isDirectory) locateKeyFilesRecursive(file, false)
} }.awaitAll() }
}
}
} else {
coroutineScope {
files.map { file ->
async(Dispatchers.IO) {
coroutineScope { files.map { file -> async(Dispatchers.IO) {
try {
FileInputStream(file).use { inputStream ->
try {
FileInputStream(file).use { inputStream ->
try {
keyManager.evaluateKey(inputStream)
} catch (ignored: Exception) { }
}
} catch (e: Exception) { Debug.warn(e) }
keyManager.evaluateKey(inputStream)
} catch (ignored: Exception) { }
}
}
}.awaitAll()
}
} catch (e: Exception) { Debug.warn(e) }
}
} }.awaitAll() }
if (keyManager.isKeyMissing) {
onShowSettingsFragment()
hideFakeSnackbar()
Expand All @@ -2490,21 +2477,17 @@ class BrowserActivity : AppCompatActivity(), BrowserSettingsListener,
else
locateKeyFilesRecursive(Storage.getFile(prefs.preferEmulated()), true)
} else {
coroutineScope {
files.map { file ->
async(Dispatchers.IO) {
coroutineScope { files.map { file -> async(Dispatchers.IO) {
try {
FileInputStream(file).use { inputStream ->
try {
FileInputStream(file).use { inputStream ->
try {
keyManager.evaluateKey(inputStream)
} catch (ignored: Exception) { }
}
} catch (e: Exception) {
Debug.warn(e)
}
keyManager.evaluateKey(inputStream)
} catch (ignored: Exception) { }
}
}.awaitAll()
}
} catch (e: Exception) {
Debug.warn(e)
}
} }.awaitAll() }
if (keyManager.isKeyMissing) {
onShowSettingsFragment()
hideFakeSnackbar()
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/hiddenramblings/tagmo/NFCIntent.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.hiddenramblings.tagmo

import android.content.ComponentName
import android.content.Intent
import com.hiddenramblings.tagmo.eightbit.os.Version

object NFCIntent {
@JvmStatic
Expand Down
51 changes: 0 additions & 51 deletions app/src/main/java/com/hiddenramblings/tagmo/amiibo/AmiiboFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ package com.hiddenramblings.tagmo.amiibo
import android.os.Parcel
import android.os.Parcelable
import androidx.documentfile.provider.DocumentFile
import com.hiddenramblings.tagmo.amiibo.tagdata.AmiiboData
import com.hiddenramblings.tagmo.eightbit.io.Debug
import com.hiddenramblings.tagmo.eightbit.os.Version
import com.hiddenramblings.tagmo.nfctech.Foomiibo
import com.hiddenramblings.tagmo.nfctech.TagArray
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import java.io.File

open class AmiiboFile : Parcelable {
Expand Down Expand Up @@ -55,49 +47,6 @@ open class AmiiboFile : Parcelable {
data = ByteArray(parcel.readInt()).also { parcel.readByteArray(it) }
}

suspend fun withRandomSerials(keyManager: KeyManager, count: Int) : ArrayList<AmiiboData> {
val dataList: ArrayList<AmiiboData> = arrayListOf()
coroutineScope {
val tagData = TagArray.getValidatedAmiibo(keyManager, this@AmiiboFile)
(0 until count).map {
async(Dispatchers.IO) {
try {
AmiiboData(keyManager.decrypt(tagData)).apply {
uID = Foomiibo.generateRandomUID()
}.also {
dataList.add(it)
}
} catch (e: Exception) {
Debug.warn(e)
}
}
}.awaitAll()
}
return dataList
}

suspend fun withRandomSerials(count: Int) : ArrayList<AmiiboData> {
val dataList: ArrayList<AmiiboData> = arrayListOf()
this@AmiiboFile.data?.let { tagData ->
coroutineScope {
(0 until count).map {
async(Dispatchers.IO) {
try {
AmiiboData(tagData).apply {
uID = Foomiibo.generateRandomUID()
}.also {
dataList.add(it)
}
} catch (e: Exception) {
Debug.warn(e)
}
}
}.awaitAll()
}
}
return dataList
}

companion object {
@JvmField
val CREATOR: Parcelable.Creator<AmiiboFile?> = object : Parcelable.Creator<AmiiboFile?> {
Expand Down
54 changes: 23 additions & 31 deletions app/src/main/java/com/hiddenramblings/tagmo/amiibo/AmiiboManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,18 @@ object AmiiboManager {
val amiiboFiles = ArrayList<AmiiboFile?>()
val files = rootFolder?.listFiles { _: File?, name: String -> binFileMatches(name) }
if (!files.isNullOrEmpty()) {
coroutineScope {
files.map { file ->
async(Dispatchers.IO) {
try {
TagArray.getValidatedFile(keyManager, file).also { data ->
amiiboFiles.add(AmiiboFile(file, Amiibo.dataToId(data), data))
}
} catch (e: Exception) {
if (Debug.getExceptionClass(e) == IOException().javaClass.name)
Debug.verbose(e)
else
Debug.info(e)
}
coroutineScope { files.map { file -> async(Dispatchers.IO) {
try {
TagArray.getValidatedFile(keyManager, file).also { data ->
amiiboFiles.add(AmiiboFile(file, Amiibo.dataToId(data), data))
}
}.awaitAll()
}
} catch (e: Exception) {
if (Debug.getExceptionClass(e) == IOException().javaClass.name)
Debug.verbose(e)
else
Debug.info(e)
}
} }.awaitAll() }
} else if (recursiveFiles) {
val directories = rootFolder?.listFiles()
if (directories.isNullOrEmpty()) return amiiboFiles
Expand All @@ -360,24 +356,20 @@ object AmiiboManager {
return arrayListOf<AmiiboFile?>().apply {
val uris = context?.let { AmiiboDocument(it).listFiles(rootFolder.uri, recursiveFiles) }
if (uris.isNullOrEmpty()) return this
coroutineScope {
uris.map { uri ->
async(Dispatchers.IO) {
try {
TagArray.getValidatedDocument(keyManager, uri).also { data ->
DocumentFile.fromSingleUri(context, uri).also {
add(AmiiboFile(it, Amiibo.dataToId(data), data))
}
}
} catch (e: Exception) {
if (Debug.getExceptionClass(e) == IOException().javaClass.name)
Debug.verbose(e)
else
Debug.info(e)
coroutineScope { uris.map { uri -> async(Dispatchers.IO) {
try {
TagArray.getValidatedDocument(keyManager, uri).also { data ->
DocumentFile.fromSingleUri(context, uri).also {
add(AmiiboFile(it, Amiibo.dataToId(data), data))
}
}
}.awaitAll()
}
} catch (e: Exception) {
if (Debug.getExceptionClass(e) == IOException().javaClass.name)
Debug.verbose(e)
else
Debug.info(e)
}
} }.awaitAll() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.hiddenramblings.tagmo.amiibo
import android.util.Base64
import com.hiddenramblings.tagmo.R
import com.hiddenramblings.tagmo.TagMo
import com.hiddenramblings.tagmo.nfctech.TagArray
import com.hiddenramblings.tagmo.nfctech.TagArray.toHex
import org.json.JSONException
import org.json.JSONObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
package com.hiddenramblings.tagmo.amiibo.tagdata

import com.hiddenramblings.tagmo.nfctech.TagArray

class AppDataMLPaperJam(appData: ByteArray?) : AppData(appData!!) {

private val sparkleCardHex = "FF FF FF FF FF FF FF"
Expand Down
Loading

0 comments on commit 9b88170

Please sign in to comment.