diff --git a/KSPCommunityFixes/Performance/TextureLoaderOptimizations.cs b/KSPCommunityFixes/Performance/TextureLoaderOptimizations.cs index 791ed1c..680db5f 100644 --- a/KSPCommunityFixes/Performance/TextureLoaderOptimizations.cs +++ b/KSPCommunityFixes/Performance/TextureLoaderOptimizations.cs @@ -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) @@ -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) { @@ -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()); @@ -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 {