Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
RemoteHostMethodInfo.cs555 linesDownload Raw Back to host
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.Generic;5using System.Collections.ObjectModel;6using System.Management.Automation.Host;7 8using Dbg = System.Management.Automation.Diagnostics;9 10namespace System.Management.Automation.Remoting11{12    /// <summary>13    /// The RemoteHostMethodId enum.14    /// </summary>15    internal enum RemoteHostMethodId16    {17        // Host read-only properties.18        GetName = 1,19        GetVersion = 2,20        GetInstanceId = 3,21        GetCurrentCulture = 4,22        GetCurrentUICulture = 5,23 24        // Host methods.25        SetShouldExit = 6,26        EnterNestedPrompt = 7,27        ExitNestedPrompt = 8,28        NotifyBeginApplication = 9,29        NotifyEndApplication = 10,30 31        // Host UI methods.32        ReadLine = 11,33        ReadLineAsSecureString = 12,34        Write1 = 13,35        Write2 = 14,36        WriteLine1 = 15,37        WriteLine2 = 16,38        WriteLine3 = 17,39        WriteErrorLine = 18,40        WriteDebugLine = 19,41        WriteProgress = 20,42        WriteVerboseLine = 21,43        WriteWarningLine = 22,44        Prompt = 23,45        PromptForCredential1 = 24,46        PromptForCredential2 = 25,47        PromptForChoice = 26,48 49        // Host Raw UI read-write properties.50        GetForegroundColor = 27,51        SetForegroundColor = 28,52        GetBackgroundColor = 29,53        SetBackgroundColor = 30,54        GetCursorPosition = 31,55        SetCursorPosition = 32,56        GetWindowPosition = 33,57        SetWindowPosition = 34,58        GetCursorSize = 35,59        SetCursorSize = 36,60        GetBufferSize = 37,61        SetBufferSize = 38,62        GetWindowSize = 39,63        SetWindowSize = 40,64        GetWindowTitle = 41,65        SetWindowTitle = 42,66 67        // Host Raw UI read-only properties.68        GetMaxWindowSize = 43,69        GetMaxPhysicalWindowSize = 44,70        GetKeyAvailable = 45,71 72        // Host Raw UI methods.73        ReadKey = 46,74        FlushInputBuffer = 47,75        SetBufferContents1 = 48,76        SetBufferContents2 = 49,77        GetBufferContents = 50,78        ScrollBufferContents = 51,79 80        // IHostSupportsInteractiveSession methods.81        PushRunspace = 52,82        PopRunspace = 53,83 84        // IHostSupportsInteractiveSession read-only properties.85        GetIsRunspacePushed = 54,86        GetRunspace = 55,87 88        // IHostSupportsMultipleChoiceSelection89        PromptForChoiceMultipleSelection = 56,90    }91 92    /// <summary>93    /// Stores information about remote host methods. By storing information94    /// in this data structure we only need to transport enums on the wire.95    /// </summary>96    internal class RemoteHostMethodInfo97    {98        /// <summary>99        /// Interface type.100        /// </summary>101        internal Type InterfaceType { get; }102 103        /// <summary>104        /// Name.105        /// </summary>106        internal string Name { get; }107 108        /// <summary>109        /// Return type.110        /// </summary>111        internal Type ReturnType { get; }112 113        /// <summary>114        /// Parameter types.115        /// </summary>116        internal Type[] ParameterTypes { get; }117 118        /// <summary>119        /// Create a new instance of RemoteHostMethodInfo.120        /// </summary>121        internal RemoteHostMethodInfo(122            Type interfaceType,123            string name,124            Type returnType,125            Type[] parameterTypes)126        {127            InterfaceType = interfaceType;128            Name = name;129            ReturnType = returnType;130            ParameterTypes = parameterTypes;131        }132 133        /// <summary>134        /// Look up.135        /// </summary>136        internal static RemoteHostMethodInfo LookUp(RemoteHostMethodId methodId)137        {138            switch (methodId)139            {140                // Host read-only properties.141 142                case RemoteHostMethodId.GetName:143                    return new RemoteHostMethodInfo(144                        typeof(PSHost),145                        "get_Name",146                        typeof(string),147                        Array.Empty<Type>());148 149                case RemoteHostMethodId.GetVersion:150                    return new RemoteHostMethodInfo(151                        typeof(PSHost),152                        "get_Version",153                        typeof(Version),154                        Array.Empty<Type>());155 156                case RemoteHostMethodId.GetInstanceId:157                    return new RemoteHostMethodInfo(158                        typeof(PSHost),159                        "get_InstanceId",160                        typeof(Guid),161                        Array.Empty<Type>());162 163                case RemoteHostMethodId.GetCurrentCulture:164                    return new RemoteHostMethodInfo(165                        typeof(PSHost),166                        "get_CurrentCulture",167                        typeof(System.Globalization.CultureInfo),168                        Array.Empty<Type>());169 170                case RemoteHostMethodId.GetCurrentUICulture:171                    return new RemoteHostMethodInfo(172                        typeof(PSHost),173                        "get_CurrentUICulture",174                        typeof(System.Globalization.CultureInfo),175                        Array.Empty<Type>());176 177                // Host methods.178 179                case RemoteHostMethodId.SetShouldExit:180                    return new RemoteHostMethodInfo(181                        typeof(PSHost),182                        "SetShouldExit",183                        typeof(void),184                        new Type[] { typeof(int) });185 186                case RemoteHostMethodId.EnterNestedPrompt:187                    return new RemoteHostMethodInfo(188                        typeof(PSHost),189                        "EnterNestedPrompt",190                        typeof(void),191                        Array.Empty<Type>());192 193                case RemoteHostMethodId.ExitNestedPrompt:194                    return new RemoteHostMethodInfo(195                        typeof(PSHost),196                        "ExitNestedPrompt",197                        typeof(void),198                        Array.Empty<Type>());199 200                case RemoteHostMethodId.NotifyBeginApplication:201                    return new RemoteHostMethodInfo(202                        typeof(PSHost),203                        "NotifyBeginApplication",204                        typeof(void),205                        Array.Empty<Type>());206 207                case RemoteHostMethodId.NotifyEndApplication:208                    return new RemoteHostMethodInfo(209                        typeof(PSHost),210                        "NotifyEndApplication",211                        typeof(void),212                        Array.Empty<Type>());213 214                // Host UI methods.215 216                case RemoteHostMethodId.ReadLine:217                    return new RemoteHostMethodInfo(218                        typeof(PSHostUserInterface),219                        "ReadLine",220                        typeof(string),221                        Array.Empty<Type>());222 223                case RemoteHostMethodId.ReadLineAsSecureString:224                    return new RemoteHostMethodInfo(225                        typeof(PSHostUserInterface),226                        "ReadLineAsSecureString",227                        typeof(System.Security.SecureString),228                        Array.Empty<Type>());229 230                case RemoteHostMethodId.Write1:231                    return new RemoteHostMethodInfo(232                        typeof(PSHostUserInterface),233                        "Write",234                        typeof(void),235                        new Type[] { typeof(string) });236 237                case RemoteHostMethodId.Write2:238                    return new RemoteHostMethodInfo(239                        typeof(PSHostUserInterface),240                        "Write",241                        typeof(void),242                        new Type[] { typeof(ConsoleColor), typeof(ConsoleColor), typeof(string) });243 244                case RemoteHostMethodId.WriteLine1:245                    return new RemoteHostMethodInfo(246                        typeof(PSHostUserInterface),247                        "WriteLine",248                        typeof(void),249                        Array.Empty<Type>());250 251                case RemoteHostMethodId.WriteLine2:252                    return new RemoteHostMethodInfo(253                        typeof(PSHostUserInterface),254                        "WriteLine",255                        typeof(void),256                        new Type[] { typeof(string) });257 258                case RemoteHostMethodId.WriteLine3:259                    return new RemoteHostMethodInfo(260                        typeof(PSHostUserInterface),261                        "WriteLine",262                        typeof(void),263                        new Type[] { typeof(ConsoleColor), typeof(ConsoleColor), typeof(string) });264 265                case RemoteHostMethodId.WriteErrorLine:266                    return new RemoteHostMethodInfo(267                        typeof(PSHostUserInterface),268                        "WriteErrorLine",269                        typeof(void),270                        new Type[] { typeof(string) });271 272                case RemoteHostMethodId.WriteDebugLine:273                    return new RemoteHostMethodInfo(274                        typeof(PSHostUserInterface),275                        "WriteDebugLine",276                        typeof(void),277                        new Type[] { typeof(string) });278 279                case RemoteHostMethodId.WriteProgress:280                    return new RemoteHostMethodInfo(281                        typeof(PSHostUserInterface),282                        "WriteProgress",283                        typeof(void),284                        new Type[] { typeof(long), typeof(ProgressRecord) });285 286                case RemoteHostMethodId.WriteVerboseLine:287                    return new RemoteHostMethodInfo(288                        typeof(PSHostUserInterface),289                        "WriteVerboseLine",290                        typeof(void),291                        new Type[] { typeof(string) });292 293                case RemoteHostMethodId.WriteWarningLine:294                    return new RemoteHostMethodInfo(295                        typeof(PSHostUserInterface),296                        "WriteWarningLine",297                        typeof(void),298                        new Type[] { typeof(string) });299 300                case RemoteHostMethodId.Prompt:301                    return new RemoteHostMethodInfo(302                        typeof(PSHostUserInterface),303                        "Prompt",304                        typeof(Dictionary<string, PSObject>),305                        new Type[] { typeof(string), typeof(string), typeof(System.Collections.ObjectModel.Collection<FieldDescription>) });306 307                case RemoteHostMethodId.PromptForCredential1:308                    return new RemoteHostMethodInfo(309                        typeof(PSHostUserInterface),310                        "PromptForCredential",311                        typeof(PSCredential),312                        new Type[] { typeof(string), typeof(string), typeof(string), typeof(string) });313 314                case RemoteHostMethodId.PromptForCredential2:315                    return new RemoteHostMethodInfo(316                        typeof(PSHostUserInterface),317                        "PromptForCredential",318                        typeof(PSCredential),319                        new Type[] { typeof(string), typeof(string), typeof(string), typeof(string), typeof(PSCredentialTypes), typeof(PSCredentialUIOptions) });320 321                case RemoteHostMethodId.PromptForChoice:322                    return new RemoteHostMethodInfo(323                        typeof(PSHostUserInterface),324                        "PromptForChoice",325                        typeof(int),326                        new Type[] { typeof(string), typeof(string), typeof(System.Collections.ObjectModel.Collection<ChoiceDescription>), typeof(int) });327 328                case RemoteHostMethodId.PromptForChoiceMultipleSelection:329                    return new RemoteHostMethodInfo(330                        typeof(IHostUISupportsMultipleChoiceSelection),331                        "PromptForChoice",332                        typeof(Collection<int>),333                        new Type[] { typeof(string), typeof(string), typeof(Collection<ChoiceDescription>), typeof(IEnumerable<int>) });334 335                // Host raw UI read-write properties.336 337                case RemoteHostMethodId.GetForegroundColor:338                    return new RemoteHostMethodInfo(339                        typeof(PSHostRawUserInterface),340                        "get_ForegroundColor",341                        typeof(ConsoleColor),342                        Array.Empty<Type>());343 344                case RemoteHostMethodId.SetForegroundColor:345                    return new RemoteHostMethodInfo(346                        typeof(PSHostRawUserInterface),347                        "set_ForegroundColor",348                        typeof(void),349                        new Type[] { typeof(ConsoleColor) });350 351                case RemoteHostMethodId.GetBackgroundColor:352                    return new RemoteHostMethodInfo(353                        typeof(PSHostRawUserInterface),354                        "get_BackgroundColor",355                        typeof(ConsoleColor),356                        Array.Empty<Type>());357 358                case RemoteHostMethodId.SetBackgroundColor:359                    return new RemoteHostMethodInfo(360                        typeof(PSHostRawUserInterface),361                        "set_BackgroundColor",362                        typeof(void),363                        new Type[] { typeof(ConsoleColor) });364 365                case RemoteHostMethodId.GetCursorPosition:366                    return new RemoteHostMethodInfo(367                        typeof(PSHostRawUserInterface),368                        "get_CursorPosition",369                        typeof(Coordinates),370                        Array.Empty<Type>());371 372                case RemoteHostMethodId.SetCursorPosition:373                    return new RemoteHostMethodInfo(374                        typeof(PSHostRawUserInterface),375                        "set_CursorPosition",376                        typeof(void),377                        new Type[] { typeof(Coordinates) });378 379                case RemoteHostMethodId.GetWindowPosition:380                    return new RemoteHostMethodInfo(381                        typeof(PSHostRawUserInterface),382                        "get_WindowPosition",383                        typeof(Coordinates),384                        Array.Empty<Type>());385 386                case RemoteHostMethodId.SetWindowPosition:387                    return new RemoteHostMethodInfo(388                        typeof(PSHostRawUserInterface),389                        "set_WindowPosition",390                        typeof(void),391                        new Type[] { typeof(Coordinates) });392 393                case RemoteHostMethodId.GetCursorSize:394                    return new RemoteHostMethodInfo(395                        typeof(PSHostRawUserInterface),396                        "get_CursorSize",397                        typeof(int),398                        Array.Empty<Type>());399 400                case RemoteHostMethodId.SetCursorSize:401                    return new RemoteHostMethodInfo(402                        typeof(PSHostRawUserInterface),403                        "set_CursorSize",404                        typeof(void),405                        new Type[] { typeof(int) });406 407                case RemoteHostMethodId.GetBufferSize:408                    return new RemoteHostMethodInfo(409                        typeof(PSHostRawUserInterface),410                        "get_BufferSize",411                        typeof(Size),412                        Array.Empty<Type>());413 414                case RemoteHostMethodId.SetBufferSize:415                    return new RemoteHostMethodInfo(416                        typeof(PSHostRawUserInterface),417                        "set_BufferSize",418                        typeof(void),419                        new Type[] { typeof(Size) });420 421                case RemoteHostMethodId.GetWindowSize:422                    return new RemoteHostMethodInfo(423                        typeof(PSHostRawUserInterface),424                        "get_WindowSize",425                        typeof(Size),426                        Array.Empty<Type>());427 428                case RemoteHostMethodId.SetWindowSize:429                    return new RemoteHostMethodInfo(430                        typeof(PSHostRawUserInterface),431                        "set_WindowSize",432                        typeof(void),433                        new Type[] { typeof(Size) });434 435                case RemoteHostMethodId.GetWindowTitle:436                    return new RemoteHostMethodInfo(437                        typeof(PSHostRawUserInterface),438                        "get_WindowTitle",439                        typeof(string),440                        Array.Empty<Type>());441 442                case RemoteHostMethodId.SetWindowTitle:443                    return new RemoteHostMethodInfo(444                        typeof(PSHostRawUserInterface),445                        "set_WindowTitle",446                        typeof(void),447                        new Type[] { typeof(string) });448 449                // Host raw UI read-only properties.450 451                case RemoteHostMethodId.GetMaxWindowSize:452                    return new RemoteHostMethodInfo(453                        typeof(PSHostRawUserInterface),454                        "get_MaxWindowSize",455                        typeof(Size),456                        Array.Empty<Type>());457 458                case RemoteHostMethodId.GetMaxPhysicalWindowSize:459                    return new RemoteHostMethodInfo(460                        typeof(PSHostRawUserInterface),461                        "get_MaxPhysicalWindowSize",462                        typeof(Size),463                        Array.Empty<Type>());464 465                case RemoteHostMethodId.GetKeyAvailable:466                    return new RemoteHostMethodInfo(467                        typeof(PSHostRawUserInterface),468                        "get_KeyAvailable",469                        typeof(bool),470                        Array.Empty<Type>());471 472                // Host raw UI methods.473 474                case RemoteHostMethodId.ReadKey:475                    return new RemoteHostMethodInfo(476                        typeof(PSHostRawUserInterface),477                        "ReadKey",478                        typeof(KeyInfo),479                        new Type[] { typeof(ReadKeyOptions) });480 481                case RemoteHostMethodId.FlushInputBuffer:482                    return new RemoteHostMethodInfo(483                        typeof(PSHostRawUserInterface),484                        "FlushInputBuffer",485                        typeof(void),486                        Array.Empty<Type>());487 488                case RemoteHostMethodId.SetBufferContents1:489                    return new RemoteHostMethodInfo(490                        typeof(PSHostRawUserInterface),491                        "SetBufferContents",492                        typeof(void),493                        new Type[] { typeof(Rectangle), typeof(BufferCell) });494 495                case RemoteHostMethodId.SetBufferContents2:496                    return new RemoteHostMethodInfo(497                        typeof(PSHostRawUserInterface),498                        "SetBufferContents",499                        typeof(void),500                        new Type[] { typeof(Coordinates), typeof(BufferCell[,]) });501 502                case RemoteHostMethodId.GetBufferContents:503                    return new RemoteHostMethodInfo(504                        typeof(PSHostRawUserInterface),505                        "GetBufferContents",506                        typeof(BufferCell[,]),507                        new Type[] { typeof(Rectangle) });508 509                case RemoteHostMethodId.ScrollBufferContents:510                    return new RemoteHostMethodInfo(511                        typeof(PSHostRawUserInterface),512                        "ScrollBufferContents",513                        typeof(void),514                        new Type[] { typeof(Rectangle), typeof(Coordinates), typeof(Rectangle), typeof(BufferCell) });515 516                // IHostSupportsInteractiveSession methods.517 518                case RemoteHostMethodId.PushRunspace:519                    return new RemoteHostMethodInfo(520                        typeof(IHostSupportsInteractiveSession),521                        "PushRunspace",522                        typeof(void),523                        new Type[] { typeof(System.Management.Automation.Runspaces.Runspace) });524 525                case RemoteHostMethodId.PopRunspace:526                    return new RemoteHostMethodInfo(527                        typeof(IHostSupportsInteractiveSession),528                        "PopRunspace",529                        typeof(void),530                        Array.Empty<Type>());531 532                // IHostSupportsInteractiveSession properties.533 534                case RemoteHostMethodId.GetIsRunspacePushed:535                    return new RemoteHostMethodInfo(536                        typeof(IHostSupportsInteractiveSession),537                        "get_IsRunspacePushed",538                        typeof(bool),539                        Array.Empty<Type>());540 541                case RemoteHostMethodId.GetRunspace:542                    return new RemoteHostMethodInfo(543                        typeof(IHostSupportsInteractiveSession),544                        "get_Runspace",545                        typeof(System.Management.Automation.Runspaces.Runspace),546                        Array.Empty<Type>());547 548                default:549                    Dbg.Assert(false, "All RemoteHostMethodId's should be handled. This code should not be reached.");550                    return null;551            }552        }553    }554}555