<commit_before>package plugin

import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInitializer
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.channel.socket.SocketChannel
import me.ryleykimmel.brandywine.Server
import me.ryleykimmel.brandywine.fs.FileSystem
import me.ryleykimmel.brandywine.game.GameService
import me.ryleykimmel.brandywine.game.auth.AuthenticationService
import me.ryleykimmel.brandywine.game.event.Consumes
import me.ryleykimmel.brandywine.game.event.EventConsumer
import me.ryleykimmel.brandywine.game.event.EventConsumerChainSet
import me.ryleykimmel.brandywine.game.model.World
import me.ryleykimmel.brandywine.game.model.player.Player
import me.ryleykimmel.brandywine.network.ResponseCode
import me.ryleykimmel.brandywine.network.Session
import me.ryleykimmel.brandywine.network.frame.codec.FrameCodec
import me.ryleykimmel.brandywine.network.frame.codec.FrameMessageCodec
import me.ryleykimmel.brandywine.network.msg.Message
import org.reflections.Reflections
import org.slf4j.LoggerFactory
import org.sql2o.Sql2o
import plugin.message.MessageRegistrar
import plugin.task.Tasks
import java.io.IOException
import java.io.UncheckedIOException
import kotlin.reflect.KClass

val world = World(EventConsumerChainSet())
val server = Server()
val logger = LoggerFactory.getLogger("plugin-bootstrap")!!

fun main(vararg args: String) = try {
    Tasks.schedule()
    MessageRegistrar.init()

    server.initializer(object : ChannelInitializer<SocketChannel>() {
        override fun initChannel(channel: SocketChannel) {
            val session = Session(channel)
            channel.pipeline().addLast("frame_codec", FrameCodec(session, MessageRegistrar.loginMetadata)).
                    addLast("message_codec", FrameMessageCodec(MessageRegistrar.loginMetadata)).
                    addLast("handler", SessionHandler(session))
        }
    })

    val gameService = GameService(world)
    server.setFileSystem(FileSystem.create("data/fs/")).
            setSql2o(Sql2o("jdbc:mysql://localhost/game_server", "root", "")).
            setAuthenticationStrategy({ ResponseCode.STATUS_OK }).
            registerService(gameService).registerService(AuthenticationService(gameService, server.authenticationStrategy)).
            init(43594)
} catch (cause: IOException) {
    throw UncheckedIOException(cause)
}

class SessionHandler(val session: Session) : SimpleChannelInboundHandler<Message>() {

    // Required extension method, Kotlin does not natively support fetching object.getClass()
    val Any.kt: KClass<out Any>
        get() = javaClass.kotlin

    override fun channelInactive(ctx: ChannelHandlerContext) {
        val player = session.attr(Player.ATTRIBUTE_KEY).get()
        if (player != null) {
            server.getService(GameService::class).removePlayer(player)
        }
    }

    override fun channelRead0(ctx: ChannelHandlerContext, message: Message) {
        val player = session.attr(Player.ATTRIBUTE_KEY).get()
        val listener = MessageRegistrar.messages[message.kt]
        listener?.handle(player ?: session, message)
    }

    override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
        logger.error("Uncaught exception occurred upstream", cause)
        session.close()
    }

}<commit_msg>As of Kotlin 1.1 this is no longer required<commit_after>package plugin

import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInitializer
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.channel.socket.SocketChannel
import me.ryleykimmel.brandywine.Server
import me.ryleykimmel.brandywine.fs.FileSystem
import me.ryleykimmel.brandywine.game.GameService
import me.ryleykimmel.brandywine.game.auth.AuthenticationService
import me.ryleykimmel.brandywine.game.event.EventConsumerChainSet
import me.ryleykimmel.brandywine.game.model.World
import me.ryleykimmel.brandywine.game.model.player.Player
import me.ryleykimmel.brandywine.network.ResponseCode
import me.ryleykimmel.brandywine.network.Session
import me.ryleykimmel.brandywine.network.frame.codec.FrameCodec
import me.ryleykimmel.brandywine.network.frame.codec.FrameMessageCodec
import me.ryleykimmel.brandywine.network.msg.Message
import org.slf4j.LoggerFactory
import org.sql2o.Sql2o
import plugin.message.MessageRegistrar
import plugin.task.Tasks
import java.io.IOException
import java.io.UncheckedIOException

val world = World(EventConsumerChainSet())
val server = Server()
val logger = LoggerFactory.getLogger("plugin-bootstrap")!!

fun main(args: Array<String>) = try {
    Tasks.schedule()
    MessageRegistrar.init()

    server.initializer(object : ChannelInitializer<SocketChannel>() {
        override fun initChannel(channel: SocketChannel) {
            val session = Session(channel)
            channel.pipeline().addLast("frame_codec", FrameCodec(session, MessageRegistrar.loginMetadata)).
                    addLast("message_codec", FrameMessageCodec(MessageRegistrar.loginMetadata)).
                    addLast("handler", SessionHandler(session))
        }
    })

    val gameService = GameService(world)
    server.setFileSystem(FileSystem.create("data/fs/")).
            setSql2o(Sql2o("jdbc:mysql://localhost/game_server", "root", "")).
            setAuthenticationStrategy({ ResponseCode.STATUS_OK }).
            registerService(gameService).registerService(AuthenticationService(gameService, server.authenticationStrategy)).
            init(43594)
} catch (cause: IOException) {
    throw UncheckedIOException(cause)
}

class SessionHandler(val session: Session) : SimpleChannelInboundHandler<Message>() {

    override fun channelInactive(ctx: ChannelHandlerContext) {
        val player = session.attr(Player.ATTRIBUTE_KEY).get()
        if (player != null) {
            server.getService(GameService::class).removePlayer(player)
        }
    }

    override fun channelRead0(ctx: ChannelHandlerContext, message: Message) {
        val player = session.attr(Player.ATTRIBUTE_KEY).get()
        val listener = MessageRegistrar.messages[message::class]
        listener?.handle(player ?: session, message)
    }

    override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
        logger.error("Uncaught exception occurred upstream", cause)
        session.close()
    }

}
{'fraction_non_alphanumeric': 0.09932208734037522, 'fraction_numerical': 0.0037836985653476275, 'mean_word_length': 4.376271186440678, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2220881', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1801, 'n_words': 305}