OSDN Git Service

d5da3684d1e6d0a260ba9cfa4aa67d28bf3ba97e
[mikumikustudio/MikuMikuStudio.git] / src / projectkyoto / mmd / file / VMDFile.java
1 /*
2  * Copyright (c) 2011 Kazuhiko Kobayashi
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'MMDLoaderJME' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 package projectkyoto.mmd.file;
34
35 import java.io.BufferedInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.net.URL;
39
40 /**
41  *
42  * @author kobayasi
43  */
44 public class VMDFile {
45     private String vmdHeader; // char[30] Vocaloid Motion Data 0002
46     private String vmdModelName; // char[20]
47     private int motionCount;
48     private VMDMotion motionArray[];
49     private int skinCount;
50     private VMDSkin skinArray[];
51
52     public VMDFile() {
53     }
54     
55     public VMDFile(URL url) throws IOException {
56         read(url.openStream());
57     }
58     public VMDFile(InputStream is2) throws IOException {
59         read(is2);
60     }
61     public void read(InputStream is2) throws IOException{
62         DataInputStreamLittleEndian is = null;
63         try {
64             is = new DataInputStreamLittleEndian(new BufferedInputStream(is2));
65             vmdHeader = is.readString(30);
66             if (!"Vocaloid Motion Data 0002".equals(vmdHeader)) {
67                 throw new InvalidVMDFileException();
68             }
69             vmdModelName = is.readString(20);
70             motionCount = is.readInt();
71             motionArray = new VMDMotion[motionCount];
72             for(int i=0;i<motionCount;i++) {
73                 motionArray[i] = new VMDMotion(is);
74             }
75             skinCount = is.readInt();
76             skinArray = new VMDSkin[skinCount];
77             for(int i=0;i<skinCount;i++) {
78                 skinArray[i] = new VMDSkin(is);
79             }
80         } finally {
81             if (is != null) {
82                 is.close();
83             }
84         }
85     }
86
87     @Override
88     public String toString() {
89         StringBuffer sb = new StringBuffer();
90         sb.append("{vmdHeader = ").append(vmdHeader).append("\n");
91         sb.append("vmdModelName = ").append(vmdModelName).append("\n");
92         sb.append("motionCount = ").append(motionCount).append("\n");
93         sb.append("motionArray = {\n");
94         for(VMDMotion m : motionArray) {
95             sb.append(m).append("\n");
96         }
97         sb.append("}\n");
98         sb.append("skinCount = ").append(skinCount).append("\n");
99         sb.append("skinArray = {\n");
100         for(VMDSkin skin : skinArray) {
101             sb.append(skin).append("\n");
102         }
103         sb.append("}\n");
104         return sb.toString();
105     }
106
107     public VMDMotion[] getMotionArray() {
108         return motionArray;
109     }
110
111     public void setMotionArray(VMDMotion[] motionArray) {
112         this.motionArray = motionArray;
113     }
114
115     public int getMotionCount() {
116         return motionCount;
117     }
118
119     public void setMotionCount(int motionCount) {
120         this.motionCount = motionCount;
121     }
122
123     public VMDSkin[] getSkinArray() {
124         return skinArray;
125     }
126
127     public void setSkinArray(VMDSkin[] skinArray) {
128         this.skinArray = skinArray;
129     }
130
131     public int getSkinCount() {
132         return skinCount;
133     }
134
135     public void setSkinCount(int skinCount) {
136         this.skinCount = skinCount;
137     }
138
139     public String getVmdHeader() {
140         return vmdHeader;
141     }
142
143     public void setVmdHeader(String vmdHeader) {
144         this.vmdHeader = vmdHeader;
145     }
146
147     public String getVmdModelName() {
148         return vmdModelName;
149     }
150
151     public void setVmdModelName(String vmdModelName) {
152         this.vmdModelName = vmdModelName;
153     }
154
155 }