pic.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package brlyt
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "strings"
  6. )
  7. func (r *Root) ParsePIC(data []byte) (*XMLPIC, error) {
  8. var pic PIC
  9. err := binary.Read(bytes.NewReader(data), binary.BigEndian, &pic)
  10. if err != nil {
  11. return nil, err
  12. }
  13. // Strip the null bytes from the strings
  14. name := strings.Replace(string(pic.PaneName[:]), "\x00", "", -1)
  15. userData := strings.Replace(string(pic.UserData[:]), "\x00", "", -1)
  16. // Get the UVSets
  17. uvSets := make([]XMLUVSet, pic.NumOfUVSets)
  18. for i := 0; i < int(pic.NumOfUVSets); i++ {
  19. offset := 88 + (i * 32)
  20. var uv UVSet
  21. err = binary.Read(bytes.NewReader(data[offset:]), binary.BigEndian, &uv)
  22. if err != nil {
  23. return nil, err
  24. }
  25. set := XMLUVSet{
  26. CoordTL: STCoordinates{
  27. T: uv.TopLeftT,
  28. S: uv.TopLeftS,
  29. },
  30. CoordTR: STCoordinates{
  31. T: uv.TopRightT,
  32. S: uv.TopRightS,
  33. },
  34. CoordBL: STCoordinates{
  35. T: uv.BottomLeftT,
  36. S: uv.BottomLeftS,
  37. },
  38. CoordBR: STCoordinates{
  39. T: uv.BottomRightT,
  40. S: uv.BottomRightS,
  41. },
  42. }
  43. uvSets[i] = set
  44. }
  45. xmlData := XMLPIC{
  46. Name: name,
  47. UserData: userData,
  48. Visible: pic.Flag & 0x1,
  49. Widescreen: (pic.Flag & 0x2) >> 1,
  50. Flag: pic.Flag,
  51. Origin: Coord2D{X: float32(pic.Origin % 3), Y: float32(pic.Origin / 3)},
  52. Alpha: pic.Alpha,
  53. Padding: 0,
  54. Translate: Coord3D{X: pic.XTranslation, Y: pic.YTranslation, Z: pic.ZTranslation},
  55. Rotate: Coord3D{X: pic.XRotate, Y: pic.YRotate, Z: pic.ZRotate},
  56. Scale: Coord2D{X: pic.XScale, Y: pic.YScale},
  57. Width: pic.Width,
  58. Height: pic.Height,
  59. TopLeftColor: Color8{
  60. R: pic.TopLeftColor[0],
  61. G: pic.TopLeftColor[1],
  62. B: pic.TopLeftColor[2],
  63. A: pic.TopLeftColor[3],
  64. },
  65. TopRightColor: Color8{
  66. R: pic.TopRightColor[0],
  67. G: pic.TopRightColor[1],
  68. B: pic.TopRightColor[2],
  69. A: pic.TopRightColor[3],
  70. },
  71. BottomLeftColor: Color8{
  72. R: pic.BottomLeftColor[0],
  73. G: pic.BottomLeftColor[1],
  74. B: pic.BottomLeftColor[2],
  75. A: pic.BottomLeftColor[3],
  76. },
  77. BottomRightColor: Color8{
  78. R: pic.BottomRightColor[0],
  79. G: pic.BottomRightColor[1],
  80. B: pic.BottomRightColor[2],
  81. A: pic.BottomRightColor[3],
  82. },
  83. MatIndex: pic.MatIndex,
  84. UVSets: &XMLUVSets{Set: uvSets},
  85. }
  86. if r.HasChildren() {
  87. xmlData.Children, err = r.ParseChildren()
  88. if err != nil {
  89. return nil, err
  90. }
  91. }
  92. return &xmlData, nil
  93. }
  94. func (b *BRLYTWriter) WritePIC(pic XMLPIC) error {
  95. header := SectionHeader{
  96. Type: SectionTypePIC,
  97. Size: uint32(96 + (32 * len(pic.UVSets.Set))),
  98. }
  99. var name [16]byte
  100. copy(name[:], pic.Name)
  101. var userData [8]byte
  102. copy(userData[:], pic.UserData)
  103. pane := PIC{
  104. Flag: pic.Flag,
  105. Origin: uint8(pic.Origin.X + (pic.Origin.Y * 3)),
  106. Alpha: pic.Alpha,
  107. PaneName: name,
  108. UserData: userData,
  109. XTranslation: pic.Translate.X,
  110. YTranslation: pic.Translate.Y,
  111. ZTranslation: pic.Translate.Z,
  112. XRotate: pic.Rotate.X,
  113. YRotate: pic.Rotate.Y,
  114. ZRotate: pic.Rotate.Z,
  115. XScale: pic.Scale.X,
  116. YScale: pic.Scale.Y,
  117. Width: pic.Width,
  118. Height: pic.Height,
  119. TopLeftColor: [4]uint8{pic.TopLeftColor.R, pic.TopLeftColor.G, pic.TopLeftColor.B, pic.TopLeftColor.A},
  120. TopRightColor: [4]uint8{pic.TopRightColor.R, pic.TopRightColor.G, pic.TopRightColor.B, pic.TopRightColor.A},
  121. BottomLeftColor: [4]uint8{pic.BottomLeftColor.R, pic.BottomLeftColor.G, pic.BottomLeftColor.B, pic.BottomLeftColor.A},
  122. BottomRightColor: [4]uint8{pic.BottomRightColor.R, pic.BottomRightColor.G, pic.BottomRightColor.B, pic.BottomRightColor.A},
  123. MatIndex: pic.MatIndex,
  124. NumOfUVSets: uint8(len(pic.UVSets.Set)),
  125. }
  126. err := write(b, header)
  127. if err != nil {
  128. return err
  129. }
  130. err = write(b, pane)
  131. if err != nil {
  132. return err
  133. }
  134. // Write the UV Sets
  135. for _, set := range pic.UVSets.Set {
  136. uvSet := UVSet{
  137. TopLeftS: set.CoordTL.S,
  138. TopLeftT: set.CoordTL.T,
  139. TopRightS: set.CoordTR.S,
  140. TopRightT: set.CoordTR.T,
  141. BottomLeftS: set.CoordBL.S,
  142. BottomLeftT: set.CoordBL.T,
  143. BottomRightS: set.CoordBR.S,
  144. BottomRightT: set.CoordBR.T,
  145. }
  146. err = write(b, uvSet)
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. return nil
  152. }