Open-source multiplayer game server compatible with the RuneScape client https://www.openrs2.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
openrs2/game/src/main/kotlin/org/openrs2/game/net/FileProvider.kt

53 lines
1.4 KiB

package org.openrs2.game.net
import io.netty.channel.DefaultFileRegion
import io.netty.channel.FileRegion
import java.nio.channels.FileChannel
import java.nio.file.Files
import java.nio.file.Path
import javax.inject.Singleton
@Singleton
public class FileProvider {
public fun get(uri: String): FileRegion? {
if (!uri.startsWith("/")) {
return null
}
var path = ROOT.resolve(uri.substring(1)).toAbsolutePath().normalize()
if (!path.startsWith(ROOT)) {
return null
}
if (!Files.exists(path)) {
path = stripChecksum(path)
}
if (!Files.isRegularFile(path)) {
return null
}
val channel = FileChannel.open(path)
return DefaultFileRegion(channel, 0, channel.size())
}
private fun stripChecksum(path: Path): Path {
val name = path.fileName.toString()
val extensionIndex = name.lastIndexOf('.')
if (extensionIndex == -1) {
return path
}
val checksumIndex = name.lastIndexOf('_', extensionIndex)
if (checksumIndex == -1) {
return path
}
return path.resolveSibling(name.substring(0, checksumIndex) + name.substring(extensionIndex))
}
private companion object {
private val ROOT = Path.of("nonfree/var/cache/client").toAbsolutePath().normalize()
}
}