Skip to content

Commit 65c299d

Browse files
committed
Add commit update log, improve debug build version check
1 parent 0614f66 commit 65c299d

File tree

5 files changed

+166
-19
lines changed

5 files changed

+166
-19
lines changed

ContentEditor.App/APIs/Github/GithubApi.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,28 @@ public class GithubApi
2020
try {
2121
var data = JsonSerializer.Deserialize<GithubReleaseInfo>(content);
2222
return data;
23-
} catch (Exception) {
24-
// ignore
23+
} catch (Exception e) {
24+
Logger.Debug("Failed to fetch release info: " + e.Message);
2525
}
2626
}
2727
return null;
2828
}
2929

30-
public async Task<GithubReleaseInfo?> FetchCommits()
30+
public async Task<List<GithubCommit>?> FetchCommits()
3131
{
3232
var (http, request) = CreateRequest($"https://api.github.com/repos/{RepoPath}/commits");
33-
throw new NotImplementedException();
33+
34+
var response = await http.SendAsync(request);
35+
if (response.StatusCode == System.Net.HttpStatusCode.OK) {
36+
var content = await response.Content.ReadAsStringAsync();
37+
try {
38+
var data = JsonSerializer.Deserialize<List<GithubCommit>>(content);
39+
return data;
40+
} catch (Exception e) {
41+
Logger.Debug("Failed to fetch commit info: " + e.Message);
42+
}
43+
}
44+
return null;
3445
}
3546

3647

@@ -63,6 +74,11 @@ public sealed class GithubReleaseInfo
6374
[JsonPropertyName("created_at")]
6475
public DateTime CreatedAt { get; set; }
6576

77+
[JsonPropertyName("published_at")]
78+
public DateTime PublishedAt { get; set; }
79+
80+
[JsonIgnore] public DateTime ReleaseDate => PublishedAt == DateTime.MinValue ? CreatedAt : PublishedAt;
81+
6682
[JsonPropertyName("assets")]
6783
public List<GithubReleaseAsset> Assets { get; set; } = new();
6884
}
@@ -81,3 +97,30 @@ public sealed class GithubReleaseAsset
8197
[JsonPropertyName("created_at")]
8298
public DateTime CreatedAt { get; set; }
8399
}
100+
101+
public sealed class GithubCommit
102+
{
103+
[JsonPropertyName("sha")]
104+
public string? Sha { get; set; }
105+
106+
[JsonPropertyName("commit")]
107+
public GithubCommitInfo Commit { get; set; } = new();
108+
}
109+
110+
public sealed class GithubCommitInfo
111+
{
112+
[JsonPropertyName("message")]
113+
public string? Message { get; set; }
114+
115+
[JsonPropertyName("author")]
116+
public GithubCommitAuthor? Author { get; set; }
117+
}
118+
119+
public sealed class GithubCommitAuthor
120+
{
121+
[JsonPropertyName("name")]
122+
public string? Name { get; set; }
123+
124+
[JsonPropertyName("date")]
125+
public DateTime Date { get; set; }
126+
}

ContentEditor.App/Configuration/AppConfig.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private class AppGameConfig
9696
private const string JsonFilename = "ce_config.json";
9797

9898
public static readonly string Version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion ?? "";
99+
public static readonly string? RevisionHash = Version.Contains("-r") ? Version.Substring(Version.IndexOf("-r")+2) : null;
99100

100101
#if DEBUG
101102
public static readonly bool IsDebugBuild = true;
@@ -717,10 +718,11 @@ public record DevSettings
717718
public record ChangelogData
718719
{
719720
public List<GithubReleaseInfo> Releases { get; set; } = new();
721+
public List<GithubCommit> Commits { get; set; } = new();
720722

721723
[JsonIgnore] public string? LatestReleaseVersion => Releases.FirstOrDefault()?.TagName?.Replace("v", "");
722724

723-
public List<GithubReleaseInfo> FindCurrentReleaseList()
725+
public List<GithubReleaseInfo> FindCurrentAndNewReleaseList()
724726
{
725727
var list = new List<GithubReleaseInfo>();
726728
if (AppConfig.IsDebugBuild) {
@@ -744,6 +746,26 @@ public List<GithubReleaseInfo> FindCurrentReleaseList()
744746
return list;
745747
}
746748

749+
public List<GithubCommit> FindCurrentAndNewCommits()
750+
{
751+
var releaseBuildDate = AppConfig.IsDebugBuild ? null : FindCurrentAndNewReleaseList().LastOrDefault()?.ReleaseDate;
752+
753+
var list = new List<GithubCommit>();
754+
foreach (var commit in Commits) {
755+
if (!AppConfig.IsDebugBuild && commit.Commit.Author?.Date < releaseBuildDate) {
756+
break;
757+
}
758+
759+
list.Add(commit);
760+
if (AppConfig.IsDebugBuild) {
761+
if (!string.IsNullOrEmpty(AppConfig.RevisionHash) && !string.IsNullOrEmpty(commit.Sha) && commit.Sha.StartsWith(AppConfig.RevisionHash)) {
762+
break;
763+
}
764+
}
765+
}
766+
767+
return list;
768+
}
747769
internal void StoreReleaseInfo(GithubReleaseInfo release)
748770
{
749771
if (string.IsNullOrEmpty(release.TagName)) return;
@@ -752,9 +774,9 @@ internal void StoreReleaseInfo(GithubReleaseInfo release)
752774
if (exists == null) {
753775
Releases.Insert(0, release);
754776
AppConfig.Settings.Save();
755-
} else if (exists.Assets.Count != release.Assets.Count || exists.Body != release.Body) {
777+
} else if (exists.Assets.Count != release.Assets.Count || exists.Body != release.Body || exists.PublishedAt == DateTime.MinValue) {
756778
exists.Assets = release.Assets;
757-
exists.CreatedAt = release.CreatedAt;
779+
exists.PublishedAt = release.PublishedAt;
758780
AppConfig.Settings.Save();
759781
}
760782
}

ContentEditor.App/Core/AutoUpdater.cs

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ private static async Task CheckForUpdate(bool isManualCheck)
3030
var knownLatest = AppConfig.Settings.Changelogs.LatestReleaseVersion;
3131

3232
if (!isManualCheck && (DateTime.UtcNow - config.LastUpdateCheck.Get()).TotalMilliseconds < UpdateCheckIntervalMillis) {
33-
AppConfig.IsOutdatedVersion = !string.IsNullOrEmpty(knownLatest) && knownLatest != AppConfig.Version;
33+
if (AppConfig.IsDebugBuild) {
34+
var commits = config.JsonSettings.Changelogs.FindCurrentAndNewCommits();
35+
AppConfig.IsOutdatedVersion = AppConfig.RevisionHash != null && commits.Count > 0 && commits[0].Sha != AppConfig.RevisionHash;
36+
} else {
37+
AppConfig.IsOutdatedVersion = !string.IsNullOrEmpty(knownLatest) && knownLatest != AppConfig.Version;
38+
}
39+
3440
if (!AppConfig.IsOutdatedVersion) {
3541
_ = Task.Delay(UpdateCheckIntervalMillis).ContinueWith(t => QueueAutoUpdateCheck());
3642
}
@@ -49,19 +55,66 @@ private static async Task CheckForUpdate(bool isManualCheck)
4955
AppConfig.Settings.Changelogs.StoreReleaseInfo(data);
5056
});
5157
}
52-
if (!string.IsNullOrEmpty(knownLatest) && knownLatest != AppConfig.Version) {
53-
AppConfig.IsOutdatedVersion = true;
54-
if (isManualCheck) Logger.Info("New version is available: " + knownLatest);
55-
} else if (isManualCheck) {
56-
Logger.Info("We are still up to date");
57-
} else {
58-
_ = Task.Delay(UpdateCheckIntervalMillis).ContinueWith(t => QueueAutoUpdateCheck());
58+
if (!AppConfig.IsDebugBuild) {
59+
if (!string.IsNullOrEmpty(knownLatest) && knownLatest != AppConfig.Version) {
60+
AppConfig.IsOutdatedVersion = true;
61+
if (isManualCheck) Logger.Info("New version is available: " + knownLatest);
62+
} else if (isManualCheck) {
63+
Logger.Info("We are still up to date");
64+
} else {
65+
_ = Task.Delay(UpdateCheckIntervalMillis).ContinueWith(t => QueueAutoUpdateCheck());
66+
}
5967
}
68+
69+
await CheckCommits(isManualCheck);
6070
} catch (Exception e) {
6171
Logger.Warn("Update check failed: " + e.Message);
6272
} finally {
6373
UpdateCheckInProgress = false;
6474
}
6575
}
6676

77+
private static async Task CheckCommits(bool isManualCheck)
78+
{
79+
var config = AppConfig.Instance;
80+
var revision = AppConfig.RevisionHash;
81+
82+
var knownLatestCommitSha = AppConfig.Settings.Changelogs.Commits.FirstOrDefault()?.Sha;
83+
84+
try {
85+
var commits = await github.FetchCommits();
86+
if (commits != null) {
87+
// use the main thread just in case to prevent potential multithreading issues
88+
MainLoop.Instance.InvokeFromUIThread(() => {
89+
AppConfig.Settings.Changelogs.Commits = commits;
90+
AppConfig.Settings.Save();
91+
92+
if (AppConfig.IsDebugBuild) {
93+
var activeCommits = config.JsonSettings.Changelogs.FindCurrentAndNewCommits();
94+
if (string.IsNullOrEmpty(revision)) {
95+
if (isManualCheck) Logger.Info("Latest commit: " + activeCommits.First().Commit.Message);
96+
return;
97+
}
98+
var currentLatest = activeCommits.FirstOrDefault()?.Sha;
99+
100+
if (activeCommits.FirstOrDefault()?.Sha?.StartsWith(revision) == true) {
101+
if (isManualCheck) {
102+
Logger.Info("We are probably still up to date");
103+
} else {
104+
_ = Task.Delay(UpdateCheckIntervalMillis).ContinueWith(t => QueueAutoUpdateCheck());
105+
}
106+
} else if (!string.IsNullOrEmpty(knownLatestCommitSha) && !string.IsNullOrEmpty(currentLatest) && knownLatestCommitSha != currentLatest && !currentLatest.StartsWith(revision)) {
107+
AppConfig.IsOutdatedVersion = true;
108+
if (isManualCheck) Logger.Info("New commit detected: " + activeCommits[0].Commit.Message);
109+
} else {
110+
if (isManualCheck) Logger.Info("We are probably still up to date");
111+
}
112+
}
113+
});
114+
}
115+
116+
} catch (Exception e) {
117+
Logger.Warn("Commit lookup failed: " + e.Message);
118+
}
119+
}
67120
}

ContentEditor.App/Imgui/App/Home.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ private static void ShowGameList()
213213
FileSystemUtils.OpenURL(GithubApi.LatestReleaseUrl);
214214
}
215215
ImGui.PopStyleColor();
216-
ImguiHelpers.Tooltip($"New version ({AppConfig.Settings.Changelogs.LatestReleaseVersion}) available!");
216+
if (AppConfig.IsDebugBuild) {
217+
ImguiHelpers.Tooltip($"New version available!");
218+
} else {
219+
ImguiHelpers.Tooltip($"New version ({AppConfig.Settings.Changelogs.LatestReleaseVersion}) available!");
220+
}
217221
}
218222
}
219223
}
@@ -499,6 +503,10 @@ private void ShowTabs()
499503
ShowUpdateLog();
500504
ImGui.EndTabItem();
501505
}
506+
if (AppConfig.Settings.Changelogs.Commits.Count > 0 && ImGui.BeginTabItem("Latest changes")) {
507+
ShowCommitLog();
508+
ImGui.EndTabItem();
509+
}
502510
}
503511
ImGui.EndTabBar();
504512
}
@@ -512,7 +520,7 @@ private void ShowUpdateLog()
512520
AutoUpdater.CheckForUpdateInBackground();
513521
}
514522
}
515-
var releases = AppConfig.Settings.Changelogs.FindCurrentReleaseList();
523+
var releases = AppConfig.Settings.Changelogs.FindCurrentAndNewReleaseList();
516524
if (releases.Count == 0) {
517525
ImGui.Text($"No release data is currently available. You can manually check the repository for changes: {GithubApi.MainRepositoryUrl}");
518526
if (ImGui.Button("Open in browser")) {
@@ -526,7 +534,7 @@ private void ShowUpdateLog()
526534
ImGui.Text("Version " + release.TagName);
527535
ImGui.PopFont();
528536

529-
ImGui.TextColored(Colors.Faded, $"Release date: {release.CreatedAt.ToString()}");
537+
ImGui.TextColored(Colors.Faded, $"Release date: {release.ReleaseDate.ToLocalTime().ToString()}");
530538

531539
if (release.TagName == AppConfig.Version) {
532540
ImGui.SameLine();
@@ -584,6 +592,27 @@ private void ShowUpdateLog()
584592
}
585593
}
586594

595+
private void ShowCommitLog()
596+
{
597+
ImGui.TextColored(Colors.Info, "The latest changes are always available in the debug builds.\nThese might not yet be fully tested and some features may be incomplete.\nA GitHub account is required to download."u8);
598+
if (ImGui.Button("View debug builds")) {
599+
FileSystemUtils.OpenURL("https://github.com/kagenocookie/REE-Content-Editor/actions");
600+
}
601+
ImGui.Separator();
602+
603+
var commits = AppConfig.Settings.Changelogs.FindCurrentAndNewCommits();
604+
foreach (var commit in commits) {
605+
ImGui.TextColored(Colors.Faded, commit.Commit.Author?.Date.ToLocalTime().ToString() ?? "[unknown time]");
606+
ImGui.SameLine();
607+
ImGui.Text(commit.Commit.Message ?? "<no message>");
608+
if (AppConfig.RevisionHash != null && commit.Sha?.StartsWith(AppConfig.RevisionHash) == true) {
609+
ImGui.SameLine();
610+
ImGui.TextColored(Colors.Success, "(current)");
611+
}
612+
ImGui.Spacing();
613+
}
614+
}
615+
587616
private void ShowRecentFilesList()
588617
{
589618
var recents = AppConfig.Settings.RecentFiles;

ContentEditor.App/NativeWindows/EditorWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ protected void ShowMainMenuBar()
749749
FileSystemUtils.OpenURL("https://ko-fi.com/shadowcookie");
750750
}
751751

752-
if (AppConfig.IsOutdatedVersion && AppConfig.Instance.EnableUpdateCheck && ImGui.MenuItem($"New version ({AppConfig.Settings.Changelogs.LatestReleaseVersion}) available!")) {
752+
if (AppConfig.IsOutdatedVersion && AppConfig.Instance.EnableUpdateCheck && ImGui.MenuItem(AppConfig.IsDebugBuild ? "New version available!" : $"New version ({AppConfig.Settings.Changelogs.LatestReleaseVersion}) available!")) {
753753
FileSystemUtils.OpenURL(GithubApi.MainRepositoryUrl);
754754
}
755755

0 commit comments

Comments
 (0)