|
| 1 | +using Maple2.File.IO; |
| 2 | + |
| 3 | +namespace Maple2.File.Parser.Flat; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Resolves model names to their corresponding NIF asset names. |
| 7 | +/// Sequence: modelName -> find flat with the same name -> get llid from it -> llid to uuid -> uuid to nif asset name |
| 8 | +/// </summary> |
| 9 | +public class ModelToNifResolver { |
| 10 | + private readonly FlatTypeIndex flatIndex; |
| 11 | + private readonly Convert.AssetIndex assetIndex; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Initializes a new instance of the ModelToNifResolver class. |
| 15 | + /// </summary> |
| 16 | + /// <param name="exportedReader">M2dReader for the exported .m2d containing flat files</param> |
| 17 | + /// <param name="assetMetadataReader">M2dReader for the asset-web-metadata.m2d file</param> |
| 18 | + /// <param name="flatRoot">Root path for flat files (default: "flat")</param> |
| 19 | + public ModelToNifResolver(M2dReader exportedReader, M2dReader assetMetadataReader, string flatRoot = "flat") { |
| 20 | + flatIndex = new FlatTypeIndex(exportedReader, flatRoot); |
| 21 | + assetIndex = new Convert.AssetIndex(assetMetadataReader); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Resolves a model name to its NIF asset name. |
| 26 | + /// </summary> |
| 27 | + /// <param name="modelName">The name of the model to resolve</param> |
| 28 | + /// <returns>The NIF asset name, or null if not found or not a NIF asset</returns> |
| 29 | + public string? GetNifAssetName(string modelName) { |
| 30 | + // Step 1: Find flat type with the same name |
| 31 | + FlatType? flatType = flatIndex.GetType(modelName); |
| 32 | + if (flatType == null) { |
| 33 | + Console.WriteLine($"Model not found: {modelName}"); |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + // Step 2: Get llid from the flat type |
| 38 | + // NIF assets are typically stored in properties like "NifAsset" or "AttachedNifAsset" |
| 39 | + string? llid = GetNifAssetLlid(flatType); |
| 40 | + if (llid == null) { |
| 41 | + Console.WriteLine($"No NIF asset found for model: {modelName}"); |
| 42 | + return null; |
| 43 | + } |
| 44 | + |
| 45 | + // Step 3 & 4: Convert llid to uuid and get asset name |
| 46 | + (string name, string path, string tags) = assetIndex.GetFields(llid); |
| 47 | + |
| 48 | + if (string.IsNullOrEmpty(name)) { |
| 49 | + Console.WriteLine($"Failed to resolve asset metadata for llid: {llid}"); |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + // Verify it's actually a NIF asset by checking tags or path |
| 54 | + if (!IsNifAsset(path, tags)) { |
| 55 | + Console.WriteLine($"Asset is not a NIF file: {name} (path: {path}, tags: {tags})"); |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + return name; |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Gets detailed information about the NIF asset for a model. |
| 64 | + /// </summary> |
| 65 | + /// <param name="modelName">The name of the model to resolve</param> |
| 66 | + /// <returns>A tuple containing (name, path, tags) of the NIF asset, or null values if not found</returns> |
| 67 | + public (string? Name, string? Path, string? Tags) GetNifAssetInfo(string modelName) { |
| 68 | + // Step 1: Find flat type with the same name |
| 69 | + FlatType? flatType = flatIndex.GetType(modelName); |
| 70 | + if (flatType == null) { |
| 71 | + Console.WriteLine($"Model not found: {modelName}"); |
| 72 | + return (null, null, null); |
| 73 | + } |
| 74 | + |
| 75 | + // Step 2: Get llid from the flat type |
| 76 | + string? llid = GetNifAssetLlid(flatType); |
| 77 | + if (llid == null) { |
| 78 | + Console.WriteLine($"No NIF asset found for model: {modelName}"); |
| 79 | + return (null, null, null); |
| 80 | + } |
| 81 | + |
| 82 | + // Step 3 & 4: Convert llid to uuid and get asset info |
| 83 | + (string name, string path, string tags) = assetIndex.GetFields(llid); |
| 84 | + |
| 85 | + if (string.IsNullOrEmpty(name)) { |
| 86 | + Console.WriteLine($"Failed to resolve asset metadata for llid: {llid}"); |
| 87 | + return (null, null, null); |
| 88 | + } |
| 89 | + |
| 90 | + return (name, path, tags); |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Extracts the NIF asset LLID from a flat type. |
| 95 | + /// Searches for properties like "NifAsset", "AttachedNifAsset", or similar. |
| 96 | + /// </summary> |
| 97 | + private string? GetNifAssetLlid(FlatType flatType) { |
| 98 | + // Common property names for NIF assets |
| 99 | + string[] nifPropertyNames = { "NifAsset", "AttachedNifAsset", "Mesh", "MeshAsset" }; |
| 100 | + |
| 101 | + foreach (string propertyName in nifPropertyNames) { |
| 102 | + FlatProperty? property = flatType.GetProperty(propertyName); |
| 103 | + if (property == null) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + // Check if the property type is AssetID or related |
| 108 | + if (property.Type == "AssetID" || property.Type.Contains("Asset")) { |
| 109 | + string? llid = property.Value?.ToString(); |
| 110 | + if (!string.IsNullOrEmpty(llid)) { |
| 111 | + return llid; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + // For AssocAttachedNifAsset, get the first value |
| 116 | + if (property.Type == "AssocAttachedNifAsset" && property.Value is Dictionary<string, string> dict) { |
| 117 | + string? firstValue = dict.Values.FirstOrDefault(); |
| 118 | + if (!string.IsNullOrEmpty(firstValue)) { |
| 119 | + return firstValue; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // If no NIF property found, try to get all properties with AssetID type |
| 125 | + foreach (FlatProperty property in flatType.GetAllProperties()) { |
| 126 | + if (property.Type == "AssetID") { |
| 127 | + string? llid = property.Value?.ToString(); |
| 128 | + if (!string.IsNullOrEmpty(llid)) { |
| 129 | + // Check if this is likely a NIF asset |
| 130 | + (string name, string path, string tags) = assetIndex.GetFields(llid); |
| 131 | + if (IsNifAsset(path, tags)) { |
| 132 | + return llid; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Checks if an asset is a NIF file based on its path and tags. |
| 143 | + /// </summary> |
| 144 | + private bool IsNifAsset(string path, string tags) { |
| 145 | + if (string.IsNullOrEmpty(path)) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + // Check file extension |
| 150 | + if (path.EndsWith(".nif", StringComparison.OrdinalIgnoreCase)) { |
| 151 | + return true; |
| 152 | + } |
| 153 | + |
| 154 | + // Check tags for gamebryo-scenegraph (NIF format indicator) |
| 155 | + if (!string.IsNullOrEmpty(tags) && tags.Contains("gamebryo-scenegraph")) { |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + return false; |
| 160 | + } |
| 161 | +} |
0 commit comments