From 323b378f827516b611c28713152e638fdb066b32 Mon Sep 17 00:00:00 2001 From: monkeyman192 Date: Sat, 27 Apr 2024 21:59:17 +1000 Subject: [PATCH] update header to version 3 --- MBINCompiler/Source/CommandLineOptions.cs | 2 +- .../Source/Tests/MBINHeaderTestsV0.cs | 20 ++--- .../Source/Tests/MBINHeaderTestsV1.cs | 20 ++--- .../Source/Tests/MBINHeaderTestsV2.cs | 22 ++--- libMBIN/Source/MBIN/MBINHeader.cs | 89 +++++++++++++++---- libMBIN/Source/Template/NMSAttribute.cs | 1 - libMBIN/Source/Template/NMSTemplate.cs | 18 ---- 7 files changed, 105 insertions(+), 67 deletions(-) diff --git a/MBINCompiler/Source/CommandLineOptions.cs b/MBINCompiler/Source/CommandLineOptions.cs index fcf012915..1c99767ef 100644 --- a/MBINCompiler/Source/CommandLineOptions.cs +++ b/MBINCompiler/Source/CommandLineOptions.cs @@ -26,7 +26,7 @@ public static class OptionBackers public static FormatType optOutputFormat = FormatType.Unknown; public static List optIncludeFilters = null; public static List optExcludeFilters = null; - public static HeaderFormat optFormatVersion = HeaderFormat.V2; + public static HeaderFormat optFormatVersion = HeaderFormat.V3; public static bool optUseThreads = true; } diff --git a/MBINCompilerTests/Source/Tests/MBINHeaderTestsV0.cs b/MBINCompilerTests/Source/Tests/MBINHeaderTestsV0.cs index 5bfc04ce9..c6827b5df 100644 --- a/MBINCompilerTests/Source/Tests/MBINHeaderTestsV0.cs +++ b/MBINCompilerTests/Source/Tests/MBINHeaderTestsV0.cs @@ -20,7 +20,7 @@ public class MBINHeaderTestsV0 { private static MBINHeader CreateMockHeader( uint magic = MBINHeader.MBIN_MAGIC, - uint formatID = MBINHeader.MBIN_VERSION, + uint formatID = MBINHeader.MBIN_VERSION_OLD, ulong timestamp = TIMESTAMP, ulong guid = TEMPLATE_GUID, string name = TEMPLATE_NAME, @@ -86,7 +86,7 @@ public void TestIsFormatV2() { [TestMethod] public void TestCreateHeaderCommon() { Assert.AreEqual( MBINHeader.MBIN_MAGIC, HeaderCommon.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderCommon.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderCommon.FormatID ); Assert.AreEqual( TIMESTAMP, HeaderCommon.Timestamp ); Assert.AreEqual( TEMPLATE_GUID, HeaderCommon.TemplateGUID ); Assert.AreEqual( TEMPLATE_NAME, HeaderCommon.TemplateName ); @@ -98,7 +98,7 @@ public void TestCreateHeaderCommon() { [TestMethod] public void TestCreateHeaderTkGeometryData() { Assert.AreEqual( MBINHeader.MBIN_MAGIC_PC, HeaderTkGeometryData.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderTkGeometryData.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderTkGeometryData.FormatID ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_TAG, HeaderTkGeometryData.Timestamp ); Assert.AreEqual( TKGEOMETRYDATA_GUID, HeaderTkGeometryData.TemplateGUID ); Assert.AreEqual( TEMPLATE_NAME, HeaderTkGeometryData.TemplateName ); @@ -110,7 +110,7 @@ public void TestCreateHeaderTkGeometryData() { [TestMethod] public void TestCreateHeaderTkAnimMetadata() { Assert.AreEqual( MBINHeader.MBIN_MAGIC, HeaderTkAnimMetadata.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderTkAnimMetadata.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderTkAnimMetadata.FormatID ); Assert.AreEqual( MBINHeader.TKANIMMETADATA_TAG, HeaderTkAnimMetadata.Timestamp ); Assert.AreEqual( TKANIMMETADATA_GUID, HeaderTkAnimMetadata.TemplateGUID ); Assert.AreEqual( TEMPLATE_NAME, HeaderTkAnimMetadata.TemplateName ); @@ -125,7 +125,7 @@ public void TestSetDefaultsV0Common() { header.SetDefaultsV0(); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( 0ul, header.Timestamp ); Assert.AreEqual( 0ul, header.TemplateGUID ); Assert.AreEqual( "", header.TemplateName ); @@ -140,7 +140,7 @@ public void TestSetDefaultsV0TkGeometry() { header.SetDefaultsV0( typeof( NMS.Toolkit.TkGeometryData ) ); Assert.AreEqual( MBINHeader.MBIN_MAGIC_PC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_TAG, header.Timestamp ); Assert.AreEqual( TKGEOMETRYDATA_GUID, header.TemplateGUID ); Assert.AreEqual( "", header.TemplateName ); @@ -155,7 +155,7 @@ public void TestSetDefaultsV0TkAnimMetadata() { header.SetDefaultsV0( typeof( NMS.Toolkit.TkAnimMetadata ) ); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKANIMMETADATA_TAG, header.Timestamp ); Assert.AreEqual( TKANIMMETADATA_GUID, header.TemplateGUID ); Assert.AreEqual( "", header.TemplateName ); @@ -170,7 +170,7 @@ public void TestSetDefaultsCommon() { header.SetDefaults( format: MBINHeader.Format.V0 ); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( 0ul, header.Timestamp ); Assert.AreEqual( 0ul, header.TemplateGUID ); Assert.AreEqual( "", header.TemplateName ); @@ -185,7 +185,7 @@ public void TestSetDefaultsTkGeometry() { header.SetDefaults( typeof( NMS.Toolkit.TkGeometryData ), MBINHeader.Format.V0 ); Assert.AreEqual( MBINHeader.MBIN_MAGIC_PC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_TAG, header.Timestamp ); Assert.AreEqual( TKGEOMETRYDATA_GUID, header.TemplateGUID ); Assert.AreEqual( "", header.TemplateName ); @@ -200,7 +200,7 @@ public void TestSetDefaultsTkAnimMetadata() { header.SetDefaults( typeof( NMS.Toolkit.TkAnimMetadata ), MBINHeader.Format.V0 ); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKANIMMETADATA_TAG, header.Timestamp ); Assert.AreEqual( TKANIMMETADATA_GUID, header.TemplateGUID ); Assert.AreEqual( "", header.TemplateName ); diff --git a/MBINCompilerTests/Source/Tests/MBINHeaderTestsV1.cs b/MBINCompilerTests/Source/Tests/MBINHeaderTestsV1.cs index d0729a846..990dd690b 100644 --- a/MBINCompilerTests/Source/Tests/MBINHeaderTestsV1.cs +++ b/MBINCompilerTests/Source/Tests/MBINHeaderTestsV1.cs @@ -27,7 +27,7 @@ public class MBINHeaderTestsV1 { private MBINHeader CreateMockHeader( uint magic = MBINHeader.MBIN_MAGIC, - uint formatID = MBINHeader.MBIN_VERSION, + uint formatID = MBINHeader.MBIN_VERSION_OLD, ulong tag = MBINHeader.MBINCVER_TAG, ulong version = ~0ul, string name = TEMPLATE_NAME, @@ -104,7 +104,7 @@ public void TestUlongToString() { [TestMethod] public void TestCreateHeaderCommon() { Assert.AreEqual( MBINHeader.MBIN_MAGIC, HeaderCommon.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderCommon.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderCommon.FormatID ); Assert.AreEqual( MBINHeader.MBINCVER_TAG, HeaderCommon.Timestamp ); Assert.AreEqual( VERSION_ID, HeaderCommon.TemplateGUID ); Assert.AreEqual( TEMPLATE_NAME, HeaderCommon.TemplateName ); @@ -116,7 +116,7 @@ public void TestCreateHeaderCommon() { [TestMethod] public void TestCreateHeaderTkGeometryData() { Assert.AreEqual( MBINHeader.MBIN_MAGIC_PC, HeaderTkGeometryData.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderTkGeometryData.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderTkGeometryData.FormatID ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_TAG, HeaderTkGeometryData.Timestamp ); Assert.AreEqual( TKGEOMETRYDATA_GUID, HeaderTkGeometryData.TemplateGUID ); Assert.AreEqual( TEMPLATE_NAME, HeaderTkGeometryData.TemplateName ); @@ -128,7 +128,7 @@ public void TestCreateHeaderTkGeometryData() { [TestMethod] public void TestCreateHeaderTkAnimMetadata() { Assert.AreEqual( MBINHeader.MBIN_MAGIC, HeaderTkAnimMetadata.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderTkAnimMetadata.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderTkAnimMetadata.FormatID ); Assert.AreEqual( MBINHeader.TKANIMMETADATA_TAG, HeaderTkAnimMetadata.Timestamp ); Assert.AreEqual( TKANIMMETADATA_GUID, HeaderTkAnimMetadata.TemplateGUID ); Assert.AreEqual( TEMPLATE_NAME, HeaderTkAnimMetadata.TemplateName ); @@ -141,7 +141,7 @@ public void TestSetDefaultsV1Common() { header.SetDefaultsV1(); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.MBINCVER_TAG, header.Tag ); Assert.AreEqual( VERSION_ID, header.MbinVersion ); Assert.AreEqual( "", header.TemplateName ); @@ -156,7 +156,7 @@ public void TestSetDefaultsV1TkGeometry() { header.SetDefaultsV1( typeof( NMS.Toolkit.TkGeometryData ) ); Assert.AreEqual( MBINHeader.MBIN_MAGIC_PC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_TAG, header.Tag ); Assert.AreEqual( TKGEOMETRYDATA_GUID, header.MbinVersion ); Assert.AreEqual( "", header.TemplateName ); @@ -171,7 +171,7 @@ public void TestSetDefaultsV1TkAnimMetadata() { header.SetDefaultsV1( typeof( NMS.Toolkit.TkAnimMetadata ) ); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKANIMMETADATA_TAG, header.Tag ); Assert.AreEqual( TKANIMMETADATA_GUID, header.MbinVersion ); Assert.AreEqual( "", header.TemplateName ); @@ -186,7 +186,7 @@ public void TestSetDefaultsCommon() { header.SetDefaults( format: MBINHeader.Format.V1 ); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.MBINCVER_TAG, header.Tag ); Assert.AreEqual( VERSION_ID, header.MbinVersion ); Assert.AreEqual( "", header.TemplateName ); @@ -201,7 +201,7 @@ public void TestSetDefaultsTkGeometry() { header.SetDefaults( typeof( NMS.Toolkit.TkGeometryData ), MBINHeader.Format.V1 ); Assert.AreEqual( MBINHeader.MBIN_MAGIC_PC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_TAG, header.Tag ); Assert.AreEqual( TKGEOMETRYDATA_GUID, header.MbinVersion ); Assert.AreEqual( "", header.TemplateName ); @@ -216,7 +216,7 @@ public void TestSetDefaultsTkAnimMetadata() { header.SetDefaults( typeof( NMS.Toolkit.TkAnimMetadata ), MBINHeader.Format.V1 ); Assert.AreEqual( MBINHeader.MBIN_MAGIC, header.MagicID ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatID ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatID ); Assert.AreEqual( MBINHeader.TKANIMMETADATA_TAG, header.Tag ); Assert.AreEqual( TKANIMMETADATA_GUID, header.MbinVersion ); Assert.AreEqual( "", header.TemplateName ); diff --git a/MBINCompilerTests/Source/Tests/MBINHeaderTestsV2.cs b/MBINCompilerTests/Source/Tests/MBINHeaderTestsV2.cs index 77cf7a11e..ff235185d 100644 --- a/MBINCompilerTests/Source/Tests/MBINHeaderTestsV2.cs +++ b/MBINCompilerTests/Source/Tests/MBINHeaderTestsV2.cs @@ -9,8 +9,8 @@ public class MBINHeaderTestsV2 { private const ushort FORMAT_V0 = 0; private const ushort FORMAT_V2 = 2; - private const uint FORMAT_ID0 = (MBINHeader.MBIN_VERSION & 0xFFFF) | (FORMAT_V0 & 0xFFFF) << 16; - private const uint FORMAT_ID2 = (MBINHeader.MBIN_VERSION & 0xFFFF) | (FORMAT_V2 & 0xFFFF) << 16; + private const uint FORMAT_ID0 = (MBINHeader.MBIN_VERSION_OLD & 0xFFFF) | (FORMAT_V0 & 0xFFFF) << 16; + private const uint FORMAT_ID2 = (MBINHeader.MBIN_VERSION_OLD & 0xFFFF) | (FORMAT_V2 & 0xFFFF) << 16; private static readonly System.Version NMS_VERSION = Version.NMSVersion; private static readonly System.Version API_VERSION = Version.AssemblyVersion; @@ -60,7 +60,7 @@ private MBINHeader CreateMockHeader( private MBINHeader HeaderCommon => CreateMockHeader(); private MBINHeader HeaderTkGeometryData => CreateMockHeader( magic: MBINHeader.MBIN_MAGIC_PC, - formatID: MBINHeader.MBIN_VERSION, + formatID: MBINHeader.MBIN_VERSION_OLD, versionID: MBINHeader.TKGEOMETRYDATA_TAG, guid: TKGEOMETRYDATA_GUID, metaOffset: MBINHeader.TKGEOMETRYDATA_PADDING @@ -105,7 +105,7 @@ public void TestCreateHeaderCommon() { Assert.AreEqual( TEMPLATE_NAME, HeaderCommon.TemplateName ); Assert.AreEqual( METAOFFSET, HeaderCommon.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderCommon.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderCommon.FormatNMS ); Assert.AreEqual( FORMAT_V2, HeaderCommon.FormatAPI ); Assert.AreEqual( NMS_VERSION_ID, HeaderCommon.VersionID_NMS ); @@ -124,7 +124,7 @@ public void TestCreateHeaderTkGeometryData() { Assert.AreEqual( TEMPLATE_NAME, HeaderTkGeometryData.TemplateName ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_PADDING, HeaderTkGeometryData.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, HeaderTkGeometryData.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, HeaderTkGeometryData.FormatNMS ); Assert.AreEqual( FORMAT_V0, HeaderTkGeometryData.FormatAPI ); } @@ -140,7 +140,7 @@ public void TestSetDefaultsV2Common() { Assert.AreEqual( "", header.TemplateName ); Assert.AreEqual( 0ul, header.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatNMS ); Assert.AreEqual( FORMAT_V2, header.FormatAPI ); Assert.AreEqual( NMS_VERSION_STRING, header.VersionNMS.ToString() ); @@ -159,7 +159,7 @@ public void TestSetDefaultsV2TkGeometry() { Assert.AreEqual( "", header.TemplateName ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_PADDING, header.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatNMS ); Assert.AreEqual( FORMAT_V0, header.FormatAPI ); } @@ -175,7 +175,7 @@ public void TestSetDefaultsCommon() { Assert.AreEqual( "", header.TemplateName ); Assert.AreEqual( 0ul, header.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatNMS ); Assert.AreEqual( FORMAT_V2, header.FormatAPI ); Assert.AreEqual( NMS_VERSION_STRING, header.VersionNMS.ToString() ); @@ -194,7 +194,7 @@ public void TestSetDefaultsTkGeometry() { Assert.AreEqual( "", header.TemplateName ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_PADDING, header.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatNMS ); Assert.AreEqual( FORMAT_V0, header.FormatAPI ); } @@ -210,7 +210,7 @@ public void TestSetDefaultsImplicitCommon() { Assert.AreEqual( "", header.TemplateName ); Assert.AreEqual( 0ul, header.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatNMS ); Assert.AreEqual( FORMAT_V2, header.FormatAPI ); Assert.AreEqual( NMS_VERSION_STRING, header.VersionNMS.ToString() ); @@ -229,7 +229,7 @@ public void TestSetDefaultsImplicitTkGeometry() { Assert.AreEqual( "", header.TemplateName ); Assert.AreEqual( MBINHeader.TKGEOMETRYDATA_PADDING, header.MetaOffset ); - Assert.AreEqual( MBINHeader.MBIN_VERSION, header.FormatNMS ); + Assert.AreEqual( MBINHeader.MBIN_VERSION_OLD, header.FormatNMS ); Assert.AreEqual( FORMAT_V0, header.FormatAPI ); } diff --git a/libMBIN/Source/MBIN/MBINHeader.cs b/libMBIN/Source/MBIN/MBINHeader.cs index cf10f4cbf..e74baac8d 100644 --- a/libMBIN/Source/MBIN/MBINHeader.cs +++ b/libMBIN/Source/MBIN/MBINHeader.cs @@ -1,6 +1,7 @@ using System; using System.Reflection; using System.Text; +using System.Globalization; namespace libMBIN { @@ -9,9 +10,10 @@ public class MBINHeader : NMSTemplate { internal const uint MBIN_MAGIC = 0xCCCCCCCC; // MBIN format ID internal const uint MBIN_MAGIC_PC = 0xDDDDDDDD; // only used by TkGeometryData and TkGeometryStreamData (*.MBIN.PC) + internal const uint MBIN_VERSION_OLD = 2500; // vanilla version // Note: The MBIN_VERSION used to be 2500, but as of the 14144058 steam version this changed to 3250, // presumably to indicate that the file format is indeed different (fields became ordered, probably for optimization reasons...) - internal const uint MBIN_VERSION = 3250; // vanilla version + internal const uint MBIN_VERSION = 3250; // vanilla version with ordered fields // used for format V1 internal const ulong MBINCVER_TAG = 0x726576434E49424D; // "revCNIBM" ("MBINCver") @@ -24,11 +26,11 @@ public class MBINHeader : NMSTemplate internal const ulong TKGEOMETRYDATA_TAG = 0xFFFFFFFFFFFFFFFF; // used by TkGeometryData and TkGeometryStreamData internal const ulong TKGEOMETRYDATA_PADDING = 0xFEFEFEFEFEFEFEFE; // used by TkGeometryData and TkGeometryStreamData - public enum Format { V0, V1, V2 } + public enum Format { V0, V1, V2, V3 } #region // Fields - /// Format V0, V1 and V2: + /// All formats: /// For *.MBIN files, this value is always 0xCCCCCCCC. /// For *.MBIN.PC files, this is always 0xDDDDDDDD. /// @@ -40,6 +42,8 @@ public enum Format { V0, V1, V2 } /// Format V2: /// Low short is NMS Format ID (always 2500) /// High short is libMBIN Format ID. (always 0 for format V0 and V1) + /// Format V3: + /// NMS MBIN format version (Always 3250) /// /* 0x04 */ public uint FormatID;// { get; set; } @@ -51,12 +55,12 @@ public enum Format { V0, V1, V2 } /// /* 0x08 */ public ulong Timestamp; - /// Format V0 and V2: + /// Format V0, V2 and V3: /// Unique across templates (files using the same template share the same GUID). /// /* 0x10 */ public ulong TemplateGUID; - /// Format V0, V1 and V2: + /// All formats: /// The name of the data template the MBIN file is using. /// Eg. "cGcTestMetadata" /// @@ -64,7 +68,10 @@ public enum Format { V0, V1, V2 } [NMS( Size = 0x40 )] // 64 bytes /* 0x18 */ public string TemplateName; - /// Format V0 and V1: Not used. + /// Format V0 and V1: Not used. + /// Format V2 and V3: + /// Full of non-zero padding bytes for anim and geometry files. + /// /* 0x58 */ public ulong EndPadding; /* Size = 0x60 */ @@ -90,7 +97,7 @@ public enum Format { V0, V1, V2 } #region // V2 Properties /// Format V2: - /// The low short from . (always 2500) + /// The low short from . (always 3250) /// /// /// @@ -175,13 +182,23 @@ public enum Format { V0, V1, V2 } // used for format V1 private string MbinVersionString; // Version of the mbin file as read initially as a string - public bool IsValid => ((MagicID == MBIN_MAGIC) || (MagicID == MBIN_MAGIC_PC)) && (FormatNMS == MBIN_VERSION); - - public int GetFormat() => IsFormatV0 ? 0 : IsFormatV1 ? 1 : FormatAPI; + public bool IsValid => ( + ((MagicID == MBIN_MAGIC) || (MagicID == MBIN_MAGIC_PC)) + && ((FormatNMS == MBIN_VERSION_OLD) || (FormatNMS == MBIN_VERSION)) + ); + + public int GetFormat() { + if (IsFormatV0) return 0; + if (IsFormatV1) return 1; + if (IsFormatV2) return 2; + if (IsFormatV3) return 3; + return -1; + } - public bool IsFormatV0 => (FormatAPI == 0) && (Tag != MBINCVER_TAG); - public bool IsFormatV1 => (FormatAPI == 0) && (Tag == MBINCVER_TAG); - public bool IsFormatV2 => (FormatAPI == 2); + public bool IsFormatV0 => (FormatAPI == 0) && (Tag != MBINCVER_TAG) && (FormatNMS == MBIN_VERSION_OLD); + public bool IsFormatV1 => (FormatAPI == 0) && (Tag == MBINCVER_TAG) && (FormatNMS == MBIN_VERSION_OLD); + public bool IsFormatV2 => (FormatAPI == 2) && (FormatNMS == MBIN_VERSION_OLD); + public bool IsFormatV3 => (FormatAPI == 0) && (FormatNMS == MBIN_VERSION); // remove the "c" (compiled?) from the start of the template name public string GetXMLTemplateName() { @@ -209,7 +226,7 @@ public System.Version GetMBINVersion() { public void SetDefaultsV0( Type type = null ) { // MBIN_MAGIC_PC is only used by TkGeometryData (*.MBIN.PC) MagicID = (type == typeof(NMS.Toolkit.TkGeometryData) | type == typeof(NMS.Toolkit.TkGeometryStreamData)) ? MBIN_MAGIC_PC : MBIN_MAGIC; - FormatID = MBIN_VERSION; + FormatID = MBIN_VERSION_OLD; Timestamp = 0; TemplateGUID = type?.GetCustomAttribute()?.GUID ?? 0; TemplateName = string.Empty; @@ -247,12 +264,52 @@ public void SetDefaultsV2( Type type = null ) { VersionAPI = Version.AssemblyVersion; } - public void SetDefaults( Type type = null, Format format = Format.V2 ) { + public void SetDefaultsV3( Type type = null ) { + // V3 differs from V2 in that the MBIN header version is no longer written + SetDefaultsV0( type ); + // We unfortunately can't write the `FormatAPI` value to the mbin file any more since it seems + // like HG is reading the MBIN format version as a uint32 now, so we don't have the extra 2 + // bytes to play with. + FormatID = MBIN_VERSION; + FormatAPI = 0; + VersionNMS = Version.NMSVersion; + VersionAPI = Version.AssemblyVersion; + } + + public void SetDefaults( Type type = null, Format format = Format.V3 ) { switch (format) { case Format.V0: SetDefaultsV0( type ); break; case Format.V1: SetDefaultsV1( type ); break; - default: SetDefaultsV2( type ); break; + case Format.V2: SetDefaultsV2( type ); break; + default: SetDefaultsV3( type ); break; + } + } + + public bool IsModded() { + if (Timestamp == TKGEOMETRYDATA_TAG || Timestamp == 0) { + // Note: This will not be correct for old files. Only V3 will return the right value. + return false; + } + + if (IsFormatV1 || IsFormatV2) { + // We can only detect V1 and V2 headers if we are modded. + return true; + } else if (IsFormatV3) { + DateTime dt = new DateTime(); + bool success = DateTime.TryParseExact( + Timestamp.ToString(), + "yyyyMMddHHmm", + CultureInfo.InvariantCulture, + DateTimeStyles.None, + out dt + ); + // If we correctly parsed the timestamp then we are vanilla since no header version + // writes a valid timestamp. + return !success; } + + // If we aren't sure, fall back to assuming we are not modded. + return false; } internal static ulong StringToUlong( string s ) { diff --git a/libMBIN/Source/Template/NMSAttribute.cs b/libMBIN/Source/Template/NMSAttribute.cs index a654ba317..cbab1da91 100644 --- a/libMBIN/Source/Template/NMSAttribute.cs +++ b/libMBIN/Source/Template/NMSAttribute.cs @@ -7,7 +7,6 @@ public class NMSAttribute : Attribute { public int Size { get; set; } public bool Ignore { get; set; } - public object DefaultValue { get; set; } [Obsolete( "Use EnumType instead." )] public string[] EnumValue { get; set; } public Type EnumType { get; set; } diff --git a/libMBIN/Source/Template/NMSTemplate.cs b/libMBIN/Source/Template/NMSTemplate.cs index e531b9b57..57b5ce5ec 100644 --- a/libMBIN/Source/Template/NMSTemplate.cs +++ b/libMBIN/Source/Template/NMSTemplate.cs @@ -583,7 +583,6 @@ public void SerializeValue( BinaryWriter writer, Type fieldType, object fieldDat if (CustomSerialize(writer, fieldType, fieldData, settings, field, ref additionalData, ref addtDataIndex)) return; - if ( settings?.DefaultValue != null ) fieldData = settings.DefaultValue; switch ( fieldType.Name ) { case "String": case "Byte[]": @@ -1019,9 +1018,6 @@ public EXmlBase SerializeEXmlValue(Type fieldType, FieldInfo field, NMSAttribute int i = 0; string valueString = String.Empty; - if ( settings?.DefaultValue != null ) { - value = settings.DefaultValue; - } switch (fieldType.Name) { @@ -1418,23 +1414,9 @@ public static NMSTemplate DeserializeEXml( EXmlBase xmlData ) { // this is th DebugLogComment( ((EXmlMeta) xmlData).Comment ); } - /* - DebugLog("Getting types"); - foreach (var property in xmlData.Elements) - { - DebugLog(property.GetType()); - }*/ - if (template == null) return null; Type templateType = template.GetType(); - var templateFields = templateType.GetFields().OrderBy(field => field.MetadataToken); // hack to get fields in order of declaration (todo: use something less hacky, this might break mono?) - - foreach (var templateField in templateFields) { - // check to see if the object has a default value in the struct - NMSAttribute settings = templateField.GetCustomAttribute(); - if (settings?.DefaultValue != null) templateField.SetValue(template, settings.DefaultValue); - } using ( var indentScope = new Logger.IndentScope() ) {