This commit is contained in:
Thomas Rudin 2019-01-13 16:37:03 +01:00
parent 7503c2c281
commit bc223722af
28 changed files with 209 additions and 210 deletions

View File

@ -1,89 +1,89 @@
package colormapping
import (
"mapserver/vfs"
"bufio"
"errors"
"bytes"
"strings"
"strconv"
"github.com/sirupsen/logrus"
"image/color"
"bufio"
"bytes"
"errors"
"github.com/sirupsen/logrus"
"image/color"
"mapserver/vfs"
"strconv"
"strings"
)
type ColorMapping struct {
colors map[string]*color.RGBA
colors map[string]*color.RGBA
}
func (m *ColorMapping) GetColor(name string) *color.RGBA {
return m.colors[name]
return m.colors[name]
}
func (m *ColorMapping) LoadBytes(buffer []byte) error {
scanner := bufio.NewScanner(bytes.NewReader(buffer))
for scanner.Scan() {
txt := strings.Trim(scanner.Text(), " ")
scanner := bufio.NewScanner(bytes.NewReader(buffer))
for scanner.Scan() {
txt := strings.Trim(scanner.Text(), " ")
if len(txt) == 0 {
//empty
continue
}
if len(txt) == 0 {
//empty
continue
}
if strings.HasPrefix(txt, "#") {
//comment
continue
}
if strings.HasPrefix(txt, "#") {
//comment
continue
}
parts := strings.Fields(txt)
parts := strings.Fields(txt)
if len(parts) < 4 {
return errors.New("invalid line")
}
if len(parts) < 4 {
return errors.New("invalid line")
}
if len(parts) >= 4 {
r, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
return err
}
if len(parts) >= 4 {
r, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
return err
}
g, err := strconv.ParseInt(parts[2], 10, 32)
if err != nil {
return err
}
g, err := strconv.ParseInt(parts[2], 10, 32)
if err != nil {
return err
}
b, err := strconv.ParseInt(parts[3], 10, 32)
if err != nil {
return err
}
b, err := strconv.ParseInt(parts[3], 10, 32)
if err != nil {
return err
}
c := color.RGBA{uint8(r),uint8(g),uint8(b), 0xFF}
m.colors[parts[0]] = &c
}
c := color.RGBA{uint8(r), uint8(g), uint8(b), 0xFF}
m.colors[parts[0]] = &c
}
if len(parts) >= 5 {
//with alpha
}
if len(parts) >= 5 {
//with alpha
}
}
}
return nil
return nil
}
//TODO: colors from fs
func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) error {
buffer, err := vfs.FSByte(useLocal, "/colors.txt")
if err != nil {
return err
}
buffer, err := vfs.FSByte(useLocal, "/colors.txt")
if err != nil {
return err
}
log.WithFields(logrus.Fields{"size": len(buffer),
"filename": filename,
"useLocal": useLocal}).Info("Loading local colors file")
log.WithFields(logrus.Fields{"size": len(buffer),
"filename": filename,
"useLocal": useLocal}).Info("Loading local colors file")
return m.LoadBytes(buffer)
return m.LoadBytes(buffer)
}
func NewColorMapping() *ColorMapping {
return &ColorMapping{colors: make(map[string]*color.RGBA)}
return &ColorMapping{colors: make(map[string]*color.RGBA)}
}

View File

@ -5,15 +5,15 @@ import (
)
func TestNewMapping(t *testing.T) {
m := NewColorMapping()
err := m.LoadVFSColors(false, "/colors.txt")
if err != nil {
t.Fatal(err)
}
m := NewColorMapping()
err := m.LoadVFSColors(false, "/colors.txt")
if err != nil {
t.Fatal(err)
}
c := m.GetColor("scifi_nodes:blacktile2")
if c == nil {
panic("no color")
}
c := m.GetColor("scifi_nodes:blacktile2")
if c == nil {
panic("no color")
}
}

View File

@ -1,10 +1,11 @@
package colormapping
import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init(){
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "colormapping"})
}

View File

@ -9,7 +9,6 @@ const (
maxValue = 1<<(numBitsPerComponent-1) - 1
)
func CoordToPlain(c MapBlockCoords) int64 {
return int64(c.Z)<<(2*numBitsPerComponent) +
int64(c.Y)<<numBitsPerComponent +

View File

@ -1,42 +1,42 @@
package coords
import (
"testing"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"testing"
)
var log *logrus.Entry
func init(){
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "coords/convert_test"})
}
func testCoordConvert(t *testing.T, mb MapBlockCoords){
log.WithFields(logrus.Fields{"coords":mb}).Info("MapblockCoords")
func testCoordConvert(t *testing.T, mb MapBlockCoords) {
log.WithFields(logrus.Fields{"coords": mb}).Info("MapblockCoords")
p := CoordToPlain(mb)
log.WithFields(logrus.Fields{"plain":p}).Info("MapblockCoords")
p := CoordToPlain(mb)
log.WithFields(logrus.Fields{"plain": p}).Info("MapblockCoords")
mb2 := PlainToCoord(p)
log.WithFields(logrus.Fields{"coords2":mb2}).Info("MapblockCoords")
mb2 := PlainToCoord(p)
log.WithFields(logrus.Fields{"coords2": mb2}).Info("MapblockCoords")
if mb.X != mb2.X {
t.Fatal("X mismatch")
}
if mb.X != mb2.X {
t.Fatal("X mismatch")
}
if mb.Y != mb2.Y {
t.Fatal("Y mismatch")
}
if mb.Y != mb2.Y {
t.Fatal("Y mismatch")
}
if mb.Z != mb2.Z {
t.Fatal("Z mismatch")
}
if mb.Z != mb2.Z {
t.Fatal("Z mismatch")
}
}
func TestConvertPlainMapBlock(t *testing.T){
testCoordConvert(t, NewMapBlockCoords(10, 0, -10))
testCoordConvert(t, NewMapBlockCoords(-2048, 2047, -10))
testCoordConvert(t, NewMapBlockCoords(-3, 0, 2047)) //0...2047
func TestConvertPlainMapBlock(t *testing.T) {
testCoordConvert(t, NewMapBlockCoords(10, 0, -10))
testCoordConvert(t, NewMapBlockCoords(-2048, 2047, -10))
testCoordConvert(t, NewMapBlockCoords(-3, 0, 2047)) //0...2047
}

View File

@ -1,18 +1,18 @@
package coords
type MapBlockCoords struct {
X,Y,Z int
X, Y, Z int
}
func NewMapBlockCoords(x,y,z int) MapBlockCoords {
return MapBlockCoords{X:x, Y:y, Z:z}
func NewMapBlockCoords(x, y, z int) MapBlockCoords {
return MapBlockCoords{X: x, Y: y, Z: z}
}
type MapBlockRange struct {
pos1, pos2 MapBlockCoords
pos1, pos2 MapBlockCoords
}
const (
MaxCoord = 2047
MinCoord = -2047
MaxCoord = 2047
MinCoord = -2047
)

View File

@ -4,7 +4,6 @@ const (
MAX_ZOOM = 13
)
func GetTileCoordsFromMapBlock(mbc MapBlockCoords) TileCoords {
return TileCoords{X:mbc.X, Y:(mbc.Z + 1) * -1, Zoom:MAX_ZOOM};
return TileCoords{X: mbc.X, Y: (mbc.Z + 1) * -1, Zoom: MAX_ZOOM}
}

View File

@ -1,44 +1,44 @@
package coords
import (
"math"
"math"
)
type TileCoords struct {
X,Y int
Zoom int
X, Y int
Zoom int
}
type TileQuadrants struct {
UpperLeft, UpperRight, LowerLeft, LowerRight TileCoords
UpperLeft, UpperRight, LowerLeft, LowerRight TileCoords
}
func NewTileCoords(x,y,zoom int) TileCoords {
return TileCoords{X:x, Y:y, Zoom:zoom}
func NewTileCoords(x, y, zoom int) TileCoords {
return TileCoords{X: x, Y: y, Zoom: zoom}
}
func (tc TileCoords) GetZoomedOutTile() TileCoords {
return TileCoords{
X: int(math.Floor(float64(tc.X) / 2.0)),
Y: int(math.Floor(float64(tc.Y) / 2.0)),
Zoom: tc.Zoom - 1}
return TileCoords{
X: int(math.Floor(float64(tc.X) / 2.0)),
Y: int(math.Floor(float64(tc.Y) / 2.0)),
Zoom: tc.Zoom - 1}
}
func (tc TileCoords) GetZoomedQuadrantsFromTile() TileQuadrants {
nextZoom := tc.Zoom + 1
nextZoom := tc.Zoom + 1
nextZoomX := tc.X * 2
nextZoomY := tc.Y * 2
nextZoomX := tc.X * 2
nextZoomY := tc.Y * 2
upperLeft := TileCoords{X: nextZoomX, Y:nextZoomY, Zoom:nextZoom}
upperRight := TileCoords{X: nextZoomX + 1, Y: nextZoomY, Zoom:nextZoom}
lowerLeft := TileCoords{X: nextZoomX, Y: nextZoomY + 1, Zoom:nextZoom}
lowerRight := TileCoords{X: nextZoomX + 1, Y: nextZoomY + 1, Zoom:nextZoom}
upperLeft := TileCoords{X: nextZoomX, Y: nextZoomY, Zoom: nextZoom}
upperRight := TileCoords{X: nextZoomX + 1, Y: nextZoomY, Zoom: nextZoom}
lowerLeft := TileCoords{X: nextZoomX, Y: nextZoomY + 1, Zoom: nextZoom}
lowerRight := TileCoords{X: nextZoomX + 1, Y: nextZoomY + 1, Zoom: nextZoom}
return TileQuadrants{
UpperLeft: upperLeft,
UpperRight: upperRight,
LowerLeft: lowerLeft,
LowerRight: lowerRight,
}
return TileQuadrants{
UpperLeft: upperLeft,
UpperRight: upperRight,
LowerLeft: lowerLeft,
LowerRight: lowerRight,
}
}

View File

@ -5,9 +5,9 @@ import (
)
type Block struct {
Pos coords.MapBlockCoords
Data []byte
Mtime int64
Pos coords.MapBlockCoords
Data []byte
Mtime int64
}
type DBAccessor interface {

View File

@ -5,6 +5,7 @@ import (
)
var log *logrus.Entry
func init(){
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "db"})
}

View File

@ -27,14 +27,14 @@ end;
//TODO: postgres test
type Sqlite3Accessor struct {
db *sql.DB
db *sql.DB
filename string
}
func (db *Sqlite3Accessor) Migrate() error {
//RW connection
rwdb, err := sql.Open("sqlite3", db.filename + "?mode=rw")
rwdb, err := sql.Open("sqlite3", db.filename+"?mode=rw")
if err != nil {
return err
}
@ -48,7 +48,7 @@ func (db *Sqlite3Accessor) Migrate() error {
}
if !hasMtime {
log.WithFields(logrus.Fields{"filename":db.filename}).Info("Migrating database")
log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database")
start := time.Now()
_, err = rwdb.Exec(migrateScript)
if err != nil {
@ -56,7 +56,7 @@ func (db *Sqlite3Accessor) Migrate() error {
}
t := time.Now()
elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed":elapsed}).Info("Migration completed")
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
}
return nil
@ -64,7 +64,7 @@ func (db *Sqlite3Accessor) Migrate() error {
func convertRows(pos int64, data []byte, mtime int64) Block {
c := coords.PlainToCoord(pos)
return Block{Pos:c, Data:data, Mtime:mtime}
return Block{Pos: c, Data: data, Mtime: mtime}
}
func (db *Sqlite3Accessor) FindLatestBlocks(mintime int64, limit int) ([]Block, error) {
@ -100,9 +100,8 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) {
return nil, nil
}
func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {
db, err := sql.Open("sqlite3", filename + "?mode=ro")
db, err := sql.Open("sqlite3", filename+"?mode=ro")
if err != nil {
return nil, err
}

View File

@ -1,15 +1,14 @@
package db
import (
_ "github.com/mattn/go-sqlite3"
"io/ioutil"
"mapserver/coords"
"mapserver/testutils"
"os"
"testing"
"mapserver/testutils"
_ "github.com/mattn/go-sqlite3"
)
func TestMigrateEmpty(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrateEmpty.*.sqlite")
if err != nil {

View File

@ -1,10 +1,11 @@
package mapblockaccessor
import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init(){
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "mapblockaccessor"})
}

View File

@ -1,17 +1,17 @@
package mapblockaccessor
import (
"fmt"
"github.com/patrickmn/go-cache"
"mapserver/coords"
"mapserver/db"
"mapserver/mapblockparser"
"github.com/patrickmn/go-cache"
"time"
"fmt"
)
type MapBlockAccessor struct {
accessor db.DBAccessor
c *cache.Cache
c *cache.Cache
}
func getKey(pos coords.MapBlockCoords) string {

View File

@ -1,13 +1,13 @@
package mapblockaccessor
import (
"os"
"github.com/sirupsen/logrus"
"io/ioutil"
"mapserver/coords"
"testing"
"mapserver/testutils"
"mapserver/db"
"github.com/sirupsen/logrus"
"mapserver/testutils"
"os"
"testing"
)
func TestSimpleAccess(t *testing.T) {

View File

@ -6,17 +6,17 @@ import (
type CountedReader struct {
Reader *bytes.Reader
Count int
Count int
}
func (r *CountedReader) Read(p []byte) (int, error){
i,err := r.Reader.Read(p)
func (r *CountedReader) Read(p []byte) (int, error) {
i, err := r.Reader.Read(p)
r.Count += i
return i, err
}
func (r *CountedReader) ReadByte() (byte, error){
i,err := r.Reader.ReadByte()
func (r *CountedReader) ReadByte() (byte, error) {
i, err := r.Reader.ReadByte()
r.Count++
return i, err
}
}

View File

@ -1,10 +1,11 @@
package mapblockparser
import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init(){
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "mapblockaccessor"})
}

View File

@ -8,13 +8,13 @@ type MapBlock struct {
BlockMapping map[int]string
}
func getNodePos(x,y,z int) int {
func getNodePos(x, y, z int) int {
return x + (y * 16) + (z * 256)
}
func (mb *MapBlock) GetNodeName(x,y,z int) string {
pos := getNodePos(x,y,z)
id := readU16(mb.Mapdata, pos * 2)
func (mb *MapBlock) GetNodeName(x, y, z int) string {
pos := getNodePos(x, y, z)
id := readU16(mb.Mapdata, pos*2)
return mb.BlockMapping[id]
}

View File

@ -5,10 +5,10 @@ import (
"bytes"
"compress/zlib"
"errors"
"github.com/sirupsen/logrus"
"io"
"strconv"
"strings"
"github.com/sirupsen/logrus"
)
const (

View File

@ -1,10 +1,11 @@
package mapblockrenderer
import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init(){
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "mapblockrenderer"})
}

View File

@ -1,14 +1,14 @@
package mapblockrenderer
import (
"errors"
"github.com/sirupsen/logrus"
"image"
"image/draw"
"mapserver/colormapping"
"mapserver/coords"
"mapserver/mapblockaccessor"
"image"
"image/draw"
"errors"
"time"
"github.com/sirupsen/logrus"
)
type MapBlockRenderer struct {
@ -21,9 +21,9 @@ func NewMapBlockRenderer(accessor *mapblockaccessor.MapBlockAccessor, colors *co
}
const (
IMG_SCALE = 16
IMG_SIZE = IMG_SCALE * 16
EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK = 16*16
IMG_SCALE = 16
IMG_SIZE = IMG_SCALE * 16
EXPECTED_BLOCKS_PER_FLAT_MAPBLOCK = 16 * 16
)
func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) (*image.NRGBA, error) {
@ -34,12 +34,12 @@ func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) (*image.NRGB
if pos1.Z != pos2.Z {
return nil, errors.New("Z does not line up")
}
start := time.Now()
defer func(){
defer func() {
t := time.Now()
elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed":elapsed}).Debug("Rendering completed")
log.WithFields(logrus.Fields{"elapsed": elapsed}).Debug("Rendering completed")
}()
upLeft := image.Point{0, 0}
@ -78,7 +78,7 @@ func (r *MapBlockRenderer) Render(pos1, pos2 coords.MapBlockCoords) (*image.NRGB
continue
}
nodeName := mb.GetNodeName(x,y,z)
nodeName := mb.GetNodeName(x, y, z)
if nodeName == "" {
continue

View File

@ -8,7 +8,7 @@ type ParamsType struct {
Worlddir string
Port int
Help bool
Version bool
Version bool
}
func Parse() ParamsType {

View File

@ -1,17 +1,15 @@
package testutils
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"io"
"path/filepath"
"os"
"fmt"
"runtime"
"path/filepath"
"runtime"
)
const emptyBlocksScript = `
create table blocks (
pos int,
@ -40,11 +38,10 @@ func copy(src, dst string) error {
}
func CreateTestDatabase(filename string) error {
_, currentfilename, _, _ := runtime.Caller(0)
return copy(filepath.Dir(currentfilename) + "/testdata/map.sqlite", filename)
_, currentfilename, _, _ := runtime.Caller(0)
return copy(filepath.Dir(currentfilename)+"/testdata/map.sqlite", filename)
}
func CreateEmptyDatabase(filename string) {
db, err := sql.Open("sqlite3", filename)
if err != nil {

View File

@ -5,10 +5,10 @@ import (
)
type Tile struct {
Pos coords.TileCoords
LayerId int
Data []byte
Mtime int64
Pos coords.TileCoords
LayerId int
Data []byte
Mtime int64
}
type DBAccessor interface {

View File

@ -1,10 +1,11 @@
package tilerenderer
import (
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init(){
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "tilerenderer"})
}

View File

@ -1,25 +1,25 @@
package tilerenderer
import (
"mapserver/coords"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
"image"
"image"
"mapserver/coords"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
)
type TileRenderer struct {
mapblockrenderer *mapblockrenderer.MapBlockRenderer
tiledb *tiledb.DBAccessor
mapblockrenderer *mapblockrenderer.MapBlockRenderer
tiledb *tiledb.DBAccessor
}
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer, tiledb *tiledb.DBAccessor) *TileRenderer {
return &TileRenderer{
mapblockrenderer: mapblockrenderer,
tiledb: tiledb,
}
return &TileRenderer{
mapblockrenderer: mapblockrenderer,
tiledb: tiledb,
}
}
//TODO layerConfig
func (tr *TileRenderer) Render(tc coords.TileCoords, layerId int) (*image.NRGBA, error) {
return nil, nil
return nil, nil
}

View File

@ -1,39 +1,39 @@
package worldconfig
import (
"fmt"
"bufio"
"fmt"
"os"
"strings"
"strconv"
"strings"
)
const (
BACKEND_SQLITE3 string = "sqlite3"
BACKEND_FILES string = "files"
BACKEND_SQLITE3 string = "sqlite3"
BACKEND_FILES string = "files"
BACKEND_POSTGRES string = "postgresql"
)
const (
CONFIG_BACKEND string = "backend"
CONFIG_PLAYER_BACKEND string = "player_backend"
CONFIG_PSQL_CONNECTION string = "pgsql_connection"
CONFIG_BACKEND string = "backend"
CONFIG_PLAYER_BACKEND string = "player_backend"
CONFIG_PSQL_CONNECTION string = "pgsql_connection"
CONFIG_PSQL_PLAYER_CONNECTION string = "pgsql_player_connection"
)
type PsqlConfig struct {
Host string
Port int
Host string
Port int
Username string
Password string
DbName string
DbName string
}
type WorldConfig struct {
Backend string
PlayerBackend string
PsqlConnection PsqlConfig
PsqlConnection PsqlConfig
PsqlPlayerConnection PsqlConfig
}

View File

@ -1,8 +1,8 @@
package worldconfig
import (
"testing"
"fmt"
"testing"
)
func TestParseSqlite(t *testing.T) {