|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using Sentry.Extensibility; |
| 5 | +using UnityEditor; |
| 6 | +using UnityEngine; |
| 7 | +using Debug = UnityEngine.Debug; |
| 8 | + |
| 9 | +namespace Sentry.Unity.Editor.Native |
| 10 | +{ |
| 11 | + internal static class SentryWindowsPlayer |
| 12 | + { |
| 13 | + internal static readonly ProcessStartInfo StartInfo = new() |
| 14 | + { |
| 15 | + UseShellExecute = false, |
| 16 | + RedirectStandardOutput = true, |
| 17 | + RedirectStandardError = true, |
| 18 | + CreateNoWindow = true |
| 19 | + }; |
| 20 | + |
| 21 | + internal static void AddNativeOptions(SentryUnityOptions options) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + internal static void AddSentryToMain(SentryUnityOptions options) |
| 26 | + { |
| 27 | + } |
| 28 | + |
| 29 | + internal static string LocateWindowsPlayerSource(IEditorApplication? editorApplication = null) |
| 30 | + { |
| 31 | + editorApplication ??= EditorApplicationAdapter.Instance; |
| 32 | + |
| 33 | + var playerProjectPath = Path.Combine(editorApplication.ApplicationContentsPath, "PlaybackEngines", "windowsstandalonesupport", "source", "windowsplayer"); |
| 34 | + if (!Directory.Exists(playerProjectPath)) |
| 35 | + { |
| 36 | + throw new DirectoryNotFoundException($"Failed to locate the WindowsPlayer source at {playerProjectPath}."); |
| 37 | + } |
| 38 | + |
| 39 | + return playerProjectPath; |
| 40 | + } |
| 41 | + |
| 42 | + internal static void CreateWindowsPlayerProject(string windowsPlayerSource, string windowsPlayerTarget, IDiagnosticLogger? logger) |
| 43 | + { |
| 44 | + EditorFileIO.CopyDirectory(windowsPlayerSource, windowsPlayerTarget, logger); |
| 45 | + |
| 46 | + // TODO: Does the .props file have to look like that? |
| 47 | + using var props = File.CreateText(Path.Combine(windowsPlayerTarget, "UnityCommon.props")); |
| 48 | + props.Write(@"<?xml version=""1.0"" encoding=""utf-8""?> |
| 49 | +<Project xmlns=""http://schemas.microsoft.com/developer/msbuild/2003""> |
| 50 | +</Project>"); |
| 51 | + } |
| 52 | + |
| 53 | + internal static string LocateMSBuild(string vsWherePath, IDiagnosticLogger? logger) |
| 54 | + { |
| 55 | + StartInfo.FileName = vsWherePath; |
| 56 | + StartInfo.Arguments = "-latest -requires Microsoft.Component.MSBuild -find MSBuild\\**\\Bin\\MSBuild.exe"; |
| 57 | + |
| 58 | + var vsWhereOutput = ""; |
| 59 | + var process = new Process {StartInfo = StartInfo}; |
| 60 | + process.OutputDataReceived += (sender, args) => vsWhereOutput += args.Data; |
| 61 | + process.Start(); |
| 62 | + process.BeginOutputReadLine(); |
| 63 | + process.BeginErrorReadLine(); |
| 64 | + process.WaitForExit(); |
| 65 | + |
| 66 | + logger?.LogDebug("VSWhere returned with: {0}", vsWhereOutput); |
| 67 | + |
| 68 | + if (!File.Exists(vsWhereOutput)) |
| 69 | + { |
| 70 | + throw new FileNotFoundException($"Failed to locate 'msbuild'. VSWhere returned {vsWhereOutput}"); |
| 71 | + } |
| 72 | + |
| 73 | + return vsWhereOutput; |
| 74 | + } |
| 75 | + |
| 76 | + internal static string LocateVSWhere(IDiagnosticLogger? logger) |
| 77 | + { |
| 78 | + var projectPath = Path.GetDirectoryName(Application.dataPath); |
| 79 | + var directories = Directory.GetDirectories(Path.Combine(projectPath, "Library", "PackageCache"), "com.unity.ide.visualstudio@*"); |
| 80 | + if (directories is null || directories.Length < 1) |
| 81 | + { |
| 82 | + throw new Exception("Failed lo locate the 'com.unity.ide.visualstudio' package."); |
| 83 | + } |
| 84 | + |
| 85 | + // TODO: Not sure if there can be more than one version of a package in the Library/PackageCache and if it even matters |
| 86 | + var vsPackagePath = directories[0]; |
| 87 | + logger?.LogDebug("Located 'com.unity.ide.visualstudio' package at {0}", vsPackagePath); |
| 88 | + |
| 89 | + var vsWherePath = Path.Combine(vsPackagePath, "Editor", "VSWhere", "vswhere.exe"); |
| 90 | + if (!File.Exists(vsWherePath)) |
| 91 | + { |
| 92 | + throw new FileNotFoundException($"Failed to find 'vswhere.exe' at '{vsWherePath}'"); |
| 93 | + } |
| 94 | + |
| 95 | + return vsWherePath; |
| 96 | + } |
| 97 | + |
| 98 | + public static void Build(SentryUnityOptions options, string executablePath) |
| 99 | + { |
| 100 | + var vsWherePath = LocateVSWhere(options.DiagnosticLogger); |
| 101 | + var msBuildPath = LocateMSBuild(vsWherePath, options.DiagnosticLogger); |
| 102 | + |
| 103 | + var playerSource = LocateWindowsPlayerSource(); |
| 104 | + var playerTarget = FileUtil.GetUniqueTempPathInProject(); |
| 105 | + |
| 106 | + CreateWindowsPlayerProject(playerSource, playerTarget, options.DiagnosticLogger); |
| 107 | + |
| 108 | + AddNativeOptions(options); |
| 109 | + AddSentryToMain(options); |
| 110 | + |
| 111 | + StartInfo.FileName = msBuildPath; |
| 112 | + StartInfo.Arguments = playerTarget; |
| 113 | + |
| 114 | + var outputData = ""; |
| 115 | + var errorData = ""; |
| 116 | + var process = new Process {StartInfo = StartInfo}; |
| 117 | + process.OutputDataReceived += (sender, args) => outputData += args.Data; |
| 118 | + process.ErrorDataReceived += (sender, args) => errorData += args.Data; |
| 119 | + process.Start(); |
| 120 | + process.BeginOutputReadLine(); |
| 121 | + process.BeginErrorReadLine(); |
| 122 | + process.WaitForExit(); |
| 123 | + |
| 124 | + if (outputData.Contains("Build succeeded")) |
| 125 | + { |
| 126 | + options.DiagnosticLogger?.LogDebug("Succeeded building the PlaybackEngine"); |
| 127 | + } |
| 128 | + else if (outputData.Contains("Build failed")) |
| 129 | + { |
| 130 | + throw new Exception($"Failed to build the PlaybackEngine: \n {outputData}"); |
| 131 | + } |
| 132 | + |
| 133 | + var logFile = Path.Combine(playerTarget, "build.log"); |
| 134 | + File.WriteAllText(logFile, outputData); |
| 135 | + File.AppendAllText(logFile, errorData); |
| 136 | + |
| 137 | + // TODO: Overwrite the executable with the build output |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments