2018-06-17 17:29:57 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-07-14 02:23:09 +00:00
|
|
|
"compress/gzip"
|
2018-06-17 17:29:57 +00:00
|
|
|
"encoding/json"
|
2021-07-14 02:23:09 +00:00
|
|
|
"errors"
|
2018-08-11 00:19:47 +00:00
|
|
|
"fmt"
|
2018-10-16 16:20:25 +00:00
|
|
|
"io/ioutil"
|
2019-05-05 21:03:20 +00:00
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2021-06-20 05:14:41 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/usercfg"
|
2018-06-17 17:29:57 +00:00
|
|
|
)
|
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
// FromJSON loads a level from JSON string (gzip supported).
|
2019-06-27 22:07:34 +00:00
|
|
|
func FromJSON(filename string, data []byte) (*Level, error) {
|
2019-07-03 23:22:30 +00:00
|
|
|
var m = New()
|
2021-07-14 02:23:09 +00:00
|
|
|
|
|
|
|
// Inspect if this file is JSON or gzip compressed.
|
|
|
|
if len(data) > 0 && data[0] == '{' {
|
|
|
|
// Looks standard JSON.
|
|
|
|
err := json.Unmarshal(data, m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else if len(data) > 1 && data[0] == 0x1f && data[1] == 0x8b {
|
|
|
|
// Gzip compressed. `1F8B` is gzip magic number.
|
|
|
|
log.Debug("Decompress level %s", filename)
|
|
|
|
if gzmap, err := FromGzip(data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
m = gzmap
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("invalid file format")
|
|
|
|
}
|
2019-06-27 22:07:34 +00:00
|
|
|
|
|
|
|
// Fill in defaults.
|
|
|
|
if m.Wallpaper == "" {
|
|
|
|
m.Wallpaper = DefaultWallpaper
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inflate the chunk metadata to map the pixels to their palette indexes.
|
2021-09-12 04:18:22 +00:00
|
|
|
m.Inflate()
|
2019-06-27 22:07:34 +00:00
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
return m, nil
|
2019-06-27 22:07:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
// ToJSON serializes the level as JSON (gzip supported).
|
|
|
|
//
|
|
|
|
// Notice about gzip: if the pkg/balance.CompressLevels boolean is true, this
|
|
|
|
// function will apply gzip compression before returning the byte string.
|
|
|
|
// This gzip-compressed level can be read back by any functions that say
|
|
|
|
// "gzip supported" in their descriptions.
|
2018-06-17 17:29:57 +00:00
|
|
|
func (m *Level) ToJSON() ([]byte, error) {
|
2021-07-14 02:23:09 +00:00
|
|
|
// Gzip compressing?
|
|
|
|
if balance.CompressDrawings {
|
|
|
|
return m.ToGzip()
|
|
|
|
}
|
|
|
|
|
2018-06-17 17:29:57 +00:00
|
|
|
out := bytes.NewBuffer([]byte{})
|
|
|
|
encoder := json.NewEncoder(out)
|
2021-06-20 05:14:41 +00:00
|
|
|
if usercfg.Current.JSONIndent {
|
2019-05-05 21:03:20 +00:00
|
|
|
encoder.SetIndent("", "\t")
|
|
|
|
}
|
2018-06-17 17:29:57 +00:00
|
|
|
err := encoder.Encode(m)
|
|
|
|
return out.Bytes(), err
|
|
|
|
}
|
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
// ToGzip serializes the level as gzip compressed JSON.
|
|
|
|
func (m *Level) ToGzip() ([]byte, error) {
|
|
|
|
var (
|
|
|
|
handle = bytes.NewBuffer([]byte{})
|
|
|
|
zipper = gzip.NewWriter(handle)
|
|
|
|
encoder = json.NewEncoder(zipper)
|
|
|
|
)
|
|
|
|
if err := encoder.Encode(m); err != nil {
|
2018-08-11 00:19:47 +00:00
|
|
|
return nil, err
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
err := zipper.Close()
|
|
|
|
return handle.Bytes(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromGzip deserializes a gzip compressed level JSON.
|
|
|
|
func FromGzip(data []byte) (*Level, error) {
|
|
|
|
// This function works, do not touch.
|
|
|
|
var (
|
|
|
|
level = New()
|
|
|
|
buf = bytes.NewBuffer(data)
|
|
|
|
reader *gzip.Reader
|
|
|
|
decoder *json.Decoder
|
|
|
|
)
|
|
|
|
|
|
|
|
reader, err := gzip.NewReader(buf)
|
2018-08-11 00:19:47 +00:00
|
|
|
if err != nil {
|
2021-07-14 02:23:09 +00:00
|
|
|
return nil, err
|
2018-08-11 00:19:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
decoder = json.NewDecoder(reader)
|
|
|
|
decoder.Decode(level)
|
2018-10-28 05:22:13 +00:00
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
return level, nil
|
|
|
|
}
|
2018-09-23 22:20:45 +00:00
|
|
|
|
2021-07-14 02:23:09 +00:00
|
|
|
// LoadJSON loads a map from JSON file (gzip supported).
|
|
|
|
func LoadJSON(filename string) (*Level, error) {
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromJSON(filename, data)
|
2018-06-17 17:29:57 +00:00
|
|
|
}
|
2019-05-05 21:03:20 +00:00
|
|
|
|
|
|
|
// WriteJSON writes a level to JSON on disk.
|
|
|
|
func (m *Level) WriteJSON(filename string) error {
|
|
|
|
json, err := m.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Level.WriteJSON: JSON encode error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filename, json, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Level.WriteJSON: WriteFile error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|