summaryrefslogtreecommitdiff
path: root/modules/mono/editor/GodotTools/GodotTools.OpenVisualStudio/Program.cs
blob: cc0deb6cc0a5c16b5e4439cce14e1ec074590ecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text.RegularExpressions;
using EnvDTE;

namespace GodotTools.OpenVisualStudio
{
    internal static class Program
    {
        [DllImport("ole32.dll")]
        private static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable pprot);

        [DllImport("ole32.dll")]
        private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        private static void ShowHelp()
        {
            Console.WriteLine("Opens the file(s) in a Visual Studio instance that is editing the specified solution.");
            Console.WriteLine("If an existing instance for the solution is not found, a new one is created.");
            Console.WriteLine();
            Console.WriteLine("Usage:");
            Console.WriteLine(@"  GodotTools.OpenVisualStudio.exe solution [file[;line[;col]]...]");
            Console.WriteLine();
            Console.WriteLine("Lines and columns begin at one. Zero or lower will result in an error.");
            Console.WriteLine("If a line is specified but a column is not, the line is selected in the text editor.");
        }

        // STAThread needed, otherwise CoRegisterMessageFilter may return CO_E_NOT_SUPPORTED.
        [STAThread]
        private static int Main(string[] args)
        {
            if (args.Length == 0 || args[0] == "--help" || args[0] == "-h")
            {
                ShowHelp();
                return 0;
            }

            string solutionFile = NormalizePath(args[0]);

            var dte = FindInstanceEditingSolution(solutionFile);

            if (dte == null)
            {
                // Open a new instance
                dte = TryVisualStudioLaunch("VisualStudio.DTE.17.0");

                if (dte == null)
                {
                    // Launch of VS 2022 failed, fallback to 2019
                    dte = TryVisualStudioLaunch("VisualStudio.DTE.16.0");
                }

                dte.UserControl = true;

                try
                {
                    dte.Solution.Open(solutionFile);
                }
                catch (ArgumentException)
                {
                    Console.Error.WriteLine("Solution.Open: Invalid path or file not found");
                    return 1;
                }

                dte.MainWindow.Visible = true;
            }

            MessageFilter.Register();

            try
            {
                // Open files

                for (int i = 1; i < args.Length; i++)
                {
                    // Both the line number and the column begin at one

                    string[] fileArgumentParts = args[i].Split(';');

                    string filePath = NormalizePath(fileArgumentParts[0]);

                    try
                    {
                        dte.ItemOperations.OpenFile(filePath);
                    }
                    catch (ArgumentException)
                    {
                        Console.Error.WriteLine("ItemOperations.OpenFile: Invalid path or file not found");
                        return 1;
                    }

                    if (fileArgumentParts.Length > 1)
                    {
                        if (int.TryParse(fileArgumentParts[1], out int line))
                        {
                            var textSelection = (TextSelection)dte.ActiveDocument.Selection;

                            if (fileArgumentParts.Length > 2)
                            {
                                if (int.TryParse(fileArgumentParts[2], out int column))
                                {
                                    textSelection.MoveToLineAndOffset(line, column);
                                }
                                else
                                {
                                    Console.Error.WriteLine("The column part of the argument must be a valid integer");
                                    return 1;
                                }
                            }
                            else
                            {
                                textSelection.GotoLine(line, Select: true);
                            }
                        }
                        else
                        {
                            Console.Error.WriteLine("The line part of the argument must be a valid integer");
                            return 1;
                        }
                    }
                }
            }
            finally
            {
                var mainWindow = dte.MainWindow;
                mainWindow.Activate();
                SetForegroundWindow(new IntPtr(mainWindow.HWnd));

                MessageFilter.Revoke();
            }

            return 0;
        }

        private static DTE TryVisualStudioLaunch(string version)
        {
            try
            {
                var visualStudioDteType = Type.GetTypeFromProgID(version, throwOnError: true);
                var dte = (DTE)Activator.CreateInstance(visualStudioDteType);

                return dte;
            }
            catch (COMException)
            {
                return null;
            }
        }

        private static DTE FindInstanceEditingSolution(string solutionPath)
        {
            if (GetRunningObjectTable(0, out IRunningObjectTable pprot) != 0)
                return null;

            try
            {
                pprot.EnumRunning(out IEnumMoniker ppenumMoniker);
                ppenumMoniker.Reset();

                var moniker = new IMoniker[1];

                while (ppenumMoniker.Next(1, moniker, IntPtr.Zero) == 0)
                {
                    string ppszDisplayName;

                    CreateBindCtx(0, out IBindCtx ppbc);

                    try
                    {
                        moniker[0].GetDisplayName(ppbc, null, out ppszDisplayName);
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(ppbc);
                    }

                    if (ppszDisplayName == null)
                        continue;

                    // The digits after the colon are the process ID
                    if (!Regex.IsMatch(ppszDisplayName, "!VisualStudio.DTE.1[6-7].0:[0-9]"))
                        continue;

                    if (pprot.GetObject(moniker[0], out object ppunkObject) == 0)
                    {
                        if (ppunkObject is DTE dte && dte.Solution.FullName.Length > 0)
                        {
                            if (NormalizePath(dte.Solution.FullName) == solutionPath)
                                return dte;
                        }
                    }
                }
            }
            finally
            {
                Marshal.ReleaseComObject(pprot);
            }

            return null;
        }

        static string NormalizePath(string path)
        {
            return new Uri(Path.GetFullPath(path)).LocalPath
                .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
                .ToUpperInvariant();
        }

        #region MessageFilter. See: http: //msdn.microsoft.com/en-us/library/ms228772.aspx

        private class MessageFilter : IOleMessageFilter
        {
            // Class containing the IOleMessageFilter
            // thread error-handling functions

            private static IOleMessageFilter _oldFilter;

            // Start the filter
            public static void Register()
            {
                IOleMessageFilter newFilter = new MessageFilter();
                int ret = CoRegisterMessageFilter(newFilter, out _oldFilter);
                if (ret != 0)
                    Console.Error.WriteLine($"CoRegisterMessageFilter failed with error code: {ret}");
            }

            // Done with the filter, close it
            public static void Revoke()
            {
                int ret = CoRegisterMessageFilter(_oldFilter, out _);
                if (ret != 0)
                    Console.Error.WriteLine($"CoRegisterMessageFilter failed with error code: {ret}");
            }

            //
            // IOleMessageFilter functions
            // Handle incoming thread requests
            int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
            {
                // Return the flag SERVERCALL_ISHANDLED
                return 0;
            }

            // Thread call was rejected, so try again.
            int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
            {
                // flag = SERVERCALL_RETRYLATER
                if (dwRejectType == 2)
                {
                    // Retry the thread call immediately if return >= 0 & < 100
                    return 99;
                }

                // Too busy; cancel call
                return -1;
            }

            int IOleMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
            {
                // Return the flag PENDINGMSG_WAITDEFPROCESS
                return 2;
            }

            // Implement the IOleMessageFilter interface
            [DllImport("ole32.dll")]
            private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
        }

        [ComImport(), Guid("00000016-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        private interface IOleMessageFilter
        {
            [PreserveSig]
            int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);

            [PreserveSig]
            int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);

            [PreserveSig]
            int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
        }

        #endregion
    }
}