Skip to content

Commit

Permalink
Ignore old cached normal maps
Browse files Browse the repository at this point in the history
If there is a cached DDS of a normal map from a
prior version still using DXT1 or DXT5 compression,
ignore it and regenerate with BC5 compression.
  • Loading branch information
mspielberg committed Mar 5, 2024
1 parent f08c728 commit ec9c44c
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions SkinManagerMod/TextureLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void BustCache(ResourceConfigJson skin, string texturePath)
}
}

private static Task<Texture2D> TryLoadFromCache(ResourceConfigJson skin, string texturePath, bool linear)
private static Task<Texture2D> TryLoadFromCache(ResourceConfigJson skin, string texturePath, bool isNormalMap)
{
var texFile = new FileInfo(texturePath);
var cached = new FileInfo(GetCachePath(skin, texturePath));
Expand All @@ -37,17 +37,18 @@ private static Task<Texture2D> TryLoadFromCache(ResourceConfigJson skin, string

if (cached.LastWriteTimeUtc < texFile.LastWriteTimeUtc)
{
cached.Delete();
Main.LogVerbose($"Cached texture {cached.FullName} is out of date");
BustCache(skin, texturePath);
return Task.FromResult<Texture2D>(null);
}

try
{
return DDSUtils.ReadDDSGz(cached, linear);
return DDSUtils.ReadDDSGz(cached, isNormalMap);
}
catch (DDSReadException e)
{
Main.Warning($"Error loading cached skin {skin.Name}: {e.Message}");
Main.Warning($"Error loading cached texture {cached.FullName}: {e.Message}");
BustCache(skin, texturePath);
return Task.FromResult<Texture2D>(null);
}
Expand All @@ -69,6 +70,7 @@ public static Task<Texture2D> LoadAsync(ResourceConfigJson skin, string textureP
var texture = new Texture2D(info.width, info.height, format,
mipChain: true, linear: isNormalMap);
var nativeArray = texture.GetRawTextureData<byte>();
Main.LogVerbose($"Loading texture {texturePath} as {format} with StbImage");
return Task.Run(() =>
{
PopulateTexture(texturePath, format, nativeArray);
Expand All @@ -81,7 +83,9 @@ public static Task<Texture2D> LoadAsync(ResourceConfigJson skin, string textureP

public static Texture2D LoadSync(ResourceConfigJson skin, string texturePath, bool isNormalMap)
{
var texture = new Texture2D(0, 0, textureFormat: TextureFormat.RGBA32, mipChain: true, linear: isNormalMap);
var textureFormat = TextureFormat.RGBA32;
var texture = new Texture2D(0, 0, textureFormat, mipChain: true, linear: isNormalMap);
Main.LogVerbose($"Loading texture {texturePath} as {textureFormat} with LoadImage");
texture.LoadImage(File.ReadAllBytes(texturePath));

return texture;
Expand Down Expand Up @@ -293,17 +297,26 @@ private static Texture2D ReadDDSHeader(Stream infile, bool linear)
return texture;
}

public static Task<Texture2D> ReadDDSGz(FileInfo fileInfo, bool linear)
public static Task<Texture2D> ReadDDSGz(FileInfo fileInfo, bool isNormalMap)
{
FileStream fileStream = null;
GZipStream infile = null;
try
{
Main.LogVerbose($"Reading from {fileInfo.FullName}");
fileStream = fileInfo.OpenRead();
infile = new GZipStream(fileStream, CompressionMode.Decompress);

var texture = ReadDDSHeader(infile, linear);
var texture = ReadDDSHeader(infile, isNormalMap);
if (isNormalMap && texture.format != TextureFormat.BC5)
{
Main.LogVerbose($"Cached normal map texture {fileInfo.FullName} has old format {texture.format}");
infile.Close();
fileStream.Close();
File.Delete(fileInfo.FullName);
return Task.FromResult<Texture2D>(null);
}

Main.LogVerbose($"Reading cached {texture.format} texture from {fileInfo.FullName}");
var nativeArray = texture.GetRawTextureData<byte>();
return Task.Run(() =>
{
Expand Down

0 comments on commit ec9c44c

Please sign in to comment.