<commit_before>package rpgboss.cache

import rpgboss.model._

import java.awt.image._
import com.google.common.cache._

class TileCache(proj: Project, map: RpgMap, cacheMaxSize: Int) {
  val autotiles = proj.autotiles.map(Autotile.readFromDisk(proj, _))
  val tilesets  = map.tilesets.map(Tileset.readFromDisk(proj, _))
  
  val cache = CacheBuilder.newBuilder()
    .concurrencyLevel(1)
    .softValues()
    .maximumSize(cacheMaxSize)
    .expireAfterWrite(10, java.util.concurrent.TimeUnit.MINUTES)
    .build(new CacheLoader[Integer, BufferedImage] {
      def load(tilecode: Integer) = {
        val tilesetIdx = (tilecode >>> 24).asInstanceOf[Byte]
        val secondByte = (tilecode >>> 16)&0xff
        val thirdByte = (tilecode >>> 8)&0xff
        val frame = tilecode.asInstanceOf[Byte] // just the last bits
        
        if(tilesetIdx == RpgMap.autotileByte) {
          // Autotile
          val autotileNum = secondByte
          val autotileConfig = thirdByte
          
          if(autotiles.length > autotileNum) {
            autotiles(autotileNum).getTile(autotileConfig, frame)
          } else ImageResource.errorTile
          
        } else if(tilesetIdx >= 0) {
          // Regular tile
          val x = secondByte
          val y = thirdByte
          
          if(tilesets.length > tilesetIdx) {
            tilesets(tilesetIdx).getTile(x, y)
            
          } else ImageResource.errorTile
        } else ImageResource.errorTile
      }
    })
  
  // frame here means the animation frame
  def getTileImage(mapData: Array[Byte], bi: Int, frame: Byte = 0) = {
    val tilecode : Int = 
      ((mapData(bi)&0xff) << 24) |
      ((mapData(bi+1)&0xff) << 16) |
      ((mapData(bi+2)&0xff) << 8) |
      ((frame&0xff))
    
    cache.get(new Integer(tilecode))
  }
}
<commit_msg>Add tile cache, will remove now, as unneeded i think<commit_after>package rpgboss.cache

import rpgboss.model._

import java.awt.image._
import com.google.common.cache._

class TileCache(proj: Project, map: RpgMap, cacheMaxSize: Int) {
  val autotiles = proj.autotiles.map(Autotile.readFromDisk(proj, _))
  val tilesets  = map.tilesets.map(Tileset.readFromDisk(proj, _))
  
  val cache = CacheBuilder.newBuilder()
    .concurrencyLevel(1)
    .softValues()
    .maximumSize(cacheMaxSize)
    .expireAfterWrite(10, java.util.concurrent.TimeUnit.MINUTES)
    .build(new CacheLoader[(Byte, Byte, Byte, Byte), BufferedImage] {
      def load(tileTuple: (Byte, Byte, Byte, Byte)) = {
        val (tilesetIdx, secondByte, thirdByte, frame) = tileTuple
        val secondBytePos = secondByte & 0xff
        val thirdBytePos = thirdByte & 0xff
        
        if(tilesetIdx == RpgMap.autotileByte) {
          // Autotile
          val autotileNum = secondBytePos
          val autotileConfig = thirdBytePos
          
          if(autotiles.length > autotileNum) {
            autotiles(autotileNum).getTile(autotileConfig, frame)
          } else ImageResource.errorTile
          
        } else if(tilesetIdx >= 0) {
          // Regular tile
          val x = secondBytePos
          val y = thirdBytePos
          
          if(tilesets.length > tilesetIdx) {
            tilesets(tilesetIdx).getTile(x, y)
            
          } else ImageResource.errorTile
        } else ImageResource.errorTile
      }
    })
  
  // frame here means the animation frame
  def getTileImage(mapData: Array[Byte], bi: Int, frame: Byte = 0) =
    cache.get((mapData(bi), mapData(bi+1), mapData(bi+2), frame))
}

{'fraction_non_alphanumeric': 0.109624145785877, 'fraction_numerical': 0.009111617312072893, 'mean_word_length': 2.4306640625, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, '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': '1360064', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1133, 'n_words': 230}