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.

66 lines
1.6 KiB

2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year 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. Solid *Solid `json:"solid,omitempty"`
  10. Gradient *Gradient `json:"gradient,omitempty"`
  11. Pattern *Pattern `json:"pattern,omitempty"`
  12. Random *Random `json:"random,omitempty"`
  13. VRange *VRange `json:"vrange,omitempty"`
  14. }
  15. type Serializable struct {
  16. Effect lucifer3.Effect
  17. }
  18. func (s *Serializable) UnmarshalJSON(raw []byte) error {
  19. value := serializedEffect{}
  20. err := json.Unmarshal(raw, &value)
  21. if err != nil {
  22. return err
  23. }
  24. switch {
  25. case value.Manual != nil:
  26. s.Effect = *value.Manual
  27. case value.Solid != nil:
  28. s.Effect = *value.Solid
  29. case value.Gradient != nil:
  30. s.Effect = *value.Gradient
  31. case value.Pattern != nil:
  32. s.Effect = *value.Pattern
  33. case value.Random != nil:
  34. s.Effect = *value.Random
  35. case value.VRange != nil:
  36. s.Effect = *value.VRange
  37. default:
  38. return errors.New("unsupported effect")
  39. }
  40. return nil
  41. }
  42. func (s *Serializable) MarshalJSON() ([]byte, error) {
  43. switch effect := s.Effect.(type) {
  44. case Manual:
  45. return json.Marshal(serializedEffect{Manual: &effect})
  46. case Solid:
  47. return json.Marshal(serializedEffect{Solid: &effect})
  48. case Gradient:
  49. return json.Marshal(serializedEffect{Gradient: &effect})
  50. case Pattern:
  51. return json.Marshal(serializedEffect{Pattern: &effect})
  52. case Random:
  53. return json.Marshal(serializedEffect{Random: &effect})
  54. case VRange:
  55. return json.Marshal(serializedEffect{VRange: &effect})
  56. default:
  57. panic(s.Effect.EffectDescription() + "is not understood by serializer")
  58. }
  59. }