Skip to content

Commit

Permalink
Move out image location to separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsupermanhd committed Dec 12, 2023
1 parent 3cba983 commit ffd43da
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
8 changes: 4 additions & 4 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"runtime/debug"

"github.com/maxsupermanhd/WebChunk/chunkStorage"
imagecache "github.com/maxsupermanhd/WebChunk/imageCache"
"github.com/maxsupermanhd/WebChunk/primitives"
"github.com/nfnt/resize"
)

func imageGetSync(loc imagecache.ImageLocation, ignoreCache bool) (*image.RGBA, error) {
func imageGetSync(loc primitives.ImageLocation, ignoreCache bool) (*image.RGBA, error) {
if !ignoreCache {
i := imageCacheGetBlockingLoc(loc)
if i != nil {
Expand All @@ -28,7 +28,7 @@ func imageGetSync(loc imagecache.ImageLocation, ignoreCache bool) (*image.RGBA,
return img, err
}

func renderTile(loc imagecache.ImageLocation) (*image.RGBA, error) {
func renderTile(loc primitives.ImageLocation) (*image.RGBA, error) {

f := findTTypeProviderFunc(loc)
if f == nil {
Expand Down Expand Up @@ -92,7 +92,7 @@ func renderTile(loc imagecache.ImageLocation) (*image.RGBA, error) {
return img, nil
}

func findTTypeProviderFunc(loc imagecache.ImageLocation) *ttypeProviderFunc {
func findTTypeProviderFunc(loc primitives.ImageLocation) *ttypeProviderFunc {
for tt := range ttypes {
if tt.Name == loc.Variant {
f := ttypes[tt]
Expand Down
10 changes: 5 additions & 5 deletions imageCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package main
import (
"image"

imagecache "github.com/maxsupermanhd/WebChunk/imageCache"
"github.com/maxsupermanhd/WebChunk/primitives"
)

func imageCacheGetBlockingLoc(loc imagecache.ImageLocation) *image.RGBA {
func imageCacheGetBlockingLoc(loc primitives.ImageLocation) *image.RGBA {
return ic.GetCachedImageBlocking(loc).Img
}

func imageCacheGetBlocking(wname, dname, variant string, cs, cx, cz int) *image.RGBA {
return ic.GetCachedImageBlocking(imagecache.ImageLocation{
return ic.GetCachedImageBlocking(primitives.ImageLocation{
World: wname,
Dimension: dname,
Variant: variant,
Expand All @@ -21,12 +21,12 @@ func imageCacheGetBlocking(wname, dname, variant string, cs, cx, cz int) *image.
}).Img
}

func imageCacheSaveLoc(img *image.RGBA, loc imagecache.ImageLocation) {
func imageCacheSaveLoc(img *image.RGBA, loc primitives.ImageLocation) {
ic.SetCachedImage(loc, img)
}

func imageCacheSave(img *image.RGBA, wname, dname, variant string, cs, cx, cz int) {
imageCacheSaveLoc(img, imagecache.ImageLocation{
imageCacheSaveLoc(img, primitives.ImageLocation{
World: wname,
Dimension: dname,
Variant: variant,
Expand Down
39 changes: 15 additions & 24 deletions imageCache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package imagecache
import (
"container/list"
"context"
"fmt"
"image"
"image/draw"
"io"
Expand All @@ -12,6 +11,7 @@ import (
"sync/atomic"
"time"

"github.com/maxsupermanhd/WebChunk/primitives"
"github.com/maxsupermanhd/lac"
)

Expand All @@ -38,26 +38,17 @@ func IN(cx, cz int) (int, int) {
return cx & 31, cz & 31
}

type ImageLocation struct {
World, Dimension, Variant string
S, X, Z int
}

func (i ImageLocation) String() string {
return fmt.Sprintf("{%s:%s:%s at %ds %dx %dz}", i.World, i.Dimension, i.Variant, i.S, i.X, i.Z)
}

type CachedImage struct {
Img *image.RGBA
Loc ImageLocation
Loc primitives.ImageLocation
SyncedToDisk bool
lastUse time.Time
ModTime time.Time
imageUnloaded bool
}

type cacheTask struct {
loc ImageLocation
loc primitives.ImageLocation
img *image.RGBA
ret chan *CachedImage
}
Expand All @@ -70,8 +61,8 @@ type ImageCache struct {
tasks chan *cacheTask
ioTasks chan *cacheTaskIO
ioReturn chan *cacheTaskIO
cache map[ImageLocation]*CachedImage
cacheReturn map[ImageLocation][]*cacheTask
cache map[primitives.ImageLocation]*CachedImage
cacheReturn map[primitives.ImageLocation][]*cacheTask
backlog *list.List
wg sync.WaitGroup
cacheStatLen atomic.Int64
Expand All @@ -93,8 +84,8 @@ func NewImageCache(logger *log.Logger, cfg *lac.ConfSubtree, ctx context.Context
tasks: make(chan *cacheTask, taskQueueLen),
ioTasks: make(chan *cacheTaskIO, ioQueueLen),
ioReturn: make(chan *cacheTaskIO, ioQueueLen),
cache: map[ImageLocation]*CachedImage{},
cacheReturn: map[ImageLocation][]*cacheTask{},
cache: map[primitives.ImageLocation]*CachedImage{},
cacheReturn: map[primitives.ImageLocation][]*cacheTask{},
backlog: list.New(),
}
c.wg.Add(ioProcessors)
Expand Down Expand Up @@ -239,9 +230,9 @@ func (c *ImageCache) processSmallerImageGet(task *cacheTask) {
c.cacheReturn[loc] = r
}

func getStorageLevelLoc(loc ImageLocation) ImageLocation {
func getStorageLevelLoc(loc primitives.ImageLocation) primitives.ImageLocation {
rx, rz := AT(loc.X*powarr[loc.S], loc.Z*powarr[loc.S])
return ImageLocation{
return primitives.ImageLocation{
World: loc.World,
Dimension: loc.Dimension,
Variant: loc.Variant,
Expand All @@ -251,7 +242,7 @@ func getStorageLevelLoc(loc ImageLocation) ImageLocation {
}
}

func copySmallerCachedImage(img *CachedImage, target ImageLocation) *CachedImage {
func copySmallerCachedImage(img *CachedImage, target primitives.ImageLocation) *CachedImage {
return &CachedImage{
Img: copyFragmentRGBA(img.Img, target),
Loc: img.Loc,
Expand All @@ -262,7 +253,7 @@ func copySmallerCachedImage(img *CachedImage, target ImageLocation) *CachedImage
}
}

func copyFragmentRGBA(from *image.RGBA, target ImageLocation) *image.RGBA {
func copyFragmentRGBA(from *image.RGBA, target primitives.ImageLocation) *image.RGBA {
if from == nil {
return nil
}
Expand Down Expand Up @@ -370,7 +361,7 @@ func (c *ImageCache) processCacheLoad(t *CachedImage, task *cacheTaskIO) {
t.SyncedToDisk = true
}

func (c *ImageCache) SetCachedImage(loc ImageLocation, img *image.RGBA) {
func (c *ImageCache) SetCachedImage(loc primitives.ImageLocation, img *image.RGBA) {
if img == nil {
return // dumbass
}
Expand All @@ -381,7 +372,7 @@ func (c *ImageCache) SetCachedImage(loc ImageLocation, img *image.RGBA) {
}
}

func (c *ImageCache) GetCachedImageBlocking(loc ImageLocation) *CachedImage {
func (c *ImageCache) GetCachedImageBlocking(loc primitives.ImageLocation) *CachedImage {
ret := make(chan *CachedImage)
c.tasks <- &cacheTask{
loc: loc,
Expand All @@ -391,7 +382,7 @@ func (c *ImageCache) GetCachedImageBlocking(loc ImageLocation) *CachedImage {
return <-ret
}

func (c *ImageCache) GetCachedImage(loc ImageLocation, ret chan *CachedImage) {
func (c *ImageCache) GetCachedImage(loc primitives.ImageLocation, ret chan *CachedImage) {
if ret == nil {
return // wtf do you expect?
}
Expand All @@ -402,7 +393,7 @@ func (c *ImageCache) GetCachedImage(loc ImageLocation, ret chan *CachedImage) {
}
}

func (c *ImageCache) GetCachedImageModTime(loc ImageLocation) time.Time {
func (c *ImageCache) GetCachedImageModTime(loc primitives.ImageLocation) time.Time {
return c.getModTimeLoc(loc)
}

Expand Down
12 changes: 7 additions & 5 deletions imageCache/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"path"
"strconv"
"time"

"github.com/maxsupermanhd/WebChunk/primitives"
)

type cacheTaskIO struct {
loc ImageLocation
loc primitives.ImageLocation
img *CachedImage
err error
}
Expand Down Expand Up @@ -50,11 +52,11 @@ func (c *ImageCache) cacheGetFilename(world, dim, variant string, s, x, z int) s
return path.Join(".", c.root, world, dim, variant, strconv.FormatInt(int64(s), 10), strconv.FormatInt(int64(x), 10)+"x"+strconv.FormatInt(int64(z), 10)+".png")
}

func (c *ImageCache) cacheGetFilenameLoc(loc ImageLocation) string {
func (c *ImageCache) cacheGetFilenameLoc(loc primitives.ImageLocation) string {
return c.cacheGetFilename(loc.World, loc.Dimension, loc.Variant, loc.S, loc.X, loc.Z)
}

func (c *ImageCache) cacheSave(img *image.RGBA, loc ImageLocation) error {
func (c *ImageCache) cacheSave(img *image.RGBA, loc primitives.ImageLocation) error {
storePath := c.cacheGetFilenameLoc(loc)
err := os.MkdirAll(path.Dir(storePath), 0764)
if err != nil {
Expand All @@ -71,7 +73,7 @@ func (c *ImageCache) cacheSave(img *image.RGBA, loc ImageLocation) error {
return file.Close()
}

func (c *ImageCache) cacheLoad(loc ImageLocation) (*CachedImage, error) {
func (c *ImageCache) cacheLoad(loc primitives.ImageLocation) (*CachedImage, error) {
fp := c.cacheGetFilenameLoc(loc)
f, err := os.Open(fp)
if err != nil {
Expand Down Expand Up @@ -114,7 +116,7 @@ func (c *ImageCache) cacheLoad(loc ImageLocation) (*CachedImage, error) {
}, nil
}

func (c *ImageCache) getModTimeLoc(loc ImageLocation) time.Time {
func (c *ImageCache) getModTimeLoc(loc primitives.ImageLocation) time.Time {
return c.getModTimeFp(c.cacheGetFilenameLoc(loc))
}

Expand Down
12 changes: 12 additions & 0 deletions primitives/primitives.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package primitives

import "fmt"

type ImageLocation struct {
World, Dimension, Variant string
S, X, Z int
}

func (i ImageLocation) String() string {
return fmt.Sprintf("{%s:%s:%s at %ds %dx %dz}", i.World, i.Dimension, i.Variant, i.S, i.X, i.Z)
}
14 changes: 7 additions & 7 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"time"

"github.com/gorilla/websocket"
imagecache "github.com/maxsupermanhd/WebChunk/imageCache"
"github.com/maxsupermanhd/WebChunk/primitives"
"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -110,9 +110,9 @@ func wsClientHandler(w http.ResponseWriter, r *http.Request, ctx context.Context
wg.Done()
}()

subbedTiles := map[imagecache.ImageLocation]bool{}
subbedTiles := map[primitives.ImageLocation]bool{}

asyncTileRequestor := func(loc imagecache.ImageLocation) {
asyncTileRequestor := func(loc primitives.ImageLocation) {
if loc.Dimension == "" || loc.World == "" {
return
}
Expand Down Expand Up @@ -192,7 +192,7 @@ clientLoop:
}
switch msg.Action {
case "tileSubscribe":
var loc imagecache.ImageLocation
var loc primitives.ImageLocation
err := mapstructure.Decode(msg.Data, &loc)
if err != nil {
log.Printf("Websocket %s sent malformed tile sub: %s", r.RemoteAddr, err.Error())
Expand All @@ -207,7 +207,7 @@ clientLoop:
}
go asyncTileRequestor(loc)
case "tileUnsubscribe":
var loc imagecache.ImageLocation
var loc primitives.ImageLocation
err := mapstructure.Decode(msg.Data, &loc)
if err != nil {
log.Printf("Websocket %s sent malformed tile unsub: %s", r.RemoteAddr, err.Error())
Expand Down Expand Up @@ -237,7 +237,7 @@ clientLoop:
break
}
oldSubbed := subbedTiles
subbedTiles = map[imagecache.ImageLocation]bool{}
subbedTiles = map[primitives.ImageLocation]bool{}
for k := range oldSubbed {
k.World = nWorld
k.Dimension = nDimension
Expand All @@ -264,7 +264,7 @@ var (
}
)

func marshalBinaryTileUpdate(loc imagecache.ImageLocation, img *image.RGBA) []byte {
func marshalBinaryTileUpdate(loc primitives.ImageLocation, img *image.RGBA) []byte {
buf := bytes.NewBuffer([]byte{})
binary.Write(buf, binary.BigEndian, uint8(0x01))
binary.Write(buf, binary.BigEndian, uint32(len(loc.World)))
Expand Down

0 comments on commit ffd43da

Please sign in to comment.