2019-01-11 03:19:08 +00:00
|
|
|
package com.smallhacker.disbrowser.resource
|
|
|
|
|
|
|
|
import com.smallhacker.disbrowser.Service
|
|
|
|
import com.smallhacker.disbrowser.asm.MetadataLine
|
2019-01-14 01:57:05 +00:00
|
|
|
import com.smallhacker.disbrowser.game.getGameSource
|
2019-01-20 10:11:30 +00:00
|
|
|
import com.smallhacker.disbrowser.memory.SnesAddress
|
2019-01-11 03:19:08 +00:00
|
|
|
import javax.ws.rs.Consumes
|
|
|
|
import javax.ws.rs.POST
|
|
|
|
import javax.ws.rs.Path
|
|
|
|
import javax.ws.rs.PathParam
|
2019-01-14 01:57:05 +00:00
|
|
|
import javax.ws.rs.core.Configuration
|
|
|
|
import javax.ws.rs.core.Context
|
2019-01-11 03:19:08 +00:00
|
|
|
import javax.ws.rs.core.MediaType
|
|
|
|
import javax.ws.rs.core.Response
|
|
|
|
|
2019-01-14 01:57:05 +00:00
|
|
|
@Path("/rest/{game}")
|
2019-01-11 03:19:08 +00:00
|
|
|
class RestResource {
|
2019-01-14 01:57:05 +00:00
|
|
|
@Context
|
|
|
|
private lateinit var config: Configuration
|
|
|
|
private val games by lazy { config.getGameSource() }
|
|
|
|
|
2019-01-11 03:19:08 +00:00
|
|
|
@POST
|
|
|
|
@Path("{address}/{field}")
|
|
|
|
@Consumes(MediaType.TEXT_PLAIN)
|
2019-01-14 01:57:05 +00:00
|
|
|
fun getIt(
|
|
|
|
@PathParam("game") gameName: String,
|
|
|
|
@PathParam("address") address: String,
|
|
|
|
@PathParam("field") fieldName: String,
|
|
|
|
body: String
|
|
|
|
): Response {
|
2019-01-11 16:35:35 +00:00
|
|
|
val parsedAddress = SnesAddress.parse(address) ?: return Response.status(400).build()
|
2019-01-11 03:19:08 +00:00
|
|
|
val field = when (fieldName) {
|
|
|
|
"preComment" -> MetadataLine::preComment
|
|
|
|
"comment" -> MetadataLine::comment
|
|
|
|
"label" -> MetadataLine::label
|
2019-01-14 01:57:05 +00:00
|
|
|
else -> return http404()
|
2019-01-11 03:19:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 01:57:05 +00:00
|
|
|
val game = games.getGame(gameName)
|
|
|
|
?: return http404()
|
|
|
|
Service.updateMetadata(game, parsedAddress, field, body)
|
2019-01-11 03:19:08 +00:00
|
|
|
|
|
|
|
return Response.noContent().build()
|
|
|
|
}
|
2019-01-14 01:57:05 +00:00
|
|
|
|
|
|
|
private fun http404(): Response = Response.status(404).build()
|
2019-01-11 03:19:08 +00:00
|
|
|
}
|