From 986b9dbdb98eb47c171a06b427e4b6c809b7ee36 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 8 Feb 2019 07:40:40 +0100 Subject: [PATCH] custom colors --- doc/config.md | 37 ++++++++++++++++++----------- server/app/setup.go | 28 +++++++++++++++++++++- server/colormapping/colormapping.go | 24 ++++++++++++------- server/colors.txt | 1 + 4 files changed, 66 insertions(+), 24 deletions(-) create mode 100644 server/colors.txt diff --git a/doc/config.md b/doc/config.md index cb02c67..fd57878 100644 --- a/doc/config.md +++ b/doc/config.md @@ -1,12 +1,29 @@ # Configuration +## Colors + +There are builtin default colors, if you want new ones or override them +just put your `colors.txt` in the same directory the mapserver runs. + +Example `colors.txt`: +``` +# scifi nodes +scifi_nodes:slope_white 240 240 240 +scifi_nodes:slope_vent 120 120 120 +scifi_nodes:white2 240 240 240 +``` + +Default colors, see: [colors.txt](../server/static/colors.txt) + +## Configuration json + All config options reside in the `mapserver.json` file with the default values Please stop the server if you make any changes there. The mapserver will generate a fresh `mapserver.json` if there is none at startup. -## Example json +### Example json ```json { "port": 8080, @@ -16,14 +33,6 @@ The mapserver will generate a fresh `mapserver.json` if there is none at startup "enablemapblock": false, "secretkey": "ZJoSpysiKGlYexof" }, - "renderstate": { - "initialrun": false, - "legacyprocessed": 16111, - "lastx": 3, - "lasty": 3, - "lastz": 8, - "lastmtime": 0 - }, "layers": [ { "id": 0, @@ -38,15 +47,15 @@ The mapserver will generate a fresh `mapserver.json` if there is none at startup } ``` -## Settings +### Settings -### port +#### port The port on which the server listens to -### webapi.secretkey +#### webapi.secretkey The generated secret for the [mod bridge](./install) -### layers +#### layers The layers as a list More layers can be added here: ```json @@ -70,5 +79,5 @@ Don't reuse the `id` after the tiles are generated. If you make more substantial changes here you may have to remove all existing tiles and start rendering from scratch. -### renderingjobs +#### renderingjobs Number of cores to use for rendering, defaults to all available cores diff --git a/server/app/setup.go b/server/app/setup.go index 2e7c814..26a1815 100644 --- a/server/app/setup.go +++ b/server/app/setup.go @@ -14,6 +14,9 @@ import ( "github.com/sirupsen/logrus" + "os" + "io/ioutil" + "errors" ) @@ -50,10 +53,33 @@ func Setup(p params.ParamsType, cfg *Config) *App { //color mapping a.Colormapping = colormapping.NewColorMapping() - err = a.Colormapping.LoadVFSColors(false, "/colors.txt") + + //load default colors + count, err := a.Colormapping.LoadVFSColors(false, "/colors.txt") if err != nil { panic(err) } + logrus.WithFields(logrus.Fields{"count": count}).Info("Loaded default colors") + + + //load provided colors, if available + info, err := os.Stat("colors.txt") + if info != nil && err == nil { + logrus.WithFields(logrus.Fields{"filename": "colors.txt"}).Info("Loading colors from filesystem") + + data, err := ioutil.ReadFile("colors.txt") + if err != nil { + panic(err) + } + + count, err = a.Colormapping.LoadBytes(data) + if err != nil { + panic(err) + } + + logrus.WithFields(logrus.Fields{"count": count}).Info("Loaded custom colors") + + } //mapblock renderer a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping) diff --git a/server/colormapping/colormapping.go b/server/colormapping/colormapping.go index 22248e8..6cda961 100644 --- a/server/colormapping/colormapping.go +++ b/server/colormapping/colormapping.go @@ -19,9 +19,14 @@ func (m *ColorMapping) GetColor(name string) *color.RGBA { return m.colors[name] } -func (m *ColorMapping) LoadBytes(buffer []byte) error { +func (m *ColorMapping) LoadBytes(buffer []byte) (int, error) { scanner := bufio.NewScanner(bytes.NewReader(buffer)) + count := 0 + line := 0 + for scanner.Scan() { + line++ + txt := strings.Trim(scanner.Text(), " ") if len(txt) == 0 { @@ -37,27 +42,28 @@ func (m *ColorMapping) LoadBytes(buffer []byte) error { parts := strings.Fields(txt) if len(parts) < 4 { - return errors.New("invalid line") + return 0, errors.New("invalid line: #" + strconv.Itoa(line)) } if len(parts) >= 4 { r, err := strconv.ParseInt(parts[1], 10, 32) if err != nil { - return err + return 0, err } g, err := strconv.ParseInt(parts[2], 10, 32) if err != nil { - return err + return 0, err } b, err := strconv.ParseInt(parts[3], 10, 32) if err != nil { - return err + return 0, err } c := color.RGBA{uint8(r), uint8(g), uint8(b), 0xFF} m.colors[parts[0]] = &c + count++ } if len(parts) >= 5 { @@ -66,20 +72,20 @@ func (m *ColorMapping) LoadBytes(buffer []byte) error { } - return nil + return count, nil } //TODO: colors from fs -func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) error { +func (m *ColorMapping) LoadVFSColors(useLocal bool, filename string) (int, error) { buffer, err := vfs.FSByte(useLocal, "/colors.txt") if err != nil { - return err + return 0, err } log.WithFields(logrus.Fields{"size": len(buffer), "filename": filename, - "useLocal": useLocal}).Info("Loading local colors file") + "useLocal": useLocal}).Info("Loading default colors") return m.LoadBytes(buffer) } diff --git a/server/colors.txt b/server/colors.txt new file mode 100644 index 0000000..1a9bc58 --- /dev/null +++ b/server/colors.txt @@ -0,0 +1 @@ +test:example 147 94 25 1 2