|
1 | | -package com.example.bd2modmanager.data.repository |
2 | | - |
3 | | -import android.content.Context |
4 | | -import com.chaquo.python.Python |
5 | | -import com.example.bd2modmanager.data.model.CharacterInfo |
6 | | -import kotlinx.coroutines.Dispatchers |
7 | | -import kotlinx.coroutines.withContext |
8 | | -import org.json.JSONArray |
9 | | -import java.io.File |
10 | | - |
11 | | -class CharacterRepository(private val context: Context) { |
12 | | - |
13 | | - companion object { |
14 | | - private const val CHARACTERS_JSON_FILENAME = "characters.json" |
15 | | - private const val MOD_CACHE_FILENAME = "mod_cache.json" |
16 | | - } |
17 | | - |
18 | | - private var characterLut: Map<String, List<CharacterInfo>> = emptyMap() |
19 | | - |
20 | | - suspend fun updateCharacterData(): Boolean { |
21 | | - return withContext(Dispatchers.IO) { |
22 | | - var success = false |
23 | | - try { |
24 | | - if (!Python.isStarted()) { |
25 | | - Python.start(com.chaquo.python.android.AndroidPlatform(context)) |
26 | | - } |
27 | | - val py = Python.getInstance() |
28 | | - val mainScript = py.getModule("main_script") |
29 | | - val result = mainScript.callAttr("update_character_data", context.filesDir.absolutePath).asList() |
30 | | - val status = result[0].toString() // SUCCESS, SKIPPED, FAILED |
31 | | - val message = result[1].toString() |
32 | | - |
33 | | - when (status) { |
34 | | - "SUCCESS" -> { |
35 | | - println("Successfully ran scraper and saved characters.json: $message") |
36 | | - // When a new characters.json is generated, the mod cache becomes invalid. |
37 | | - val cacheFile = File(context.cacheDir, MOD_CACHE_FILENAME) |
38 | | - if (cacheFile.exists()) { |
39 | | - cacheFile.delete() |
40 | | - println("Deleted mod cache to force re-scan.") |
41 | | - } |
42 | | - success = true |
43 | | - } |
44 | | - "SKIPPED" -> { |
45 | | - println("Scraper skipped: $message") |
46 | | - success = true |
47 | | - } |
48 | | - "FAILED" -> { |
49 | | - println("Scraper script failed: $message. Will use local version if available.") |
50 | | - success = false |
51 | | - } |
52 | | - } |
53 | | - } catch (e: Exception) { |
54 | | - e.printStackTrace() |
55 | | - println("Failed to execute scraper python script, will use local version. Error: ${e.message}") |
56 | | - success = false |
57 | | - } |
58 | | - characterLut = parseCharacterJson() |
59 | | - success |
60 | | - } |
61 | | - } |
62 | | - |
63 | | - private suspend fun parseCharacterJson(): Map<String, List<CharacterInfo>> { |
64 | | - return withContext(Dispatchers.IO) { |
65 | | - val lut = mutableMapOf<String, MutableList<CharacterInfo>>() |
66 | | - val internalFile = File(context.filesDir, CHARACTERS_JSON_FILENAME) |
67 | | - if (!internalFile.exists()) return@withContext emptyMap() |
68 | | - val jsonString = try { internalFile.readText() } catch (e: Exception) { e.printStackTrace(); "" } |
69 | | - if (jsonString.isNotEmpty()) { |
70 | | - try { |
71 | | - // Try parsing new format first |
72 | | - val rootObject = org.json.JSONObject(jsonString) |
73 | | - val jsonArray = rootObject.getJSONArray("characters") |
74 | | - for (i in 0 until jsonArray.length()) { |
75 | | - val obj = jsonArray.getJSONObject(i) |
76 | | - val fileId = obj.getString("file_id").lowercase() |
77 | | - val charInfo = CharacterInfo(obj.getString("character"), obj.getString("costume"), obj.getString("type"), obj.getString("hashed_name")) |
78 | | - lut.getOrPut(fileId) { mutableListOf() }.add(charInfo) |
79 | | - } |
80 | | - } catch (e: org.json.JSONException) { |
81 | | - // Fallback to old format |
82 | | - try { |
83 | | - val jsonArray = JSONArray(jsonString) |
84 | | - for (i in 0 until jsonArray.length()) { |
85 | | - val obj = jsonArray.getJSONObject(i) |
86 | | - val fileId = obj.getString("file_id").lowercase() |
87 | | - val charInfo = CharacterInfo(obj.getString("character"), obj.getString("costume"), obj.getString("type"), obj.getString("hashed_name")) |
88 | | - lut.getOrPut(fileId) { mutableListOf() }.add(charInfo) |
89 | | - } |
90 | | - } catch (e2: Exception) { |
91 | | - e2.printStackTrace() |
92 | | - } |
93 | | - } catch (e: Exception) { |
94 | | - e.printStackTrace() |
95 | | - } |
96 | | - } |
97 | | - lut |
98 | | - } |
99 | | - } |
100 | | - |
101 | | - fun findBestMatch(fileId: String?, fileNames: List<String>): CharacterInfo? { |
102 | | - if (fileId == null) return null |
103 | | - |
104 | | - val candidates = characterLut[fileId] ?: return null |
105 | | - if (candidates.size == 1) return candidates.first() |
106 | | - if (candidates.isEmpty()) return null |
107 | | - |
108 | | - val hasCutsceneKeyword = fileNames.any { it.contains("cutscene", ignoreCase = true) } |
109 | | - if (hasCutsceneKeyword) { |
110 | | - candidates.find { it.type == "cutscene" }?.let { return it } |
111 | | - } |
112 | | - |
113 | | - val validHashCandidates = candidates.filter { !it.hashedName.isNullOrBlank() } |
114 | | - if (validHashCandidates.size == 1) { |
115 | | - return validHashCandidates.first() |
116 | | - } |
117 | | - |
118 | | - candidates.find { it.type == "idle" }?.let { return it } |
119 | | - |
120 | | - return candidates.first() |
121 | | - } |
122 | | - |
123 | | - fun extractFileId(entryName: String): String? { |
124 | | - return "(char\\d{6}|illust_dating\\d+|illust_special\\d+|illust_talk\\d+|npc\\d+|specialillust\\w+|storypack\\w+|\\bRhythmHitAnim\\b)".toRegex(RegexOption.IGNORE_CASE).find(entryName)?.value?.lowercase() |
125 | | - } |
126 | | -} |
| 1 | +package com.example.bd2modmanager.data.repository |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.chaquo.python.Python |
| 5 | +import com.example.bd2modmanager.data.model.CharacterInfo |
| 6 | +import kotlinx.coroutines.Dispatchers |
| 7 | +import kotlinx.coroutines.withContext |
| 8 | +import org.json.JSONArray |
| 9 | +import java.io.File |
| 10 | + |
| 11 | +class CharacterRepository(private val context: Context) { |
| 12 | + |
| 13 | + companion object { |
| 14 | + private const val CHARACTERS_JSON_FILENAME = "characters.json" |
| 15 | + private const val MOD_CACHE_FILENAME = "mod_cache.json" |
| 16 | + } |
| 17 | + |
| 18 | + private var characterLut: Map<String, List<CharacterInfo>> = emptyMap() |
| 19 | + |
| 20 | + suspend fun updateCharacterData(): Boolean { |
| 21 | + return withContext(Dispatchers.IO) { |
| 22 | + var success = false |
| 23 | + try { |
| 24 | + if (!Python.isStarted()) { |
| 25 | + Python.start(com.chaquo.python.android.AndroidPlatform(context)) |
| 26 | + } |
| 27 | + val py = Python.getInstance() |
| 28 | + val mainScript = py.getModule("main_script") |
| 29 | + val result = mainScript.callAttr("update_character_data", context.filesDir.absolutePath).asList() |
| 30 | + val status = result[0].toString() // SUCCESS, SKIPPED, FAILED |
| 31 | + val message = result[1].toString() |
| 32 | + |
| 33 | + when (status) { |
| 34 | + "SUCCESS" -> { |
| 35 | + println("Successfully ran scraper and saved characters.json: $message") |
| 36 | + // When a new characters.json is generated, the mod cache becomes invalid. |
| 37 | + val cacheFile = File(context.filesDir, MOD_CACHE_FILENAME) |
| 38 | + if (cacheFile.exists()) { |
| 39 | + cacheFile.delete() |
| 40 | + println("Deleted mod cache to force re-scan.") |
| 41 | + } |
| 42 | + success = true |
| 43 | + } |
| 44 | + "SKIPPED" -> { |
| 45 | + println("Scraper skipped: $message") |
| 46 | + success = true |
| 47 | + } |
| 48 | + "FAILED" -> { |
| 49 | + println("Scraper script failed: $message. Will use local version if available.") |
| 50 | + success = false |
| 51 | + } |
| 52 | + } |
| 53 | + } catch (e: Exception) { |
| 54 | + e.printStackTrace() |
| 55 | + println("Failed to execute scraper python script, will use local version. Error: ${e.message}") |
| 56 | + success = false |
| 57 | + } |
| 58 | + characterLut = parseCharacterJson() |
| 59 | + success |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private suspend fun parseCharacterJson(): Map<String, List<CharacterInfo>> { |
| 64 | + return withContext(Dispatchers.IO) { |
| 65 | + val lut = mutableMapOf<String, MutableList<CharacterInfo>>() |
| 66 | + val internalFile = File(context.filesDir, CHARACTERS_JSON_FILENAME) |
| 67 | + if (!internalFile.exists()) return@withContext emptyMap() |
| 68 | + val jsonString = try { internalFile.readText() } catch (e: Exception) { e.printStackTrace(); "" } |
| 69 | + if (jsonString.isNotEmpty()) { |
| 70 | + try { |
| 71 | + // Try parsing new format first |
| 72 | + val rootObject = org.json.JSONObject(jsonString) |
| 73 | + val jsonArray = rootObject.getJSONArray("characters") |
| 74 | + for (i in 0 until jsonArray.length()) { |
| 75 | + val obj = jsonArray.getJSONObject(i) |
| 76 | + val fileId = obj.getString("file_id").lowercase() |
| 77 | + val charInfo = CharacterInfo(obj.getString("character"), obj.getString("costume"), obj.getString("type"), obj.getString("hashed_name")) |
| 78 | + lut.getOrPut(fileId) { mutableListOf() }.add(charInfo) |
| 79 | + } |
| 80 | + } catch (e: org.json.JSONException) { |
| 81 | + // Fallback to old format |
| 82 | + try { |
| 83 | + val jsonArray = JSONArray(jsonString) |
| 84 | + for (i in 0 until jsonArray.length()) { |
| 85 | + val obj = jsonArray.getJSONObject(i) |
| 86 | + val fileId = obj.getString("file_id").lowercase() |
| 87 | + val charInfo = CharacterInfo(obj.getString("character"), obj.getString("costume"), obj.getString("type"), obj.getString("hashed_name")) |
| 88 | + lut.getOrPut(fileId) { mutableListOf() }.add(charInfo) |
| 89 | + } |
| 90 | + } catch (e2: Exception) { |
| 91 | + e2.printStackTrace() |
| 92 | + } |
| 93 | + } catch (e: Exception) { |
| 94 | + e.printStackTrace() |
| 95 | + } |
| 96 | + } |
| 97 | + lut |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + fun findBestMatch(fileId: String?, fileNames: List<String>): CharacterInfo? { |
| 102 | + if (fileId == null) return null |
| 103 | + |
| 104 | + val candidates = characterLut[fileId] ?: return null |
| 105 | + if (candidates.size == 1) return candidates.first() |
| 106 | + if (candidates.isEmpty()) return null |
| 107 | + |
| 108 | + val hasCutsceneKeyword = fileNames.any { it.contains("cutscene", ignoreCase = true) } |
| 109 | + if (hasCutsceneKeyword) { |
| 110 | + candidates.find { it.type == "cutscene" }?.let { return it } |
| 111 | + } |
| 112 | + |
| 113 | + val validHashCandidates = candidates.filter { !it.hashedName.isNullOrBlank() } |
| 114 | + if (validHashCandidates.size == 1) { |
| 115 | + return validHashCandidates.first() |
| 116 | + } |
| 117 | + |
| 118 | + candidates.find { it.type == "idle" }?.let { return it } |
| 119 | + |
| 120 | + return candidates.first() |
| 121 | + } |
| 122 | + |
| 123 | + fun extractFileId(entryName: String): String? { |
| 124 | + return "(char\\d{6}|illust_dating\\d+|illust_special\\d+|illust_talk\\d+|npc\\d+|specialillust\\w+|storypack\\w+|\\bRhythmHitAnim\\b)".toRegex(RegexOption.IGNORE_CASE).find(entryName)?.value?.lowercase() |
| 125 | + } |
| 126 | +} |
0 commit comments