From b7d41f0df36e70125e4e56eaec21c68eb8e2efb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flurin=20Br=C3=BChwiler?= Date: Fri, 21 Jul 2023 18:01:03 +0200 Subject: [PATCH 1/5] update token count on hot reload --- Chess-Challenge/Chess-Challenge.csproj | 12 ------------ .../Application/Core/ChallengeController.cs | 12 ++++++------ .../Framework/Application/Core/HotReloadmanager.cs | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs diff --git a/Chess-Challenge/Chess-Challenge.csproj b/Chess-Challenge/Chess-Challenge.csproj index 4b6d061f8..5e1c3d5ad 100644 --- a/Chess-Challenge/Chess-Challenge.csproj +++ b/Chess-Challenge/Chess-Challenge.csproj @@ -22,18 +22,6 @@ - - - Always - - - Never - - - Always - - - PreserveNewest diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index b9be8cdf3..02b04e2cf 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -50,12 +50,12 @@ public enum PlayerType // Other readonly BoardUI boardUI; readonly MoveGenerator moveGenerator; - readonly int tokenCount; + static int tokenCount; readonly StringBuilder pgns; public ChallengeController() { - tokenCount = GetTokenCount(); + UpdateTokenCount(); Warmer.Warm(); moveGenerator = new(); @@ -211,13 +211,13 @@ ChessPlayer CreatePlayer(PlayerType type) }; } - static int GetTokenCount() + public static void UpdateTokenCount() { - string path = Path.Combine(Directory.GetCurrentDirectory(), "src", "My Bot", "MyBot.cs"); - + string path = Path.Combine(Directory.GetCurrentDirectory(), "../../../", "src", "My Bot", "MyBot.cs"); + using StreamReader reader = new(path); string txt = reader.ReadToEnd(); - return TokenCounter.CountTokens(txt); + tokenCount = TokenCounter.CountTokens(txt); } void OnMoveChosen(Move chosenMove) diff --git a/Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs b/Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs new file mode 100644 index 000000000..fd4557925 --- /dev/null +++ b/Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs @@ -0,0 +1,14 @@ +using System; +using ChessChallenge.Application; + +[assembly: System.Reflection.Metadata.MetadataUpdateHandler(typeof(HotReloadmanager))] + +namespace ChessChallenge.Application; + +public static class HotReloadmanager +{ + public static void UpdateApplication(Type[]? updatedTypes) + { + ChallengeController.UpdateTokenCount(); + } +} \ No newline at end of file From 5c70335f84ad81eee5644d7301239f9056e20a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flurin=20Br=C3=BChwiler?= Date: Fri, 21 Jul 2023 18:30:57 +0200 Subject: [PATCH 2/5] refactoring --- Chess-Challenge/Chess-Challenge.csproj | 6 ++++++ .../Application/Core/ChallengeController.cs | 13 ++++++++----- .../Framework/Application/Core/HotReloadmanager.cs | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Chess-Challenge/Chess-Challenge.csproj b/Chess-Challenge/Chess-Challenge.csproj index 5e1c3d5ad..069ef5447 100644 --- a/Chess-Challenge/Chess-Challenge.csproj +++ b/Chess-Challenge/Chess-Challenge.csproj @@ -45,4 +45,10 @@ + + + Always + + + diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index 02b04e2cf..d89a47fcd 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -211,12 +211,15 @@ ChessPlayer CreatePlayer(PlayerType type) }; } - public static void UpdateTokenCount() + public static void UpdateTokenCount(bool isHotReloadUpdate = false) { - string path = Path.Combine(Directory.GetCurrentDirectory(), "../../../", "src", "My Bot", "MyBot.cs"); - - using StreamReader reader = new(path); - string txt = reader.ReadToEnd(); + var location = isHotReloadUpdate ? "../../../src/My Bot/MyBot.cs" : "src/My Bot/MyBot.cs"; + var path = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), location)); + if (!File.Exists(path)) + { + Console.WriteLine("Could not find MyBot.cs"); + } + var txt = File.ReadAllText(path); tokenCount = TokenCounter.CountTokens(txt); } diff --git a/Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs b/Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs index fd4557925..ca0fbc9fe 100644 --- a/Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs +++ b/Chess-Challenge/src/Framework/Application/Core/HotReloadmanager.cs @@ -9,6 +9,6 @@ public static class HotReloadmanager { public static void UpdateApplication(Type[]? updatedTypes) { - ChallengeController.UpdateTokenCount(); + ChallengeController.UpdateTokenCount(true); } } \ No newline at end of file From f5079e2728fff1106a16c0fc792a172405fcc76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flurin=20Br=C3=BChwiler?= Date: Fri, 21 Jul 2023 18:32:09 +0200 Subject: [PATCH 3/5] remove changes --- Chess-Challenge/Chess-Challenge.csproj | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Chess-Challenge/Chess-Challenge.csproj b/Chess-Challenge/Chess-Challenge.csproj index 069ef5447..6bc3df676 100644 --- a/Chess-Challenge/Chess-Challenge.csproj +++ b/Chess-Challenge/Chess-Challenge.csproj @@ -22,6 +22,18 @@ + + + Always + + + Never + + + Always + + + PreserveNewest @@ -45,10 +57,4 @@ - - - Always - - - From 4a8f08d0f4ff7c87bd6002bcde9641b7a895c327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flurin=20Br=C3=BChwiler?= Date: Fri, 21 Jul 2023 18:32:32 +0200 Subject: [PATCH 4/5] remove change --- Chess-Challenge/Chess-Challenge.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chess-Challenge/Chess-Challenge.csproj b/Chess-Challenge/Chess-Challenge.csproj index 6bc3df676..4b6d061f8 100644 --- a/Chess-Challenge/Chess-Challenge.csproj +++ b/Chess-Challenge/Chess-Challenge.csproj @@ -33,7 +33,7 @@ Always - + PreserveNewest From dcac6fb53a63331313c6da7b7663b6d6cc5a38d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flurin=20Br=C3=BChwiler?= Date: Fri, 21 Jul 2023 19:39:09 +0200 Subject: [PATCH 5/5] using consolehelper --- .../src/Framework/Application/Core/ChallengeController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs index d89a47fcd..8668e8d3f 100644 --- a/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs +++ b/Chess-Challenge/src/Framework/Application/Core/ChallengeController.cs @@ -217,7 +217,7 @@ public static void UpdateTokenCount(bool isHotReloadUpdate = false) var path = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), location)); if (!File.Exists(path)) { - Console.WriteLine("Could not find MyBot.cs"); + Log("Could not find MyBot.cs", true); } var txt = File.ReadAllText(path); tokenCount = TokenCounter.CountTokens(txt);