<commit_before>package com.jetbrains.rider.plugins.unity.run

import com.intellij.execution.ProgramRunnerUtil
import com.intellij.execution.configurations.CommandLineTokenizer
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.executors.DefaultDebugExecutor
import com.intellij.execution.process.ProcessInfo
import com.intellij.execution.runners.ExecutionEnvironmentBuilder
import com.intellij.execution.util.ExecUtil
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import com.intellij.xdebugger.XDebuggerManager
import com.jetbrains.rider.plugins.unity.run.attach.UnityAttachProcessConfiguration
import com.jetbrains.rider.plugins.unity.util.EditorInstanceJson
import com.jetbrains.rider.plugins.unity.util.EditorInstanceJsonStatus
import com.jetbrains.rider.plugins.unity.util.convertPidToDebuggerPort
import com.jetbrains.rider.run.configurations.remote.RemoteConfiguration
import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Paths

data class UnityProcessInfo(val projectName: String?, val roleName: String?)

object UnityRunUtil {
    private val logger = Logger.getInstance(UnityRunUtil::class.java)

    fun isUnityEditorProcess(processInfo: ProcessInfo): Boolean {
        logger.trace("Checking Unity Process: isUnityEditorProcess")
        val name = processInfo.executableDisplayName
        logger.trace("Checking Unity Process, name: $name")
        var execPathName = ""
        if (processInfo.executableCannonicalPath.isPresent)
            execPathName = Paths.get(processInfo.executableCannonicalPath.get()).fileName.toString() // for the case of symlink

        logger.trace("Checking Unity Process, execPathName: $execPathName")

        val result = (name.startsWith("Unity", true)
            || name.contains("Unity.app")
            || execPathName.equals("Unity", true))
            && !name.contains("UnityDebug")
            && !name.contains("UnityShader")
            && !name.contains("UnityHelper")
            && !name.contains("Unity Helper")
            && !name.contains("Unity Hub")
            && !name.contains("UnityCrashHandler")
            && !name.contains("UnityPackageManager")
            && !name.contains("Unity.Licensing.Client")
            && !name.contains("UnityDownloadAssistant")
            && !name.contains("unityhub", true)
        logger.trace("Checking Unity Process, result: $result")
        return result
    }

    fun isValidUnityEditorProcess(pid: Int, processList: Array<out ProcessInfo>): Boolean {
        logger.trace("Checking Unity Process, current pid: $pid")
        logger.trace("Checking Unity Process, processCount: ${processList.count()}")
        return processList.any { it.pid == pid && isUnityEditorProcess(it) }
    }
<commit_msg>Simplify checking for Unity editor processes<commit_after>package com.jetbrains.rider.plugins.unity.run

import com.intellij.execution.ProgramRunnerUtil
import com.intellij.execution.configurations.CommandLineTokenizer
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.execution.executors.DefaultDebugExecutor
import com.intellij.execution.process.ProcessInfo
import com.intellij.execution.runners.ExecutionEnvironmentBuilder
import com.intellij.execution.util.ExecUtil
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil
import com.intellij.xdebugger.XDebuggerManager
import com.jetbrains.rider.plugins.unity.run.attach.UnityAttachProcessConfiguration
import com.jetbrains.rider.plugins.unity.util.EditorInstanceJson
import com.jetbrains.rider.plugins.unity.util.EditorInstanceJsonStatus
import com.jetbrains.rider.plugins.unity.util.convertPidToDebuggerPort
import com.jetbrains.rider.run.configurations.remote.RemoteConfiguration
import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.Paths

data class UnityProcessInfo(val projectName: String?, val roleName: String?)

object UnityRunUtil {
    private val logger = Logger.getInstance(UnityRunUtil::class.java)

    fun isUnityEditorProcess(processInfo: ProcessInfo): Boolean {
        val name = processInfo.executableDisplayName

        // For symlinks
        val canonicalName = if (processInfo.executableCannonicalPath.isPresent) {
            Paths.get(processInfo.executableCannonicalPath.get()).fileName.toString()
        }
        else ""

        logger.trace("isUnityEditorProcess: $name")
        logger.trace("Checking Unity Process, execPathName: $canonicalName")

        // Based on Unity's own VS Code debugger, we simply look for "Unity" or "Unity Editor". Java's
        // ProcessInfo#executableDisplayName is the executable name with `.exe` removed. This matches the behaviour of
        // .NET's Process.ProcessName
        // https://github.com/Unity-Technologies/MonoDevelop.Debugger.Soft.Unity/blob/9f116ee5d344bce5888e838a75ded418bd7852c7/UnityProcessDiscovery.cs#L155
        return (name.equals("Unity", true) || name.equals("Unity Editor", true)
            || canonicalName.equals("unity", true) || canonicalName.equals("Unity Editor", true))
    }

    fun isValidUnityEditorProcess(pid: Int, processList: Array<out ProcessInfo>): Boolean {
        logger.trace("Checking Unity Process, current pid: $pid")
        logger.trace("Checking Unity Process, processCount: ${processList.count()}")
        return processList.any { it.pid == pid && isUnityEditorProcess(it) }
    }

{'fraction_non_alphanumeric': 0.10210669384981312, 'fraction_numerical': 0.004757050628610262, 'mean_word_length': 4.430811808118081, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6274642', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1629, 'n_words': 295}