/*
* VsqCommon.cs
* Copyright (c) 2008-2009 kbinani
*
* This file is part of Boare.Lib.Vsq.
*
* Boare.Lib.Vsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* Boare.Lib.Vsq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#if JAVA
package org.kbinani.vsq;
import java.io.*;
import org.kbinani.*;
#else
using System;
using bocoree;
using bocoree.java.io;
namespace Boare.Lib.Vsq {
using boolean = System.Boolean;
#endif
///
/// vsqファイルのメタテキストの[Common]セクションに記録される内容を取り扱う
///
#if JAVA
public class VsqCommon implements Cloneable, Serializable {
#else
[Serializable]
public class VsqCommon : ICloneable {
#endif
public String Version;
public String Name;
public String Color;
public int DynamicsMode;
public int PlayMode = 1;
#if !JAVA
public object Clone() {
return clone();
}
#endif
public Object clone() {
String[] spl = PortUtil.splitString( Color, new char[] { ',' }, 3 );
int r = PortUtil.parseInt( spl[0] );
int g = PortUtil.parseInt( spl[1] );
int b = PortUtil.parseInt( spl[2] );
VsqCommon res = new VsqCommon( Name, r, g, b, DynamicsMode, PlayMode );
res.Version = Version;
return res;
}
///
/// 各パラメータを指定したコンストラクタ
///
/// トラック名
/// Color値(意味は不明)
/// DynamicsMode(デフォルトは1)
/// PlayMode(デフォルトは1)
public VsqCommon( String name, int red, int green, int blue, int dynamics_mode, int play_mode ) {
this.Version = "DSB301";
this.Name = name;
this.Color = red + "," + green + "," + blue;
this.DynamicsMode = dynamics_mode;
this.PlayMode = play_mode;
}
#if JAVA
public VsqCommon(){
this( "Miku", 179, 181, 123, 1, 1 );
#else
public VsqCommon()
: this( "Miku", 179, 181, 123, 1, 1 ) {
#endif
}
///
/// MetaTextのテキストファイルからのコンストラクタ
///
/// 読み込むテキストファイル
/// 読み込んだ最後の行が返される
public VsqCommon( TextMemoryStream sr, ByRef last_line ) {
Version = "";
Name = "";
Color = "0,0,0";
DynamicsMode = 0;
PlayMode = 1;
last_line.value = sr.readLine();
String[] spl;
while ( !last_line.value.StartsWith( "[" ) ) {
spl = PortUtil.splitString( last_line.value, new char[] { '=' } );
String search = spl[0];
if ( search.Equals( "Version" ) ) {
this.Version = spl[1];
} else if ( search.Equals( "Name" ) ) {
this.Name = spl[1];
} else if ( search.Equals( "Color" ) ) {
this.Color = spl[1];
} else if ( search.Equals( "DynamicsMode" ) ) {
this.DynamicsMode = PortUtil.parseInt( spl[1] );
} else if ( search.Equals( "PlayMode" ) ) {
this.PlayMode = PortUtil.parseInt( spl[1] );
}
if ( sr.peek() < 0 ) {
break;
}
last_line.value = sr.readLine();
}
}
///
/// インスタンスの内容をテキストファイルに出力します
///
/// 出力先
public void write( TextMemoryStream sw ) {
sw.writeLine( "[Common]" );
sw.writeLine( "Version=" + Version );
sw.writeLine( "Name=" + Name );
sw.writeLine( "Color=" + Color );
sw.writeLine( "DynamicsMode=" + DynamicsMode );
sw.writeLine( "PlayMode=" + PlayMode );
}
///
/// VsqCommon構造体を構築するテストを行います
///
/// テストに成功すればtrue、そうでなければfalse
public static boolean test()
#if JAVA
throws IOException
#endif
{
String fpath = PortUtil.createTempFile();
BufferedWriter sw = new BufferedWriter( new FileWriter( fpath ) );
sw.write( "Version=DSB301" );
sw.newLine();
sw.write( "Name=Voice1" );
sw.newLine();
sw.write( "Color=181,162,123" );
sw.newLine();
sw.write( "DynamicsMode=1" );
sw.newLine();
sw.write( "PlayMode=1" );
sw.newLine();
sw.write( "[Master]" );
sw.newLine();
sw.close();
VsqCommon vsqCommon = null;
ByRef last_line = new ByRef( "" );
TextMemoryStream sr = null;
try {
sr = new TextMemoryStream( fpath, "UTF8" );
vsqCommon = new VsqCommon( sr, last_line );
} catch ( Exception ex ) {
} finally {
if ( sr != null ) {
try {
sr.close();
} catch ( Exception ex2 ) {
}
}
}
if ( vsqCommon == null ) {
vsqCommon = new VsqCommon();
}
boolean result;
if ( vsqCommon.Version.Equals( "DSB301" ) &&
vsqCommon.Name.Equals( "Voice1" ) &&
vsqCommon.Color.Equals( "181,162,123" ) &&
vsqCommon.DynamicsMode == 1 &&
vsqCommon.PlayMode == 1 &&
last_line.value.Equals( "[Master]" ) ) {
result = true;
} else {
result = false;
}
PortUtil.deleteFile( fpath );
return result;
}
}
#if !JAVA
}
#endif