wnd.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "strings"
  6. )
  7. type Window struct {
  8. Flag uint8
  9. Origin uint8
  10. Alpha uint8
  11. _ uint8
  12. PaneName [16]byte
  13. UserData [8]byte
  14. XTranslation float32
  15. YTranslation float32
  16. ZTranslation float32
  17. XRotate float32
  18. YRotate float32
  19. ZRotate float32
  20. XScale float32
  21. YScale float32
  22. Width float32
  23. Height float32
  24. Coordinate1 float32
  25. Coordinate2 float32
  26. Coordinate3 float32
  27. Coordinate4 float32
  28. FrameCount uint8
  29. _ [3]byte
  30. WindowOffset uint32
  31. WindowFrameOffset uint32
  32. TopLeftColor [4]uint8
  33. TopRightColor [4]uint8
  34. BottomLeftColor [4]uint8
  35. BottomRightColor [4]uint8
  36. MatIndex uint16
  37. NumOfUVSets uint8
  38. _ uint8
  39. }
  40. type WindowMat struct {
  41. MatIndex uint16
  42. Index uint8
  43. _ uint8
  44. }
  45. func (r *Root) ParseWND(data []byte) {
  46. var wnd Window
  47. err := binary.Read(bytes.NewReader(data), binary.BigEndian, &wnd)
  48. if err != nil {
  49. panic(err)
  50. }
  51. // Strip the null bytes from the strings
  52. name := strings.Replace(string(wnd.PaneName[:]), "\x00", "", -1)
  53. userData := strings.Replace(string(wnd.UserData[:]), "\x00", "", -1)
  54. // Get the UVSets
  55. uvSets := make([]XMLUVSet, wnd.NumOfUVSets)
  56. for i := 0; i < int(wnd.NumOfUVSets); i++ {
  57. offset := 116 + (i * 32)
  58. var uv UVSet
  59. err := binary.Read(bytes.NewReader(data[offset:]), binary.BigEndian, &uv)
  60. if err != nil {
  61. panic(err)
  62. }
  63. set := XMLUVSet{
  64. CoordTL: STCoordinates{
  65. T: uv.TopLeftT,
  66. S: uv.TopLeftS,
  67. },
  68. CoordTR: STCoordinates{
  69. T: uv.TopRightT,
  70. S: uv.TopRightS,
  71. },
  72. CoordBL: STCoordinates{
  73. T: uv.BottomLeftT,
  74. S: uv.BottomLeftS,
  75. },
  76. CoordBR: STCoordinates{
  77. T: uv.BottomRightT,
  78. S: uv.BottomRightS,
  79. },
  80. }
  81. uvSets[i] = set
  82. }
  83. // Parse Window Mat table
  84. mats := make([]XMLWindowMat, wnd.FrameCount)
  85. for i := 0; i < int(wnd.FrameCount); i++ {
  86. offset := 116 + (int(wnd.NumOfUVSets) * 32) + (i * 4)
  87. var actualOffset uint32
  88. err := binary.Read(bytes.NewReader(data[offset:]), binary.BigEndian, &actualOffset)
  89. if err != nil {
  90. panic(err)
  91. }
  92. var mat WindowMat
  93. err = binary.Read(bytes.NewReader(data[actualOffset-8:]), binary.BigEndian, &mat)
  94. if err != nil {
  95. panic(err)
  96. }
  97. material := XMLWindowMat{
  98. MatIndex: mat.MatIndex,
  99. Index: mat.Index,
  100. }
  101. mats[i] = material
  102. }
  103. xmlData := XMLWindow{
  104. Name: name,
  105. UserData: userData,
  106. Visible: wnd.Flag & 0x1,
  107. Widescreen: (wnd.Flag & 0x2) >> 1,
  108. Flag: wnd.Flag,
  109. Origin: Coord2D{X: float32(wnd.Origin % 3), Y: float32(wnd.Origin / 3)},
  110. Alpha: wnd.Alpha,
  111. Padding: 0,
  112. Translate: Coord3D{X: wnd.XTranslation, Y: wnd.YTranslation, Z: wnd.ZTranslation},
  113. Rotate: Coord3D{X: wnd.XRotate, Y: wnd.YRotate, Z: wnd.ZRotate},
  114. Scale: Coord2D{X: wnd.XScale, Y: wnd.YScale},
  115. Width: wnd.Width,
  116. Height: wnd.Height,
  117. Coordinate1: wnd.Coordinate1,
  118. Coordinate2: wnd.Coordinate2,
  119. Coordinate3: wnd.Coordinate3,
  120. Coordinate4: wnd.Coordinate4,
  121. TopLeftColor: Color8{
  122. R: wnd.TopLeftColor[0],
  123. G: wnd.TopLeftColor[1],
  124. B: wnd.TopLeftColor[2],
  125. A: wnd.TopLeftColor[3],
  126. },
  127. TopRightColor: Color8{
  128. R: wnd.TopRightColor[0],
  129. G: wnd.TopRightColor[1],
  130. B: wnd.TopRightColor[2],
  131. A: wnd.TopRightColor[3],
  132. },
  133. BottomLeftColor: Color8{
  134. R: wnd.BottomLeftColor[0],
  135. G: wnd.BottomLeftColor[1],
  136. B: wnd.BottomLeftColor[2],
  137. A: wnd.BottomLeftColor[3],
  138. },
  139. BottomRightColor: Color8{
  140. R: wnd.BottomRightColor[0],
  141. G: wnd.BottomRightColor[1],
  142. B: wnd.BottomRightColor[2],
  143. A: wnd.BottomRightColor[3],
  144. },
  145. MatIndex: wnd.MatIndex,
  146. UVSets: &XMLUVSets{Set: uvSets},
  147. Materials: &XMLWindowMats{Mats: mats},
  148. }
  149. r.Panes = append(r.Panes, Children{WND: &xmlData})
  150. }
  151. func (b *BRLYTWriter) WriteWND(data XMLWindow) {
  152. temp := bytes.NewBuffer(nil)
  153. header := SectionHeader{
  154. Type: SectionTypeWND,
  155. Size: 76,
  156. }
  157. var name [16]byte
  158. copy(name[:], data.Name)
  159. var userData [8]byte
  160. copy(userData[:], data.UserData)
  161. wnd := Window{
  162. Flag: data.Flag,
  163. Origin: uint8(data.Origin.X + (data.Origin.Y * 3)),
  164. Alpha: data.Alpha,
  165. PaneName: name,
  166. UserData: userData,
  167. XTranslation: data.Translate.X,
  168. YTranslation: data.Translate.Y,
  169. ZTranslation: data.Translate.Z,
  170. XRotate: data.Rotate.X,
  171. YRotate: data.Rotate.Y,
  172. ZRotate: data.Rotate.Z,
  173. XScale: data.Scale.X,
  174. YScale: data.Scale.Y,
  175. Width: data.Width,
  176. Height: data.Height,
  177. Coordinate1: data.Coordinate1,
  178. Coordinate2: data.Coordinate2,
  179. Coordinate3: data.Coordinate3,
  180. Coordinate4: data.Coordinate4,
  181. FrameCount: uint8(len(data.Materials.Mats)),
  182. WindowOffset: 104,
  183. WindowFrameOffset: uint32(124 + len(data.UVSets.Set)*32),
  184. TopLeftColor: [4]uint8{data.TopLeftColor.R, data.TopLeftColor.G, data.TopLeftColor.B, data.TopLeftColor.A},
  185. TopRightColor: [4]uint8{data.TopRightColor.R, data.TopRightColor.G, data.TopRightColor.B, data.TopRightColor.A},
  186. BottomLeftColor: [4]uint8{data.BottomLeftColor.R, data.BottomLeftColor.G, data.BottomLeftColor.B, data.BottomLeftColor.A},
  187. BottomRightColor: [4]uint8{data.BottomRightColor.R, data.BottomRightColor.G, data.BottomRightColor.B, data.BottomRightColor.A},
  188. MatIndex: data.MatIndex,
  189. NumOfUVSets: uint8(len(data.UVSets.Set)),
  190. }
  191. write(temp, header)
  192. write(temp, wnd)
  193. // Write the UV Sets
  194. for _, set := range data.UVSets.Set {
  195. uvSet := UVSet{
  196. TopLeftS: set.CoordTL.S,
  197. TopLeftT: set.CoordTL.T,
  198. TopRightS: set.CoordTR.S,
  199. TopRightT: set.CoordTR.T,
  200. BottomLeftS: set.CoordBL.S,
  201. BottomLeftT: set.CoordBL.T,
  202. BottomRightS: set.CoordBR.S,
  203. BottomRightT: set.CoordBR.T,
  204. }
  205. write(temp, uvSet)
  206. }
  207. // Write the offsets to the Window Mats
  208. for i := 0; i < len(data.Materials.Mats); i++ {
  209. var offset uint32
  210. offset = uint32(temp.Len() + (len(data.Materials.Mats) * 4))
  211. write(temp, offset)
  212. }
  213. // Write Window Mats
  214. for _, mat := range data.Materials.Mats {
  215. windowMat := WindowMat{
  216. MatIndex: mat.MatIndex,
  217. Index: mat.Index,
  218. }
  219. write(temp, windowMat)
  220. }
  221. binary.BigEndian.PutUint32(temp.Bytes()[4:8], uint32(temp.Len()))
  222. write(b, temp.Bytes())
  223. }