refactor: split up core into multiple modules

This commit is contained in:
Cilly Leang 2026-02-05 01:05:02 +11:00
parent 2725342c3f
commit 0d84411f14
Signed by: cilly
GPG key ID: 6500251E087653C9
38 changed files with 344 additions and 149 deletions

View file

@ -0,0 +1,50 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
plugins {
alias(libs.plugins.androidLibrary)
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.kotlinSerialization)
}
kotlin {
jvm()
androidTarget {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_11)
}
}
sourceSets {
commonMain.dependencies {
implementation(libs.kotlinx.serialization.json)
}
}
}
dependencies {
coreLibraryDesugaring(libs.desugar)
}
android {
namespace = "moe.lava.neon.api"
compileSdk = libs.versions.android.compileSdk.get().toInt()
defaultConfig {
minSdk = libs.versions.android.minSdk.get().toInt()
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
isCoreLibraryDesugaringEnabled = true
}
}

View file

@ -0,0 +1,14 @@
package moe.lava.neon.api
import android.annotation.SuppressLint
import android.os.Build
import java.util.Locale
@SuppressLint("ConstantLocale")
internal actual val platformSuperProps = PlatformProps(
device = Build.DEVICE,
// TODO: this only outputs language but not country (e.g. en instead of en-AU)
// .toLanguageTag() is close, but returns too much junk (e.g. en-AU-u-fw-mon)
systemLocale = Locale.getDefault().language,
osVersion = "${Build.VERSION.SDK_INT}",
)

View file

@ -0,0 +1,95 @@
package moe.lava.neon.api
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNamingStrategy
import java.time.ZoneId
import kotlin.io.encoding.Base64
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid
@OptIn(ExperimentalUuidApi::class)
private val storedVendorId = Uuid.random().toString().lowercase()
@OptIn(ExperimentalUuidApi::class)
private val storedLaunchId = Uuid.random().toString().lowercase()
internal data class PlatformProps(
val device: String,
val systemLocale: String,
val osVersion: String,
)
internal expect val platformSuperProps: PlatformProps
object ApiConstants {
val superProps = Base64.encode(Json.encodeToString(SuperProperties()).encodeToByteArray())
val baseHeaders = mapOf(
"X-Debug-Options" to "bugReporterEnabled",
"X-Discord-Locale" to "en-US",
"X-Discord-Timezone" to ZoneId.systemDefault().id,
"X-Super-Properties" to superProps,
)
const val userAgent = "Discord-Android/311020;RNA"
const val gatewayUserAgent = "okhttp/4.12.0"
@OptIn(ExperimentalSerializationApi::class)
val json = Json {
namingStrategy = JsonNamingStrategy.SnakeCase
ignoreUnknownKeys = true
encodeDefaults = true
}
// TODO: Desktop uses separate properties
@Suppress("PropertyName")
@Serializable
data class SuperProperties(
val os: String = "Android",
val browser: String = "Discord Android",
val device: String = platformSuperProps.device,
val system_locale: String = platformSuperProps.systemLocale,
val has_client_mods: Boolean = false,
val client_version: String = "311.20 - rn",
val release_channel: String = "googleRelease",
val device_vendor_id: String = storedVendorId,
val design_id: Int = 2,
val browser_user_agent: String = "",
val browser_version: String = "",
val os_version: String = platformSuperProps.osVersion,
val client_build_number: Long = 31102000334720,
val client_event_source: String? = null,
val client_launch_id: String = storedLaunchId,
// TODO: this is a random snowflake
val launch_signature: String = "1769227908736837151",
val client_app_state: String = "active",
)
// *Ideally* we extend the previous class, but pretty much every way
// I came up with to do it looks disgusting (I'm open to suggestions though!)
@Suppress("PropertyName")
@Serializable
data class GatewayProperties(
val os: String = "Android",
val browser: String = "Discord Android",
val device: String = platformSuperProps.device,
val system_locale: String = platformSuperProps.systemLocale,
val has_client_mods: Boolean = false,
val client_version: String = "311.20 - rn",
val release_channel: String = "googleRelease",
val device_vendor_id: String = storedVendorId,
val design_id: Int = 2,
val browser_user_agent: String = "",
val browser_version: String = "",
val os_version: String = platformSuperProps.osVersion,
val client_build_number: Long = 31102000334720,
val client_event_source: String? = null,
val client_launch_id: String = storedLaunchId,
// TODO: this is a random snowflake
val launch_signature: String = "1769227908736837151",
val client_app_state: String = "active",
val is_fast_connect: Boolean = true,
val gateway_connect_reasons: String = "executeRunnable:Main",
)
}

View file

@ -0,0 +1,6 @@
package moe.lava.neon.api.objects
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.LongAsStringSerializer
typealias Snowflake = @Serializable(LongAsStringSerializer::class) Long

View file

@ -0,0 +1,10 @@
package moe.lava.neon.api.objects
import kotlinx.serialization.Serializable
@Serializable
data class User(
val id: Snowflake,
val username: String,
val discriminator: String,
)

View file

@ -0,0 +1,11 @@
package moe.lava.neon.api
import java.util.Locale
// TODO
@Suppress("ConstantLocale")
internal actual val platformSuperProps = PlatformProps(
device = "",
systemLocale = Locale.getDefault().language,
osVersion = "",
)