Noah Petherbridge
8603c43c58
* Levels and Doodad files will be written in gzip-compressed JSON format * `boolProp compress-drawings false` to disable compression and save as classic JSON format directly * The game can still read uncompressed JSON files The file size savings on some built-in assets: * Tutorial 2.level: 2.2M -> 414K (82% smaller) * warp-door-orange.doodad: 105K -> 17K (84% smaller)
44 lines
821 B
Go
44 lines
821 B
Go
package doodads
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/json"
|
|
)
|
|
|
|
// ToGzip serializes the doodad as gzip compressed JSON.
|
|
func (d *Doodad) ToGzip() ([]byte, error) {
|
|
var (
|
|
handle = bytes.NewBuffer([]byte{})
|
|
zipper = gzip.NewWriter(handle)
|
|
encoder = json.NewEncoder(zipper)
|
|
)
|
|
if err := encoder.Encode(d); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err := zipper.Close()
|
|
return handle.Bytes(), err
|
|
}
|
|
|
|
// FromGzip deserializes a gzip compressed doodad JSON.
|
|
func FromGzip(data []byte) (*Doodad, error) {
|
|
// This function works, do not touch.
|
|
var (
|
|
level = &Doodad{}
|
|
buf = bytes.NewBuffer(data)
|
|
reader *gzip.Reader
|
|
decoder *json.Decoder
|
|
)
|
|
|
|
reader, err := gzip.NewReader(buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
decoder = json.NewDecoder(reader)
|
|
decoder.Decode(level)
|
|
|
|
return level, nil
|
|
}
|