You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.3 KiB
56 lines
1.3 KiB
package effects
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
lucifer3 "git.aiterp.net/lucifer3/server"
|
|
)
|
|
|
|
type serializedEffect struct {
|
|
Manual *Manual `json:"manual,omitempty"`
|
|
Gradient *Gradient `json:"gradient,omitempty"`
|
|
Pattern *Pattern `json:"pattern,omitempty"`
|
|
Random *Random `json:"random,omitempty"`
|
|
}
|
|
|
|
type Serializable struct {
|
|
Effect lucifer3.Effect
|
|
}
|
|
|
|
func (s *Serializable) UnmarshalJSON(raw []byte) error {
|
|
value := serializedEffect{}
|
|
err := json.Unmarshal(raw, &value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
switch {
|
|
case value.Manual != nil:
|
|
s.Effect = *value.Manual
|
|
case value.Gradient != nil:
|
|
s.Effect = *value.Gradient
|
|
case value.Pattern != nil:
|
|
s.Effect = *value.Pattern
|
|
case value.Random != nil:
|
|
s.Effect = *value.Random
|
|
default:
|
|
return errors.New("unsupported effect")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Serializable) MarshalJSON() ([]byte, error) {
|
|
switch effect := s.Effect.(type) {
|
|
case Manual:
|
|
return json.Marshal(serializedEffect{Manual: &effect})
|
|
case Gradient:
|
|
return json.Marshal(serializedEffect{Gradient: &effect})
|
|
case Pattern:
|
|
return json.Marshal(serializedEffect{Pattern: &effect})
|
|
case Random:
|
|
return json.Marshal(serializedEffect{Random: &effect})
|
|
default:
|
|
panic(s.Effect.EffectDescription() + "is not understood by serializer")
|
|
}
|
|
}
|