{"TotalCount":30,"Files":[{"Ident":"subzerostudios.gmodassetimporter","Path":"Code/Assembly.cs","FileName":"Assembly.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":422538,"IsPrivate":false,"Code":"global using Sandbox;\r\nglobal using System;\r\nglobal using System.Collections.Generic;\r\nglobal using System.Linq;\r\n\r\n// Runtime assembly intentionally empty \u2014 conversion lives in Editor.\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/ConversionModels.cs","FileName":"ConversionModels.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.Collections.Generic;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\n/// \u003Csummary\u003E\r\n/// Shared options for a GMod \u2192 s\u0026box conversion run.\r\n/// \u003C/summary\u003E\r\npublic sealed class ConversionOptions\r\n{\r\n\t/// \u003Csummary\u003EGMod content root (folder that contains models/ and materials/), e.g. .../garrysmod.\u003C/summary\u003E\r\n\tpublic string GmodRoot { get; set; } = \u0022\u0022;\r\n\r\n\t/// \u003Csummary\u003EOutput Assets root (folder that will contain models/ and materials/).\u003C/summary\u003E\r\n\tpublic string OutputRoot { get; set; } = \u0022\u0022;\r\n\r\n\t/// \u003Csummary\u003EPreserve Source-relative models/... and materials/... paths under the output root.\u003C/summary\u003E\r\n\tpublic bool PreservePaths { get; set; } = true;\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Log Source sequence names during convert. Real AnimFile clips need FBX (SMD AnimFile is rejected by ModelDoc).\r\n\t/// Mesh always gets AnimBindPose.\r\n\t/// \u003C/summary\u003E\r\n\tpublic bool ExportAnimations { get; set; } = false;\r\n\r\n\t/// \u003Csummary\u003EWhen true, folder input also includes every subfolder (recursive).\u003C/summary\u003E\r\n\tpublic bool Recursive { get; set; } = true;\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// When false, models still reference their expected .vmat paths but no VMAT/PNG files are written,\r\n\t/// and folder batches skip loose .vtf textures.\r\n\t/// \u003C/summary\u003E\r\n\tpublic bool ImportMaterials { get; set; } = true;\r\n\r\n\t/// \u003Csummary\u003EWhen false, .mdl files are skipped (materials-only run).\u003C/summary\u003E\r\n\tpublic bool ImportModels { get; set; } = true;\r\n\r\n\t/// \u003Csummary\u003EOptional global scale applied via ModelModifier_ScaleAndMirror.\u003C/summary\u003E\r\n\tpublic float ModelScale { get; set; } = 1.0f;\r\n\r\n\tpublic Action\u003Cstring\u003E OnLog { get; set; } = _ =\u003E { };\r\n}\r\n\r\npublic sealed class ConversionResult\r\n{\r\n\tpublic bool Success { get; set; }\r\n\tpublic string Message { get; set; } = \u0022\u0022;\r\n\tpublic List\u003Cstring\u003E WrittenFiles { get; } = new();\r\n\tpublic List\u003Cstring\u003E Warnings { get; } = new();\r\n}\r\n\r\npublic sealed class Vec3\r\n{\r\n\tpublic float X, Y, Z;\r\n\tpublic Vec3() { }\r\n\tpublic Vec3( float x, float y, float z ) { X = x; Y = y; Z = z; }\r\n\tpublic static Vec3 Zero =\u003E new( 0, 0, 0 );\r\n}\r\n\r\npublic sealed class Vec2\r\n{\r\n\tpublic float X, Y;\r\n\tpublic Vec2() { }\r\n\tpublic Vec2( float x, float y ) { X = x; Y = y; }\r\n}\r\n\r\npublic sealed class Quat\r\n{\r\n\tpublic float X, Y, Z, W = 1;\r\n\tpublic Quat() { }\r\n\tpublic Quat( float x, float y, float z, float w ) { X = x; Y = y; Z = z; W = w; }\r\n\tpublic static Quat Identity =\u003E new( 0, 0, 0, 1 );\r\n}\r\n\r\npublic sealed class BoneWeight\r\n{\r\n\tpublic int Bone;\r\n\tpublic float Weight;\r\n}\r\n\r\npublic sealed class MeshVertex\r\n{\r\n\tpublic Vec3 Position = Vec3.Zero;\r\n\tpublic Vec3 Normal = new( 0, 0, 1 );\r\n\tpublic Vec2 Uv = new();\r\n\tpublic List\u003CBoneWeight\u003E Weights { get; } = new();\r\n}\r\n\r\npublic sealed class MeshTriangle\r\n{\r\n\tpublic int A, B, C;\r\n\tpublic string MaterialName = \u0022\u0022;\r\n}\r\n\r\npublic sealed class BoneData\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic int Parent = -1;\r\n\tpublic Vec3 Position = Vec3.Zero;\r\n\tpublic Vec3 Rotation = Vec3.Zero; // degrees (SMD-friendly)\r\n\tpublic Quat Quaternion = Quat.Identity;\r\n}\r\n\r\npublic sealed class AttachmentData\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic string ParentBone = \u0022\u0022;\r\n\tpublic Vec3 Origin = Vec3.Zero;\r\n\tpublic Vec3 Angles = Vec3.Zero;\r\n}\r\n\r\npublic sealed class ImporterBodyGroupChoice\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic List\u003Cstring\u003E MeshNames { get; } = new();\r\n}\r\n\r\npublic sealed class ImporterBodyGroup\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic List\u003CImporterBodyGroupChoice\u003E Choices { get; } = new();\r\n}\r\n\r\npublic sealed class MaterialRemap\r\n{\r\n\tpublic string From = \u0022\u0022;\r\n\tpublic string To = \u0022\u0022;\r\n}\r\n\r\npublic sealed class MaterialGroupData\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic List\u003CMaterialRemap\u003E Remaps { get; } = new();\r\n}\r\n\r\npublic sealed class AnimationClip\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic string FileName = \u0022\u0022;\r\n\tpublic float FrameRate = 30;\r\n\tpublic bool Looping;\r\n}\r\n\r\npublic sealed class MeshExport\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic string FileName = \u0022\u0022;\r\n\tpublic List\u003CMeshVertex\u003E Vertices { get; } = new();\r\n\tpublic List\u003CMeshTriangle\u003E Triangles { get; } = new();\r\n}\r\n\r\npublic sealed class ModelBuildContext\r\n{\r\n\tpublic string ModelName = \u0022\u0022;\r\n\tpublic string ModelAssetDirectory = \u0022\u0022; // e.g. models/foo/bar\r\n\tpublic List\u003CBoneData\u003E Bones { get; } = new();\r\n\tpublic List\u003CMeshExport\u003E Meshes { get; } = new();\r\n\tpublic List\u003CAttachmentData\u003E Attachments { get; } = new();\r\n\tpublic List\u003CImporterBodyGroup\u003E BodyGroups { get; } = new();\r\n\tpublic List\u003CMaterialRemap\u003E MaterialRemaps { get; } = new();\r\n\tpublic List\u003CMaterialGroupData\u003E MaterialGroups { get; } = new();\r\n\tpublic List\u003CAnimationClip\u003E Animations { get; } = new();\r\n\tpublic float Scale = 1.0f;\r\n\tpublic bool IncludeBindPose = true;\r\n}\r\n\r\npublic sealed class MaterialBuildResult\r\n{\r\n\tpublic string SourceMaterialToken = \u0022\u0022;\r\n\tpublic string VmatAssetPath = \u0022\u0022; // materials/.../name.vmat\r\n\tpublic bool UsedFallback;\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Source1/VtxReader.cs","FileName":"VtxReader.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\npublic sealed class VtxFile\r\n{\r\n\tpublic List\u003CVtxBodyPart\u003E BodyParts { get; } = new();\r\n}\r\n\r\npublic sealed class VtxBodyPart\r\n{\r\n\tpublic List\u003CVtxModel\u003E Models { get; } = new();\r\n}\r\n\r\npublic sealed class VtxModel\r\n{\r\n\tpublic List\u003CVtxLod\u003E Lods { get; } = new();\r\n}\r\n\r\npublic sealed class VtxLod\r\n{\r\n\tpublic List\u003CVtxMesh\u003E Meshes { get; } = new();\r\n}\r\n\r\npublic sealed class VtxMesh\r\n{\r\n\tpublic List\u003Cint\u003E Indices { get; } = new(); // triangle list indices into model vertex range\r\n}\r\n\r\n/// \u003Csummary\u003E\r\n/// VTX reader \u2014 uses the strip-group index buffer as a triangle list (same as the first\r\n/// working import). Fancy strip expansion proved less reliable on INS2 weapon VTXs.\r\n/// \u003C/summary\u003E\r\npublic static class VtxReader\r\n{\r\n\tpublic static string FindVtxPath( string mdlPath )\r\n\t{\r\n\t\tvar dir = Path.GetDirectoryName( mdlPath )!;\r\n\t\tvar name = Path.GetFileNameWithoutExtension( mdlPath );\r\n\t\tvar candidates = new[]\r\n\t\t{\r\n\t\t\tPath.Combine( dir, name \u002B \u0022.dx90.vtx\u0022 ),\r\n\t\t\tPath.Combine( dir, name \u002B \u0022.dx80.vtx\u0022 ),\r\n\t\t\tPath.Combine( dir, name \u002B \u0022.sw.vtx\u0022 ),\r\n\t\t\tPath.Combine( dir, name \u002B \u0022.vtx\u0022 )\r\n\t\t};\r\n\t\treturn PathResolve.FindExistingFile( candidates );\r\n\t}\r\n\r\n\tpublic static VtxFile Load( string path ) =\u003E Load( File.ReadAllBytes( path ) );\r\n\r\n\tpublic static VtxFile Load( byte[] data )\r\n\t{\r\n\t\tusing var ms = new MemoryStream( data );\r\n\t\tusing var br = new BinaryReader( ms );\r\n\r\n\t\tvar version = br.ReadInt32();\r\n\t\tif ( version != 7 )\r\n\t\t\tthrow new InvalidDataException( $\u0022Unsupported VTX version {version}\u0022 );\r\n\r\n\t\tbr.ReadInt32(); // vertCacheSize\r\n\t\tbr.ReadUInt16(); // maxBonesPerStrip\r\n\t\tbr.ReadUInt16(); // maxBonesPerTri\r\n\t\tbr.ReadInt32(); // maxBonesPerVert\r\n\t\tbr.ReadInt32(); // checkSum\r\n\t\tbr.ReadInt32(); // numLODs\r\n\t\tvar materialReplacementListOffset = br.ReadInt32();\r\n\t\tvar numBodyParts = br.ReadInt32();\r\n\t\tvar bodyPartOffset = br.ReadInt32();\r\n\t\t_ = materialReplacementListOffset;\r\n\r\n\t\tvar file = new VtxFile();\r\n\t\tfor ( var bp = 0; bp \u003C numBodyParts; bp\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar bpPos = bodyPartOffset \u002B bp * 8;\r\n\t\t\tbr.BaseStream.Position = bpPos;\r\n\t\t\tvar numModels = br.ReadInt32();\r\n\t\t\tvar modelOffset = br.ReadInt32();\r\n\r\n\t\t\tvar body = new VtxBodyPart();\r\n\t\t\tfor ( var m = 0; m \u003C numModels; m\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar modelPos = bpPos \u002B modelOffset \u002B m * 8;\r\n\t\t\t\tbr.BaseStream.Position = modelPos;\r\n\t\t\t\tvar numLods = br.ReadInt32();\r\n\t\t\t\tvar lodOffset = br.ReadInt32();\r\n\r\n\t\t\t\tvar model = new VtxModel();\r\n\t\t\t\tif ( numLods \u003E 0 )\r\n\t\t\t\t{\r\n\t\t\t\t\tvar lodPos = modelPos \u002B lodOffset;\r\n\t\t\t\t\tbr.BaseStream.Position = lodPos;\r\n\t\t\t\t\tvar numMeshes = br.ReadInt32();\r\n\t\t\t\t\tvar meshOffset = br.ReadInt32();\r\n\t\t\t\t\tbr.ReadSingle(); // switchPoint\r\n\r\n\t\t\t\t\tvar lod = new VtxLod();\r\n\t\t\t\t\tfor ( var me = 0; me \u003C numMeshes; me\u002B\u002B )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tvar meshPos = lodPos \u002B meshOffset \u002B me * 9;\r\n\t\t\t\t\t\tbr.BaseStream.Position = meshPos;\r\n\t\t\t\t\t\tvar numStripGroups = br.ReadInt32();\r\n\t\t\t\t\t\tvar stripGroupOffset = br.ReadInt32();\r\n\t\t\t\t\t\tbr.ReadByte();\r\n\r\n\t\t\t\t\t\tvar mesh = new VtxMesh();\r\n\t\t\t\t\t\tfor ( var sg = 0; sg \u003C numStripGroups; sg\u002B\u002B )\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\tvar sgPos = meshPos \u002B stripGroupOffset \u002B sg * 25;\r\n\t\t\t\t\t\t\tbr.BaseStream.Position = sgPos;\r\n\t\t\t\t\t\t\tvar numVerts = br.ReadInt32();\r\n\t\t\t\t\t\t\tvar vertOffset = br.ReadInt32();\r\n\t\t\t\t\t\t\tvar numIndices = br.ReadInt32();\r\n\t\t\t\t\t\t\tvar indexOffset = br.ReadInt32();\r\n\t\t\t\t\t\t\tbr.ReadInt32(); // numStrips\r\n\t\t\t\t\t\t\tbr.ReadInt32(); // stripOffset\r\n\t\t\t\t\t\t\tbr.ReadByte(); // flags\r\n\r\n\t\t\t\t\t\t\tvar vertMap = new int[numVerts];\r\n\t\t\t\t\t\t\tbr.BaseStream.Position = sgPos \u002B vertOffset;\r\n\t\t\t\t\t\t\tfor ( var v = 0; v \u003C numVerts; v\u002B\u002B )\r\n\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\tbr.ReadByte(); br.ReadByte(); br.ReadByte();\r\n\t\t\t\t\t\t\t\tbr.ReadByte();\r\n\t\t\t\t\t\t\t\tvertMap[v] = br.ReadUInt16();\r\n\t\t\t\t\t\t\t\tbr.ReadByte(); br.ReadByte(); br.ReadByte();\r\n\t\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\t\tbr.BaseStream.Position = sgPos \u002B indexOffset;\r\n\t\t\t\t\t\t\tfor ( var i = 0; i \u003C numIndices; i\u002B\u002B )\r\n\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\tvar idx = br.ReadUInt16();\r\n\t\t\t\t\t\t\t\tif ( idx \u003C vertMap.Length )\r\n\t\t\t\t\t\t\t\t\tmesh.Indices.Add( vertMap[idx] );\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\r\n\t\t\t\t\t\tlod.Meshes.Add( mesh );\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tmodel.Lods.Add( lod );\r\n\t\t\t\t}\r\n\r\n\t\t\t\tbody.Models.Add( model );\r\n\t\t\t}\r\n\r\n\t\t\tfile.BodyParts.Add( body );\r\n\t\t}\r\n\r\n\t\treturn file;\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Writers/PngWriter.cs","FileName":"PngWriter.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.IO;\r\nusing System.IO.Compression;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\n/// \u003Csummary\u003E\r\n/// Minimal RGBA8 PNG writer (no external deps).\r\n/// \u003C/summary\u003E\r\npublic static class PngWriter\r\n{\r\n\tstatic readonly byte[] Signature = { 137, 80, 78, 71, 13, 10, 26, 10 };\r\n\r\n\tpublic static void Write( string path, int width, int height, byte[] rgba )\r\n\t{\r\n\t\tDirectory.CreateDirectory( Path.GetDirectoryName( path )! );\r\n\t\tusing var fs = File.Create( path );\r\n\t\tfs.Write( Signature );\r\n\r\n\t\tWriteChunk( fs, \u0022IHDR\u0022, BuildIhdr( width, height ) );\r\n\t\tWriteChunk( fs, \u0022IDAT\u0022, BuildIdat( width, height, rgba ) );\r\n\t\tWriteChunk( fs, \u0022IEND\u0022, Array.Empty\u003Cbyte\u003E() );\r\n\t}\r\n\r\n\tstatic byte[] BuildIhdr( int width, int height )\r\n\t{\r\n\t\tusing var ms = new MemoryStream( 13 );\r\n\t\tusing var bw = new BinaryWriter( ms );\r\n\t\tWriteInt32Be( bw, width );\r\n\t\tWriteInt32Be( bw, height );\r\n\t\tbw.Write( (byte)8 ); // bit depth\r\n\t\tbw.Write( (byte)6 ); // RGBA\r\n\t\tbw.Write( (byte)0 );\r\n\t\tbw.Write( (byte)0 );\r\n\t\tbw.Write( (byte)0 );\r\n\t\treturn ms.ToArray();\r\n\t}\r\n\r\n\tstatic byte[] BuildIdat( int width, int height, byte[] rgba )\r\n\t{\r\n\t\tvar raw = new byte[(width * 4 \u002B 1) * height];\r\n\t\tfor ( var y = 0; y \u003C height; y\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar rowStart = y * (width * 4 \u002B 1);\r\n\t\t\traw[rowStart] = 0; // filter none\r\n\t\t\tBuffer.BlockCopy( rgba, y * width * 4, raw, rowStart \u002B 1, width * 4 );\r\n\t\t}\r\n\r\n\t\tusing var ms = new MemoryStream();\r\n\t\t// zlib wrapper: CMF/FLG \u002B deflate \u002B adler32\r\n\t\tms.WriteByte( 0x78 );\r\n\t\tms.WriteByte( 0x01 );\r\n\t\tusing ( var deflate = new DeflateStream( ms, CompressionLevel.Optimal, leaveOpen: true ) )\r\n\t\t{\r\n\t\t\tdeflate.Write( raw, 0, raw.Length );\r\n\t\t}\r\n\t\tvar adler = Adler32( raw );\r\n\t\tms.WriteByte( (byte)((adler \u003E\u003E 24) \u0026 0xFF) );\r\n\t\tms.WriteByte( (byte)((adler \u003E\u003E 16) \u0026 0xFF) );\r\n\t\tms.WriteByte( (byte)((adler \u003E\u003E 8) \u0026 0xFF) );\r\n\t\tms.WriteByte( (byte)(adler \u0026 0xFF) );\r\n\t\treturn ms.ToArray();\r\n\t}\r\n\r\n\tstatic void WriteChunk( Stream stream, string type, byte[] data )\r\n\t{\r\n\t\tusing var ms = new MemoryStream();\r\n\t\tusing var bw = new BinaryWriter( ms );\r\n\t\tWriteInt32Be( bw, data.Length );\r\n\t\tvar typeBytes = System.Text.Encoding.ASCII.GetBytes( type );\r\n\t\tbw.Write( typeBytes );\r\n\t\tbw.Write( data );\r\n\t\tvar bytes = ms.ToArray();\r\n\t\tstream.Write( bytes, 0, bytes.Length );\r\n\r\n\t\tvar crc = 0xFFFFFFFFu;\r\n\t\tcrc = Crc32Update( crc, typeBytes, 0, typeBytes.Length );\r\n\t\tcrc = Crc32Update( crc, data, 0, data.Length );\r\n\t\tspanWriteInt32Be( stream, (int)(crc ^ 0xFFFFFFFFu) );\r\n\t}\r\n\r\n\tstatic void spanWriteInt32Be( Stream stream, int value )\r\n\t{\r\n\t\tstream.WriteByte( (byte)((value \u003E\u003E 24) \u0026 0xFF) );\r\n\t\tstream.WriteByte( (byte)((value \u003E\u003E 16) \u0026 0xFF) );\r\n\t\tstream.WriteByte( (byte)((value \u003E\u003E 8) \u0026 0xFF) );\r\n\t\tstream.WriteByte( (byte)(value \u0026 0xFF) );\r\n\t}\r\n\r\n\tstatic void WriteInt32Be( BinaryWriter bw, int value )\r\n\t{\r\n\t\tbw.Write( (byte)((value \u003E\u003E 24) \u0026 0xFF) );\r\n\t\tbw.Write( (byte)((value \u003E\u003E 16) \u0026 0xFF) );\r\n\t\tbw.Write( (byte)((value \u003E\u003E 8) \u0026 0xFF) );\r\n\t\tbw.Write( (byte)(value \u0026 0xFF) );\r\n\t}\r\n\r\n\tstatic uint Adler32( byte[] data )\r\n\t{\r\n\t\tuint a = 1, b = 0;\r\n\t\tforeach ( var t in data )\r\n\t\t{\r\n\t\t\ta = (a \u002B t) % 65521;\r\n\t\t\tb = (b \u002B a) % 65521;\r\n\t\t}\r\n\t\treturn (b \u003C\u003C 16) | a;\r\n\t}\r\n\r\n\tstatic readonly uint[] CrcTable = CreateCrcTable();\r\n\r\n\tstatic uint[] CreateCrcTable()\r\n\t{\r\n\t\tvar table = new uint[256];\r\n\t\tfor ( uint n = 0; n \u003C 256; n\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar c = n;\r\n\t\t\tfor ( var k = 0; k \u003C 8; k\u002B\u002B )\r\n\t\t\t\tc = (c \u0026 1) != 0 ? 0xEDB88320u ^ (c \u003E\u003E 1) : c \u003E\u003E 1;\r\n\t\t\ttable[n] = c;\r\n\t\t}\r\n\t\treturn table;\r\n\t}\r\n\r\n\tstatic uint Crc32Update( uint crc, byte[] data, int offset, int length )\r\n\t{\r\n\t\tfor ( var i = 0; i \u003C length; i\u002B\u002B )\r\n\t\t\tcrc = CrcTable[(crc ^ data[offset \u002B i]) \u0026 0xFF] ^ (crc \u003E\u003E 8);\r\n\t\treturn crc;\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Ui/GmodImporterWindow.cs","FileName":"GmodImporterWindow.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.IO;\r\nusing System.Net;\r\nusing System.Text.RegularExpressions;\r\nusing Editor;\r\nusing GmodAssetImporter;\r\n\r\n/// \u003Csummary\u003E\r\n/// Editor UI for converting GMod Source1 models/textures to s\u0026box VMDL/PNG.\r\n/// GMod: .vtf = texture, .vmt = material recipe (textures are what we port).\r\n/// \u003C/summary\u003E\r\npublic sealed class GmodImporterWindow : Dialog\r\n{\r\n\tstatic GmodImporterWindow _current;\r\n\r\n\treadonly LineEdit _inputEdit;\r\n\treadonly LineEdit _gmodEdit;\r\n\treadonly LineEdit _outputEdit;\r\n\treadonly Checkbox _preservePaths;\r\n\treadonly Checkbox _exportAnims;\r\n\treadonly Checkbox _recursive;\r\n\treadonly Checkbox _importMaterials;\r\n\treadonly Checkbox _importModels;\r\n\treadonly LineEdit _scaleEdit;\r\n\treadonly TextEdit _log;\r\n\treadonly Label _status;\r\n\r\n\tpublic static void Open()\r\n\t{\r\n\t\tif ( _current is { IsValid: true } )\r\n\t\t{\r\n\t\t\t_current.Show();\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t_current = new GmodImporterWindow();\r\n\t}\r\n\r\n\tGmodImporterWindow() : base( null )\r\n\t{\r\n\t\tWindow.Title = \u0022Gmod Asset Importer\u0022;\r\n\t\tWindow.SetWindowIcon( \u0022folder_open\u0022 );\r\n\t\tWindow.Size = new Vector2( 760, 720 );\r\n\t\tWindow.MinimumSize = new Vector2( 600, 560 );\r\n\t\tWindow.StateCookie = \u0022GmodAssetImporter\u0022;\r\n\r\n\t\tLayout = Layout.Column();\r\n\t\tLayout.Margin = 16;\r\n\t\tLayout.Spacing = 10;\r\n\r\n\t\tvar subtitle = Layout.Add( new Label( \u0022Convert GMod .mdl / .vmt / .vtf into s\u0026box .vmdl, .vmat and PNG.\u0022 )\r\n\t\t{\r\n\t\t\tWordWrap = true\r\n\t\t} );\r\n\t\tsubtitle.SetStyles( \u0022color: #9aa0a6; font-size: 12px;\u0022 );\r\n\r\n\t\tAddSection( \u0022Paths\u0022 );\r\n\r\n\t\t_inputEdit = AddLabeledPathRow( \u0022Input\u0022, \u0022File or folder (.mdl, .vmt, .vtf)\u0022, out var inputActions );\r\n\t\tinputActions.Add( new Button( \u0022File\u0022, \u0022draft\u0022 ) { Clicked = BrowseInputFile } );\r\n\t\tinputActions.Add( new Button( \u0022Folder\u0022, \u0022folder_open\u0022 ) { Clicked = BrowseInputFolder } );\r\n\r\n\t\t_gmodEdit = AddLabeledPathRow( \u0022GMod root\u0022, \u0022\u2026/garrysmod (contains models/ and materials/)\u0022, out var gmodActions );\r\n\t\tgmodActions.Add( new Button( \u0022Detect\u0022, \u0022travel_explore\u0022 )\r\n\t\t{\r\n\t\t\tToolTip = \u0022Guess GMod root from the input path\u0022,\r\n\t\t\tClicked = DetectGmod\r\n\t\t} );\r\n\t\tgmodActions.Add( new Button( \u0022...\u0022, \u0022folder_open\u0022 ) { Clicked = BrowseGmod } );\r\n\r\n\t\t_outputEdit = AddLabeledPathRow( \u0022Output\u0022, \u0022Project Assets root\u0022, out var outputActions );\r\n\t\toutputActions.Add( new Button( \u0022...\u0022, \u0022folder_open\u0022 ) { Clicked = BrowseOutput } );\r\n\t\tvar assetsPath = Sandbox.Project.Current?.GetAssetsPath();\r\n\t\tif ( !string.IsNullOrWhiteSpace( assetsPath ) )\r\n\t\t\t_outputEdit.Text = assetsPath;\r\n\r\n\t\tAddSection( \u0022Import\u0022 );\r\n\r\n\t\tvar importRow = Layout.AddRow();\r\n\t\timportRow.Spacing = 16;\r\n\t\t_importModels = importRow.Add( new Checkbox( \u0022Models\u0022 ) { Value = true } );\r\n\t\t_importModels.ToolTip = \u0022Off: skip .mdl meshes. With Materials on, a model\u0027s materials are still converted.\u0022;\r\n\t\t_importMaterials = importRow.Add( new Checkbox( \u0022Materials\u0022 ) { Value = true } );\r\n\t\t_importMaterials.ToolTip = \u0022Off: convert models only. The .vmdl still points at the expected .vmat paths.\u0022;\r\n\t\t_recursive = importRow.Add( new Checkbox( \u0022Subfolders\u0022 ) { Value = true } );\r\n\t\t_recursive.ToolTip = \u0022When input is a folder, also convert files in child folders.\u0022;\r\n\t\timportRow.AddStretchCell();\r\n\r\n\t\tAddSection( \u0022Options\u0022 );\r\n\r\n\t\tvar optionsRow = Layout.AddRow();\r\n\t\toptionsRow.Spacing = 16;\r\n\t\t_preservePaths = optionsRow.Add( new Checkbox( \u0022Preserve paths\u0022 ) { Value = true } );\r\n\t\t_preservePaths.ToolTip = \u0022Keep Source-relative models/\u2026 and materials/\u2026 folders under the output root.\u0022;\r\n\t\t_exportAnims = optionsRow.Add( new Checkbox( \u0022Log sequences\u0022 ) { Value = false } );\r\n\t\t_exportAnims.ToolTip = \u0022Log Source sequence names during convert (clips are not exported yet).\u0022;\r\n\t\toptionsRow.AddStretchCell();\r\n\t\toptionsRow.Add( new Label( \u0022Scale\u0022 ) );\r\n\t\t_scaleEdit = optionsRow.Add( new LineEdit { Text = \u00221.0\u0022, PlaceholderText = \u00221.0\u0022 } );\r\n\t\t_scaleEdit.MaximumWidth = 72;\r\n\t\t_scaleEdit.ToolTip = \u0022Global model scale applied in the VMDL.\u0022;\r\n\r\n\t\tLayout.AddSeparator( true );\r\n\r\n\t\t_status = Layout.Add( new Label( \u0022Ready\u0022 ) { WordWrap = true } );\r\n\t\t_status.SetStyles( \u0022color: #9aa0a6; font-size: 12px;\u0022 );\r\n\r\n\t\tvar logHeader = Layout.AddRow();\r\n\t\tlogHeader.Spacing = 8;\r\n\t\tvar logLabel = logHeader.Add( new Label( \u0022Log\u0022 ) );\r\n\t\tlogLabel.SetStyles( \u0022font-weight: 600;\u0022 );\r\n\t\tlogHeader.AddStretchCell();\r\n\t\tlogHeader.Add( new Button( \u0022Clear\u0022, \u0022delete_sweep\u0022 )\r\n\t\t{\r\n\t\t\tToolTip = \u0022Clear the log\u0022,\r\n\t\t\tClicked = ClearLog\r\n\t\t} );\r\n\r\n\t\t_log = Layout.Add( new TextEdit(), 1 );\r\n\t\t_log.ReadOnly = true;\r\n\t\t_log.Editable = false;\r\n\t\t_log.TextSelectable = true;\r\n\t\t_log.SetStyles( \u0022font-family: Consolas, monospace; padding: 10px; font-size: 12px; background-color: rgba(0,0,0,0.25);\u0022 );\r\n\r\n\t\tvar footer = Layout.AddRow();\r\n\t\tfooter.Spacing = 8;\r\n\t\tfooter.AddStretchCell();\r\n\t\tfooter.Add( new Button( \u0022Scan\u0022, \u0022search\u0022 )\r\n\t\t{\r\n\t\t\tToolTip = \u0022Count MDLs and materials in the input path\u0022,\r\n\t\t\tClicked = ScanInput\r\n\t\t} );\r\n\t\tfooter.Add( new Button.Primary( \u0022Convert\u0022, \u0022play_arrow\u0022 ) { Clicked = RunConvert } );\r\n\r\n\t\tShow();\r\n\t}\r\n\r\n\tvoid AddSection( string title )\r\n\t{\r\n\t\tLayout.AddSpacingCell( 4 );\r\n\t\tLayout.AddSeparator( true );\r\n\t\tvar label = Layout.Add( new Label( title ) );\r\n\t\tlabel.SetStyles( \u0022font-weight: 600; font-size: 12px; color: #d0d4d8;\u0022 );\r\n\t}\r\n\r\n\tLineEdit AddLabeledPathRow( string label, string placeholder, out Layout actions )\r\n\t{\r\n\t\tvar row = Layout.AddRow();\r\n\t\trow.Spacing = 8;\r\n\t\tvar name = row.Add( new Label( label ) );\r\n\t\tname.MinimumWidth = 84;\r\n\t\tname.SetStyles( \u0022color: #c0c4c8;\u0022 );\r\n\t\tvar edit = row.Add( new LineEdit { PlaceholderText = placeholder }, 1 );\r\n\t\tactions = row;\r\n\t\treturn edit;\r\n\t}\r\n\r\n\tenum LogTone\r\n\t{\r\n\t\tInfo,\r\n\t\tDetect,\r\n\t\tIssue\r\n\t}\r\n\r\n\t// Detect = cyan scan/summary lines; Issue = red failures/warnings; Info = normal grey.\r\n\tconst string LogColorInfo = \u0022#c8c8c8\u0022;\r\n\tconst string LogColorDetect = \u0022#5ec8ff\u0022;\r\n\tconst string LogColorIssue = \u0022#ff686b\u0022;\r\n\tconst string LogColorModels = \u0022#4da3ff\u0022;\r\n\tconst string LogColorMaterials = \u0022#ff8cc8\u0022;\r\n\tconst string LogColorSuccess = \u0022#6dce6d\u0022;\r\n\r\n\tstatic readonly Regex DetectedCountsRegex = new(\r\n\t\t@\u0022^Detected\\s\u002B(\\d\u002B)\\s\u002BMDL\\(s\\),\\s\u002B(\\d\u002B)\\s\u002Bmaterial file\\(s\\)\\s\u002B\\((\\d\u002B)\\s\u002B\\.vmt,\\s\u002B(\\d\u002B)\\s\u002B\\.vtf\\)$\u0022,\r\n\t\tRegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase );\r\n\r\n\tstatic readonly Regex DetectedMaterialsOnlyRegex = new(\r\n\t\t@\u0022^Detected\\s\u002B(\\d\u002B)\\s\u002Bmaterial file\\(s\\)\\s\u002B\\((\\d\u002B)\\s\u002B\\.vmt,\\s\u002B(\\d\u002B)\\s\u002B\\.vtf\\)$\u0022,\r\n\t\tRegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase );\r\n\r\n\tpublic override void OnDestroyed()\r\n\t{\r\n\t\tif ( _current == this )\r\n\t\t\t_current = null;\r\n\t\tbase.OnDestroyed();\r\n\t}\r\n\r\n\tvoid BrowseInputFile()\r\n\t{\r\n\t\tvar path = EditorUtility.OpenFileDialog( \u0022Select MDL or VTF\u0022, \u0022mdl;vtf;vmt\u0022, _inputEdit.Text );\r\n\t\tif ( string.IsNullOrWhiteSpace( path ) )\r\n\t\t\treturn;\r\n\r\n\t\t_inputEdit.Text = path;\r\n\t\tif ( string.IsNullOrWhiteSpace( _gmodEdit.Text ) )\r\n\t\t\t_gmodEdit.Text = PathResolve.DetectGmodRoot( path );\r\n\r\n\t\tAppendLog( $\u0022Input file: {path}\u0022 );\r\n\t\tDescribePath( path );\r\n\t}\r\n\r\n\tvoid BrowseInputFolder()\r\n\t{\r\n\t\tvar folder = PickFolder( \u0022Select input folder\u0022, _inputEdit.Text );\r\n\t\tif ( string.IsNullOrWhiteSpace( folder ) )\r\n\t\t\treturn;\r\n\r\n\t\t_inputEdit.Text = folder;\r\n\t\tif ( string.IsNullOrWhiteSpace( _gmodEdit.Text ) )\r\n\t\t\t_gmodEdit.Text = PathResolve.DetectGmodRoot( folder );\r\n\r\n\t\tAppendLog( $\u0022Input folder: {folder}\u0022 );\r\n\t\tDescribePath( folder );\r\n\t}\r\n\r\n\tvoid BrowseGmod()\r\n\t{\r\n\t\tvar folder = PickFolder( \u0022Select GMod content root (folder with models/ and materials/)\u0022, _gmodEdit.Text );\r\n\t\tif ( string.IsNullOrWhiteSpace( folder ) )\r\n\t\t\treturn;\r\n\r\n\t\tvar detected = PathResolve.DetectGmodRoot( folder );\r\n\t\t_gmodEdit.Text = string.IsNullOrWhiteSpace( detected ) ? folder : detected;\r\n\t}\r\n\r\n\tvoid BrowseOutput()\r\n\t{\r\n\t\t// Always open on this project\u0027s Assets folder (not the previously typed path).\r\n\t\tvar start = Sandbox.Project.Current?.GetAssetsPath();\r\n\t\tif ( string.IsNullOrWhiteSpace( start ) || !Directory.Exists( start ) )\r\n\t\t\tstart = _outputEdit.Text;\r\n\r\n\t\tvar folder = PickFolder( \u0022Select output Assets root\u0022, start );\r\n\t\tif ( !string.IsNullOrWhiteSpace( folder ) )\r\n\t\t\t_outputEdit.Text = folder;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// OS folder picker \u2014 Open selects the folder you\u0027re in (unlike OpenFileDialog).\r\n\t/// \u003C/summary\u003E\r\n\tstatic string PickFolder( string title, string defaultPath )\r\n\t{\r\n\t\tvar start = ResolveBrowseDirectory( defaultPath );\r\n\t\tvar fd = new FileDialog( null );\r\n\t\tfd.Title = title;\r\n\t\tif ( !string.IsNullOrWhiteSpace( start ) )\r\n\t\t\tfd.Directory = start;\r\n\t\tfd.SetFindDirectory();\r\n\t\tfd.SetModeOpen();\r\n\r\n\t\tif ( !fd.Execute() )\r\n\t\t\treturn null;\r\n\r\n\t\tvar selected = fd.SelectedFile;\r\n\t\tif ( string.IsNullOrWhiteSpace( selected ) )\r\n\t\t\treturn null;\r\n\r\n\t\treturn Directory.Exists( selected ) ? selected : Path.GetDirectoryName( selected );\r\n\t}\r\n\r\n\tstatic string ResolveBrowseDirectory( string path )\r\n\t{\r\n\t\tif ( string.IsNullOrWhiteSpace( path ) )\r\n\t\t\treturn \u0022\u0022;\r\n\r\n\t\tpath = path.Trim();\r\n\t\tif ( Directory.Exists( path ) )\r\n\t\t\treturn path;\r\n\t\tif ( File.Exists( path ) )\r\n\t\t\treturn Path.GetDirectoryName( path ) ?? \u0022\u0022;\r\n\r\n\t\tvar parent = Path.GetDirectoryName( path );\r\n\t\treturn Directory.Exists( parent ) ? parent : \u0022\u0022;\r\n\t}\r\n\r\n\tvoid ScanInput()\r\n\t{\r\n\t\tvar input = _inputEdit.Text?.Trim() ?? \u0022\u0022;\r\n\t\tif ( string.IsNullOrWhiteSpace( input ) )\r\n\t\t{\r\n\t\t\tSetStatus( \u0022Set an input file or folder first.\u0022, issue: true );\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tDescribePath( input );\r\n\t}\r\n\r\n\tvoid DescribePath( string path )\r\n\t{\r\n\t\tif ( Directory.Exists( path ) )\r\n\t\t{\r\n\t\t\tvar counts = PathResolve.CountSourceAssets( path, _recursive.Value );\r\n\t\t\tvar materialCount = counts.Vmt \u002B counts.Vtf;\r\n\t\t\tSetStatus( $\u0022Detected {counts.Mdl} MDL(s), {materialCount} material file(s) ({counts.Vmt} .vmt, {counts.Vtf} .vtf)\u0022 );\r\n\t\t\tAppendDetectedCounts( counts.Mdl, counts.Vmt, counts.Vtf );\r\n\t\t\tif ( counts.Vtf == 0 \u0026\u0026 counts.Vmt \u003E 0 )\r\n\t\t\t\tAppendLog( \u0022Note: no .vtf here \u2014 VMT textures will be looked up under the GMod root.\u0022 );\r\n\t\t\telse if ( counts.Vtf \u003E 0 || counts.Vmt \u003E 0 )\r\n\t\t\t\tAppendLog( \u0022Ready: each .vmt becomes a VMAT; loose .vtf become a basic VMAT (normal/mask maps stay PNG only).\u0022 );\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif ( File.Exists( path ) )\r\n\t\t{\r\n\t\t\tvar ext = Path.GetExtension( path ).ToLowerInvariant();\r\n\t\t\tvar msg = ext switch\r\n\t\t\t{\r\n\t\t\t\t\u0022.vtf\u0022 =\u003E \u0022VTF texture \u2014 will convert to PNG \u002B VMAT (uses sibling .vmt if present)\u0022,\r\n\t\t\t\t\u0022.vmt\u0022 =\u003E \u0022VMT material \u2014 will convert to VMAT \u002B PNG textures\u0022,\r\n\t\t\t\t\u0022.mdl\u0022 =\u003E (_importModels.Value, _importMaterials.Value) switch\r\n\t\t\t\t{\r\n\t\t\t\t\t(true, true) =\u003E \u0022MDL model \u2014 will convert with materials\u0022,\r\n\t\t\t\t\t(true, false) =\u003E \u0022MDL model \u2014 will convert model only (Import materials off)\u0022,\r\n\t\t\t\t\t(false, true) =\u003E \u0022MDL model \u2014 will convert materials only (Import models off)\u0022,\r\n\t\t\t\t\t_ =\u003E \u0022MDL model \u2014 Import models and Import materials are both off\u0022\r\n\t\t\t\t},\r\n\t\t\t\t_ =\u003E $\u0022Selected {ext}\u0022\r\n\t\t\t};\r\n\t\t\tSetStatus( msg );\r\n\t\t\tAppendLog( msg, LogTone.Detect );\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tSetStatus( \u0022Path not found.\u0022, issue: true );\r\n\t\tAppendLog( \u0022Path not found.\u0022, LogTone.Issue );\r\n\t}\r\n\r\n\tvoid DetectGmod()\r\n\t{\r\n\t\tvar from = !string.IsNullOrWhiteSpace( _inputEdit.Text ) ? _inputEdit.Text : _gmodEdit.Text;\r\n\t\tvar detected = PathResolve.DetectGmodRoot( from );\r\n\t\tif ( string.IsNullOrWhiteSpace( detected ) )\r\n\t\t{\r\n\t\t\tAppendLog( \u0022Could not detect GMod root from input path.\u0022, LogTone.Issue );\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t_gmodEdit.Text = detected;\r\n\t\tAppendLog( $\u0022Detected GMod root: {detected}\u0022, LogTone.Detect );\r\n\t}\r\n\r\n\tvoid RunConvert()\r\n\t{\r\n\t\tClearLog();\r\n\t\tSetStatus( \u0022Converting...\u0022 );\r\n\r\n\t\tvar options = new ConversionOptions\r\n\t\t{\r\n\t\t\tGmodRoot = _gmodEdit.Text?.Trim() ?? \u0022\u0022,\r\n\t\t\tOutputRoot = _outputEdit.Text?.Trim() ?? \u0022\u0022,\r\n\t\t\tPreservePaths = _preservePaths.Value,\r\n\t\t\tExportAnimations = _exportAnims.Value,\r\n\t\t\tRecursive = _recursive.Value,\r\n\t\t\tImportMaterials = _importMaterials.Value,\r\n\t\t\tImportModels = _importModels.Value,\r\n\t\t\tModelScale = float.TryParse( _scaleEdit.Text, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var s ) ? s : 1f,\r\n\t\t\tOnLog = AppendLog\r\n\t\t};\r\n\r\n\t\tif ( string.IsNullOrWhiteSpace( options.OutputRoot ) )\r\n\t\t{\r\n\t\t\tSetStatus( \u0022Set an output Assets root.\u0022, issue: true );\r\n\t\t\tAppendLog( \u0022Set an output Assets root.\u0022, LogTone.Issue );\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\tif ( string.IsNullOrWhiteSpace( _inputEdit.Text ) )\r\n\t\t{\r\n\t\t\tSetStatus( \u0022Set an input file or folder.\u0022, issue: true );\r\n\t\t\tAppendLog( \u0022Set an input file or folder.\u0022, LogTone.Issue );\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar input = _inputEdit.Text.Trim();\r\n\t\t\tDescribePath( input );\r\n\t\t\tvar result = ConversionPipeline.Convert( input, options );\r\n\t\t\tSetStatus( result.Success ? result.Message : $\u0022Failed: {result.Message}\u0022, issue: !result.Success );\r\n\t\t\tif ( !result.Success )\r\n\t\t\t\tAppendLog( $\u0022Failed: {result.Message}\u0022, LogTone.Issue );\r\n\t\t\telse\r\n\t\t\t\tAppendLog( result.Message, LogTone.Detect );\r\n\r\n\t\t\tif ( result.Warnings.Count \u003E 0 )\r\n\t\t\t{\r\n\t\t\t\tAppendLog( $\u0022{result.Warnings.Count} warning(s):\u0022, LogTone.Issue );\r\n\t\t\t\tforeach ( var warning in result.Warnings )\r\n\t\t\t\t\tAppendLog( warning, LogTone.Issue );\r\n\t\t\t}\r\n\t\t}\r\n\t\tcatch ( Exception ex )\r\n\t\t{\r\n\t\t\tSetStatus( $\u0022Failed: {ex.Message}\u0022, issue: true );\r\n\t\t\tAppendLog( ex.ToString(), LogTone.Issue );\r\n\t\t}\r\n\t}\r\n\r\n\tvoid ClearLog()\r\n\t{\r\n\t\t_log.Clear();\r\n\t\t_status.Text = \u0022Ready\u0022;\r\n\t\t_status.SetStyles( \u0022color: #9aa0a6; font-size: 12px;\u0022 );\r\n\t}\r\n\r\n\tvoid SetStatus( string text, bool issue = false )\r\n\t{\r\n\t\t_status.Text = text ?? \u0022\u0022;\r\n\t\t_status.SetStyles( issue\r\n\t\t\t? \u0022color: #ff686b; font-size: 12px;\u0022\r\n\t\t\t: \u0022color: #9aa0a6; font-size: 12px;\u0022 );\r\n\t}\r\n\r\n\tvoid AppendLog( string line ) =\u003E AppendLog( line, ClassifyLog( line ) );\r\n\r\n\tvoid AppendLog( string line, LogTone tone )\r\n\t{\r\n\t\tif ( string.IsNullOrEmpty( line ) )\r\n\t\t\treturn;\r\n\r\n\t\tif ( TryAppendDetectedCounts( line ) )\r\n\t\t\treturn;\r\n\r\n\t\tif ( TryAppendBatchComplete( line ) )\r\n\t\t\treturn;\r\n\r\n\t\tvar color = tone switch\r\n\t\t{\r\n\t\t\tLogTone.Detect =\u003E LogColorDetect,\r\n\t\t\tLogTone.Issue =\u003E LogColorIssue,\r\n\t\t\t_ =\u003E LogColorInfo\r\n\t\t};\r\n\r\n\t\tvar html = $\u0022\u003Cspan style=\\\u0022color:{color};\\\u0022\u003E{WebUtility.HtmlEncode( line )}\u003C/span\u003E\u0022;\r\n\t\t_log.AppendHtml( html );\r\n\t}\r\n\r\n\tstatic readonly Regex BatchCompleteRegex = new(\r\n\t\t@\u0022^Batch complete\\s*-\\s*(.\u002B)$\u0022,\r\n\t\tRegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase );\r\n\r\n\tbool TryAppendBatchComplete( string line )\r\n\t{\r\n\t\tvar match = BatchCompleteRegex.Match( line );\r\n\t\tif ( !match.Success )\r\n\t\t\treturn false;\r\n\r\n\t\t_log.AppendHtml(\r\n\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorSuccess};\\\u0022\u003EBatch complete\u003C/span\u003E\u0022 \u002B\r\n\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorInfo};\\\u0022\u003E - {WebUtility.HtmlEncode( match.Groups[1].Value )}\u003C/span\u003E\u0022 );\r\n\t\treturn true;\r\n\t}\r\n\r\n\tvoid AppendDetectedCounts( int mdl, int vmt, int vtf )\r\n\t{\r\n\t\tvar materials = vmt \u002B vtf;\r\n\t\t_log.AppendHtml(\r\n\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorInfo};\\\u0022\u003EDetected \u003C/span\u003E\u0022 \u002B\r\n\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorModels};\\\u0022\u003E{mdl} MDL(s)\u003C/span\u003E\u0022 \u002B\r\n\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorInfo};\\\u0022\u003E, \u003C/span\u003E\u0022 \u002B\r\n\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorMaterials};\\\u0022\u003E{materials} material file(s) ({vmt} .vmt, {vtf} .vtf)\u003C/span\u003E\u0022 );\r\n\t}\r\n\r\n\tbool TryAppendDetectedCounts( string line )\r\n\t{\r\n\t\tvar full = DetectedCountsRegex.Match( line );\r\n\t\tif ( full.Success )\r\n\t\t{\r\n\t\t\tAppendDetectedCounts(\r\n\t\t\t\tint.Parse( full.Groups[1].Value ),\r\n\t\t\t\tint.Parse( full.Groups[3].Value ),\r\n\t\t\t\tint.Parse( full.Groups[4].Value ) );\r\n\t\t\treturn true;\r\n\t\t}\r\n\r\n\t\tvar matsOnly = DetectedMaterialsOnlyRegex.Match( line );\r\n\t\tif ( matsOnly.Success )\r\n\t\t{\r\n\t\t\t_log.AppendHtml(\r\n\t\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorInfo};\\\u0022\u003EDetected \u003C/span\u003E\u0022 \u002B\r\n\t\t\t\t$\u0022\u003Cspan style=\\\u0022color:{LogColorMaterials};\\\u0022\u003E{matsOnly.Groups[1].Value} material file(s) ({matsOnly.Groups[2].Value} .vmt, {matsOnly.Groups[3].Value} .vtf)\u003C/span\u003E\u0022 );\r\n\t\t\treturn true;\r\n\t\t}\r\n\r\n\t\treturn false;\r\n\t}\r\n\r\n\tstatic LogTone ClassifyLog( string line )\r\n\t{\r\n\t\tif ( string.IsNullOrWhiteSpace( line ) )\r\n\t\t\treturn LogTone.Info;\r\n\r\n\t\tif ( IsIssueLine( line ) )\r\n\t\t\treturn LogTone.Issue;\r\n\r\n\t\tif ( IsDetectLine( line ) )\r\n\t\t\treturn LogTone.Detect;\r\n\r\n\t\treturn LogTone.Info;\r\n\t}\r\n\r\n\tstatic bool IsDetectLine( string line )\r\n\t{\r\n\t\treturn line.StartsWith( \u0022Detected \u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022Folder scan:\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022Materials:\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022Batch:\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022Converted \u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022Batch complete\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022MDL model\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022VTF texture\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022VMT material\u0022, StringComparison.OrdinalIgnoreCase );\r\n\t}\r\n\r\n\tstatic bool IsIssueLine( string line )\r\n\t{\r\n\t\treturn line.StartsWith( \u0022Error:\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022Failed:\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022Failed \u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.EndsWith( \u0022warning(s):\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022Missing VTF\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022VTF not found\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022VTF decode failed\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022VMT parse failed\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022Material not found\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022writing fallback VMAT\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022nothing to convert\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022Path not found\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022Could not detect\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.Contains( \u0022No .mdl\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t|| line.StartsWith( \u0022System.\u0022, StringComparison.Ordinal );\r\n\t}\r\n}\r\n\r\npublic static class GmodImporterMenu\r\n{\r\n\t[Menu( \u0022Editor\u0022, \u0022Gmod Asset Importer/Open...\u0022 )]\r\n\tpublic static void OpenImporter()\r\n\t{\r\n\t\tGmodImporterWindow.Open();\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Pipeline/ConversionPipeline.cs","FileName":"ConversionPipeline.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.IO;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\n/// \u003Csummary\u003E\r\n/// Orchestrates single-file and batch GMod \u2192 s\u0026box conversions.\r\n/// Textures are .vtf (port to PNG). .vmt is optional material recipe (used by model import).\r\n/// \u003C/summary\u003E\r\npublic static class ConversionPipeline\r\n{\r\n\tpublic static ConversionResult Convert( string inputPath, ConversionOptions options )\r\n\t{\r\n\t\tif ( string.IsNullOrWhiteSpace( inputPath ) )\r\n\t\t\treturn Fail( \u0022No input path\u0022 );\r\n\r\n\t\tif ( string.IsNullOrWhiteSpace( options.OutputRoot ) )\r\n\t\t\treturn Fail( \u0022No output root\u0022 );\r\n\r\n\t\tif ( !options.ImportModels \u0026\u0026 !options.ImportMaterials )\r\n\t\t\treturn Fail( \u0022Import models and Import materials are both off - nothing to convert\u0022 );\r\n\r\n\t\tDirectory.CreateDirectory( options.OutputRoot );\r\n\r\n\t\tif ( Directory.Exists( inputPath ) )\r\n\t\t\treturn ConvertFolder( inputPath, options );\r\n\r\n\t\tvar ext = Path.GetExtension( inputPath ).ToLowerInvariant();\r\n\t\treturn ext switch\r\n\t\t{\r\n\t\t\t\u0022.mdl\u0022 =\u003E ConvertMdl( inputPath, options ),\r\n\t\t\t\u0022.vtf\u0022 =\u003E MaterialPipeline.ConvertVtfFile( inputPath, options ),\r\n\t\t\t\u0022.vmt\u0022 =\u003E MaterialPipeline.ConvertVmtFile( inputPath, options ),\r\n\t\t\t_ =\u003E Fail( $\u0022Unsupported input type: {ext} (use .mdl, .vtf, .vmt, or a folder)\u0022 )\r\n\t\t};\r\n\t}\r\n\r\n\tpublic static ConversionResult ConvertFolder( string folder, ConversionOptions options )\r\n\t{\r\n\t\tvar result = new ConversionResult { Success = true };\r\n\t\tvar writeLog = options.OnLog ?? (_ =\u003E { });\r\n\t\tvar search = options.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;\r\n\r\n\t\tif ( string.IsNullOrWhiteSpace( options.GmodRoot ) )\r\n\t\t\toptions.GmodRoot = PathResolve.DetectGmodRoot( folder );\r\n\r\n\t\tvar counts = PathResolve.CountSourceAssets( folder, options.Recursive );\r\n\t\twriteLog( $\u0022Detected {counts.Mdl} MDL(s), {counts.Vmt \u002B counts.Vtf} material file(s) ({counts.Vmt} .vmt, {counts.Vtf} .vtf)\u0022 );\r\n\r\n\t\tvar mdls = Directory.GetFiles( folder, \u0022*.mdl\u0022, search );\r\n\t\tvar hasMaterialFiles = counts.Vtf \u003E 0 || counts.Vmt \u003E 0;\r\n\r\n\t\tif ( mdls.Length == 0 \u0026\u0026 !hasMaterialFiles )\r\n\t\t\treturn Fail( \u0022No .mdl, .vmt or .vtf files found in folder\u0022 );\r\n\r\n\t\tif ( !options.ImportMaterials \u0026\u0026 hasMaterialFiles )\r\n\t\t{\r\n\t\t\twriteLog( $\u0022Skipping {counts.Vmt} .vmt / {counts.Vtf} .vtf (Import materials off)\u0022 );\r\n\t\t\tif ( mdls.Length == 0 )\r\n\t\t\t\treturn Fail( \u0022Import materials is off and the folder has no .mdl files - nothing to convert\u0022 );\r\n\t\t\thasMaterialFiles = false;\r\n\t\t}\r\n\r\n\t\tif ( mdls.Length \u003E 0 )\r\n\t\t{\r\n\t\t\twriteLog( options.ImportModels\r\n\t\t\t\t? $\u0022Batch: {mdls.Length} model(s)\u0022\r\n\t\t\t\t: $\u0022Batch: materials for {mdls.Length} model(s) (Import models off)\u0022 );\r\n\t\t\tforeach ( var mdl in mdls )\r\n\t\t\t{\r\n\t\t\t\tvar one = ConvertMdl( mdl, options );\r\n\t\t\t\tMerge( result, one );\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif ( hasMaterialFiles )\r\n\t\t{\r\n\t\t\twriteLog( $\u0022Batch: {counts.Vmt} .vmt / {counts.Vtf} .vtf -\u003E VMAT \u002B PNG\u0022 );\r\n\t\t\tvar one = MaterialPipeline.ConvertMaterialsFolder( folder, options );\r\n\t\t\tMerge( result, one );\r\n\t\t}\r\n\r\n\t\tresult.Success = result.WrittenFiles.Count \u003E 0;\r\n\t\tresult.Message = $\u0022Batch complete - {result.WrittenFiles.Count} file(s), {result.Warnings.Count} warning(s)\u0022;\r\n\t\treturn result;\r\n\t}\r\n\r\n\tstatic ConversionResult ConvertMdl( string mdlPath, ConversionOptions options ) =\u003E options.ImportModels\r\n\t\t? MdlPipeline.ConvertModel( mdlPath, options )\r\n\t\t: MdlPipeline.ConvertModelMaterials( mdlPath, options );\r\n\r\n\tstatic void Merge( ConversionResult into, ConversionResult one )\r\n\t{\r\n\t\tinto.WrittenFiles.AddRange( one.WrittenFiles );\r\n\t\tinto.Warnings.AddRange( one.Warnings );\r\n\t\tif ( !one.Success )\r\n\t\t{\r\n\t\t\tinto.Warnings.Add( one.Message );\r\n\t\t\tinto.Success = into.WrittenFiles.Count \u003E 0 || into.Success;\r\n\t\t}\r\n\t}\r\n\r\n\tstatic ConversionResult Fail( string message ) =\u003E new()\r\n\t{\r\n\t\tSuccess = false,\r\n\t\tMessage = message\r\n\t};\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Source1/VtfReader.cs","FileName":"VtfReader.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.IO;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\n/// \u003Csummary\u003E\r\n/// Source1 VTF reader for common GMod formats (BGRA8888, RGBA8888, DXT1, DXT5).\r\n/// \u003C/summary\u003E\r\npublic sealed class VtfImage\r\n{\r\n\tpublic int Width;\r\n\tpublic int Height;\r\n\tpublic byte[] Rgba; // length = Width * Height * 4\r\n}\r\n\r\npublic static class VtfReader\r\n{\r\n\tconst int VtfSignature = 0x00465456; // \u0022VTF\\0\u0022 little-endian as int? Actually \u0027V\u0027,\u0027T\u0027,\u0027F\u0027,\u0027\\0\u0027 = 0x00465456\r\n\r\n\tenum ImageFormat\r\n\t{\r\n\t\tNone = -1,\r\n\t\tRGBA8888 = 0,\r\n\t\tABGR8888 = 1,\r\n\t\tRGB888 = 2,\r\n\t\tBGR888 = 3,\r\n\t\tRGB565 = 4,\r\n\t\tI8 = 5,\r\n\t\tIA88 = 6,\r\n\t\tP8 = 7,\r\n\t\tA8 = 8,\r\n\t\tRGB888_Bluescreen = 9,\r\n\t\tBGR888_Bluescreen = 10,\r\n\t\tARGB8888 = 11,\r\n\t\tBGRA8888 = 12,\r\n\t\tDXT1 = 13,\r\n\t\tDXT3 = 14,\r\n\t\tDXT5 = 15,\r\n\t\tBGRX8888 = 16,\r\n\t\tBGR565 = 17,\r\n\t\tBGRX5551 = 18,\r\n\t\tBGRA4444 = 19,\r\n\t\tDXT1_ONEBITALPHA = 20,\r\n\t\tBGRA5551 = 21,\r\n\t\tUV88 = 22,\r\n\t\tUVWQ8888 = 23,\r\n\t\tRGBA16161616F = 24,\r\n\t\tRGBA16161616 = 25,\r\n\t\tUVLX8888 = 26\r\n\t}\r\n\r\n\tpublic static VtfImage Load( string path )\r\n\t{\r\n\t\treturn Load( File.ReadAllBytes( path ) );\r\n\t}\r\n\r\n\tpublic static VtfImage Load( byte[] data )\r\n\t{\r\n\t\tusing var ms = new MemoryStream( data );\r\n\t\tusing var br = new BinaryReader( ms );\r\n\r\n\t\tvar sig = br.ReadBytes( 4 );\r\n\t\tif ( sig.Length \u003C 4 || sig[0] != \u0027V\u0027 || sig[1] != \u0027T\u0027 || sig[2] != \u0027F\u0027 || sig[3] != 0 )\r\n\t\t\tthrow new InvalidDataException( \u0022Not a VTF file\u0022 );\r\n\r\n\t\tvar versionMajor = br.ReadUInt32();\r\n\t\tvar versionMinor = br.ReadUInt32();\r\n\t\tvar headerSize = br.ReadUInt32();\r\n\t\tvar width = br.ReadUInt16();\r\n\t\tvar height = br.ReadUInt16();\r\n\t\tbr.ReadUInt32(); // flags\r\n\t\tvar frameCount = Math.Max( 1, (int)br.ReadUInt16() );\r\n\t\tbr.ReadUInt16(); // firstFrame\r\n\t\tms.Position \u002B= 4; // padding0\r\n\t\tms.Position \u002B= 12; // reflectivity\r\n\t\tms.Position \u002B= 4; // padding1\r\n\t\tbr.ReadSingle(); // bumpScale\r\n\t\tvar highFormat = (ImageFormat)br.ReadInt32();\r\n\t\tvar mipCount = Math.Max( 1, (int)br.ReadByte() );\r\n\t\tvar lowFormat = (ImageFormat)br.ReadInt32();\r\n\t\tvar lowWidth = br.ReadByte();\r\n\t\tvar lowHeight = br.ReadByte();\r\n\r\n\t\t// Pre-7.3 layout: header, low-res thumbnail, then high-res mips\r\n\t\tuint imageOffset = headerSize;\r\n\t\tif ( lowFormat != ImageFormat.None \u0026\u0026 lowWidth \u003E 0 \u0026\u0026 lowHeight \u003E 0 )\r\n\t\t\timageOffset \u002B= (uint)ComputeFaceSize( lowWidth, lowHeight, lowFormat );\r\n\r\n\t\tif ( versionMajor \u003E 7 || (versionMajor == 7 \u0026\u0026 versionMinor \u003E= 2) )\r\n\t\t\tbr.ReadUInt16(); // depth\r\n\r\n\t\tif ( versionMajor \u003E 7 || (versionMajor == 7 \u0026\u0026 versionMinor \u003E= 3) )\r\n\t\t{\r\n\t\t\tms.Position \u002B= 3; // padding2\r\n\t\t\tvar numResources = br.ReadInt32();\r\n\t\t\t// Resource dictionary starts at byte 80 for 7.3\u002B\r\n\t\t\tms.Position = 80;\r\n\t\t\tfor ( var r = 0; r \u003C numResources; r\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar typeBytes = br.ReadBytes( 3 );\r\n\t\t\t\tbr.ReadByte(); // resource flags\r\n\t\t\t\tvar offset = br.ReadUInt32();\r\n\t\t\t\tvar type = typeBytes[0] | (typeBytes[1] \u003C\u003C 8) | (typeBytes[2] \u003C\u003C 16);\r\n\t\t\t\tif ( type == 0x30 ) // high-res image\r\n\t\t\t\t\timageOffset = offset;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tms.Position = imageOffset;\r\n\r\n\t\t// Skip smaller mip levels so we land on the largest mip of frame 0\r\n\t\tlong skip = 0;\r\n\t\tfor ( var mip = mipCount - 1; mip \u003E= 1; mip-- )\r\n\t\t{\r\n\t\t\tvar mw = Math.Max( 1, width \u003E\u003E mip );\r\n\t\t\tvar mh = Math.Max( 1, height \u003E\u003E mip );\r\n\t\t\tskip \u002B= (long)ComputeFaceSize( mw, mh, highFormat ) * frameCount;\r\n\t\t}\r\n\r\n\t\tms.Position = imageOffset \u002B skip;\r\n\t\tvar faceSize = ComputeFaceSize( width, height, highFormat );\r\n\t\tvar face = br.ReadBytes( faceSize );\r\n\t\tvar rgba = Decode( face, width, height, highFormat );\r\n\r\n\t\treturn new VtfImage\r\n\t\t{\r\n\t\t\tWidth = width,\r\n\t\t\tHeight = height,\r\n\t\t\tRgba = rgba\r\n\t\t};\r\n\t}\r\n\r\n\tstatic int ComputeFaceSize( int w, int h, ImageFormat format )\r\n\t{\r\n\t\treturn format switch\r\n\t\t{\r\n\t\t\tImageFormat.DXT1 or ImageFormat.DXT1_ONEBITALPHA =\u003E Math.Max( 1, (w \u002B 3) / 4 ) * Math.Max( 1, (h \u002B 3) / 4 ) * 8,\r\n\t\t\tImageFormat.DXT3 or ImageFormat.DXT5 =\u003E Math.Max( 1, (w \u002B 3) / 4 ) * Math.Max( 1, (h \u002B 3) / 4 ) * 16,\r\n\t\t\tImageFormat.RGBA8888 or ImageFormat.ABGR8888 or ImageFormat.ARGB8888 or ImageFormat.BGRA8888 or ImageFormat.BGRX8888 =\u003E w * h * 4,\r\n\t\t\tImageFormat.RGB888 or ImageFormat.BGR888 =\u003E w * h * 3,\r\n\t\t\tImageFormat.I8 or ImageFormat.A8 =\u003E w * h,\r\n\t\t\tImageFormat.IA88 =\u003E w * h * 2,\r\n\t\t\t_ =\u003E w * h * 4\r\n\t\t};\r\n\t}\r\n\r\n\tstatic byte[] Decode( byte[] src, int width, int height, ImageFormat format )\r\n\t{\r\n\t\treturn format switch\r\n\t\t{\r\n\t\t\tImageFormat.RGBA8888 =\u003E DecodeRgba8888( src, width, height ),\r\n\t\t\tImageFormat.BGRA8888 or ImageFormat.BGRX8888 =\u003E DecodeBgra8888( src, width, height ),\r\n\t\t\tImageFormat.ARGB8888 =\u003E DecodeArgb8888( src, width, height ),\r\n\t\t\tImageFormat.ABGR8888 =\u003E DecodeAbgr8888( src, width, height ),\r\n\t\t\tImageFormat.RGB888 =\u003E DecodeRgb888( src, width, height, bgr: false ),\r\n\t\t\tImageFormat.BGR888 =\u003E DecodeRgb888( src, width, height, bgr: true ),\r\n\t\t\tImageFormat.DXT1 or ImageFormat.DXT1_ONEBITALPHA =\u003E DecodeDxt1( src, width, height ),\r\n\t\t\tImageFormat.DXT5 =\u003E DecodeDxt5( src, width, height ),\r\n\t\t\tImageFormat.DXT3 =\u003E DecodeDxt3( src, width, height ),\r\n\t\t\tImageFormat.I8 =\u003E DecodeI8( src, width, height ),\r\n\t\t\t_ =\u003E throw new NotSupportedException( $\u0022Unsupported VTF format: {format}\u0022 )\r\n\t\t};\r\n\t}\r\n\r\n\tstatic byte[] DecodeRgba8888( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tBuffer.BlockCopy( src, 0, dst, 0, Math.Min( src.Length, dst.Length ) );\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeBgra8888( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar n = Math.Min( src.Length / 4, w * h );\r\n\t\tfor ( var i = 0; i \u003C n; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar o = i * 4;\r\n\t\t\tdst[o] = src[o \u002B 2];\r\n\t\t\tdst[o \u002B 1] = src[o \u002B 1];\r\n\t\t\tdst[o \u002B 2] = src[o];\r\n\t\t\tdst[o \u002B 3] = src[o \u002B 3];\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeArgb8888( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar n = Math.Min( src.Length / 4, w * h );\r\n\t\tfor ( var i = 0; i \u003C n; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar o = i * 4;\r\n\t\t\tdst[o] = src[o \u002B 1];\r\n\t\t\tdst[o \u002B 1] = src[o \u002B 2];\r\n\t\t\tdst[o \u002B 2] = src[o \u002B 3];\r\n\t\t\tdst[o \u002B 3] = src[o];\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeAbgr8888( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar n = Math.Min( src.Length / 4, w * h );\r\n\t\tfor ( var i = 0; i \u003C n; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar o = i * 4;\r\n\t\t\tdst[o] = src[o \u002B 3];\r\n\t\t\tdst[o \u002B 1] = src[o \u002B 2];\r\n\t\t\tdst[o \u002B 2] = src[o \u002B 1];\r\n\t\t\tdst[o \u002B 3] = src[o];\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeRgb888( byte[] src, int w, int h, bool bgr )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar n = Math.Min( src.Length / 3, w * h );\r\n\t\tfor ( var i = 0; i \u003C n; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar s = i * 3;\r\n\t\t\tvar d = i * 4;\r\n\t\t\tvar r = bgr ? src[s \u002B 2] : src[s];\r\n\t\t\tvar g = src[s \u002B 1];\r\n\t\t\tvar b = bgr ? src[s] : src[s \u002B 2];\r\n\t\t\tdst[d] = r;\r\n\t\t\tdst[d \u002B 1] = g;\r\n\t\t\tdst[d \u002B 2] = b;\r\n\t\t\tdst[d \u002B 3] = 255;\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeI8( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar n = Math.Min( src.Length, w * h );\r\n\t\tfor ( var i = 0; i \u003C n; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar v = src[i];\r\n\t\t\tvar d = i * 4;\r\n\t\t\tdst[d] = v;\r\n\t\t\tdst[d \u002B 1] = v;\r\n\t\t\tdst[d \u002B 2] = v;\r\n\t\t\tdst[d \u002B 3] = 255;\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeDxt1( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar blocksX = Math.Max( 1, (w \u002B 3) / 4 );\r\n\t\tvar blocksY = Math.Max( 1, (h \u002B 3) / 4 );\r\n\t\tvar offset = 0;\r\n\t\tfor ( var by = 0; by \u003C blocksY; by\u002B\u002B )\r\n\t\tfor ( var bx = 0; bx \u003C blocksX; bx\u002B\u002B )\r\n\t\t{\r\n\t\t\tif ( offset \u002B 8 \u003E src.Length ) return dst;\r\n\t\t\tDecodeDxt1Block( src, offset, bx * 4, by * 4, w, h, dst, oneBitAlpha: false );\r\n\t\t\toffset \u002B= 8;\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeDxt3( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar blocksX = Math.Max( 1, (w \u002B 3) / 4 );\r\n\t\tvar blocksY = Math.Max( 1, (h \u002B 3) / 4 );\r\n\t\tvar offset = 0;\r\n\t\tfor ( var by = 0; by \u003C blocksY; by\u002B\u002B )\r\n\t\tfor ( var bx = 0; bx \u003C blocksX; bx\u002B\u002B )\r\n\t\t{\r\n\t\t\tif ( offset \u002B 16 \u003E src.Length ) return dst;\r\n\t\t\tvar alphaBits = BitConverter.ToUInt64( src, offset );\r\n\t\t\tDecodeDxt1Block( src, offset \u002B 8, bx * 4, by * 4, w, h, dst, oneBitAlpha: false, alwaysFourColor: true );\r\n\t\t\tfor ( var py = 0; py \u003C 4; py\u002B\u002B )\r\n\t\t\tfor ( var px = 0; px \u003C 4; px\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar x = bx * 4 \u002B px;\r\n\t\t\t\tvar y = by * 4 \u002B py;\r\n\t\t\t\tif ( x \u003E= w || y \u003E= h ) continue;\r\n\t\t\t\tvar aIdx = py * 4 \u002B px;\r\n\t\t\t\tvar a = (int)((alphaBits \u003E\u003E (aIdx * 4)) \u0026 0xF);\r\n\t\t\t\tdst[(y * w \u002B x) * 4 \u002B 3] = (byte)((a * 255) / 15);\r\n\t\t\t}\r\n\t\t\toffset \u002B= 16;\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] DecodeDxt5( byte[] src, int w, int h )\r\n\t{\r\n\t\tvar dst = new byte[w * h * 4];\r\n\t\tvar blocksX = Math.Max( 1, (w \u002B 3) / 4 );\r\n\t\tvar blocksY = Math.Max( 1, (h \u002B 3) / 4 );\r\n\t\tvar offset = 0;\r\n\t\tfor ( var by = 0; by \u003C blocksY; by\u002B\u002B )\r\n\t\tfor ( var bx = 0; bx \u003C blocksX; bx\u002B\u002B )\r\n\t\t{\r\n\t\t\tif ( offset \u002B 16 \u003E src.Length ) return dst;\r\n\t\t\tvar a0 = src[offset];\r\n\t\t\tvar a1 = src[offset \u002B 1];\r\n\t\t\tulong aBits = src[offset \u002B 2]\r\n\t\t\t\t| ((ulong)src[offset \u002B 3] \u003C\u003C 8)\r\n\t\t\t\t| ((ulong)src[offset \u002B 4] \u003C\u003C 16)\r\n\t\t\t\t| ((ulong)src[offset \u002B 5] \u003C\u003C 24)\r\n\t\t\t\t| ((ulong)src[offset \u002B 6] \u003C\u003C 32)\r\n\t\t\t\t| ((ulong)src[offset \u002B 7] \u003C\u003C 40);\r\n\r\n\t\t\tDecodeDxt1Block( src, offset \u002B 8, bx * 4, by * 4, w, h, dst, oneBitAlpha: false, alwaysFourColor: true );\r\n\r\n\t\t\tvar alphas = BuildDxt5AlphaPalette( a0, a1 );\r\n\t\t\tfor ( var py = 0; py \u003C 4; py\u002B\u002B )\r\n\t\t\tfor ( var px = 0; px \u003C 4; px\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar x = bx * 4 \u002B px;\r\n\t\t\t\tvar y = by * 4 \u002B py;\r\n\t\t\t\tif ( x \u003E= w || y \u003E= h ) continue;\r\n\t\t\t\tvar idx = py * 4 \u002B px;\r\n\t\t\t\tvar code = (int)((aBits \u003E\u003E (idx * 3)) \u0026 0x7);\r\n\t\t\t\tdst[(y * w \u002B x) * 4 \u002B 3] = alphas[code];\r\n\t\t\t}\r\n\t\t\toffset \u002B= 16;\r\n\t\t}\r\n\t\treturn dst;\r\n\t}\r\n\r\n\tstatic byte[] BuildDxt5AlphaPalette( byte a0, byte a1 )\r\n\t{\r\n\t\tvar p = new byte[8];\r\n\t\tp[0] = a0;\r\n\t\tp[1] = a1;\r\n\t\tif ( a0 \u003E a1 )\r\n\t\t{\r\n\t\t\tfor ( var i = 1; i \u003C= 6; i\u002B\u002B )\r\n\t\t\t\tp[i \u002B 1] = (byte)(((7 - i) * a0 \u002B i * a1) / 7);\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tfor ( var i = 1; i \u003C= 4; i\u002B\u002B )\r\n\t\t\t\tp[i \u002B 1] = (byte)(((5 - i) * a0 \u002B i * a1) / 5);\r\n\t\t\tp[6] = 0;\r\n\t\t\tp[7] = 255;\r\n\t\t}\r\n\t\treturn p;\r\n\t}\r\n\r\n\t/// \u003Cparam name=\u0022alwaysFourColor\u0022\u003EDXT3/DXT5 colour blocks ignore the c0 \u0026lt;= c1 three-colour mode.\u003C/param\u003E\r\n\tstatic void DecodeDxt1Block( byte[] src, int offset, int blockX, int blockY, int w, int h, byte[] dst, bool oneBitAlpha, bool alwaysFourColor = false )\r\n\t{\r\n\t\tvar c0 = BitConverter.ToUInt16( src, offset );\r\n\t\tvar c1 = BitConverter.ToUInt16( src, offset \u002B 2 );\r\n\t\tvar indices = BitConverter.ToUInt32( src, offset \u002B 4 );\r\n\t\tvar colors = new byte[4 * 4];\r\n\t\tRgb565( c0, out colors[0], out colors[1], out colors[2] );\r\n\t\tcolors[3] = 255;\r\n\t\tRgb565( c1, out colors[4], out colors[5], out colors[6] );\r\n\t\tcolors[7] = 255;\r\n\r\n\t\tif ( c0 \u003E c1 || alwaysFourColor )\r\n\t\t{\r\n\t\t\tfor ( var i = 0; i \u003C 3; i\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tcolors[8 \u002B i] = (byte)((2 * colors[i] \u002B colors[4 \u002B i]) / 3);\r\n\t\t\t\tcolors[12 \u002B i] = (byte)((colors[i] \u002B 2 * colors[4 \u002B i]) / 3);\r\n\t\t\t}\r\n\t\t\tcolors[11] = 255;\r\n\t\t\tcolors[15] = 255;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tfor ( var i = 0; i \u003C 3; i\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tcolors[8 \u002B i] = (byte)((colors[i] \u002B colors[4 \u002B i]) / 2);\r\n\t\t\t\tcolors[12 \u002B i] = 0;\r\n\t\t\t}\r\n\t\t\tcolors[11] = 255;\r\n\t\t\tcolors[15] = oneBitAlpha ? (byte)0 : (byte)255;\r\n\t\t}\r\n\r\n\t\tfor ( var py = 0; py \u003C 4; py\u002B\u002B )\r\n\t\tfor ( var px = 0; px \u003C 4; px\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar x = blockX \u002B px;\r\n\t\t\tvar y = blockY \u002B py;\r\n\t\t\tif ( x \u003E= w || y \u003E= h ) continue;\r\n\t\t\tvar code = (int)((indices \u003E\u003E (2 * (py * 4 \u002B px))) \u0026 0x3);\r\n\t\t\tvar di = (y * w \u002B x) * 4;\r\n\t\t\tvar ci = code * 4;\r\n\t\t\tdst[di] = colors[ci];\r\n\t\t\tdst[di \u002B 1] = colors[ci \u002B 1];\r\n\t\t\tdst[di \u002B 2] = colors[ci \u002B 2];\r\n\t\t\tdst[di \u002B 3] = colors[ci \u002B 3];\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void Rgb565( ushort c, out byte r, out byte g, out byte b )\r\n\t{\r\n\t\tvar ri = (c \u003E\u003E 11) \u0026 0x1F;\r\n\t\tvar gi = (c \u003E\u003E 5) \u0026 0x3F;\r\n\t\tvar bi = c \u0026 0x1F;\r\n\t\tr = (byte)((ri \u003C\u003C 3) | (ri \u003E\u003E 2));\r\n\t\tg = (byte)((gi \u003C\u003C 2) | (gi \u003E\u003E 4));\r\n\t\tb = (byte)((bi \u003C\u003C 3) | (bi \u003E\u003E 2));\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Assembly.cs","FileName":"Assembly.cs","PackageType":"library","CodeKind":"Game","AssetVersionId":422538,"IsPrivate":false,"Code":"global using Sandbox;\r\nglobal using System;\r\nglobal using System.Collections.Generic;\r\nglobal using System.Linq;\r\n\r\n// Runtime assembly intentionally empty \u2014 conversion lives in Editor.\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"UnitTests/VtfReaderTests.cs","FileName":"VtfReaderTests.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":422538,"IsPrivate":false,"Code":"using System.IO;\r\nusing Microsoft.VisualStudio.TestTools.UnitTesting;\r\nusing GmodAssetImporter;\r\n\r\n[TestClass]\r\npublic class VtfReaderTests\r\n{\r\n\t[TestMethod]\r\n\tpublic void DecodesBgra8888Vtf()\r\n\t{\r\n\t\tvar image = VtfReader.Load( BuildVtf71( withThumbnail: false ) );\r\n\t\tAssert.AreEqual( 2, image.Width );\r\n\t\tAssert.AreEqual( 2, image.Height );\r\n\t\tAssert.AreEqual( 255, image.Rgba[0] ); // R\r\n\t\tAssert.AreEqual( 0, image.Rgba[1] );\r\n\t\tAssert.AreEqual( 0, image.Rgba[2] );\r\n\t}\r\n\r\n\t[TestMethod]\r\n\tpublic void SkipsLowResThumbnailBeforeHighResImage()\r\n\t{\r\n\t\tvar image = VtfReader.Load( BuildVtf71( withThumbnail: true ) );\r\n\t\tAssert.AreEqual( 255, image.Rgba[0] ); // first high-res pixel is red, not the thumbnail\r\n\t\tAssert.AreEqual( 0, image.Rgba[1] );\r\n\t\tAssert.AreEqual( 0, image.Rgba[2] );\r\n\t\tAssert.AreEqual( 255, image.Rgba[13] ); // last pixel white\r\n\t}\r\n\r\n\t/// \u003Csummary\u003EMinimal VTF 7.1 with a 2x2 BGRA8888 image and optional 1x1 BGRA8888 thumbnail.\u003C/summary\u003E\r\n\tstatic byte[] BuildVtf71( bool withThumbnail )\r\n\t{\r\n\t\tusing var ms = new MemoryStream();\r\n\t\tusing ( var bw = new BinaryWriter( ms, System.Text.Encoding.ASCII, leaveOpen: true ) )\r\n\t\t{\r\n\t\t\tbw.Write( new byte[] { (byte)\u0027V\u0027, (byte)\u0027T\u0027, (byte)\u0027F\u0027, 0 } );\r\n\t\t\tbw.Write( 7u ); // major\r\n\t\t\tbw.Write( 1u ); // minor\r\n\t\t\tbw.Write( 64u ); // headerSize\r\n\t\t\tbw.Write( (ushort)2 ); // width\r\n\t\t\tbw.Write( (ushort)2 ); // height\r\n\t\t\tbw.Write( 0u ); // flags\r\n\t\t\tbw.Write( (ushort)1 ); // frames\r\n\t\t\tbw.Write( (ushort)0 ); // firstFrame\r\n\t\t\tbw.Write( new byte[4] ); // pad\r\n\t\t\tbw.Write( 1f ); bw.Write( 1f ); bw.Write( 1f ); // reflectivity\r\n\t\t\tbw.Write( new byte[4] ); // pad\r\n\t\t\tbw.Write( 1f ); // bump\r\n\t\t\tbw.Write( 12 ); // high format BGRA8888\r\n\t\t\tbw.Write( (byte)1 ); // mipcount\r\n\t\t\tbw.Write( withThumbnail ? 12 : -1 ); // low format (int)\r\n\t\t\tbw.Write( (byte)(withThumbnail ? 1 : 0) ); // low w\r\n\t\t\tbw.Write( (byte)(withThumbnail ? 1 : 0) ); // low h\r\n\t\t\tbw.Flush();\r\n\t\t\twhile ( ms.Length \u003C 64 )\r\n\t\t\t\tms.WriteByte( 0 );\r\n\r\n\t\t\tms.Position = 64;\r\n\t\t\tif ( withThumbnail )\r\n\t\t\t\tbw.Write( new byte[] { 9, 9, 9, 255 } );\r\n\r\n\t\t\tbw.Write( new byte[]\r\n\t\t\t{\r\n\t\t\t\t0, 0, 255, 255, // red in BGRA\r\n\t\t\t\t0, 255, 0, 255, // green\r\n\t\t\t\t255, 0, 0, 255, // blue\r\n\t\t\t\t255, 255, 255, 255 // white\r\n\t\t\t} );\r\n\t\t\tbw.Flush();\r\n\t\t}\r\n\r\n\t\treturn ms.ToArray();\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Assembly.cs","FileName":"Assembly.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"// GmodAssetImporter editor assembly.\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"UnitTests/PathResolveTests.cs","FileName":"PathResolveTests.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.IO;\r\nusing Microsoft.VisualStudio.TestTools.UnitTesting;\r\nusing GmodAssetImporter;\r\n\r\n[TestClass]\r\npublic class PathResolveTests\r\n{\r\n\t[TestMethod]\r\n\tpublic void DetectsGmodRootFromNestedModelPath()\r\n\t{\r\n\t\tvar root = Path.Combine( Path.GetTempPath(), \u0022gmodassetimporter_gmod_\u0022 \u002B Guid.NewGuid().ToString( \u0022N\u0022 ) );\r\n\t\tvar models = Path.Combine( root, \u0022models\u0022, \u0022weapons\u0022 );\r\n\t\tDirectory.CreateDirectory( models );\r\n\t\tDirectory.CreateDirectory( Path.Combine( root, \u0022materials\u0022 ) );\r\n\t\tvar mdl = Path.Combine( models, \u0022gun.mdl\u0022 );\r\n\t\tFile.WriteAllText( mdl, \u0022x\u0022 );\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar detected = PathResolve.DetectGmodRoot( mdl );\r\n\t\t\tAssert.AreEqual( Path.GetFullPath( root ), Path.GetFullPath( detected ) );\r\n\r\n\t\t\tvar rel = PathResolve.GetModelsRelativeDirectory( mdl, root );\r\n\t\t\tAssert.AreEqual( \u0022models/weapons\u0022, rel.Replace( \u0027\\\\\u0027, \u0027/\u0027 ) );\r\n\t\t}\r\n\t\tfinally\r\n\t\t{\r\n\t\t\ttry { Directory.Delete( root, true ); } catch { /* ignore */ }\r\n\t\t}\r\n\t}\r\n\r\n\t[TestMethod]\r\n\tpublic void StripsMeshExtensionsAndMakesAssetsRelative()\r\n\t{\r\n\t\tAssert.AreEqual( \u0022studio_w_m9\u0022, PathResolve.StripMeshSourceExtension( \u0022studio_w_m9.smd\u0022 ) );\r\n\t\tAssert.AreEqual( \u0022studio_w_m9\u0022, PathResolve.SanitizeAssetName( \u0022studio_w_m9.smd.smd\u0022 ) );\r\n\t\tAssert.AreEqual( \u0022weapon_m9_dm.vmat\u0022, PathResolve.MaterialRemapFromName( \u0022materials/foo/weapon_m9_dm.vmt\u0022 ) );\r\n\t\tAssert.AreEqual( \u0022weapon_m9_dm\u0022, PathResolve.MaterialSlotName( \u0022weapon_m9_dm.vmat\u0022 ) );\r\n\t\tAssert.AreEqual( \u0022gun.body\u0022, PathResolve.MaterialSlotName( \u0022models/weapons/gun.body\u0022 ) );\r\n\t\tAssert.AreEqual( \u0022gun.mag.vmat\u0022, PathResolve.MaterialRemapFromName( \u0022gun.mag\u0022 ) );\r\n\r\n\t\tvar abs = @\u0022F:\\game\\Assets\\test\\models\\weapons\\gun.fbx\u0022;\r\n\t\tvar rel = PathResolve.MakeAssetsRelative( abs, @\u0022F:\\game\\Assets\\test\u0022 );\r\n\t\tAssert.AreEqual( \u0022test/models/weapons/gun.fbx\u0022, rel.Replace( \u0027\\\\\u0027, \u0027/\u0027 ) );\r\n\t}\r\n\r\n\t[TestMethod]\r\n\tpublic void CountsVtfVmtInFolder()\r\n\t{\r\n\t\tvar root = Path.Combine( Path.GetTempPath(), \u0022gmodassetimporter_scan_\u0022 \u002B Guid.NewGuid().ToString( \u0022N\u0022 ) );\r\n\t\tvar mats = Path.Combine( root, \u0022materials\u0022, \u0022weapons\u0022 );\r\n\t\tDirectory.CreateDirectory( mats );\r\n\t\tFile.WriteAllText( Path.Combine( mats, \u0022gun.vtf\u0022 ), \u0022x\u0022 );\r\n\t\tFile.WriteAllText( Path.Combine( mats, \u0022gun.vmt\u0022 ), \u0022x\u0022 );\r\n\t\tFile.WriteAllText( Path.Combine( mats, \u0022scope.vtf\u0022 ), \u0022x\u0022 );\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar counts = PathResolve.CountSourceAssets( root, recursive: true );\r\n\t\t\tAssert.AreEqual( 2, counts.Vtf );\r\n\t\t\tAssert.AreEqual( 1, counts.Vmt );\r\n\t\t\tAssert.AreEqual( 0, counts.Mdl );\r\n\r\n\t\t\tvar token = PathResolve.GetMaterialsRelativeToken( Path.Combine( mats, \u0022gun.vtf\u0022 ), root );\r\n\t\t\tAssert.AreEqual( \u0022weapons/gun\u0022, token.Replace( \u0027\\\\\u0027, \u0027/\u0027 ) );\r\n\t\t}\r\n\t\tfinally\r\n\t\t{\r\n\t\t\ttry { Directory.Delete( root, true ); } catch { /* ignore */ }\r\n\t\t}\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"UnitTests/VmatWriterTests.cs","FileName":"VmatWriterTests.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":422538,"IsPrivate":false,"Code":"using System.Text;\r\nusing Microsoft.VisualStudio.TestTools.UnitTesting;\r\nusing GmodAssetImporter;\r\n\r\n[TestClass]\r\npublic class VmatWriterTests\r\n{\r\n\t[TestMethod]\r\n\tpublic void EmitsAutoGeneratedHeaderAndComplexShader()\r\n\t{\r\n\t\tvar text = VmatWriter.Build( new VmatWriteRequest\r\n\t\t{\r\n\t\t\tColorTextureAssetPath = \u0022materials/test/source_files/gun_color.png\u0022,\r\n\t\t\tNormalTextureAssetPath = \u0022materials/test/source_files/gun_normal.png\u0022\r\n\t\t} );\r\n\r\n\t\tAssert.IsTrue( text.StartsWith( \u0022// THIS FILE IS AUTO-GENERATED\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022shaders/complex.shader\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022TextureColor \\\u0022materials/test/source_files/gun_color.png\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022TextureNormal \\\u0022materials/test/source_files/gun_normal.png\\\u0022\u0022 ) );\r\n\r\n\t\tvar bytes = new UTF8Encoding( false ).GetBytes( text );\r\n\t\tAssert.AreEqual( (byte)\u0027/\u0027, bytes[0] );\r\n\t\tAssert.AreEqual( (byte)\u0027/\u0027, bytes[1] );\r\n\t}\r\n\r\n\t[TestMethod]\r\n\tpublic void IncludesTranslucentAndSelfIllumWhenRequested()\r\n\t{\r\n\t\tvar text = VmatWriter.Build( new VmatWriteRequest\r\n\t\t{\r\n\t\t\tColorTextureAssetPath = \u0022materials/a/source_files/a_color.png\u0022,\r\n\t\t\tTranslucent = true,\r\n\t\t\tSelfIllumTextureAssetPath = \u0022materials/a/source_files/a_selfillum.png\u0022\r\n\t\t} );\r\n\r\n\t\tAssert.IsTrue( text.Contains( \u0022F_TRANSLUCENT 1\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022F_SELF_ILLUM 1\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022TextureSelfIllumMask\u0022 ) );\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Source1/BinaryUtil.cs","FileName":"BinaryUtil.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Text;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\nstatic class BinaryUtil\r\n{\r\n\tpublic static string ReadNullTerminated( BinaryReader br, int max )\r\n\t{\r\n\t\tvar bytes = br.ReadBytes( max );\r\n\t\tvar len = Array.IndexOf( bytes, (byte)0 );\r\n\t\tif ( len \u003C 0 ) len = bytes.Length;\r\n\t\treturn Encoding.ASCII.GetString( bytes, 0, len );\r\n\t}\r\n\r\n\tpublic static string ReadStringAt( BinaryReader br, long baseOffset, int relativeIndex )\r\n\t{\r\n\t\tif ( relativeIndex == 0 ) return \u0022\u0022;\r\n\t\tvar pos = br.BaseStream.Position;\r\n\t\tbr.BaseStream.Position = baseOffset \u002B relativeIndex;\r\n\t\tvar chars = new List\u003Cbyte\u003E();\r\n\t\twhile ( true )\r\n\t\t{\r\n\t\t\tvar b = br.ReadByte();\r\n\t\t\tif ( b == 0 ) break;\r\n\t\t\tchars.Add( b );\r\n\t\t\tif ( chars.Count \u003E 512 ) break;\r\n\t\t}\r\n\t\tbr.BaseStream.Position = pos;\r\n\t\treturn Encoding.ASCII.GetString( chars.ToArray() );\r\n\t}\r\n\r\n\tpublic static Vec3 ReadVec3( BinaryReader br ) =\u003E new( br.ReadSingle(), br.ReadSingle(), br.ReadSingle() );\r\n\r\n\tpublic static Quat ReadQuat( BinaryReader br ) =\u003E new( br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle() );\r\n\r\n\tpublic static float[] QuaternionToEulerDegrees( Quat q )\r\n\t{\r\n\t\t// Source-style Euler (pitch, yaw, roll) approximate for SMD\r\n\t\tvar sinrCosp = 2 * (q.W * q.X \u002B q.Y * q.Z);\r\n\t\tvar cosrCosp = 1 - 2 * (q.X * q.X \u002B q.Y * q.Y);\r\n\t\tvar roll = MathF.Atan2( sinrCosp, cosrCosp );\r\n\r\n\t\tvar sinp = 2 * (q.W * q.Y - q.Z * q.X);\r\n\t\tvar pitch = MathF.Abs( sinp ) \u003E= 1 ? MathF.CopySign( MathF.PI / 2, sinp ) : MathF.Asin( sinp );\r\n\r\n\t\tvar sinyCosp = 2 * (q.W * q.Z \u002B q.X * q.Y);\r\n\t\tvar cosyCosp = 1 - 2 * (q.Y * q.Y \u002B q.Z * q.Z);\r\n\t\tvar yaw = MathF.Atan2( sinyCosp, cosyCosp );\r\n\r\n\t\tconst float r2d = 180f / MathF.PI;\r\n\t\treturn new[] { pitch * r2d, yaw * r2d, roll * r2d };\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Util/PathResolve.cs","FileName":"PathResolve.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Linq;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\npublic static class PathResolve\r\n{\r\n\tpublic static string NormalizeSlashes( string path )\r\n\t{\r\n\t\treturn (path ?? \u0022\u0022).Replace( \u0027\\\\\u0027, \u0027/\u0027 ).Trim();\r\n\t}\r\n\r\n\tpublic static string StripExtension( string path )\r\n\t{\r\n\t\tpath = NormalizeSlashes( path );\r\n\t\tvar idx = path.LastIndexOf( \u0027.\u0027 );\r\n\t\treturn idx \u003E 0 ? path[..idx] : path;\r\n\t}\r\n\r\n\tpublic static string GetFileNameWithoutExtension( string path )\r\n\t{\r\n\t\tpath = NormalizeSlashes( path );\r\n\t\tvar name = Path.GetFileNameWithoutExtension( path );\r\n\t\treturn name ?? \u0022\u0022;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Walk upward from \u003Cparamref name=\u0022startPath\u0022/\u003E looking for a folder that contains models/ or materials/.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string DetectGmodRoot( string startPath )\r\n\t{\r\n\t\tif ( string.IsNullOrWhiteSpace( startPath ) )\r\n\t\t\treturn \u0022\u0022;\r\n\r\n\t\tvar dir = Directory.Exists( startPath ) ? startPath : Path.GetDirectoryName( startPath );\r\n\t\twhile ( !string.IsNullOrEmpty( dir ) )\r\n\t\t{\r\n\t\t\tvar models = Path.Combine( dir, \u0022models\u0022 );\r\n\t\t\tvar materials = Path.Combine( dir, \u0022materials\u0022 );\r\n\t\t\tif ( Directory.Exists( models ) || Directory.Exists( materials ) )\r\n\t\t\t\treturn dir;\r\n\r\n\t\t\t// GarrysMod\\garrysmod layout\r\n\t\t\tvar garrysmod = Path.Combine( dir, \u0022garrysmod\u0022 );\r\n\t\t\tif ( Directory.Exists( Path.Combine( garrysmod, \u0022models\u0022 ) ) || Directory.Exists( Path.Combine( garrysmod, \u0022materials\u0022 ) ) )\r\n\t\t\t\treturn garrysmod;\r\n\r\n\t\t\tdir = Path.GetDirectoryName( dir );\r\n\t\t}\r\n\r\n\t\treturn \u0022\u0022;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// If path is under gmodRoot/models/..., return models/... relative path without extension.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string GetModelsRelativeDirectory( string mdlPath, string gmodRoot )\r\n\t{\r\n\t\tmdlPath = NormalizeSlashes( Path.GetFullPath( mdlPath ) );\r\n\t\tgmodRoot = NormalizeSlashes( Path.GetFullPath( gmodRoot ?? \u0022\u0022 ) );\r\n\r\n\t\tif ( string.IsNullOrEmpty( gmodRoot ) )\r\n\t\t{\r\n\t\t\tvar dir = NormalizeSlashes( Path.GetDirectoryName( mdlPath ) ?? \u0022\u0022 );\r\n\t\t\tvar idx = dir.IndexOf( \u0022/models/\u0022, StringComparison.OrdinalIgnoreCase );\r\n\t\t\tif ( idx \u003E= 0 )\r\n\t\t\t\treturn dir[(idx \u002B 1)..];\r\n\t\t\treturn \u0022models\u0022;\r\n\t\t}\r\n\r\n\t\tvar modelsRoot = NormalizeSlashes( Path.Combine( gmodRoot, \u0022models\u0022 ) );\r\n\t\tvar mdlDir = NormalizeSlashes( Path.GetDirectoryName( mdlPath ) ?? \u0022\u0022 );\r\n\t\tif ( mdlDir.StartsWith( modelsRoot, StringComparison.OrdinalIgnoreCase ) )\r\n\t\t{\r\n\t\t\tvar rel = mdlDir[modelsRoot.Length..].TrimStart( \u0027/\u0027 );\r\n\t\t\treturn string.IsNullOrEmpty( rel ) ? \u0022models\u0022 : $\u0022models/{rel}\u0022;\r\n\t\t}\r\n\r\n\t\treturn \u0022models\u0022;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Relative materials path for a VTF/VMT under materials/\u2026 (no extension), e.g. models/weapons/gun.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string GetMaterialsRelativeToken( string materialFilePath, string gmodRoot )\r\n\t{\r\n\t\tmaterialFilePath = NormalizeSlashes( Path.GetFullPath( materialFilePath ) );\r\n\t\tgmodRoot = string.IsNullOrWhiteSpace( gmodRoot ) ? \u0022\u0022 : NormalizeSlashes( Path.GetFullPath( gmodRoot ) );\r\n\r\n\t\tstring relative = null;\r\n\t\tif ( !string.IsNullOrEmpty( gmodRoot ) )\r\n\t\t{\r\n\t\t\tvar materialsRoot = NormalizeSlashes( Path.Combine( gmodRoot, \u0022materials\u0022 ) );\r\n\t\t\tif ( materialFilePath.StartsWith( materialsRoot \u002B \u0022/\u0022, StringComparison.OrdinalIgnoreCase )\r\n\t\t\t\t|| materialFilePath.StartsWith( materialsRoot, StringComparison.OrdinalIgnoreCase ) )\r\n\t\t\t{\r\n\t\t\t\trelative = materialFilePath.Length \u003E materialsRoot.Length\r\n\t\t\t\t\t? materialFilePath[materialsRoot.Length..].TrimStart( \u0027/\u0027 )\r\n\t\t\t\t\t: \u0022\u0022;\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tif ( relative == null )\r\n\t\t{\r\n\t\t\tvar idx = materialFilePath.IndexOf( \u0022/materials/\u0022, StringComparison.OrdinalIgnoreCase );\r\n\t\t\tif ( idx \u003E= 0 )\r\n\t\t\t\trelative = materialFilePath[(idx \u002B \u0022/materials/\u0022.Length)..];\r\n\t\t}\r\n\r\n\t\trelative ??= Path.GetFileName( materialFilePath );\r\n\t\treturn StripExtension( relative.Trim( \u0027/\u0027 ) );\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Count Source1 texture/material files under a folder (for UI / batch detection).\r\n\t/// \u003C/summary\u003E\r\n\tpublic static (int Vtf, int Vmt, int Mdl) CountSourceAssets( string folder, bool recursive )\r\n\t{\r\n\t\tif ( !Directory.Exists( folder ) )\r\n\t\t\treturn (0, 0, 0);\r\n\r\n\t\tvar search = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;\r\n\t\treturn (\r\n\t\t\tDirectory.GetFiles( folder, \u0022*.vtf\u0022, search ).Length,\r\n\t\t\tDirectory.GetFiles( folder, \u0022*.vmt\u0022, search ).Length,\r\n\t\t\tDirectory.GetFiles( folder, \u0022*.mdl\u0022, search ).Length\r\n\t\t);\r\n\t}\r\n\r\n\tpublic static string CombineAsset( params string[] parts )\r\n\t{\r\n\t\treturn NormalizeSlashes( string.Join( \u0022/\u0022, parts.Where( p =\u003E !string.IsNullOrWhiteSpace( p ) ).Select( p =\u003E p.Trim( \u0027/\u0027 ) ) ) );\r\n\t}\r\n\r\n\tpublic static IEnumerable\u003Cstring\u003E EnumerateMaterialSearchRoots( string gmodRoot, IEnumerable\u003Cstring\u003E cdMaterials )\r\n\t{\r\n\t\tif ( !string.IsNullOrWhiteSpace( gmodRoot ) )\r\n\t\t{\r\n\t\t\tyield return Path.Combine( gmodRoot, \u0022materials\u0022 );\r\n\t\t\tyield return gmodRoot;\r\n\t\t}\r\n\r\n\t\tforeach ( var cd in cdMaterials ?? Array.Empty\u003Cstring\u003E() )\r\n\t\t{\r\n\t\t\tvar clean = NormalizeSlashes( cd ).Trim( \u0027/\u0027 );\r\n\t\t\tif ( string.IsNullOrEmpty( clean ) )\r\n\t\t\t\tcontinue;\r\n\r\n\t\t\tif ( !string.IsNullOrWhiteSpace( gmodRoot ) )\r\n\t\t\t\tyield return Path.Combine( gmodRoot, \u0022materials\u0022, clean.Replace( \u0027/\u0027, Path.DirectorySeparatorChar ) );\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static string FindExistingFile( IEnumerable\u003Cstring\u003E candidates )\r\n\t{\r\n\t\tforeach ( var c in candidates )\r\n\t\t{\r\n\t\t\tif ( !string.IsNullOrWhiteSpace( c ) \u0026\u0026 File.Exists( c ) )\r\n\t\t\t\treturn c;\r\n\t\t}\r\n\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static string SanitizeAssetName( string name )\r\n\t{\r\n\t\tname = StripMeshSourceExtension( NormalizeSlashes( name ?? \u0022\u0022 ).Trim( \u0027/\u0027 ) );\r\n\t\t// Material tokens can be paths \u2014 keep leaf name only for FBX/VMDL slots.\r\n\t\tname = Path.GetFileName( name ) ?? name;\r\n\t\tname = StripMeshSourceExtension( name );\r\n\t\tforeach ( var c in Path.GetInvalidFileNameChars() )\r\n\t\t\tname = name.Replace( c, \u0027_\u0027 );\r\n\t\treturn name.Replace( \u0027 \u0027, \u0027_\u0027 );\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// ModelDoc DefaultMaterialGroup \u0022from\u0022 name: FBX material slot \u002B \u0022.vmat\u0022.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string MaterialRemapFromName( string sourceMaterialName )\r\n\t{\r\n\t\tvar slot = MaterialSlotName( sourceMaterialName );\r\n\t\tif ( string.IsNullOrEmpty( slot ) )\r\n\t\t\tslot = \u0022default\u0022;\r\n\t\treturn slot \u002B \u0022.vmat\u0022;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Leaf material name with only known asset extensions stripped, so names like\r\n\t/// \u0022gun.body\u0022 and \u0022gun.mag\u0022 stay distinct slots.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string MaterialSlotName( string sourceMaterialName )\r\n\t{\r\n\t\tvar slot = SanitizeAssetName( sourceMaterialName ?? \u0022\u0022 );\r\n\t\treturn string.IsNullOrEmpty( slot ) ? \u0022default\u0022 : slot;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Strip trailing mesh/source extensions (.smd, .fbx, \u2026) so names don\u0027t become foo.smd.fbx.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string StripMeshSourceExtension( string name )\r\n\t{\r\n\t\tname = NormalizeSlashes( name ?? \u0022\u0022 );\r\n\t\tstring[] exts = { \u0022.smd\u0022, \u0022.fbx\u0022, \u0022.dmx\u0022, \u0022.obj\u0022, \u0022.mdl\u0022, \u0022.vmdl\u0022, \u0022.vta\u0022, \u0022.vmt\u0022, \u0022.vmat\u0022 };\r\n\t\tvar stripped = true;\r\n\t\twhile ( stripped )\r\n\t\t{\r\n\t\t\tstripped = false;\r\n\t\t\tforeach ( var ext in exts )\r\n\t\t\t{\r\n\t\t\t\tif ( name.EndsWith( ext, StringComparison.OrdinalIgnoreCase ) )\r\n\t\t\t\t{\r\n\t\t\t\t\tname = name[..^ext.Length];\r\n\t\t\t\t\tstripped = true;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn name;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Convert an absolute path (or path under output root) into a game asset path\r\n\t/// relative to the project\u0027s Assets folder, e.g. test/models/weapons/gun.fbx.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string MakeAssetsRelative( string absolutePath, string outputRoot = null )\r\n\t{\r\n\t\tif ( string.IsNullOrWhiteSpace( absolutePath ) )\r\n\t\t\treturn \u0022\u0022;\r\n\r\n\t\tvar abs = NormalizeSlashes( Path.GetFullPath( absolutePath ) );\r\n\t\tconst string marker = \u0022/assets/\u0022;\r\n\t\tvar idx = abs.IndexOf( marker, StringComparison.OrdinalIgnoreCase );\r\n\t\tif ( idx \u003E= 0 )\r\n\t\t\treturn abs[(idx \u002B marker.Length)..].Trim( \u0027/\u0027 );\r\n\r\n\t\tif ( !string.IsNullOrWhiteSpace( outputRoot ) )\r\n\t\t{\r\n\t\t\tvar root = NormalizeSlashes( Path.GetFullPath( outputRoot ) ).TrimEnd( \u0027/\u0027 );\r\n\t\t\tif ( abs.StartsWith( root \u002B \u0022/\u0022, StringComparison.OrdinalIgnoreCase ) )\r\n\t\t\t\treturn abs[(root.Length \u002B 1)..];\r\n\t\t\tif ( abs.Equals( root, StringComparison.OrdinalIgnoreCase ) )\r\n\t\t\t\treturn \u0022\u0022;\r\n\t\t}\r\n\r\n\t\treturn Path.GetFileName( abs ) ?? \u0022\u0022;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003E\r\n\t/// Absolute file under OutputRoot \u2192 Assets-relative game path.\r\n\t/// \u003C/summary\u003E\r\n\tpublic static string AssetPathUnderOutput( string outputRoot, string relativeUnderOutput )\r\n\t{\r\n\t\tvar abs = Path.Combine( outputRoot ?? \u0022\u0022, (relativeUnderOutput ?? \u0022\u0022).Replace( \u0027/\u0027, Path.DirectorySeparatorChar ) );\r\n\t\treturn MakeAssetsRelative( abs, outputRoot );\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"UnitTests/FbxWriterTests.cs","FileName":"FbxWriterTests.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":422538,"IsPrivate":false,"Code":"using System.IO;\r\nusing Microsoft.VisualStudio.TestTools.UnitTesting;\r\nusing GmodAssetImporter;\r\n\r\n[TestClass]\r\npublic class FbxWriterTests\r\n{\r\n\t[TestMethod]\r\n\tpublic void WritesAsciiFbxWithVertices()\r\n\t{\r\n\t\tvar dir = Path.Combine( Path.GetTempPath(), \u0022gmodassetimporter_fbx_\u0022 \u002B System.Guid.NewGuid().ToString( \u0022N\u0022 ) );\r\n\t\tDirectory.CreateDirectory( dir );\r\n\t\tvar path = Path.Combine( dir, \u0022tri.fbx\u0022 );\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tvar mesh = new MeshExport { Name = \u0022tri\u0022 };\r\n\t\t\tmesh.Vertices.Add( new MeshVertex { Position = new Vec3( 0, 0, 0 ) } );\r\n\t\t\tmesh.Vertices.Add( new MeshVertex { Position = new Vec3( 1, 0, 0 ) } );\r\n\t\t\tmesh.Vertices.Add( new MeshVertex { Position = new Vec3( 0, 1, 0 ) } );\r\n\t\t\tmesh.Triangles.Add( new MeshTriangle { A = 0, B = 1, C = 2, MaterialName = \u0022weapon_m9\u0022 } );\r\n\r\n\t\t\tvar bones = new System.Collections.Generic.List\u003CBoneData\u003E\r\n\t\t\t{\r\n\t\t\t\tnew() { Name = \u0022root\u0022, Parent = -1 }\r\n\t\t\t};\r\n\r\n\t\t\tFbxWriter.WriteMesh( path, bones, mesh );\r\n\t\t\tvar text = File.ReadAllText( path );\r\n\t\t\tAssert.IsTrue( text.Contains( \u0022FBXVersion: 7400\u0022 ) );\r\n\t\t\tAssert.IsTrue( text.Contains( \u0022Vertices:\u0022 ) );\r\n\t\t\tAssert.IsTrue( text.Contains( \u0022LayerElementMaterial:\u0022 ) );\r\n\t\t\tAssert.IsTrue( text.Contains( \u0022Material::weapon_m9\u0022 ) );\r\n\t\t\tAssert.IsFalse( text.Contains( \u0022Deformer:\u0022 ) ); // static bind-pose mesh, no skin clusters\r\n\t\t}\r\n\t\tfinally\r\n\t\t{\r\n\t\t\ttry { Directory.Delete( dir, true ); } catch { /* ignore */ }\r\n\t\t}\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"UnitTests/VmdlWriterTests.cs","FileName":"VmdlWriterTests.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":422538,"IsPrivate":false,"Code":"using Microsoft.VisualStudio.TestTools.UnitTesting;\r\nusing GmodAssetImporter;\r\n\r\n[TestClass]\r\npublic class VmdlWriterTests\r\n{\r\n\t[TestMethod]\r\n\tpublic void EmitsKv3HeaderRenderMeshAndBindPose()\r\n\t{\r\n\t\tvar ctx = new ModelBuildContext\r\n\t\t{\r\n\t\t\tModelAssetDirectory = \u0022models/test\u0022,\r\n\t\t\tIncludeBindPose = true,\r\n\t\t\tMeshes =\r\n\t\t\t{\r\n\t\t\t\tnew MeshExport { Name = \u0022body\u0022, FileName = \u0022body.fbx\u0022 }\r\n\t\t\t},\r\n\t\t\tMaterialRemaps =\r\n\t\t\t{\r\n\t\t\t\tnew MaterialRemap { From = \u0022gun.vmat\u0022, To = \u0022materials/gun.vmat\u0022 }\r\n\t\t\t},\r\n\t\t\tAttachments =\r\n\t\t\t{\r\n\t\t\t\tnew AttachmentData { Name = \u0022muzzle\u0022, ParentBone = \u0022ValveBiped.muzzle\u0022, Origin = new Vec3( 1, 2, 3 ) }\r\n\t\t\t},\r\n\t\t\tBodyGroups =\r\n\t\t\t{\r\n\t\t\t\tnew ImporterBodyGroup\r\n\t\t\t\t{\r\n\t\t\t\t\tName = \u0022magazine\u0022,\r\n\t\t\t\t\tChoices =\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tnew ImporterBodyGroupChoice { Name = \u0022default\u0022, MeshNames = { \u0022body\u0022 } }\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t},\r\n\t\t\tAnimations =\r\n\t\t\t{\r\n\t\t\t\tnew AnimationClip { Name = \u0022idle\u0022, FileName = \u0022anim_idle.smd\u0022, FrameRate = 30, Looping = true }\r\n\t\t\t}\r\n\t\t};\r\n\r\n\t\tvar text = VmdlWriter.Build( ctx );\r\n\t\tAssert.IsTrue( text.StartsWith( \u0022\u003C!-- kv3\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022_class = \\\u0022RenderMeshFile\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022filename = \\\u0022models/test/body.fbx\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022_class = \\\u0022AnimBindPose\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022_class = \\\u0022Attachment\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022name = \\\u0022muzzle\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022_class = \\\u0022BodyGroup\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022_class = \\\u0022AnimFile\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022source_filename = \\\u0022models/test/anim_idle.smd\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022from = \\\u0022gun.vmat\\\u0022\u0022 ) );\r\n\t\tAssert.IsTrue( text.Contains( \u0022to = \\\u0022materials/gun.vmat\\\u0022\u0022 ) );\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Writers/VmatWriter.cs","FileName":"VmatWriter.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System.IO;\r\nusing System.Text;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\npublic sealed class VmatWriteRequest\r\n{\r\n\tpublic string ColorTextureAssetPath; // materials/.../name_color.png\r\n\tpublic string NormalTextureAssetPath;\r\n\tpublic string SelfIllumTextureAssetPath;\r\n\tpublic bool Translucent;\r\n\tpublic bool AlphaTest;\r\n\tpublic float Metalness = 0f;\r\n\tpublic float Roughness = 0.5f;\r\n}\r\n\r\npublic static class VmatWriter\r\n{\r\n\tstatic readonly Encoding Utf8NoBom = new UTF8Encoding( encoderShouldEmitUTF8Identifier: false );\r\n\r\n\tpublic static string Build( VmatWriteRequest req )\r\n\t{\r\n\t\tvar color = Escape( req.ColorTextureAssetPath ?? \u0022materials/default/default_color.tga\u0022 );\r\n\t\tvar normal = Escape( req.NormalTextureAssetPath ?? \u0022materials/default/default_normal.tga\u0022 );\r\n\t\tvar roughDefault = \u0022materials/default/default_rough.tga\u0022;\r\n\t\tvar aoDefault = \u0022materials/default/default_ao.tga\u0022;\r\n\r\n\t\tvar sb = new StringBuilder();\r\n\t\tsb.AppendLine( \u0022// THIS FILE IS AUTO-GENERATED\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022Layer0\u0022 );\r\n\t\tsb.AppendLine( \u0022{\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tshader \\\u0022shaders/complex.shader\\\u0022\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- PBR ----\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tF_SPECULAR 1\u0022 );\r\n\t\tif ( req.Translucent )\r\n\t\t\tsb.AppendLine( \u0022\\tF_TRANSLUCENT 1\u0022 );\r\n\t\tif ( req.AlphaTest )\r\n\t\t\tsb.AppendLine( \u0022\\tF_ALPHA_TEST 1\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- Ambient Occlusion ----\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_flAmbientOcclusionDirectDiffuse \\\u00220.000\\\u0022\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_flAmbientOcclusionDirectSpecular \\\u00220.000\\\u0022\u0022 );\r\n\t\tsb.AppendLine( $\u0022\\tTextureAmbientOcclusion \\\u0022{aoDefault}\\\u0022\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- Color ----\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_flModelTintAmount \\\u00221.000\\\u0022\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_vColorTint \\\u0022[1.000000 1.000000 1.000000 0.000000]\\\u0022\u0022 );\r\n\t\tsb.AppendLine( $\u0022\\tTextureColor \\\u0022{color}\\\u0022\u0022 );\r\n\t\tsb.AppendLine( $\u0022\\tTextureNormal \\\u0022{normal}\\\u0022\u0022 );\r\n\t\tsb.AppendLine( $\u0022\\tTextureRoughness \\\u0022{roughDefault}\\\u0022\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- Fade ----\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_flFadeExponent \\\u00221.000\\\u0022\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- Fog ----\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_bFogEnabled \\\u00221\\\u0022\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- Metalness ----\u0022 );\r\n\t\tsb.AppendLine( $\u0022\\tg_flMetalness \\\u0022{req.Metalness:0.000}\\\u0022\u0022 );\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- Roughness ----\u0022 );\r\n\t\tsb.AppendLine( $\u0022\\tg_flRoughnessScaleFactor \\\u0022{req.Roughness:0.000}\\\u0022\u0022 );\r\n\r\n\t\tif ( !string.IsNullOrWhiteSpace( req.SelfIllumTextureAssetPath ) )\r\n\t\t{\r\n\t\t\tsb.AppendLine();\r\n\t\t\tsb.AppendLine( \u0022\\t//---- Self Illum ----\u0022 );\r\n\t\t\tsb.AppendLine( \u0022\\tF_SELF_ILLUM 1\u0022 );\r\n\t\t\tsb.AppendLine( $\u0022\\tTextureSelfIllumMask \\\u0022{Escape( req.SelfIllumTextureAssetPath )}\\\u0022\u0022 );\r\n\t\t}\r\n\r\n\t\tsb.AppendLine();\r\n\t\tsb.AppendLine( \u0022\\t//---- Texture Coordinates ----\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_vTexCoordOffset \\\u0022[0.000 0.000]\\\u0022\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_vTexCoordScale \\\u0022[1.000 1.000]\\\u0022\u0022 );\r\n\t\tsb.AppendLine( \u0022\\tg_vTexCoordScrollSpeed \\\u0022[0.000 0.000]\\\u0022\u0022 );\r\n\t\tsb.AppendLine( \u0022}\u0022 );\r\n\t\treturn sb.ToString();\r\n\t}\r\n\r\n\tpublic static void Write( string absolutePath, VmatWriteRequest req )\r\n\t{\r\n\t\tDirectory.CreateDirectory( Path.GetDirectoryName( absolutePath )! );\r\n\t\tFile.WriteAllText( absolutePath, Build( req ), Utf8NoBom );\r\n\t}\r\n\r\n\tstatic string Escape( string path ) =\u003E PathResolve.NormalizeSlashes( path ?? \u0022\u0022 );\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Pipeline/MdlPipeline.cs","FileName":"MdlPipeline.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Linq;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\n/// \u003Csummary\u003EMDL/VVD/VTX to VMDL \u002B FBX (ModelDoc-preferred) conversion.\u003C/summary\u003E\r\npublic static class MdlPipeline\r\n{\r\n\tpublic static ConversionResult ConvertModel( string mdlPath, ConversionOptions options )\r\n\t{\r\n\t\tvar result = new ConversionResult();\r\n\t\tvar writeLog = options.OnLog ?? (_ =\u003E { });\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tif ( string.IsNullOrWhiteSpace( options.OutputRoot ) )\r\n\t\t\t\tthrow new InvalidOperationException( \u0022Output root is required\u0022 );\r\n\r\n\t\t\toptions.GmodRoot = string.IsNullOrWhiteSpace( options.GmodRoot )\r\n\t\t\t\t? PathResolve.DetectGmodRoot( mdlPath )\r\n\t\t\t\t: options.GmodRoot;\r\n\r\n\t\t\twriteLog( $\u0022Loading {mdlPath}\u0022 );\r\n\t\t\tvar mdl = MdlReader.Load( mdlPath );\r\n\r\n\t\t\tvar vvdPath = Path.ChangeExtension( mdlPath, \u0022.vvd\u0022 );\r\n\t\t\tif ( !File.Exists( vvdPath ) )\r\n\t\t\t\tthrow new FileNotFoundException( \u0022Companion .vvd not found\u0022, vvdPath );\r\n\r\n\t\t\tvar vtxPath = VtxReader.FindVtxPath( mdlPath );\r\n\t\t\tif ( vtxPath == null )\r\n\t\t\t\tthrow new FileNotFoundException( \u0022Companion .vtx not found\u0022, Path.ChangeExtension( mdlPath, \u0022.vtx\u0022 ) );\r\n\r\n\t\t\tvar vvd = VvdReader.Load( vvdPath );\r\n\t\t\tvar vtx = VtxReader.Load( vtxPath );\r\n\t\t\twriteLog( $\u0022VVD verts={vvd.Vertices.Count}, VTX bodyparts={vtx.BodyParts.Count}\u0022 );\r\n\r\n\t\t\tvar modelDirUnderOutput = options.PreservePaths\r\n\t\t\t\t? PathResolve.GetModelsRelativeDirectory( mdlPath, options.GmodRoot )\r\n\t\t\t\t: \u0022models/converted\u0022;\r\n\t\t\tvar modelName = PathResolve.SanitizeAssetName( Path.GetFileNameWithoutExtension( mdlPath ) );\r\n\t\t\tvar absModelDir = Path.Combine( options.OutputRoot, modelDirUnderOutput.Replace( \u0027/\u0027, Path.DirectorySeparatorChar ) );\r\n\t\t\tDirectory.CreateDirectory( absModelDir );\r\n\r\n\t\t\t// VMDL paths must be Assets-relative (e.g. test/models/...), not only models/...\r\n\t\t\tvar modelAssetDirectory = PathResolve.MakeAssetsRelative( absModelDir, options.OutputRoot );\r\n\r\n\t\t\tvar context = new ModelBuildContext\r\n\t\t\t{\r\n\t\t\t\tModelName = modelName,\r\n\t\t\t\tModelAssetDirectory = modelAssetDirectory,\r\n\t\t\t\tScale = options.ModelScale,\r\n\t\t\t\tIncludeBindPose = true\r\n\t\t\t};\r\n\t\t\tcontext.Bones.AddRange( mdl.Bones );\r\n\t\t\tcontext.Attachments.AddRange( mdl.Attachments );\r\n\r\n\t\t\t// Materials\r\n\t\t\tif ( !options.ImportMaterials \u0026\u0026 mdl.MaterialNames.Count \u003E 0 )\r\n\t\t\t\twriteLog( $\u0022Skipping {mdl.MaterialNames.Count} material(s) (Import materials off); model keeps expected .vmat paths\u0022 );\r\n\r\n\t\t\tvar materialResults = new Dictionary\u003Cint, MaterialBuildResult\u003E();\r\n\t\t\tfor ( var i = 0; i \u003C mdl.MaterialNames.Count; i\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar token = ResolveMaterialToken( mdl.MaterialNames[i], mdl.CdMaterials, options.GmodRoot );\r\n\t\t\t\tvar mat = options.ImportMaterials\r\n\t\t\t\t\t? MaterialPipeline.ConvertMaterial( token, options, mdl.CdMaterials, result )\r\n\t\t\t\t\t: MaterialPipeline.GetExpectedMaterial( token, options );\r\n\t\t\t\tmaterialResults[i] = mat;\r\n\t\t\t\tcontext.MaterialRemaps.Add( new MaterialRemap\r\n\t\t\t\t{\r\n\t\t\t\t\t// Must match FBX material slot name \u002B \u0022.vmat\u0022 (ModelDoc import convention)\r\n\t\t\t\t\tFrom = PathResolve.MaterialRemapFromName( mdl.MaterialNames[i] ),\r\n\t\t\t\t\tTo = mat.VmatAssetPath\r\n\t\t\t\t} );\r\n\t\t\t}\r\n\r\n\t\t\tBuildSkinGroups( mdl, materialResults, context );\r\n\r\n\t\t\t// Geometry from bodyparts / VTX \u2192 FBX (preferred by ModelDoc; SMD is deprecated)\r\n\t\t\tfor ( var bpIndex = 0; bpIndex \u003C mdl.BodyParts.Count; bpIndex\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar bp = mdl.BodyParts[bpIndex];\r\n\t\t\t\tvar bodyGroup = new ImporterBodyGroup { Name = PathResolve.StripMeshSourceExtension( bp.Name ) };\r\n\r\n\t\t\t\tfor ( var modelIndex = 0; modelIndex \u003C bp.Models.Count; modelIndex\u002B\u002B )\r\n\t\t\t\t{\r\n\t\t\t\t\tvar studioModel = bp.Models[modelIndex];\r\n\t\t\t\t\tvar meshName = PathResolve.SanitizeAssetName( $\u0022{bp.Name}_{studioModel.Name}\u0022 );\r\n\t\t\t\t\tif ( string.IsNullOrEmpty( meshName ) )\r\n\t\t\t\t\t\tmeshName = $\u0022mesh_{bpIndex}_{modelIndex}\u0022;\r\n\r\n\t\t\t\t\tvar export = BuildMeshExport( meshName, studioModel, mdl, vvd, vtx, bpIndex, modelIndex, materialResults );\r\n\t\t\t\t\tif ( export.Triangles.Count == 0 )\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\twriteLog( $\u0022Skipping empty mesh {meshName}\u0022 );\r\n\t\t\t\t\t\tcontinue;\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t\tvar fbxFile = meshName \u002B \u0022.fbx\u0022;\r\n\t\t\t\t\tvar fbxAbs = Path.Combine( absModelDir, fbxFile );\r\n\t\t\t\t\tFbxWriter.WriteMesh( fbxAbs, context.Bones, export );\r\n\t\t\t\t\tresult.WrittenFiles.Add( fbxAbs );\r\n\r\n\t\t\t\t\texport.FileName = fbxFile;\r\n\t\t\t\t\tcontext.Meshes.Add( export );\r\n\r\n\t\t\t\t\tbodyGroup.Choices.Add( new ImporterBodyGroupChoice\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tName = PathResolve.StripMeshSourceExtension( studioModel.Name ),\r\n\t\t\t\t\t\tMeshNames = { meshName }\r\n\t\t\t\t\t} );\r\n\t\t\t\t}\r\n\r\n\t\t\t\tif ( bodyGroup.Choices.Count \u003E 0 )\r\n\t\t\t\t\tcontext.BodyGroups.Add( bodyGroup );\r\n\t\t\t}\r\n\r\n\t\t\tif ( context.Meshes.Count == 0 )\r\n\t\t\t\tthrow new InvalidOperationException( \u0022No meshes extracted from model\u0022 );\r\n\r\n\t\t\tif ( options.ExportAnimations )\r\n\t\t\t\tExportSequences( mdl, context, writeLog );\r\n\t\t\telse if ( mdl.Sequences.Count \u003E 0 )\r\n\t\t\t\twriteLog( $\u0022Skipping {mdl.Sequences.Count} sequence clip(s) (AnimBindPose only; enable Export sequence clips for names log)\u0022 );\r\n\r\n\t\t\tvar vmdlUnderOutput = PathResolve.CombineAsset( modelDirUnderOutput, modelName \u002B \u0022.vmdl\u0022 );\r\n\t\t\tvar vmdlAbs = Path.Combine( options.OutputRoot, vmdlUnderOutput.Replace( \u0027/\u0027, Path.DirectorySeparatorChar ) );\r\n\t\t\tvar vmdlAsset = PathResolve.MakeAssetsRelative( vmdlAbs, options.OutputRoot );\r\n\t\t\tVmdlWriter.Write( vmdlAbs, context );\r\n\t\t\tresult.WrittenFiles.Add( vmdlAbs );\r\n\r\n\t\t\tresult.Success = true;\r\n\t\t\tresult.Message = $\u0022Converted {modelName} -\u003E {vmdlAsset}\u0022;\r\n\t\t\twriteLog( result.Message );\r\n\t\t}\r\n\t\tcatch ( Exception ex )\r\n\t\t{\r\n\t\t\tresult.Success = false;\r\n\t\t\tresult.Message = ex.Message;\r\n\t\t\twriteLog( $\u0022Error: {ex.Message}\u0022 );\r\n\t\t}\r\n\r\n\t\treturn result;\r\n\t}\r\n\r\n\t/// \u003Csummary\u003EWrite only the VMAT/PNG materials an .mdl references; no mesh or .vmdl.\u003C/summary\u003E\r\n\tpublic static ConversionResult ConvertModelMaterials( string mdlPath, ConversionOptions options )\r\n\t{\r\n\t\tvar result = new ConversionResult();\r\n\t\tvar writeLog = options.OnLog ?? (_ =\u003E { });\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\toptions.GmodRoot = string.IsNullOrWhiteSpace( options.GmodRoot )\r\n\t\t\t\t? PathResolve.DetectGmodRoot( mdlPath )\r\n\t\t\t\t: options.GmodRoot;\r\n\r\n\t\t\twriteLog( $\u0022Loading materials for {mdlPath}\u0022 );\r\n\t\t\tvar mdl = MdlReader.Load( mdlPath );\r\n\t\t\tforeach ( var name in mdl.MaterialNames )\r\n\t\t\t{\r\n\t\t\t\tvar token = ResolveMaterialToken( name, mdl.CdMaterials, options.GmodRoot );\r\n\t\t\t\tMaterialPipeline.ConvertMaterial( token, options, mdl.CdMaterials, result );\r\n\t\t\t}\r\n\r\n\t\t\tresult.Success = true;\r\n\t\t\tresult.Message = $\u0022Converted {mdl.MaterialNames.Count} material(s) for {Path.GetFileNameWithoutExtension( mdlPath )}\u0022;\r\n\t\t\twriteLog( result.Message );\r\n\t\t}\r\n\t\tcatch ( Exception ex )\r\n\t\t{\r\n\t\t\tresult.Success = false;\r\n\t\t\tresult.Message = ex.Message;\r\n\t\t\twriteLog( $\u0022Error: {ex.Message}\u0022 );\r\n\t\t}\r\n\r\n\t\treturn result;\r\n\t}\r\n\r\n\tstatic string ResolveMaterialToken( string materialName, List\u003Cstring\u003E cdMaterials, string gmodRoot )\r\n\t{\r\n\t\tmaterialName = PathResolve.NormalizeSlashes( materialName ).Trim( \u0027/\u0027 );\r\n\t\tif ( cdMaterials.Count == 0 )\r\n\t\t\treturn materialName;\r\n\r\n\t\t// Source tries every $cdmaterials folder in order; the material name may itself contain subfolders\r\n\t\tvar roots = PathResolve.EnumerateMaterialSearchRoots( gmodRoot, null ).ToList();\r\n\t\tforeach ( var cd in cdMaterials )\r\n\t\t{\r\n\t\t\tvar candidate = PathResolve.CombineAsset( cd, materialName );\r\n\t\t\tif ( VmtParser.ResolveVmtPath( candidate, roots ) != null )\r\n\t\t\t\treturn candidate;\r\n\t\t}\r\n\r\n\t\treturn materialName.Contains( \u0027/\u0027 )\r\n\t\t\t? materialName\r\n\t\t\t: PathResolve.CombineAsset( cdMaterials[0], materialName );\r\n\t}\r\n\r\n\tstatic void BuildSkinGroups( MdlModel mdl, Dictionary\u003Cint, MaterialBuildResult\u003E materials, ModelBuildContext context )\r\n\t{\r\n\t\tif ( mdl.SkinFamilies.Count \u003C= 1 ) return;\r\n\r\n\t\tfor ( var family = 1; family \u003C mdl.SkinFamilies.Count; family\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar row = mdl.SkinFamilies[family];\r\n\t\t\tvar group = new MaterialGroupData { Name = $\u0022skin_{family}\u0022 };\r\n\t\t\tfor ( var slot = 0; slot \u003C row.Length \u0026\u0026 slot \u003C mdl.SkinReferenceCount; slot\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar matIndex = row[slot];\r\n\t\t\t\tif ( matIndex \u003C 0 || matIndex \u003E= mdl.MaterialNames.Count ) continue;\r\n\t\t\t\tif ( !materials.TryGetValue( matIndex, out var mat ) ) continue;\r\n\r\n\t\t\t\t// Remap the family-0 material for this slot \u2192 family material\r\n\t\t\t\tvar baseIndex = ResolveSkinMaterial( mdl, slot );\r\n\t\t\t\tvar sourceName = baseIndex \u003E= 0 \u0026\u0026 baseIndex \u003C mdl.MaterialNames.Count\r\n\t\t\t\t\t? mdl.MaterialNames[baseIndex]\r\n\t\t\t\t\t: mdl.MaterialNames[matIndex];\r\n\r\n\t\t\t\tgroup.Remaps.Add( new MaterialRemap\r\n\t\t\t\t{\r\n\t\t\t\t\tFrom = PathResolve.MaterialRemapFromName( sourceName ),\r\n\t\t\t\t\tTo = mat.VmatAssetPath\r\n\t\t\t\t} );\r\n\t\t\t}\r\n\r\n\t\t\tif ( group.Remaps.Count \u003E 0 )\r\n\t\t\t\tcontext.MaterialGroups.Add( group );\r\n\t\t}\r\n\t}\r\n\r\n\t/// \u003Csummary\u003EStudio mesh material is a skin reference; family 0 maps it to a texture index.\u003C/summary\u003E\r\n\tstatic int ResolveSkinMaterial( MdlModel mdl, int skinRef )\r\n\t{\r\n\t\tif ( mdl.SkinFamilies.Count == 0 ) return skinRef;\r\n\t\tvar row = mdl.SkinFamilies[0];\r\n\t\treturn skinRef \u003E= 0 \u0026\u0026 skinRef \u003C row.Length ? row[skinRef] : skinRef;\r\n\t}\r\n\r\n\tstatic MeshExport BuildMeshExport(\r\n\t\tstring meshName,\r\n\t\tMdlStudioModel studioModel,\r\n\t\tMdlModel mdl,\r\n\t\tVvdFile vvd,\r\n\t\tVtxFile vtx,\r\n\t\tint bpIndex,\r\n\t\tint modelIndex,\r\n\t\tDictionary\u003Cint, MaterialBuildResult\u003E materials )\r\n\t{\r\n\t\tvar export = new MeshExport { Name = meshName };\r\n\t\tvar baseVertex = studioModel.VertexIndex;\r\n\r\n\t\t// Copy this model\u0027s vertices into local list\r\n\t\tfor ( var i = 0; i \u003C studioModel.VertexCount; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar srcIndex = baseVertex \u002B i;\r\n\t\t\tif ( srcIndex \u003C 0 || srcIndex \u003E= vvd.Vertices.Count )\r\n\t\t\t{\r\n\t\t\t\texport.Vertices.Add( new MeshVertex() );\r\n\t\t\t\tcontinue;\r\n\t\t\t}\r\n\r\n\t\t\tvar src = vvd.Vertices[srcIndex];\r\n\t\t\tvar copy = new MeshVertex\r\n\t\t\t{\r\n\t\t\t\tPosition = new Vec3( src.Position.X, src.Position.Y, src.Position.Z ),\r\n\t\t\t\tNormal = new Vec3( src.Normal.X, src.Normal.Y, src.Normal.Z ),\r\n\t\t\t\tUv = new Vec2( src.Uv.X, src.Uv.Y )\r\n\t\t\t};\r\n\t\t\tforeach ( var w in src.Weights )\r\n\t\t\t\tcopy.Weights.Add( new BoneWeight { Bone = w.Bone, Weight = w.Weight } );\r\n\t\t\texport.Vertices.Add( copy );\r\n\t\t}\r\n\r\n\t\tif ( bpIndex \u003C vtx.BodyParts.Count\r\n\t\t\t\u0026\u0026 modelIndex \u003C vtx.BodyParts[bpIndex].Models.Count\r\n\t\t\t\u0026\u0026 vtx.BodyParts[bpIndex].Models[modelIndex].Lods.Count \u003E 0 )\r\n\t\t{\r\n\t\t\tvar lod = vtx.BodyParts[bpIndex].Models[modelIndex].Lods[0];\r\n\t\t\tfor ( var meshIdx = 0; meshIdx \u003C lod.Meshes.Count; meshIdx\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar vtxMesh = lod.Meshes[meshIdx];\r\n\t\t\t\t// FBX material slot must match DefaultMaterialGroup remap \u0022from\u0022 (sans .vmat)\r\n\t\t\t\tvar materialName = \u0022default\u0022;\r\n\t\t\t\tvar meshVertexOffset = 0;\r\n\t\t\t\tif ( meshIdx \u003C studioModel.Meshes.Count )\r\n\t\t\t\t{\r\n\t\t\t\t\tvar studioMesh = studioModel.Meshes[meshIdx];\r\n\t\t\t\t\tmeshVertexOffset = studioMesh.VertexOffset;\r\n\t\t\t\t\tvar matId = ResolveSkinMaterial( mdl, studioMesh.Material );\r\n\t\t\t\t\tif ( matId \u003E= 0 \u0026\u0026 matId \u003C mdl.MaterialNames.Count )\r\n\t\t\t\t\t\tmaterialName = PathResolve.MaterialSlotName( mdl.MaterialNames[matId] );\r\n\t\t\t\t\telse if ( materials.TryGetValue( matId, out var mat ) )\r\n\t\t\t\t\t\tmaterialName = PathResolve.MaterialSlotName( mat.VmatAssetPath );\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// VTX vertex ids are relative to the studio mesh, not the studio model\r\n\t\t\t\tvar indices = vtxMesh.Indices;\r\n\t\t\t\tvar vertexCount = export.Vertices.Count;\r\n\t\t\t\tfor ( var i = 0; i \u002B 2 \u003C indices.Count; i \u002B= 3 )\r\n\t\t\t\t{\r\n\t\t\t\t\tvar a = meshVertexOffset \u002B indices[i];\r\n\t\t\t\t\tvar b = meshVertexOffset \u002B indices[i \u002B 1];\r\n\t\t\t\t\tvar c = meshVertexOffset \u002B indices[i \u002B 2];\r\n\t\t\t\t\tif ( a \u003E= vertexCount || b \u003E= vertexCount || c \u003E= vertexCount )\r\n\t\t\t\t\t\tcontinue;\r\n\r\n\t\t\t\t\texport.Triangles.Add( new MeshTriangle\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tA = a,\r\n\t\t\t\t\t\tB = b,\r\n\t\t\t\t\t\tC = c,\r\n\t\t\t\t\t\tMaterialName = materialName\r\n\t\t\t\t\t} );\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn export;\r\n\t}\r\n\r\n\tstatic void ExportSequences( MdlModel mdl, ModelBuildContext context, Action\u003Cstring\u003E writeLog )\r\n\t{\r\n\t\t// ModelDoc rejects SMD as AnimFile (\u0022unrecognized file type\u0022). Until we decode Source\r\n\t\t// anim RLE into FBX clips, keep AnimBindPose only and surface sequence names in the log.\r\n\t\t_ = context;\r\n\t\tvar names = new List\u003Cstring\u003E();\r\n\t\tvar used = new HashSet\u003Cstring\u003E( StringComparer.OrdinalIgnoreCase );\r\n\t\tforeach ( var seq in mdl.Sequences )\r\n\t\t{\r\n\t\t\tvar animName = PathResolve.SanitizeAssetName( seq.Name );\r\n\t\t\tif ( string.IsNullOrEmpty( animName ) || !used.Add( animName ) )\r\n\t\t\t\tcontinue;\r\n\t\t\tnames.Add( animName );\r\n\t\t}\r\n\r\n\t\tif ( names.Count \u003E 0 )\r\n\t\t\twriteLog( $\u0022Sequence names ({names.Count}, not exported \u2014 need FBX anims): {string.Join( \u0022, \u0022, names.Take( 24 ) )}{(names.Count \u003E 24 ? \u0022\u2026\u0022 : \u0022\u0022)}\u0022 );\r\n\t}\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"Editor/Conversion/Source1/MdlReader.cs","FileName":"MdlReader.cs","PackageType":"library","CodeKind":"Editor","AssetVersionId":422538,"IsPrivate":false,"Code":"using System;\r\nusing System.Collections.Generic;\r\nusing System.IO;\r\nusing System.Text;\r\n\r\nnamespace GmodAssetImporter;\r\n\r\npublic sealed class MdlModel\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic int Version;\r\n\tpublic List\u003CBoneData\u003E Bones { get; } = new();\r\n\tpublic List\u003Cstring\u003E MaterialNames { get; } = new();\r\n\tpublic List\u003Cstring\u003E CdMaterials { get; } = new();\r\n\tpublic List\u003Cshort[]\u003E SkinFamilies { get; } = new();\r\n\tpublic int SkinReferenceCount;\r\n\tpublic List\u003CMdlBodyPart\u003E BodyParts { get; } = new();\r\n\tpublic List\u003CAttachmentData\u003E Attachments { get; } = new();\r\n\tpublic List\u003CMdlSequence\u003E Sequences { get; } = new();\r\n\tpublic List\u003CMdlAnimDesc\u003E AnimDescs { get; } = new();\r\n\tpublic byte[] RawData;\r\n}\r\n\r\npublic sealed class MdlBodyPart\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic List\u003CMdlStudioModel\u003E Models { get; } = new();\r\n}\r\n\r\npublic sealed class MdlStudioModel\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic int VertexCount;\r\n\tpublic int VertexIndex; // vertex index into VVD LOD0 (header stores a byte offset)\r\n\tpublic List\u003CMdlMesh\u003E Meshes { get; } = new();\r\n}\r\n\r\npublic sealed class MdlMesh\r\n{\r\n\tpublic int Material;\r\n\tpublic int VertexCount;\r\n\tpublic int VertexOffset; // relative to model\r\n}\r\n\r\npublic sealed class MdlSequence\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic int AnimIndex; // local anim index (first blend)\r\n\tpublic bool Looping;\r\n\tpublic float Fps = 30;\r\n}\r\n\r\npublic sealed class MdlAnimDesc\r\n{\r\n\tpublic string Name = \u0022\u0022;\r\n\tpublic float Fps = 30;\r\n\tpublic int FrameCount = 1;\r\n\tpublic int Flags;\r\n\tpublic int AnimIndex; // offset to anim data relative to this animdesc\r\n\tpublic long DescOffset;\r\n}\r\n\r\npublic static class MdlReader\r\n{\r\n\tconst int IdStudio = 0x54534449; // \u0022IDST\u0022\r\n\tconst int StudioVertexSize = 48; // sizeof( mstudiovertex_t )\r\n\r\n\tpublic static MdlModel Load( string path ) =\u003E Load( File.ReadAllBytes( path ) );\r\n\r\n\tpublic static MdlModel Load( byte[] data )\r\n\t{\r\n\t\tusing var ms = new MemoryStream( data );\r\n\t\tusing var br = new BinaryReader( ms, Encoding.ASCII, leaveOpen: true );\r\n\r\n\t\tvar id = br.ReadInt32();\r\n\t\tif ( id != IdStudio )\r\n\t\t\tthrow new InvalidDataException( $\u0022Invalid MDL id 0x{id:X8}\u0022 );\r\n\r\n\t\tvar version = br.ReadInt32();\r\n\t\tif ( version \u003C 44 || (version \u003E 49 \u0026\u0026 version != 53) )\r\n\t\t\tthrow new InvalidDataException( $\u0022Unsupported MDL version {version}\u0022 );\r\n\r\n\t\tbr.ReadInt32(); // checksum\r\n\t\tvar name = BinaryUtil.ReadNullTerminated( br, 64 );\r\n\t\tbr.ReadInt32(); // length\r\n\t\tms.Position \u002B= 12 * 6; // eye, illum, hull min/max, view bb min/max (6 vectors)\r\n\t\tbr.ReadInt32(); // flags\r\n\r\n\t\tvar numBones = br.ReadInt32();\r\n\t\tvar boneIndex = br.ReadInt32();\r\n\t\tbr.ReadInt32(); br.ReadInt32(); // bonecontrollers\r\n\t\tvar numHitboxSets = br.ReadInt32();\r\n\t\tvar hitboxSetIndex = br.ReadInt32();\r\n\t\tvar numLocalAnim = br.ReadInt32();\r\n\t\tvar localAnimIndex = br.ReadInt32();\r\n\t\tvar numLocalSeq = br.ReadInt32();\r\n\t\tvar localSeqIndex = br.ReadInt32();\r\n\t\tbr.ReadInt32(); br.ReadInt32(); // activitylistversion, eventsindexed\r\n\t\tvar numTextures = br.ReadInt32();\r\n\t\tvar textureIndex = br.ReadInt32();\r\n\t\tvar numCdTextures = br.ReadInt32();\r\n\t\tvar cdTextureIndex = br.ReadInt32();\r\n\t\tvar numSkinRef = br.ReadInt32();\r\n\t\tvar numSkinFamilies = br.ReadInt32();\r\n\t\tvar skinIndex = br.ReadInt32();\r\n\t\tvar numBodyParts = br.ReadInt32();\r\n\t\tvar bodyPartIndex = br.ReadInt32();\r\n\t\tvar numAttachments = br.ReadInt32();\r\n\t\tvar attachmentIndex = br.ReadInt32();\r\n\r\n\t\t// skip remaining header fields we don\u0027t need for offsets already captured\r\n\t\t_ = numHitboxSets;\r\n\t\t_ = hitboxSetIndex;\r\n\r\n\t\tvar mdl = new MdlModel\r\n\t\t{\r\n\t\t\tName = name,\r\n\t\t\tVersion = version,\r\n\t\t\tRawData = data,\r\n\t\t\tSkinReferenceCount = numSkinRef\r\n\t\t};\r\n\r\n\t\tReadBones( br, mdl, boneIndex, numBones );\r\n\t\tReadMaterials( br, mdl, textureIndex, numTextures, cdTextureIndex, numCdTextures );\r\n\t\tReadSkinFamilies( br, mdl, skinIndex, numSkinFamilies, numSkinRef );\r\n\t\tReadBodyParts( br, mdl, bodyPartIndex, numBodyParts );\r\n\t\tReadAttachments( br, mdl, attachmentIndex, numAttachments );\r\n\t\tReadAnimDescs( br, mdl, localAnimIndex, numLocalAnim );\r\n\t\tReadSequences( br, mdl, localSeqIndex, numLocalSeq );\r\n\r\n\t\treturn mdl;\r\n\t}\r\n\r\n\tstatic void ReadBones( BinaryReader br, MdlModel mdl, int index, int count )\r\n\t{\r\n\t\tfor ( var i = 0; i \u003C count; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar offset = index \u002B i * 216; // mstudiobone_t is 216 bytes on v48/49\r\n\t\t\tif ( offset \u002B 216 \u003E br.BaseStream.Length )\r\n\t\t\t{\r\n\t\t\t\t// older versions can be 184 \u2014 try smaller stride fallback\r\n\t\t\t\toffset = index \u002B i * 184;\r\n\t\t\t}\r\n\r\n\t\t\tbr.BaseStream.Position = offset;\r\n\t\t\tvar szNameIndex = br.ReadInt32();\r\n\t\t\tvar parent = br.ReadInt32();\r\n\t\t\tbr.BaseStream.Position = offset \u002B 8 \u002B 6 * 4; // skip bonecontroller[6]\r\n\t\t\tvar pos = BinaryUtil.ReadVec3( br );\r\n\t\t\tvar quat = BinaryUtil.ReadQuat( br );\r\n\t\t\tbr.BaseStream.Position = offset \u002B 8 \u002B 24 \u002B 12 \u002B 16 \u002B 12; // after pos/quat/rot\r\n\t\t\t// Actually layout: nameindex(4) parent(4) bonecontroller[6](24) pos(12) quat(16) rot(12) posscale(12) rotscale(12) poseToBone[12](48) qAlignment(16) flags(4) proc(8) surfaceprop(4) contents(4) unused[8](32)\r\n\t\t\t// Safer: re-read from known offsets\r\n\t\t\tbr.BaseStream.Position = offset;\r\n\t\t\tszNameIndex = br.ReadInt32();\r\n\t\t\tparent = br.ReadInt32();\r\n\t\t\tbr.BaseStream.Position = offset \u002B 32; // after controllers\r\n\t\t\tpos = BinaryUtil.ReadVec3( br );\r\n\t\t\tquat = BinaryUtil.ReadQuat( br );\r\n\t\t\tvar rot = BinaryUtil.ReadVec3( br );\r\n\r\n\t\t\tvar boneName = BinaryUtil.ReadStringAt( br, offset, szNameIndex );\r\n\t\t\tif ( string.IsNullOrEmpty( boneName ) )\r\n\t\t\t\tboneName = $\u0022bone{i}\u0022;\r\n\r\n\t\t\tvar euler = BinaryUtil.QuaternionToEulerDegrees( quat );\r\n\t\t\tmdl.Bones.Add( new BoneData\r\n\t\t\t{\r\n\t\t\t\tName = boneName,\r\n\t\t\t\tParent = parent,\r\n\t\t\t\tPosition = pos,\r\n\t\t\t\tQuaternion = quat,\r\n\t\t\t\tRotation = new Vec3( euler[0], euler[1], euler[2] )\r\n\t\t\t} );\r\n\t\t\t_ = rot;\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void ReadMaterials( BinaryReader br, MdlModel mdl, int textureIndex, int numTextures, int cdIndex, int numCd )\r\n\t{\r\n\t\tfor ( var i = 0; i \u003C numTextures; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar offset = textureIndex \u002B i * 64; // mstudiotexture_t\r\n\t\t\tbr.BaseStream.Position = offset;\r\n\t\t\tvar nameIndex = br.ReadInt32();\r\n\t\t\tvar name = BinaryUtil.ReadStringAt( br, offset, nameIndex );\r\n\t\t\tmdl.MaterialNames.Add( string.IsNullOrEmpty( name ) ? $\u0022material{i}\u0022 : name );\r\n\t\t}\r\n\r\n\t\tfor ( var i = 0; i \u003C numCd; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tbr.BaseStream.Position = cdIndex \u002B i * 4;\r\n\t\t\tvar nameIndex = br.ReadInt32();\r\n\t\t\tvar name = BinaryUtil.ReadStringAt( br, 0, nameIndex );\r\n\t\t\t// cdtextureindex entries are absolute offsets from file start\r\n\t\t\tif ( string.IsNullOrEmpty( name ) )\r\n\t\t\t{\r\n\t\t\t\tbr.BaseStream.Position = nameIndex;\r\n\t\t\t\tname = BinaryUtil.ReadNullTerminated( br, 256 );\r\n\t\t\t}\r\n\t\t\tmdl.CdMaterials.Add( PathResolve.NormalizeSlashes( name ).Trim( \u0027/\u0027 ) );\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void ReadSkinFamilies( BinaryReader br, MdlModel mdl, int skinIndex, int families, int refs )\r\n\t{\r\n\t\tif ( families \u003C= 0 || refs \u003C= 0 ) return;\r\n\t\tbr.BaseStream.Position = skinIndex;\r\n\t\tfor ( var f = 0; f \u003C families; f\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar row = new short[refs];\r\n\t\t\tfor ( var r = 0; r \u003C refs; r\u002B\u002B )\r\n\t\t\t\trow[r] = br.ReadInt16();\r\n\t\t\tmdl.SkinFamilies.Add( row );\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void ReadBodyParts( BinaryReader br, MdlModel mdl, int index, int count )\r\n\t{\r\n\t\tfor ( var i = 0; i \u003C count; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar bpOffset = index \u002B i * 16; // mstudiobodyparts_t\r\n\t\t\tbr.BaseStream.Position = bpOffset;\r\n\t\t\tvar szNameIndex = br.ReadInt32();\r\n\t\t\tvar numModels = br.ReadInt32();\r\n\t\t\tbr.ReadInt32(); // base\r\n\t\t\tvar modelIndex = br.ReadInt32();\r\n\t\t\tvar bpName = BinaryUtil.ReadStringAt( br, bpOffset, szNameIndex );\r\n\t\t\tif ( string.IsNullOrEmpty( bpName ) ) bpName = $\u0022body{i}\u0022;\r\n\r\n\t\t\tvar bp = new MdlBodyPart { Name = bpName };\r\n\t\t\tfor ( var m = 0; m \u003C numModels; m\u002B\u002B )\r\n\t\t\t{\r\n\t\t\t\tvar modelOffset = bpOffset \u002B modelIndex \u002B m * 148; // mstudiomodel_t ~148\r\n\t\t\t\tbr.BaseStream.Position = modelOffset;\r\n\t\t\t\tvar modelName = BinaryUtil.ReadNullTerminated( br, 64 );\r\n\t\t\t\tbr.ReadInt32(); // type\r\n\t\t\t\tbr.ReadSingle(); // boundingradius\r\n\t\t\t\tvar numMeshes = br.ReadInt32();\r\n\t\t\t\tvar meshIndex = br.ReadInt32();\r\n\t\t\t\tvar numVertices = br.ReadInt32();\r\n\t\t\t\tvar vertexByteOffset = br.ReadInt32(); // byte offset into VVD vertex data\r\n\t\t\t\t// tangentsindex, attachments, eyeballs follow \u2014 ignore\r\n\r\n\t\t\t\tvar studioModel = new MdlStudioModel\r\n\t\t\t\t{\r\n\t\t\t\t\tName = string.IsNullOrEmpty( modelName ) ? $\u0022{bpName}_{m}\u0022 : modelName,\r\n\t\t\t\t\tVertexCount = numVertices,\r\n\t\t\t\t\tVertexIndex = vertexByteOffset / StudioVertexSize\r\n\t\t\t\t};\r\n\r\n\t\t\t\tfor ( var me = 0; me \u003C numMeshes; me\u002B\u002B )\r\n\t\t\t\t{\r\n\t\t\t\t\tvar meshOffset = modelOffset \u002B meshIndex \u002B me * 116; // mstudiomesh_t\r\n\t\t\t\t\tbr.BaseStream.Position = meshOffset;\r\n\t\t\t\t\tvar material = br.ReadInt32();\r\n\t\t\t\t\tbr.ReadInt32(); // modelindex\r\n\t\t\t\t\tvar meshVertexCount = br.ReadInt32();\r\n\t\t\t\t\tvar meshVertexOffset = br.ReadInt32();\r\n\t\t\t\t\tstudioModel.Meshes.Add( new MdlMesh\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tMaterial = material,\r\n\t\t\t\t\t\tVertexCount = meshVertexCount,\r\n\t\t\t\t\t\tVertexOffset = meshVertexOffset\r\n\t\t\t\t\t} );\r\n\t\t\t\t}\r\n\r\n\t\t\t\tbp.Models.Add( studioModel );\r\n\t\t\t}\r\n\r\n\t\t\tmdl.BodyParts.Add( bp );\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void ReadAttachments( BinaryReader br, MdlModel mdl, int index, int count )\r\n\t{\r\n\t\tfor ( var i = 0; i \u003C count; i\u002B\u002B )\r\n\t\t{\r\n\t\t\t// mstudioattachment_t: sznameindex, flags, localbone, local[12], unused[8] = 92 bytes typically on v48\r\n\t\t\tvar offset = index \u002B i * 92;\r\n\t\t\tif ( offset \u002B 20 \u003E br.BaseStream.Length ) break;\r\n\t\t\tbr.BaseStream.Position = offset;\r\n\t\t\tvar nameIndex = br.ReadInt32();\r\n\t\t\tbr.ReadInt32(); // flags\r\n\t\t\tvar localBone = br.ReadInt32();\r\n\t\t\t// local matrix 3x4\r\n\t\t\tvar m00 = br.ReadSingle(); var m01 = br.ReadSingle(); var m02 = br.ReadSingle(); var m03 = br.ReadSingle();\r\n\t\t\tvar m10 = br.ReadSingle(); var m11 = br.ReadSingle(); var m12 = br.ReadSingle(); var m13 = br.ReadSingle();\r\n\t\t\tvar m20 = br.ReadSingle(); var m21 = br.ReadSingle(); var m22 = br.ReadSingle(); var m23 = br.ReadSingle();\r\n\r\n\t\t\tvar attName = BinaryUtil.ReadStringAt( br, offset, nameIndex );\r\n\t\t\tvar boneName = localBone \u003E= 0 \u0026\u0026 localBone \u003C mdl.Bones.Count ? mdl.Bones[localBone].Name : \u0022\u0022;\r\n\r\n\t\t\t// Extract translation from matrix column\r\n\t\t\tvar origin = new Vec3( m03, m13, m23 );\r\n\t\t\t// Approximate yaw/pitch/roll from rotation basis\r\n\t\t\tvar yaw = MathF.Atan2( m10, m00 ) * (180f / MathF.PI);\r\n\t\t\tvar pitch = MathF.Atan2( -m20, MathF.Sqrt( m21 * m21 \u002B m22 * m22 ) ) * (180f / MathF.PI);\r\n\t\t\tvar roll = MathF.Atan2( m21, m22 ) * (180f / MathF.PI);\r\n\r\n\t\t\tmdl.Attachments.Add( new AttachmentData\r\n\t\t\t{\r\n\t\t\t\tName = string.IsNullOrEmpty( attName ) ? $\u0022attachment{i}\u0022 : attName,\r\n\t\t\t\tParentBone = boneName,\r\n\t\t\t\tOrigin = origin,\r\n\t\t\t\tAngles = new Vec3( pitch, yaw, roll )\r\n\t\t\t} );\r\n\r\n\t\t\t_ = (m01, m02, m11, m12);\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void ReadAnimDescs( BinaryReader br, MdlModel mdl, int index, int count )\r\n\t{\r\n\t\t// mstudioanimdesc_t size varies; v48 is 100 bytes\r\n\t\tconst int stride = 100;\r\n\t\tfor ( var i = 0; i \u003C count; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar offset = index \u002B i * stride;\r\n\t\t\tif ( offset \u002B stride \u003E br.BaseStream.Length ) break;\r\n\t\t\tbr.BaseStream.Position = offset;\r\n\t\t\tbr.ReadInt32(); // baseptr\r\n\t\t\tvar nameIndex = br.ReadInt32();\r\n\t\t\tvar fps = br.ReadSingle();\r\n\t\t\tvar flags = br.ReadInt32();\r\n\t\t\tvar numframes = br.ReadInt32();\r\n\t\t\tbr.ReadInt32(); // nummovements\r\n\t\t\tbr.ReadInt32(); // movementindex\r\n\t\t\tmsSkip( br, 6 * 4 ); // ikrulezeroframe etc / deprecated\r\n\t\t\tvar animindex = br.ReadInt32();\r\n\r\n\t\t\tvar animName = BinaryUtil.ReadStringAt( br, offset, nameIndex );\r\n\t\t\tmdl.AnimDescs.Add( new MdlAnimDesc\r\n\t\t\t{\r\n\t\t\t\tName = string.IsNullOrEmpty( animName ) ? $\u0022anim{i}\u0022 : animName,\r\n\t\t\t\tFps = fps \u003E 0 ? fps : 30,\r\n\t\t\t\tFrameCount = Math.Max( 1, numframes ),\r\n\t\t\t\tFlags = flags,\r\n\t\t\t\tAnimIndex = animindex,\r\n\t\t\t\tDescOffset = offset\r\n\t\t\t} );\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void ReadSequences( BinaryReader br, MdlModel mdl, int index, int count )\r\n\t{\r\n\t\t// mstudioseqdesc_t is large (~212\u002B). We only need name, flags, anim index.\r\n\t\tconst int stride = 212;\r\n\t\tfor ( var i = 0; i \u003C count; i\u002B\u002B )\r\n\t\t{\r\n\t\t\tvar offset = index \u002B i * stride;\r\n\t\t\tif ( offset \u002B 80 \u003E br.BaseStream.Length ) break;\r\n\t\t\tbr.BaseStream.Position = offset;\r\n\t\t\tbr.ReadInt32(); // baseptr\r\n\t\t\tvar szNameIndex = br.ReadInt32();\r\n\t\t\tvar szActivity = br.ReadInt32();\r\n\t\t\tvar flags = br.ReadInt32();\r\n\t\t\tbr.ReadInt32(); // activity\r\n\t\t\tbr.ReadInt32(); // actweight\r\n\t\t\tbr.ReadInt32(); // numevents\r\n\t\t\tbr.ReadInt32(); // eventindex\r\n\t\t\tmsSkip( br, 12 * 2 ); // bbmin bbmax\r\n\t\t\tvar numblends = br.ReadInt32();\r\n\t\t\tvar animindexindex = br.ReadInt32(); // index to short array of anim indices\r\n\t\t\t_ = szActivity;\r\n\t\t\t_ = numblends;\r\n\r\n\t\t\tvar seqName = BinaryUtil.ReadStringAt( br, offset, szNameIndex );\r\n\t\t\tvar animIdx = 0;\r\n\t\t\tif ( animindexindex != 0 )\r\n\t\t\t{\r\n\t\t\t\tbr.BaseStream.Position = offset \u002B animindexindex;\r\n\t\t\t\tanimIdx = br.ReadInt16();\r\n\t\t\t}\r\n\r\n\t\t\tvar fps = 30f;\r\n\t\t\tif ( animIdx \u003E= 0 \u0026\u0026 animIdx \u003C mdl.AnimDescs.Count )\r\n\t\t\t\tfps = mdl.AnimDescs[animIdx].Fps;\r\n\r\n\t\t\tmdl.Sequences.Add( new MdlSequence\r\n\t\t\t{\r\n\t\t\t\tName = string.IsNullOrEmpty( seqName ) ? $\u0022seq{i}\u0022 : seqName,\r\n\t\t\t\tAnimIndex = animIdx,\r\n\t\t\t\tLooping = (flags \u0026 0x0001) != 0, // STUDIO_LOOPING\r\n\t\t\t\tFps = fps\r\n\t\t\t} );\r\n\t\t}\r\n\t}\r\n\r\n\tstatic void msSkip( BinaryReader br, int bytes ) =\u003E br.BaseStream.Position \u002B= bytes;\r\n}\r\n"},{"Ident":"subzerostudios.gmodassetimporter","Path":"UnitTests/LibraryTest.cs","FileName":"LibraryTest.cs","PackageType":"library","CodeKind":"UnitTest","AssetVersionId":422538,"IsPrivate":false,"Code":"using Microsoft.VisualStudio.TestTools.UnitTesting;\r\nusing GmodAssetImporter;\r\n\r\n[TestClass]\r\npublic partial class LibraryTests\r\n{\r\n\t[TestMethod]\r\n\tpublic void ConversionOptionsDefaults()\r\n\t{\r\n\t\tvar options = new ConversionOptions();\r\n\t\tAssert.IsTrue( options.PreservePaths );\r\n\t\tAssert.IsFalse( options.ExportAnimations );\r\n\t\tAssert.AreEqual( 1.0f, options.ModelScale );\r\n\t\toptions.OnLog( \u0022ok\u0022 );\r\n\t}\r\n}\r\n"}]}