From 9b129cd6485c5b97bf4b7ce11182a2e5798dae6f Mon Sep 17 00:00:00 2001 From: Miepee <38186597+Miepee@users.noreply.github.com> Date: Wed, 4 May 2022 14:14:52 +0200 Subject: [PATCH 1/3] show git commit+branch for debug builds --- UndertaleModLib/Util/GitVersion.cs | 76 +++++++++++++++++++++++++++++ UndertaleModTool/MainWindow.xaml.cs | 22 +++++++-- 2 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 UndertaleModLib/Util/GitVersion.cs diff --git a/UndertaleModLib/Util/GitVersion.cs b/UndertaleModLib/Util/GitVersion.cs new file mode 100644 index 000000000..87ea0849b --- /dev/null +++ b/UndertaleModLib/Util/GitVersion.cs @@ -0,0 +1,76 @@ +using System.Diagnostics; +using System.IO; + +namespace UndertaleModLib.Util; + +/// +/// Includes miscellaneous git information about the project to compile. +/// Only intended for Debug use! +/// +public static class GitVersion +{ + /// + /// The constant for the git executable. + /// + private const string Git = "git"; + + /// + /// The constant to receive commit name. + /// + private const string GitCommit = "describe --always --dirty"; + + /// + /// The constant to receive branch name. + /// + private const string GitBranch = "rev-parse --abbrev-ref HEAD"; + + /// + /// Gets and returns the git commit and branch name. + /// + /// The git commit and branch name. + public static string GetGitVersion() + { + string commitOutput; + string branchOutput; + + try + { + // Start git, get the commit number and assign that to commitOutput + using (Process process = new Process()) + { + process.StartInfo.FileName = Git; + process.StartInfo.Arguments = GitCommit; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.CreateNoWindow = true; + process.Start(); + StreamReader reader = process.StandardOutput; + string output = reader.ReadToEnd(); + commitOutput = output.Trim(); + process.WaitForExit(); + } + + // Start git, get the branch name and assign that to branchOutput + using (Process process = new Process()) + { + process.StartInfo.FileName = Git; + process.StartInfo.Arguments = GitBranch; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.CreateNoWindow = true; + process.Start(); + StreamReader reader = process.StandardOutput; + string output = reader.ReadToEnd(); + branchOutput = output.Trim(); + process.WaitForExit(); + } + } + // If git can't be found, assign default values + catch + { + commitOutput = branchOutput = "unavailable"; + } + + // return combined commit + branch + string finalGitVersion = $"{commitOutput} ({branchOutput})"; + return finalGitVersion; + } +} \ No newline at end of file diff --git a/UndertaleModTool/MainWindow.xaml.cs b/UndertaleModTool/MainWindow.xaml.cs index efea26b21..72c5a870d 100644 --- a/UndertaleModTool/MainWindow.xaml.cs +++ b/UndertaleModTool/MainWindow.xaml.cs @@ -27,6 +27,7 @@ using UndertaleModLib.Models; using UndertaleModLib.ModelsDebug; using UndertaleModLib.Scripting; +using UndertaleModLib.Util; using UndertaleModTool.Windows; using System.IO.Pipes; using Ookii.Dialogs.Wpf; @@ -93,7 +94,7 @@ public static string GetTitleForObject(object obj) else if (obj is UndertaleNamedResource namedRes) { string content = namedRes.Name?.Content; - + string header = obj switch { UndertaleAudioGroup => "Audio Group", @@ -301,7 +302,13 @@ public bool RoomRendererEnabled // Version info public static string Edition = ""; + + // On debug, build with git versions. Otherwise, use the provided release version. +#if DEBUG + public static string Version = GitVersion.GetGitVersion(); +#else public static string Version = Assembly.GetExecutingAssembly().GetName().Version.ToString() + (Edition != "" ? "-" + Edition : ""); +#endif public MainWindow() { @@ -313,7 +320,7 @@ public MainWindow() SelectionHistory.Clear(); ClosedTabsHistory.Clear(); - TitleMain = "UndertaleModTool by krzys_h v" + Version; + TitleMain = "UndertaleModTool by krzys_h v:" + Version; CanSave = false; CanSafelySave = false; @@ -1732,7 +1739,7 @@ private void MenuItem_Delete_Click(object sender, RoutedEventArgs e) if (Highlighted is UndertaleObject obj) DeleteItem(obj); } - + private void MenuItem_Add_Click(object sender, RoutedEventArgs e) { object source = null; @@ -2517,7 +2524,12 @@ public async void UpdateApp(SettingsWindow window) httpClient = new(); httpClient.DefaultRequestHeaders.Accept.Clear(); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json")); - httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("UndertaleModTool", Version)); + + // remove the invalid characters (everything within square brackets) from the version string. + // Probably needs to be expanded later, these are just the ones I know of. + Regex invalidChars = new Regex(@"[ ()]"); + string version = invalidChars.Replace(Version, ""); + httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("UndertaleModTool", version)); double bytesToMB = 1024 * 1024; @@ -3231,7 +3243,7 @@ public void CloseTab(int tabIndex, bool addDefaultTab = true) Tabs[i].TabIndex = i; // if closing the currently open tab - if (currIndex == tabIndex) + if (currIndex == tabIndex) { // and if that tab is not the last if (Tabs.Count > 1 && tabIndex < Tabs.Count - 1) From 2f798ac27a2c84f449dd3e59f94bb98835fd6b30 Mon Sep 17 00:00:00 2001 From: Miepee <38186597+Miepee@users.noreply.github.com> Date: Wed, 4 May 2022 18:57:47 +0200 Subject: [PATCH 2/3] Determine git info at build time instead of runtime --- .gitignore | 1 + UndertaleModLib/UndertaleModLib.csproj | 13 +++++++ UndertaleModLib/Util/GitVersion.cs | 49 ++++++++++---------------- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/.gitignore b/.gitignore index e739f1a73..9ca9cc147 100644 --- a/.gitignore +++ b/.gitignore @@ -332,3 +332,4 @@ ASALocalRun/ Test/Test - Backup.csproj UndertaleModLib/UndertaleModLib - Backup.csproj UndertaleModTests/UndertaleModTests - Backup.csproj +UndertaleModLib/gitversion.txt diff --git a/UndertaleModLib/UndertaleModLib.csproj b/UndertaleModLib/UndertaleModLib.csproj index 0c2039dce..14dd01c50 100644 --- a/UndertaleModLib/UndertaleModLib.csproj +++ b/UndertaleModLib/UndertaleModLib.csproj @@ -35,4 +35,17 @@ + + + + + + + PreserveNewest + + + + + + \ No newline at end of file diff --git a/UndertaleModLib/Util/GitVersion.cs b/UndertaleModLib/Util/GitVersion.cs index 87ea0849b..c82891672 100644 --- a/UndertaleModLib/Util/GitVersion.cs +++ b/UndertaleModLib/Util/GitVersion.cs @@ -1,5 +1,8 @@ +using System; using System.Diagnostics; using System.IO; +using System.Linq; +using System.Reflection; namespace UndertaleModLib.Util; @@ -30,47 +33,33 @@ public static class GitVersion /// The git commit and branch name. public static string GetGitVersion() { - string commitOutput; - string branchOutput; + string gitOutput = ""; + // try to access the embedded resource try { - // Start git, get the commit number and assign that to commitOutput - using (Process process = new Process()) - { - process.StartInfo.FileName = Git; - process.StartInfo.Arguments = GitCommit; - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.CreateNoWindow = true; - process.Start(); - StreamReader reader = process.StandardOutput; - string output = reader.ReadToEnd(); - commitOutput = output.Trim(); - process.WaitForExit(); - } + var assembly = Assembly.GetExecutingAssembly(); + var resourceName = "UndertaleModLib.gitversion.txt"; - // Start git, get the branch name and assign that to branchOutput - using (Process process = new Process()) + using (Stream stream = assembly.GetManifestResourceStream(resourceName)) + using (StreamReader reader = new StreamReader(stream)) { - process.StartInfo.FileName = Git; - process.StartInfo.Arguments = GitBranch; - process.StartInfo.RedirectStandardOutput = true; - process.StartInfo.CreateNoWindow = true; - process.Start(); - StreamReader reader = process.StandardOutput; - string output = reader.ReadToEnd(); - branchOutput = output.Trim(); - process.WaitForExit(); + // \r is getting nuked just in case Windows is weird. + gitOutput = reader.ReadToEnd().Trim().Replace("\r", ""); } + + // gets formatted as " ()" + var outputAsArray = gitOutput.Split('\n'); + gitOutput = $"{outputAsArray[0]} ({outputAsArray[1]})"; } - // If git can't be found, assign default values + // If accessing it fails, give it a default output catch { - commitOutput = branchOutput = "unavailable"; + gitOutput = "unavailable"; } // return combined commit + branch - string finalGitVersion = $"{commitOutput} ({branchOutput})"; - return finalGitVersion; + if (String.IsNullOrWhiteSpace(gitOutput)) gitOutput = "unavailable"; + return gitOutput; } } \ No newline at end of file From 6c856398f55e48288af2854dcbb77a1124a2589e Mon Sep 17 00:00:00 2001 From: Miepee <38186597+Miepee@users.noreply.github.com> Date: Wed, 4 May 2022 19:07:31 +0200 Subject: [PATCH 3/3] Remove unneded variables --- UndertaleModLib/Util/GitVersion.cs | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/UndertaleModLib/Util/GitVersion.cs b/UndertaleModLib/Util/GitVersion.cs index c82891672..d556aae67 100644 --- a/UndertaleModLib/Util/GitVersion.cs +++ b/UndertaleModLib/Util/GitVersion.cs @@ -12,21 +12,6 @@ namespace UndertaleModLib.Util; /// public static class GitVersion { - /// - /// The constant for the git executable. - /// - private const string Git = "git"; - - /// - /// The constant to receive commit name. - /// - private const string GitCommit = "describe --always --dirty"; - - /// - /// The constant to receive branch name. - /// - private const string GitBranch = "rev-parse --abbrev-ref HEAD"; - /// /// Gets and returns the git commit and branch name. ///