三国卡牌客户端基础资源仓库
yyl
2 天以前 cec146fc3fe287928e075c79ece20a20a9b16b20
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
 
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
using SimpleJSON;
using IOPath = System.IO.Path;
 
namespace Microsoft.Unity.VisualStudio.Editor {
    internal class VisualStudioTraeInstallation : VisualStudioInstallation {
        private static readonly IGenerator _generator = new SdkStyleProjectGeneration();
 
        public override bool SupportsAnalyzers {
            get {
                return true;
            }
        }
 
        public override Version LatestLanguageVersionSupported {
            get {
                return new Version(11, 0);
            }
        }
 
        private string GetExtensionPath() {
            var vscode = IsPrerelease ? ".vscode-insiders" : ".vscode";
            var extensionsPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), vscode, "extensions");
            if (!Directory.Exists(extensionsPath))
                return null;
 
            return Directory
                .EnumerateDirectories(extensionsPath, $"{MicrosoftUnityExtensionId}*") // publisherid.extensionid
                .OrderByDescending(n => n)
                .FirstOrDefault();
        }
 
        public override string[] GetAnalyzers() {
            var vstuPath = GetExtensionPath();
            if (string.IsNullOrEmpty(vstuPath))
                return Array.Empty<string>();
 
            return GetAnalyzers(vstuPath);
        }
 
        public override IGenerator ProjectGenerator {
            get {
                return _generator;
            }
        }
 
        private static bool IsCandidateForDiscovery(string path) {
#if UNITY_EDITOR_OSX
            return Directory.Exists(path) && Regex.IsMatch(path, ".*Trae.*.app$", RegexOptions.IgnoreCase);
#elif UNITY_EDITOR_WIN
            return File.Exists(path) && Regex.IsMatch(path, ".*Trae.*.exe$", RegexOptions.IgnoreCase);
#else
            return File.Exists(path) && path.EndsWith("trae", StringComparison.OrdinalIgnoreCase);
#endif
        }
 
        [Serializable]
        internal class VisualStudioCodeManifest {
            public string name;
            public string version;
        }
 
        public static bool TryDiscoverInstallation(string editorPath, out IVisualStudioInstallation installation) {
            installation = null;
 
            if (string.IsNullOrEmpty(editorPath))
                return false;
 
            if (!IsCandidateForDiscovery(editorPath))
                return false;
 
            Version version = null;
            var isPrerelease = false;
 
            try {
                var manifestBase = GetRealPath(editorPath);
 
#if UNITY_EDITOR_WIN
                // on Windows, editorPath is a file, resources as subdirectory
                manifestBase = IOPath.GetDirectoryName(manifestBase);
#elif UNITY_EDITOR_OSX
                // on Mac, editorPath is a directory
                manifestBase = IOPath.Combine(manifestBase, "Contents");
#else
                // on Linux, editorPath is a file, in a bin sub-directory
                var parent = Directory.GetParent(manifestBase);
                // but we can link to [vscode]/code or [vscode]/bin/code
                manifestBase = parent?.Name == "bin" ? parent.Parent?.FullName : parent?.FullName;
#endif
 
                if (manifestBase == null)
                    return false;
 
                var manifestFullPath = IOPath.Combine(manifestBase, "resources", "app", "package.json");
                if (File.Exists(manifestFullPath)) {
                    var manifest = JsonUtility.FromJson<VisualStudioCodeManifest>(File.ReadAllText(manifestFullPath));
                    Version.TryParse(manifest.version.Split('-').First(), out version);
                    isPrerelease = manifest.version.ToLower().Contains("insider");
                }
            } catch (Exception) {
                // do not fail if we are not able to retrieve the exact version number
            }
 
            isPrerelease = isPrerelease || editorPath.ToLower().Contains("insider");
            installation = new VisualStudioTraeInstallation() {
                IsPrerelease = isPrerelease,
                Name = "Trae" + (isPrerelease ? " - Insider" : string.Empty) + (version != null ? $" [{version.ToString(3)}]" : string.Empty),
                Path = editorPath,
                Version = version ?? new Version()
            };
 
            return true;
        }
 
        public static IEnumerable<IVisualStudioInstallation> GetVisualStudioInstallations() {
            var candidates = new List<string>();
 
#if UNITY_EDITOR_WIN
            var localAppPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs");
            var programFiles = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
 
            foreach (var basePath in new[] { localAppPath, programFiles }) {
                candidates.Add(IOPath.Combine(basePath, "trae", "trae.exe"));
            }
#elif UNITY_EDITOR_OSX
            var appPath = IOPath.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
            candidates.AddRange(Directory.EnumerateDirectories(appPath, "Trae*.app"));
#elif UNITY_EDITOR_LINUX
            // Well known locations
            candidates.Add("/usr/bin/trae");
            candidates.Add("/bin/trae");
            candidates.Add("/usr/local/bin/trae");
 
            // Preference ordered base directories relative to which desktop files should be searched
            candidates.AddRange(GetXdgCandidates());
#endif
 
            foreach (var candidate in candidates.Distinct()) {
                if (TryDiscoverInstallation(candidate, out var installation))
                    yield return installation;
            }
        }
 
#if UNITY_EDITOR_LINUX
        private static readonly Regex DesktopFileExecEntry = new Regex(@"Exec=(\S+)", RegexOptions.Singleline | RegexOptions.Compiled);
 
        private static IEnumerable<string> GetXdgCandidates()
        {
            var envdirs = Environment.GetEnvironmentVariable("XDG_DATA_DIRS");
            if (string.IsNullOrEmpty(envdirs))
                yield break;
 
            var dirs = envdirs.Split(':');
            foreach(var dir in dirs)
            {
                Match match = null;
 
                try
                {
                    var desktopFile = IOPath.Combine(dir, "applications/code.desktop");
                    if (!File.Exists(desktopFile))
                        continue;
                
                    var content = File.ReadAllText(desktopFile);
                    match = DesktopFileExecEntry.Match(content);
                }
                catch
                {
                    // do not fail if we cannot read desktop file
                }
 
                if (match == null || !match.Success)
                    continue;
 
                yield return match.Groups[1].Value;
                break;
            }
        }
 
        [System.Runtime.InteropServices.DllImport ("libc")]
        private static extern int readlink(string path, byte[] buffer, int buflen);
 
        internal static string GetRealPath(string path)
        {
            byte[] buf = new byte[512];
            int ret = readlink(path, buf, buf.Length);
            if (ret == -1) return path;
            char[] cbuf = new char[512];
            int chars = System.Text.Encoding.Default.GetChars(buf, 0, ret, cbuf, 0);
            return new String(cbuf, 0, chars);
        }
#else
        internal static string GetRealPath(string path) {
            return path;
        }
#endif
 
        public override void CreateExtraFiles(string projectDirectory) {
            try {
                var vscodeDirectory = IOPath.Combine(projectDirectory.NormalizePathSeparators(), ".vscode");
                Directory.CreateDirectory(vscodeDirectory);
 
                var enablePatch = !File.Exists(IOPath.Combine(vscodeDirectory, ".vstupatchdisable"));
 
                CreateRecommendedExtensionsFile(vscodeDirectory, enablePatch);
                CreateSettingsFile(vscodeDirectory, enablePatch);
                CreateLaunchFile(vscodeDirectory, enablePatch);
            } catch (IOException) {
            }
        }
 
        private const string DefaultLaunchFileContent = @"{
    ""version"": ""0.2.0"",
    ""configurations"": [
        {
            ""name"": ""Attach to Unity"",
            ""type"": ""vstuc"",
            ""request"": ""attach""
        }
     ]
}";
 
        private static void CreateLaunchFile(string vscodeDirectory, bool enablePatch) {
            var launchFile = IOPath.Combine(vscodeDirectory, "launch.json");
            if (File.Exists(launchFile)) {
                if (enablePatch)
                    PatchLaunchFile(launchFile);
 
                return;
            }
 
            File.WriteAllText(launchFile, DefaultLaunchFileContent);
        }
 
        private static void PatchLaunchFile(string launchFile) {
            try {
                const string configurationsKey = "configurations";
                const string typeKey = "type";
 
                var content = File.ReadAllText(launchFile);
                var launch = JSONNode.Parse(content);
 
                var configurations = launch[configurationsKey] as JSONArray;
                if (configurations == null) {
                    configurations = new JSONArray();
                    launch.Add(configurationsKey, configurations);
                }
 
                if (configurations.Linq.Any(entry => entry.Value[typeKey].Value == "vstuc"))
                    return;
 
                var defaultContent = JSONNode.Parse(DefaultLaunchFileContent);
                configurations.Add(defaultContent[configurationsKey][0]);
 
                WriteAllTextFromJObject(launchFile, launch);
            } catch (Exception) {
                // do not fail if we cannot patch the launch.json file
            }
        }
 
        private void CreateSettingsFile(string vscodeDirectory, bool enablePatch) {
            var settingsFile = IOPath.Combine(vscodeDirectory, "settings.json");
            if (File.Exists(settingsFile)) {
                if (enablePatch)
                    PatchSettingsFile(settingsFile);
 
                return;
            }
 
            const string excludes = @"    ""files.exclude"": {
        ""**/.DS_Store"": true,
        ""**/.git"": true,
        ""**/.vs"": true,
        ""**/.gitmodules"": true,
        ""**/.vsconfig"": true,
        ""**/*.booproj"": true,
        ""**/*.pidb"": true,
        ""**/*.suo"": true,
        ""**/*.user"": true,
        ""**/*.userprefs"": true,
        ""**/*.unityproj"": true,
        ""**/*.dll"": true,
        ""**/*.exe"": true,
        ""**/*.pdf"": true,
        ""**/*.mid"": true,
        ""**/*.midi"": true,
        ""**/*.wav"": true,
        ""**/*.gif"": true,
        ""**/*.ico"": true,
        ""**/*.jpg"": true,
        ""**/*.jpeg"": true,
        ""**/*.png"": true,
        ""**/*.psd"": true,
        ""**/*.tga"": true,
        ""**/*.tif"": true,
        ""**/*.tiff"": true,
        ""**/*.3ds"": true,
        ""**/*.3DS"": true,
        ""**/*.fbx"": true,
        ""**/*.FBX"": true,
        ""**/*.lxo"": true,
        ""**/*.LXO"": true,
        ""**/*.ma"": true,
        ""**/*.MA"": true,
        ""**/*.obj"": true,
        ""**/*.OBJ"": true,
        ""**/*.asset"": true,
        ""**/*.cubemap"": true,
        ""**/*.flare"": true,
        ""**/*.mat"": true,
        ""**/*.meta"": true,
        ""**/*.prefab"": true,
        ""**/*.unity"": true,
        ""build/"": true,
        ""Build/"": true,
        ""Library/"": true,
        ""library/"": true,
        ""obj/"": true,
        ""Obj/"": true,
        ""Logs/"": true,
        ""logs/"": true,
        ""ProjectSettings/"": true,
        ""UserSettings/"": true,
        ""temp/"": true,
        ""Temp/"": true
    }";
 
            var content = @"{
" + excludes + @",
    ""dotnet.defaultSolution"": """ + IOPath.GetFileName(ProjectGenerator.SolutionFile()) + @"""
}";
 
            File.WriteAllText(settingsFile, content);
        }
 
        private void PatchSettingsFile(string settingsFile) {
            try {
                const string excludesKey = "files.exclude";
                const string solutionKey = "dotnet.defaultSolution";
 
                var content = File.ReadAllText(settingsFile);
                var settings = JSONNode.Parse(content);
 
                var excludes = settings[excludesKey] as JSONObject;
                if (excludes == null)
                    return;
 
                var patchList = new List<string>();
                var patched = false;
 
                // Remove files.exclude for solution+project files in the project root
                foreach (var exclude in excludes) {
                    if (!bool.TryParse(exclude.Value, out var exc) || !exc)
                        continue;
 
                    var key = exclude.Key;
 
                    if (!key.EndsWith(".sln") && !key.EndsWith(".csproj"))
                        continue;
 
                    if (!Regex.IsMatch(key, "^(\\*\\*[\\\\\\/])?\\*\\.(sln|csproj)$"))
                        continue;
 
                    patchList.Add(key);
                    patched = true;
                }
 
                // Check default solution
                var defaultSolution = settings[solutionKey];
                var solutionFile = IOPath.GetFileName(ProjectGenerator.SolutionFile());
                if (defaultSolution == null || defaultSolution.Value != solutionFile) {
                    settings[solutionKey] = solutionFile;
                    patched = true;
                }
 
                if (!patched)
                    return;
 
                foreach (var patch in patchList)
                    excludes.Remove(patch);
 
                WriteAllTextFromJObject(settingsFile, settings);
            } catch (Exception) {
                // do not fail if we cannot patch the settings.json file
            }
        }
 
        private const string MicrosoftUnityExtensionId = "visualstudiotoolsforunity.vstuc";
        private const string DefaultRecommendedExtensionsContent = @"{
    ""recommendations"": [
      """ + MicrosoftUnityExtensionId + @"""
    ]
}
";
 
        private static void CreateRecommendedExtensionsFile(string vscodeDirectory, bool enablePatch) {
            // see https://tattoocoder.com/recommending-vscode-extensions-within-your-open-source-projects/
            var extensionFile = IOPath.Combine(vscodeDirectory, "extensions.json");
            if (File.Exists(extensionFile)) {
                if (enablePatch)
                    PatchRecommendedExtensionsFile(extensionFile);
 
                return;
            }
 
            File.WriteAllText(extensionFile, DefaultRecommendedExtensionsContent);
        }
 
        private static void PatchRecommendedExtensionsFile(string extensionFile) {
            try {
                const string recommendationsKey = "recommendations";
 
                var content = File.ReadAllText(extensionFile);
                var extensions = JSONNode.Parse(content);
 
                var recommendations = extensions[recommendationsKey] as JSONArray;
                if (recommendations == null) {
                    recommendations = new JSONArray();
                    extensions.Add(recommendationsKey, recommendations);
                }
 
                if (recommendations.Linq.Any(entry => entry.Value.Value == MicrosoftUnityExtensionId))
                    return;
 
                recommendations.Add(MicrosoftUnityExtensionId);
                WriteAllTextFromJObject(extensionFile, extensions);
            } catch (Exception) {
                // do not fail if we cannot patch the extensions.json file
            }
        }
 
        private static void WriteAllTextFromJObject(string file, JSONNode node) {
            using (var fs = File.Open(file, FileMode.Create))
            using (var sw = new StreamWriter(fs)) {
                // Keep formatting/indent in sync with default contents
                sw.Write(node.ToString(aIndent: 4));
            }
        }
 
        public override bool Open(string path, int line, int column, string solution) {
            line = Math.Max(1, line);
            column = Math.Max(0, column);
 
            var directory = IOPath.GetDirectoryName(solution);
            var application = Path;
 
            ProcessRunner.Start(string.IsNullOrEmpty(path) ?
                ProcessStartInfoFor(application, $"\"{directory}\"") :
                ProcessStartInfoFor(application, $"\"{directory}\" -g \"{path}\":{line}:{column}"));
 
            return true;
        }
 
        private static ProcessStartInfo ProcessStartInfoFor(string application, string arguments) {
#if UNITY_EDITOR_OSX
            // wrap with built-in OSX open feature
            arguments = $"-n \"{application}\" --args {arguments}";
            application = "open";
            return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect:false, shell: true);
#else
            return ProcessRunner.ProcessStartInfoFor(application, arguments, redirect: false);
#endif
        }
 
        public static void Initialize() {
        }
    }
}