ActiveTextureManagement.CacheController.FetchCacheTexture C# (CSharp) Метод

FetchCacheTexture() публичный статический Метод

public static FetchCacheTexture ( TexInfo Texture, bool compress, bool mipmaps ) : TextureInfoWrapper
Texture TexInfo
compress bool
mipmaps bool
Результат TextureInfoWrapper
        public static TextureInfoWrapper FetchCacheTexture(TexInfo Texture, bool compress, bool mipmaps)
        {
            String textureName = Texture.name;
            String originalTextureFile = KSPUtil.ApplicationRootPath + "GameData/" + textureName;
            String cacheFile = KSPUtil.ApplicationRootPath + "GameData/ActiveTextureManagement/textureCache/" + textureName;
            String cacheConfigFile = cacheFile + ".tcache";
            cacheFile += ".imgcache";

            String hashString = GetMD5String(originalTextureFile);
            if (TextureHashTable.ContainsKey(hashString))
            {
                ActiveTextureManagement.DBGLog("hash triggered... " + textureName);
                TextureInfoWrapper tex = TextureHashTable[hashString];
                if (tex.name != textureName)
                {
                    TextureInfoWrapper cacheTexInfo = new TextureInfoWrapper(Texture.file, tex.texture, tex.isNormalMap, tex.isReadable, tex.isCompressed);
                    cacheTexInfo.name = textureName;
                    ActiveTextureManagement.DBGLog("Re-using from hash dictionary... " + textureName+" is a duplicate of "+tex.name);

                    return cacheTexInfo;
                }
            }

            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, hashString );
                }

                originalTextureFile += format;
                Texture.Resize(origWidth, origHeight);

                if (format != null && File.Exists(originalTextureFile) && File.Exists(cacheFile))
                {

                    String cacheIsNormString = config.GetValue("is_normal");
                    String cacheIsCompressed = config.GetValue("is_compressed");
                    String cacheWidthString = config.GetValue("width");
                    String cacheHeihtString = config.GetValue("height");
                    string hasAlphaString = config.GetValue("hasAlpha");
                    string hasMipmapsString = config.GetValue("hasMipmaps");
                    bool cacheIsNorm = false;
                    int cacheWidth = 0;
                    int cacheHeight = 0;
                    bool hasAlpha = true;
                    bool hasMipmaps = true;
                    bool isCompressed = true;
                    bool.TryParse(cacheIsNormString, out cacheIsNorm);
                    bool.TryParse(cacheIsCompressed, out isCompressed);
                    int.TryParse(cacheWidthString, out cacheWidth);
                    int.TryParse(cacheHeihtString, out cacheHeight);
                    bool.TryParse(hasAlphaString, out hasAlpha);
                    bool.TryParse(hasMipmapsString, out hasMipmaps);

                    if (cacheHash != hashString || compress != isCompressed || mipmaps != hasMipmaps || 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, hashString);
                    }
                    else
                    {
                        ActiveTextureManagement.DBGLog("Loading from cache... " + textureName);
                        Texture.needsResize = false;
                        Texture.width = Texture.resizeWidth;
                        Texture.height = Texture.resizeHeight;
                        Texture.filename = cacheFile;
                        TextureInfoWrapper tex = TextureConverter.DDSToTexture(Texture.file, Texture, hasMipmaps, isCompressed, hasAlpha);
                        if (TextureHashTable.ContainsKey(hashString))
                        {
                            TextureHashTable[hashString] = tex;
                        }
                        else
                        {
                            TextureHashTable.Add(hashString, tex);
                        }

                        return tex;
                    }
                }
                else
                {
                    return RebuildCache(Texture, compress, mipmaps, hashString);
                }
            }
            else
            {
                return RebuildCache(Texture, compress, mipmaps, hashString);
            }
        }

Usage Example

        static public TextureInfoWrapper UpdateTexture(TexInfo texture)
        {
            string     overrideName    = overridesList.Find(n => texture.name.Length == Regex.Match(texture.name, n).Length);
            bool       mipmaps         = true;
            bool       compress        = texture.isNormalMap ? false : true;
            int        scale           = 1;
            int        maxSize         = 0;
            int        minSize         = 64;
            FilterMode filterMode      = FilterMode.Bilinear;
            bool       makeNotReadable = false;

            if (foldersList.Exists(n => texture.name.StartsWith(n)))
            {
                if (texture.isNormalMap)
                {
                    mipmaps  = DatabaseLoaderTexture_ATM.config_mipmaps_normals;
                    compress = DatabaseLoaderTexture_ATM.config_compress_normals;
                    scale    = DatabaseLoaderTexture_ATM.config_scale_normals;
                    maxSize  = DatabaseLoaderTexture_ATM.config_max_size_normals;
                    minSize  = DatabaseLoaderTexture_ATM.config_min_size_normals;
                }
                else
                {
                    mipmaps  = DatabaseLoaderTexture_ATM.config_mipmaps;
                    compress = DatabaseLoaderTexture_ATM.config_compress;
                    scale    = DatabaseLoaderTexture_ATM.config_scale;
                    maxSize  = DatabaseLoaderTexture_ATM.config_max_size;
                    minSize  = DatabaseLoaderTexture_ATM.config_min_size;
                }
                filterMode      = config_filter_mode;
                makeNotReadable = config_make_not_readable;

                if (overrideName != null)
                {
                    ConfigNode overrideNode            = overrides.GetNode(overrideName);
                    String     normalString            = texture.isNormalMap ? "_normals" : "";
                    String     mipmapsString           = overrideNode.GetValue("mipmaps" + normalString);
                    String     compressString          = overrideNode.GetValue("compress" + normalString);
                    String     scaleString             = overrideNode.GetValue("scale" + normalString);
                    String     max_sizeString          = overrideNode.GetValue("max_size" + normalString);
                    String     min_sizeString          = overrideNode.GetValue("min_size" + normalString);
                    String     filter_modeString       = overrideNode.GetValue("filter_mode");
                    String     make_not_readableString = overrideNode.GetValue("make_not_readable");

                    if (mipmapsString != null)
                    {
                        bool.TryParse(mipmapsString, out mipmaps);
                    }
                    if (compressString != null)
                    {
                        bool.TryParse(compressString, out compress);
                    }
                    if (scaleString != null)
                    {
                        int.TryParse(scaleString, out scale);
                    }
                    if (filter_modeString != null)
                    {
                        try
                        {
                            filterMode = (FilterMode)Enum.Parse(typeof(FilterMode), filter_modeString);
                        }
                        catch
                        {
                            filterMode = config_filter_mode;
                        }
                    }
                    if (make_not_readableString != null)
                    {
                        bool.TryParse(make_not_readableString, out makeNotReadable);
                    }
                    if (max_sizeString != null)
                    {
                        int.TryParse(max_sizeString, out maxSize);
                    }
                    if (min_sizeString != null)
                    {
                        int.TryParse(min_sizeString, out minSize);
                    }
                }
            }
            texture.SetScalingParams(scale, maxSize, minSize);
            texture.makeNotReadable = makeNotReadable && !readableList.Contains(texture.name);
            TextureInfoWrapper ret = CacheController.FetchCacheTexture(texture, compress, mipmaps);

            ret.texture.filterMode = filterMode;
            return(ret);
        }
All Usage Examples Of ActiveTextureManagement.CacheController::FetchCacheTexture