public void MakeStruct(CodeClass codeClass, BfsStruct dataStruct)
{
foreach (BfsStructField structField in dataStruct.StructFieldList)
{
string type = "";
string name = structField.Name;
if (structField.FieldType is BfsNamedType)
{
BfsNamedType namedType = structField.FieldType as BfsNamedType;
type = namedType.DataBlock.Name;
}
else if (structField.FieldType is BfsPrimitiveType)
{
BfsPrimitiveType primitiveType = structField.FieldType as BfsPrimitiveType;
type = CSHelper.TypeMap(primitiveType).ToLower();
}
else if (structField.FieldType is BfsFunctionType)
{
BfsFunctionType functionType = structField.FieldType as BfsFunctionType;
if (functionType.FunctionName == "ascii")
type = "string";
else
BfsCompiler.ReportError(functionType.SourceRange,
"Only ASCII strings are supported so far..!");
}
if (structField.FieldType.ArrayExtension == null)
codeClass.CodeFields.Add(type + " " + name + ";");
else
{
if (structField.FieldType.ArrayExtension is BfsKnownArray)
codeClass.CodeFields.Add(type + " [] " + name + ";");
else
if (structField.FieldType.ArrayExtension is BfsUnknownArray)
codeClass.CodeFields.Add("List<" + type + "> " + name + ";");
else
BfsCompiler.ReportError(structField.FieldType.ArrayExtension.SourceRange,
"Unknown array extended type. Known or unknown array type expected!");
}
}
CodeMethod read = new CodeMethod(dataStruct.Name + " Read(BfsBinaryReader file)");
codeClass.CodeMethods.Add(read);
MakeReadStruct(dataStruct, read);
//ToString() method
CodeMethod toString = new CodeMethod("override string ToString()");
codeClass.CodeMethods.Add(toString);
toString.CodeLines.Add("StringBuilder sb = new StringBuilder();");
toString.CodeLines.Add("sb.AppendLine(\"== Struct: "+ dataStruct.Name +" ==\");");
foreach (BfsStructField field in dataStruct.StructFieldList)
toString.CodeLines.Add("sb.AppendLine(\"" + field.Name + " : \" + " + field.ToString() + ".ToString());");
toString.CodeLines.Add("return sb.ToString();");
}