From 2685577bb6c42a600c5b197977448eb706c3437d Mon Sep 17 00:00:00 2001 From: Olyutorskii Date: Wed, 6 Oct 2010 21:17:37 +0900 Subject: [PATCH] =?utf8?q?TogaGem1.103.2=E7=89=88=E3=82=88=E3=82=8A?= =?utf8?q?=E7=A7=BB=E8=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CHANGELOG.txt | 11 + LICENSE.txt | 34 ++ README.txt | 96 +++ build.xml | 68 +++ pom.xml | 281 +++++++++ src/main/assembly/descriptor.xml | 38 ++ src/main/config/checks.xml | 367 ++++++++++++ src/main/config/pmdrules.xml | 80 +++ .../sourceforge/mikutoga/parser/CommonParser.java | 327 +++++++++++ .../sourceforge/mikutoga/parser/LoopHandler.java | 44 ++ .../mikutoga/parser/MmdEofException.java | 54 ++ .../mikutoga/parser/MmdFormatException.java | 100 ++++ .../jp/sourceforge/mikutoga/parser/MmdSource.java | 328 +++++++++++ .../jp/sourceforge/mikutoga/parser/ParseStage.java | 47 ++ .../sourceforge/mikutoga/parser/package-info.java | 17 + .../mikutoga/parser/pmd/PmdBasicHandler.java | 54 ++ .../mikutoga/parser/pmd/PmdBoneHandler.java | 137 +++++ .../mikutoga/parser/pmd/PmdEngHandler.java | 82 +++ .../mikutoga/parser/pmd/PmdJointHandler.java | 139 +++++ .../sourceforge/mikutoga/parser/pmd/PmdLimits.java | 81 +++ .../mikutoga/parser/pmd/PmdMaterialHandler.java | 100 ++++ .../mikutoga/parser/pmd/PmdMorphHandler.java | 77 +++ .../sourceforge/mikutoga/parser/pmd/PmdParser.java | 29 + .../mikutoga/parser/pmd/PmdParserBase.java | 642 +++++++++++++++++++++ .../mikutoga/parser/pmd/PmdParserExt1.java | 177 ++++++ .../mikutoga/parser/pmd/PmdParserExt2.java | 84 +++ .../mikutoga/parser/pmd/PmdParserExt3.java | 208 +++++++ .../mikutoga/parser/pmd/PmdRigidHandler.java | 130 +++++ .../mikutoga/parser/pmd/PmdShapeHandler.java | 107 ++++ .../mikutoga/parser/pmd/PmdToonHandler.java | 39 ++ .../mikutoga/parser/pmd/package-info.java | 14 + .../mikutoga/parser/ParseStageTest.java | 47 ++ src/test/java/sample/pmd/DummyHandler.java | 336 +++++++++++ src/test/java/sample/pmd/DummyMain.java | 104 ++++ 34 files changed, 4479 insertions(+) create mode 100644 CHANGELOG.txt create mode 100644 LICENSE.txt create mode 100644 README.txt create mode 100644 build.xml create mode 100644 pom.xml create mode 100644 src/main/assembly/descriptor.xml create mode 100644 src/main/config/checks.xml create mode 100644 src/main/config/pmdrules.xml create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/CommonParser.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/LoopHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/MmdEofException.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/MmdFormatException.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/MmdSource.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/ParseStage.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/package-info.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBasicHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBoneHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdEngHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdJointHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdLimits.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMaterialHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMorphHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParser.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserBase.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt1.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt2.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt3.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdRigidHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdShapeHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdToonHandler.java create mode 100644 src/main/java/jp/sourceforge/mikutoga/parser/pmd/package-info.java create mode 100644 src/test/java/jp/sourceforge/mikutoga/parser/ParseStageTest.java create mode 100644 src/test/java/sample/pmd/DummyHandler.java create mode 100644 src/test/java/sample/pmd/DummyMain.java diff --git a/CHANGELOG.txt b/CHANGELOG.txt new file mode 100644 index 0000000..d90cb85 --- /dev/null +++ b/CHANGELOG.txt @@ -0,0 +1,11 @@ +[UTF-8 Japanese] + + +TogaGem 変更履歴 + + +1.104-SNAPSHOT (2010-10-06) + ・TogaParser1.103.2版を吸収する形で開発をスタート。 + + +--- EOF --- diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..225e3e3 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,34 @@ +[UTF-8 English & Japanese] + +The MIT License + + +Copyright(c) 2010 olyutorskii + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + +TogaGemオリジナル制作者自身からのコメント: + + ※ 少なくともこのソフトウェアの実行、複製、配布、改造は自由です。 + ※ 少なくともこのソフトウェアは無保証です。 + + +--- EOF --- diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..1672527 --- /dev/null +++ b/README.txt @@ -0,0 +1,96 @@ +[UTF-8 Japanese] + + T o g a G e m + Readme + + Copyright(c) 2010 olyutorskii + + +=== TogaGemとは === + +TogaGemは、3D動画制作ツール、MikuMikuDance(MMD)で用いられる +各種データファイルを読み書きするためのJavaライブラリです。 +TogaGemは、MikuTogaプロジェクトの派生ライブラリとして、 +TogaParserライブラリの1.103.2版を引き継ぐ形で誕生しました。 + +MikuTogaプロジェクトは、MMDによる3Dアニメーション制作を支援するプログラムの +整備のために発足した、オープンソースプロジェクトです。 + +現時点では、PMDモデルファイル(*.pmd)の読み書きが可能です。 + +※ MMD開発者の樋口M氏は、MikuTogaの開発活動に一切関与していません。 +  TogaGemに関する問い合わせをを樋口M氏へ投げかけないように!約束だよ! + + +=== 実行環境 === + + - TogaGemはJava言語(JLS3)で記述されたプログラムです。 + - TogaGemはJRE1.6に準拠したJava実行環境で利用できるように作られています。 + 原則として、JRE1.6に準拠した実行系であれば、プラットフォームを選びません。 + + +=== アーカイブ管理体制 === + + このアーカイブは、UTF-8による開発環境を前提として構成されています。 + このアーカイブの原本となる開発資産は、 + http://hg.sourceforge.jp/view/mikutoga/TogaGem + を上位に持つMercurialリポジトリで管理されています。 + アーカイブの代わりにMercurialを通じての開発資産へのアクセスが可能です。 + # hg clone http://hg.sourceforge.jp/view/mikutoga/TogaGem + + +=== 開発プロジェクト運営元 === + + http://sourceforge.jp/projects/mikutoga/ まで。 + + +=== ディレクトリ内訳構成 === + +基本的にはMaven2のmaven-archetype-quickstart構成に準じます。 + +./README.txt + あなたが今見てるこれ。 + +./CHANGELOG.txt + 変更履歴。 + +./LICENSE.txt + ライセンスに関して。 + +./pom.xml + Maven2用プロジェクト構成定義ファイル。 + +./src/main/java/ + Javaのソースコード。 + +./src/test/java/ + JUnit 4.* 用のユニットテストコード。 + +./src/test/java/sample/ + パーサ利用のサンプルプログラム。 + +./src/main/config/ + 各種ビルド・構成管理に必要なファイル群。 + +./src/main/config/checks.xml + Checkstyle用configファイル。 + +./src/main/config/pmdrules.xml + PMD用ルール定義ファイル。 + + +=== 謝辞 === + +このライブラリを制作するにあたって、以下の情報を参考にしました。 + + +通りすがりの記憶 (T.Tetosuki氏ブログ) +「MMD のモデルデータ(PMD)形式 めも (まとめ)」 +http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4 + + +PMDエディタver0.0.6.3(極北P氏制作) +同梱readme.txt + + +--- EOF --- diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..233a6e0 --- /dev/null +++ b/build.xml @@ -0,0 +1,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..943fe0d --- /dev/null +++ b/pom.xml @@ -0,0 +1,281 @@ + + + + + + 4.0.0 + + jp.sourceforge.mikutoga + togagem + + 1.104-SNAPSHOT + + jar + TogaGem + + TogaGem is a file reader/writer library for MikuMikuDance(MMD) with Java. TogaGem is a sub-project of MikuToga project. + + http://sourceforge.jp/projects/mikutoga/ + 2010 + + + MikuToga Partners + http://sourceforge.jp/projects/mikutoga/ + + + + + The MIT License + http://www.opensource.org/licenses/mit-license.php + manual + + + + + + olyutorskii + http://sites.google.com/site/olyutorskiipit/ + MikuToga Partners + http://sourceforge.jp/projects/mikutoga/devel/ + + Project Founder + Java Developer + + + + + + + + + 2.1 + + + + + + scm:hg:http://hg.sourceforge.jp/view/mikutoga/TogaGem + scm:hg:ssh://hg.sourceforge.jp//hgroot/mikutoga/TogaGem + http://hg.sourceforge.jp/view/mikutoga/TogaGem + + + + SourceForge.JP + http://sourceforge.jp/projects/mikutoga/ticket/ + + + + + + + UTF-8 + + 1.6 + 1.6 + + true + true + + UTF-8 + UTF-8 + + ${project.basedir}/src/main/config + + + + + + + + junit + junit + [4,) + test + + + + + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.6 + 1.6 + true + true + + + + + + + + org.apache.maven.plugins + maven-clean-plugin + + + + ${project.basedir} + + **/.DS_Store + **/Thumbs.db + **/core + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + ${project.organization.name} + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + src/main/assembly/descriptor.xml + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + + + + [2.1,3) + + + [1.6,) + + + + + + + + + + + src/main/resources + true + + **/version.properties + + + + + + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.7 + + protected + + + + + maven-site-plugin + 2.1.1 + + ja + ${project.build.sourceEncoding} + ${project.reporting.outputEncoding} + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.5 + + ${project.myrepoconf}/checks.xml + + + + + org.apache.maven.plugins + maven-pmd-plugin + 2.5 + + UTF-8 + 1.6 + + ${project.myrepoconf}/pmdrules.xml + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.3.1 + + Max + Low + ${project.build.sourceEncoding} + ${project.reporting.outputEncoding} + + + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.4 + + + + org.codehaus.mojo + javancss-maven-plugin + 2.0 + + + + org.apache.maven.plugins + maven-jxr-plugin + 2.2 + + + + + + + + diff --git a/src/main/assembly/descriptor.xml b/src/main/assembly/descriptor.xml new file mode 100644 index 0000000..b744cbd --- /dev/null +++ b/src/main/assembly/descriptor.xml @@ -0,0 +1,38 @@ + + + + + + + src + + + zip + + + + + + *.txt + pom.xml + build.xml + + true + + + src/ + true + + + + + + diff --git a/src/main/config/checks.xml b/src/main/config/checks.xml new file mode 100644 index 0000000..67da074 --- /dev/null +++ b/src/main/config/checks.xml @@ -0,0 +1,367 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/config/pmdrules.xml b/src/main/config/pmdrules.xml new file mode 100644 index 0000000..1b5fcf2 --- /dev/null +++ b/src/main/config/pmdrules.xml @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/CommonParser.java b/src/main/java/jp/sourceforge/mikutoga/parser/CommonParser.java new file mode 100644 index 0000000..443c86a --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/CommonParser.java @@ -0,0 +1,327 @@ +/* + * common MMD parser + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CoderResult; +import java.nio.charset.CodingErrorAction; + +/** + * 各種パーサの共通実装。 + */ +public class CommonParser { + + /** 日本語デコード作業用入力バッファ長。バイト単位。 */ + public static final int TEXTBUF_SZ = 512; + + /** + * MMD各種ファイルで用いられる日本語エンコーディング。(windows-31j) + * ほぼShift_JISのスーパーセットと思ってよい。 + * デコード結果はUCS-2集合に収まるはず。 + */ + protected static final Charset CS_WIN31J = Charset.forName("windows-31j"); + + private static final byte TERMINATOR = (byte) '\0'; // 0x00 + private static final char UCSYEN = '\u00a5'; + private static final char SJISYEN = (char) 0x005c; // '\u005c\u005c'; + + private final MmdSource source; + private final CharsetDecoder decoder; + private final byte[] textArray; + private final ByteBuffer textBuffer; // textArrayの別ビュー + private final CharBuffer charBuffer; + + /** + * コンストラクタ。 + * @param source 入力ソース + */ + public CommonParser(MmdSource source){ + super(); + + this.source = source; + this.decoder = CS_WIN31J.newDecoder(); + this.textArray = new byte[TEXTBUF_SZ]; + this.textBuffer = ByteBuffer.wrap(this.textArray); + int maxChars = + (int)(TEXTBUF_SZ * (this.decoder.maxCharsPerByte())) + 1; + this.charBuffer = CharBuffer.allocate(maxChars); + + this.decoder.onMalformedInput(CodingErrorAction.REPORT); + this.decoder.onUnmappableCharacter(CodingErrorAction.REPORT); + this.textBuffer.clear(); + this.charBuffer.clear(); + + return; + } + + /** + * 入力ソースにまだデータが残っているか判定する。 + * @return まだ読み込んでいないデータが残っていればtrue + * @throws IOException IOエラー + * @see MmdSource#hasMore() + */ + protected boolean hasMore() throws IOException{ + boolean result = this.source.hasMore(); + return result; + } + + /** + * 入力ソースを読み飛ばす。 + * @param skipLength 読み飛ばすバイト数。 + * @throws IOException IOエラー + * @throws MmdEofException 読み飛ばす途中でストリーム終端に達した。 + * @see MmdSource#skip(long) + */ + protected void skip(long skipLength) + throws IOException, MmdEofException { + long result = this.source.skip(skipLength); + if(result != skipLength){ + throw new MmdEofException(this.source.getPosition()); + } + + return; + } + + /** + * 入力ソースを読み飛ばす。 + * @param skipLength 読み飛ばすバイト数。 + * @throws IOException IOエラー + * @throws MmdEofException 読み飛ばす途中でストリーム終端に達した。 + * @see MmdSource#skip(long) + */ + protected void skip(int skipLength) + throws IOException, MmdEofException { + skip((long) skipLength); + } + + /** + * byte値を読み込む。 + * @return 読み込んだbyte値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseByte() + */ + protected byte parseByte() + throws IOException, MmdEofException{ + return this.source.parseByte(); + } + + /** + * 符号無し値としてbyte値を読み込み、int型に変換して返す。 + * 符号は拡張されない。(0xffは0x000000ffとなる) + * @return 読み込まれた値のint値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseUByteAsInteger() + */ + protected int parseUByteAsInteger() + throws IOException, MmdEofException{ + return this.source.parseUByteAsInteger(); + } + + /** + * byte値を読み込み、boolean型に変換して返す。 + * 0x00は偽、それ以外は真と解釈される。 + * @return 読み込まれた値のboolean値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseBoolean() + */ + protected boolean parseBoolean() + throws IOException, MmdEofException{ + return this.source.parseBoolean(); + } + + /** + * short値を読み込む。 + * short値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込んだshort値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseShort() + */ + protected short parseShort() + throws IOException, MmdEofException{ + return this.source.parseShort(); + } + + /** + * 符号無し値としてshort値を読み込み、int型に変換して返す。 + * 符号は拡張されない。(0xffffは0x0000ffffとなる) + * short値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込まれた値のint値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseUShortAsInteger() + */ + protected int parseUShortAsInteger() + throws IOException, MmdEofException{ + return this.source.parseUShortAsInteger(); + } + + /** + * int値を読み込む。 + * int値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込んだint値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseInteger() + */ + protected int parseInteger() + throws IOException, MmdEofException{ + return this.source.parseInteger(); + } + + /** + * float値を読み込む。 + * float値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込んだfloat値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseFloat() + */ + protected float parseFloat() + throws IOException, MmdEofException{ + return this.source.parseFloat(); + } + + /** + * byte配列を読み込む。 + * @param dst 格納先配列 + * @param offset 読み込み開始オフセット + * @param length 読み込みバイト数 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws IndexOutOfBoundsException 引数が配列属性と矛盾 + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseByteArray(byte[], int, int) + */ + protected void parseByteArray(byte[] dst, int offset, int length) + throws IOException, + NullPointerException, + IndexOutOfBoundsException, + MmdEofException { + this.source.parseByteArray(dst, offset, length); + return; + } + + /** + * byte配列を読み込む。 + * 配列要素全ての読み込みが試みられる。 + * @param dst 格納先配列 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseByteArray(byte[]) + */ + protected void parseByteArray(byte[] dst) + throws IOException, NullPointerException, MmdEofException{ + this.source.parseByteArray(dst); + return; + } + + /** + * float配列を読み込む。 + * @param dst 格納先配列 + * @param offset 読み込み開始オフセット + * @param length 読み込みfloat要素数 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws IndexOutOfBoundsException 引数が配列属性と矛盾 + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseFloatArray(float[], int, int) + */ + protected void parseFloatArray(float[] dst, int offset, int length) + throws IOException, + NullPointerException, + IndexOutOfBoundsException, + MmdEofException { + this.source.parseFloatArray(dst, offset, length); + return; + } + + /** + * float配列を読み込む。 + * 配列要素全ての読み込みが試みられる。 + * @param dst 格納先配列 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see MmdSource#parseFloatArray(float[]) + */ + protected void parseFloatArray(float[] dst) + throws IOException, NullPointerException, MmdEofException{ + this.source.parseFloatArray(dst); + return; + } + + /** + * 指定された最大バイト長に収まるゼロ終端(0x00)文字列を読み込む。 + * 入力バイト列はwindows-31jエンコーディングとして解釈される。 + * ゼロ終端以降のデータは無視されるが、 + * IO入力は指定バイト数だけ読み進められる。 + * ゼロ終端が見つからないまま指定バイト数が読み込み終わった場合、 + * そこまでのデータから文字列を構成する。 + *

+ * 戻り結果にはU+00A5(UCS円通貨記号)が含まれないことが保証される。 + * ※0x5c(Win31J円通貨)はU+005C(UCSバックスラッシュ)にデコードされる。 + * + * @param maxlen 読み込みバイト数 + * @return デコードされた文字列 + * @throws IOException IOエラー + * @throws IllegalArgumentException 読み込みバイト数が負であるか、 + * または内部バッファに対し大きすぎる。 + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @throws MmdFormatException 不正な文字エンコーディングが検出された。 + */ + protected String parseZeroTermString(int maxlen) + throws IOException, + IllegalArgumentException, + MmdEofException, + MmdFormatException { + if(this.textArray.length < maxlen || maxlen < 0){ + throw new IllegalArgumentException(); + } + + this.source.parseByteArray(this.textArray, 0, maxlen); + + int length = -1; + for(int pos = 0; pos < maxlen; pos++){ + byte ch = this.textArray[pos]; + if(ch == TERMINATOR){ + length = pos; + break; + } + } + if(length < 0) length = maxlen; + + this.textBuffer.rewind(); + this.textBuffer.limit(length); + this.charBuffer.clear(); + CoderResult decResult = + this.decoder.decode(this.textBuffer, this.charBuffer, true); + if( ! decResult.isUnderflow() || decResult.isError()){ + throw new MmdFormatException("illegal character encoding", + this.source.getPosition() ); + } + + this.charBuffer.flip(); + String result = this.charBuffer.toString(); + + if(result.indexOf(UCSYEN) >= 0){ + result = result.replace(UCSYEN, SJISYEN); + } + + return result; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/LoopHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/LoopHandler.java new file mode 100644 index 0000000..5183ede --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/LoopHandler.java @@ -0,0 +1,44 @@ +/* + * loop handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser; + +/** + * ループ構造の通知用ハンドラ。 + * ステージ指定により、多重ネストループをもサポートする。 + */ +public interface LoopHandler { + + /** + * ループ構造開始の通知を受け取る。 + * 0回ループの場合も含め一度呼ばれる。 + * @param stage ループ種別 + * @param loops ループ回数。未知の場合は負の値。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void loopStart(ParseStage stage, int loops) throws MmdFormatException; + + /** + * ループ構造の1イテレーション終了の通知を受け取る。 + * 1度しか回らないループでも呼ばれる。0回ループでは決して呼ばれない。 + * @param stage ループ種別 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void loopNext(ParseStage stage) throws MmdFormatException; + + /** + * ループ構造終了の通知を受け取る。 + * 0回ループの場合も含め一度呼ばれる。 + * @param stage ループ種別 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void loopEnd(ParseStage stage) throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/MmdEofException.java b/src/main/java/jp/sourceforge/mikutoga/parser/MmdEofException.java new file mode 100644 index 0000000..fac9deb --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/MmdEofException.java @@ -0,0 +1,54 @@ +/* + * unexpected file EOF founded exception + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser; + +/** + * 入力ソースが予期せずに終了した異常系。 + */ +@SuppressWarnings("serial") +public class MmdEofException extends MmdFormatException { + + /** + * コンストラクタ。 + */ + public MmdEofException(){ + this(null); + return; + } + + /** + * コンストラクタ。 + * @param message エラーメッセージ + */ + public MmdEofException(String message){ + this(message, -1L); + return; + } + + /** + * コンストラクタ。 + * @param position 入力ソース先頭からのエラー位置。(バイト単位) + * 負の値を与えると、エラー位置は無効と解釈される。 + */ + public MmdEofException(long position){ + this(null, position); + return; + } + + /** + * コンストラクタ。 + * @param message エラーメッセージ + * @param position 入力ソース先頭からのエラー位置。(バイト単位) + * 負の値を与えると、エラー位置は無効と解釈される。 + */ + public MmdEofException(String message, long position){ + super(message, position); + return; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/MmdFormatException.java b/src/main/java/jp/sourceforge/mikutoga/parser/MmdFormatException.java new file mode 100644 index 0000000..a56822c --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/MmdFormatException.java @@ -0,0 +1,100 @@ +/* + * unexpected file format founded exception + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser; + +/** + * MMD関連ファイルのパース異常系。 + * 必要に応じて、パースに失敗した位置を保持する。 + */ +@SuppressWarnings("serial") +public class MmdFormatException extends Exception { + + private final long position; + + /** + * コンストラクタ。 + */ + public MmdFormatException(){ + this(null); + return; + } + + /** + * コンストラクタ。 + * @param message エラーメッセージ + */ + public MmdFormatException(String message){ + this(message, -1L); + return; + } + + /** + * コンストラクタ。 + * @param position 入力ソース先頭から数えたエラー位置。(バイト単位) + * 負の値を与えると、エラー位置は無効と解釈される。 + */ + public MmdFormatException(long position){ + this(null, position); + return; + } + + /** + * コンストラクタ。 + * @param message エラーメッセージ + * @param position 入力ソース先頭から数えたエラー位置。(バイト単位) + * 負の値を与えると、エラー位置は無効と解釈される。 + */ + public MmdFormatException(String message, long position){ + super(message); + this.position = position; + return; + } + + /** + * {@inheritDoc} + * 有効なエラー発生位置を保持している場合、追加出力される。 + * @return {@inheritDoc} + */ + @Override + public String getMessage(){ + StringBuilder result = new StringBuilder(); + + String msg = super.getMessage(); + if(msg != null) result.append(msg); + + if(hasPosition()){ + result.append('(') + .append("position:") + .append(this.position) + .append(')'); + } + + if(result.length() <= 0) return null; + + return result.toString(); + } + + /** + * エラー位置を取得する。 + * @return 入力ソース先頭からのバイト数で表されるエラー位置。 + * 負なら無効なエラー位置。 + */ + public long getPosition(){ + return this.position; + } + + /** + * 有効なエラー位置が設定されているか判定する。 + * @return エラー位置が有効(非負)ならtrue + */ + public boolean hasPosition(){ + if(this.position < 0L) return false; + return true; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/MmdSource.java b/src/main/java/jp/sourceforge/mikutoga/parser/MmdSource.java new file mode 100644 index 0000000..051660f --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/MmdSource.java @@ -0,0 +1,328 @@ +/* + * MMD file input source + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser; + +import java.io.Closeable; +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +/** + * MMD各種ファイルの入力ソース。 + * 入力ソース終端の判定が可能。 + * パースエラー発生位置(バイト単位)の取得が可能。 + * リトルエンディアン形式で格納された各種プリミティブ型値の解決を行う。 + */ +public class MmdSource implements Closeable { + + private static final int BYTES_SHORT = Short .SIZE / Byte.SIZE; + private static final int BYTES_INT = Integer.SIZE / Byte.SIZE; + private static final int BYTES_FLOAT = Float .SIZE / Byte.SIZE; + private static final int BUF_SZ = 4; + + private static final int MASK_8BIT = 0xff; + private static final int MASK_16BIT = 0xffff; + + static{ + assert BUF_SZ >= BYTES_SHORT; + assert BUF_SZ >= BYTES_INT; + assert BUF_SZ >= BYTES_FLOAT; + } + + private final PushbackInputStream istream; + private final byte[] readArray; // 読み込みバッファ + private final ByteBuffer readBuffer; // 読み込みバッファの別ビュー + private long position; // 読み込み位置 + + /** + * コンストラクタ。 + * @param is 入力ストリーム。 + * I/O効率が考慮されたバッファリングを行うストリームを渡すのが望ましい。 + * @throws NullPointerException ストリーム引数がnull。 + */ + public MmdSource(InputStream is) + throws NullPointerException { + super(); + + if(is == null) throw new NullPointerException(); + + // 読み戻しバッファは1byte確保 + this.istream = new PushbackInputStream(is); + + this.readArray = new byte[BUF_SZ]; + this.readBuffer = ByteBuffer.wrap(this.readArray); + this.readBuffer.order(ByteOrder.LITTLE_ENDIAN); + this.readBuffer.clear(); + + this.position = 0L; + + return; + } + + /** + * 今までに読み込みに成功したバイト数を返す。 + * @return 読み込みに成功したバイト数。 + */ + public long getPosition(){ + return this.position; + } + + /** + * 入力ソースを読み飛ばす。 + * 入力ソースがディスクファイルに由来する場合、 + * 空読みするより早くなるかも。 + * @param skipLength 読み飛ばすバイト数。 + * @return 実際に読み飛ばしたバイト数。 + * @throws IOException IOエラー + * @see java.io.InputStream#skip(long) + */ + public long skip(long skipLength) + throws IOException{ + if(skipLength <= 0L) return 0L; + + long remain = skipLength; + while(remain > 0L){ // BufferedInputStream対策 + long result = this.istream.skip(remain); + if(result <= 0L) break; + this.position += result; + remain -= result; + } + + return skipLength - remain; + } + + /** + * 入力ソースにまだデータが残っているか判定する。 + * @return まだ読み込んでいないデータが残っていればtrue + * @throws IOException IOエラー + */ + public boolean hasMore() throws IOException{ + int bData = this.istream.read(); + if(bData < 0){ + return false; + } + + this.istream.unread(bData); + + return true; + } + + /** + * 入力ソースを閉じる。 + * 読み込み済みバイト数の情報は保持される。 + * @throws IOException IOエラー + * @see java.io.InputStream#close() + */ + public void close() throws IOException{ + this.istream.close(); + this.readBuffer.clear(); + return; + } + + /** + * 指定したバイト数だけ内部バッファに読み込む。 + * @param fillSize 読み込むバイト数 + * @throws IOException IOエラー + * @throws IndexOutOfBoundsException 引数がバッファサイズと矛盾。 + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + protected void fillBuffer(int fillSize) + throws IOException, IndexOutOfBoundsException, MmdEofException{ + int result = this.istream.read(this.readArray, 0, fillSize); + if(result >= 0){ + this.position += result; + } + + if(result != fillSize){ + throw new MmdEofException(this.position); + } + + this.readBuffer.rewind(); + + return; + } + + /** + * byte値を読み込む。 + * @return 読み込んだbyte値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public byte parseByte() throws IOException, MmdEofException{ + int bData = this.istream.read(); + if(bData < 0){ + throw new MmdEofException(this.position); + }else{ + this.position++; + } + + byte result = (byte) bData; + return result; + } + + /** + * 符号無し値としてbyte値を読み込み、int型に変換して返す。 + * 符号は拡張されない。(0xffは0x000000ffとなる) + * @return 読み込まれた値のint値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public int parseUByteAsInteger() + throws IOException, MmdEofException{ + return ((int) parseByte()) & MASK_8BIT; + } + + /** + * byte値を読み込み、boolean型に変換して返す。 + * 0x00は偽、それ以外は真と解釈される。 + * @return 読み込まれた値のboolean値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public boolean parseBoolean() throws IOException, MmdEofException{ + byte result = parseByte(); + if(result == 0x00) return false; + return true; + } + + /** + * short値を読み込む。 + * short値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込んだshort値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public short parseShort() throws IOException, MmdEofException{ + fillBuffer(BYTES_SHORT); + short result = this.readBuffer.getShort(); + return result; + } + + /** + * 符号無し値としてshort値を読み込み、int型に変換して返す。 + * 符号は拡張されない。(0xffffは0x0000ffffとなる) + * short値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込まれた値のint値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public int parseUShortAsInteger() + throws IOException, MmdEofException{ + return ((int) parseShort()) & MASK_16BIT; + } + + /** + * int値を読み込む。 + * int値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込んだint値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public int parseInteger() throws IOException, MmdEofException{ + fillBuffer(BYTES_INT); + int result = this.readBuffer.getInt(); + return result; + } + + /** + * float値を読み込む。 + * float値はリトルエンディアンで格納されていると仮定される。 + * @return 読み込んだfloat値 + * @throws IOException IOエラー + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public float parseFloat() throws IOException, MmdEofException{ + fillBuffer(BYTES_FLOAT); + float result = this.readBuffer.getFloat(); + return result; + } + + /** + * byte配列を読み込む。 + * @param dst 格納先配列 + * @param offset 読み込み開始オフセット + * @param length 読み込みバイト数 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws IndexOutOfBoundsException 引数が配列属性と矛盾 + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + * @see java.io.InputStream#read(byte[], int, int) + */ + public void parseByteArray(byte[] dst, int offset, int length) + throws IOException, + NullPointerException, + IndexOutOfBoundsException, + MmdEofException { + int result = this.istream.read(dst, offset, length); + if(result >= 0){ + this.position += result; + } + + if(result != length){ + throw new MmdEofException(this.position); + } + + return; + } + + /** + * byte配列を読み込む。 + * @param dst 格納先配列 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public void parseByteArray(byte[] dst) + throws IOException, NullPointerException, MmdEofException{ + parseByteArray(dst, 0, dst.length); + return; + } + + /** + * float配列を読み込む。 + * @param dst 格納先配列 + * @param offset 読み込み開始オフセット + * @param length 読み込みfloat要素数 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws IndexOutOfBoundsException 引数が配列属性と矛盾 + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public void parseFloatArray(float[] dst, int offset, int length) + throws IOException, + NullPointerException, + IndexOutOfBoundsException, + MmdEofException { + if(offset < 0 || length < 0 || dst.length - offset < length){ + throw new IndexOutOfBoundsException(); + } + + for(int idx = 0; idx < length; idx++){ + dst[offset+idx] = parseFloat(); + } + + return; + } + + /** + * float配列を読み込む。 + * @param dst 格納先配列 + * @throws IOException IOエラー + * @throws NullPointerException 配列がnull + * @throws MmdEofException 読み込む途中でストリーム終端に達した。 + */ + public void parseFloatArray(float[] dst) + throws IOException, NullPointerException, MmdEofException{ + parseFloatArray(dst, 0, dst.length); + return; + } + + // TODO ビッグエンディアン対応が今後必要になる状況はありうるか? +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/ParseStage.java b/src/main/java/jp/sourceforge/mikutoga/parser/ParseStage.java new file mode 100644 index 0000000..ea04c7d --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/ParseStage.java @@ -0,0 +1,47 @@ +/* + * parse-processing stage + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser; + +/** + * パース処理の進行ステージ種別を表す。 + * ループ構造の識別に用いられる。 + */ +public class ParseStage { + + private final String name; + + /** + * コンストラクタ。 + * 進行ステージ名は空文字列が指定される。 + */ + public ParseStage(){ + this(""); + return; + } + + /** + * コンストラクタ。 + * @param name 進行ステージ名 + */ + public ParseStage(String name){ + super(); + this.name = name; + return; + } + + /** + * {@inheritDoc} + * 進行ステージ名を返す。 + * @return {@inheritDoc} 進行ステージ名 + */ + @Override + public String toString(){ + return this.name; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/package-info.java b/src/main/java/jp/sourceforge/mikutoga/parser/package-info.java new file mode 100644 index 0000000..e01d5f6 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/package-info.java @@ -0,0 +1,17 @@ +/* + * package information for Javadoc + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +/** + * MMD用各種パーサ共通ライブラリ。 + *

+ * PMDモデルファイルやVMDモーションファイルなどの個別のパーサは + * 別パッケージにより提供される。 + */ + +package jp.sourceforge.mikutoga.parser; + +/* EOF */ diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBasicHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBasicHandler.java new file mode 100644 index 0000000..dccc41c --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBasicHandler.java @@ -0,0 +1,54 @@ +/* + * PMD basic information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.MmdFormatException; + +/** + * PMDモデル情報ファイルの基本情報の通知用ハンドラ。 + */ +public interface PmdBasicHandler { + + /** + * PMDファイルのパース処理開始の通知を受け取る。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdParseStart() + throws MmdFormatException; + + /** + * PMDファイルのパース処理終了の通知を受け取る。 + * @param hasMoreData 入力ソースに + * まだ読み込まれていないデータがあればtrue + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdParseEnd(boolean hasMoreData) + throws MmdFormatException; + + /** + * PMDファイルのヘッダ情報の通知を受け取る。 + * @param ver ヘッダ情報の版数。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdHeaderInfo(float ver) + throws MmdFormatException; + + /** + * PMDファイルのモデル基本情報の通知を受け取る。 + * @param modelName モデル名 + * @param description モデルの説明文。改行CRLFは"\n"に変換される。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdModelInfo(String modelName, String description) + throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBoneHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBoneHandler.java new file mode 100644 index 0000000..5cbdf38 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdBoneHandler.java @@ -0,0 +1,137 @@ +/* + * PMD bone information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルの各種ボーン情報の通知用ハンドラ。 + * ボーン定義の出現順と、0から始まるボーンIDは対応する。 + */ +public interface PmdBoneHandler extends LoopHandler { + + /** + * ボーン情報パースステージ。 + */ + class PmdBoneStage extends ParseStage{ + /** コンストラクタ。 */ + PmdBoneStage(){ super(); return; } + } + + /** ボーン定義抽出ループ。 */ + PmdBoneStage BONE_LIST = new PmdBoneStage(); + + /** IKリスト抽出ループ。 */ + PmdBoneStage IK_LIST = new PmdBoneStage(); + + /** IKチェーンリスト抽出ループ。 */ + PmdBoneStage IKCHAIN_LIST = new PmdBoneStage(); + + /** ボーングループ名抽出ループ。 */ + PmdBoneStage BONEGROUP_LIST = new PmdBoneStage(); + + /** ボーングループ内訳抽出ループ。 */ + PmdBoneStage GROUPEDBONE_LIST = new PmdBoneStage(); + + /** + * ボーン定義情報の通知を受け取る。 + * {@link #BONE_LIST}ループの構成要素。 + * @param boneName ボーン名 + * @param boneKind ボーン種別。 + *

    + *
  • 0:回転 + *
  • 1:回転/移動 + *
  • 2:IK + *
  • 3:不明 + *
  • 4:IK影響下(回転) + *
  • 5:回転影響下 + *
  • 6:IK接続先 + *
  • 7:非表示 + *
  • 8:捩り + *
  • 9:回転連動 + *
+ * ※8,9はMMD4.0から? + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdBoneInfo(String boneName, byte boneKind) + throws MmdFormatException; + + /** + * ボーン間接続情報の通知を受け取る。 + * {@link #BONE_LIST}ループの構成要素。 + * @param parentId 親(前)ボーンID。無い場合は0xffff。 + * @param tailId 子(次)ボーンID。末端の場合は0。 + * 捩りボーンの場合は軸方向のボーンID、 + * 回転連動ボーンの場合は影響元ボーンID + * @param ikId 影響IKボーンID。未指定の場合は0。 + * ※回転連動では影響度(0-100)、負や100以上もOK! + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdBoneLink(int parentId, int tailId, int ikId) + throws MmdFormatException; + + /** + * ボーン位置情報の通知を受け取る。 + * {@link #BONE_LIST}ループの構成要素。 + * @param xPos X座標 + * @param yPos Y座標 + * @param zPos Z座標 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdBonePosition(float xPos, float yPos, float zPos) + throws MmdFormatException; + + /** + * IKボーン情報の通知を受け取る。 + * {@link #IK_LIST}ループの構成要素。 + * @param boneId IKボーンID + * @param targetId IKボーンが最初に接続するIK接続先ボーンID + * @param depth 再帰演算の深さ + * @param weight 制限角度強度 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdIKInfo(int boneId, int targetId, int depth, float weight) + throws MmdFormatException; + + /** + * IKチェイン要素の通知を受け取る。 + * {@link #IK_LIST}ループの下位{@link #IKCHAIN_LIST}ループの構成要素。 + * @param childId IK影響下ボーンID + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdIKChainInfo(int childId) + throws MmdFormatException; + + /** + * ボーングループ名定義の通知を受け取る。 + * {@link #BONEGROUP_LIST}ループの構成要素。 + * @param groupName ボーングループ名。末尾のLF(0x0a)は削除される。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdBoneGroupInfo(String groupName) throws MmdFormatException; + + /** + * ボーングループ内訳の通知を受け取る。 + * {@link #GROUPEDBONE_LIST}ループの構成要素。 + * @param boneId グループに所属するボーンのID + * @param groupId ボーンが所属するボーングループIDに1を足した数 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdGroupedBoneInfo(int boneId, int groupId) + throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdEngHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdEngHandler.java new file mode 100644 index 0000000..0a26bc9 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdEngHandler.java @@ -0,0 +1,82 @@ +/* + * PMD english information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルの英語情報の通知用ハンドラ。 + */ +public interface PmdEngHandler extends LoopHandler { + + /** + * 英語情報パースステージ。 + */ + class PmdEngStage extends ParseStage{ + /** コンストラクタ。 */ + PmdEngStage(){ super(); return; } + } + + /** ボーン英語名抽出ループ。 */ + ParseStage ENGBONE_LIST = new PmdEngStage(); + + /** モーフ英語名抽出ループ。 */ + ParseStage ENGMORPH_LIST = new PmdEngStage(); + + /** ボーングループ英語名抽出グループ。 */ + ParseStage ENGBONEGROUP_LIST = new PmdEngStage(); + + /** + * PMD英語情報の有無の通知を受け取る。 + * @param hasEnglishInfo 英語情報が含まれればtrue + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdEngEnabled(boolean hasEnglishInfo) throws MmdFormatException; + + /** + * PMD英語基本情報の通知を受け取る。 + * @param modelName モデル名 + * @param description モデルの説明文。改行CRLFは"\n"に変換される。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdEngModelInfo(String modelName, String description) + throws MmdFormatException; + + /** + * 英語ボーン名の通知を受け取る。 + * {@link #ENGBONE_LIST}ループの構成要素 + * @param boneName 英語ボーン名 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdEngBoneInfo(String boneName) throws MmdFormatException; + + /** + * 英語モーフ名の通知を受け取る。 + * モーフ名「base」に対応する英語名は通知されない。 + * {@link #ENGMORPH_LIST}ループの構成要素 + * @param morphName 英語モーフ名 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdEngMorphInfo(String morphName) throws MmdFormatException; + + /** + * 英語ボーングループ名の通知を受け取る。 + * {@link #ENGBONEGROUP_LIST}ループの構成要素 + * @param groupName 英語ボーングループ名 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdEngBoneGroupInfo(String groupName) throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdJointHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdJointHandler.java new file mode 100644 index 0000000..70567f9 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdJointHandler.java @@ -0,0 +1,139 @@ +/* + * PMD joint information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルの各種剛体間ジョイント情報の通知用ハンドラ。 + */ +public interface PmdJointHandler extends LoopHandler { + + /** + * ジョイント情報パースステージ。 + */ + class PmdJointStage extends ParseStage{ + /** コンストラクタ。 */ + PmdJointStage(){ super(); return; } + } + + /** ジョイント情報抽出ループ。 */ + PmdJointStage JOINT_LIST = new PmdJointStage(); + + /** + * ジョイント名の通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * @param jointName ジョイント名 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdJointName(String jointName) + throws MmdFormatException; + + /** + * ジョイントが繋ぐ接続剛体IDの通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * @param rigidIdA 接続剛体AのID + * @param rigidIdB 接続剛体BのID + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdJointLink(int rigidIdA, int rigidIdB) + throws MmdFormatException; + + /** + * ジョイント位置の通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * @param posX X座標 + * @param posY Y座標 + * @param posZ Z座標 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdJointPosition(float posX, float posY, float posZ) + throws MmdFormatException; + + /** + * ジョイント回転姿勢の通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * @param radX X軸回転量(radian) + * @param radY Y軸回転量(radian) + * @param radZ Z軸回転量(radian) + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdJointRotation(float radX, float radY, float radZ) + throws MmdFormatException; + + /** + * ジョイント移動制限の通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * ※(制限端その1<=その2)条件を満たす必要はあるか? + * @param posXlim1 X座標制限端その1 + * @param posXlim2 X座標制限端その2 + * @param posYlim1 Y座標制限端その1 + * @param posYlim2 Y座標制限端その2 + * @param posZlim1 Z座標制限端その1 + * @param posZlim2 Z座標制限端その2 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdPositionLimit(float posXlim1, float posXlim2, + float posYlim1, float posYlim2, + float posZlim1, float posZlim2 ) + throws MmdFormatException; + + /** + * ジョイント回転制限の通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * ※(制限端その1<=その2)条件を満たす必要はあるか? + * @param radXlim1 X軸制限端その1(radian) + * @param radXlim2 X軸制限端その2(radian) + * @param radYlim1 Y軸制限端その1(radian) + * @param radYlim2 Y軸制限端その2(radian) + * @param radZlim1 Z軸制限端その1(radian) + * @param radZlim2 Z軸制限端その2(radian) + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdRotationLimit(float radXlim1, float radXlim2, + float radYlim1, float radYlim2, + float radZlim1, float radZlim2 ) + throws MmdFormatException; + + /** + * ジョイントのばね移動情報の通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * @param elasticPosX X座標 + * @param elasticPosY Y座標 + * @param elasticPosZ Z座標 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdElasticPosition(float elasticPosX, + float elasticPosY, + float elasticPosZ ) + throws MmdFormatException; + + /** + * ジョイントのばね回転情報の通知を受け取る。 + * {@link #JOINT_LIST}ループの構成要素。 + * @param elasticDegX X軸変量(degree) + * @param elasticDegY Y軸変量(degree) + * @param elasticDegZ Z軸変量(degree) + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示。 + */ + void pmdElasticRotation(float elasticDegX, + float elasticDegY, + float elasticDegZ ) + throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdLimits.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdLimits.java new file mode 100644 index 0000000..09daa2b --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdLimits.java @@ -0,0 +1,81 @@ +/* + * pmd limit numbers + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.CommonParser; + +/** + * PMDファイルフォーマットの各種リミット値その他定数。 + */ +public final class PmdLimits { + + /** モデル名最大長。バイト単位。 */ + public static final int MAXBYTES_MODELNAME = 20; + + /** モデル説明文最大長。バイト単位。 */ + public static final int MAXBYTES_MODELDESC = 256; + + /** ボーン名最大長。バイト単位。 */ + public static final int MAXBYTES_BONENAME = 20; + + /** モーフ名最大長。バイト単位。 */ + public static final int MAXBYTES_MORPHNAME = 20; + + /** ボーングループ名最大長。バイト単位。 */ + public static final int MAXBYTES_BONEGROUPNAME = 50; + + /** テクスチャファイル名最大長。バイト単位。 */ + public static final int MAXBYTES_TEXTUREFILENAME = 20; + + /** 独自トゥーンテクスチャファイル名の最大長。バイト単位。 */ + public static final int MAXBYTES_TOONFILENAME = 100; + + /** 剛体名最大長。バイト単位。 */ + public static final int MAXBYTES_RIGIDNAME = 20; + + /** ジョイント名最大長。バイト単位。 */ + public static final int MAXBYTES_JOINTNAME = 20; + + + /** + * ボーン最大数。 + * (Id : 0 - 0xfffe) + * MMDがいくつまで受け入れるかはまた別の話だよ。 + */ + public static final int MAX_BONE = 65535; + + /** 剛体衝突グループ総数。 */ + public static final int RIGIDGROUP_FIXEDNUM = 16; + + /** 独自トゥーンテクスチャファイル名テーブルの固定数。 */ + public static final int TOON_FIXEDNUM = 10; + + + static{ + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_MODELNAME; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_MODELDESC; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_BONENAME; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_MORPHNAME; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_BONEGROUPNAME; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_TEXTUREFILENAME; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_TOONFILENAME; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_RIGIDNAME; + assert CommonParser.TEXTBUF_SZ >= MAXBYTES_JOINTNAME; + } + + + /** + * 隠しコンストラクタ。 + */ + private PmdLimits(){ + super(); + assert false; + throw new AssertionError(); + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMaterialHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMaterialHandler.java new file mode 100644 index 0000000..f0d51f7 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMaterialHandler.java @@ -0,0 +1,100 @@ +/* + * PMD material information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルの各種材質情報の通知用ハンドラ。 + * 色空間はsRGB? + */ +public interface PmdMaterialHandler extends LoopHandler { + + /** + * モデル材質パースステージ。 + */ + class PmdMaterialStage extends ParseStage{ + /** コンストラクタ。 */ + PmdMaterialStage(){ super(); return; } + } + + /** 材質抽出ループ。 */ + PmdMaterialStage MATERIAL_LIST = new PmdMaterialStage(); + + /** + * 材質の拡散光成分の通知を受け取る。 + * {@link #MATERIAL_LIST}ループの構成要素。 + * @param red 0.0~1.0の範囲の赤成分 + * @param green 0.0~1.0の範囲の緑成分 + * @param blue 0.0~1.0の範囲の青成分 + * @param alpha 0.0(透明)~1.0(不透明)のアルファ値。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMaterialDiffuse(float red, float green, float blue, + float alpha ) + throws MmdFormatException; + + /** + * 材質の反射光成分の通知を受け取る。 + * {@link #MATERIAL_LIST}ループの構成要素。 + * @param red 0.0~1.0の範囲の赤成分 + * @param green 0.0~1.0の範囲の緑成分 + * @param blue 0.0~1.0の範囲の青成分 + * @param shininess 光沢強度(1~15ぐらい) + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMaterialSpecular(float red, float green, float blue, + float shininess) + throws MmdFormatException; + + /** + * 材質の環境色成分の通知を受け取る。 + * {@link #MATERIAL_LIST}ループの構成要素。 + * @param red 0.0~1.0の範囲の赤成分 + * @param green 0.0~1.0の範囲の緑成分 + * @param blue 0.0~1.0の範囲の青成分 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMaterialAmbient(float red, float green, float blue) + throws MmdFormatException; + + /** + * シェーディング情報の通知を受け取る。 + * {@link #MATERIAL_LIST}ループの構成要素。 + * @param toonIdx トゥーンファイル番号。 + * 0ならtoon01.bmp。9ならtoon10.bmp。0xffならtoon0.bmp。 + * @param textureFile テクスチャファイル名。 + * 無ければ空文字。 + * @param sphereFile スフィアマップファイル名。 + * 無ければ空文字。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMaterialShading(int toonIdx, + String textureFile, String sphereFile ) + throws MmdFormatException; + + /** + * 材質情報の通知を受け取る。 + * {@link #MATERIAL_LIST}ループの構成要素。 + * @param hasEdge エッジを表示するならtrue + * @param vertexNum 面頂点数。 + * 3の倍数のはず。 + * 3で割ると積算で表される面IDの範囲を表す。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMaterialInfo(boolean hasEdge, int vertexNum) + throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMorphHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMorphHandler.java new file mode 100644 index 0000000..eb36cd3 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdMorphHandler.java @@ -0,0 +1,77 @@ +/* + * PMD morph information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルのモーフ情報の通知用ハンドラ。 + */ +public interface PmdMorphHandler extends LoopHandler { + + /** + * モーフ情報パースステージ。 + */ + class PmdMorphStage extends ParseStage{ + /** コンストラクタ。 */ + PmdMorphStage(){ super(); return; } + } + + /** モーフ抽出ループ。 */ + PmdMorphStage MORPH_LIST = new PmdMorphStage(); + + /** モーフ頂点抽出ループ。 */ + PmdMorphStage MORPHVERTEX_LIST = new PmdMorphStage(); + + /** モーフ出現順抽出ループ。 */ + PmdMorphStage MORPHORDER_LIST = new PmdMorphStage(); + + /** + * モーフ情報の通知を受け取る。 + * {@link #MORPH_LIST}ループの構成要素 + * @param morphName モーフ名 + * @param morphType モーフ種別。 + *
    + *
  • 0:base + *
  • 1:まゆ + *
  • 2:目 + *
  • 3:リップ + *
  • 4:その他 + *
+ * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMorphInfo(String morphName, byte morphType) + throws MmdFormatException; + + /** + * モーフ形状の通知を受け取る。 + * {@link #MORPH_LIST}ループの下位{@link #MORPHVERTEX_LIST}の構成要素 + * @param serialId base型の場合は頂点ID、それ以外はモーフ頂点ID + * @param xPos base型の場合はX座標、それ以外はX軸変位 + * @param yPos base型の場合はY座標、それ以外はY軸変位 + * @param zPos base型の場合はZ座標、それ以外はZ軸変位 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMorphVertexInfo(int serialId, + float xPos, float yPos, float zPos) + throws MmdFormatException; + + /** + * 各モーフ種別内のGUI表示順の通知を受け取る。 + * {@link #MORPHORDER_LIST}ループの構成要素 + * @param morphId モーフ通し番号。同一種別内の大小関係のみ意味がある。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdMorphOrderInfo(int morphId) throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParser.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParser.java new file mode 100644 index 0000000..ca77571 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParser.java @@ -0,0 +1,29 @@ +/* + * pmd file parser (up to date) + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.MmdSource; + +/** + * PMDモデルファイルのパーサ最新版。 + * 将来のリリースにおいて、 + * 常に最新のPMDモデルファイルフォーマットに対応したパーサの + * 別名であることが保証される。つもり。 + */ +public class PmdParser extends PmdParserExt3{ + + /** + * コンストラクタ。 + * @param source 入力ソース + */ + public PmdParser(MmdSource source){ + super(source); + return; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserBase.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserBase.java new file mode 100644 index 0000000..8a45f6b --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserBase.java @@ -0,0 +1,642 @@ +/* + * pmd file parser + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import java.io.IOException; +import jp.sourceforge.mikutoga.parser.CommonParser; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.MmdSource; + +/** + * PMDモデルファイルのパーサ基本部。 + */ +public class PmdParserBase extends CommonParser { + + /** 改行文字列 CR。 */ + protected static final String CR = "\r"; // 0x0d + /** 改行文字列 LF。 */ + protected static final String LF = "\n"; // 0x0a + /** 改行文字列 CRLF。 */ + protected static final String CRLF = CR + LF; // 0x0d, 0x0a + + private static final String MAGIC = "Pmd"; + private static final int MAGIC_SZ = MAGIC.getBytes(CS_WIN31J).length; + + private static final int VERTEX_DATA_SZ = 38; + private static final int SURFACE_DATA_SZ = 6; + private static final int MATERIAL_DATA_SZ = 70; + private static final int BONE_DATA_SZ = 39; + private static final int MORPHVERTEX_DATA_SZ = 16; + private static final int MORPHORDER_DATA_SZ = 2; + private static final int BONEGROUP_DATA_SZ = 50; + private static final int GROUPEDBONE_DATA_SZ = 3; + + + private PmdBasicHandler basicHandler = null; + private PmdShapeHandler shapeHandler = null; + private PmdMaterialHandler materialHandler = null; + private PmdBoneHandler boneHandler = null; + private PmdMorphHandler morphHandler = null; + + private int boneCount = -1; + private int morphCount = -1; + private int boneGroupCount = -1; + + + /** + * コンストラクタ。 + * @param source 入力ソース + */ + public PmdParserBase(MmdSource source){ + super(source); + return; + } + + /** + * 文字列の最後がLF(0x0a)の場合削除する。 + * @param name 文字列 + * @return 末尾LFが削除された文字列 + */ + public static String chopLastLF(String name){ + String result; + + if(name.endsWith(LF)){ + result = name.substring(0, name.length() - 1); + }else{ + result = name; + } + + return result; + } + + /** + * シェーディング用ファイル情報から + * テクスチャファイル名とスフィアマップファイル名を分離する。 + * @param shadingFile シェーディング用ファイル情報 + * @return [0]:テクスチャファイル名 [1]:スフィアマップファイル名。 + * 該当ファイル名が無い場合は空文字列。 + */ + public static String[] splitShadingFileInfo(String shadingFile){ + String[] result; + + result = shadingFile.split('\\'+"*", 2); + assert result.length == 1 || result.length == 2; + + if(result.length == 1){ + String onlyFile = result[0]; + result = new String[2]; + result[0] = ""; + result[1] = ""; + if(onlyFile.endsWith(".sph") || onlyFile.endsWith(".spa")){ + result[1] = onlyFile; + }else{ + result[0] = onlyFile; + } + } + + assert result.length == 2; + + return result; + } + + /** + * 基本情報通知ハンドラを登録する。 + * @param handler ハンドラ + */ + public void setBasicHandler(PmdBasicHandler handler){ + this.basicHandler = handler; + return; + } + + /** + * 形状情報通知ハンドラを登録する。 + * @param handler ハンドラ + */ + public void setShapeHandler(PmdShapeHandler handler){ + this.shapeHandler = handler; + return; + } + + /** + * 材質情報通知ハンドラを登録する。 + * @param handler ハンドラ + */ + public void setMaterialHandler(PmdMaterialHandler handler){ + this.materialHandler = handler; + return; + } + + /** + * ボーン情報通知ハンドラを登録する。 + * @param handler ハンドラ + */ + public void setBoneHandler(PmdBoneHandler handler){ + this.boneHandler = handler; + return; + } + + /** + * モーフ情報通知ハンドラを登録する。 + * @param handler ハンドラ + */ + public void setMorphHandler(PmdMorphHandler handler){ + this.morphHandler = handler; + return; + } + + /** + * パースによって得られたボーン数を返す。 + * @return ボーン数 + */ + protected int getBoneCount(){ + return this.boneCount; + } + + /** + * パースによって得られたモーフ数を返す。 + * @return モーフ数 + */ + protected int getMorphCount(){ + return this.morphCount; + } + + /** + * パースによって得られたボーングループ数を返す。 + * @return ボーングループ数 + */ + protected int getBoneGroupCount(){ + return this.boneGroupCount; + } + + /** + * PMDファイルのパースを開始する。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + public void parsePmd() + throws IOException, MmdFormatException { + if(this.basicHandler != null){ + this.basicHandler.pmdParseStart(); + } + + parseBody(); + + boolean hasMoreData = hasMore(); + if(this.basicHandler != null){ + this.basicHandler.pmdParseEnd(hasMoreData); + } + + return; + } + + /** + * PMDファイル本体のパースを開始する。 + * パーサを拡張する場合はこのメソッドをオーバーライドする。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + protected void parseBody() throws IOException, MmdFormatException{ + parsePmdHeader(); + + parseVertexList(); + parseSurfaceList(); + parseMaterialList(); + parseBoneList(); + parseIKList(); + parseMorphList(); + parseMorphOrderList(); + parseBoneGroupList(); + parseGroupedBoneList(); + + return; + } + + /** + * PMDファイルヘッダ部のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parsePmdHeader() throws IOException, MmdFormatException{ + String magic = parseZeroTermString(MAGIC_SZ); + if( ! magic.equals(MAGIC) ){ + throw new MmdFormatException("unrecognized magic data"); + } + + float ver = parseFloat(); + String modelName = + parseZeroTermString(PmdLimits.MAXBYTES_MODELNAME); + String description = + parseZeroTermString(PmdLimits.MAXBYTES_MODELDESC); + description = description.replace(CRLF, LF); + + if(this.basicHandler != null){ + this.basicHandler.pmdHeaderInfo(ver); + this.basicHandler.pmdModelInfo(modelName, description); + } + + return; + } + + /** + * 頂点情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseVertexList() throws IOException, MmdFormatException{ + int vertexNum = parseInteger(); + + if(this.shapeHandler == null){ + skip(VERTEX_DATA_SZ * vertexNum); + return; + } + + this.shapeHandler.loopStart(PmdShapeHandler.VERTEX_LIST, vertexNum); + + for(int ct = 0; ct < vertexNum; ct++){ + float xPos = parseFloat(); + float yPos = parseFloat(); + float zPos = parseFloat(); + this.shapeHandler.pmdVertexPosition(xPos, yPos, zPos); + + float xVec = parseFloat(); + float yVec = parseFloat(); + float zVec = parseFloat(); + this.shapeHandler.pmdVertexNormal(xVec, yVec, zVec); + + float uVal = parseFloat(); + float vVal = parseFloat(); + this.shapeHandler.pmdVertexUV(uVal, vVal); + + int boneId1 = parseUShortAsInteger(); + int boneId2 = parseUShortAsInteger(); + int weightForB1 = parseUByteAsInteger(); + this.shapeHandler.pmdVertexWeight(boneId1, boneId2, weightForB1); + + boolean hideEdge = parseBoolean(); + this.shapeHandler.pmdVertexEdge(hideEdge); + + this.shapeHandler.loopNext(PmdShapeHandler.VERTEX_LIST); + } + + this.shapeHandler.loopEnd(PmdShapeHandler.VERTEX_LIST); + + return; + } + + /** + * 面情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseSurfaceList() throws IOException, MmdFormatException{ + int vertexNum = parseInteger(); + if(vertexNum % 3 != 0) throw new MmdFormatException(); + int surfaceNum = vertexNum / 3; + + if(this.shapeHandler == null){ + skip(SURFACE_DATA_SZ * surfaceNum); + return; + } + + this.shapeHandler.loopStart(PmdShapeHandler.SURFACE_LIST, surfaceNum); + + for(int ct = 0; ct < surfaceNum; ct++){ + int vertexId1 = parseUShortAsInteger(); + int vertexId2 = parseUShortAsInteger(); + int vertexId3 = parseUShortAsInteger(); + this.shapeHandler.pmdSurfaceTriangle(vertexId1, + vertexId2, + vertexId3 ); + this.shapeHandler.loopNext(PmdShapeHandler.SURFACE_LIST); + } + + this.shapeHandler.loopEnd(PmdShapeHandler.SURFACE_LIST); + + return; + } + + /** + * 材質情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseMaterialList() throws IOException, MmdFormatException{ + int materialNum = parseInteger(); + + if(this.materialHandler == null){ + skip(MATERIAL_DATA_SZ * materialNum); + return; + } + + this.materialHandler.loopStart(PmdMaterialHandler.MATERIAL_LIST, + materialNum ); + + for(int ct = 0; ct < materialNum; ct++){ + float red; + float green; + float blue; + + red = parseFloat(); + green = parseFloat(); + blue = parseFloat(); + float alpha = parseFloat(); + this.materialHandler.pmdMaterialDiffuse(red, green, blue, alpha); + + float shininess = parseFloat(); + red = parseFloat(); + green = parseFloat(); + blue = parseFloat(); + this.materialHandler.pmdMaterialSpecular(red, green, blue, + shininess); + + red = parseFloat(); + green = parseFloat(); + blue = parseFloat(); + this.materialHandler.pmdMaterialAmbient(red, green, blue); + + int toonidx = parseUByteAsInteger(); + boolean hasEdge = parseBoolean(); + int surfaceCount = parseInteger(); + String shadingFile = + parseZeroTermString(PmdLimits.MAXBYTES_TEXTUREFILENAME); + String[] splitted = splitShadingFileInfo(shadingFile); + String textureFile = splitted[0]; + String sphereFile = splitted[1]; + + this.materialHandler.pmdMaterialShading(toonidx, + textureFile, sphereFile ); + this.materialHandler.pmdMaterialInfo(hasEdge, surfaceCount); + + this.materialHandler.loopNext(PmdMaterialHandler.MATERIAL_LIST); + } + + this.materialHandler.loopEnd(PmdMaterialHandler.MATERIAL_LIST); + + return; + } + + /** + * ボーン情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseBoneList() throws IOException, MmdFormatException{ + this.boneCount = parseUShortAsInteger(); + + if(this.boneHandler == null){ + skip(BONE_DATA_SZ * this.boneCount); + return; + } + + this.boneHandler.loopStart(PmdBoneHandler.BONE_LIST, this.boneCount); + + for(int ct = 0; ct < this.boneCount; ct++){ + String boneName = + parseZeroTermString(PmdLimits.MAXBYTES_BONENAME); + int parentId = parseUShortAsInteger(); + int tailId = parseUShortAsInteger(); + byte boneKind = parseByte(); + int ikId = parseUShortAsInteger(); + + this.boneHandler.pmdBoneInfo(boneName, boneKind); + this.boneHandler.pmdBoneLink(parentId, tailId, ikId); + + float xPos = parseFloat(); + float yPos = parseFloat(); + float zPos = parseFloat(); + + this.boneHandler.pmdBonePosition(xPos, yPos, zPos); + + this.boneHandler.loopNext(PmdBoneHandler.BONE_LIST); + } + + this.boneHandler.loopEnd(PmdBoneHandler.BONE_LIST); + + return; + } + + /** + * IKリスト情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseIKList() throws IOException, MmdFormatException{ + int ikCount = parseUShortAsInteger(); + + if(this.boneHandler != null){ + this.boneHandler.loopStart(PmdBoneHandler.IK_LIST, ikCount); + } + + for(int ct = 0; ct < ikCount; ct++){ + int boneId = parseUShortAsInteger(); + int targetId = parseUShortAsInteger(); + int chainLength = parseUByteAsInteger(); + int depth = parseUShortAsInteger(); + float weight = parseFloat(); + + parseIKChainList(chainLength); + + if(this.boneHandler != null){ + this.boneHandler.pmdIKInfo(boneId, targetId, depth, weight); + this.boneHandler.loopNext(PmdBoneHandler.IK_LIST); + } + } + + if(this.boneHandler != null){ + this.boneHandler.loopEnd(PmdBoneHandler.IK_LIST); + } + + return; + } + + /** + * IKチェーン情報のパースと通知。 + * @param chainLength チェーン長 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseIKChainList(int chainLength) + throws IOException, MmdFormatException{ + if(this.boneHandler != null){ + this.boneHandler.loopStart(PmdBoneHandler.IKCHAIN_LIST, + chainLength); + } + + for(int ct = 0; ct < chainLength; ct++){ + int childId = parseUShortAsInteger(); + if(this.boneHandler != null){ + this.boneHandler.pmdIKChainInfo(childId); + this.boneHandler.loopNext(PmdBoneHandler.IKCHAIN_LIST); + } + } + + if(this.boneHandler != null){ + this.boneHandler.loopEnd(PmdBoneHandler.IKCHAIN_LIST); + } + + return; + } + + /** + * モーフ情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseMorphList() throws IOException, MmdFormatException{ + this.morphCount = parseUShortAsInteger(); + + if(this.morphHandler != null){ + this.morphHandler.loopStart(PmdMorphHandler.MORPH_LIST, + this.morphCount ); + } + + for(int ct = 0; ct < this.morphCount; ct++){ + String morphName = + parseZeroTermString(PmdLimits.MAXBYTES_MORPHNAME); + int vertexCount = parseInteger(); + byte morphType = parseByte(); + + if(this.morphHandler != null){ + this.morphHandler.pmdMorphInfo(morphName, morphType); + } + + parseMorphVertexList(vertexCount); + + if(this.morphHandler != null){ + this.morphHandler.loopNext(PmdMorphHandler.MORPH_LIST); + } + } + + if(this.morphHandler != null){ + this.morphHandler.loopEnd(PmdMorphHandler.MORPH_LIST); + } + + return; + } + + /** + * モーフ形状のパースと通知。 + * @param vertexCount 頂点数 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseMorphVertexList(int vertexCount) + throws IOException, MmdFormatException{ + if(this.morphHandler == null){ + skip(MORPHVERTEX_DATA_SZ * vertexCount); + return; + } + + this.morphHandler.loopStart(PmdMorphHandler.MORPHVERTEX_LIST, + vertexCount ); + + for(int ct = 0; ct < vertexCount; ct++){ + int vertexId = parseInteger(); + float xPos = parseFloat(); + float yPos = parseFloat(); + float zPos = parseFloat(); + this.morphHandler.pmdMorphVertexInfo(vertexId, xPos, yPos, zPos); + this.morphHandler.loopNext(PmdMorphHandler.MORPHVERTEX_LIST); + } + + this.morphHandler.loopEnd(PmdMorphHandler.MORPHVERTEX_LIST); + + return; + } + + /** + * モーフGUI表示順のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseMorphOrderList() + throws IOException, MmdFormatException{ + int morphOrderCount = parseUByteAsInteger(); + + if(this.morphHandler == null){ + skip(MORPHORDER_DATA_SZ * morphOrderCount); + return; + } + + this.morphHandler.loopStart(PmdMorphHandler.MORPHORDER_LIST, + morphOrderCount ); + + for(int ct = 0; ct < morphOrderCount; ct++){ + int morphId = parseUShortAsInteger(); + this.morphHandler.pmdMorphOrderInfo(morphId); + + this.morphHandler.loopNext(PmdMorphHandler.MORPHORDER_LIST); + } + + this.morphHandler.loopEnd(PmdMorphHandler.MORPHORDER_LIST); + + return; + } + + /** + * ボーングループ名のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseBoneGroupList() + throws IOException, MmdFormatException{ + this.boneGroupCount = parseUByteAsInteger(); + + if(this.boneHandler == null){ + skip(BONEGROUP_DATA_SZ * this.boneGroupCount); + return; + } + + this.boneHandler.loopStart(PmdBoneHandler.BONEGROUP_LIST, + this.boneGroupCount); + + for(int ct = 0; ct < this.boneGroupCount; ct++){ + String groupName = + parseZeroTermString(PmdLimits.MAXBYTES_BONEGROUPNAME); + groupName = chopLastLF(groupName); + this.boneHandler.pmdBoneGroupInfo(groupName); + + this.boneHandler.loopNext(PmdBoneHandler.BONEGROUP_LIST); + } + + this.boneHandler.loopEnd(PmdBoneHandler.BONEGROUP_LIST); + + return; + } + + /** + * ボーングループ内訳のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseGroupedBoneList() + throws IOException, MmdFormatException{ + int groupedBoneCount = parseInteger(); + + if(this.boneHandler == null){ + skip(GROUPEDBONE_DATA_SZ * groupedBoneCount); + return; + } + + this.boneHandler.loopStart(PmdBoneHandler.GROUPEDBONE_LIST, + groupedBoneCount); + + for(int ct = 0; ct < groupedBoneCount; ct++){ + int boneId = parseUShortAsInteger(); + int groupId = parseUByteAsInteger(); + this.boneHandler.pmdGroupedBoneInfo(boneId, groupId); + + this.boneHandler.loopNext(PmdBoneHandler.GROUPEDBONE_LIST); + } + + this.boneHandler.loopEnd(PmdBoneHandler.GROUPEDBONE_LIST); + + return; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt1.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt1.java new file mode 100644 index 0000000..cffe8d9 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt1.java @@ -0,0 +1,177 @@ +/* + * pmd parser extension 1 + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import java.io.IOException; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.MmdSource; + +/** + * PMDモデルファイルのパーサ拡張その1。 + * ※英名対応 + */ +public class PmdParserExt1 extends PmdParserBase { + + private PmdEngHandler engHandler = null; + private boolean hasEnglishInfo = true; + + /** + * コンストラクタ。 + * @param source 入力ソース + */ + public PmdParserExt1(MmdSource source){ + super(source); + return; + } + + /** + * 英語ハンドラを登録する。 + * @param handler ハンドラ + */ + public void setEngHandler(PmdEngHandler handler){ + this.engHandler = handler; + return; + } + + /** + * {@inheritDoc} + * @throws IOException {@inheritDoc} + * @throws MmdFormatException {@inheritDoc} + */ + @Override + protected void parseBody() + throws IOException, MmdFormatException{ + super.parseBody(); + + if(hasMore()){ + parseEngHeader(); + if(this.hasEnglishInfo){ + parseEngBoneList(); + parseEngMorphName(); + parseEngBoneGroupName(); + } + } + + return; + } + + /** + * PMDモデル英語基本情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseEngHeader() + throws IOException, MmdFormatException{ + this.hasEnglishInfo = parseBoolean(); + + if(this.engHandler != null){ + this.engHandler.pmdEngEnabled(this.hasEnglishInfo); + } + if( ! this.hasEnglishInfo ) return; + + String modelName = + parseZeroTermString(PmdLimits.MAXBYTES_MODELNAME); + String description = + parseZeroTermString(PmdLimits.MAXBYTES_MODELDESC); + description = description.replace(CRLF, LF); + + if(this.engHandler != null){ + this.engHandler.pmdEngModelInfo(modelName, description); + } + + return; + } + + /** + * PMDモデル英語ボーン名のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseEngBoneList() + throws IOException, MmdFormatException{ + int boneNum = getBoneCount(); + + if(this.engHandler == null){ + skip(PmdLimits.MAXBYTES_BONENAME * boneNum); + return; + } + + this.engHandler.loopStart(PmdEngHandler.ENGBONE_LIST, boneNum); + + for(int ct = 0; ct < boneNum; ct++){ + String boneName = + parseZeroTermString(PmdLimits.MAXBYTES_BONENAME); + this.engHandler.pmdEngBoneInfo(boneName); + + this.engHandler.loopNext(PmdEngHandler.ENGBONE_LIST); + } + + this.engHandler.loopEnd(PmdEngHandler.ENGBONE_LIST); + + return; + } + + /** + * PMDモデル英語モーフ名のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseEngMorphName() + throws IOException, MmdFormatException{ + int morphNum = getMorphCount() - 1; // base は英名なし + + if(this.engHandler == null){ + skip(PmdLimits.MAXBYTES_MORPHNAME * morphNum); + return; + } + + this.engHandler.loopStart(PmdEngHandler.ENGMORPH_LIST, morphNum); + + for(int ct = 0; ct < morphNum; ct++){ + String morphName = + parseZeroTermString(PmdLimits.MAXBYTES_MORPHNAME); + this.engHandler.pmdEngMorphInfo(morphName); + + this.engHandler.loopNext(PmdEngHandler.ENGMORPH_LIST); + } + + this.engHandler.loopEnd(PmdEngHandler.ENGMORPH_LIST); + + return; + } + + /** + * PMDモデル英語ボーングループ名のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseEngBoneGroupName() + throws IOException, MmdFormatException{ + int groupNum = getBoneGroupCount(); + + if(this.engHandler == null){ + skip(PmdLimits.MAXBYTES_BONEGROUPNAME * groupNum); + return; + } + + this.engHandler.loopStart(PmdEngHandler.ENGBONEGROUP_LIST, groupNum); + + for(int ct = 0; ct < groupNum; ct++){ + String boneGroupName = + parseZeroTermString(PmdLimits.MAXBYTES_BONEGROUPNAME); + this.engHandler.pmdEngBoneGroupInfo(boneGroupName); + + this.engHandler.loopNext(PmdEngHandler.ENGBONEGROUP_LIST); + } + + this.engHandler.loopEnd(PmdEngHandler.ENGBONEGROUP_LIST); + + return; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt2.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt2.java new file mode 100644 index 0000000..3088b63 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt2.java @@ -0,0 +1,84 @@ +/* + * pmd parser extensin 2 + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import java.io.IOException; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.MmdSource; + +/** + * PMDモデルファイルのパーサ拡張その2。 + * ※独自トゥーンテクスチャファイル名対応 + */ +public class PmdParserExt2 extends PmdParserExt1 { + + private PmdToonHandler toonHandler = null; + + /** + * コンストラクタ。 + * @param source 入力ソース + */ + public PmdParserExt2(MmdSource source){ + super(source); + return; + } + + /** + * トゥーンテクスチャファイルハンドラを登録する。 + * @param handler ハンドラ + */ + public void setToonHandler(PmdToonHandler handler){ + this.toonHandler = handler; + return; + } + + /** + * {@inheritDoc} + * @throws IOException {@inheritDoc} + * @throws MmdFormatException {@inheritDoc} + */ + @Override + protected void parseBody() + throws IOException, MmdFormatException{ + super.parseBody(); + + if(hasMore()){ + parseToonName(); + } + + return; + } + + /** + * 独自トゥーンテクスチャファイル名のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseToonName() throws IOException, MmdFormatException{ + if(this.toonHandler == null){ + skip(PmdLimits.MAXBYTES_TOONFILENAME * PmdLimits.TOON_FIXEDNUM); + return; + } + + this.toonHandler.loopStart(PmdToonHandler.TOON_LIST, + PmdLimits.TOON_FIXEDNUM ); + + for(int ct = 0; ct < PmdLimits.TOON_FIXEDNUM; ct++){ + String toonName = + parseZeroTermString(PmdLimits.MAXBYTES_TOONFILENAME); + this.toonHandler.pmdToonFileInfo(toonName); + + this.toonHandler.loopNext(PmdToonHandler.TOON_LIST); + } + + this.toonHandler.loopEnd(PmdToonHandler.TOON_LIST); + + return; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt3.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt3.java new file mode 100644 index 0000000..a34272b --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdParserExt3.java @@ -0,0 +1,208 @@ +/* + * pmd parser extension 3 + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import java.io.IOException; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.MmdSource; + +/** + * PMDモデルファイルのパーサ拡張その3。 + * ※剛体情報対応 + */ +public class PmdParserExt3 extends PmdParserExt2 { + + private static final int RIGID_DATA_SZ = 83; + private static final int JOINT_DATA_SZ = 124; + + private PmdRigidHandler rigidHandler = null; + private PmdJointHandler jointHandler = null; + + /** + * コンストラクタ。 + * @param source 入力ソース + */ + public PmdParserExt3(MmdSource source){ + super(source); + return; + } + + /** + * 剛体ハンドラを登録する。 + * @param handler 剛体ハンドラ + */ + public void setRigidHandler(PmdRigidHandler handler){ + this.rigidHandler = handler; + return; + } + + /** + * ジョイントハンドラを登録する。 + * @param handler ジョイントハンドラ + */ + public void setJointHandler(PmdJointHandler handler){ + this.jointHandler = handler; + return; + } + + /** + * {@inheritDoc} + * @throws IOException {@inheritDoc} + * @throws MmdFormatException {@inheritDoc} + */ + @Override + protected void parseBody() + throws IOException, MmdFormatException { + super.parseBody(); + + if(hasMore()){ + parseRigidList(); + parseJointList(); + } + + return; + } + + /** + * 剛体情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseRigidList() throws IOException, MmdFormatException{ + int rigidNum = parseInteger(); + + if(this.rigidHandler == null){ + skip(RIGID_DATA_SZ * rigidNum); + return; + } + + this.rigidHandler.loopStart(PmdRigidHandler.RIGID_LIST, rigidNum); + + for(int ct = 0; ct < rigidNum; ct++){ + String rigidName = + parseZeroTermString(PmdLimits.MAXBYTES_RIGIDNAME); + this.rigidHandler.pmdRigidName(rigidName); + + int linkedBoneId = parseUShortAsInteger(); + int rigidGroupId = parseUByteAsInteger(); + short collisionMap = parseShort(); + this.rigidHandler.pmdRigidInfo(rigidGroupId, linkedBoneId); + + byte shapeType = parseByte(); + float width = parseFloat(); + float height = parseFloat(); + float depth = parseFloat(); + this.rigidHandler.pmdRigidShape(shapeType, width, height, depth); + + float posX = parseFloat(); + float posY = parseFloat(); + float posZ = parseFloat(); + this.rigidHandler.pmdRigidPosition(posX, posY, posZ); + + float rotX = parseFloat(); + float rotY = parseFloat(); + float rotZ = parseFloat(); + this.rigidHandler.pmdRigidRotation(rotX, rotY, rotZ); + + float mass = parseFloat(); + float dampingPos = parseFloat(); + float dampingRot = parseFloat(); + float restitution = parseFloat(); + float friction = parseFloat(); + this.rigidHandler.pmdRigidPhysics(mass, + dampingPos, dampingRot, + restitution, friction ); + + byte behaveType = parseByte(); + this.rigidHandler.pmdRigidBehavior(behaveType, collisionMap); + + this.rigidHandler.loopNext(PmdRigidHandler.RIGID_LIST); + } + + this.rigidHandler.loopEnd(PmdRigidHandler.RIGID_LIST); + + return; + } + + /** + * ジョイント情報のパースと通知。 + * @throws IOException IOエラー + * @throws MmdFormatException フォーマットエラー + */ + private void parseJointList() throws IOException, MmdFormatException{ + int jointNum = parseInteger(); + + if(this.jointHandler == null){ + skip(JOINT_DATA_SZ * jointNum); + return; + } + + this.jointHandler.loopStart(PmdJointHandler.JOINT_LIST, jointNum); + + for(int ct = 0; ct < jointNum; ct++){ + String jointName = + parseZeroTermString(PmdLimits.MAXBYTES_JOINTNAME); + this.jointHandler.pmdJointName(jointName); + + int rigidIdA = parseInteger(); + int rigidIdB = parseInteger(); + this.jointHandler.pmdJointLink(rigidIdA, rigidIdB); + + float posX = parseFloat(); + float posY = parseFloat(); + float posZ = parseFloat(); + this.jointHandler.pmdJointPosition(posX, posY, posZ); + + float rotX = parseFloat(); + float rotY = parseFloat(); + float rotZ = parseFloat(); + this.jointHandler.pmdJointRotation(rotX, rotY, rotZ); + + float posXlim1 = parseFloat(); + float posYlim1 = parseFloat(); + float posZlim1 = parseFloat(); + float posXlim2 = parseFloat(); + float posYlim2 = parseFloat(); + float posZlim2 = parseFloat(); + this.jointHandler.pmdPositionLimit(posXlim1, posXlim2, + posYlim1, posYlim2, + posZlim1, posZlim2 ); + + float rotXlim1 = parseFloat(); + float rotYlim1 = parseFloat(); + float rotZlim1 = parseFloat(); + float rotXlim2 = parseFloat(); + float rotYlim2 = parseFloat(); + float rotZlim2 = parseFloat(); + this.jointHandler.pmdRotationLimit(rotXlim1, rotXlim2, + rotYlim1, rotYlim2, + rotZlim1, rotZlim2 ); + + float elasticPosX = parseFloat(); + float elasticPosY = parseFloat(); + float elasticPosZ = parseFloat(); + this.jointHandler.pmdElasticPosition(elasticPosX, + elasticPosY, + elasticPosZ ); + + float elasticRotX = parseFloat(); + float elasticRotY = parseFloat(); + float elasticRotZ = parseFloat(); + this.jointHandler.pmdElasticRotation(elasticRotX, + elasticRotY, + elasticRotZ ); + + this.jointHandler.loopNext(PmdJointHandler.JOINT_LIST); + } + + this.jointHandler.loopEnd(PmdJointHandler.JOINT_LIST); + + return; + } + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdRigidHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdRigidHandler.java new file mode 100644 index 0000000..98ee70c --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdRigidHandler.java @@ -0,0 +1,130 @@ +/* + * PMD rigid information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルの各種剛体情報の通知用ハンドラ。 + */ +public interface PmdRigidHandler extends LoopHandler { + + /** + * 剛体情報パースステージ。 + */ + class PmdRigidStage extends ParseStage{ + /** コンストラクタ。 */ + PmdRigidStage(){ super(); return; } + } + + /** 剛体情報抽出ループ。 */ + PmdRigidStage RIGID_LIST = new PmdRigidStage(); + + /** + * 剛体名の通知を受け取る。 + * {@link #RIGID_LIST}ループの構成要素。 + * @param rigidName 剛体名 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdRigidName(String rigidName) + throws MmdFormatException; + + /** + * 剛体基本情報の通知を受け取る。 + * {@link #RIGID_LIST}ループの構成要素。 + * @param rigidGroupId 剛体グループ番号から1引いた数。(0-15) + * @param linkedBoneId 接続先ボーンID + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdRigidInfo(int rigidGroupId, + int linkedBoneId) + throws MmdFormatException; + + /** + * 剛体形状の通知を受け取る。 + * {@link #RIGID_LIST}ループの構成要素。 + * @param shapeType 形状種別。 + *
    + *
  • 0x00:球 + *
  • 0x01:ç®± + *
  • 0x02:カプセル + *
+ * @param width 球orカプセル半径。箱の幅。 + * @param height 箱orカプセルの高さ + * @param depth 箱の奥行き + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdRigidShape(byte shapeType, + float width, float height, float depth) + throws MmdFormatException; + + /** + * 剛体位置の通知を受け取る。 + * {@link #RIGID_LIST}ループの構成要素。 + * @param posX X座標 + * @param posY Y座標 + * @param posZ Z座標 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdRigidPosition(float posX, float posY, float posZ) + throws MmdFormatException; + + /** + * 剛体姿勢の通知を受け取る。 + * {@link #RIGID_LIST}ループの構成要素。 + * @param radX X軸回転量(radian) + * @param radY Y軸回転量(radian) + * @param radZ Z軸回転量(radian) + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdRigidRotation(float radX, float radY, float radZ) + throws MmdFormatException; + + /** + * 剛体物理系数の通知を受け取る。 + * {@link #RIGID_LIST}ループの構成要素。 + * @param mass 質量 + * @param dampingPos 移動減衰率 + * @param dampingRot 回転減衰率 + * @param restitution 反発力 + * @param friction 摩擦力 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdRigidPhysics(float mass, + float dampingPos, float dampingRot, + float restitution, float friction ) + throws MmdFormatException; + + /** + * 剛体の振る舞い情報の通知を受け取る。 + * {@link #RIGID_LIST}ループの構成要素。 + * @param behaveType 剛体タイプ。 + *
    + *
  • 0:ボーン追従 + *
  • 1:物理演算 + *
  • 2:物理演算+ボーン位置合わせ + *
+ * @param collisionMap 非衝突剛体グループビットマップ。 + * (衝突グループ番号-1)位置のビット位置は1に、 + * (非衝突グループ番号-1)位置のビット位置は0になる。 + * 例)グループ1と8のみが非衝突指定の場合、0xff7eになる。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdRigidBehavior(byte behaveType, short collisionMap) + throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdShapeHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdShapeHandler.java new file mode 100644 index 0000000..9d4f591 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdShapeHandler.java @@ -0,0 +1,107 @@ +/* + * PMD shape information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルの各種形状(頂点、面)の通知用ハンドラ。 + * 0から始まる頂点ID順に頂点は出現する。 + * 0から始まる面ID順に面は出現する。 + */ +public interface PmdShapeHandler extends LoopHandler { + + /** + * モデル形状パースステージ。 + */ + class PmdShapeStage extends ParseStage{ + /** コンストラクタ。 */ + PmdShapeStage(){ super(); return; } + } + + /** 頂点抽出ループ。 */ + PmdShapeStage VERTEX_LIST = new PmdShapeStage(); + /** 面抽出ループ。 */ + PmdShapeStage SURFACE_LIST = new PmdShapeStage(); + + /** + * 頂点の座標の通知を受け取る。 + * {@link #VERTEX_LIST}ループの構成要素 + * @param xPos X座標 + * @param yPos Y座標 + * @param zPos Z座標 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdVertexPosition(float xPos, float yPos, float zPos) + throws MmdFormatException; + + /** + * 頂点の法線情報の通知を受け取る。 + * {@link #VERTEX_LIST}ループの構成要素 + * ※単位ベクトル化必須? + * @param xVec 法線ベクトルX成分 + * @param yVec 法線ベクトルY成分 + * @param zVec 法線ベクトルZ成分 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdVertexNormal(float xVec, float yVec, float zVec) + throws MmdFormatException; + + /** + * 頂点のUVマッピング情報の通知を受け取る。 + * (頂点UV) + * {@link #VERTEX_LIST}ループの構成要素 + * @param uVal テクスチャのU座標 + * @param vVal テクスチャのV座標 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdVertexUV(float uVal, float vVal ) + throws MmdFormatException; + + /** + * 頂点のボーン間ウェイトバランス情報の通知を受け取る。 + * {@link #VERTEX_LIST}ループの構成要素 + * @param boneId1 ボーンその1識別ID + * @param boneId2 ボーンその2識別ID + * @param weightForB1 ボーンその1への影響度。0(min)~100(max) + * ボーンその2への影響度は100からの引き算で求める。 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdVertexWeight(int boneId1, int boneId2, int weightForB1) + throws MmdFormatException; + + /** + * 頂点のエッジ表現情報の通知を受け取る。 + * 材質単位でのエッジ表現指定に優先される。 + * {@link #VERTEX_LIST}ループの構成要素 + * @param hideEdge エッジ無効ならtrue + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdVertexEdge(boolean hideEdge) + throws MmdFormatException; + + /** + * 3つの頂点から構成される面情報の通知を受け取る。 + * {@link #SURFACE_LIST}ループの構成要素 + * @param vertexId1 頂点IDその1 + * @param vertexId2 頂点IDその1 + * @param vertexId3 頂点IDその1 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdSurfaceTriangle(int vertexId1, int vertexId2, int vertexId3) + throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdToonHandler.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdToonHandler.java new file mode 100644 index 0000000..8d285f8 --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/PmdToonHandler.java @@ -0,0 +1,39 @@ +/* + * PMD toon texture file information handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +import jp.sourceforge.mikutoga.parser.LoopHandler; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.ParseStage; + +/** + * PMDモデルの独自トゥーンテクスチャファイル名の通知用ハンドラ。 + */ +public interface PmdToonHandler extends LoopHandler { + + /** + * 独自トゥーンテクスチャファイル名パースステージ。 + */ + class PmdToonStage extends ParseStage{ + /** コンストラクタ。 */ + PmdToonStage(){ super(); return; } + } + + /** トゥーンテクスチャファイル名抽出ループ。 */ + PmdToonStage TOON_LIST = new PmdToonStage(); + + /** + * 独自トゥーンテクスチャファイル名の通知を受け取る。 + * {@link #TOON_LIST}ループの構成要素 + * @param toonName 独自トゥーンテクスチャファイル名 + * @throws MmdFormatException 不正フォーマットによる + * パース処理の中断をパーサに指示 + */ + void pmdToonFileInfo(String toonName) throws MmdFormatException; + +} diff --git a/src/main/java/jp/sourceforge/mikutoga/parser/pmd/package-info.java b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/package-info.java new file mode 100644 index 0000000..cf9ed5d --- /dev/null +++ b/src/main/java/jp/sourceforge/mikutoga/parser/pmd/package-info.java @@ -0,0 +1,14 @@ +/* + * package information for Javadoc + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +/** + * PMDモデルファイル用パーサライブラリ。 + */ + +package jp.sourceforge.mikutoga.parser.pmd; + +/* EOF */ diff --git a/src/test/java/jp/sourceforge/mikutoga/parser/ParseStageTest.java b/src/test/java/jp/sourceforge/mikutoga/parser/ParseStageTest.java new file mode 100644 index 0000000..a135013 --- /dev/null +++ b/src/test/java/jp/sourceforge/mikutoga/parser/ParseStageTest.java @@ -0,0 +1,47 @@ +/* + * + */ + +package jp.sourceforge.mikutoga.parser; + +import junit.framework.TestCase; +import org.junit.Test; + +/** + * + */ +public class ParseStageTest extends TestCase { + + public ParseStageTest(String testName) { + super(testName); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + /** + * Test of toString method, of class ParseStage. + */ + @Test + public void testToString(){ + System.out.println("toString"); + + ParseStage instance; + + instance = new ParseStage(); + assertEquals("", instance.toString()); + + instance = new ParseStage("ABC"); + assertEquals("ABC", instance.toString()); + + return; + } + +} diff --git a/src/test/java/sample/pmd/DummyHandler.java b/src/test/java/sample/pmd/DummyHandler.java new file mode 100644 index 0000000..5caead7 --- /dev/null +++ b/src/test/java/sample/pmd/DummyHandler.java @@ -0,0 +1,336 @@ +/* + * sample handler + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package sample.pmd; + +import jp.sourceforge.mikutoga.parser.ParseStage; +import jp.sourceforge.mikutoga.parser.pmd.PmdBasicHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdBoneHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdEngHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdJointHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdMaterialHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdMorphHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdRigidHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdShapeHandler; +import jp.sourceforge.mikutoga.parser.pmd.PmdToonHandler; + +/** + * サンプルのハンドラ。 + */ +public class DummyHandler + implements PmdBasicHandler, + PmdBoneHandler, + PmdShapeHandler, + PmdMorphHandler, + PmdMaterialHandler, + PmdEngHandler, + PmdToonHandler, + PmdRigidHandler, + PmdJointHandler { + + public DummyHandler(){ + super(); + return; + } + + public void pmdParseStart(){ + System.out.println("PMD parse start"); + return; + } + + public void pmdParseEnd(boolean hasMoreData){ + System.out.println("PMD parse end"); + if(hasMoreData){ + System.out.println("thre is unknown data below"); + } + return; + } + + public void loopStart(ParseStage stage, int loops){ + System.out.println("===== Loop start ===== * " + loops); + return; + } + + public void loopNext(ParseStage stage){ + return; + } + + public void loopEnd(ParseStage stage){ + System.out.println("===== Loop end ====="); + return; + } + + public void pmdHeaderInfo(float ver){ + System.out.println("ver=" + ver); + return; + } + + public void pmdModelInfo(String modelName, String description){ + System.out.println("modelName=" + modelName); + System.out.println("comment=" + description); + return; + } + + public void pmdVertexPosition(float xPos, float yPos, float zPos){ +// System.out.println("x,y,z = " + xPos + "," + yPos + "," + zPos); + return; + } + + public void pmdVertexNormal(float xVec, float yVec, float zVec){ +// System.out.println("x,y,z = " + xVec + "," + yVec + "," + zVec); + } + + public void pmdVertexUV(float uVal, float vVal){ + return; + } + + public void pmdVertexWeight(int boneId_1, int boneId_2, + int weightForB1){ +// System.out.println("x,y,z = " +// + boneId_1 + "," + boneId_2 + "," + weightForB1); + return; + } + + public void pmdVertexEdge(boolean hideEdge){ +// System.out.println("x,y,z = " + hideEdge); + return; + } + + public void pmdSurfaceTriangle(int vertexId_1, + int vertexId_2, + int vertexId_3){ +// System.out.println("v1,v2,v3 = " +// + vertexId_1 + "," + vertexId_2 + "," + vertexId_3); + } + + public void pmdMaterialDiffuse(float red, float green, float blue, + float alpha ){ +// System.out.println("diffuse rgba=" +// + red + "," + green + "," + blue + "," + alpha); + return; + } + + public void pmdMaterialSpecular(float red, float green, float blue, + float shininess){ +// System.out.println("specular rgbs=" +// + red + "," + green + "," + blue + "," + shininess); + return; + } + + public void pmdMaterialAmbient(float red, float green, float blue){ +// System.out.println("ambient rgb=" + red + "," + green + "," + blue); + return; + } + + public void pmdMaterialShading(int toon_idx, + String textureFile, String sphereFile){ +// System.out.println("toon idx=" + toon_idx); +// System.out.println("texfile=" + textureFile); +// System.out.println("spherefile=" + sphereFile); + return; + } + + public void pmdMaterialInfo(boolean hasEdge, int surfaceNum){ +// System.out.println("surfaces=" + surfaceNum); + return; + } + + public void pmdBoneInfo(String boneName, byte boneKind){ +// System.out.println("bonename = " + boneName); +// System.out.println("kind = " + boneKind); + return; + } + + public void pmdBoneLink(int parentId, int tailId, int ikId){ +// System.out.println("parent = " + parentId); +// System.out.println("tail = " + tailId); +// System.out.println("ik = " + ikId); + return; + } + + public void pmdBonePosition(float xPos, float yPos, float zPos){ +// System.out.println("x="+xPos+" y="+yPos+" z="+zPos); + return; + } + + public void pmdIKInfo(int boneId, int targetId, + int depth, float weight ){ + return; + } + + public void pmdIKChainInfo(int childId){ +// System.out.println("chained bone = " + childId); + return; + } + + public void pmdMorphInfo(String morphName, byte morphType){ +// System.out.println("morph name = " + morphName); + return; + } + + public void pmdMorphVertexInfo(int vertexId, + float xPos, float yPos, float zPos){ +// System.out.println("id="+vertexId+",x="+xPos+",y="+yPos+",z="+zPos); + return; + } + + public void pmdMorphOrderInfo(int morphId){ +// System.out.println("morph idx = " + morphId); + return; + } + + public void pmdBoneGroupInfo(String groupName){ +// System.out.println("group name = " + groupName); + return; + } + + public void pmdGroupedBoneInfo(int boneId, int groupId){ +// System.out.println("bone index=" +// + boneId + " groupIndex=" + groupId); + return; + } + + public void pmdEngEnabled(boolean hasEnglishInfo){ + return; + } + + public void pmdEngModelInfo(String modelName, String description){ + return; + } + + public void pmdEngBoneInfo(String boneName){ +// System.out.println("bone eng name = " + boneName); + return; + } + + public void pmdEngMorphInfo(String morphName){ +// System.out.println("morph eng name = " + morphName); + return; + } + + public void pmdEngBoneGroupInfo(String groupName){ +// System.out.println("group eng name = " + groupName); + return; + } + + public void pmdToonFileInfo(String toonName){ +// System.out.println("toon file name = " + toonName); + return; + } + + public void pmdRigidName(String rigidName){ +// System.out.println("rigid name = " + rigidName); + return; + } + + public void pmdRigidInfo(int rigidGroupId, int linkedBoneId){ +// System.out.println("rigid group = " +// + rigidGroupId + ",linked Bone =" + linkedBoneId); + return; + } + + public void pmdRigidShape(byte shapeType, + float width, float height, float depth){ +// System.out.println("type="+shapeType); +// System.out.println("w="+width); +// System.out.println("h="+height); +// System.out.println("d="+depth); + return; + } + + public void pmdRigidPosition(float posX, float posY, float posZ){ +// System.out.println("x="+posX); +// System.out.println("y="+posY); +// System.out.println("z="+posZ); + return; + } + + public void pmdRigidRotation(float rotX, float rotY, float rotZ){ +// System.out.println("x="+rotX); +// System.out.println("y="+rotY); +// System.out.println("z="+rotZ); + return; + } + + public void pmdRigidPhysics(float mass, + float fadePos, float fadeRot, + float restitution, float friction ){ +// System.out.println("mass="+mass); +// System.out.println("fadePos="+fadePos); +// System.out.println("fadeRot="+fadeRot); +// System.out.println("recoil="+restitution); +// System.out.println("friction="+friction); + return; + } + + public void pmdRigidBehavior(byte behaveType, short collisionMap){ +// System.out.println("type="+behaveType); +// System.out.println("map="+collisionMap); + return; + } + + public void pmdJointName(String jointName){ +// System.out.println("joint name = " + jointName); + return; + } + + public void pmdJointLink(int rigidId_A, int rigidId_B){ +// System.out.println("rigid1 = " + rigidId_A+",rigidB = " + rigidId_B); + return; + } + + public void pmdJointPosition(float posX, float posY, float posZ){ +// System.out.println("posX = " + posX); +// System.out.println("posY = " + posY); +// System.out.println("posZ = " + posZ); + return; + } + + public void pmdJointRotation(float rotX, float rotY, float rotZ){ +// System.out.println("rotX = " + rotX); +// System.out.println("rotY = " + rotY); +// System.out.println("rotZ = " + rotZ); + return; + } + + public void pmdPositionLimit(float posX_lim1, float posX_lim2, + float posY_lim1, float posY_lim2, + float posZ_lim1, float posZ_lim2 ){ +// System.out.println("limX = " + posX_lim1+"-"+posX_lim2); +// System.out.println("limY = " + posY_lim1+"-"+posY_lim2); +// System.out.println("limZ = " + posZ_lim1+"-"+posZ_lim2); + return; + } + + public void pmdRotationLimit(float rotX_lim1, float rotX_lim2, + float rotY_lim1, float rotY_lim2, + float rotZ_lim1, float rotZ_lim2 ){ +// System.out.println("limX = " + rotX_lim1+"-"+rotX_lim2); +// System.out.println("limY = " + rotY_lim1+"-"+rotY_lim2); +// System.out.println("limZ = " + rotZ_lim1+"-"+rotZ_lim2); + return; + } + + public void pmdElasticPosition(float elasticPosX, + float elasticPosY, + float elasticPosZ ){ +// System.out.println("posX="+elasticPosX); +// System.out.println("posY="+elasticPosY); +// System.out.println("posZ="+elasticPosZ); + return; + } + + public void pmdElasticRotation(float elasticRotX, + float elasticRotY, + float elasticRotZ ){ +// System.out.println("posX="+elasticRotX); +// System.out.println("posY="+elasticRotY); +// System.out.println("posZ="+elasticRotZ); + return; + } + +} diff --git a/src/test/java/sample/pmd/DummyMain.java b/src/test/java/sample/pmd/DummyMain.java new file mode 100644 index 0000000..ed4d624 --- /dev/null +++ b/src/test/java/sample/pmd/DummyMain.java @@ -0,0 +1,104 @@ +/* + * sample parser + * + * License : The MIT License + * Copyright(c) 2010 MikuToga Partners + */ + +package sample.pmd; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import jp.sourceforge.mikutoga.parser.MmdFormatException; +import jp.sourceforge.mikutoga.parser.MmdSource; +import jp.sourceforge.mikutoga.parser.pmd.PmdParser; + +/** + * パーサ利用のサンプルプログラム。 + */ +public class DummyMain { + + private static final String PMDFILE; + private static final int BUF_SZ = 4086; + private static final DummyHandler handler = new DummyHandler(); + + static{ + PMDFILE = "D:\\Test\\test.pmd"; + } + + /** + * 入力ソースを準備する。 + * @param fname ファイル名 + * @return 入力ソース + */ + private static MmdSource buildSource(String fname){ + File file = new File(fname); + + InputStream is; + try{ + is = new FileInputStream(file); + }catch(FileNotFoundException e){ + System.err.println(e); + System.exit(1); + return null; + } + is = new BufferedInputStream(is, BUF_SZ); + + MmdSource source = new MmdSource(is); + + return source; + } + + /** + * 各種ハンドラをパーサにセットアップする。 + * @param parser パーサ + */ + private static void setupHandler(PmdParser parser){ + parser.setBasicHandler(handler); + parser.setShapeHandler(handler); + parser.setMaterialHandler(handler); + parser.setBoneHandler(handler); + parser.setMorphHandler(handler); + parser.setEngHandler(handler); + parser.setToonHandler(handler); + parser.setRigidHandler(handler); + parser.setJointHandler(handler); + + return; + } + + /** + * Java実行エントリ + * @param args 起動引数 + */ + public static void main(String[] args){ + String fname; + if(args.length == 1) fname = args[0]; + else fname = PMDFILE; + + MmdSource source = buildSource(fname); + + PmdParser parser = new PmdParser(source); + + setupHandler(parser); + + try{ + parser.parsePmd(); + }catch(IOException e){ + System.err.println(e); + System.exit(1); + }catch(MmdFormatException e){ + System.err.println(e); + System.exit(1); + } + + System.exit(0); + + return; + } + +} -- 2.11.0