<commit_before>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework.Graphics;

namespace cocos2d
{
    //TODO: AsyncLoad
    public class CCTextureCache : IDisposable
    {
        private readonly object m_pDictLock;

        protected Dictionary<string, CCTexture2D> m_pTextures = new Dictionary<string,CCTexture2D>();
        private static Dictionary<CCTexture2D, string> m_pTextureRefNames = new Dictionary<CCTexture2D, string>();

        #region Singleton

        private static CCTextureCache s_sharedTextureCache;

        ~CCTextureCache() 
        {
            Dispose();
        }

        private CCTextureCache()
        {
            Debug.Assert(s_sharedTextureCache == null, "Attempted to allocate a second instance of a singleton.");

            m_pTextures = new Dictionary<string, CCTexture2D>();

            m_pDictLock = new object();
        }

        public static CCTextureCache SharedTextureCache
        {
            get 
            {
                if (s_sharedTextureCache == null)
                {
                    s_sharedTextureCache = new CCTextureCache();
                }
                else
                {
                    s_sharedTextureCache.Restore();
                }
                return (s_sharedTextureCache);
            }
        }

        #endregion

        public static void PurgeSharedTextureCache()
        {
//            s_sharedTextureCache = null;
            if (s_sharedTextureCache != null)
            {
                s_sharedTextureCache.Dispose();
            }
        }

        public void UnloadContent()
        {
            m_pTextures.Clear();
            m_pTextureRefNames.Clear();
        }

        /// <summary>
        /// Restores all of the textures in the cache - used when the game is resurrected on Android.
        /// </summary>
        public void Restore()
        {
            if (m_pTextureRefNames.Count > 0)
            {
//                CCLog.Log("Restoring the texture cache!");
                List<string> l = new List<string>(m_pTextureRefNames.Values);
                m_pTextureRefNames.Clear();
                foreach (string name in l)
                {
//                    CCLog.Log("Restoring: " + name);
                    AddImage(name);
                }
            }
        }

        public void ReloadMyTexture(CCTexture2D t)
        {
            if (m_pTextureRefNames.ContainsKey(t))
            {
                string file = m_pTextureRefNames[t];
                AddImage(file);
            }
        }

        public CCTexture2D AddImage(string fileimage)
        {
            Debug.Assert(!String.IsNullOrEmpty(fileimage), "TextureCache: fileimage MUST not be NULL");

            CCTexture2D texture;

            lock (m_pDictLock)
            {
                //remove possible -HD suffix to prevent caching the same image twice (issue #1040)
                string pathKey = fileimage;
                //CCFileUtils.ccRemoveHDSuffixFromFile(pathKey);

                bool bHasTexture = m_pTextures.TryGetValue(pathKey, out texture);
                if(!bHasTexture || texture == null) 
                {
                    // Create a new one only if the current one does not exist
                    texture = new CCTexture2D();
                }
                if(!texture.IsTextureDefined)
                {
                    // Either we are creating a new one or else we need to refresh the current one.
                    // CCLog.Log("Loading texture {0}", fileimage);
                    Texture2D textureXna = null;
                    try
                    {
                        textureXna = CCApplication.SharedApplication.Content.Load<Texture2D>(fileimage);
                    }
                    catch (Exception)
                    {
                        string srcfile = fileimage;
                        if (srcfile.IndexOf('.') > -1)
                        {
                            // Remove the extension
                            srcfile = srcfile.Substring(0, srcfile.LastIndexOf('.'));
                        }
                        textureXna = CCApplication.SharedApplication.Content.Load<Texture2D>(srcfile);
                    }
                    bool isInited = texture.InitWithTexture(textureXna);

                    if (isInited)
                    {
                        texture.IsManaged = true;
                        texture.ContentFile = fileimage;
                        m_pTextures[pathKey] = texture;<commit_msg>added multiple testing for the source file<commit_after>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Xna.Framework.Graphics;

namespace cocos2d
{
    //TODO: AsyncLoad
    public class CCTextureCache : IDisposable
    {
        private readonly object m_pDictLock;

        protected Dictionary<string, CCTexture2D> m_pTextures = new Dictionary<string,CCTexture2D>();
        private static Dictionary<CCTexture2D, string> m_pTextureRefNames = new Dictionary<CCTexture2D, string>();

        #region Singleton

        private static CCTextureCache s_sharedTextureCache;

        ~CCTextureCache() 
        {
            Dispose();
        }

        private CCTextureCache()
        {
            Debug.Assert(s_sharedTextureCache == null, "Attempted to allocate a second instance of a singleton.");

            m_pTextures = new Dictionary<string, CCTexture2D>();

            m_pDictLock = new object();
        }

        public static CCTextureCache SharedTextureCache
        {
            get 
            {
                if (s_sharedTextureCache == null)
                {
                    s_sharedTextureCache = new CCTextureCache();
                }
                else
                {
                    s_sharedTextureCache.Restore();
                }
                return (s_sharedTextureCache);
            }
        }

        #endregion

        public static void PurgeSharedTextureCache()
        {
//            s_sharedTextureCache = null;
            if (s_sharedTextureCache != null)
            {
                s_sharedTextureCache.Dispose();
            }
        }

        public void UnloadContent()
        {
            m_pTextures.Clear();
            m_pTextureRefNames.Clear();
        }

        /// <summary>
        /// Restores all of the textures in the cache - used when the game is resurrected on Android.
        /// </summary>
        public void Restore()
        {
            if (m_pTextureRefNames.Count > 0)
            {
//                CCLog.Log("Restoring the texture cache!");
                List<string> l = new List<string>(m_pTextureRefNames.Values);
                m_pTextureRefNames.Clear();
                foreach (string name in l)
                {
//                    CCLog.Log("Restoring: " + name);
                    AddImage(name);
                }
            }
        }

        public void ReloadMyTexture(CCTexture2D t)
        {
            if (m_pTextureRefNames.ContainsKey(t))
            {
                string file = m_pTextureRefNames[t];
                AddImage(file);
            }
        }

        public CCTexture2D AddImage(string fileimage)
        {
            Debug.Assert(!String.IsNullOrEmpty(fileimage), "TextureCache: fileimage MUST not be NULL");

            CCTexture2D texture;

            lock (m_pDictLock)
            {
                //remove possible -HD suffix to prevent caching the same image twice (issue #1040)
                string pathKey = fileimage;
                //CCFileUtils.ccRemoveHDSuffixFromFile(pathKey);

                bool bHasTexture = m_pTextures.TryGetValue(pathKey, out texture);
                if(!bHasTexture || texture == null) 
                {
                    // Create a new one only if the current one does not exist
                    texture = new CCTexture2D();
                }
                if(!texture.IsTextureDefined)
                {
                    // Either we are creating a new one or else we need to refresh the current one.
                    // CCLog.Log("Loading texture {0}", fileimage);
                    Texture2D textureXna = null;
                    try
                    {
                        textureXna = CCApplication.SharedApplication.Content.Load<Texture2D>(fileimage);
                    }
                    catch (Exception)
                    {
                    }
                    if(textureXna == null) 
                    {
                        string srcfile = fileimage;
                        if (srcfile.IndexOf('.') > -1)
                        {
                            // Remove the extension
                            srcfile = srcfile.Substring(0, srcfile.LastIndexOf('.'));
                        }
                        try {
                            textureXna = CCApplication.SharedApplication.Content.Load<Texture2D>(srcfile);
                        }
                        catch(Exception) {
                            if(!srcfile.EndsWith ("-hd")) {
                                srcfile = srcfile + "-hd";
                            try 
                            {
                                textureXna = CCApplication.SharedApplication.Content.Load<Texture2D>(srcfile);
                            }
                            catch(Exception) {
                            }
                            }
                        }
                    }
                    if(textureXna == null) {
                        throw(new ArgumentException("Texture " + fileimage + " was not found."));
                    }
                    bool isInited = texture.InitWithTexture(textureXna);

                    if (isInited)
                    {
                        texture.IsManaged = true;
                        texture.ContentFile = fileimage;
                        m_pTextures[pathKey] = texture;
{'fraction_non_alphanumeric': 0.07745010425975574, 'fraction_numerical': 0.004269685234832688, 'mean_word_length': 1.2107111501316945, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5088879', 'n_tokens_mistral': 2543, 'n_tokens_neox': 2337, 'n_words': 536}