pane.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "strings"
  6. )
  7. // Pane represents the structure of a pan1 section.
  8. type Pane struct {
  9. Flag uint8
  10. Origin uint8
  11. Alpha uint8
  12. _ uint8
  13. PaneName [16]byte
  14. UserData [8]byte
  15. XTranslation float32
  16. YTranslation float32
  17. ZTranslation float32
  18. XRotate float32
  19. YRotate float32
  20. ZRotate float32
  21. XScale float32
  22. YScale float32
  23. Width float32
  24. Height float32
  25. }
  26. func (r *Root) ParsePAN(data []byte) {
  27. var pane Pane
  28. err := binary.Read(bytes.NewReader(data), binary.BigEndian, &pane)
  29. if err != nil {
  30. panic(err)
  31. }
  32. // Strip the null bytes from the strings
  33. name := strings.Replace(string(pane.PaneName[:]), "\x00", "", -1)
  34. userData := strings.Replace(string(pane.UserData[:]), "\x00", "", -1)
  35. xmlData := XMLPane{
  36. Name: name,
  37. UserData: userData,
  38. Flag: pane.Flag,
  39. Origin: Coord2D{X: float32(pane.Origin % 3), Y: float32(pane.Origin / 3)},
  40. Alpha: pane.Alpha,
  41. Padding: 0,
  42. Translate: Coord3D{X: pane.XTranslation, Y: pane.YTranslation, Z: pane.ZTranslation},
  43. Rotate: Coord3D{X: pane.XRotate, Y: pane.YRotate, Z: pane.ZRotate},
  44. Scale: Coord2D{X: pane.XScale, Y: pane.YScale},
  45. Width: pane.Width,
  46. Height: pane.Height,
  47. }
  48. r.Panes = append(r.Panes, Children{Pane: &xmlData})
  49. }
  50. // ParseBND is almost 1:1 with ParsePAN as PAN and BND are the same types.
  51. func (r *Root) ParseBND(data []byte) {
  52. var pane Pane
  53. err := binary.Read(bytes.NewReader(data), binary.BigEndian, &pane)
  54. if err != nil {
  55. panic(err)
  56. }
  57. // Strip the null bytes from the strings
  58. name := strings.Replace(string(pane.PaneName[:]), "\x00", "", -1)
  59. userData := strings.Replace(string(pane.UserData[:]), "\x00", "", -1)
  60. xmlData := XMLBND{
  61. Name: name,
  62. UserData: userData,
  63. Flag: pane.Flag,
  64. Origin: Coord2D{X: float32(pane.Origin % 3), Y: float32(pane.Origin / 3)},
  65. Alpha: pane.Alpha,
  66. Padding: 0,
  67. Translate: Coord3D{X: pane.XTranslation, Y: pane.YTranslation, Z: pane.ZTranslation},
  68. Rotate: Coord3D{X: pane.XRotate, Y: pane.YRotate, Z: pane.ZRotate},
  69. Scale: Coord2D{X: pane.XScale, Y: pane.YScale},
  70. Width: pane.Width,
  71. Height: pane.Height,
  72. }
  73. r.Panes = append(r.Panes, Children{BND: &xmlData})
  74. }
  75. func (r *Root) ParsePAS() {
  76. r.Panes = append(r.Panes, Children{
  77. PAS: &XMLPAS{},
  78. })
  79. }
  80. func (r *Root) ParsePAE() {
  81. r.Panes = append(r.Panes, Children{
  82. PAE: &XMLPAE{},
  83. })
  84. }
  85. func (b *BRLYTWriter) WritePane(pan XMLPane) {
  86. header := SectionHeader{
  87. Type: SectionTypePAN,
  88. Size: 76,
  89. }
  90. var name [16]byte
  91. copy(name[:], pan.Name)
  92. var userData [8]byte
  93. copy(userData[:], pan.UserData)
  94. pane := Pane{
  95. Flag: pan.Flag,
  96. Origin: uint8(pan.Origin.X + (pan.Origin.Y * 3)),
  97. Alpha: pan.Alpha,
  98. PaneName: name,
  99. UserData: userData,
  100. XTranslation: pan.Translate.X,
  101. YTranslation: pan.Translate.Y,
  102. ZTranslation: pan.Translate.Z,
  103. XRotate: pan.Rotate.X,
  104. YRotate: pan.Rotate.Y,
  105. ZRotate: pan.Rotate.Z,
  106. XScale: pan.Scale.X,
  107. YScale: pan.Scale.Y,
  108. Width: pan.Width,
  109. Height: pan.Height,
  110. }
  111. write(b, header)
  112. write(b, pane)
  113. }
  114. func (b *BRLYTWriter) WriteBND(pan XMLBND) {
  115. header := SectionHeader{
  116. Type: SectionTypeBND,
  117. Size: 76,
  118. }
  119. var name [16]byte
  120. copy(name[:], pan.Name)
  121. var userData [8]byte
  122. copy(userData[:], pan.UserData)
  123. pane := Pane{
  124. Flag: pan.Flag,
  125. Origin: uint8(pan.Origin.X + (pan.Origin.Y * 3)),
  126. Alpha: pan.Alpha,
  127. PaneName: name,
  128. UserData: userData,
  129. XTranslation: pan.Translate.X,
  130. YTranslation: pan.Translate.Y,
  131. ZTranslation: pan.Translate.Z,
  132. XRotate: pan.Rotate.X,
  133. YRotate: pan.Rotate.Y,
  134. ZRotate: pan.Rotate.Z,
  135. XScale: pan.Scale.X,
  136. YScale: pan.Scale.Y,
  137. Width: pan.Width,
  138. Height: pan.Height,
  139. }
  140. write(b, header)
  141. write(b, pane)
  142. }
  143. func (b *BRLYTWriter) WritePAS() {
  144. header := SectionHeader{
  145. Type: SectionTypePAS,
  146. Size: 8,
  147. }
  148. write(b, header)
  149. }
  150. func (b *BRLYTWriter) WritePAE() {
  151. header := SectionHeader{
  152. Type: SectionTypePAE,
  153. Size: 8,
  154. }
  155. write(b, header)
  156. }