public static GameDatabase.TextureInfo FetchCacheTexture(TexInfo Texture, bool compress, bool mipmaps, bool makeNotReadable)
{
String textureName = Texture.name;
String originalTextureFile = KSPUtil.ApplicationRootPath + "GameData/" + textureName;
String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName;
String cacheConfigFile = cacheFile + ".tcache";
cacheFile += ".pngcache";
if (File.Exists(cacheConfigFile))
{
ConfigNode config = ConfigNode.Load(cacheConfigFile);
string format = config.GetValue("orig_format");
String cacheHash = config.GetValue("md5");
int origWidth, origHeight;
string origWidthString = config.GetValue("orig_width");
string origHeightString = config.GetValue("orig_height");
int.TryParse(origWidthString, out origWidth);
int.TryParse(origHeightString, out origHeight);
if (origWidthString == null || origHeightString == null ||
cacheHash == null || format == null)
{
return(RebuildCache(Texture, compress, mipmaps, makeNotReadable));
}
originalTextureFile += format;
String hashString = GetMD5String(originalTextureFile);
Texture.Resize(origWidth, origHeight);
if (format != null && File.Exists(originalTextureFile) && File.Exists(cacheFile))
{
String cacheIsNormString = config.GetValue("is_normal");
String cacheWidthString = config.GetValue("width");
String cacheHeihtString = config.GetValue("height");
bool cacheIsNorm = false;
int cacheWidth = 0;
int cacheHeight = 0;
bool.TryParse(cacheIsNormString, out cacheIsNorm);
int.TryParse(cacheWidthString, out cacheWidth);
int.TryParse(cacheHeihtString, out cacheHeight);
if (cacheHash != hashString || cacheIsNorm != Texture.isNormalMap || Texture.resizeWidth != cacheWidth || Texture.resizeHeight != cacheHeight)
{
if (cacheHash != hashString)
{
ActiveTextureManagement.DBGLog(cacheHash + " != " + hashString);
}
if (cacheIsNorm != Texture.isNormalMap)
{
ActiveTextureManagement.DBGLog(cacheIsNorm + " != " + Texture.isNormalMap);
}
if (Texture.resizeWidth != cacheWidth)
{
ActiveTextureManagement.DBGLog(Texture.resizeWidth + " != " + cacheWidth);
}
if (Texture.resizeHeight != cacheHeight)
{
ActiveTextureManagement.DBGLog(Texture.resizeHeight + " != " + cacheHeight);
}
return(RebuildCache(Texture, compress, mipmaps, makeNotReadable));
}
else if (cacheHash == hashString && !Texture.needsResize)
{
return(RebuildCache(Texture, compress, mipmaps, makeNotReadable));
}
else
{
ActiveTextureManagement.DBGLog("Loading from cache... " + textureName);
Texture.needsResize = false;
Texture2D newTex = new Texture2D(4, 4);
GameDatabase.TextureInfo cacheTexture = new GameDatabase.TextureInfo(newTex, Texture.isNormalMap, !makeNotReadable, compress);
Texture.texture = cacheTexture;
Texture.filename = cacheFile;
TextureConverter.IMGToTexture(Texture, mipmaps, cacheIsNorm);
cacheTexture.name = textureName;
newTex.name = textureName;
if (compress)
{
newTex.Compress(true);
}
newTex.Apply(mipmaps, makeNotReadable);
return(cacheTexture);
}
}
else
{
return(RebuildCache(Texture, compress, mipmaps, makeNotReadable));
}
}
else
{
return(RebuildCache(Texture, compress, mipmaps, makeNotReadable));
}
}