Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MBINCompiler/Source/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static class OptionBackers
public static List<string> optExcludeFilters = null;
public static HeaderFormat optFormatVersion = HeaderFormat.V4;
public static bool optUseThreads = true;
public static bool optTyped = false;
}

// --debug
Expand Down Expand Up @@ -80,6 +81,9 @@ internal set {
// --no-threads
public static bool UseThreads { get => optUseThreads; internal set => optUseThreads = value; }

// --typed
public static bool IncludeTypeInfo { get => optTyped; internal set => optTyped = value; }

public static readonly List<Option> OPTIONS_GENERAL = new List<Option> {
new Option { shortName = 'q', longName = "quiet",
description = "Do not display any console messages.\n" +
Expand Down Expand Up @@ -155,6 +159,8 @@ internal set {
new Option { longName = "no-threads", isHidden = true, description = "Disable multi-threading." },

new Option { longName = "stream", description = "Enable sending MXML to Console." },

new Option { longName = "typed", description = "Enable type information in MXML." },
};

public static readonly List<Option> OPTIONS_LIST = new List<Option> {
Expand Down
2 changes: 1 addition & 1 deletion MBINCompiler/Source/Commands/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private static string ConvertMBIN( string inputPath, FileStream fIn, MemoryStrea
if ( data is null ) throw new InvalidDataException( "Invalid MBIN data." );

msg = $"Failed serializing {mbin.Header.GetXMLTemplateName()} to MXML.";
string mxml = MXmlFile.WriteTemplate(data, HideVersionInfo);
string mxml = MXmlFile.WriteTemplate(data, HideVersionInfo, IncludeTypeInfo);

if ( StreamToConsole ) {
EmitInfo($"");
Expand Down
1 change: 1 addition & 0 deletions MBINCompiler/Source/Commands/ConvertMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public override int ExecuteCommand( CommandLineParser options ) {
UseThreads = !options.GetOptionSwitch( "no-threads" );
StreamToConsole = options.GetOptionSwitch("stream");
HideVersionInfo = options.GetOptionSwitch("no-version");
IncludeTypeInfo = options.GetOptionSwitch( "typed" );

var arg = options.GetOptionArg( "format-version" );
if ( arg != null ) {
Expand Down
10 changes: 6 additions & 4 deletions libMBIN/Source/MXML/MXmlFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ public static MXmlData ReadMXmlDataFromString(string xml)
/// Writes the NMSTemplate object to an .mxml file.
/// </summary>
/// <param name="outputpath">The location to write the .mxml file.</param>
/// <param name="hideVersionInfo">version info is written to the MXML file.</param>
public static string WriteTemplate(NMSTemplate template) => WriteTemplate(template, false);
/// <param name="hideVersionInfo">If true, version info is not written to the MXML file.</param>
/// <param name="includeTypeInfo">If true, type info is written to the MXML file.</param>
public static string WriteTemplate(NMSTemplate template) => WriteTemplate(template, false, false);
/// <summary>
/// Writes the NMSTemplate object to an .mxml file.
/// </summary>
/// <param name="outputpath">The location to write the .mxml file.</param>
/// <param name="hideVersionInfo">If true, version info is not written to the MXML file.</param>
public static string WriteTemplate(NMSTemplate template, bool hideVersionInfo)
/// <param name="includeTypeInfo">If true, type info is written to the MXML file.</param>
public static string WriteTemplate(NMSTemplate template, bool hideVersionInfo, bool includeTypeInfo)
{
var origCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Expand All @@ -114,7 +116,7 @@ public static string WriteTemplate(NMSTemplate template, bool hideVersionInfo)
var ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
string str_ver = $"{ver.Major}.{ver.Minor:00}.{ver.Build}.{ver.Revision}";
if ( !hideVersionInfo ) xmlTextWriter.WriteComment($"File created using MBINCompiler version ({str_ver})");
var data = template.SerializeMXml(false);
var data = template.SerializeMXml(false, false, includeTypeInfo);
Serializer.Serialize(xmlTextWriter, data, Namespaces);
xmlTextWriter.Flush();

Expand Down
5 changes: 5 additions & 0 deletions libMBIN/Source/MXML/MXmlProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class MXmlProperty : MXmlBase
public string Value { get; set; }
[XmlAttribute("linked")]
public string Linked { get; set; }
[XmlAttribute("array_size")]
public string ArraySize { get; set; }
[XmlAttribute("_id")]
public string ID { get; set; }
[XmlAttribute("_index")]
Expand All @@ -26,6 +28,9 @@ public override string ToString()
if (this.Linked != null) {
result += $" linked=\"{this.Linked}\"";
}
if (this.ArraySize != null) {
result += $" array_size=\"{this.ArraySize}\"";
}
result += ">";
return result;
}
Expand Down
51 changes: 33 additions & 18 deletions libMBIN/Source/Template/NMSTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,16 @@ public byte[] SerializeBytes() {
return stream.ToArray();
}
}
public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute settings, object value, bool isField = true)

/// <summary>
/// .
/// </summary>
/// <param name="fieldType">The field type of the field.</param>
/// <param name="field">A field.</param>
/// <param name="settings">The settings of the field.</param>
/// <param name="value">The value of the field.</param>
/// <param name="IncludeTypeInfo">If true, type info is written to the MXML file.</param>
public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute settings, object value, bool IncludeTypeInfo)
{
string t = fieldType.Name;
int i = 0;
Expand Down Expand Up @@ -1237,13 +1246,13 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
case "Colour32":
// Handle the Colour32 explicitly since we want to write floats to the MXML, not ints.
Colour colour = new Colour((Colour32)value);
MXmlProperty colour_field = (MXmlProperty)colour.SerializeMXml( true );
MXmlProperty colour_field = (MXmlProperty)colour.SerializeMXml( true, false, IncludeTypeInfo );
colour_field.Name = fieldName;
return colour_field;
case "LinkableNMSTemplate":
LinkableNMSTemplate linkedTemplate = (LinkableNMSTemplate) value;
if (linkedTemplate.Template != null) {
MXmlProperty templateXmlData = (MXmlProperty)linkedTemplate.Template.SerializeMXml( true, true );
MXmlProperty templateXmlData = (MXmlProperty)linkedTemplate.Template.SerializeMXml( true, true, IncludeTypeInfo );
templateXmlData.Name = fieldName;
templateXmlData.Value = linkedTemplate.Template.GetType().Name;
if (linkedTemplate.Linked.StringValue() != "") {
Expand Down Expand Up @@ -1280,7 +1289,7 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
} else {
Dictionary<string, uint> IdCounter = new Dictionary<string, uint>{};
foreach ( var template in templates ) {
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( listType, field, settings, template, false );
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( listType, field, settings, template, IncludeTypeInfo );
data.Name = fieldName;
string typeIdField = TypeHasID(listType);
if (typeIdField != null) {
Expand Down Expand Up @@ -1310,7 +1319,7 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
if ( value != null ) {
NMSTemplate template = (NMSTemplate) value;

MXmlProperty templateXmlData = (MXmlProperty)template.SerializeMXml( true, true );
MXmlProperty templateXmlData = (MXmlProperty)template.SerializeMXml( true, true, IncludeTypeInfo );
templateXmlData.Name = fieldName;
templateXmlData.Value = template.GetType().Name;

Expand All @@ -1335,7 +1344,7 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
string id_field = field.GetCustomAttribute<NMSAttribute>()?.KeyField ?? "";

foreach ( var template in (IEnumerable)value ) {
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( hashMapType, field, settings, template, false );
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( hashMapType, field, settings, template, IncludeTypeInfo );

// Get aforementioned id field and write to the `_id` attribute.
MXmlProperty IdData = (MXmlProperty)data.Elements.Where(
Expand All @@ -1357,7 +1366,7 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
} else {
template = (NMSTemplate) value;
}
var templateXmlData = template.SerializeMXml( true );
var templateXmlData = template.SerializeMXml( true, false, IncludeTypeInfo );
templateXmlData.Name = fieldName;

return templateXmlData;
Expand All @@ -1367,11 +1376,15 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
Name = fieldName
};

if (IncludeTypeInfo) {
arrayProperty.ArraySize = field.GetCustomAttribute<NMSAttribute>()?.Size.ToString();
}

Array array = (Array) value;
string[] names = GetEnumNames( field.Name, array.Length, settings );
i = 0;
foreach ( var template in array ) {
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( arrayType, field, settings, template, false );
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( arrayType, field, settings, template, IncludeTypeInfo );
// Only change the name if we have an associated enum.
string overwriteName = names[i];
if (overwriteName != null && overwriteName != "") {
Expand Down Expand Up @@ -1467,7 +1480,13 @@ private static int GetArrayLength( string fieldName, NMSAttribute settings ) {
return GetEnumNames( fieldName, settings ).Length;
}

public MXmlBase SerializeMXml(bool isChildTemplate, bool isGenericTemplate = false) {
/// <summary>
/// .
/// </summary>
/// <param name="isChildTemplate">If true, </param>
/// <param name="isGenericTemplate">If true, </param>
/// <param name="IncludeTypeInfo">If true, type info is written to the MXML file.</param>
public MXmlBase SerializeMXml(bool isChildTemplate, bool isGenericTemplate = false, bool IncludeTypeInfo = false) {
Type type = GetType();
string typeName = type.Name != "NMSString0x20A" ? type.Name : "NMSString0x20";
MXmlBase xmlData = new MXmlProperty {};
Expand Down Expand Up @@ -1495,9 +1514,9 @@ public MXmlBase SerializeMXml(bool isChildTemplate, bool isGenericTemplate = fal
if ( field.IsInitOnly ) continue;

if ( isGenericTemplate ) {
subElement.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ) ) );
subElement.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ), IncludeTypeInfo ) );
} else {
xmlData.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ) ) );
xmlData.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ), IncludeTypeInfo ) );
}
}

Expand Down Expand Up @@ -1807,19 +1826,15 @@ public void WriteToMbin(string outputpath)
}
}

/// <summary>
/// Writes the NMSTemplate object to an .mxml file.
/// </summary>
/// <param name="outputpath">The location to write the .mxml file.</param>
public void WriteToMxml(string outputpath) => WriteToMxml(outputpath, false);
/// <summary>
/// Writes the NMSTemplate object to an .mxml file.
/// </summary>
/// <param name="outputpath">The location to write the .mxml file.</param>
/// <param name="hideVersionInfo">If true, version info is not written to the MXML file.</param>
public void WriteToMxml(string outputpath, bool hideVersionInfo)
/// <param name="IncludeTypeInfo">If true, type info is written to the MXML file.</param>
public void WriteToMxml(string outputpath, bool hideVersionInfo, bool IncludeTypeInfo)
{
var data = MXmlFile.WriteTemplate(this, hideVersionInfo);
var data = MXmlFile.WriteTemplate(this, hideVersionInfo, IncludeTypeInfo);
File.WriteAllText(outputpath, data);
}

Expand Down
Loading