<commit_before>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;

using Microsoft.Build.Shared;
using System.Reflection;

#if !FEATURE_APM
using System.Threading.Tasks;
#endif

namespace Microsoft.Build.Internal
{
    /// <summary>
    /// Enumeration of all possible (currently supported) options for handshakes.
    /// </summary>
    [Flags]
    internal enum HandshakeOptions
    {
        None = 0,

        /// <summary>
        /// Process is a TaskHost
        /// </summary>
        TaskHost = 1,

        /// <summary>
        /// Using the 2.0 CLR
        /// </summary>
        CLR2 = 2,

        /// <summary>
        /// 64-bit Intel process
        /// </summary>
        X64 = 4,

        /// <summary>
        /// Node reuse enabled
        /// </summary>
        NodeReuse = 8,

        /// <summary>
        /// Building with BelowNormal priority
        /// </summary>
        LowPriority = 16
    }

    /// <summary>
    /// This class contains utility methods for the MSBuild engine.
    /// </summary>
    static internal class CommunicationsUtilities
    {
        /// <summary>
        /// The timeout to connect to a node.
        /// </summary>
        private const int DefaultNodeConnectionTimeout = 900 * 1000; // 15 minutes; enough time that a dev will typically do another build in this time

        /// <summary>
        /// Flag if we have already calculated the FileVersion hashcode
        /// </summary>
        private static bool s_fileVersionChecked;

        /// <summary>
        /// A hashcode calculated from the fileversion
        /// </summary>
        private static int s_fileVersionHash;

        /// <summary>
        /// Whether to trace communications
        /// </summary>
        private static bool s_trace = String.Equals(Environment.GetEnvironmentVariable("MSBUILDDEBUGCOMM"), "1", StringComparison.Ordinal);

        /// <summary>
        /// Place to dump trace
        /// </summary>
        private static string s_debugDumpPath;

        /// <summary>
        /// Ticks at last time logged
        /// </summary>
        private static long s_lastLoggedTicks = DateTime.UtcNow.Ticks;

        /// <summary>
        /// Delegate to debug the communication utilities.
        /// </summary>
        internal delegate void LogDebugCommunications(string format, params object[] stuff);

        /// <summary>
        /// Gets or sets the node connection timeout.
        /// </summary>
        static internal int NodeConnectionTimeout
        {
            get { return GetIntegerVariableOrDefault("MSBUILDNODECONNECTIONTIMEOUT", DefaultNodeConnectionTimeout); }
        }

        /// <summary>
        /// Looks up the file version and caches the hashcode
        /// This file version hashcode is used in calculating the handshake
        /// </summary>
        private static int FileVersionHash
        {
            get
            {
                if (!s_fileVersionChecked)
                {
                    // We only hash in any complus_installroot value, not a file version.
                    // This is because in general msbuildtaskhost.exe does not load any assembly that
                    // the parent process loads, so they can't compare the version of a particular assembly.
                    // They can't compare their own versions, because if one of them is serviced, they
                    // won't match any more. The only known incompatibility is between a razzle and non-razzle
                    // parent and child. COMPLUS_Version can (and typically will) differ legitimately between
                    // them, so just check COMPLUS_InstallRoot.
                    string complusInstallRoot = Environment.GetEnvironmentVariable("COMPLUS_INSTALLROOT");

                    // This is easier in .NET 4+:
                    //  var fileIdentity = typeof(CommunicationsUtilities).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
                    // but we need to be 3.5 compatible here to work in MSBuildTaskHost
                    string fileIdentity = null;
                    foreach (var attribute in typeof(CommunicationsUtilities).GetTypeInfo().Assembly.GetCustomAttributes(false))
                    {
                        if (attribute is AssemblyInformationalVersionAttribute informationalVersionAttribute)
                        {
                            fileIdentity = informationalVersionAttribute.InformationalVersion;
                            break;
                        }
                    }

                    ErrorUtilities.VerifyThrow(fileIdentity != null, "Did not successfully retrieve InformationalVersion.");

                    s_fileVersionHash = GetHandshakeHashCode(complusInstallRoot ?? fileIdentity);
                    s_fileVersionChecked = true;
                }

                return s_fileVersionHash;
            }
        }

        /// <summary>
        /// Get environment block
        /// </summary>
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]<commit_msg>Change means of accessing file version (#5783)<commit_after>﻿// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;

using Microsoft.Build.Shared;
using System.Reflection;

#if !FEATURE_APM
using System.Threading.Tasks;
#endif

namespace Microsoft.Build.Internal
{
    /// <summary>
    /// Enumeration of all possible (currently supported) options for handshakes.
    /// </summary>
    [Flags]
    internal enum HandshakeOptions
    {
        None = 0,

        /// <summary>
        /// Process is a TaskHost
        /// </summary>
        TaskHost = 1,

        /// <summary>
        /// Using the 2.0 CLR
        /// </summary>
        CLR2 = 2,

        /// <summary>
        /// 64-bit Intel process
        /// </summary>
        X64 = 4,

        /// <summary>
        /// Node reuse enabled
        /// </summary>
        NodeReuse = 8,

        /// <summary>
        /// Building with BelowNormal priority
        /// </summary>
        LowPriority = 16
    }

    /// <summary>
    /// This class contains utility methods for the MSBuild engine.
    /// </summary>
    static internal class CommunicationsUtilities
    {
        /// <summary>
        /// The timeout to connect to a node.
        /// </summary>
        private const int DefaultNodeConnectionTimeout = 900 * 1000; // 15 minutes; enough time that a dev will typically do another build in this time

        /// <summary>
        /// Flag if we have already calculated the FileVersion hashcode
        /// </summary>
        private static bool s_fileVersionChecked;

        /// <summary>
        /// A hashcode calculated from the fileversion
        /// </summary>
        private static int s_fileVersionHash;

        /// <summary>
        /// Whether to trace communications
        /// </summary>
        private static bool s_trace = String.Equals(Environment.GetEnvironmentVariable("MSBUILDDEBUGCOMM"), "1", StringComparison.Ordinal);

        /// <summary>
        /// Place to dump trace
        /// </summary>
        private static string s_debugDumpPath;

        /// <summary>
        /// Ticks at last time logged
        /// </summary>
        private static long s_lastLoggedTicks = DateTime.UtcNow.Ticks;

        /// <summary>
        /// Delegate to debug the communication utilities.
        /// </summary>
        internal delegate void LogDebugCommunications(string format, params object[] stuff);

        /// <summary>
        /// Gets or sets the node connection timeout.
        /// </summary>
        static internal int NodeConnectionTimeout
        {
            get { return GetIntegerVariableOrDefault("MSBUILDNODECONNECTIONTIMEOUT", DefaultNodeConnectionTimeout); }
        }

        /// <summary>
        /// Looks up the file version and caches the hashcode
        /// This file version hashcode is used in calculating the handshake
        /// </summary>
        private static int FileVersionHash
        {
            get
            {
                if (!s_fileVersionChecked)
                {
                    // We only hash in any complus_installroot value, not a file version.
                    // This is because in general msbuildtaskhost.exe does not load any assembly that
                    // the parent process loads, so they can't compare the version of a particular assembly.
                    // They can't compare their own versions, because if one of them is serviced, they
                    // won't match any more. The only known incompatibility is between a razzle and non-razzle
                    // parent and child. COMPLUS_Version can (and typically will) differ legitimately between
                    // them, so just check COMPLUS_InstallRoot.
                    string complusInstallRoot = Environment.GetEnvironmentVariable("COMPLUS_INSTALLROOT");

                    // This is easier in .NET 4+:
                    //  var fileIdentity = typeof(CommunicationsUtilities).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
                    // but we need to be 3.5 compatible here to work in MSBuildTaskHost
                    string fileIdentity = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;

                    ErrorUtilities.VerifyThrow(fileIdentity != null, "Did not successfully retrieve InformationalVersion.");

                    s_fileVersionHash = GetHandshakeHashCode(complusInstallRoot ?? fileIdentity);
                    s_fileVersionChecked = true;
                }

                return s_fileVersionHash;
            }
        }

        /// <summary>
        /// Get environment block
        /// </summary>
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
{'fraction_non_alphanumeric': 0.09054820415879017, 'fraction_numerical': 0.0058601134215500944, 'mean_word_length': 1.817842876165113, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 73, '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': '6597500', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2442, 'n_words': 832}