diff --git a/build.gradle.kts b/build.gradle.kts index ce4c0ef..02f657a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -14,11 +14,11 @@ } dependencies { - implementation("com.fasterxml.jackson.core:jackson-annotations:2.17.1") - implementation("com.fasterxml.jackson.core:jackson-core:2.17.1") - implementation("com.fasterxml.jackson.core:jackson-databind:2.17.1") - implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.1") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.1") + implementation("com.fasterxml.jackson.core:jackson-annotations:2.17.2") + implementation("com.fasterxml.jackson.core:jackson-core:2.17.2") + implementation("com.fasterxml.jackson.core:jackson-databind:2.17.2") + implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.2") + implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.17.2") implementation("com.github.pemistahl:lingua:1.2.2") implementation("com.google.protobuf:protobuf-java:4.27.2") implementation("com.google.protobuf:protobuf-kotlin:4.27.2") diff --git a/src/main/kotlin/net/taehui/twilight/Twilight.kt b/src/main/kotlin/net/taehui/twilight/Twilight.kt index c7c0f77..d87c8b9 100644 --- a/src/main/kotlin/net/taehui/twilight/Twilight.kt +++ b/src/main/kotlin/net/taehui/twilight/Twilight.kt @@ -61,8 +61,8 @@ DB.loadDB() AbilityClassSystem.loadAbilityClass() AbilitySystem.loadAbility() - BannedIP.loadBannedIP() - BannedNote.loadBannedNote() + BannedIPSystem.loadBannedIP() + BannedNoteSystem.loadBannedNote() TVSystem.loadBannedTV() EdgeSystem.loadEdge() AvatarIPSystem.loadAvatarIP() @@ -99,13 +99,13 @@ } finally { AwilightHandler.dispose() WitSystem.dispose() - PlatformIDSystem.savePlatformID() PlatformSystem.dispose() TVSystem.dispose() AutoSystem.dispose() + PlatformIDSystem.savePlatformID() AvatarIPSystem.saveAvatarIP() - BannedIP.saveBannedIP() - BannedNote.saveBannedNote() + BannedIPSystem.saveBannedIP() + BannedNoteSystem.saveBannedNote() TVSystem.saveBannedTV() Configure.saveConfigure() } diff --git a/src/main/kotlin/net/taehui/twilight/qwilight/QwilightAvatar.kt b/src/main/kotlin/net/taehui/twilight/qwilight/QwilightAvatar.kt index 451cee7..b1f9d44 100644 --- a/src/main/kotlin/net/taehui/twilight/qwilight/QwilightAvatar.kt +++ b/src/main/kotlin/net/taehui/twilight/qwilight/QwilightAvatar.kt @@ -239,7 +239,7 @@ ValveSystem.putDrawing(avatarID, eventData[0]) } AvatarHandler.establish(this, avatarID, text.qwilightName, isValve, true) - } else if (BannedIP.isBanned(remote)) { + } else if (BannedIPSystem.isBanned(remote)) { send(EventOuterClass.Event.EventID.WARNING, translateLanguage("bannedIP")) } else if (avatarIP.isLoopbackAddress || avatarIP.isSiteLocalAddress || Configure.hash.contains( qwilightHash @@ -803,7 +803,7 @@ val noteFileData = eventData[0].toByteArray() val targetComputing = BaseCompiler.handleCompile(noteFileData, dataID).single() val noteID512 = Utility.getID512(noteFileData) - if (!targetComputing.isBanned && !BannedNote.isBanned(targetComputing.noteID)) { + if (!targetComputing.isBanned && !BannedNoteSystem.isBanned(targetComputing.noteID)) { logFuture { Files.write(TwilightComponent.NOTE_ENTRY_PATH.resolve(noteID512), noteFileData) DB.setNote( @@ -988,7 +988,7 @@ val text = jm.readValue(eventText, JSON.QwilightValveComment::class.java) val targetComputing = BaseCompiler.handleCompile(eventData[0].toByteArray(), text.dataID).single() - if (!targetComputing.isBanned && !BannedNote.isBanned(targetComputing.noteID)) { + if (!targetComputing.isBanned && !BannedNoteSystem.isBanned(targetComputing.noteID)) { logFuture { if (SiteHandler.hasAvatar(this, SiteHandler.commentSiteID)) { SiteHandler.putSiteYell( diff --git a/src/main/kotlin/net/taehui/twilight/system/AvatarIPSystem.kt b/src/main/kotlin/net/taehui/twilight/system/AvatarIPSystem.kt index d411e83..be7085d 100644 --- a/src/main/kotlin/net/taehui/twilight/system/AvatarIPSystem.kt +++ b/src/main/kotlin/net/taehui/twilight/system/AvatarIPSystem.kt @@ -12,6 +12,7 @@ object AvatarIPSystem : Logger { private var avatarIPStore = ConcurrentHashMap>() + private var isLoaded = false fun loadAvatarIP() { try { @@ -20,17 +21,19 @@ Paths.get("Avatar IP.json").toFile().absoluteFile, object : TypeReference>>() {}) ) + isLoaded = true logInfo("Loaded Avatar IP") - saveAvatarIP() } catch (e: IOException) { logFault(e) } } fun saveAvatarIP() { - Files.newOutputStream(Paths.get("Avatar IP.json")) - .use { ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(it, avatarIPStore) } - logInfo("Saved Avatar IP") + if (isLoaded) { + Files.newOutputStream(Paths.get("Avatar IP.json")) + .use { ObjectMapper().writerWithDefaultPrettyPrinter().writeValue(it, avatarIPStore) } + logInfo("Saved Avatar IP") + } } fun putAvatarIP(avatar: Avatar, avatarID: String) { diff --git a/src/main/kotlin/net/taehui/twilight/system/BannedIP.kt b/src/main/kotlin/net/taehui/twilight/system/BannedIP.kt deleted file mode 100644 index 39cfd9f..0000000 --- a/src/main/kotlin/net/taehui/twilight/system/BannedIP.kt +++ /dev/null @@ -1,54 +0,0 @@ -package net.taehui.twilight.system - -import com.fasterxml.jackson.core.type.TypeReference -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule -import net.taehui.twilight.Logger -import java.io.IOException -import java.nio.file.Paths -import java.time.LocalDateTime -import java.util.concurrent.ConcurrentHashMap - -object BannedIP : Logger { - private val bannedIPStore = ConcurrentHashMap() - - private fun getJSONMapper(): ObjectMapper { - val jm = ObjectMapper() - jm.registerModule(JavaTimeModule()) - return jm - } - - fun loadBannedIP() { - try { - bannedIPStore.putAll( - getJSONMapper().readValue( - Paths.get("Banned IP.json").toFile().absoluteFile, - object : TypeReference>() {}) - ) - logInfo("Loaded Banned IP") - saveBannedIP() - } catch (e: IOException) { - logFault(e) - } - } - - fun saveBannedIP() { - getJSONMapper().writerWithDefaultPrettyPrinter().writeValue( - Paths.get("Banned IP.json").toFile().absoluteFile, - bannedIPStore - ) - logInfo("Saved Banned IP") - } - - fun putBannedIP(avatarIP: String, date: LocalDateTime) { - bannedIPStore[avatarIP] = date - } - - fun wipeBannedIP(avatarIP: String) { - bannedIPStore.remove(avatarIP) - } - - fun isBanned(avatarIP: String): Boolean { - return bannedIPStore.getOrDefault(avatarIP, LocalDateTime.MIN).isAfter(LocalDateTime.now()) - } -} \ No newline at end of file diff --git a/src/main/kotlin/net/taehui/twilight/system/BannedIPSystem.kt b/src/main/kotlin/net/taehui/twilight/system/BannedIPSystem.kt new file mode 100644 index 0000000..63fc6c0 --- /dev/null +++ b/src/main/kotlin/net/taehui/twilight/system/BannedIPSystem.kt @@ -0,0 +1,57 @@ +package net.taehui.twilight.system + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule +import net.taehui.twilight.Logger +import java.io.IOException +import java.nio.file.Paths +import java.time.LocalDateTime +import java.util.concurrent.ConcurrentHashMap + +object BannedIPSystem : Logger { + private val bannedIPStore = ConcurrentHashMap() + private var isLoaded = false + + private fun getJSONMapper(): ObjectMapper { + val jm = ObjectMapper() + jm.registerModule(JavaTimeModule()) + return jm + } + + fun loadBannedIP() { + try { + bannedIPStore.putAll( + getJSONMapper().readValue( + Paths.get("Banned IP.json").toFile().absoluteFile, + object : TypeReference>() {}) + ) + isLoaded = true + logInfo("Loaded Banned IP") + } catch (e: IOException) { + logFault(e) + } + } + + fun saveBannedIP() { + if (isLoaded) { + getJSONMapper().writerWithDefaultPrettyPrinter().writeValue( + Paths.get("Banned IP.json").toFile().absoluteFile, + bannedIPStore + ) + logInfo("Saved Banned IP") + } + } + + fun putBannedIP(avatarIP: String, date: LocalDateTime) { + bannedIPStore[avatarIP] = date + } + + fun wipeBannedIP(avatarIP: String) { + bannedIPStore.remove(avatarIP) + } + + fun isBanned(avatarIP: String): Boolean { + return bannedIPStore.getOrDefault(avatarIP, LocalDateTime.MIN).isAfter(LocalDateTime.now()) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/taehui/twilight/system/BannedNote.kt b/src/main/kotlin/net/taehui/twilight/system/BannedNote.kt deleted file mode 100644 index 45fe6b8..0000000 --- a/src/main/kotlin/net/taehui/twilight/system/BannedNote.kt +++ /dev/null @@ -1,44 +0,0 @@ -package net.taehui.twilight.system - -import com.fasterxml.jackson.core.type.TypeReference -import com.fasterxml.jackson.databind.ObjectMapper -import net.taehui.twilight.Logger -import java.io.IOException -import java.nio.file.Paths -import java.util.concurrent.ConcurrentHashMap - -object BannedNote : Logger { - private val bannedNoteStore = mutableMapOf() - - fun loadBannedNote() { - try { - bannedNoteStore.putAll( - ObjectMapper().readValue( - Paths.get("Banned Note.json").toFile().absoluteFile, - object : TypeReference>() {}) - ) - logInfo("Loaded Banned Note") - saveBannedNote() - } catch (e: IOException) { - logFault(e) - } - } - - fun saveBannedNote() { - ObjectMapper().writerWithDefaultPrettyPrinter() - .writeValue(Paths.get("Banned Note.json").toFile().absoluteFile, bannedNoteStore) - logInfo("Saved Banned Note") - } - - fun putBannedNoteFile(noteID: String, commentary: String) { - bannedNoteStore[noteID] = commentary - } - - fun wipeBannedNoteFile(noteID: String) { - bannedNoteStore.remove(noteID) - } - - fun isBanned(noteID: String): Boolean { - return bannedNoteStore.contains(noteID) - } -} \ No newline at end of file diff --git a/src/main/kotlin/net/taehui/twilight/system/BannedNoteSystem.kt b/src/main/kotlin/net/taehui/twilight/system/BannedNoteSystem.kt new file mode 100644 index 0000000..9bab50b --- /dev/null +++ b/src/main/kotlin/net/taehui/twilight/system/BannedNoteSystem.kt @@ -0,0 +1,47 @@ +package net.taehui.twilight.system + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import net.taehui.twilight.Logger +import java.io.IOException +import java.nio.file.Paths +import java.util.concurrent.ConcurrentHashMap + +object BannedNoteSystem : Logger { + private val bannedNoteStore = ConcurrentHashMap() + private var isLoaded = false + + fun loadBannedNote() { + try { + bannedNoteStore.putAll( + ObjectMapper().readValue( + Paths.get("Banned Note.json").toFile().absoluteFile, + object : TypeReference>() {}) + ) + isLoaded = true + logInfo("Loaded Banned Note") + } catch (e: IOException) { + logFault(e) + } + } + + fun saveBannedNote() { + if (isLoaded) { + ObjectMapper().writerWithDefaultPrettyPrinter() + .writeValue(Paths.get("Banned Note.json").toFile().absoluteFile, bannedNoteStore) + logInfo("Saved Banned Note") + } + } + + fun putBannedNoteFile(noteID: String, commentary: String) { + bannedNoteStore[noteID] = commentary + } + + fun wipeBannedNoteFile(noteID: String) { + bannedNoteStore.remove(noteID) + } + + fun isBanned(noteID: String): Boolean { + return bannedNoteStore.contains(noteID) + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/taehui/twilight/system/DB.kt b/src/main/kotlin/net/taehui/twilight/system/DB.kt index 6366c09..f4865c5 100644 --- a/src/main/kotlin/net/taehui/twilight/system/DB.kt +++ b/src/main/kotlin/net/taehui/twilight/system/DB.kt @@ -1086,6 +1086,7 @@ ).use { dbStatement -> dbStatement.setString(1, noteID) dbStatement.executeQuery().use { rows -> + var avatarPlace = -1 while (rows.next()) { val rowAvatarID = rows.getString("Avatar_ID") if (ubuntuIDs?.contains(rowAvatarID) != false) { @@ -1120,20 +1121,17 @@ val isPaused = rows.getBoolean("Is_Paused") val inputFlags = rows.getInt("Input_Flags") + val avatarPlace = ++avatarPlace }) } } - var commentPlace = -1 val commentsMe = mutableListOf() comments.forEachIndexed { i, comment -> val isMe = comment.avatarID == avatarID if (viewUnit == -1 || commentsMe.size < viewUnit || isMe) { commentsMe.add(comment) - if (isMe) { - commentPlace = i - } } } @@ -1151,7 +1149,6 @@ val favor = favor val totalFavor = totalFavor val comments = commentsMe - val commentPlace = commentPlace val totalComments = comments.size } } diff --git a/src/main/kotlin/net/taehui/twilight/system/IO.kt b/src/main/kotlin/net/taehui/twilight/system/IO.kt index 6061d15..a07c619 100644 --- a/src/main/kotlin/net/taehui/twilight/system/IO.kt +++ b/src/main/kotlin/net/taehui/twilight/system/IO.kt @@ -120,7 +120,7 @@ w[2] ).let { if (it != null) { - BannedIP.putBannedIP( + BannedIPSystem.putBannedIP( it.remote, LocalDateTime.parse( w[3] ) @@ -136,7 +136,7 @@ "allow" -> { if (w.size > 2) { - BannedIP.wipeBannedIP(w[2]) + BannedIPSystem.wipeBannedIP(w[2]) continue } } @@ -312,7 +312,7 @@ "ban" -> { if (w.size > 3) { val noteID = w[2] - BannedNote.putBannedNoteFile(noteID, w[3]) + BannedNoteSystem.putBannedNoteFile(noteID, w[3]) DB.wipeNote(noteID, this) continue } @@ -320,7 +320,7 @@ "allow" -> { if (w.size > 2) { - BannedNote.wipeBannedNoteFile(w[2]) + BannedNoteSystem.wipeBannedNoteFile(w[2]) continue } } diff --git a/src/main/kotlin/net/taehui/twilight/system/LevelSystem.kt b/src/main/kotlin/net/taehui/twilight/system/LevelSystem.kt index 69b0800..8d751aa 100644 --- a/src/main/kotlin/net/taehui/twilight/system/LevelSystem.kt +++ b/src/main/kotlin/net/taehui/twilight/system/LevelSystem.kt @@ -11,6 +11,7 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArrayList import java.util.stream.Stream +import kotlin.io.path.exists object LevelSystem : Logger { class LevelGroup { @@ -154,18 +155,16 @@ avatarIDLevelNameMap.computeIfAbsent(avatarID) { mutableListOf() }.add(levelName) levelGroup.levelItems.forEach { levelItem -> val levelID = levelItem.levelID - try { - levelIDDrawingMap[levelID] = - Files.readAllBytes( - TwilightComponent.LEVEL_ENTRY_PATH.resolve(levelName) - .resolve("$levelID.png") - ) - } catch (_: IOException) { + val levelIDDrawingFilePath = TwilightComponent.LEVEL_ENTRY_PATH.resolve(levelName) + .resolve("$levelID.png") + if (levelIDDrawingFilePath.exists()) { + levelIDDrawingMap[levelID] = Files.readAllBytes(levelIDDrawingFilePath) } } logInfo("Loaded Level (${levelFilePath.fileName})") - jm.writerWithDefaultPrettyPrinter().writeValue(levelFilePath.toFile().absoluteFile, levelGroup) + jm.writerWithDefaultPrettyPrinter() + .writeValue(levelFilePath.toFile().absoluteFile, levelGroup) logInfo("Saved Level (${levelFilePath.fileName})") } catch (e: IOException) { logFault(e) diff --git a/src/main/kotlin/net/taehui/twilight/system/PlatformIDSystem.kt b/src/main/kotlin/net/taehui/twilight/system/PlatformIDSystem.kt index c3c037f..741a048 100644 --- a/src/main/kotlin/net/taehui/twilight/system/PlatformIDSystem.kt +++ b/src/main/kotlin/net/taehui/twilight/system/PlatformIDSystem.kt @@ -10,12 +10,7 @@ object PlatformIDSystem : Logger { private var platformIDAvatarIDMap = ConcurrentHashMap() private var avatarIDPlatformIDMap = ConcurrentHashMap() - - fun savePlatformID() { - ObjectMapper().writerWithDefaultPrettyPrinter() - .writeValue(Paths.get("Platform ID.json").toFile().absoluteFile, platformIDAvatarIDMap) - logInfo("Saved Platform ID") - } + private var isLoaded = false fun loadPlatformID() { try { @@ -26,12 +21,21 @@ platformIDAvatarIDMap.forEach { (platformID, avatarID) -> avatarIDPlatformIDMap[avatarID] = platformID } + isLoaded = true logInfo("Loaded Platform ID") } catch (e: IOException) { logFault(e) } } + fun savePlatformID() { + if (isLoaded) { + ObjectMapper().writerWithDefaultPrettyPrinter() + .writeValue(Paths.get("Platform ID.json").toFile().absoluteFile, platformIDAvatarIDMap) + logInfo("Saved Platform ID") + } + } + fun putPlatformID(platformID: String, avatarID: String) { platformIDAvatarIDMap[platformID] = avatarID avatarIDPlatformIDMap[avatarID] = platformID diff --git a/src/main/kotlin/net/taehui/twilight/system/PlatformSystem.kt b/src/main/kotlin/net/taehui/twilight/system/PlatformSystem.kt index b49876b..b3041bb 100644 --- a/src/main/kotlin/net/taehui/twilight/system/PlatformSystem.kt +++ b/src/main/kotlin/net/taehui/twilight/system/PlatformSystem.kt @@ -271,6 +271,7 @@ val twilightAbilitySiteYell = ObjectMapper().readValue(siteYellData.siteYell, JSON.TwilightAbilitySiteYell::class.java) val inputMode = when (twilightAbilitySiteYell.inputMode) { + Component.InputMode.INPUT_MODE_6 -> "6K" Component.InputMode.INPUT_MODE_5_1 -> "⑤K" Component.InputMode.INPUT_MODE_7_1 -> "⑦K" Component.InputMode.INPUT_MODE_9 -> "9K" diff --git a/src/main/kotlin/net/taehui/twilight/system/TVSystem.kt b/src/main/kotlin/net/taehui/twilight/system/TVSystem.kt index b5f4cfe..ecd6bb0 100644 --- a/src/main/kotlin/net/taehui/twilight/system/TVSystem.kt +++ b/src/main/kotlin/net/taehui/twilight/system/TVSystem.kt @@ -137,7 +137,6 @@ object : TypeReference>() {}) ) logInfo("Loaded Banned TV") - saveBannedTV() } catch (e: IOException) { logFault(e) } diff --git a/src/main/kotlin/net/taehui/twilight/www/WwwAvatar.kt b/src/main/kotlin/net/taehui/twilight/www/WwwAvatar.kt index 4a203ef..4190b6a 100644 --- a/src/main/kotlin/net/taehui/twilight/www/WwwAvatar.kt +++ b/src/main/kotlin/net/taehui/twilight/www/WwwAvatar.kt @@ -141,7 +141,7 @@ "/qwilight/www/comment" -> { val noteID = params.getOrDefault("noteID", "") - if (BannedNote.isBanned(noteID)) { + if (BannedNoteSystem.isBanned(noteID)) { send(ctx, object { val favor = null val totalFavor = 0 @@ -665,7 +665,7 @@ val noteFileData = ByteBufUtil.getBytes(msg.content()) var isBanned = false BaseCompiler.handleCompile(noteFileData, -1).forEach { - if (it.isBanned || BannedNote.isBanned(it.noteID)) { + if (it.isBanned || BannedNoteSystem.isBanned(it.noteID)) { isBanned = true } else { logFuture {