@@ -22,7 +22,7 @@ import java.nio.charset.CodingErrorAction
2222import java.nio.file.Path
2323import java.nio.file.StandardOpenOption
2424
25- class VmdLoadException (message : String ) : Exception(message)
25+ class VmdLoadException (message : String , cause : Throwable ? = null ) : Exception(message, cause )
2626
2727private inline fun IntIterable.forEachInt (action : (Int ) -> Unit ) {
2828 val iterator = intIterator()
@@ -56,8 +56,8 @@ class VmdLoader : ModelFileLoader {
5656
5757 private val SHIFT_JIS = Charset .forName(" Shift-JIS" )
5858 private val decoder = SHIFT_JIS .newDecoder()
59- .onMalformedInput(CodingErrorAction .REPORT )
60- .onUnmappableCharacter(CodingErrorAction .REPORT )
59+ .onMalformedInput(CodingErrorAction .REPLACE )
60+ .onUnmappableCharacter(CodingErrorAction .REPLACE )
6161
6262 private fun loadString (buffer : ByteBuffer , maxLength : Int ): String {
6363 val bytes = ByteBuffer .allocate(maxLength)
@@ -67,7 +67,14 @@ class VmdLoader : ModelFileLoader {
6767 .indexOfFirst { bytes.get(it) == 0 .toByte() }
6868 .takeIf { it != - 1 } ? : maxLength
6969 val stringBytes = bytes.slice(0 , nullIndex).order(ByteOrder .LITTLE_ENDIAN )
70- return decoder.decode(stringBytes).toString()
70+ return try {
71+ decoder.decode(stringBytes).toString()
72+ } catch (ex: Exception ) {
73+ val hexBytes = (0 until nullIndex).joinToString(" " ) {
74+ " %02X" .format(bytes.get(it))
75+ }
76+ throw VmdLoadException (" Failed to decode string for byte $hexBytes " , ex)
77+ }
7178 }
7279
7380 private fun loadHeader (buffer : ByteBuffer ) {
0 commit comments