Skip to content

Commit

Permalink
Move Main.kt into its own server module
Browse files Browse the repository at this point in the history
Update logback
  • Loading branch information
GregHib committed Jan 24, 2025
1 parent 5910410 commit d51821b
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/create_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run:
gh release create -d ${{ github.ref_name }} ./game/build/distributions/void-${{ env.build_version }}.zip
gh release create -d ${{ github.ref_name }} ./server/build/distributions/void-${{ env.build_version }}.zip
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN mkdir /app
WORKDIR /app/

# Copy JAR file
COPY ./game/build/libs/void-server-*.jar /app/void-server.jar
COPY ./server/build/libs/void-server-*.jar /app/void-server.jar

# Copy configuration and cache files
COPY ./data/map/ /app/data/map/
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Run the following command in the terminal to set up Gradle or close and re-open

Extract the [cache files](https://mega.nz/folder/ZMN2AQaZ#4rJgfzbVW0_mWsr1oPLh1A) into a new directory called `/cache/` inside of the `/data/` directory.

From here you can navigate in the left panel to `/game/src/main/kotlin/world/gregs/voidps/` (Or Ctrl/Cmd + N for class search) where you will find [Main.kt](./game/src/main/kotlin/world/gregs/voidps/Main.kt) which you should be able to right-click and run.
From here you can navigate in the left panel to `/server/src/main/kotlin/world/gregs/voidps/` (Or Ctrl/Cmd + N for class search) where you will find [Main.kt](./server/src/main/kotlin/world/gregs/voidps/Main.kt) which you should be able to right-click and run.

You can also run in the command line using the gradle wrapper.

Expand Down
65 changes: 1 addition & 64 deletions game/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
`java-library`
application
id("com.github.johnrengelman.shadow")
}

dependencies {
implementation(project(":engine"))
implementation(project(":cache"))
Expand All @@ -21,70 +13,15 @@ dependencies {

implementation("org.rsmod:rsmod-pathfinder:${findProperty("pathfinderVersion")}")
implementation("io.insert-koin:koin-core:${findProperty("koinVersion")}")
implementation("io.github.classgraph:classgraph:${findProperty("classgraphVersion")}")

implementation("io.insert-koin:koin-logger-slf4j:${findProperty("koinLogVersion")}")
implementation("ch.qos.logback:logback-classic:${findProperty("logbackVersion")}")
implementation("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger-jvm:${findProperty("inlineLoggingVersion")}")
implementation("io.github.classgraph:classgraph:4.8.165")

testImplementation("org.junit.jupiter:junit-jupiter-params:${findProperty("junitVersion")}")
testImplementation("io.insert-koin:koin-test:${findProperty("koinVersion")}")
testImplementation("io.mockk:mockk:${findProperty("mockkVersion")}")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:${findProperty("kotlinCoroutinesVersion")}")

}
tasks.withType<Test> {
jvmArgs("-XX:-OmitStackTraceInFastThrow")
}

application {
mainClass.set("world.gregs.voidps.Main")
tasks.run.get().workingDir = rootProject.projectDir
}

tasks {
named<ShadowJar>("shadowJar") {
minimize {
exclude(dependency("org.postgresql:postgresql:.*"))
exclude(dependency("org.jetbrains.exposed:exposed-jdbc:.*"))
exclude(dependency("ch.qos.logback:logback-classic:.*"))
}
archiveBaseName.set("void-server-${version}")
archiveClassifier.set("")
archiveVersion.set("")
}
}

distributions {
create("bundle") {
distributionBaseName = "void"
contents {
from(tasks["shadowJar"])
from("../data/definitions/") {
into("data/definitions")
}
from("../data/map") {
into("data/map")
}
from("../data/spawns") {
into("data/spawns")
}
val emptyDirs = listOf("cache", "saves")
for (dir in emptyDirs) {
val file = layout.buildDirectory.get().dir("tmp/empty/$dir/").asFile
file.mkdirs()
}
from(layout.buildDirectory.dir("tmp/empty/")) {
into("data")
}
val resourcesDir = layout.projectDirectory.dir("src/main/resources")
from(resourcesDir.file("game.properties"))
val bat = resourcesDir.file("run-server.bat").asFile
bat.writeText(bat.readText().replace("-dev.jar", "-${version}.jar"))
from(bat)
val shell = resourcesDir.file("run-server.sh").asFile
shell.writeText(shell.readText().replace("-dev.jar", "-${version}.jar"))
from(shell)
}
}
}
56 changes: 29 additions & 27 deletions game/src/main/kotlin/world/gregs/voidps/script/ScriptLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@ package world.gregs.voidps.script

import com.github.michaelbull.logging.InlineLogger
import io.github.classgraph.ClassGraph
import world.gregs.voidps.Main
import world.gregs.voidps.engine.client.ui.chat.plural
import world.gregs.voidps.engine.data.Settings
import kotlin.reflect.KClass
import kotlin.system.measureTimeMillis

private val logger = InlineLogger("ScriptLoader")

fun loadScripts(scriptPackage: String = Settings["storage.scripts.package"], botScriptPackage: String = Settings["storage.scripts.bots.package"]) {
var scriptCount = 0
val found = mutableSetOf<String>()
val isJar = Main::class.java.getResource("${Main::class.simpleName}.class")?.protocol == "jar"
val arguments = emptyArray<String>()
val time = measureTimeMillis {
ClassGraph()
.filterClasspathElements { isJar || !it.endsWith(".jar") }
.acceptPackages(scriptPackage, botScriptPackage)
.enableMethodInfo()
.scan().use { scanResult ->
for (info in scanResult.allClasses) {
if (!info.hasMethod("main")) {
continue
object ScriptLoader {
fun load(mainClass: KClass<*> = ScriptLoader::class, scriptPackage: String = Settings["storage.scripts.package"], botScriptPackage: String = Settings["storage.scripts.bots.package"]) {
var scriptCount = 0
val found = mutableSetOf<String>()
val isJar = mainClass.java.getResource("${mainClass.simpleName}.class")?.protocol == "jar"
val arguments = emptyArray<String>()
val time = measureTimeMillis {
ClassGraph()
.filterClasspathElements { isJar || !it.endsWith(".jar") }
.acceptPackages(scriptPackage, botScriptPackage)
.enableMethodInfo()
.scan().use { scanResult ->
for (info in scanResult.allClasses) {
if (!info.hasMethod("main")) {
continue
}
val name = info.name
found.add(name)
scriptCount++
val clazz = Class.forName(name)
val constructor = clazz.declaredConstructors.first()
constructor.newInstance(arguments)
}
val name = info.name
found.add(name)
scriptCount++
val clazz = Class.forName(name)
val constructor = clazz.declaredConstructors.first()
constructor.newInstance(arguments)
}
}
}
if (scriptCount == 0) {
logger.warn { "No scripts found." }
}
logger.info { "Loaded $scriptCount ${"script".plural(scriptCount)} in ${time}ms" }
}
if (scriptCount == 0) {
logger.warn { "No scripts found." }
}
logger.info { "Loaded $scriptCount ${"script".plural(scriptCount)} in ${time}ms" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import world.gregs.voidps.gameModule
import world.gregs.voidps.getTickStages
import world.gregs.voidps.network.client.Client
import world.gregs.voidps.network.client.ConnectionQueue
import world.gregs.voidps.script.loadScripts
import world.gregs.voidps.script.ScriptLoader
import world.gregs.voidps.type.Tile
import world.gregs.voidps.type.setRandom
import world.gregs.voidps.world.interact.world.spawn.loadItemSpawns
Expand Down Expand Up @@ -187,7 +187,7 @@ abstract class WorldTest : KoinTest {
}
})
}
loadScripts()
ScriptLoader.load()
MapDefinitions(CollisionDecoder(get()), get(), get(), cache).loadCache()
saves = File(Settings["storage.players.path"])
saves?.mkdirs()
Expand Down
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kotlinIoVersion = 0.1.16
kotlinCoroutinesVersion = 1.7.3
koinVersion = 3.5.3
koinLogVersion = 3.5.3
logbackVersion = 1.4.14
logbackVersion = 1.5.16
inlineLoggingVersion = 1.0.6
mockkVersion = 1.13.9
jbcryptVersion = 0.4
Expand All @@ -19,4 +19,5 @@ postgresqlVersion=42.7.3
hikariVersion=5.1.0
testcontainersVersion=1.19.7
postgresVersion=16.2.0
embeddedPostgresVersion=2.0.6
embeddedPostgresVersion=2.0.6
classgraphVersion=4.8.179
1 change: 1 addition & 0 deletions qodana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ exclude:
- tools
- types
- yaml
- server
projectJDK: 19
linter: jetbrains/qodana-jvm-community:2023.3
77 changes: 77 additions & 0 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar

plugins {
application
id("com.github.johnrengelman.shadow")
}

dependencies {
implementation(project(":cache"))
implementation(project(":engine"))
implementation(project(":network"))
api(project(":game"))
implementation(project(":yaml"))
implementation(project(":types"))

implementation(kotlin("script-runtime"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${findProperty("kotlinCoroutinesVersion")}")
implementation("org.rsmod:rsmod-pathfinder:${findProperty("pathfinderVersion")}")

implementation("io.insert-koin:koin-core:${findProperty("koinVersion")}")
implementation("io.insert-koin:koin-logger-slf4j:${findProperty("koinLogVersion")}")
implementation("ch.qos.logback:logback-classic:${findProperty("logbackVersion")}")
implementation("com.michael-bull.kotlin-inline-logger:kotlin-inline-logger-jvm:${findProperty("inlineLoggingVersion")}")

}

application {
mainClass.set("world.gregs.voidps.Main")
}

tasks {
named<ShadowJar>("shadowJar") {
minimize {
exclude(dependency("org.jetbrains.kotlin:kotlin-script-runtime:.*"))
exclude(dependency("org.postgresql:postgresql:.*"))
exclude(dependency("org.jetbrains.exposed:exposed-jdbc:.*"))
exclude(dependency("ch.qos.logback:logback-classic:.*"))
}
archiveBaseName.set("void-server-${version}")
archiveClassifier.set("")
archiveVersion.set("")
}
}

distributions {
create("bundle") {
distributionBaseName = "void"
contents {
from(tasks["shadowJar"])
from("../data/definitions/") {
into("data/definitions")
}
from("../data/map") {
into("data/map")
}
from("../data/spawns") {
into("data/spawns")
}
val emptyDirs = listOf("cache", "saves")
for (dir in emptyDirs) {
val file = layout.buildDirectory.get().dir("tmp/empty/$dir/").asFile
file.mkdirs()
}
from(layout.buildDirectory.dir("tmp/empty/")) {
into("data")
}
val resourcesDir = layout.projectDirectory.dir("src/main/resources")
from(resourcesDir.file("game.properties"))
val bat = resourcesDir.file("run-server.bat").asFile
bat.writeText(bat.readText().replace("-dev.jar", "-${version}.jar"))
from(bat)
val shell = resourcesDir.file("run-server.sh").asFile
shell.writeText(shell.readText().replace("-dev.jar", "-${version}.jar"))
from(shell)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import world.gregs.voidps.engine.map.collision.CollisionDecoder
import world.gregs.voidps.network.GameServer
import world.gregs.voidps.network.LoginServer
import world.gregs.voidps.network.login.protocol.decoders
import world.gregs.voidps.script.loadScripts
import world.gregs.voidps.script.ScriptLoader
import java.util.*

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ object Main {
slf4jLogger(level = Level.ERROR)
modules(engineModule, gameModule, module)
}
loadScripts()
ScriptLoader.load(Main::class)
}

private fun cache(cache: Cache) = module {
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions server/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%logger{0}] - %msg%n</pattern>
</encoder>
</appender>
<root level="trace">
<appender-ref ref="console"/>
</root>
<logger name="com.zaxxer.hikari.pool" level="WARN"/>
<logger name="com.zaxxer.hikari.HikariConfig" level="WARN"/>
<logger name="Exposed" level="WARN">
</logger>
</configuration>
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ include("buffer")
include("network")
include("types")
include("yaml")
include("server")

0 comments on commit d51821b

Please sign in to comment.