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.

61 lines
1.4 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. package effects
  2. import (
  3. "encoding/json"
  4. "errors"
  5. lucifer3 "git.aiterp.net/lucifer3/server"
  6. )
  7. type serializedEffect struct {
  8. Manual *Manual `json:"manual,omitempty"`
  9. Gradient *Gradient `json:"gradient,omitempty"`
  10. Pattern *Pattern `json:"pattern,omitempty"`
  11. Random *Random `json:"random,omitempty"`
  12. VRange *VRange `json:"vrange,omitempty"`
  13. }
  14. type Serializable struct {
  15. Effect lucifer3.Effect
  16. }
  17. func (s *Serializable) UnmarshalJSON(raw []byte) error {
  18. value := serializedEffect{}
  19. err := json.Unmarshal(raw, &value)
  20. if err != nil {
  21. return err
  22. }
  23. switch {
  24. case value.Manual != nil:
  25. s.Effect = *value.Manual
  26. case value.Gradient != nil:
  27. s.Effect = *value.Gradient
  28. case value.Pattern != nil:
  29. s.Effect = *value.Pattern
  30. case value.Random != nil:
  31. s.Effect = *value.Random
  32. case value.VRange != nil:
  33. s.Effect = *value.VRange
  34. default:
  35. return errors.New("unsupported effect")
  36. }
  37. return nil
  38. }
  39. func (s *Serializable) MarshalJSON() ([]byte, error) {
  40. switch effect := s.Effect.(type) {
  41. case Manual:
  42. return json.Marshal(serializedEffect{Manual: &effect})
  43. case Gradient:
  44. return json.Marshal(serializedEffect{Gradient: &effect})
  45. case Pattern:
  46. return json.Marshal(serializedEffect{Pattern: &effect})
  47. case Random:
  48. return json.Marshal(serializedEffect{Random: &effect})
  49. case VRange:
  50. return json.Marshal(serializedEffect{VRange: &effect})
  51. default:
  52. panic(s.Effect.EffectDescription() + "is not understood by serializer")
  53. }
  54. }