<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
#if !WINRT && !WINDOWS_PHONE8
using System.Threading;
#else
using Windows.System.Threading;
#endif
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Cocos2D
{
    public class CCTextureCache : IDisposable, ICCSelectorProtocol
    {
        struct AsyncStruct
        {
            public string  FileName;
            public Action<CCTexture2D> Action;
        };
        private List<AsyncStruct> _asyncLoadedImages = new List<AsyncStruct>();
        private Action _ProcessingAction;
#if !WINRT && !WINDOWS_PHONE8
        private Thread _thread;
#endif

        private static CCTextureCache s_sharedTextureCache;

        private readonly object m_pDictLock = new object();
        protected Dictionary<string, CCTexture2D> m_pTextures = new Dictionary<string, CCTexture2D>();

        ~CCTextureCache() 
        {
            Dispose();
        }

        private CCTextureCache()
        {
            _ProcessingAction = new Action(
            () =>
            {

                while (true)
                {
                    lock (_asyncLoadedImages)
                    {
                        if (_asyncLoadedImages.Count == 0)
                        {
                            return;
                        }
                    }

                    try
                    {
                        var image = _asyncLoadedImages[0];
                        var texture = AddImage(image.FileName);

                        if (image.Action != null)
                        {
                            CCDirector.SharedDirector.Scheduler.ScheduleSelector(
                               f => image.Action(texture), this, 0, 0, 0, false
                               );
                        }
                    }
                    catch (Exception ex)
                    {
                        CCLog.Log("Failed to load image");
                        CCLog.Log(ex.ToString());
                    }

                    lock (_asyncLoadedImages)
                    {
                        _asyncLoadedImages.RemoveAt(0);
                    }
                }
            }
                    );
        }

        public void Update(float dt)
        {
        }

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

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

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

        public bool Contains(string assetFile)
        {
            return m_pTextures.ContainsKey(assetFile);
        }
#if WINRT || WINDOWS_PHONE8
        /// <summary>
        /// The asynchronous handler for the task.
        /// </summary>
        /// <param name="action"></param>
        private void AddImageAsyncHandler(Windows.Foundation.IAsyncAction action)
        {
            _ProcessingAction();
        }
#endif
        public void AddImageAsync(string fileimage, Action<CCTexture2D> action)
        {
            Debug.Assert(!String.IsNullOrEmpty(fileimage), "TextureCache: fileimage MUST not be NULL");

            lock (_asyncLoadedImages)
            {
                _asyncLoadedImages.Add(new AsyncStruct() { FileName = fileimage, Action = action });
            }


#if !WINRT && !WINDOWS_PHONE8
            if (_thread == null)
            {
                _thread = new Thread(() => {
                    System.Threading.Thread.CurrentThread.Name = "TextureCacheAsync";
                    _ProcessingAction();
                    _thread = null;
                });
                _thread.Start();
            }
#else
            ThreadPool.RunAsync(new WorkItemHandler(AddImageAsyncHandler));
#endif
        }

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

            lock (m_pDictLock)
            {
                CCTexture2D texture;

                var assetName = fileimage;
                if (Path.HasExtension(assetName))
                {
                    assetName = CCFileUtils.RemoveExtension(assetName);
                }

                if (!m_pTextures.TryGetValue(assetName, out texture))
                {
                    texture = new CCTexture2D();
<commit_msg>use Task for AddImageAsync<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Graphics;

namespace Cocos2D
{
    public class CCTextureCache : IDisposable, ICCSelectorProtocol
    {
        struct AsyncStruct
        {
            public string  FileName;
            public Action<CCTexture2D> Action;
        };

        private List<AsyncStruct> _asyncLoadedImages = new List<AsyncStruct>();
        private Action _processingAction;
        private Task _task;

        private static CCTextureCache s_sharedTextureCache;

        private readonly object m_pDictLock = new object();
        protected Dictionary<string, CCTexture2D> m_pTextures = new Dictionary<string, CCTexture2D>();

        ~CCTextureCache() 
        {
            Dispose();
        }

        private CCTextureCache()
        {
            _processingAction = new Action(
                () =>
                {
                    while (true)
                    {
                        lock (_asyncLoadedImages)
                        {
                            if (_asyncLoadedImages.Count == 0)
                            {
                                _task = null;
                                return;
                            }
                        }

                        try
                        {
                            var image = _asyncLoadedImages[0];
                            var texture = AddImage(image.FileName);

                            if (image.Action != null)
                            {
                                CCDirector.SharedDirector.Scheduler.ScheduleSelector(
                                    f => image.Action(texture), this, 0, 0, 0, false
                                    );
                            }
                        }
                        catch (Exception ex)
                        {
                            CCLog.Log("Failed to load image");
                            CCLog.Log(ex.ToString());
                        }

                        lock (_asyncLoadedImages)
                        {
                            _asyncLoadedImages.RemoveAt(0);
                        }
                    }
                }
                );
        }

        public void Update(float dt)
        {
        }

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

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

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

        public bool Contains(string assetFile)
        {
            return m_pTextures.ContainsKey(assetFile);
        }

        public void AddImageAsync(string fileimage, Action<CCTexture2D> action)
        {
            Debug.Assert(!String.IsNullOrEmpty(fileimage), "TextureCache: fileimage MUST not be NULL");

            lock (_asyncLoadedImages)
            {
                _asyncLoadedImages.Add(new AsyncStruct() {FileName = fileimage, Action = action});
            }


            if (_task == null)
            {
                _task = new Task(() => {
#if !WINRT && !XBOX
                    System.Threading.Thread.CurrentThread.Name = "TextureCacheAsync";
#endif
                    _processingAction();
                });
                _task.Start();
            }
        }

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

            lock (m_pDictLock)
            {
                CCTexture2D texture;

                var assetName = fileimage;
                if (Path.HasExtension(assetName))
                {
                    assetName = CCFileUtils.RemoveExtension(assetName);
                }

                if (!m_pTextures.TryGetValue(assetName, out texture))
                {
                    texture = new CCTexture2D();

{'fraction_non_alphanumeric': 0.07622310245155765, 'fraction_numerical': 0.003425757413553153, 'mean_word_length': 1.1585027726432533, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '108087', 'n_tokens_mistral': 2308, 'n_tokens_neox': 2124, 'n_words': 446}