mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
more chunk pool codes
This commit is contained in:
@@ -29,7 +29,7 @@ import kotlin.math.roundToInt
|
|||||||
class SavegameCollection(files0: List<DiskSkimmer>) {
|
class SavegameCollection(files0: List<DiskSkimmer>) {
|
||||||
|
|
||||||
/** Sorted in reverse by the last modified time of the files, index zero being the most recent */
|
/** Sorted in reverse by the last modified time of the files, index zero being the most recent */
|
||||||
val files = files0.sortedBy { it.diskFile.name }.sortedByDescending {
|
val files: List<DiskSkimmer> = files0.sortedBy { it.diskFile.name }.sortedByDescending {
|
||||||
it.getLastModifiedTime().shl(2) or
|
it.getLastModifiedTime().shl(2) or
|
||||||
it.diskFile.extension.matches(Regex("^[abc]${'$'}")).toLong(1) or
|
it.diskFile.extension.matches(Regex("^[abc]${'$'}")).toLong(1) or
|
||||||
it.diskFile.extension.isBlank().toLong(0)
|
it.diskFile.extension.isBlank().toLong(0)
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package net.torvald.terrarum.gameworld
|
package net.torvald.terrarum.gameworld
|
||||||
|
|
||||||
|
import net.torvald.terrarum.App
|
||||||
import net.torvald.terrarum.INGAME
|
import net.torvald.terrarum.INGAME
|
||||||
import net.torvald.terrarum.Point2i
|
import net.torvald.terrarum.Point2i
|
||||||
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
|
|
||||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.ClusteredFormatDOM
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.ClusteredFormatDOM
|
||||||
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.Clustfile
|
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.archivers.Clustfile
|
||||||
import net.torvald.terrarum.realestate.LandUtil
|
import net.torvald.terrarum.realestate.LandUtil
|
||||||
import net.torvald.terrarum.realestate.LandUtil.CHUNK_H
|
import net.torvald.terrarum.realestate.LandUtil.CHUNK_H
|
||||||
import net.torvald.terrarum.realestate.LandUtil.CHUNK_W
|
import net.torvald.terrarum.realestate.LandUtil.CHUNK_W
|
||||||
|
import net.torvald.terrarum.savegame.DiskEntry
|
||||||
|
import net.torvald.terrarum.savegame.DiskSkimmer
|
||||||
|
import net.torvald.terrarum.savegame.EntryFile
|
||||||
import net.torvald.terrarum.serialise.Common
|
import net.torvald.terrarum.serialise.Common
|
||||||
import net.torvald.terrarum.serialise.toUint
|
import net.torvald.terrarum.serialise.toUint
|
||||||
import net.torvald.unsafe.UnsafeHelper
|
import net.torvald.unsafe.UnsafeHelper
|
||||||
@@ -37,7 +40,8 @@ enum class ChunkAllocClass {
|
|||||||
* Created by minjaesong on 2024-09-07.
|
* Created by minjaesong on 2024-09-07.
|
||||||
*/
|
*/
|
||||||
open class ChunkPool(
|
open class ChunkPool(
|
||||||
val DOM: ClusteredFormatDOM,
|
// `DiskSkimmer` or `ClusteredFormatDOM`
|
||||||
|
val disk: Any,
|
||||||
val wordSizeInBytes: Long,
|
val wordSizeInBytes: Long,
|
||||||
val world: GameWorld,
|
val world: GameWorld,
|
||||||
val chunkNumToFileNum: (Int) -> String,
|
val chunkNumToFileNum: (Int) -> String,
|
||||||
@@ -165,18 +169,47 @@ open class ChunkPool(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun fetchFromDisk(chunkNumber: Int) {
|
private fun fetchFromDisk(chunkNumber: Int) {
|
||||||
// read data from the disk
|
|
||||||
val fileName = chunkNumToFileNum(chunkNumber)
|
val fileName = chunkNumToFileNum(chunkNumber)
|
||||||
Clustfile(DOM, fileName).let {
|
|
||||||
val bytes = Common.unzip(it.readBytes())
|
// read data from the disk
|
||||||
val ptr = allocate(chunkNumber)
|
if (disk is ClusteredFormatDOM) {
|
||||||
UnsafeHelper.memcpyFromArrToPtr(bytes, 0, ptr.ptr, bytes.size)
|
Clustfile(disk, fileName).let {
|
||||||
renumber(ptr)
|
val bytes = Common.unzip(it.readBytes())
|
||||||
|
val ptr = allocate(chunkNumber)
|
||||||
|
UnsafeHelper.memcpyFromArrToPtr(bytes, 0, ptr.ptr, bytes.size)
|
||||||
|
renumber(ptr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (disk is DiskSkimmer) {
|
||||||
|
val fileID = fileName.toLong()
|
||||||
|
disk.getFile(fileID)!!.let {
|
||||||
|
val bytes = Common.unzip(it.bytes)
|
||||||
|
val ptr = allocate(chunkNumber)
|
||||||
|
UnsafeHelper.memcpyFromArrToPtr(bytes, 0, ptr.ptr, bytes.size)
|
||||||
|
renumber(ptr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun storeToDisk(chunkNumber: Int) {
|
private fun storeToDisk(chunkNumber: Int) {
|
||||||
TODO()
|
val fileName = chunkNumToFileNum(chunkNumber)
|
||||||
|
|
||||||
|
// write to the disk (the disk must be an autosaving copy of the original)
|
||||||
|
if (disk is ClusteredFormatDOM) {
|
||||||
|
Clustfile(disk, fileName).let {
|
||||||
|
val bytes = Common.zip(serialise(chunkNumber).iterator())
|
||||||
|
it.overwrite(bytes.toByteArray())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// append the new entry
|
||||||
|
else if (disk is DiskSkimmer) {
|
||||||
|
val fileID = fileName.toLong()
|
||||||
|
|
||||||
|
val bytes = Common.zip(serialise(chunkNumber).iterator())
|
||||||
|
val oldEntry = disk.getEntry(fileID)
|
||||||
|
val timeNow = App.getTIME_T()
|
||||||
|
disk.appendEntry(DiskEntry(fileID, 0L, oldEntry?.creationDate ?: timeNow, timeNow, EntryFile(bytes)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkForChunk(chunkNumber: Int) {
|
private fun checkForChunk(chunkNumber: Int) {
|
||||||
@@ -185,6 +218,14 @@ open class ChunkPool(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun serialise(chunkNumber: Int): ByteArray {
|
||||||
|
val ptr = pointers[chunkNumber]!!
|
||||||
|
val out = ByteArray(chunkSize.toInt())
|
||||||
|
UnsafeHelper.memcpyFromPtrToArr(ptr, out, 0, chunkSize)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given the word-aligned byte sequence of `[B0, B1, B2, B3, ...]`,
|
* Given the word-aligned byte sequence of `[B0, B1, B2, B3, ...]`,
|
||||||
* Return format:
|
* Return format:
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ class DiskSkimmer(
|
|||||||
noInit: Boolean = false
|
noInit: Boolean = false
|
||||||
): SimpleFileSystem {
|
): SimpleFileSystem {
|
||||||
|
|
||||||
|
|
||||||
override fun getBackingFile() = diskFile
|
override fun getBackingFile() = diskFile
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -50,6 +50,10 @@ internal object UnsafeHelper {
|
|||||||
unsafe.copyMemory(srcObj, getArrayOffset(srcObj) + startIndex, null, destPos, len.toLong())
|
unsafe.copyMemory(srcObj, getArrayOffset(srcObj) + startIndex, null, destPos, len.toLong())
|
||||||
fun memcpyFromArrToPtr(srcObj: Any, startIndex: Int, destPos: Long, len: Long) =
|
fun memcpyFromArrToPtr(srcObj: Any, startIndex: Int, destPos: Long, len: Long) =
|
||||||
unsafe.copyMemory(srcObj, getArrayOffset(srcObj) + startIndex, null, destPos, len)
|
unsafe.copyMemory(srcObj, getArrayOffset(srcObj) + startIndex, null, destPos, len)
|
||||||
|
fun memcpyFromPtrToArr(srcPos: Long, destObj: Any, startIndex: Int, len: Int) =
|
||||||
|
unsafe.copyMemory(null, srcPos, destObj, getArrayOffset(destObj) + startIndex, len.toLong())
|
||||||
|
fun memcpyFromPtrToArr(srcPos: Long, destObj: Any, startIndex: Int, len: Long) =
|
||||||
|
unsafe.copyMemory(null, srcPos, destObj, getArrayOffset(destObj) + startIndex, len)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The array object in JVM is stored in this memory map:
|
* The array object in JVM is stored in this memory map:
|
||||||
|
|||||||
Reference in New Issue
Block a user