Skip to content

Commit

Permalink
Convert to a multiplatform module
Browse files Browse the repository at this point in the history
Etch support is only in JVM, but the helper utils are in common
  • Loading branch information
hufman committed Mar 24, 2024
1 parent 36dd262 commit 1794a59
Show file tree
Hide file tree
Showing 28 changed files with 51 additions and 54 deletions.
41 changes: 19 additions & 22 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,40 @@ plugins {
}
apply from: '../buildtools/ColoredOutput.gradle'
apply from: '../buildtools/jacoco.gradle'
apply plugin: 'java-library'
apply plugin: 'kotlin'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'maven-publish'

kotlin {
jvm {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
sourceSets {
commonMain {

implementation project(path: ':etch')
}
}
commonTest {
dependencies {
implementation 'junit:junit:4.12'
}
}
jvmMain {
dependencies {
implementation project(path: ':etch')
}

}
}
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"

// https://youtrack.jetbrains.com/issue/IDEA-119280
tasks.register('copyTestResources', Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
tasks.register('copyTestResourcesOut', Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/../out/test/classes"
}
// doesn't seem to actually work, you should run this step manually
processTestResources.dependsOn copyTestResources
processTestResources.dependsOn copyTestResourcesOut

publishing {
publications {
maven(MavenPublication) {
// publish to Jitpack
groupId 'io.bimmergestalt'
artifactId 'IDriveConnectKit'
version '0.6'

from components.java
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package io.bimmergestalt.idriveconnectkit

import de.bmw.idrive.BMWRemoting
import de.bmw.idrive.BMWRemotingServer
import java.io.InputStream
import java.security.MessageDigest

object Utils {
fun etchAsInt(obj: Any?, default: Int = 0): Int {
/** Etch likes to shrink numbers to the smallest type that will fit
Expand All @@ -17,28 +12,6 @@ object Utils {
else -> default
}
}

fun BMWRemotingServer.rhmi_setResourceCached(handle: Int, type: BMWRemoting.RHMIResourceType, data: InputStream?): Boolean {
data ?: return false
val bytes = data.readBytes()
return rhmi_setResourceCached(handle, type, bytes)
}

fun BMWRemotingServer.rhmi_setResourceCached(handle: Int, type: BMWRemoting.RHMIResourceType, data: ByteArray?): Boolean {
data ?: return false
val hash = md5sum(data)
val cached = rhmi_checkResource(hash, handle, data.size, "", type)
if (!cached) {
rhmi_setResource(handle, data, type)
}
return cached
}

fun md5sum(data: ByteArray): ByteArray {
val hasher = MessageDigest.getInstance("MD5")
hasher.update(data)
return hasher.digest()
}
}

fun <K, V> MutableMap<K, V>.withRealDefault(defaultValue: (key: K) -> V): MutableMap<K, V> = MutableMapWithRealDefault(this, defaultValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ abstract class RHMIModel private constructor(open val app: RHMIApplication, open
/**
* Return the data at this row, or null if it doesn't exist
*/
abstract operator fun get(index: Int): Array<Any>
abstract operator fun get(index: Int): Array<Any>?

/**
* Return the table index of the first row of this RHMIList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ object XMLUtils {
}

private fun getGetterMethodName(name: String): String {
return "get" + name.substring(0, 1).toUpperCase() + name.substring(1)
return "get" + name.substring(0, 1).uppercase() + name.substring(1)
}
private fun getSetterMethodName(name: String): String {
return "set" + name.substring(0, 1).toUpperCase() + name.substring(1)
return "set" + name.substring(0, 1).uppercase() + name.substring(1)
}

fun unmarshalAttributes(obj: Any, attrs: Map<String, String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import org.junit.Assert.*
import org.junit.Test
import org.w3c.dom.Node
import java.lang.IndexOutOfBoundsException
import kotlin.test.BeforeTest
import kotlin.test.expect

class TestXMLParsing {
val xml = this.javaClass.classLoader.getResourceAsStream("ui_layout.xml")!!.bufferedReader().use {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import de.bmw.idrive.BMWRemoting
import de.bmw.idrive.BMWRemotingServer
import java.io.InputStream
import java.security.MessageDigest

object RHMIUtils {
fun BMWRemotingServer.rhmi_setResourceCached(handle: Int, type: BMWRemoting.RHMIResourceType, data: InputStream?): Boolean {
data ?: return false
val bytes = data.readBytes()
return rhmi_setResourceCached(handle, type, bytes)
}

fun BMWRemotingServer.rhmi_setResourceCached(handle: Int, type: BMWRemoting.RHMIResourceType, data: ByteArray?): Boolean {
data ?: return false
val hash = md5sum(data)
val cached = rhmi_checkResource(hash, handle, data.size, "", type)
if (!cached) {
rhmi_setResource(handle, data, type)
}
return cached
}

fun md5sum(data: ByteArray): ByteArray {
val hasher = MessageDigest.getInstance("MD5")
hasher.update(data)
return hasher.digest()
}
}

0 comments on commit 1794a59

Please sign in to comment.