Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkAtra committed Jun 17, 2021
1 parent 206c080 commit 026cba1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
16 changes: 8 additions & 8 deletions core/src/main/kotlin/de/darkatra/bfme2/ConversionExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ fun Boolean.toByte(): Byte = when (this) {
// Bytes to Data
fun ByteArray.toBigEndianShort(): Short = ByteBuffer.wrap(this).order(ByteOrder.BIG_ENDIAN).short
fun ByteArray.toLittleEndianShort(): Short = ByteBuffer.wrap(this).order(ByteOrder.LITTLE_ENDIAN).short
fun ByteArray.toBigEndianUShort(): UShort = toBigEndianUInt().toUShort()
fun ByteArray.toLittleEndianUShort(): UShort = toLittleEndianUInt().toUShort()
fun ByteArray.toBigEndianUShort(): UShort = toBigEndianShort().toUShort()
fun ByteArray.toLittleEndianUShort(): UShort = toLittleEndianShort().toUShort()

fun ByteArray.toBigEndianInt(): Int = ByteBuffer.wrap(this).order(ByteOrder.BIG_ENDIAN).int
fun ByteArray.toLittleEndianInt(): Int = ByteBuffer.wrap(this).order(ByteOrder.LITTLE_ENDIAN).int
fun ByteArray.toBigEndianUInt(): UInt = this.map { it.toUInt() and 0xFFu }.reduce { acc, uInt -> acc shl 8 or uInt }
fun ByteArray.toLittleEndianUInt(): UInt = this.reversedArray().map { it.toUInt() and 0xFFu }.reduce { acc, uInt -> acc shl 8 or uInt }
fun ByteArray.toBigEndianUInt(): UInt = toBigEndianInt().toUInt()
fun ByteArray.toLittleEndianUInt(): UInt = toLittleEndianInt().toUInt()

fun ByteArray.toBigEndianLong(): Long = ByteBuffer.wrap(this).order(ByteOrder.BIG_ENDIAN).long
fun ByteArray.toLittleEndianLong(): Long = ByteBuffer.wrap(this).order(ByteOrder.LITTLE_ENDIAN).long
fun ByteArray.toBigEndianULong(): ULong = this.map { it.toULong() and 0xFFu }.reduce { acc, uLong -> acc shl 8 or uLong }
fun ByteArray.toLittleEndianULong(): ULong = this.reversedArray().map { it.toULong() and 0xFFu }.reduce { acc, uLong -> acc shl 8 or uLong }
fun ByteArray.toBigEndianULong(): ULong = toBigEndianLong().toULong()
fun ByteArray.toLittleEndianULong(): ULong = toLittleEndianLong().toULong()

fun ByteArray.toBigEndianFloat(): Float = java.lang.Float.intBitsToFloat(ByteBuffer.wrap(this).order(ByteOrder.BIG_ENDIAN).int)
fun ByteArray.toLittleEndianFloat(): Float = java.lang.Float.intBitsToFloat(ByteBuffer.wrap(this).order(ByteOrder.LITTLE_ENDIAN).int)
fun ByteArray.toBigEndianFloat(): Float = java.lang.Float.intBitsToFloat(this.toBigEndianInt())
fun ByteArray.toLittleEndianFloat(): Float = java.lang.Float.intBitsToFloat(this.toLittleEndianInt())

fun Byte.toBoolean(): Boolean = when (this) {
0.toByte() -> false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal class InputStreamExtensionsTest {
@Test
internal fun shouldReadUShortPrefixedStringWithUS_ASCIIEncoding() {

// byte representation of "LothlorienGrass05" with the length (17) prefixed as UShort
// byte representation of "LothlorienGrass05" in US_ASCII with the length (17) prefixed as UShort
// 17, 0 is the UShort 17
// 76, 111, 116, 104, 108, 111, 114, 105, 101, 110, 71, 114, 97, 115, 115, 48, 53 is the String "LothlorienGrass05" encoded in US_ASCII
val bytes = byteArrayOf(17, 0, 76, 111, 116, 104, 108, 111, 114, 105, 101, 110, 71, 114, 97, 115, 115, 48, 53)
Expand All @@ -99,7 +99,7 @@ internal class InputStreamExtensionsTest {
@Test
internal fun shouldReadUShortPrefixedStringWithUTF_16Encoding() {

// byte representation of "Neutral" with the length (7) prefixed as UShort
// byte representation of "Neutral" in UTF_16LE with the length (7) prefixed as UShort
// 7, 0 is the UShort 7
// 78, 0, 101, 0, 117, 0, 116, 0, 114, 0, 97, 0, 108, 0 is the String "Neutral" encoded in UTF_16LE
val bytes = byteArrayOf(7, 0, 78, 0, 101, 0, 117, 0, 116, 0, 114, 0, 97, 0, 108, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PropertyKeyReader {

val propertyType = PropertyType.ofByte(reader.readByte())

val nameIndex = reader.readNBytes(3).toLittleEndianUInt()
val nameIndex = byteArrayOf(*reader.readNBytes(3), 0).toLittleEndianUInt()
val name = context.getAssetName(nameIndex)

return PropertyKey(
Expand Down
21 changes: 13 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,23 @@

<kotlin.version>1.5.10</kotlin.version>
<java.version>11</java.version>

<version.commons-io>2.10.0</version.commons-io>
<version.assertj>3.20.1</version.assertj>
<version.jmh>1.28</version.jmh>
<version.surefire>3.0.0-M5</version.surefire>
</properties>

<dependencyManagement>
<dependencies>
<!-- Kotlin -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
</dependency>

<!-- Project Versions -->
<dependency>
<groupId>de.darkatra.bfme2</groupId>
<artifactId>big</artifactId>
Expand All @@ -85,12 +87,14 @@
<version>${project.version}</version>
</dependency>

<!-- IO Utils -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.10.0</version>
<version>${version.commons-io}</version>
</dependency>

<!-- JUnit5 -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
Expand All @@ -104,10 +108,11 @@
<scope>test</scope>
</dependency>

<!-- Assertions -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.20.1</version>
<version>${version.assertj}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down Expand Up @@ -150,7 +155,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<version>${version.surefire}</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down

0 comments on commit 026cba1

Please sign in to comment.