Skip to content

Commit

Permalink
Add texture material meta
Browse files Browse the repository at this point in the history
  • Loading branch information
Luzifix committed Aug 2, 2024
1 parent e6e5229 commit ce1395c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
87 changes: 80 additions & 7 deletions ADTMeta/Steps/TexMeta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ public static class TexMeta

private const string META_FILE_TEXTURE_INFO_BY_TEXTURE_FILE_ID = "TextureInfoByTextureFileID.json";
private const string META_FILE_TEXTURE_INFO_BY_TEXTURE_FILE_PATH = "TextureInfoByTextureFilePath.json";
private const string META_FILE_TEXTURE_MATERIAL_BY_TEXTURE_FILE_ID = "TextureMaterialByTextureFileID.json";
private const string META_FILE_TEXTURE_MATERIAL_BY_TEXTURE_FILE_PATH = "TextureMaterialByTextureFilePath.json";
private const string META_FILE_GROUND_EFFECT_BY_TEXTURE_FILE_ID = "GroundEffectByTextureFileID.json";
private const string META_FILE_GROUND_EFFECT_BY_TEXTURE_FILE_PATH = "GroundEffectByTextureFilePath.json";

private static ConcurrentDictionary<int, TextureInfo> _textureInfoMap = new ConcurrentDictionary<int, TextureInfo>();
private static ConcurrentDictionary<int, List<uint>> _textureGroundEffectMap = new ConcurrentDictionary<int, List<uint>>();
private static ConcurrentDictionary<int, byte> _textureMaterialMap = new ConcurrentDictionary<int, byte>();

public static void Generate()
{
Expand All @@ -43,6 +46,7 @@ public static void Generate()

TerrainTexture terrainTexture = new TerrainTexture(memoryStream.ToArray());
CollectTextureParameters(terrainTexture);
CollectTextureMaterial(terrainTexture);
CollectGroundEffects(terrainTexture);
}
}
Expand Down Expand Up @@ -74,7 +78,6 @@ private static void CollectTextureParameters(TerrainTexture terrainTexture)

if (_textureInfoMap.TryGetValue((int)terrainTexture.TextureHeightIds.Textures[i], out var existingInfo))
{

if (existingInfo.Scale != mtxp.TextureScale || existingInfo.HeightScale != mtxp.HeightScale || existingInfo.HeightOffset != mtxp.HeightOffset)
{
// Check if the old values were defaults, if so don't bother
Expand All @@ -99,6 +102,46 @@ private static void CollectTextureParameters(TerrainTexture terrainTexture)
}
}

private static void CollectTextureMaterial(TerrainTexture terrainTexture)
{
if (terrainTexture.Chunks == null)
return;

foreach (var chunk in terrainTexture.Chunks)
{
if (chunk.TerrainMaterials == null || chunk.TerrainMaterials.TerrainMaterialIds == null || terrainTexture.TextureHeightIds == null || terrainTexture.TextureHeightIds.Textures == null)
continue;

for (byte i = 0; i < chunk.TerrainMaterials.TerrainMaterialIds.Length; i++)
{
if (chunk.TerrainMaterials.TerrainMaterialIds[i] == 0)
continue;

if (terrainTexture.TextureDiffuseIds.Textures[i] == 0)
continue;

if (_textureMaterialMap.TryGetValue((int)terrainTexture.TextureDiffuseIds.Textures[i], out var existingInfo))
{
if (existingInfo != chunk.TerrainMaterials.TerrainMaterialIds[i])
{
// Check if the old values were defaults, if so don't bother
if (existingInfo == 0)
continue;

if (AppSettings.Instance.Verbose)
{
Console.WriteLine("[DEBUG] Texture " + terrainTexture.TextureDiffuseIds.Textures[i] + " has conflicting material");
Console.WriteLine("\t Existing: " + existingInfo);
Console.WriteLine("\t New: " + chunk.TerrainMaterials.TerrainMaterialIds[i]);
}
}
}

_textureMaterialMap[(int)terrainTexture.TextureDiffuseIds.Textures[i]] = chunk.TerrainMaterials.TerrainMaterialIds[i];
}
}
}

private static void CollectGroundEffects(TerrainTexture terrainTexture)
{
if (terrainTexture.TextureDiffuseIds == null || terrainTexture.Chunks == null)
Expand Down Expand Up @@ -220,25 +263,36 @@ private static void RemoveGroundEffectDuplicates()

_textureGroundEffectMap = new(_textureGroundEffectMap.Where(x => x.Value.Count > 0).ToDictionary());
}

private static void Load()
{
try
{
string textureMetaFile = Path.Combine(AppSettings.Instance.MetaFolder, META_FOLDER_TEXTURE, META_FILE_TEXTURE_INFO_BY_TEXTURE_FILE_ID);
if (File.Exists(textureMetaFile))
_textureInfoMap = new ConcurrentDictionary<int, TextureInfo>(JsonConvert.DeserializeObject<Dictionary<int, TextureInfo>>(File.ReadAllText(textureMetaFile)));
string textureInfoMetaFile = Path.Combine(AppSettings.Instance.MetaFolder, META_FOLDER_TEXTURE, META_FILE_TEXTURE_INFO_BY_TEXTURE_FILE_ID);
if (File.Exists(textureInfoMetaFile))
_textureInfoMap = JsonConvert.DeserializeObject<ConcurrentDictionary<int, TextureInfo>>(File.ReadAllText(textureInfoMetaFile));
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to load texture info meta: {ex.Message}");
}

try
{
string textureMaterialMetaFile = Path.Combine(AppSettings.Instance.MetaFolder, META_FOLDER_TEXTURE, META_FILE_TEXTURE_MATERIAL_BY_TEXTURE_FILE_ID);
if (File.Exists(textureMaterialMetaFile))
_textureMaterialMap = JsonConvert.DeserializeObject<ConcurrentDictionary<int, byte>>(File.ReadAllText(textureMaterialMetaFile));
}
catch (Exception ex)
{
Console.WriteLine($"[ERROR] Failed to load texture meta: {ex.Message}");
Console.WriteLine($"[ERROR] Failed to load texture material meta: {ex.Message}");
}

try
{
string groundEffectMetaFile = Path.Combine(AppSettings.Instance.MetaFolder, META_FOLDER_GROUND_EFFECT, META_FILE_GROUND_EFFECT_BY_TEXTURE_FILE_ID);
if (File.Exists(groundEffectMetaFile))
_textureGroundEffectMap = new ConcurrentDictionary<int, List<uint>>(JsonConvert.DeserializeObject<Dictionary<int, List<uint>>>(File.ReadAllText(groundEffectMetaFile)));
_textureGroundEffectMap = JsonConvert.DeserializeObject<ConcurrentDictionary<int, List<uint>>>(File.ReadAllText(groundEffectMetaFile));
}
catch (Exception ex)
{
Expand All @@ -254,6 +308,8 @@ private static void Save()

SaveTextureInfoByTextureFileID(Path.Combine(textureMetaPath, META_FILE_TEXTURE_INFO_BY_TEXTURE_FILE_ID));
SaveTextureInfoByTextureFilePath(Path.Combine(textureMetaPath, META_FILE_TEXTURE_INFO_BY_TEXTURE_FILE_PATH));
SaveTextureMaterialByTextureFileID(Path.Combine(textureMetaPath, META_FILE_TEXTURE_MATERIAL_BY_TEXTURE_FILE_ID));
SaveTextureMaterialByTextureFilePath(Path.Combine(textureMetaPath, META_FILE_TEXTURE_MATERIAL_BY_TEXTURE_FILE_PATH));

var groundEffectMetaPath = Path.Combine(AppSettings.Instance.MetaFolder, META_FOLDER_GROUND_EFFECT);
if (!Directory.Exists(groundEffectMetaPath))
Expand All @@ -280,6 +336,23 @@ public static void SaveTextureInfoByTextureFilePath(string path)
File.WriteAllText(path, JsonConvert.SerializeObject(textureInfoByFilePath.OrderBy(x => x.Key).ToDictionary(), Formatting.Indented));
}

private static void SaveTextureMaterialByTextureFileID(string path)
{
File.WriteAllText(path, JsonConvert.SerializeObject(_textureMaterialMap.OrderBy(x => x.Key).ToDictionary(x => x.Key.ToString(), x => x.Value), Formatting.Indented));
}

public static void SaveTextureMaterialByTextureFilePath(string path)
{
var textureMaterialByFilePath = new Dictionary<string, byte>();
foreach (var entry in _textureMaterialMap)
{
if (ListFile.NameMap.TryGetValue(entry.Key, out var filename))
textureMaterialByFilePath[filename.Replace("_h.blp", ".blp").Replace("_s.blp", ".blp")] = entry.Value;
}

File.WriteAllText(path, JsonConvert.SerializeObject(textureMaterialByFilePath.OrderBy(x => x.Key).ToDictionary(), Formatting.Indented));
}

public static void SaveGroundEffecByTextureFileID(string path)
{
File.WriteAllText(path, JsonConvert.SerializeObject(_textureGroundEffectMap.OrderBy(x => x.Key).ToDictionary(x => x.Key.ToString(), x => x.Value), Formatting.Indented));
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ Automatically dumps Texture and GroundEffect metadata from World of Warcraft ADT
#### Texture
- [Texture info by texture file id](Meta/wow/Texture/TextureInfoByTextureFileID.json)
- [Texture info by texture file path](Meta/wow/Texture/TextureInfoByTextureFilePath.json)
- [Texture material by texture file id](Meta/wow/Texture/TextureMaterialByTextureFileID.json)
- [Texture material by texture file path](Meta/wow/Texture/TextureMaterialByTextureFilePath.json)

#### GroundEffects
- [GroundEffects by texture file id](Meta/wow/GroundEffect/GroundEffectByTextureFileID.json)
- [GroundEffects by texture file path](Meta/wow/GroundEffect/GroundEffectByTextureFilePath.json)

## Usage
Arguments: `ADTMeta.exe <product> (metaFolder)`

0 comments on commit ce1395c

Please sign in to comment.