using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ASToFra.LibraryCreatorXML
{
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;
BinaryReader mdlReader;
public MDLObject(string mdlFile)
{
sPath = mdlFile;
// Set default values
mdlVersion = FsVersion.unknown;
sGUID = null;
sShortName = Path.GetFileNameWithoutExtension(mdlFile);
sName = sShortName;
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();
}catch{
// Something wrong, but I am not going to do anything
// special now
}
}
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));
}
// Else just skip the section
else
{
mdlReader.ReadBytes(length);
}
}
}
public string filename
{
get
{
return Path.GetFileName(sPath);
}
}
public string filenameWithoutExtension
{
get
{
return Path.GetFileNameWithoutExtension(sPath);
}
}
public string path
{
get
{
return sPath;
}
}
public FsVersion version
{
get
{
return mdlVersion;
}
}
public string GUID
{
get
{
return sGUID;
}
set
{
sGUID = value;
}
}
public string name
{
get
{
return sName;
}
}
public string shortName
{
get
{
return sShortName;
}
}
}
}