<commit_before>﻿using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Spectrum.Framework.Graphics;
using Spectrum.Framework.Graphics.Animation;
using Spectrum.Framework.Screens;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Spectrum.Framework.Content
{
    public class ContentLoadException : Exception
    {

    }

    public class ContentHelper
    {
        private static ContentHelper single;
        public static ContentHelper Single { get { if (single == null) { single = new ContentHelper(SpectrumGame.Game.Content); } return single; } }
        public static Texture2D Blank { get { return ContentHelper.Load<Texture2D>("blank"); } }
        public static Dictionary<Type, ICachedContentParser> ContentParsers = new Dictionary<Type, ICachedContentParser>()
            {
                {typeof(SpecModel), new ModelParser()},
                {typeof(AnimationPlayer), new AnimationParser()},
                {typeof(Texture2D), new Texture2DParser()},
                {typeof(ScalableTexture), new ScalableTextureParser()},
                {typeof(float[,]), new HeightmapParser()}
            };
        public ContentManager Content { get; private set; }
        public ContentHelper(ContentManager content)
        {
            Content = content;
        }

        public static T Load<T>(string plugin, string path) where T : class
        {
            Plugin p = SpectrumGame.Game.Plugins[plugin];
            if (p == null) { return null; }
            return p.Content._load<T>(path, true);
        }

        public static T Load<T>(string path, bool usePrefix = true) where T : class
        {
            return (T)Single._load<T>(path, usePrefix);
        }

        public static object LoadType(Type type, string path, string plugin)
        {
            MethodInfo load;
            if (plugin == null)
                load = typeof(ContentHelper).GetMethod("Load", new Type[] { typeof(string) });
            else
                load = typeof(ContentHelper).GetMethod("Load", new Type[] { typeof(string), typeof(string) });
            load = load.MakeGenericMethod(type);
            if (plugin == null)
                return load.Invoke(null, new object[] { path });
            else
                return load.Invoke(null, new object[] { plugin, path });
        }

        private T _load<T>(string path, bool usePrefix) where T : class
        {
            try
            {
                if (ContentParsers.ContainsKey(typeof(T)))
                {
                    ICachedContentParser parser = ContentParsers[typeof(T)];
                    if (usePrefix)
                        path = Content.RootDirectory + "\\" + parser.Prefix + path;
                    path += parser.Suffix;
                    return (T)ContentParsers[typeof(T)].Load(path);
                }
                if (typeof(T) == typeof(SpriteFont)) { path = @"Fonts\" + path; }
                if (typeof(T) == typeof(Effect))
                {
                    path = @"HLSL\" + path + ".mgfx";
                }
                return Content.Load<T>(path);
            }
            catch (FileNotFoundException)
            {
                throw new ContentLoadException();
            }
        }

        public Texture2D ObjectTexture(object obj)
        {
            if (obj == null) { return Blank; }
            PropertyInfo[] pinfos = obj.GetType().GetProperties();
            foreach (PropertyInfo pinfo in pinfos)
            {
                if (pinfo.Name == "GUITex")
                {
                    Texture2D output = pinfo.GetValue(obj, null) as Texture2D;
                    if (output != null) { return output; }
                }
            }
            return Blank;
        }
    }
}
<commit_msg>Fix a bug where loading content could fail<commit_after>﻿using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Spectrum.Framework.Graphics;
using Spectrum.Framework.Graphics.Animation;
using Spectrum.Framework.Screens;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Spectrum.Framework.Content
{
    public class ContentLoadException : Exception
    {

    }

    public class ContentHelper
    {
        private static ContentHelper single;
        public static ContentHelper Single { get { if (single == null) { single = new ContentHelper(SpectrumGame.Game.Content); } return single; } }
        public static Texture2D Blank { get { return ContentHelper.Load<Texture2D>("blank"); } }
        public static Dictionary<Type, ICachedContentParser> ContentParsers = new Dictionary<Type, ICachedContentParser>()
            {
                {typeof(SpecModel), new ModelParser()},
                {typeof(AnimationPlayer), new AnimationParser()},
                {typeof(Texture2D), new Texture2DParser()},
                {typeof(ScalableTexture), new ScalableTextureParser()},
                {typeof(float[,]), new HeightmapParser()}
            };
        public ContentManager Content { get; private set; }
        public ContentHelper(ContentManager content)
        {
            Content = content;
        }

        public static T Load<T>(string plugin, string path) where T : class
        {
            Plugin p = SpectrumGame.Game.Plugins[plugin];
            if (p == null) { return null; }
            return p.Content._load<T>(path, true);
        }

        public static T Load<T>(string path, bool usePrefix) where T : class
        {
            return (T)Single._load<T>(path, usePrefix);
        }

        public static T Load<T>(string path) where T : class
        {
            return Load<T>(path, true);
        }

        public static object LoadType(Type type, string path, string plugin)
        {
            MethodInfo load = null;
            if (plugin == null)
                load = typeof(ContentHelper).GetMethod("Load", new Type[] { typeof(string) });
            else
                load = typeof(ContentHelper).GetMethod("Load", new Type[] { typeof(string), typeof(string) });
            load = load.MakeGenericMethod(type);
            if (plugin == null)
                return load.Invoke(null, new object[] { path });
            else
                return load.Invoke(null, new object[] { plugin, path });
        }

        private T _load<T>(string path, bool usePrefix) where T : class
        {
            try
            {
                if (ContentParsers.ContainsKey(typeof(T)))
                {
                    ICachedContentParser parser = ContentParsers[typeof(T)];
                    if (usePrefix)
                        path = Content.RootDirectory + "\\" + parser.Prefix + path;
                    path += parser.Suffix;
                    return (T)ContentParsers[typeof(T)].Load(path);
                }
                if (typeof(T) == typeof(SpriteFont)) { path = @"Fonts\" + path; }
                if (typeof(T) == typeof(Effect))
                {
                    path = @"HLSL\" + path + ".mgfx";
                }
                return Content.Load<T>(path);
            }
            catch (FileNotFoundException)
            {
                throw new ContentLoadException();
            }
        }

        public Texture2D ObjectTexture(object obj)
        {
            if (obj == null) { return Blank; }
            PropertyInfo[] pinfos = obj.GetType().GetProperties();
            foreach (PropertyInfo pinfo in pinfos)
            {
                if (pinfo.Name == "GUITex")
                {
                    Texture2D output = pinfo.GetValue(obj, null) as Texture2D;
                    if (output != null) { return output; }
                }
            }
            return Blank;
        }
    }
}

{'fraction_non_alphanumeric': 0.10492825951341235, 'fraction_numerical': 0.0017467248908296944, 'mean_word_length': 1.7804370447450573, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6181457', 'n_tokens_mistral': 2080, 'n_tokens_neox': 1976, 'n_words': 516}