Skip to content

Commit

Permalink
Fix text editing
Browse files Browse the repository at this point in the history
Text editing works rather well. You can't change the length of the text
which is kinda disappointing.

Signed-off-by: Jadon Fowler <jadonflower@gmail.com>
  • Loading branch information
phase committed Aug 26, 2017
1 parent 977d85d commit 2221728
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/main/kotlin/xyz/jadonfowler/sbhs/SBHS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ object SBHS {
// About Page
val t = JTextPane()
t.text = "Sonic Battle Hack Suite $VERSION was made by Phase.\n" +
"You can find the source at https://github.com/phase/sbhs"
"You can find the source at https://github.com/phase/sbhs\n" +
"Current ROM open: $gameLocation"
t.isEditable = false
mainTabs.addTab("About", null, t, "About Page")
}
Expand Down
29 changes: 18 additions & 11 deletions src/main/kotlin/xyz/jadonfowler/sbhs/SBString.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,32 @@ object SBString {

fun to(s: String): IntArray {
var s = s
s = s.replace("\n\n", "]^")
s = s.replace("\n------\n", "]^")
s = s.replace("\n", "[^")
println(s)
val to = IntArray(s.length * 2 - 1)
var i = 0
var k = 0
while (i < s.length * 2) {
val c = s.toCharArray()[k++]
val chars = s.toCharArray()
while (k < chars.size) {
val c = chars[k++]
if (!TABLE.keys.contains(c)) {
i -= 2
i += 2
continue
}
to[i] = (TABLE[c] as Int).toByte().toInt()

This comment has been minimized.

Copy link
@phase

phase Oct 17, 2017

Author Owner

I would like to claim that this horrific code came from the automatic Java -> Kotlin conversion, and I did not intentionally cast a number from Int to Byte to Int.

val b = TABLE[c]!!
to[i] = b
try { // Should fail for the last one
to[i + 1] = 0 // 0x00
if (b == 0xFB || b == 0xFD || b == 0xFE || b == 0xFF) {
i--
} else {
to[i + 1] = 0 // 0x00
}
} catch (e: Exception) {
}

i += 2
}
println("Produced: ${to.map { Integer.toHexString(it).toUpperCase() }.joinToString(" ")}")
return to
}

Expand All @@ -142,7 +148,7 @@ object SBString {
continue
} else if (i == 0xFF) {
when (last) {
0xFE -> s += "\n\n"
0xFE -> s += "\n------\n"
0xFD -> s += "\n"
else -> {
}
Expand All @@ -151,21 +157,22 @@ object SBString {
if (REVERSE_TABLE.containsKey(i)) {
s += REVERSE_TABLE[i]
last = -1
continue
}
}/*
* else if (i == 0xFE || i == 0xFD) { last = i; continue;// ignore }
*/
last = i
}
return s.replace("]]", "[").replace("]", "\n\n").replace("[", "\n").replace("\\s\\s\\s".toRegex(), "")
return s.replace("]]", "[").replace("]", "\n------\n").replace("[", "\n").replace("\\s\\s\\s".toRegex(), "")
}

fun convert(last: Int, i: Int): String {
if (i == 0 && last == 0) {
return " "
} else if (i == 0xFF) {
when (last) {
0xFE -> return "\n\n"
0xFE -> return "\n------\n"
0xFD -> return "\n"
else -> {
}
Expand All @@ -175,4 +182,4 @@ object SBString {
}
return ""
}
}
}
31 changes: 14 additions & 17 deletions src/main/kotlin/xyz/jadonfowler/sbhs/TextManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ object TextManager {
}
}
s = s.replace("]", "").replace("[", "")
s = s.replace("{", "{^")
// System.out.println(s);
//int[] conv = SBString.to(s);
// System.out.println(orig.length + "\n" + conv.length + "\n" + "They
Expand All @@ -50,25 +51,21 @@ object TextManager {
textArea.text = s
// System.out.println(hex(Math.abs(from - to - SBString.to(s).length)));
val write = JButton("Write to ROM")
write.addActionListener(object : ActionListener {
internal val t = to.toByte()
internal val f = from.toByte()

override fun actionPerformed(e: ActionEvent) {
try {
var j = 0
val text = SBString.to(textArea.text)
for (i in t..f - 1) {
SBHS.raf.seek(i.toLong())
SBHS.raf.write(text[j])
j++
}
} catch (E: Exception) {
E.printStackTrace()
write.addActionListener { e: ActionEvent ->
try {
var j = 0
val text = SBString.to(textArea.text)
for (i in to..from - 1) {
// println("0x" + Integer.toHexString(i))
SBHS.raf.seek(i.toLong())
SBHS.raf.write(text[j])
j++
}

} catch (E: Exception) {
E.printStackTrace()
}
})

}
// p.add(write);
val sp = JScrollPane(textArea)
// sp.setBounds(23, 3, 394, 20);
Expand Down

0 comments on commit 2221728

Please sign in to comment.