Add converter.

This commit is contained in:
mii
2020-09-23 18:16:37 +09:00
parent 96e5c5e31e
commit 60e2be1030
73 changed files with 163 additions and 18 deletions

View File

@@ -1 +1 @@
D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\MainKt.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\SaveEditor.class D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Converter.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\MainKt.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\SaveEditor.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\Data.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\NBT.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\NBT_Byte.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\NBT_Compound.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\NBT_End.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\NBT_Int.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\NBT_Long.class;D:\Git\SaveEditor\build\classes\kotlin\main\codes\mii\SaveEditor\Types\NBT_String.class

View File

@@ -1,2 +1,2 @@
12 45
10 34

View File

@@ -1 +1 @@
<EFBFBD>! <EFBFBD>!<EFBFBD>$<EFBFBD>'<EFBFBD>#<EFBFBD>#<EFBFBD>

View File

@@ -1,7 +1,93 @@
package codes.mii.SaveEditor package codes.mii.SaveEditor
class Converter { import codes.mii.SaveEditor.Types.NBT_Byte
fun conver(byte: Byte) { import codes.mii.SaveEditor.Types.NBT_Compound
import codes.mii.SaveEditor.Types.NBT_End
import codes.mii.SaveEditor.Types.NBT_String
import java.io.File
import java.io.IOException
import java.nio.file.Files
class Converter {
fun convert(byte: ByteArray): NBT_Compound {
val result = NBT_Compound(0, "")
val byteSize = byte.size
var readingPosition = 0
while (byteSize > readingPosition) {
println(byte[readingPosition])
val nowByte = byte[readingPosition]
var nameLength = 0
val name = StringBuilder()
if (nowByte != 0.toByte()) {
var nameLengthText = ""
repeat(2) {
readingPosition++
nameLengthText += byte[readingPosition].toString(16)
}
nameLength = nameLengthText.toInt(16)
repeat (nameLength) {
readingPosition++
name.append(hexToAscii(byte[readingPosition].toString(16)))
}
}
when(nowByte) {
0.toByte() -> {
result.data.add(NBT_End())
readingPosition++
}
1.toByte() -> {
readingPosition++
result.data.add(NBT_Byte(nameLength.toByte(), name.toString(), byte[readingPosition]))
}
8.toByte() -> {
var textLengthText = ""
repeat(2) {
readingPosition++
textLengthText += byte[readingPosition].toString(16)
}
val textLength = textLengthText.toInt(16)
val text = StringBuilder()
repeat (textLength) {
readingPosition++
text.append(hexToAscii(byte[readingPosition].toString(16)))
}
result.data.add(NBT_String(nameLength.toByte(), name.toString(), textLength.toByte(), text.toString()))
}
10.toByte() -> {
result.data.add(NBT_Compound(nameLength.toByte(), name.toString()))
}
}
readingPosition++
}
return result
}
@Throws(IOException::class)
fun convertFile(file: File): ByteArray? {
return Files.readAllBytes(file.toPath())
}
private fun hexToAscii(hexStr: String): String? {
val output = StringBuilder("")
var i = 0
while (i < hexStr.length) {
val str = hexStr.substring(i, i + 2)
output.append(str.toInt(16).toChar())
i += 2
}
return output.toString()
} }
} }

View File

@@ -1,26 +1,41 @@
package codes.mii.SaveEditor package codes.mii.SaveEditor
import codes.mii.SaveEditor.Types.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.nio.file.Files import java.nio.file.Files
class SaveEditor { class SaveEditor {
val converter: Converter = Converter()
fun start(args: Array<String>) { fun start(args: Array<String>) {
println("Minecraft Save Editor.") println("Minecraft Save Editor.")
if (args.count() >= 1) { if (args.count() >= 1) {
var bytes = convertFile(File(args[0])) val bytes = converter.convertFile(File(args[0]))
var builder: StringBuilder = StringBuilder()
bytes?.forEach { val result = converter.convert(bytes!!)
val byteString = it.toString(16)
builder.append(if (byteString.length == 1) { "0$byteString" } else { byteString } + " ") result.data.forEach {
when(it) {
is NBT_String -> {
println("STRING, Name: ${it.name}, Data: ${it.data}")
}
is NBT_End -> {
println("END")
}
is NBT_Byte -> {
println("BYTE, Name: ${it.name}, Data: ${it.data}")
}
is NBT_Compound -> {
println("COMPOUND, Name: ${it.name}")
}
}
} }
println(builder.toString())
} }
} }
@Throws(IOException::class)
fun convertFile(file: File): ByteArray? {
return Files.readAllBytes(file.toPath())
}
} }

View File

@@ -2,5 +2,6 @@ package codes.mii.SaveEditor.Types
interface Data { interface Data {
val typeByte: Byte val typeByte: Byte
val nameLength: Byte
val name: String
} }

View File

@@ -0,0 +1,10 @@
package codes.mii.SaveEditor.Types
enum class NBT(val type: Data) {
End(NBT_End()),
Byte(NBT_Byte(0,"",0)),
Int(NBT_Int(0, "", 0)),
Long(NBT_Long(0, "", 0)),
String(NBT_String(0, "", 0, "tes")),
Compound(NBT_Compound(0, ""))
}

View File

@@ -0,0 +1,5 @@
package codes.mii.SaveEditor.Types
class NBT_Byte(override val nameLength: Byte, override val name: String, val data: Byte) : Data {
override val typeByte: Byte = 1
}

View File

@@ -0,0 +1,6 @@
package codes.mii.SaveEditor.Types
class NBT_Compound(override val nameLength: Byte, override val name: String) : Data {
override val typeByte: Byte = 10
val data: MutableList<Data> = mutableListOf()
}

View File

@@ -0,0 +1,7 @@
package codes.mii.SaveEditor.Types
class NBT_End : Data {
override val name: String = "End"
override val nameLength: Byte = 0
override val typeByte: Byte = 0
}

View File

@@ -0,0 +1,5 @@
package codes.mii.SaveEditor.Types
class NBT_Int(override val nameLength: Byte, override val name: String, val data: Int) : Data {
override val typeByte: Byte = 3
}

View File

@@ -0,0 +1,5 @@
package codes.mii.SaveEditor.Types
class NBT_Long(override val nameLength: Byte, override val name: String, val data: Long) : Data {
override val typeByte: Byte = 4
}

View File

@@ -0,0 +1,5 @@
package codes.mii.SaveEditor.Types
class NBT_String(override val nameLength: Byte, override val name: String, val length: Byte, val data: String) : Data {
override val typeByte: Byte = 8
}