public class MDLObject
{
// Enumeration of the support FS versions
public enum FsVersion
{
unknown = 0,
Fs2004 = 9,
FsX = 10,
}
// Local variables
FsVersion mdlVersion = FsVersion.unknown;
string sGUID;
string sName;
string sShortName;
string sPath;
BoundingBox m_bbox;
BinaryReader mdlReader;
bool sFound;
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 14/01/2007 22:53
// Procedure Name: MDLObject
// Purpose: Constructor
// ----------------------------------------------------------------------------------------
public MDLObject(string mdlFile)
{
sPath = mdlFile
// Set default values
mdlVersion = FsVersion.unknown;
sGUID = null;
sShortName = Path.GetFileNameWithoutExtension(mdlFile);
sName = sShortName;
m_bbox = null;
sFound = false;
try
{
mdlReader = new BinaryReader(File.Open(mdlFile, FileMode.Open),System.Text.Encoding.Default);
string sRIFF = new string(mdlReader.ReadChars(4));
mdlReader.ReadInt32(); // skip length
string sHEAD = new string(mdlReader.ReadChars(4));
// If this is a valid RIFF file
if (sRIFF == "RIFF")
{
// If Fs2004
if (sHEAD == "MDL9")
{
mdlVersion = FsVersion.Fs2004;
readInformation(mdlReader);
}
// If FsX
else if (sHEAD == "MDLX")
{
mdlVersion = FsVersion.FsX;
readInformation(mdlReader);
}
}
mdlReader.Close();
sFound = true;
}catch{
// Something wrong, but I am not going to do anything
// special now
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 14/01/2007 22:55
// Procedure Name: readInformation
// Purpose: Read the information from the MDL file
// ----------------------------------------------------------------------------------------
public void readInformation(BinaryReader mdlReader)
{
string sLABL;
int length;
while (mdlReader.PeekChar() != -1)
{
sLABL = new string(mdlReader.ReadChars(4));
length = mdlReader.ReadInt32();
// If this is the GUID, read it
if (sLABL == "MDLG")
{
uint g1, g21, g22; //, g31, g32, g4;
byte[] g3;
string g3s, g4s;
g1 = mdlReader.ReadUInt32();
g21 = mdlReader.ReadUInt16();
g22 = mdlReader.ReadUInt16();
g3 = mdlReader.ReadBytes(8);
g3s = g3[0].ToString("X").PadLeft(2, '0') + g3[1].ToString("X").PadLeft(2, '0');
g4s = g3[2].ToString("X").PadLeft(2, '0') + g3[3].ToString("X").PadLeft(2, '0') +
g3[4].ToString("X").PadLeft(2, '0') + g3[5].ToString("X").PadLeft(2, '0') +
g3[6].ToString("X").PadLeft(2, '0') + g3[7].ToString("X").PadLeft(2, '0');
sGUID = "{" + g1.ToString("X").PadLeft(8, '0') + "-" +
g21.ToString("X").PadLeft(4, '0') + "-" +
g22.ToString("X").PadLeft(4, '0') + "-" +
g3s + "-" + g4s + "}";
}
// If this is the name, read it
else if (sLABL == "MDLN")
{
sName = new string(mdlReader.ReadChars(length));
}
// If this is the bounding box, read it
else if (sLABL == "BBOX")
{
m_bbox = new BoundingBox(mdlReader.ReadSingle(),
mdlReader.ReadSingle(), mdlReader.ReadSingle(),
mdlReader.ReadSingle(), mdlReader.ReadSingle(),
mdlReader.ReadSingle());
}
// Else just skip the section
else
{
mdlReader.ReadBytes(length);
}
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 17/01/2007 22:53
// Procedure Name: filename
// Purpose: Return the MDL filename
// ----------------------------------------------------------------------------------------
public string filename
{
get
{
return Path.GetFileName(sPath);
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 17/01/2007 22:53
// Procedure Name: filename
// Purpose: Return the MDL filename without extension
// ----------------------------------------------------------------------------------------
public string filenameWithoutExtension
{
get
{
return Path.GetFileNameWithoutExtension(sPath);
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 17/01/2007 22:52
// Procedure Name: path
// Purpose: Path property
// ----------------------------------------------------------------------------------------
public string path
{
get
{
return sPath;
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 14/01/2007 22:56
// Procedure Name: version
// Purpose: Version property
// ----------------------------------------------------------------------------------------
public FsVersion version
{
get
{
return mdlVersion;
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 14/01/2007 22:56
// Procedure Name: GUID
// Purpose: GUID property
// ----------------------------------------------------------------------------------------
public string GUID
{
get
{
return sGUID;
}
set
{
sGUID = value;
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 14/01/2007 22:56
// Procedure Name: name
// Purpose: Name property
// ----------------------------------------------------------------------------------------
public string name
{
get
{
return sName;
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 14/01/2007 22:56
// Procedure Name: bbox
// Purpose: Bounding box property
// ----------------------------------------------------------------------------------------
public BoundingBox bbox
{
get
{
return m_bbox;
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 20/02/2007 21:34
// Procedure Name: found
// Purpose: Found property
// ----------------------------------------------------------------------------------------
public bool found
{
get
{
return sFound;
}
}
// ----------------------------------------------------------------------------------------
// Author: arno
// Date: 04/03/2007 15:34
// Procedure Name: shortName
// Purpose: shortName property
// ----------------------------------------------------------------------------------------
public string shortName
{
get
{
return sShortName;
}
}
}