mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-10 13:51:53 +09:00
Some space saving, terrain reads GZip directly
This commit is contained in:
@@ -8,6 +8,7 @@ import com.jme3.math.FastMath
|
||||
import net.torvald.terrarum.*
|
||||
import net.torvald.terrarum.concurrent.ThreadParallel
|
||||
import net.torvald.terrarum.gameactors.roundInt
|
||||
import net.torvald.terrarum.gameworld.toUint
|
||||
import net.torvald.terrarum.mapdrawer.FeaturesDrawer.TILE_SIZE
|
||||
import net.torvald.terrarum.mapdrawer.LightmapRenderer.normaliseToColour
|
||||
import net.torvald.terrarum.mapdrawer.MapCamera.x
|
||||
@@ -17,7 +18,12 @@ import net.torvald.terrarum.mapdrawer.MapCamera.width
|
||||
import net.torvald.terrarum.realestate.LandUtil
|
||||
import org.lwjgl.opengl.GL11
|
||||
import org.newdawn.slick.*
|
||||
import org.newdawn.slick.opengl.*
|
||||
import java.io.FileInputStream
|
||||
import java.nio.ByteBuffer
|
||||
import java.util.*
|
||||
import java.util.zip.GZIPInputStream
|
||||
import java.util.zip.InflaterInputStream
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-01-19.
|
||||
@@ -27,10 +33,41 @@ object TilesDrawer {
|
||||
private val TILE_SIZE = FeaturesDrawer.TILE_SIZE
|
||||
private val TILE_SIZEF = FeaturesDrawer.TILE_SIZE.toFloat()
|
||||
|
||||
val tilesTerrain = SpriteSheet(ModMgr.getPath("basegame", "tiles/terrain.tga"), TILE_SIZE, TILE_SIZE)
|
||||
//val tilesTerrain = SpriteSheet(ModMgr.getPath("basegame", "tiles/terrain.tga"), TILE_SIZE, TILE_SIZE)
|
||||
// Slick has some weird quirks with PNG's transparency. I'm using 32-bit targa here.
|
||||
val tilesWire = SpriteSheet(ModMgr.getPath("basegame", "tiles/wire.tga"), TILE_SIZE, TILE_SIZE)
|
||||
|
||||
val tilesTerrain: SpriteSheet
|
||||
|
||||
init {
|
||||
// read DEFLATEd terrain.tar
|
||||
val tgaLoader = TGAImageData()
|
||||
val terrainImageData = tgaLoader.loadImage(
|
||||
GZIPInputStream(
|
||||
FileInputStream(ModMgr.getFile("basegame", "tiles/terrain.tga.gz")),
|
||||
8192
|
||||
), false, null)
|
||||
terrainImageData.rewind()
|
||||
/*val terrainTex = InternalTextureLoader.get().getTexture(object : ImageData {
|
||||
override fun getHeight(): Int = tgaLoader.height
|
||||
override fun getTexWidth(): Int = tgaLoader.texWidth
|
||||
override fun getDepth(): Int = tgaLoader.depth
|
||||
override fun getImageBufferData(): ByteBuffer = terrainImageData
|
||||
override fun getWidth(): Int = tgaLoader.width
|
||||
override fun getTexHeight(): Int = tgaLoader.texHeight
|
||||
}, Image.FILTER_NEAREST)*/
|
||||
|
||||
//// method 1
|
||||
//val terrainImage = Image(terrainTex)
|
||||
//tilesTerrain = SpriteSheet(terrainImage, TILE_SIZE, TILE_SIZE)
|
||||
|
||||
//// method 2
|
||||
val terrainImgBuffer = ImageBuffer(tgaLoader.width, tgaLoader.height)
|
||||
terrainImageData.get(terrainImgBuffer.rgba)
|
||||
tilesTerrain = SpriteSheet(terrainImgBuffer.image, TILE_SIZE, TILE_SIZE)
|
||||
}
|
||||
|
||||
|
||||
val breakAnimSteps = 10
|
||||
|
||||
val WALL = GameWorld.WALL
|
||||
|
||||
@@ -468,15 +468,19 @@ object VDUtil {
|
||||
}
|
||||
/**
|
||||
* Add subdirectory to the specified directory.
|
||||
*
|
||||
* @return EntryID of newly created directory
|
||||
*/
|
||||
fun addDir(disk: VirtualDisk, parentPath: VDPath, name: ByteArray) {
|
||||
fun addDir(disk: VirtualDisk, parentPath: VDPath, name: ByteArray): EntryID {
|
||||
val parentID = getFile(disk, parentPath)!!.entryID
|
||||
return addDir(disk, parentID, name)
|
||||
}
|
||||
/**
|
||||
* Add file to the specified directory.
|
||||
*
|
||||
* @return EntryID of newly created directory
|
||||
*/
|
||||
fun addDir(disk: VirtualDisk, directoryID: EntryID, name: ByteArray) {
|
||||
fun addDir(disk: VirtualDisk, parentDir: EntryID, name: ByteArray): EntryID {
|
||||
disk.checkReadOnly()
|
||||
disk.checkCapacity(EntryDirectory.NEW_ENTRY_SIZE)
|
||||
|
||||
@@ -484,16 +488,18 @@ object VDUtil {
|
||||
|
||||
try {
|
||||
// add record to the directory
|
||||
getAsDirectory(disk, directoryID).add(newID)
|
||||
getAsDirectory(disk, parentDir).add(newID)
|
||||
// add entry on the disk
|
||||
disk.entries[newID] = DiskEntry(
|
||||
newID,
|
||||
directoryID,
|
||||
parentDir,
|
||||
name,
|
||||
currentUnixtime,
|
||||
currentUnixtime,
|
||||
EntryDirectory()
|
||||
)
|
||||
|
||||
return newID
|
||||
}
|
||||
catch (e: KotlinNullPointerException) {
|
||||
throw FileNotFoundException("No such directory")
|
||||
@@ -519,6 +525,8 @@ object VDUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
val entry = disk.entries[directoryID]
|
||||
if (entry != null && entry.contents is EntryDirectory) {
|
||||
entry.contents.forEach {
|
||||
@@ -543,20 +551,49 @@ object VDUtil {
|
||||
/**
|
||||
* Imports external file and returns corresponding DiskEntry.
|
||||
*/
|
||||
fun importFile(file: File, id: EntryID): DiskEntry {
|
||||
fun importFile(file: File, newID: EntryID, charset: Charset): DiskEntry {
|
||||
if (file.isDirectory) {
|
||||
throw IOException("The file is a directory")
|
||||
}
|
||||
|
||||
return DiskEntry(
|
||||
entryID = id,
|
||||
entryID = newID,
|
||||
parentEntryID = 0, // placeholder
|
||||
filename = file.name.toByteArray(),
|
||||
filename = file.name.toEntryName(DiskEntry.NAME_LENGTH, charset),
|
||||
creationDate = currentUnixtime,
|
||||
modificationDate = currentUnixtime,
|
||||
contents = EntryFile(file.readBytes64())
|
||||
)
|
||||
}
|
||||
|
||||
fun importDirRecurse(disk: VirtualDisk, dir: File, path: VDPath, charset: Charset) =
|
||||
importDirRecurse(disk, dir, getFile(disk, path)!!.entryID, charset)
|
||||
|
||||
fun importDirRecurse(disk: VirtualDisk, dir: File, superNode: EntryID, charset: Charset, newName: String? = null) {
|
||||
fun recurse1(file: File, node: EntryID) {
|
||||
// return conditions
|
||||
if (!file.isDirectory) {
|
||||
// if not a directory, add to node
|
||||
val importedFile = importFile(file, disk.generateUniqueID(), charset)
|
||||
addFile(disk, node, importedFile)
|
||||
return
|
||||
}
|
||||
// recurse
|
||||
else {
|
||||
// mkdir
|
||||
val newDir = addDir(disk, node, file.name.toEntryName(DiskEntry.NAME_LENGTH, charset))
|
||||
// for entries in this fileDirectory...
|
||||
file.listFiles().forEach { recurse1(it, newDir) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// mkdir to superNode
|
||||
val newDir = addDir(disk, superNode, (newName ?: dir.name).toEntryName(DiskEntry.NAME_LENGTH, charset))
|
||||
// for entries in this fileDirectory...
|
||||
dir.listFiles().forEach { recurse1(it, newDir) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Export file on the virtual disk into real disk.
|
||||
*/
|
||||
@@ -565,6 +602,33 @@ object VDUtil {
|
||||
outfile.writeBytes64(entryFile.bytes)
|
||||
}
|
||||
|
||||
fun exportDirRecurse(disk: VirtualDisk, parentDir: EntryID, outfile: File, charset: Charset) {
|
||||
fun recurse1(file: DiskEntry, dir: File) {
|
||||
// return conditions
|
||||
if (file.contents is EntryFile) {
|
||||
// if not a directory, write as file
|
||||
val newFile = File(dir, file.getFilenameString(charset))
|
||||
newFile.writeBytes64(file.contents.bytes)
|
||||
return
|
||||
}
|
||||
// recurse
|
||||
else if (file.contents is EntryDirectory) {
|
||||
// mkdir
|
||||
val newDir = File(dir, file.getFilenameString(charset))
|
||||
newDir.mkdir()
|
||||
// for entries in this fileDirectory...
|
||||
file.contents.forEach { recurse1(disk.entries[it]!!, newDir) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// mkdir to superNode
|
||||
val newDir = File(outfile, disk.entries[parentDir]!!.getFilenameString(charset))
|
||||
newDir.mkdir()
|
||||
// for entries in this fileDirectory...
|
||||
getDirectoryEntries(disk, parentDir).forEach { recurse1(it, newDir) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for name collision in specified directory.
|
||||
*/
|
||||
@@ -765,7 +829,8 @@ object VDUtil {
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
throw InternalError("Aw, snap! Here's what it says:\n$e")
|
||||
e.printStackTrace()
|
||||
throw InternalError("Aw, snap!")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -779,7 +844,8 @@ object VDUtil {
|
||||
}
|
||||
}
|
||||
catch (e: Exception) {
|
||||
throw InternalError("Aw, snap! Here's what it says:\n$e")
|
||||
e.printStackTrace()
|
||||
throw InternalError("Aw, snap!")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ class EntryDirectory(private val entries: ArrayList<EntryID> = ArrayList<EntryID
|
||||
}
|
||||
|
||||
fun remove(entryID: EntryID) {
|
||||
entries.removeAt(entryID)
|
||||
entries.remove(entryID)
|
||||
}
|
||||
|
||||
fun contains(entryID: EntryID) = entries.contains(entryID)
|
||||
|
||||
Reference in New Issue
Block a user