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 new file mode 100644 index 000000000..d556aae67 --- /dev/null +++ b/UndertaleModLib/Util/GitVersion.cs @@ -0,0 +1,50 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; + +namespace UndertaleModLib.Util; + +/// +/// Includes miscellaneous git information about the project to compile. +/// Only intended for Debug use! +/// +public static class GitVersion +{ + /// + /// Gets and returns the git commit and branch name. + /// + /// The git commit and branch name. + public static string GetGitVersion() + { + string gitOutput = ""; + + // try to access the embedded resource + try + { + var assembly = Assembly.GetExecutingAssembly(); + var resourceName = "UndertaleModLib.gitversion.txt"; + + using (Stream stream = assembly.GetManifestResourceStream(resourceName)) + using (StreamReader reader = new StreamReader(stream)) + { + // \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 accessing it fails, give it a default output + catch + { + gitOutput = "unavailable"; + } + + // return combined commit + branch + if (String.IsNullOrWhiteSpace(gitOutput)) gitOutput = "unavailable"; + return gitOutput; + } +} \ 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)