internal static void __Initialize__()
        {
            // Only perform this for entry assembly
            if (!(Assembly.GetEntryAssembly() == null || // .NET FW: null during module .ctor
                  Assembly.GetEntryAssembly() == Assembly.GetCallingAssembly()))  // .NET Core: check against calling assembly
            {
                return;
            }

            // delete old temp files if we can
            var dirs = Directory.GetDirectories(Path.GetTempPath(), "Xenko*");

            if (dirs != null)
            {
                foreach (string s in dirs)
                {
                    try
                    {
                        Directory.Delete(s, true);
                    } catch (Exception e) {
                        // might have been in use, oh well
                    }
                }
            }

            // Make sure our nuget local store is added to nuget config
            var    folder      = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string xenkoFolder = null;

            while (folder != null)
            {
                if (File.Exists(Path.Combine(folder, @"build\Xenko.sln")))
                {
                    xenkoFolder = folder;
                    var settings = NuGet.Configuration.Settings.LoadDefaultSettings(null);
                    // Remove non-existing sources: https://github.com/xenko3d/xenko/issues/338
                    RemoveDeletedSources(settings, "Xenko");
                    CheckPackageSource(settings, $"Xenko Dev {xenkoFolder}", Path.Combine(xenkoFolder, @"bin\packages"));
                    settings.SaveToDisk();
                    break;
                }
                folder = Path.GetDirectoryName(folder);
            }

            // Note: we perform nuget restore inside the assembly resolver rather than top level module ctor (otherwise it freezes)
            AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) =>
            {
                lock (assembliesLock)
                {
                    if (!assembliesResolved)
                    {
                        // Note: using NuGet will try to recursively resolve NuGet.*.resources.dll, so set assembliesResolved right away so that it bypasses everything
                        assembliesResolved = true;
                        var logger      = new Logger();
                        var task        = RestoreHelper.Restore(logger, Assembly.GetExecutingAssembly().GetName().Name, new VersionRange(new NuGetVersion(XenkoVersion.NuGetVersion)));
                        var timeouttask = RestoreHelper.TimeoutAfter(task, new TimeSpan(0, 0, 20));
                        var(request, result) = timeouttask.Result;
                        assemblies           = RestoreHelper.ListAssemblies(request, result);
                    }

                    if (assemblies != null)
                    {
                        var aname = new AssemblyName(eventArgs.Name);
                        if (aname.Name.StartsWith("Microsoft.Build") && aname.Name != "Microsoft.Build.Locator")
                        {
                            return(null);
                        }
                        var assemblyPath = assemblies.FirstOrDefault(x => Path.GetFileNameWithoutExtension(x) == aname.Name);
                        if (assemblyPath != null)
                        {
                            return(Assembly.LoadFrom(assemblyPath));
                        }
                    }
                }

                return(null);
            };
        }
Beispiel #2
0
        internal static void __Initialize__()
        {
            // Only perform this for entry assembly (which is null during module .ctor)
            if (Assembly.GetEntryAssembly() != null)
            {
                return;
            }

            // Make sure our nuget local store is added to nuget config
            var    folder      = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string xenkoFolder = null;

            while (folder != null)
            {
                if (File.Exists(Path.Combine(folder, @"build\Xenko.sln")))
                {
                    xenkoFolder = folder;
                    var settings = NuGet.Configuration.Settings.LoadDefaultSettings(null);
                    // Remove non-existing sources: https://github.com/xenko3d/xenko/issues/338
                    RemoveDeletedSources(settings, "Xenko");
                    CheckPackageSource(settings, $"Xenko Dev {xenkoFolder}", Path.Combine(xenkoFolder, @"bin\packages"));
                    settings.SaveToDisk();
                    break;
                }
                folder = Path.GetDirectoryName(folder);
            }

            // Note: we perform nuget restore inside the assembly resolver rather than top level module ctor (otherwise it freezes)
            AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) =>
            {
                if (!assembliesResolved)
                {
                    lock (assembliesLock)
                    {
                        // Note: using NuGet will try to recursively resolve NuGet.*.resources.dll, so set assembliesResolved right away so that it bypasses everything
                        assembliesResolved = true;

                        var logger = new Logger();
                        var previousSynchronizationContext = SynchronizationContext.Current;
                        try
                        {
                            // Since we execute restore synchronously, we don't want any surprise concerning synchronization context (i.e. Avalonia one doesn't work with this)
                            SynchronizationContext.SetSynchronizationContext(null);

                            // Only allow this specific version
                            var versionRange = new VersionRange(new NuGetVersion(XenkoVersion.NuGetVersion), true, new NuGetVersion(XenkoVersion.NuGetVersion), true);
                            var(request, result) = RestoreHelper.Restore(logger, Assembly.GetExecutingAssembly().GetName().Name, versionRange).Result;
                            if (!result.Success)
                            {
                                throw new InvalidOperationException($"Could not restore NuGet packages");
                            }

                            assemblies = RestoreHelper.ListAssemblies(request, result);
                        }
                        catch (Exception e)
                        {
                            var logFile = Path.GetTempPath() + Guid.NewGuid().ToString() + ".txt";
                            var logText = $@"Error restoring NuGet packages!

==== Exception details ====

{e}

==== Log ====

{string.Join(Environment.NewLine, logger.Logs.Select(x => $"[{x.Level}] {x.Message}"))}
";
                            File.WriteAllText(logFile, logText);
#if XENKO_NUGET_RESOLVER_UX
                            // Write log to file
                            System.Windows.Forms.MessageBox.Show($"{e.Message}{Environment.NewLine}{Environment.NewLine}Please see details in {logFile} (which will be automatically opened)", "Error restoring NuGet packages");
                            Process.Start(logFile);
#else
                            // Display log in console
                            Console.WriteLine(logText);
#endif
                            Environment.Exit(1);
                        }
                        finally
                        {
                            SynchronizationContext.SetSynchronizationContext(previousSynchronizationContext);
                        }
                    }
                }

                if (assemblies != null)
                {
                    var aname = new AssemblyName(eventArgs.Name);
                    if (aname.Name.StartsWith("Microsoft.Build") && aname.Name != "Microsoft.Build.Locator")
                    {
                        return(null);
                    }
                    var assemblyPath = assemblies.FirstOrDefault(x => Path.GetFileNameWithoutExtension(x) == aname.Name);
                    if (assemblyPath != null)
                    {
                        return(Assembly.LoadFrom(assemblyPath));
                    }
                }
                return(null);
            };
        }