Skip to content

Commit

Permalink
fix: Fix compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed Oct 19, 2024
1 parent 2667ca6 commit 9fe87be
Show file tree
Hide file tree
Showing 24 changed files with 360 additions and 314 deletions.
23 changes: 0 additions & 23 deletions HEADER.md

This file was deleted.

25 changes: 25 additions & 0 deletions HEADER.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This file is part of NoHorny. The plugin securing your server against nsfw builds.
*
* MIT License
*
* Copyright (c) 2024 Xpdustry
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ indra {
spotless {
kotlin {
ktlint()
licenseHeaderFile(rootProject.file("HEADER.md"))
licenseHeaderFile(rootProject.file("HEADER.txt"))
}
kotlinGradle {
ktlint()
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/xpdustry/nohorny/NoHornyAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
*/
package com.xpdustry.nohorny

import mindustry.Vars

public interface NoHornyAPI {
public fun setCache(cache: NoHornyCache)

public companion object {
@JvmStatic public fun get(): NoHornyAPI = NoHornyPlugin.INSTANCE
@JvmStatic public fun get(): NoHornyAPI = Vars.mods.getMod(NoHornyPlugin::class.java).main as NoHornyAPI
}
}
10 changes: 6 additions & 4 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyAutoBan.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,19 @@ import mindustry.world.blocks.logic.LogicBlock
import mindustry.world.blocks.logic.LogicDisplay

internal class NoHornyAutoBan(private val plugin: NoHornyPlugin) : NoHornyListener {

override fun onInit() {
onEvent<ImageAnalyzerEvent> { (result, cluster, _, author) ->
if (result.rating == ImageAnalyzer.Rating.UNSAFE &&
plugin.config.autoBan &&
author != null) {
author != null
) {
for (player in Groups.player) {
if (player.uuid() == author.uuid || player.ip() == author.address.hostAddress) {
Vars.netServer.admins.banPlayer(player.uuid())
player.kick("[scarlet]You have been banned for building a NSFW building.")
Call.sendMessage(
"[pink]NoHorny: [white]${player.plainName()} has been banned for building a NSFW building.")
"[pink]NoHorny: [white]${player.plainName()} has been banned for building a NSFW building.",
)
}
}
cluster.blocks
Expand All @@ -65,7 +66,8 @@ internal class NoHornyAutoBan(private val plugin: NoHornyPlugin) : NoHornyListen
val building = Vars.world.build(point.x, point.y)
if (building is LogicDisplay.LogicDisplayBuild ||
building is LogicBlock.LogicBuild ||
building is CanvasBlock.CanvasBuild) {
building is CanvasBlock.CanvasBuild
) {
Vars.world.tile(point.x, point.y)?.setNet(Blocks.air)
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ import java.util.concurrent.CompletableFuture
public interface NoHornyCache {
public fun getResult(
cluster: Cluster<out NoHornyImage>,
image: BufferedImage
image: BufferedImage,
): CompletableFuture<ImageAnalyzer.Result?>

public fun putResult(
cluster: Cluster<out NoHornyImage>,
image: BufferedImage,
result: ImageAnalyzer.Result
result: ImageAnalyzer.Result,
)

public data object None : NoHornyCache {
override fun getResult(
cluster: Cluster<out NoHornyImage>,
image: BufferedImage
image: BufferedImage,
): CompletableFuture<ImageAnalyzer.Result?> = CompletableFuture.completedFuture(null)

override fun putResult(
cluster: Cluster<out NoHornyImage>,
image: BufferedImage,
result: ImageAnalyzer.Result
result: ImageAnalyzer.Result,
): Unit = Unit
}
}
6 changes: 2 additions & 4 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal data class NoHornyConfig(
val minimumCanvasClusterSize: Int = 9,
val minimumProcessorCount: Int = 5,
val processorSearchRadius: Int = 10,
val alwaysProcess: Boolean = false
val alwaysProcess: Boolean = false,
) {
init {
require(minimumInstructionCount >= 1) { "minimumInstructionCount cannot be lower than 1" }
Expand All @@ -58,7 +58,7 @@ internal data class NoHornyConfig(
val sightEngineSecret: Secret,
val unsafeThreshold: Float = 0.55F,
val warningThreshold: Float = 0.4F,
val kinds: List<ImageAnalyzer.Kind> = listOf(ImageAnalyzer.Kind.NUDITY)
val kinds: List<ImageAnalyzer.Kind> = listOf(ImageAnalyzer.Kind.NUDITY),
) : Analyzer {
init {
require(unsafeThreshold >= 0) { "unsafeThreshold cannot be lower than 0" }
Expand All @@ -67,8 +67,6 @@ internal data class NoHornyConfig(
}
}

data class ModerateContent(val moderateContentToken: Secret) : Analyzer

data class Fallback(val primary: Analyzer, val secondary: Analyzer) : Analyzer
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public sealed interface NoHornyImage {
public data class Canvas(
override val resolution: Int,
public val pixels: IntIntMap,
public val author: Author?
public val author: Author?,
) : NoHornyImage {
override fun copy(): Canvas = Canvas(resolution, IntIntMap(pixels), author)
}

public data class Display(
override val resolution: Int,
public val processors: MutableMap<ImmutablePoint, Processor>
public val processors: MutableMap<ImmutablePoint, Processor>,
) : NoHornyImage {
override fun copy(): Display = Display(resolution, processors.toMutableMap())
}
Expand All @@ -62,7 +62,7 @@ public sealed interface NoHornyImage {
val x2: Int,
val y2: Int,
val x3: Int,
val y3: Int
val y3: Int,
) : Instruction
}

Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/xpdustry/nohorny/NoHornyListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ package com.xpdustry.nohorny
import arc.util.CommandHandler

internal interface NoHornyListener {

fun onInit() = Unit

fun onServerCommandsRegistration(handler: CommandHandler) = Unit
Expand Down
96 changes: 68 additions & 28 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,54 @@ import mindustry.Vars
import org.slf4j.LoggerFactory

internal interface NoHornyLogger {

fun error(text: String, vararg args: Any = emptyArray())

fun info(text: String, vararg args: Any = emptyArray())

fun debug(text: String, vararg args: Any = emptyArray())

fun trace(text: String, vararg args: Any = emptyArray())
fun error(
text: String,
vararg args: Any = emptyArray(),
)

fun info(
text: String,
vararg args: Any = emptyArray(),
)

fun debug(
text: String,
vararg args: Any = emptyArray(),
)

fun trace(
text: String,
vararg args: Any = emptyArray(),
)

companion object : NoHornyLogger by findImplementation()

object ARC : NoHornyLogger {

override fun error(text: String, vararg args: Any) = log(Log.LogLevel.err, text, args)

override fun info(text: String, vararg args: Any) = log(Log.LogLevel.info, text, args)

override fun debug(text: String, vararg args: Any) = log(Log.LogLevel.debug, text, args)

override fun trace(text: String, vararg args: Any) = log(Log.LogLevel.debug, text, args)

private fun log(level: Log.LogLevel, text: String, args: Array<out Any>) {
override fun error(
text: String,
vararg args: Any,
) = log(Log.LogLevel.err, text, args)

override fun info(
text: String,
vararg args: Any,
) = log(Log.LogLevel.info, text, args)

override fun debug(
text: String,
vararg args: Any,
) = log(Log.LogLevel.debug, text, args)

override fun trace(
text: String,
vararg args: Any,
) = log(Log.LogLevel.debug, text, args)

private fun log(
level: Log.LogLevel,
text: String,
args: Array<out Any>,
) {
val throwable: Throwable?
val arguments: Array<out Any>
if (args.lastOrNull() is Throwable) {
Expand All @@ -69,21 +95,35 @@ internal interface NoHornyLogger {
}

object SLF4J : NoHornyLogger {

private val logger = LoggerFactory.getLogger(NoHornyPlugin::class.java)

override fun error(text: String, vararg args: Any) = logger.error(text, *args)

override fun info(text: String, vararg args: Any) = logger.info(text, *args)

override fun debug(text: String, vararg args: Any) = logger.debug(text, *args)

override fun trace(text: String, vararg args: Any) = logger.trace(text, *args)
override fun error(
text: String,
vararg args: Any,
) = logger.error(text, *args)

override fun info(
text: String,
vararg args: Any,
) = logger.info(text, *args)

override fun debug(
text: String,
vararg args: Any,
) = logger.debug(text, *args)

override fun trace(
text: String,
vararg args: Any,
) = logger.trace(text, *args)
}
}

private fun findImplementation() =
if (Vars.mods.getMod("distributor-core") != null ||
Vars.mods.getMod("distributor-logging") != null)
Vars.mods.getMod("distributor-logging") != null
) {
NoHornyLogger.SLF4J
else NoHornyLogger.ARC
} else {
NoHornyLogger.ARC
}
19 changes: 7 additions & 12 deletions src/main/kotlin/com/xpdustry/nohorny/NoHornyPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ import com.sksamuel.hoplite.addPathSource
import com.xpdustry.nohorny.analyzer.DebugImageAnalyzer
import com.xpdustry.nohorny.analyzer.FallbackAnalyzer
import com.xpdustry.nohorny.analyzer.ImageAnalyzer
import com.xpdustry.nohorny.analyzer.ModerateContentAnalyzer
import com.xpdustry.nohorny.analyzer.SightEngineAnalyzer
import mindustry.Vars
import mindustry.mod.Plugin
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.Executors
Expand All @@ -45,13 +48,8 @@ import java.util.concurrent.atomic.AtomicInteger
import kotlin.io.path.notExists
import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration
import mindustry.Vars
import mindustry.mod.Plugin
import okhttp3.Dispatcher
import okhttp3.OkHttpClient

public class NoHornyPlugin : Plugin(), NoHornyAPI {

private val directory: Path
get() = Vars.modDirectory.child("nohorny").file().toPath()

Expand Down Expand Up @@ -82,7 +80,6 @@ public class NoHornyPlugin : Plugin(), NoHornyAPI {
internal var cache: NoHornyCache = NoHornyCache.None

init {
INSTANCE = this
Files.createDirectories(directory)
listeners += NoHornyTracker(this)
listeners += NoHornyAutoBan(this)
Expand All @@ -101,7 +98,8 @@ public class NoHornyPlugin : Plugin(), NoHornyAPI {
EXECUTOR.shutdownNow()
}
}
})
},
)
}

override fun registerServerCommands(handler: CommandHandler) {
Expand Down Expand Up @@ -144,7 +142,6 @@ public class NoHornyPlugin : Plugin(), NoHornyAPI {
when (config) {
is NoHornyConfig.Analyzer.None -> ImageAnalyzer.None
is NoHornyConfig.Analyzer.Debug -> DebugImageAnalyzer(directory.resolve("debug"))
is NoHornyConfig.Analyzer.ModerateContent -> ModerateContentAnalyzer(config, http)
is NoHornyConfig.Analyzer.SightEngine -> SightEngineAnalyzer(config, http)
is NoHornyConfig.Analyzer.Fallback ->
FallbackAnalyzer(createAnalyzer(config.primary), createAnalyzer(config.secondary))
Expand All @@ -153,12 +150,10 @@ public class NoHornyPlugin : Plugin(), NoHornyAPI {
private object NoHornyThreadFactory : ThreadFactory {
private val count = AtomicInteger(0)

override fun newThread(runnable: Runnable) =
Thread(runnable, "nohorny-worker-${count.incrementAndGet()}").apply { isDaemon = true }
override fun newThread(runnable: Runnable) = Thread(runnable, "nohorny-worker-${count.incrementAndGet()}").apply { isDaemon = true }
}

internal companion object {
@JvmStatic internal lateinit var INSTANCE: NoHornyPlugin
@JvmStatic internal val EXECUTOR = Executors.newScheduledThreadPool(4, NoHornyThreadFactory)
}
}
Loading

0 comments on commit 9fe87be

Please sign in to comment.