OSDN Git Service

モデルデータ不備の異常系を別パッケージに
[mikutoga/TogaGem.git] / src / main / java / jp / sourceforge / mikutoga / vmd / model / binio / VmdExporter.java
1 /*
2  * vmd exporter
3  *
4  * License : The MIT License
5  * Copyright(c) 2011 MikuToga Partners
6  */
7
8 package jp.sourceforge.mikutoga.vmd.model.binio;
9
10 import java.io.IOException;
11 import java.io.OutputStream;
12 import jp.sourceforge.mikutoga.binio.BinaryExporter;
13 import jp.sourceforge.mikutoga.binio.IllegalTextExportException;
14 import jp.sourceforge.mikutoga.vmd.IllegalVmdDataException;
15 import jp.sourceforge.mikutoga.vmd.VmdConst;
16 import jp.sourceforge.mikutoga.vmd.model.VmdMotion;
17
18 /**
19  * VMDファイルのエクスポーター。
20  */
21 public class VmdExporter extends BinaryExporter {
22
23     private static final byte[] HEADFILLER = {
24         (byte)'J',
25         (byte)'K',
26         (byte)'L',
27         (byte)'M',
28     };
29
30     private static final byte[] FDFILLER =
31         { (byte)0x00, (byte)0xfd };
32
33
34     private final BasicExporter    basicExporter;
35     private final CameraExporter   cameraExporter;
36     private final LightingExporter lightingExporter;
37
38
39     /**
40      * コンストラクタ。
41      * @param stream 出力ストリーム
42      */
43     public VmdExporter(OutputStream stream){
44         super(stream);
45
46         this.basicExporter    = new BasicExporter(stream);
47         this.cameraExporter   = new CameraExporter(stream);
48         this.lightingExporter = new LightingExporter(stream);
49
50         return;
51     }
52
53     /**
54      * モーションデータをVMDファイル形式で出力する。
55      * <p>異常時には出力データのフラッシュが試みられる。
56      * @param motion モーションデータ
57      * @throws IOException 出力エラー
58      * @throws IllegalVmdDataException モーションデータに不備が発見された
59      */
60     public void dumpVmdMotion(VmdMotion motion)
61             throws IOException, IllegalVmdDataException{
62         try{
63             dumpVmdMotionImpl(motion);
64         }finally{
65             flush();
66         }
67
68         return;
69     }
70
71     /**
72      * モーションデータをVMDファイル形式で出力する。
73      * @param motion モーションデータ
74      * @throws IOException 出力エラー
75      * @throws IllegalVmdDataException モーションデータに不備が発見された
76      */
77     private void dumpVmdMotionImpl(VmdMotion motion)
78             throws IOException, IllegalVmdDataException{
79         dumpHeader();
80
81         try{
82             dumpModelName(motion);
83             this.basicExporter.dumpBoneMotion(motion);
84             this.basicExporter.dumpMorphMotion(motion);
85         }catch(IllegalTextExportException e){
86             throw new IllegalVmdDataException(e);
87         }
88
89         this.cameraExporter.dumpCameraMotion(motion);
90         this.lightingExporter.dumpLuminousMotion(motion);
91         this.lightingExporter.dumpShadowMotion(motion);
92
93         return;
94     }
95
96     /**
97      * ヘッダ情報を出力する。
98      * @throws IOException 出力エラー
99      */
100     private void dumpHeader() throws IOException{
101         byte[] header = VmdConst.createMagicHeader();
102         dumpByteArray(header);
103         dumpByteArray(HEADFILLER);
104
105         assert header.length + HEADFILLER.length == VmdConst.HEADER_LENGTH;
106
107         return;
108     }
109
110     /**
111      * モデル名を出力する。
112      * <p>演出データのモデル名には
113      * 便宜的に{@link VmdConst.MODELNAME_STAGEACT}が使われる。
114      * @param motion モーションデータ
115      * @throws IOException 出力エラー
116      * @throws IllegalTextExportException 不正なモデル名の出現
117      */
118     private void dumpModelName(VmdMotion motion)
119             throws IOException, IllegalTextExportException{
120         String modelName = motion.getModelName();
121         if(modelName == null) modelName = VmdConst.MODELNAME_STAGEACT;
122
123         dumpFixedW31j(modelName, VmdConst.MODELNAME_MAX, FDFILLER);
124
125         return;
126     }
127
128 }