OSDN Git Service

imported tso2mqo v0.31
[tdcgexplorer/tso2mqo.git] / TDCGFile.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.IO;\r
4 using System.Text;\r
5 \r
6 namespace Tso2MqoGui\r
7 {\r
8     public class TDCGFile\r
9     {\r
10         protected BinaryReader  r;\r
11 \r
12         public TDCGFile(string file)\r
13             : this(File.OpenRead(file))\r
14         {\r
15         }\r
16 \r
17         public TDCGFile(Stream s)\r
18             : this(new BinaryReader(s))\r
19         {\r
20         }\r
21 \r
22         public TDCGFile(BinaryReader r)\r
23         {\r
24             this.r  = r;\r
25         }\r
26 \r
27         public static int   debug_count = 0;\r
28 \r
29         public void ReadVertex(ref Vertex v)\r
30         {\r
31             v.Pos.X     = r.ReadSingle();\r
32             v.Pos.Y     = r.ReadSingle();\r
33             v.Pos.Z     = r.ReadSingle();\r
34             v.Nrm.X     = r.ReadSingle();\r
35             v.Nrm.Y     = r.ReadSingle();\r
36             v.Nrm.Z     = r.ReadSingle();\r
37             v.Tex.X     = r.ReadSingle();\r
38             v.Tex.Y     = r.ReadSingle();\r
39 \r
40             int     cnt = r.ReadInt32();\r
41             byte[]  idx = new byte[4]{0, 0, 0, 0};\r
42 \r
43           //System.Diagnostics.Debug.Assert(cnt >= 1 && cnt <= 4);\r
44 #if false\r
45             if(cnt >= 1)    { idx[0]= (byte)r.ReadInt32(); v.Wgt.X = r.ReadSingle(); }\r
46             if(cnt >= 2)    { idx[1]= (byte)r.ReadInt32(); v.Wgt.Y = r.ReadSingle(); }\r
47             if(cnt >= 3)    { idx[2]= (byte)r.ReadInt32(); v.Wgt.Z = r.ReadSingle(); }\r
48             if(cnt >= 4)    { idx[3]= (byte)r.ReadInt32(); v.Wgt.W = r.ReadSingle(); }\r
49 #endif\r
50 #if false\r
51             if(cnt >= 1)    { idx[3]= (byte)r.ReadInt32(); v.Wgt.X = r.ReadSingle(); }\r
52             if(cnt >= 2)    { idx[2]= (byte)r.ReadInt32(); v.Wgt.Y = r.ReadSingle(); }\r
53             if(cnt >= 3)    { idx[1]= (byte)r.ReadInt32(); v.Wgt.Z = r.ReadSingle(); }\r
54             if(cnt >= 4)    { idx[0]= (byte)r.ReadInt32(); v.Wgt.W = r.ReadSingle(); }\r
55 #endif\r
56 #if false\r
57             if(cnt >= 1)    { idx[0]= (byte)r.ReadInt32(); v.Wgt.W = r.ReadSingle(); }\r
58             if(cnt >= 2)    { idx[1]= (byte)r.ReadInt32(); v.Wgt.Z = r.ReadSingle(); }\r
59             if(cnt >= 3)    { idx[2]= (byte)r.ReadInt32(); v.Wgt.Y = r.ReadSingle(); }\r
60             if(cnt >= 4)    { idx[3]= (byte)r.ReadInt32(); v.Wgt.X = r.ReadSingle(); }\r
61 #endif\r
62 #if true\r
63             if(cnt >= 1)    { idx[3]= (byte)r.ReadInt32(); v.Wgt.W = r.ReadSingle(); }\r
64             if(cnt >= 2)    { idx[2]= (byte)r.ReadInt32(); v.Wgt.Z = r.ReadSingle(); }\r
65             if(cnt >= 3)    { idx[1]= (byte)r.ReadInt32(); v.Wgt.Y = r.ReadSingle(); }\r
66             if(cnt >= 4)    { idx[0]= (byte)r.ReadInt32(); v.Wgt.X = r.ReadSingle(); }\r
67             if(cnt >= 5)    { r.ReadInt32(); r.ReadSingle(); }\r
68             if(cnt >= 6)    { r.ReadInt32(); r.ReadSingle(); }\r
69             if(cnt >= 7)    { r.ReadInt32(); r.ReadSingle(); }\r
70             if(cnt >= 8)    { r.ReadInt32(); r.ReadSingle(); }\r
71 #endif\r
72 \r
73             /*\r
74           //if(debug_count++ < 30)\r
75             {\r
76                 System.Diagnostics.Debug.WriteLine("["\r
77                     + idx[0] + ":" + v.Wgt.X.ToString("N8") +"], ["\r
78                     + idx[1] + ":" + v.Wgt.Y.ToString("N8") +"], ["\r
79                     + idx[2] + ":" + v.Wgt.Z.ToString("N8") +"], ["\r
80                     + idx[3] + ":" + v.Wgt.W.ToString("N8") +"]");\r
81             }\r
82             */\r
83 \r
84             v.Idx   = BitConverter.ToUInt32(idx, 0);\r
85         }\r
86 \r
87         public void ReadVertexDebug(ref Vertex v)\r
88         {\r
89             v.Pos.X     = r.ReadSingle();\r
90             v.Pos.Y     = r.ReadSingle();\r
91             v.Pos.Z     = r.ReadSingle();\r
92             v.Nrm.X     = r.ReadSingle();\r
93             v.Nrm.Y     = r.ReadSingle();\r
94             v.Nrm.Z     = r.ReadSingle();\r
95             v.Tex.X     = r.ReadSingle();\r
96             v.Tex.Y     = r.ReadSingle();\r
97 \r
98             int     cnt = r.ReadInt32();\r
99             byte[]  idx = new byte[4]{0, 0, 0, 0};\r
100 \r
101             System.Diagnostics.Debug.Assert(cnt >= 1 && cnt <= 4);\r
102 \r
103             if(cnt >= 1)    { idx[3]= (byte)r.ReadInt32(); v.Wgt.W = r.ReadSingle(); }\r
104             if(cnt >= 2)    { idx[2]= (byte)r.ReadInt32(); v.Wgt.Z = r.ReadSingle(); }\r
105             if(cnt >= 3)    { idx[1]= (byte)r.ReadInt32(); v.Wgt.Y = r.ReadSingle(); }\r
106             if(cnt >= 4)    { idx[0]= (byte)r.ReadInt32(); v.Wgt.X = r.ReadSingle(); }\r
107 \r
108             v.Idx   = BitConverter.ToUInt32(idx, 0);\r
109 \r
110             System.Diagnostics.Debug.WriteLine("cnt=" + cnt);\r
111         }\r
112 \r
113         Encoding    enc = Encoding.GetEncoding("Shift_JIS");\r
114         List<byte>  buf = new List<byte>();\r
115 \r
116         public string ReadString()\r
117         {\r
118             buf.Clear();\r
119 \r
120             for(;;)\r
121             {\r
122                 byte    b   = r.ReadByte();\r
123 \r
124                 if(b == 0)\r
125                     break;\r
126 \r
127                 buf.Add(b);\r
128             }\r
129 \r
130             return enc.GetString(buf.ToArray());\r
131         }\r
132 \r
133         public unsafe Matrix44 ReadMatrix()\r
134         {\r
135             Matrix44    m   = new Matrix44();\r
136             float*      p   = &m.m11;\r
137 \r
138             for(int i= 0; i < 16; ++i)\r
139                 *p++    = r.ReadSingle();\r
140 \r
141             return m;\r
142         }\r
143     }\r
144 }\r