Skip to content

Commit

Permalink
Fix KSP issue #69 : 0-byte icon files can hang loading
Browse files Browse the repository at this point in the history
  • Loading branch information
gotmachine committed Nov 4, 2022
1 parent 88c2bc1 commit 78729ba
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions KSPCommunityFixes/Performance/TextureLoaderOptimizations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ static bool DatabaseLoaderTexture_PNG_Load_Prefix(DatabaseLoaderTexture_PNG __in
return false;
}

static string[] noMipMapsTextureNames = new string[3]
{
Path.DirectorySeparatorChar + "Icons" + Path.DirectorySeparatorChar,
Path.DirectorySeparatorChar + "Tutorials" + Path.DirectorySeparatorChar,
Path.DirectorySeparatorChar + "SimpleIcons" + Path.DirectorySeparatorChar
};

static string flagTextureName = Path.DirectorySeparatorChar + "Flags" + Path.DirectorySeparatorChar;

static IEnumerator GetEnumerator(DatabaseLoaderTexture_PNG loader, UrlDir.UrlFile urlFile, FileInfo file)
{
if (!instance.userOptInChoiceDone)
Expand Down Expand Up @@ -337,16 +346,25 @@ static IEnumerator GetEnumerator(DatabaseLoaderTexture_PNG loader, UrlDir.UrlFil
yield break;
}

Texture2D texture2D = DownloadHandlerTexture.GetContent(imageWWW);
// fix for https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/69
if (texture2D.IsNullOrDestroyed())
{
Debug.LogWarning("Texture load error in '" + file.FullName);
loader.obj = null;
loader.successful = false;
yield break;
}

if (Path.GetFileNameWithoutExtension(file.Name).EndsWith("NRM"))
{
Texture2D content = DownloadHandlerTexture.GetContent(imageWWW);
Texture2D normal;
bool normalIsCompressed;

if (isSetPixelDataSupported)
normalIsCompressed = BitmapToCompressedNormalMapFast(content, !textureCacheEnabled, out normal);
normalIsCompressed = BitmapToCompressedNormalMapFast(texture2D, !textureCacheEnabled, out normal);
else
normalIsCompressed = BitmapToCompressedNormalMapFast_Legacy(content, !textureCacheEnabled, out normal);
normalIsCompressed = BitmapToCompressedNormalMapFast_Legacy(texture2D, !textureCacheEnabled, out normal);

if (normalIsCompressed && textureCacheEnabled)
{
Expand All @@ -369,27 +387,25 @@ static IEnumerator GetEnumerator(DatabaseLoaderTexture_PNG loader, UrlDir.UrlFil
yield break;
}

Texture2D texture2D = DownloadHandlerTexture.GetContent(imageWWW);
string[] array = new string[3] { "Icons", "Tutorials", "SimpleIcons" };
bool flag = false;
bool flag2 = false;
for (int i = 0; i < array.Length; i++)
bool noMipMaps = false;
bool compressHighQuality = false;
for (int i = 0; i < noMipMapsTextureNames.Length; i++)
{
if (path.Contains(Path.DirectorySeparatorChar + array[i] + Path.DirectorySeparatorChar))
if (path.Contains(noMipMapsTextureNames[i]))
{
flag = true;
noMipMaps = true;
}
}

if (path.Contains(Path.DirectorySeparatorChar + "Flags" + Path.DirectorySeparatorChar))
if (path.Contains(flagTextureName))
{
Texture2D texture2D2 = new Texture2D(texture2D.width, texture2D.height, texture2D.format, mipChain: true);
texture2D2.LoadImage(imageWWW.downloadHandler.data);
texture2D = texture2D2;
flag2 = true;
compressHighQuality = true;
}

if (flag)
if (noMipMaps)
{
Texture2D texture2D3 = new Texture2D(texture2D.width, texture2D.height, TextureFormat.ARGB32, mipChain: false);
texture2D3.SetPixels32(texture2D.GetPixels32());
Expand All @@ -398,7 +414,7 @@ static IEnumerator GetEnumerator(DatabaseLoaderTexture_PNG loader, UrlDir.UrlFil

if (texture2D.width % 4 == 0 && texture2D.height % 4 == 0)
{
texture2D.Compress(flag2);
texture2D.Compress(compressHighQuality);
}
else
{
Expand Down

0 comments on commit 78729ba

Please sign in to comment.