ajbecker
10/11/2013 - 11:57 PM

Logic to create ScriptResource code -- pulled from IL of System.Web.Extensions

Logic to create ScriptResource code -- pulled from IL of System.Web.Extensions

namespace foo {



    #region TestClass

    static class HexParser
    {
        public static byte[] Parse(string token)
        {
            byte[] array = new byte[token.Length / 2];
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = byte.Parse(token.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture);
            }
            return array;
        }
        public static string ToString(byte[] tokenBytes)
        {
            StringBuilder stringBuilder = new StringBuilder(tokenBytes.Length * 2);
            for (int i = 0; i < tokenBytes.Length; i++)
            {
                stringBuilder.Append(tokenBytes[i].ToString("x2", System.Globalization.CultureInfo.InvariantCulture));
            }
            return stringBuilder.ToString();
        }
    }

    static class AssemblyUtil
    {
        private const string _emptyFileVersion = "0.0.0.0";
        public static string GetAssemblyFileVersion(Assembly assembly)
        {
            AssemblyFileVersionAttribute[] array = (AssemblyFileVersionAttribute[])assembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);
            string text;
            if (array.Length > 0)
            {
                text = array[0].Version;
                if (string.IsNullOrEmpty(text))
                {
                    text = "0.0.0.0";
                }
            }
            else
            {
                text = "0.0.0.0";
            }
            return text;
        }
    }
    class Pair<TFirst, TSecond>
    {
        private readonly TFirst _first;
        private readonly TSecond _second;
        public TFirst First
        {
            get
            {
                return this._first;
            }
        }
        public TSecond Second
        {
            get
            {
                return this._second;
            }
        }
        public Pair(TFirst first, TSecond second)
        {
            this._first = first;
            this._second = second;
        }
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return true;
            }
            Pair<TFirst, TSecond> pair = obj as Pair<TFirst, TSecond>;
            if (pair != null)
            {
                if (pair._first != null || this._first != null)
                {
                    if (pair._first == null)
                    {
                        return false;
                    }
                    TFirst first = pair._first;
                    if (!first.Equals(this._first))
                    {
                        return false;
                    }
                }
                if (pair._second == null && this._second == null)
                {
                    return true;
                }
                if (pair._second != null)
                {
                    TSecond second = pair._second;
                    return second.Equals(this._second);
                }
                return false;
            }
            return false;
        }
        public override int GetHashCode()
        {
            int arg_24_0;
            if (this._first != null)
            {
                TFirst first = this._first;
                arg_24_0 = first.GetHashCode();
            }
            else
            {
                arg_24_0 = 0;
            }
            int h = arg_24_0;
            int arg_49_0;
            if (this._second != null)
            {
                TSecond second = this._second;
                arg_49_0 = second.GetHashCode();
            }
            else
            {
                arg_49_0 = 0;
            }
            int h2 = arg_49_0;
            return h ^ h2;
        }
    }

    class ScriptResourceInfo
    {
        private string _contentType;
        private bool _performSubstitution;
        private string _scriptName;
        private string _scriptResourceName;
        private string _typeName;
        private bool _isDebug;
        private static readonly System.Collections.IDictionary _scriptCache = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
        private static readonly System.Collections.IDictionary _duplicateScriptAttributesChecked = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
        public static readonly ScriptResourceInfo Empty = new ScriptResourceInfo();
        public string ContentType
        {
            get
            {
                return this._contentType;
            }
            set
            {
                this._contentType = value;
            }
        }
        public bool IsDebug
        {
            get
            {
                return this._isDebug;
            }
            set
            {
                this._isDebug = value;
            }
        }
        public bool PerformSubstitution
        {
            get
            {
                return this._performSubstitution;
            }
            set
            {
                this._performSubstitution = value;
            }
        }
        public string ScriptName
        {
            get
            {
                return this._scriptName;
            }
            set
            {
                this._scriptName = value;
            }
        }
        public string ScriptResourceName
        {
            get
            {
                return this._scriptResourceName;
            }
        }
        public string TypeName
        {
            get
            {
                return this._typeName;
            }
        }
        private ScriptResourceInfo()
        {
        }
        public ScriptResourceInfo(System.Web.UI.ScriptResourceAttribute attr)
            : this()
        {
            this._scriptName = attr.ScriptName;
            this._scriptResourceName = attr.ScriptResourceName;
            this._typeName = attr.TypeName;
        }
        public static ScriptResourceInfo GetInstance(Assembly assembly, string resourceName)
        {
            if (!ScriptResourceInfo._duplicateScriptAttributesChecked.Contains(assembly))
            {
                Dictionary<string, bool> dictionary = new Dictionary<string, bool>();
                object[] customAttributes = assembly.GetCustomAttributes(typeof(System.Web.UI.ScriptResourceAttribute), false);
                for (int i = 0; i < customAttributes.Length; i++)
                {
                    System.Web.UI.ScriptResourceAttribute scriptResourceAttribute = (System.Web.UI.ScriptResourceAttribute)customAttributes[i];
                    string scriptName = scriptResourceAttribute.ScriptName;
                    if (dictionary.ContainsKey(scriptName))
                    {
                        throw new InvalidOperationException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Blah Blah Blah", new object[]
                            {
                                scriptName,
                                assembly.GetName()
                            }));
                    }
                    dictionary.Add(scriptName, true);
                }
                ScriptResourceInfo._duplicateScriptAttributesChecked[assembly] = true;
            }
            Pair<Assembly, string> key = new Pair<Assembly, string>(assembly, resourceName);
            ScriptResourceInfo scriptResourceInfo = (ScriptResourceInfo)ScriptResourceInfo._scriptCache[key];
            if (scriptResourceInfo == null)
            {
                scriptResourceInfo = ScriptResourceInfo.Empty;
                object[] customAttributes2 = assembly.GetCustomAttributes(typeof(System.Web.UI.ScriptResourceAttribute), false);
                for (int j = 0; j < customAttributes2.Length; j++)
                {
                    System.Web.UI.ScriptResourceAttribute scriptResourceAttribute2 = (System.Web.UI.ScriptResourceAttribute)customAttributes2[j];
                    if (string.Equals(scriptResourceAttribute2.ScriptName, resourceName, StringComparison.Ordinal))
                    {
                        scriptResourceInfo = new ScriptResourceInfo(scriptResourceAttribute2);
                        break;
                    }
                }
                customAttributes2 = assembly.GetCustomAttributes(typeof(System.Web.UI.WebResourceAttribute), false);
                for (int k = 0; k < customAttributes2.Length; k++)
                {
                    System.Web.UI.WebResourceAttribute webResourceAttribute = (System.Web.UI.WebResourceAttribute)customAttributes2[k];
                    if (string.Equals(webResourceAttribute.WebResource, resourceName, StringComparison.Ordinal))
                    {
                        if (scriptResourceInfo == ScriptResourceInfo.Empty)
                        {
                            scriptResourceInfo = new ScriptResourceInfo();
                            scriptResourceInfo.ScriptName = resourceName;
                        }
                        scriptResourceInfo.ContentType = webResourceAttribute.ContentType;
                        scriptResourceInfo.PerformSubstitution = webResourceAttribute.PerformSubstitution;
                        break;
                    }
                }
                scriptResourceInfo.IsDebug = resourceName.EndsWith(".debug.js", StringComparison.OrdinalIgnoreCase);
                ScriptResourceInfo._scriptCache[key] = scriptResourceInfo;
            }
            return scriptResourceInfo;
        }
    }

    class TestClass
    {
        public static void Test(string text)
        {
            ProcessRequest(text);
        }

        static string GetResourceName(string rawResourceName)
        {
            if (rawResourceName.EndsWith(".resources", StringComparison.OrdinalIgnoreCase))
            {
                return rawResourceName.Substring(0, rawResourceName.Length - 10);
            }
            return rawResourceName;
        }

        static System.Resources.ResourceManager GetResourceManager(string resourceName, Assembly assembly)
        {
            if (string.IsNullOrEmpty(resourceName))
            {
                return null;
            }
            return new System.Resources.ResourceManager(GetResourceName(resourceName), assembly);
        }

        private static void WriteScript(Assembly assembly, ScriptResourceInfo instance, ScriptResourceInfo resourceInfo, System.Resources.ResourceManager resourceManager, System.Resources.ResourceSet resourceSet, System.Resources.ResourceManager resourceManager2, System.Resources.ResourceSet resourceSet2, object zip, object notifyScriptLoaded, StringBuilder output)
        {
            using (System.IO.StreamReader streamReader = new System.IO.StreamReader(assembly.GetManifestResourceStream(resourceInfo.ScriptName), true))
            {
                if (resourceInfo.IsDebug)
                {
                    AssemblyName name = assembly.GetName();
                    output.AppendLine("// Name:        " + resourceInfo.ScriptName);
                    output.AppendLine("// Assembly:    " + name.Name);
                    output.AppendLine("// Version:     " + name.Version.ToString());
                    output.AppendLine("// FileVersion: " + AssemblyUtil.GetAssemblyFileVersion(assembly));
                }
                if (resourceInfo.PerformSubstitution)
                {
                    //ScriptResourceAttribute.CopyScriptToStringBuilderWithSubstitution(streamReader.ReadToEnd(), assembly, zip, notifyScriptLoaded, output);
                }
                else
                {
                    output.Append(streamReader.ReadToEnd());
                }
                //ScriptResourceAttribute.WriteResourceToStringBuilder(resourceInfo, releaseResourceInfo, resourceManager, neutralSet, releaseResourceManager, releaseNeutralSet, output);
                //ScriptResourceAttribute.WriteNotificationToStringBuilder(notifyScriptLoaded, output);
            }
        }

        private static string GetScriptFromWebResourceInternal(Assembly assembly, string resourceName, System.Globalization.CultureInfo culture, bool flag2, bool param2, out string contentType)
        {
            ScriptResourceInfo instance = ScriptResourceInfo.GetInstance(assembly, resourceName);
            ScriptResourceInfo scriptResourceInfo = null;
            if (resourceName.EndsWith(".debug.js", StringComparison.OrdinalIgnoreCase))
            {
                string resourceName2 = resourceName.Substring(0, resourceName.Length - 9) + ".js";
                scriptResourceInfo = ScriptResourceInfo.GetInstance(assembly, resourceName2);
            }
            if (instance == ScriptResourceInfo.Empty && (scriptResourceInfo == null || scriptResourceInfo == ScriptResourceInfo.Empty))
            {
                throw new Exception("THis is a test");
            }
            System.Resources.ResourceManager resourceManager = null;
            System.Resources.ResourceSet resourceSet = null;
            System.Resources.ResourceManager resourceManager2 = null;
            System.Resources.ResourceSet resourceSet2 = null;
            System.Globalization.CultureInfo currentUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
            string result;
            try
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                if (!string.IsNullOrEmpty(instance.ScriptResourceName))
                {
                    resourceManager = GetResourceManager(instance.ScriptResourceName, assembly);
                    resourceSet = resourceManager.GetResourceSet(System.Globalization.CultureInfo.InvariantCulture, true, true);
                }
                if (scriptResourceInfo != null && !string.IsNullOrEmpty(scriptResourceInfo.ScriptResourceName))
                {
                    resourceManager2 = GetResourceManager(scriptResourceInfo.ScriptResourceName, assembly);
                    resourceSet2 = resourceManager2.GetResourceSet(System.Globalization.CultureInfo.InvariantCulture, true, true);
                }
                if (scriptResourceInfo != null && !string.IsNullOrEmpty(scriptResourceInfo.ScriptResourceName) && !string.IsNullOrEmpty(instance.ScriptResourceName) && scriptResourceInfo.TypeName != instance.TypeName)
                {
                    throw new HttpException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Type Mismatch", new object[]
                {
                    scriptResourceInfo.ScriptResourceName
                }));
                }
                StringBuilder stringBuilder = new StringBuilder();
                WriteScript(assembly, instance, scriptResourceInfo, resourceManager, resourceSet, resourceManager2, resourceSet2, false, true, stringBuilder);
                contentType = instance.ContentType;
                result = stringBuilder.ToString();
            }
            finally
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = currentUICulture;
                if (resourceSet2 != null)
                {
                    resourceSet2.Dispose();
                }
                if (resourceSet != null)
                {
                    resourceSet.Dispose();
                }
            }
            return result;
        }

        private static void ProcessRequest(string text)
        {
            char c = text[0];
            bool flag;
            bool flag2;
            bool notifyScriptLoaded = false;
            if (c <= 'Z')
            {
                switch (c)
                {
                    case 'Q':
                        flag = false;
                        flag2 = true;
                        notifyScriptLoaded = true;
                        goto IL_C3;
                    case 'R':
                        flag = false;
                        flag2 = false;
                        notifyScriptLoaded = true;
                        goto IL_C3;
                    case 'S':
                        break;
                    case 'T':
                        throw new Exception("This is just a test");
                        return;
                    case 'U':
                        flag = true;
                        flag2 = false;
                        notifyScriptLoaded = true;
                        goto IL_C3;
                    default:
                        if (c == 'Z')
                        {
                            flag = true;
                            flag2 = true;
                            notifyScriptLoaded = true;
                            goto IL_C3;
                        }
                        break;
                }
            }
            else
            {
                switch (c)
                {
                    case 'q':
                        flag = false;
                        flag2 = true;
                        notifyScriptLoaded = false;
                        goto IL_C3;
                    case 'r':
                        flag = false;
                        flag2 = false;
                        notifyScriptLoaded = false;
                        goto IL_C3;
                    case 's':
                    case 't':
                        break;
                    case 'u':
                        flag = true;
                        flag2 = false;
                        notifyScriptLoaded = false;
                        goto IL_C3;
                    default:
                        if (c == 'z')
                        {
                            flag = true;
                            flag2 = true;
                            notifyScriptLoaded = false;
                            goto IL_C3;
                        }
                        break;
                }
            }
            throw new Exception("this is just a test");
            return;
        IL_C3:
            text = text.Substring(1);
            if (string.IsNullOrEmpty(text))
            {
                throw new Exception("this is just a test");
            }
            string[] array = text.Split(new char[]
        {
            '|'
        });
            if (flag)
            {
                if (array.Length != 3 && array.Length != 5)
                {
                    throw new Exception("this is just a test");
                }
            }
            else
            {
                if (array.Length % 2 != 0)
                {
                    throw new Exception("this is just a test");
                }
            }
            StringBuilder stringBuilder = new StringBuilder();
            string text2 = null;
            if (flag)
            {
                string assemblyName = array[0];
                string resourceName = array[1];
                string text3 = array[2];
                Assembly assembly = GetAssembly(assemblyName);
                if (assembly == null)
                {
                    throw new Exception("this is just a test");
                }
                stringBuilder.Append(GetScriptFromWebResourceInternal(assembly, resourceName, string.IsNullOrEmpty(text3) ? System.Globalization.CultureInfo.InvariantCulture : new System.Globalization.CultureInfo(text3), flag2, false, out text2));
            }

            string result = stringBuilder.ToString();
        }

        private static Assembly GetAssembly(string assemblyName)
        {
            string[] array = assemblyName.Split(new char[]
        {
            ','
        });
            if (array.Length != 1 && array.Length != 4)
            {
                throw new Exception("this is just a test");
            }
            AssemblyName assemblyName2 = new AssemblyName();
            assemblyName2.Name = array[0];
            if (array.Length == 4)
            {
                assemblyName2.Version = new Version(array[1]);
                string text = array[2];
                assemblyName2.CultureInfo = ((text.Length > 0) ? new System.Globalization.CultureInfo(text) : System.Globalization.CultureInfo.InvariantCulture);
                assemblyName2.SetPublicKeyToken(HexParser.Parse(array[3]));
            }
            Assembly result = null;
            try
            {
                result = Assembly.Load(assemblyName2);
            }
            catch (System.IO.FileNotFoundException innerException)
            {
                throw new Exception("this is just a test");
            }
            catch (System.IO.FileLoadException innerException2)
            {
                throw new Exception("this is just a test");
            }
            catch (BadImageFormatException innerException3)
            {
                throw new Exception("this is just a test");
            }
            return result;
        }
    }

    #endregion






}