Skip to content

Commit

Permalink
PC-1070 initial structure (partial)
Browse files Browse the repository at this point in the history
  • Loading branch information
Polyana committed Dec 21, 2023
1 parent 238071f commit d9c86d2
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import kotlinx.coroutines.CoroutineScope
* Server side instance that handles all server features
*
* @property platform The platform where MineCity is running
* @property serverIp The IP being used to host the server
*/
public interface MineCityServer: CoroutineScope {
public val platform: MineCityPlatform
public val serverIp: String

/**
* Checks if the current thread is the main thread
Expand Down
16 changes: 16 additions & 0 deletions persistence/base/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
id("maven-publish")
kotlin("jvm")
kotlin("plugin.serialization")
}

kotlin {
explicitApi()
}

val inlineLoggerVersion: String by rootProject

dependencies {
api(project(":core"))
compileOnly("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:$inlineLoggerVersion")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package br.com.gamemods.minecity.persistence.base

import kotlinx.coroutines.CoroutineScope

public interface MineCityPersistence: CoroutineScope
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package br.com.gamemods.minecity.persistence.base

import java.io.IOException

public class MineCityPersistenceException(message: String? = null, cause: Throwable? = null): IOException(message, cause)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package br.com.gamemods.minecity.persistence.base.annotation

/**
* Indicates that the annotated element should only be used by MineCity Persistence API implementation, not API users.
*/
@Retention(value = AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.TYPEALIAS, AnnotationTarget.PROPERTY,
AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.CONSTRUCTOR
)
@RequiresOptIn(
level = RequiresOptIn.Level.ERROR, message = "This is an internal MineCity API that " +
"should not be used from outside of br.com.gamemods.minecity.persistence. No compatibility guarantees are provided. " +
"It is recommended to report your use-case of internal API to MineCity2 issue tracker, " +
"so stable API could be provided instead"
)
public annotation class InternalMineCityPersistenceApi
14 changes: 14 additions & 0 deletions persistence/sql/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("maven-publish")
kotlin("jvm")
kotlin("plugin.serialization")
}

val inlineLoggerVersion: String by rootProject

dependencies {
implementation(project(":persistence:base"))
implementation("org.flywaydb:flyway-core:10.1.0")
implementation("org.jdbi:jdbi3-core:3.41.3")
compileOnly("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:$inlineLoggerVersion")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package br.com.gamemods.minecity.persistence.sql

import br.com.gamemods.minecity.api.server.MineCityServer
import br.com.gamemods.minecity.persistence.base.MineCityPersistence
import br.com.gamemods.minecity.persistence.base.MineCityPersistenceException
import br.com.gamemods.minecity.persistence.base.annotation.InternalMineCityPersistenceApi
import com.github.michaelbull.logging.InlineLogger
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.job
import org.flywaydb.core.Flyway
import javax.sql.DataSource

@InternalMineCityPersistenceApi
class MineCitySqlPersistence(
private val server: MineCityServer,
private val dataSource: DataSource,
): MineCityPersistence {
override val coroutineContext = SupervisorJob(server.coroutineContext.job) + Dispatchers.IO + CoroutineName("MineCitySqlPersistence ${server.serverIp}")
private val log = InlineLogger()

@Throws(MineCityPersistenceException::class)
fun upgrade() {
val flyway = Flyway.configure()
.dataSource(dataSource)
.locations("classpath:br/com/gamemods/minecity/persistence/sql/migration")
.load()

val result = try {
flyway.migrate()
} catch (e: Throwable) {
log.error(e) {
"SQL migration failed with an exception!"
}
throw MineCityPersistenceException("SQL migration failed with an exception!", e)
}
if (!result.success) {
log.error {
"SQL migration failed."
}
} else if (result.migrationsExecuted > 0) {
log.info {
"SQL databased migrated from ${result.initialSchemaVersion} to ${result.targetSchemaVersion} (${result.migrationsExecuted} migrations executed) in ${result.totalMigrationTime}ms."
}
} else {
log.info { "SQL database is up to date." }
}
if (result.warnings.isNotEmpty()) {
log.warn {
buildString {
append("SQL database migration raised ${result.warnings.size} warning(s):")
appendLine()
result.warnings.forEach {
append(it)
appendLine()
appendLine()
}
}
}
}
if (result.success)
}
}
5 changes: 5 additions & 0 deletions platform/fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ loom {
}
}

val persistenceProject = project(":persistence")

dependencies {
// To change the versions see the gradle.properties file
minecraft("com.mojang:minecraft:$minecraftVersion")
Expand All @@ -46,6 +48,9 @@ dependencies {

// modImplementation("net.fabricmc.fabric-api:fabric-api-deprecated:${project.findProperty("fabric_version")}")
implementation(include(project(":core"))!!)
persistenceProject.subprojects.forEach { persistenceImpl ->
implementation(include(project(persistenceImpl.path))!!)
}

compileOnly("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger:$inlineLoggerVersion")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import kotlin.coroutines.CoroutineContext
*/
@InternalMineCityApi
class MineCityFabricServer(val mcServer: FabricMinecraftServerWrapper): MineCityServer {
override val coroutineContext: CoroutineContext = Dispatchers.Sync + CoroutineName("${mcServer.serverIp} (Sync)")
override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Sync + CoroutineName("MineCityFabricServer ${mcServer.serverIp} (Sync)")
private val log = InlineLogger()
override val platform: MineCityFabric get() = MineCityFabric
override val serverIp: String get() = mcServer.serverIp

@ServerSideOnly
fun onServerStarting() {
Expand Down
6 changes: 5 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ pluginManagement {
}
}

include(":api", ":core", ":platform:fabric")
include(
":api", ":core",
":persistence:base", ":persistence:sql",
":platform:fabric"
)

0 comments on commit d9c86d2

Please sign in to comment.