Skip to content

Commit d94a754

Browse files
authored
Merge pull request #674 from HolterPhylo/ArraySizeInfo
Add array size info as part of optional --typed cli argument
2 parents 6094ece + f4bb7b2 commit d94a754

File tree

6 files changed

+52
-23
lines changed

6 files changed

+52
-23
lines changed

MBINCompiler/Source/CommandLineOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public static class OptionBackers
2828
public static List<string> optExcludeFilters = null;
2929
public static HeaderFormat optFormatVersion = HeaderFormat.V4;
3030
public static bool optUseThreads = true;
31+
public static bool optTyped = false;
3132
}
3233

3334
// --debug
@@ -80,6 +81,9 @@ internal set {
8081
// --no-threads
8182
public static bool UseThreads { get => optUseThreads; internal set => optUseThreads = value; }
8283

84+
// --typed
85+
public static bool IncludeTypeInfo { get => optTyped; internal set => optTyped = value; }
86+
8387
public static readonly List<Option> OPTIONS_GENERAL = new List<Option> {
8488
new Option { shortName = 'q', longName = "quiet",
8589
description = "Do not display any console messages.\n" +
@@ -155,6 +159,8 @@ internal set {
155159
new Option { longName = "no-threads", isHidden = true, description = "Disable multi-threading." },
156160

157161
new Option { longName = "stream", description = "Enable sending MXML to Console." },
162+
163+
new Option { longName = "typed", description = "Enable type information in MXML." },
158164
};
159165

160166
public static readonly List<Option> OPTIONS_LIST = new List<Option> {

MBINCompiler/Source/Commands/Convert.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private static string ConvertMBIN( string inputPath, FileStream fIn, MemoryStrea
218218
if ( data is null ) throw new InvalidDataException( "Invalid MBIN data." );
219219

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

223223
if ( StreamToConsole ) {
224224
EmitInfo($"");

MBINCompiler/Source/Commands/ConvertMode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public override int ExecuteCommand( CommandLineParser options ) {
1919
UseThreads = !options.GetOptionSwitch( "no-threads" );
2020
StreamToConsole = options.GetOptionSwitch("stream");
2121
HideVersionInfo = options.GetOptionSwitch("no-version");
22+
IncludeTypeInfo = options.GetOptionSwitch( "typed" );
2223

2324
var arg = options.GetOptionArg( "format-version" );
2425
if ( arg != null ) {

libMBIN/Source/MXML/MXmlFile.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,16 @@ public static MXmlData ReadMXmlDataFromString(string xml)
9090
/// Writes the NMSTemplate object to an .mxml file.
9191
/// </summary>
9292
/// <param name="outputpath">The location to write the .mxml file.</param>
93-
/// <param name="hideVersionInfo">version info is written to the MXML file.</param>
94-
public static string WriteTemplate(NMSTemplate template) => WriteTemplate(template, false);
93+
/// <param name="hideVersionInfo">If true, version info is not written to the MXML file.</param>
94+
/// <param name="includeTypeInfo">If true, type info is written to the MXML file.</param>
95+
public static string WriteTemplate(NMSTemplate template) => WriteTemplate(template, false, false);
9596
/// <summary>
9697
/// Writes the NMSTemplate object to an .mxml file.
9798
/// </summary>
9899
/// <param name="outputpath">The location to write the .mxml file.</param>
99100
/// <param name="hideVersionInfo">If true, version info is not written to the MXML file.</param>
100-
public static string WriteTemplate(NMSTemplate template, bool hideVersionInfo)
101+
/// <param name="includeTypeInfo">If true, type info is written to the MXML file.</param>
102+
public static string WriteTemplate(NMSTemplate template, bool hideVersionInfo, bool includeTypeInfo)
101103
{
102104
var origCulture = Thread.CurrentThread.CurrentCulture;
103105
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
@@ -114,7 +116,7 @@ public static string WriteTemplate(NMSTemplate template, bool hideVersionInfo)
114116
var ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
115117
string str_ver = $"{ver.Major}.{ver.Minor:00}.{ver.Build}.{ver.Revision}";
116118
if ( !hideVersionInfo ) xmlTextWriter.WriteComment($"File created using MBINCompiler version ({str_ver})");
117-
var data = template.SerializeMXml(false);
119+
var data = template.SerializeMXml(false, false, includeTypeInfo);
118120
Serializer.Serialize(xmlTextWriter, data, Namespaces);
119121
xmlTextWriter.Flush();
120122

libMBIN/Source/MXML/MXmlProperty.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class MXmlProperty : MXmlBase
99
public string Value { get; set; }
1010
[XmlAttribute("linked")]
1111
public string Linked { get; set; }
12+
[XmlAttribute("array_size")]
13+
public string ArraySize { get; set; }
1214
[XmlAttribute("_id")]
1315
public string ID { get; set; }
1416
[XmlAttribute("_index")]
@@ -26,6 +28,9 @@ public override string ToString()
2628
if (this.Linked != null) {
2729
result += $" linked=\"{this.Linked}\"";
2830
}
31+
if (this.ArraySize != null) {
32+
result += $" array_size=\"{this.ArraySize}\"";
33+
}
2934
result += ">";
3035
return result;
3136
}

libMBIN/Source/Template/NMSTemplate.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,16 @@ public byte[] SerializeBytes() {
11841184
return stream.ToArray();
11851185
}
11861186
}
1187-
public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute settings, object value, bool isField = true)
1187+
1188+
/// <summary>
1189+
/// .
1190+
/// </summary>
1191+
/// <param name="fieldType">The field type of the field.</param>
1192+
/// <param name="field">A field.</param>
1193+
/// <param name="settings">The settings of the field.</param>
1194+
/// <param name="value">The value of the field.</param>
1195+
/// <param name="IncludeTypeInfo">If true, type info is written to the MXML file.</param>
1196+
public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute settings, object value, bool IncludeTypeInfo)
11881197
{
11891198
string t = fieldType.Name;
11901199
int i = 0;
@@ -1237,13 +1246,13 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
12371246
case "Colour32":
12381247
// Handle the Colour32 explicitly since we want to write floats to the MXML, not ints.
12391248
Colour colour = new Colour((Colour32)value);
1240-
MXmlProperty colour_field = (MXmlProperty)colour.SerializeMXml( true );
1249+
MXmlProperty colour_field = (MXmlProperty)colour.SerializeMXml( true, false, IncludeTypeInfo );
12411250
colour_field.Name = fieldName;
12421251
return colour_field;
12431252
case "LinkableNMSTemplate":
12441253
LinkableNMSTemplate linkedTemplate = (LinkableNMSTemplate) value;
12451254
if (linkedTemplate.Template != null) {
1246-
MXmlProperty templateXmlData = (MXmlProperty)linkedTemplate.Template.SerializeMXml( true, true );
1255+
MXmlProperty templateXmlData = (MXmlProperty)linkedTemplate.Template.SerializeMXml( true, true, IncludeTypeInfo );
12471256
templateXmlData.Name = fieldName;
12481257
templateXmlData.Value = linkedTemplate.Template.GetType().Name;
12491258
if (linkedTemplate.Linked.StringValue() != "") {
@@ -1280,7 +1289,7 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
12801289
} else {
12811290
Dictionary<string, uint> IdCounter = new Dictionary<string, uint>{};
12821291
foreach ( var template in templates ) {
1283-
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( listType, field, settings, template, false );
1292+
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( listType, field, settings, template, IncludeTypeInfo );
12841293
data.Name = fieldName;
12851294
string typeIdField = TypeHasID(listType);
12861295
if (typeIdField != null) {
@@ -1310,7 +1319,7 @@ public MXmlBase SerializeMXmlValue(Type fieldType, FieldInfo field, NMSAttribute
13101319
if ( value != null ) {
13111320
NMSTemplate template = (NMSTemplate) value;
13121321

1313-
MXmlProperty templateXmlData = (MXmlProperty)template.SerializeMXml( true, true );
1322+
MXmlProperty templateXmlData = (MXmlProperty)template.SerializeMXml( true, true, IncludeTypeInfo );
13141323
templateXmlData.Name = fieldName;
13151324
templateXmlData.Value = template.GetType().Name;
13161325

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

13371346
foreach ( var template in (IEnumerable)value ) {
1338-
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( hashMapType, field, settings, template, false );
1347+
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( hashMapType, field, settings, template, IncludeTypeInfo );
13391348

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

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

1379+
if (IncludeTypeInfo) {
1380+
arrayProperty.ArraySize = field.GetCustomAttribute<NMSAttribute>()?.Size.ToString();
1381+
}
1382+
13701383
Array array = (Array) value;
13711384
string[] names = GetEnumNames( field.Name, array.Length, settings );
13721385
i = 0;
13731386
foreach ( var template in array ) {
1374-
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( arrayType, field, settings, template, false );
1387+
MXmlProperty data = (MXmlProperty)SerializeMXmlValue( arrayType, field, settings, template, IncludeTypeInfo );
13751388
// Only change the name if we have an associated enum.
13761389
string overwriteName = names[i];
13771390
if (overwriteName != null && overwriteName != "") {
@@ -1467,7 +1480,13 @@ private static int GetArrayLength( string fieldName, NMSAttribute settings ) {
14671480
return GetEnumNames( fieldName, settings ).Length;
14681481
}
14691482

1470-
public MXmlBase SerializeMXml(bool isChildTemplate, bool isGenericTemplate = false) {
1483+
/// <summary>
1484+
/// .
1485+
/// </summary>
1486+
/// <param name="isChildTemplate">If true, </param>
1487+
/// <param name="isGenericTemplate">If true, </param>
1488+
/// <param name="IncludeTypeInfo">If true, type info is written to the MXML file.</param>
1489+
public MXmlBase SerializeMXml(bool isChildTemplate, bool isGenericTemplate = false, bool IncludeTypeInfo = false) {
14711490
Type type = GetType();
14721491
string typeName = type.Name != "NMSString0x20A" ? type.Name : "NMSString0x20";
14731492
MXmlBase xmlData = new MXmlProperty {};
@@ -1495,9 +1514,9 @@ public MXmlBase SerializeMXml(bool isChildTemplate, bool isGenericTemplate = fal
14951514
if ( field.IsInitOnly ) continue;
14961515

14971516
if ( isGenericTemplate ) {
1498-
subElement.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ) ) );
1517+
subElement.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ), IncludeTypeInfo ) );
14991518
} else {
1500-
xmlData.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ) ) );
1519+
xmlData.Elements.Add( SerializeMXmlValue( field.FieldType, field, settings, field.GetValue( this ), IncludeTypeInfo ) );
15011520
}
15021521
}
15031522

@@ -1807,19 +1826,15 @@ public void WriteToMbin(string outputpath)
18071826
}
18081827
}
18091828

1810-
/// <summary>
1811-
/// Writes the NMSTemplate object to an .mxml file.
1812-
/// </summary>
1813-
/// <param name="outputpath">The location to write the .mxml file.</param>
1814-
public void WriteToMxml(string outputpath) => WriteToMxml(outputpath, false);
18151829
/// <summary>
18161830
/// Writes the NMSTemplate object to an .mxml file.
18171831
/// </summary>
18181832
/// <param name="outputpath">The location to write the .mxml file.</param>
18191833
/// <param name="hideVersionInfo">If true, version info is not written to the MXML file.</param>
1820-
public void WriteToMxml(string outputpath, bool hideVersionInfo)
1834+
/// <param name="IncludeTypeInfo">If true, type info is written to the MXML file.</param>
1835+
public void WriteToMxml(string outputpath, bool hideVersionInfo, bool IncludeTypeInfo)
18211836
{
1822-
var data = MXmlFile.WriteTemplate(this, hideVersionInfo);
1837+
var data = MXmlFile.WriteTemplate(this, hideVersionInfo, IncludeTypeInfo);
18231838
File.WriteAllText(outputpath, data);
18241839
}
18251840

0 commit comments

Comments
 (0)