From 58fae48a2a72ebbfa8757cfab5de2aa924507e2d Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sun, 5 May 2019 14:03:20 -0700 Subject: [PATCH] WIP: MsgPack stubs, Level Filesystem Module * Add some encoding/decoding functions for binary msgpack format for levels and doodads. Currently it writes msgpack files that can be decoded and printed by Python (mp2json.py) but it can't re-read from the binary format. For now, levels will continue to write in JSON format. * Add filesystem abstraction functions to the balance/ package to search multiple paths to find Levels and Doodads, to make way for system-level doodads. --- color.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/color.go b/color.go index 7f1b2f5..699abb9 100644 --- a/color.go +++ b/color.go @@ -7,6 +7,8 @@ import ( "image/color" "regexp" "strconv" + + "github.com/vmihailenco/msgpack" ) var ( @@ -155,6 +157,52 @@ func (c *Color) UnmarshalJSON(b []byte) error { return nil } +func (c Color) EncodeMsgpack(enc *msgpack.Encoder) error { + return enc.EncodeString(fmt.Sprintf( + `"#%02x%02x%02x"`, + c.Red, c.Green, c.Blue, + )) +} + +func (c Color) DecodeMsgpack(dec *msgpack.Decoder) error { + hex, err := dec.DecodeString() + if err != nil { + return fmt.Errorf("Color.DecodeMsgpack: %s", err) + } + + parsed, err := HexColor(hex) + if err != nil { + return fmt.Errorf("Color.DecodeMsgpack: HexColor: %s", err) + } + + c.Red = parsed.Red + c.Blue = parsed.Blue + c.Green = parsed.Green + c.Alpha = parsed.Alpha + return nil +} + +// // MarshalMsgpack serializes the Color for msgpack. +// func (c Color) MarshalMsgpack() ([]byte, error) { +// data := []uint8{ +// c.Red, c.Green, c.Blue, c.Alpha, +// } +// return msgpack.Marshal(data) +// } +// +// // UnmarshalMsgpack decodes a Color from msgpack format. +// func (c *Color) UnmarshalMsgpack(b []byte) error { +// var data []uint8 +// if err := msgpack.Unmarshal(data, b); err != nil { +// return err +// } +// c.Red = 255 +// c.Green = data[1] +// c.Blue = data[2] +// c.Alpha = data[3] +// return nil +// } + // Add a relative color value to the color. func (c Color) Add(r, g, b, a int) Color { var (