public static Assembly LoadFromFile(string path, string xform, VersionManagerALC context)
        {
            lock (_ALCs)
            {
                try
                {
                    AssemblyName asmName = AssemblyName.GetAssemblyName(path);

                    if (_ContextMap.ContainsKey(xform))
                    {
                        return(_ContextMap[xform]._AssemblyMap[xform]);
                    }

                    VersionManagerALC c = _ALCs.FirstOrDefault(i => i == context);
                    if (c != null)
                    {
                        try
                        {
                            Assembly assembly = c.LoadFromAssemblyPath(path);

                            if (assembly != null)
                            {
                                c._AssemblyMap[xform] = assembly;
                                _ContextMap[xform]    = c;
                            }

                            return(assembly);
                        }
                        catch //(Exception ex)
                        {
                            //STEM.Sys.EventLog.WriteEntry("VersionManagerALC:LoadFromFile", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
                        }

                        return(null);
                    }
                    else
                    {
                        foreach (VersionManagerALC i in _ALCs)
                        {
                            Assembly assembly = LoadFromFile(path, xform, i);

                            if (assembly != null)
                            {
                                return(assembly);
                            }
                        }
                    }

                    VersionManagerALC newContext = new VersionManagerALC();
                    _ALCs.Add(newContext);
                    return(LoadFromFile(path, xform, newContext));
                }
                catch { }

                return(null);
            }
        }
Example #2
0
        static string Cache(_Cache cache, string file, bool renameSourceAssemblies)
        {
            if (String.IsNullOrEmpty(file))
            {
                throw new ArgumentNullException(nameof(file));
            }

            file = STEM.Sys.IO.Path.AdjustPath(file);

            if (file.EndsWith("STEM.Auth.dll", StringComparison.InvariantCultureIgnoreCase))
            {
                return(file);
            }

            string xform = TransformFilename(file);

            if (String.IsNullOrEmpty(xform))
            {
                STEM.Sys.EventLog.WriteEntry("VersionManager.Cache", new Exception(file, new ArgumentNullException(nameof(xform))), EventLog.EventLogEntryType.Information);
            }

            string vcFile = xform;

            if (vcFile != null)
            {
                if (xform.StartsWith("STEM.Surge.7.0.0.0", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(file);
                }
                if (xform.StartsWith("STEM.Surge.Internal.7.0.0.0", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(file);
                }
                if (xform.StartsWith("STEM.Sys.7.0.0.0", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(file);
                }
                if (xform.StartsWith("STEM.Sys.Internal.7.0.0.0", StringComparison.InvariantCultureIgnoreCase))
                {
                    return(file);
                }

                string fileXformed = Path.Combine(STEM.Sys.IO.Path.GetDirectoryName(file), xform);

                if ((STEM.Sys.IO.Path.GetDirectoryName(file).TrimEnd(Path.DirectorySeparatorChar)).Equals(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar), StringComparison.CurrentCultureIgnoreCase))
                {
                    try
                    {
                        using (new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None)) { }
                    }
                    catch
                    {
                        try
                        {
                            lock (_Cached)
                                _Evaluated[file] = File.GetLastWriteTimeUtc(file);
                        }
                        catch { }

                        lock (_Cached)
                            _Cached.Add(file.ToUpper(System.Globalization.CultureInfo.CurrentCulture));

                        return(vcFile);
                    }
                }

                if (file.IndexOf("win-x86", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                    file.IndexOf("win-x64", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                    file.IndexOf("linux-x86", StringComparison.InvariantCultureIgnoreCase) >= 0 ||
                    file.IndexOf("linux-x64", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    if (!xform.StartsWith("STEM."))
                    {
                        renameSourceAssemblies = false;
                    }
                }

                vcFile = Path.Combine(VersionCache, xform);
                if (!renameSourceAssemblies)
                {
                    vcFile = Path.Combine(VersionCache, STEM.Sys.IO.Path.GetFileName(file));
                }

                if (cache != null)
                {
                    if (cache.Recurse)
                    {
                        string subDir = STEM.Sys.IO.Path.GetDirectoryName(file.Replace(STEM.Sys.IO.Path.FirstTokenOfPath(file), STEM.Sys.IO.Path.FirstTokenOfPath(cache.Path))).Substring(cache.Path.Length).Trim(Path.DirectorySeparatorChar);

                        vcFile = Path.Combine(Path.Combine(VersionCache, subDir), STEM.Sys.IO.Path.GetFileName(vcFile));

                        if (!Directory.Exists(STEM.Sys.IO.Path.GetDirectoryName(vcFile)))
                        {
                            Directory.CreateDirectory(STEM.Sys.IO.Path.GetDirectoryName(vcFile));
                        }
                    }
                }

                if (!vcFile.Equals(file, StringComparison.CurrentCultureIgnoreCase))
                {
                    if (File.Exists(vcFile))
                    {
                        try
                        {
                            using (new FileStream(vcFile, FileMode.Open, FileAccess.Read, FileShare.None)) { }
                            File.Copy(file, vcFile, true);
                        }
                        catch { }
                    }
                    else
                    {
                        File.Copy(file, vcFile, true);
                    }

                    if (STEM.Sys.IO.Path.GetDirectoryName(file).StartsWith(VersionCache, StringComparison.CurrentCultureIgnoreCase))
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch { }
                    }

                    if ((STEM.Sys.IO.Path.GetDirectoryName(file).TrimEnd(Path.DirectorySeparatorChar)).Equals(AppDomain.CurrentDomain.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar), StringComparison.CurrentCultureIgnoreCase))
                    {
                        try
                        {
                            File.Delete(file);
                        }
                        catch { }
                    }
                }

                if (renameSourceAssemblies)
                {
                    try
                    {
                        if (File.Exists(file))
                        {
                            if (!file.Equals(fileXformed, StringComparison.CurrentCultureIgnoreCase))
                            {
                                if (File.Exists(file) && File.Exists(fileXformed))
                                {
                                    if (File.GetLastWriteTimeUtc(file) >= File.GetLastWriteTimeUtc(fileXformed))
                                    {
                                        Guid myExt = Guid.NewGuid();

                                        try
                                        {
                                            while (File.Exists(file))
                                            {
                                                try
                                                {
                                                    if (!File.Exists(file + "." + myExt.ToString()))
                                                    {
                                                        File.Move(file, file + "." + myExt.ToString());
                                                    }
                                                    break;
                                                }
                                                catch { }
                                            }
                                        }
                                        catch { }

                                        while (File.Exists(file + "." + myExt.ToString()))
                                        {
                                            try
                                            {
                                                try
                                                {
                                                    if (File.Exists(fileXformed))
                                                    {
                                                        File.Delete(fileXformed);
                                                    }
                                                }
                                                catch
                                                {
                                                    if (File.Exists(fileXformed))
                                                    {
                                                        File.Delete(file + "." + myExt.ToString());
                                                    }
                                                }

                                                if (File.Exists(file + "." + myExt.ToString()))
                                                {
                                                    File.Move(file + "." + myExt.ToString(), fileXformed);
                                                }
                                            }
                                            catch { }
                                        }
                                    }
                                    else
                                    {
                                        while (File.Exists(file) && File.Exists(fileXformed))
                                        {
                                            try
                                            {
                                                File.Delete(file);
                                            }
                                            catch { }
                                        }
                                    }
                                }
                                else
                                {
                                    while (File.Exists(file) && !File.Exists(fileXformed))
                                    {
                                        try
                                        {
                                            File.Move(file, fileXformed);
                                        }
                                        catch { }
                                    }
                                }
                            }
                        }
                    }
                    catch { }
                }

                try
                {
                    if (File.Exists(vcFile))
                    {
                        if (!IsAssembly(file))
                        {
                            try
                            {
                                lock (_Cached)
                                    _Evaluated[file] = File.GetLastWriteTimeUtc(file);
                            }
                            catch { }

                            lock (_Cached)
                                _Cached.Add(file.ToUpper(System.Globalization.CultureInfo.CurrentCulture));

                            return(file);
                        }

                        lock (_Cached)
                        {
                            if (!_Attempts.ContainsKey(file))
                            {
                                _Attempts[file] = 0;
                            }

                            _Attempts[file]    = _Attempts[file] + 1;
                            _LastAttempt[file] = DateTime.UtcNow;
                        }

                        try
                        {
                            Assembly asm = VersionManagerALC.LoadFromFile(vcFile, xform, null);

                            if (asm == null)
                            {
                                throw new Exception(file + " could not be loaded.");
                            }

                            AssemblyName aName = new AssemblyName(asm.FullName);

                            lock (_Cached)
                            {
                                _CachedAssemblies[vcFile] = asm;

                                lock (_AssemblyByName)
                                    _AssemblyByName[aName] = asm;

                                if (!_Cached.Contains(vcFile.ToUpper(System.Globalization.CultureInfo.CurrentCulture)))
                                {
                                    _Cached.Add(vcFile.ToUpper(System.Globalization.CultureInfo.CurrentCulture));
                                }

                                if (!_Cached.Contains(System.IO.Path.Combine(STEM.Sys.IO.Path.GetDirectoryName(vcFile), xform).ToUpper(System.Globalization.CultureInfo.CurrentCulture)))
                                {
                                    _Cached.Add(System.IO.Path.Combine(STEM.Sys.IO.Path.GetDirectoryName(vcFile), xform).ToUpper(System.Globalization.CultureInfo.CurrentCulture));
                                }

                                try
                                {
                                    _Evaluated[vcFile] = File.GetLastWriteTimeUtc(vcFile);
                                }
                                catch { }

                                if (renameSourceAssemblies)
                                {
                                    try
                                    {
                                        _Evaluated[fileXformed] = File.GetLastWriteTimeUtc(fileXformed);
                                    }
                                    catch { }
                                }
                                else
                                {
                                    try
                                    {
                                        _Evaluated[file] = File.GetLastWriteTimeUtc(file);
                                    }
                                    catch { }
                                }

                                _Attempts.Remove(file);
                                _LastAttempt.Remove(file);
                            }

                            if (onAssemblyLoaded != null)
                            {
                                foreach (AssemblyLoaded d in onAssemblyLoaded.GetInvocationList())
                                {
                                    try
                                    {
                                        d(vcFile, asm);
                                    }
                                    catch (Exception ex)
                                    {
                                        STEM.Sys.EventLog.WriteEntry("VersionManager:onAssemblyLoaded", ex.ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            lock (_Cached)
                                if (!_Cached.Contains(vcFile.ToUpper(System.Globalization.CultureInfo.CurrentCulture)))
                                {
                                    _Cached.Add(vcFile.ToUpper(System.Globalization.CultureInfo.CurrentCulture));
                                    STEM.Sys.EventLog.WriteEntry("VersionManager.Cache", new Exception("Could not load " + vcFile, ex).ToString(), STEM.Sys.EventLog.EventLogEntryType.Error);
                                }
                        }
                    }
                }
                catch { }

                return(STEM.Sys.IO.Path.GetFileName(vcFile));
            }
            else
            {
                try
                {
                    lock (_Cached)
                        _Evaluated[file] = File.GetLastWriteTimeUtc(file);
                }
                catch { }

                return(STEM.Sys.IO.Path.GetFileName(file));
            }
        }