diff --git a/AutoSynthesis/AutoSynthesis.csproj b/AutoSynthesis/AutoSynthesis.csproj index 9eba1bf..29865a8 100644 --- a/AutoSynthesis/AutoSynthesis.csproj +++ b/AutoSynthesis/AutoSynthesis.csproj @@ -177,6 +177,7 @@ ResXFileCodeGenerator Resources.Designer.cs + Designer @@ -206,10 +207,9 @@ - - - - + + PreserveNewest + diff --git a/AutoSynthesis/Classes/Backend/CraftingEngine.cs b/AutoSynthesis/Classes/Backend/CraftingEngine.cs index 8b36755..505a1e1 100644 --- a/AutoSynthesis/Classes/Backend/CraftingEngine.cs +++ b/AutoSynthesis/Classes/Backend/CraftingEngine.cs @@ -14,25 +14,41 @@ static class CraftingEngine private static SettingsContainer Settings { get; set; } public static bool CraftingActive { get; set; } = false; public static DateTime NextFoodUse { get; set; } + public static DateTime FoodExpiration { get; set; } public static DateTime NextSyrupUse { get; set; } public static CancellationTokenSource Cts { get; set; } private static Action EndCraftCallback { get; set; } public static Action ErrorMessageHandler { get; set; } private static Action SetFoodAndSyrupTimings { get; set; } + + // Action to update the "Craft Count" text box + private static Action SetCraftCount { get; set; } + private static bool CraftPrimedToCancel { get; set; } + private static bool FoodPrimedToCancel { get; set; } private const int CONSUMABLE_MARGIN_IN_MINUTES = 2; private const int STANDARD_SYRUP_TIME = 15; private const int STANDARD_MENU_DELAY = 2000; private const int STANDARD_ANIMATION_DELAY = 2000; private const int STANDARD_TICK_TIME = 50; - private static int CraftCount = 0; - private static int TotalCount = 0; + + // The current craft number + private static int CraftNumber = 0; + + // The craft number target + private static int CraftCountTarget = 0; + + // The number of crafts completed + private static int CompletedCount = 0; + + private static int FoodConsumed = 0; #endregion #region System Methods public static void InitiateCraftingEngine(Dictionary hotKeyDictionary, - SettingsContainer userSettings, Action endCraftCallback, Action errorMessageHandler, Action setFoodAndSyrupTimings) + SettingsContainer userSettings, Action endCraftCallback, Action errorMessageHandler, + Action setFoodAndSyrupTimings, Action setCraftCount) { // Ensure craft is not already happening if (CraftingActive) @@ -48,12 +64,14 @@ public static void InitiateCraftingEngine(Dictionary hotKeyDicti EndCraftCallback = endCraftCallback; ErrorMessageHandler = errorMessageHandler; SetFoodAndSyrupTimings = setFoodAndSyrupTimings; + SetCraftCount = setCraftCount; // Load process or throw error if process does not exist ProcessManager.LoadProcess(); // Set values CraftPrimedToCancel = false; + FoodPrimedToCancel = false; // Store values HotkeySet = hotKeyDictionary; @@ -71,12 +89,27 @@ public static void InitiateCraftingEngine(Dictionary hotKeyDicti } } + public static void CancelAfterFood() + { + if (!CraftingActive) + return; + + if (!FoodPrimedToCancel) + { + FoodPrimedToCancel = true; + UICommunicator.UpdateStatus("Finishing this food...", true); + return; + } + + UICommunicator.UpdateStatus("Ending Food..."); + } + public static void CancelCrafting() { if (!CraftingActive) return; - if(!CraftPrimedToCancel) + if (!CraftPrimedToCancel) { CraftPrimedToCancel = true; UICommunicator.UpdateStatus("Finishing this craft...", true); @@ -107,36 +140,42 @@ private static void RunCraftingEngine(CancellationToken token) UICommunicator.ErrorMessageHandler = ErrorMessageHandler; UICommunicator.UpdateStatus("Setting up for Crafting..."); UICommunicator.StartTimedProgressBarUpdates(); - UICommunicator.UpdateCompletedUIInfo(0, Settings.CraftCount); - - CraftCount = 1; - TotalCount = 0; + CraftNumber = 1; + CraftCountTarget = Settings.CraftCount; + CompletedCount = 0; + FoodConsumed = 0; if (HotkeySet[HKType.Food] != null) { - NextFoodUse = CalculateNextConsumableUse(Settings.StartingFoodTime); + UICommunicator.UpdateFoodUIInfo(FoodConsumed, Settings.FoodCount); + NextFoodUse = CalculateNextConsumableUse(Settings.StartingFoodTime, DateTime.Now); UICommunicator.UpdateFood(NextFoodUse); + + FoodExpiration = DateTime.Now.AddMinutes(Settings.StartingFoodTime); + RunFoodProgressBar(Settings.StartingFoodTime * 60 * 1000); } if (HotkeySet[HKType.Syrup] != null) { - NextSyrupUse = CalculateNextConsumableUse(Settings.StartingSyrupTime); + NextSyrupUse = CalculateNextConsumableUse(Settings.StartingSyrupTime, DateTime.Now); UICommunicator.UpdateSyrup(NextSyrupUse); } + RunTotalProgressBar(); + DateTime craftStartTime = DateTime.Now; - // If crafts remaining was 0, loop infinitley + // If crafts remaining was 0, loop infinitely // If not, craft until quota is met while (!CraftingComplete()) { // UI MESSAGE: Set timer for overall craft - UICommunicator.UpdateCraftUIInfo(CraftCount, Settings.CraftCount); + UICommunicator.UpdateCraftUIInfo(CraftNumber, CraftCountTarget); - // Add requested delay - Break(Settings.StartingDelay); - // Begin Craft Timer: RunCraftProgressBar(); + // Add requested delay + Break(Settings.StartingDelay); + // Initiate Macro 1 SendMacroInput(HotkeySet[HKType.Macro1], 1); @@ -146,30 +185,30 @@ private static void RunCraftingEngine(CancellationToken token) // Initiate Macro 3 SendMacroInput(HotkeySet[HKType.Macro3], 3); - // Update UI Message - TotalCount = CraftCount; - UICommunicator.UpdateCompletedUIInfo(TotalCount, Settings.CraftCount); + // Finish and add requested end delay + Break(STANDARD_MENU_DELAY + Settings.EndingDelay); // Collectable Menu Option SendCollectableConfirmationInput(); - // Standard delay for menus - Break(STANDARD_MENU_DELAY); + CompletedCount = CraftNumber; // Use Food and Syrup SendFoodAndSyrupInput(); // Prepare next craft if crafting is not finished PrepareNextCraftInput(); - Break(STANDARD_ANIMATION_DELAY); - CraftCount += 1; + CraftNumber += 1; } - } + + var totalCraftTime = (int)(DateTime.Now - craftStartTime).TotalSeconds; + Logger.Write($"Actual total craft time of {totalCraftTime}"); + } catch (Exception e) when (!(e is CraftCancelRequest)) { ErrorMessageHandler(e); - } + } finally { EndCraftingProcess(); @@ -177,19 +216,26 @@ private static void RunCraftingEngine(CancellationToken token) } private static void EndCraftingProcess() - { + { // ALL CLEANUP UICommunicator.EndAllProgress(); UICommunicator.UpdateStatus("Crafting Finished!"); var craftCompleted = "Completed "; - craftCompleted += Settings.CraftCount > 0 ? $"{TotalCount}/{Settings.CraftCount}" : $"{TotalCount}"; + craftCompleted += CraftCountTarget > 0 ? $"{CompletedCount}/{CraftCountTarget}" : $"{CompletedCount}"; UICommunicator.UpdateStatus2(craftCompleted); CraftingActive = false; - var foodRemaining = (int)(NextFoodUse - DateTime.Now).TotalMinutes; + var foodRemaining = (int)(FoodExpiration - DateTime.Now).TotalMinutes; var syrupRemaining = (int)(NextSyrupUse - DateTime.Now).TotalMinutes; SetFoodAndSyrupTimings.Invoke(foodRemaining, syrupRemaining); + if (Settings.CraftCount > 0) + { + // Update the "Craft Count" text box + var craftsRemaining = CraftCountTarget - CompletedCount; + SetCraftCount.Invoke(craftsRemaining); + } + EndCraftCallback.Invoke(); } @@ -211,22 +257,10 @@ private static void SendMacroInput(Hotkey hotkey, int macroNumber) return; Logger.Write("Sending Macro " + macroNumber); // UI message: MACRO NUMBER macroNumber - UICommunicator.UpdateMacroUIInfo(macroNumber, hotkey.TimerInMiliseconds, VerifyFinalMacro(macroNumber)); + UICommunicator.UpdateMacroUIInfo(macroNumber, hotkey.TimerInMilliseconds); SendInput(hotkey); } - private static bool VerifyFinalMacro(int macroNumber) - { - switch (macroNumber) - { - case 1: - return (HotkeySet[HKType.Macro2] == null && HotkeySet[HKType.Macro3] == null); - case 2: - return (HotkeySet[HKType.Macro3] == null); - } - return true; - } - private static void SendCollectableConfirmationInput() { if (Settings.CollectableCraft == false) @@ -235,14 +269,13 @@ private static void SendCollectableConfirmationInput() Logger.Write("Accepting Collectable Craft"); UICommunicator.UpdateStatus("Accepting Collectable Craft..."); + SendInput(HotkeySet[HKType.Confirm], 2); Break(STANDARD_MENU_DELAY); - SendInput(HotkeySet[HKType.Confirm]); - SendInput(HotkeySet[HKType.Confirm]); } private static bool CraftingComplete() { - return CraftPrimedToCancel || ((Settings.CraftCount != 0) && (TotalCount >= Settings.CraftCount)); + return CraftPrimedToCancel || ((CraftCountTarget != 0) && (CompletedCount >= CraftCountTarget)); } private static void SendFoodAndSyrupInput() @@ -257,26 +290,36 @@ private static void SendFoodAndSyrupInput() // leave if neither are to be used if (!useFood && !useSyrup) return; + + if (useFood && FoodPrimedToCancel || (Settings.FoodCount > 0 && FoodConsumed >= Settings.FoodCount)) + { + // Only cancel crafting if food is actually about to expire + if (DateTime.Compare(FoodExpiration, DateTime.Now) <= 0) + { + throw new CraftCancelRequest(); + } + // Don't use food + return; + } + UICommunicator.UpdateStatus("Refreshing Consumables..."); Logger.Write("Refreshing Consumables"); // enter a craft and leave it out - Break(500); + Break(Settings.StartingDelay); try { ProcessManager.DisableInputs(); - Break(50); + Break(100); SendInput(HotkeySet[HKType.Confirm], 3); - SendInput(HotkeySet[HKType.Cancel]); - Break(50); } finally { ProcessManager.EnableInputs(); } - Break(1500); + Break(STANDARD_ANIMATION_DELAY); try { @@ -285,14 +328,13 @@ private static void SendFoodAndSyrupInput() SendInput(HotkeySet[HKType.Confirm]); SendInput(HotkeySet[HKType.Cancel]); SendInput(HotkeySet[HKType.Confirm]); - Break(50); } finally { ProcessManager.EnableInputs(); } - Break(2000); + Break(STANDARD_MENU_DELAY); // use food and syrup as needed if (useFood) @@ -300,15 +342,22 @@ private static void SendFoodAndSyrupInput() UICommunicator.UpdateStatus("Using Food..."); Logger.Write("Using Food"); SendInput(HotkeySet[HKType.Food]); - NextFoodUse = CalculateNextConsumableUse(Settings.FoodDuration); + + FoodConsumed++; + UICommunicator.UpdateFoodUIInfo(FoodConsumed, Settings.FoodCount); + + NextFoodUse = CalculateNextConsumableUse(Settings.FoodDuration, FoodExpiration); UICommunicator.UpdateFood(NextFoodUse); + + FoodExpiration = FoodExpiration.AddMinutes(Settings.FoodDuration); + RunFoodProgressBar((int)(FoodExpiration - DateTime.Now).TotalMilliseconds); } if (useSyrup) { UICommunicator.UpdateStatus("Using Syrup..."); Logger.Write("Using Syrup"); SendInput(HotkeySet[HKType.Syrup]); - NextSyrupUse = CalculateNextConsumableUse(STANDARD_SYRUP_TIME); + NextSyrupUse = CalculateNextConsumableUse(STANDARD_SYRUP_TIME, DateTime.Now); UICommunicator.UpdateSyrup(NextSyrupUse); } } @@ -317,53 +366,167 @@ private static void PrepareNextCraftInput() { if (CraftingComplete()) return; - + UICommunicator.UpdateStatus("Preparing Next Craft..."); Logger.Write("Resetting Craft Cycle"); try { ProcessManager.DisableInputs(); - Break(50); + Break(100); SendInput(HotkeySet[HKType.Confirm], 3); - Break(50); } finally { ProcessManager.EnableInputs(); } - + Break(STANDARD_ANIMATION_DELAY); } - private static DateTime CalculateNextConsumableUse(int timeRemainingInMinutes) + #endregion + + #region Calculation Methods + + private static DateTime CalculateNextConsumableUse(int timeRemainingInMinutes, DateTime start) { var timeMargin = CalculateFoodSyrupMargin(); - return DateTime.Now.AddMinutes(timeRemainingInMinutes - timeMargin); + return start.AddMinutes(timeRemainingInMinutes - timeMargin); } private static double CalculateFoodSyrupMargin() { - double totalTime = HotkeySet[HKType.Macro1].TimerInMiliseconds; - if (HotkeySet[HKType.Macro2] != null) - totalTime += HotkeySet[HKType.Macro2].TimerInMiliseconds; - if (HotkeySet[HKType.Macro3] != null) - totalTime += HotkeySet[HKType.Macro3].TimerInMiliseconds; - return totalTime / (1000 * 60) + 0.25; + return (double)CalculateMacroTime() / (1000 * 60) + 0.25; + } + + private static int CalculateInputTime(HKType type) + { + if (HotkeySet[type] == null) + { + return 0; + } + + int totalTime = HotkeySet[type].TimerInMilliseconds; + if (HotkeySet[type].ModKeyCodes != null) + { + totalTime += 50; + } + return totalTime; + } + + private static int CalculateMacroTime() + { + int totalTime = CalculateInputTime(HKType.Macro1); + totalTime += CalculateInputTime(HKType.Macro2); + totalTime += CalculateInputTime(HKType.Macro3); + return totalTime; + } + + private static int CalculateCraftTime() + { + int totalTime = Settings.StartingDelay; + totalTime += CalculateMacroTime(); + totalTime += STANDARD_MENU_DELAY; + totalTime += Settings.EndingDelay; + if (Settings.CollectableCraft) + { + totalTime += CalculateInputTime(HKType.Confirm) * 2; + totalTime += STANDARD_MENU_DELAY; + } + return totalTime; } #endregion #region UI Methods - private static void RunCraftProgressBar() + private static void RunTotalProgressBar() { - int totalTime = HotkeySet[HKType.Macro1].TimerInMiliseconds; - if (HotkeySet[HKType.Macro2] != null) - totalTime += HotkeySet[HKType.Macro2].TimerInMiliseconds; - if (HotkeySet[HKType.Macro3] != null) - totalTime += HotkeySet[HKType.Macro3].TimerInMiliseconds; + // Don't set up the total progress bar if we don't have targets + if (CraftCountTarget == 0 && Settings.FoodCount == 0) + { + return; + } + + DateTime start = DateTime.Now; + DateTime simulated = start; + DateTime simulatedNextFood = NextFoodUse; + DateTime simulatedNextSyrup = NextSyrupUse; + DateTime simulatedFoodExpiration = FoodExpiration; + bool done = false; + int craftsCompleted = 0; + int foodConsumed = 0; + while (!done) + { + simulated = simulated.AddMilliseconds(CalculateCraftTime()); + craftsCompleted++; + + if (CraftCountTarget > 0 && craftsCompleted >= CraftCountTarget) + { + break; + } + + bool useFood = (HotkeySet[HKType.Food] != null && DateTime.Compare(simulatedNextFood, simulated) <= 0); + bool useSyrup = (HotkeySet[HKType.Syrup] != null && DateTime.Compare(simulatedNextSyrup, simulated) <= 0); + if (Settings.FoodCount > 0 && + HotkeySet[HKType.Food] != null && + foodConsumed >= Settings.FoodCount + && DateTime.Compare(simulatedFoodExpiration, simulated) <= 0) + { + break; + } + if (useFood || useSyrup) + { + simulated = simulated.AddMilliseconds( + Settings.StartingDelay + + 100 + + CalculateInputTime(HKType.Confirm) * 3 + + STANDARD_ANIMATION_DELAY + + 100 + + CalculateInputTime(HKType.Confirm) + + CalculateInputTime(HKType.Cancel) + + CalculateInputTime(HKType.Confirm) + + STANDARD_MENU_DELAY); + if (useFood) + { + simulated = simulated.AddMilliseconds(CalculateInputTime(HKType.Food)); + simulatedFoodExpiration = simulatedFoodExpiration.AddMinutes(Settings.FoodDuration); + simulatedNextFood = CalculateNextConsumableUse(Settings.FoodDuration, simulatedFoodExpiration); + foodConsumed++; + } + if (useSyrup) + { + simulated = simulated.AddMilliseconds(CalculateInputTime(HKType.Syrup)); + simulatedNextSyrup = CalculateNextConsumableUse(STANDARD_SYRUP_TIME, simulated); + } + } + + // Prepare next craft input + simulated = simulated.AddMilliseconds( + 100 + + CalculateInputTime(HKType.Confirm) * 3 + + STANDARD_ANIMATION_DELAY); + // For each craft, add an artificial execution delay + simulated = simulated.AddMilliseconds(400); + } + + CraftCountTarget = craftsCompleted; + var totalDuration = (int)(simulated - start).TotalMilliseconds; + Logger.Write($"Estimating total craft time of {totalDuration}"); + UICommunicator.BeginTotalTimer(totalDuration); + } + + private static void RunCraftProgressBar() + { + int totalTime = CalculateCraftTime(); UICommunicator.BeginCraftTimer(totalTime); } + + private static void RunFoodProgressBar(int milliseconds) + { + // int totalTime = minutes * 60 * 1000; + UICommunicator.BeginFoodTimer(milliseconds); + + } #endregion #region Timing Methods @@ -378,8 +541,7 @@ private static void SendInput(Hotkey hotkey, int repeat = 1) { UICommunicator.UpdateStatus2("Sending \"" + hotkey.ToString() + "\""); KeyInputEngine.SendKeysToGame(hotkey.KeyCode, hotkey.ModKeyCodes); - SleepThread(hotkey.TimerInMiliseconds); - + SleepThread(hotkey.TimerInMilliseconds); } } @@ -393,6 +555,7 @@ private static void SleepThread(int timeInMilliseconds) Thread.Sleep(STANDARD_TICK_TIME); } } + #endregion } } diff --git a/AutoSynthesis/Classes/Backend/KeyInputEngine.cs b/AutoSynthesis/Classes/Backend/KeyInputEngine.cs index 72baf64..de204a2 100644 --- a/AutoSynthesis/Classes/Backend/KeyInputEngine.cs +++ b/AutoSynthesis/Classes/Backend/KeyInputEngine.cs @@ -2,10 +2,6 @@ using System.Runtime.InteropServices; using WindowsInput.Native; using System.Threading; -//using WindowsInput; - - - namespace AutoSynthesis { @@ -34,6 +30,8 @@ public static void SendKeysToGame(VirtualKeyCode key, VirtualKeyCode[] modKeys = { SendMessage(ProcessManager.ProcessPtr(), WM_KEYDOWN, (int)modKey, 0); } + + Thread.Sleep(50); } // send key command diff --git a/AutoSynthesis/Classes/Backend/UICommunicator.cs b/AutoSynthesis/Classes/Backend/UICommunicator.cs index a01d664..9a46008 100644 --- a/AutoSynthesis/Classes/Backend/UICommunicator.cs +++ b/AutoSynthesis/Classes/Backend/UICommunicator.cs @@ -22,24 +22,33 @@ static class UICommunicator #region Properties and Consts public static Label UpdateLabel { get; set; } public static Label UpdateLabel2 { get; set; } - public static Label CraftsCompletedLabel { get; set; } + public static Label TotalTimerLabel { get; set; } public static Label FoodSyrupLabel { get; set; } public static Label CraftTimerLabel { get; set; } public static Label MacroTimerLabel { get; set; } - public static ProgressBar ProgressOverall { get; set; } + public static Label FoodTimerLabel { get; set; } + public static ProgressBar ProgressTotal { get; set; } public static ProgressBar ProgressCraft { get; set; } public static ProgressBar ProgressMacro { get; set; } + public static ProgressBar ProgressFood { get; set; } + public static Action ErrorMessageHandler { get; set; } + private static double ProgressTotalTimeDuration { get; set; } private static double ProgressCraftTimeDuration { get; set; } + private static DateTime ProgressTotalTime { get; set; } private static DateTime ProgressCraftTime { get; set; } private static double ProgressMacroTimeDuration { get; set; } private static DateTime ProgressMacroTime { get; set; } + private static double ProgressFoodTimeDuration { get; set; } + private static DateTime ProgressFoodTime { get; set; } private static DateTime NullDateTime { get; set; } private static int MacroNumber { get; set; } private static int CraftNumber { get; set; } - private static int MaxNumber { get; set; } + private static int MaxCraft { get; set; } + private static int FoodNumber { get; set; } + private static int MaxFood { get; set; } private static DateTime NextFood { get; set; } private static DateTime NextSyrup { get; set; } @@ -60,92 +69,93 @@ static class UICommunicator private static bool UpdateOverride { get; set; } + private static string PreviousTotalTimerText { get; set; } private static string PreviousCraftTimerText { get; set; } private static string PreviousMacroTimerText { get; set; } + private static string PreviousFoodTimerText { get; set; } private static string PreviousFoodSyrupTimerText { get; set; } #endregion #region Setup Methods - public static void ConnectUI(Label headerLabel, Label updateLabel, Label updateLabel2, Label craftLabel, - Label macroLabel, Label foodSyrupLabel, ProgressBar progressOverall, - ProgressBar progressCraft, ProgressBar progressMacro) + public static void ConnectUI(Label totalLabel, Label updateLabel, Label updateLabel2, Label craftTimerLabel, + Label macroTimerLabel, Label foodSyrupLabel, Label foodTimerLabel, ProgressBar progressTotal, + ProgressBar progressCraft, ProgressBar progressMacro, ProgressBar progressFood) { - CraftsCompletedLabel = headerLabel; + TotalTimerLabel = totalLabel; UpdateLabel = updateLabel; UpdateLabel2 = updateLabel2; FoodSyrupLabel = foodSyrupLabel; - CraftTimerLabel = craftLabel; - MacroTimerLabel = macroLabel; - ProgressOverall = progressOverall; + CraftTimerLabel = craftTimerLabel; + MacroTimerLabel = macroTimerLabel; + FoodTimerLabel = foodTimerLabel; + ProgressTotal = progressTotal; ProgressCraft = progressCraft; ProgressMacro = progressMacro; + ProgressFood = progressFood; + + TotalTimerLabel.Visibility = Visibility.Hidden; + CraftTimerLabel.Visibility = Visibility.Hidden; + MacroTimerLabel.Visibility = Visibility.Hidden; + FoodTimerLabel.Visibility = Visibility.Hidden; + + updateLabel.Content = ""; + updateLabel2.Content = ""; + foodSyrupLabel.Content = ""; } public static void ResetValues() { CraftNumber = 0; - MaxNumber = 0; + MaxCraft = 0; MacroNumber = 0; FoodEnabled = false; SyrupEnabled = false; PreviousUpdate2Message = ""; + ProgressTotalTime = new DateTime(); + ProgressTotalTimeDuration = 0; ProgressCraftTime = new DateTime(); ProgressCraftTimeDuration = 0; ProgressMacroTime = new DateTime(); ProgressMacroTimeDuration = 0; + ProgressFoodTime = new DateTime(); + ProgressFoodTimeDuration = 0; UpdateOverride = false; PreviousCraftTimerText = ""; PreviousMacroTimerText = ""; PreviousFoodSyrupTimerText = ""; - } - #endregion + } + #endregion - #region Update Methods (Called by External Functions) - public static void UpdateCraftUIInfo(int craftCount, int max) + #region Update Methods (Called by External Functions) + // Updates visual display on craft status + public static void UpdateCraftUIInfo(int craftCount, int max) { - // Updates visual display on craft status - // Label CraftNumber = craftCount; - MaxNumber = max; + MaxCraft = max; } - public static void UpdateCompletedUIInfo(int craftCount, int max) + // Updates visual display on food status + public static void UpdateFoodUIInfo(int foodCount, int max) { - CraftNumber = craftCount; - MaxNumber = max; - string uiText = $"Crafted: "; - string craftCounter = CraftNumber.ToString(); - if (max != 0) - craftCounter += $"/{MaxNumber}"; - - if (craftCounter.Length > 5) - { - UpdateCraftNumberLabel(craftCounter); - } else - { - UpdateCraftNumberLabel(uiText + craftCounter); - } - - // Progress Bar - if (max > 0) - { - double p = (double)CraftNumber / MaxNumber; - SmoothProgressUpdate(ProgressOverall, p); - } + FoodNumber = foodCount; + MaxFood = max; } - public static void UpdateMacroUIInfo(int macroNumber, int macroTimer, bool finalMacro = false) + public static void UpdateMacroUIInfo(int macroNumber, int macroTimer) { MacroNumber = macroNumber; UpdateStatus($"Using Macro {MacroNumber}..."); ProgressMacroTimeDuration = macroTimer; - if (!finalMacro) - ProgressMacroTime = DateTime.Now.AddMilliseconds(macroTimer); - else - ProgressMacroTime = ProgressCraftTime; + ProgressMacroTime = DateTime.Now.AddMilliseconds(macroTimer); + } + + public static void BeginTotalTimer(int totalTime) + { + ProgressTotalTimeDuration = totalTime; + ProgressTotalTime = DateTime.Now.AddMilliseconds(totalTime); } public static void BeginCraftTimer(int totalTime) @@ -154,6 +164,19 @@ public static void BeginCraftTimer(int totalTime) ProgressCraftTime = DateTime.Now.AddMilliseconds(totalTime); } + public static void BeginFoodTimer(int totalTime) + { + ProgressFoodTimeDuration = totalTime; + if (IsDateNull(ProgressFoodTime)) + { + ProgressFoodTime = DateTime.Now.AddMilliseconds(totalTime); + } + else + { + ProgressFoodTime = ProgressFoodTime.AddMilliseconds(totalTime); + } + } + public static void UpdateFood(DateTime nextFood) { FoodEnabled = true; @@ -191,7 +214,8 @@ public static void UpdateStatus2(string text) { PreviousUpdate2Count++; text += "(" + PreviousUpdate2Count + ")"; - } else + } + else { PreviousUpdate2Count = 1; } @@ -207,24 +231,13 @@ public static void EndAllProgress() UpdateOverrideReset(); UpdateStatus2(""); DropProgressToZero(); - UpdateProgressBar(ProgressOverall, 0); + UpdateProgressBar(ProgressTotal, 0); UpdateProgressBar(ProgressCraft, 0); UpdateProgressBar(ProgressMacro, 0); FoodSyrupLabel.Dispatcher.Invoke(() => { FoodSyrupLabel.Content = ""; }); } #endregion - #region Label Updates - private static void UpdateCraftNumberLabel(string uiText) - { - // Update label - Action action = () => { CraftsCompletedLabel.Content = uiText; }; - DispatchActionLabel(CraftsCompletedLabel, action); - // Update progress bar - } - - #endregion - #region Progress Bar Updates public static void StartTimedProgressBarUpdates() { @@ -235,10 +248,14 @@ public static void StartTimedProgressBarUpdates() { try { - SetProgressbarLabelVisible(); + SetProgressBarLabelVisible(); while (!token.IsCancellationRequested) { + // Update Total Craft Timer + UpdateTimerProgressBar(ProgressTotal, ProgressTotalTime, ProgressTotalTimeDuration); + UpdateTotalTimerText(); + // Update Craft Timer UpdateTimerProgressBar(ProgressCraft, ProgressCraftTime, ProgressCraftTimeDuration); UpdateCraftTimerText(); @@ -248,12 +265,15 @@ public static void StartTimedProgressBarUpdates() UpdateMacroTimerText(); // Update Food Label + UpdateTimerProgressBar(ProgressFood, ProgressFoodTime, ProgressFoodTimeDuration); + UpdateFoodTimerText(); + UpdateFoodSyrupLabel(); Thread.Sleep(TICK_TIME); } - SetProgressbarLabelVisible(false); + SetProgressBarLabelVisible(false); } catch (Exception e) { @@ -263,10 +283,26 @@ public static void StartTimedProgressBarUpdates() Task.Run(action, token); } + private static void UpdateTotalTimerText() + { + var output = $"Total: "; + var timer = GetTimeRemainingString(ProgressTotalTime); + output += timer; + + if (output != PreviousTotalTimerText) + { + TotalTimerLabel.Dispatcher.Invoke(() => { TotalTimerLabel.Content = output; }); + } + + PreviousTotalTimerText = output; + } private static void UpdateCraftTimerText() { - var output = "Craft " + CraftNumber + ": "; + var output = $"Craft {CraftNumber}"; + if (MaxCraft > 0) + output += $"/{MaxCraft}"; + output += ": "; var timer = GetTimeRemainingString(ProgressCraftTime); output += timer; @@ -288,6 +324,23 @@ private static void UpdateMacroTimerText() PreviousMacroTimerText = output; } + private static void UpdateFoodTimerText() + { + var output = $"Food {FoodNumber}"; + if (MaxFood > 0) + { + output += $"/{MaxFood}"; + } + output += ": "; + var timer = GetTimeRemainingString(ProgressFoodTime); + output += timer; + + if (output != PreviousFoodTimerText) + FoodTimerLabel.Dispatcher.Invoke(() => { FoodTimerLabel.Content = output; }); + + PreviousFoodTimerText = output; + } + private static void UpdateFoodSyrupLabel() { var output = GetFoodSyrupLabelString(); @@ -303,7 +356,7 @@ private static string GetFoodSyrupLabelString() { var foodString = GetTimeRemainingString(NextFood, true); var syrupString = GetTimeRemainingString(NextSyrup, true); - return "Next Food: " + foodString + " Next Syrup: " + syrupString; + return "Next Food: " + foodString + ". Next Syrup: " + syrupString; } if (FoodEnabled) { @@ -333,24 +386,27 @@ private static string GetTimeRemainingString(DateTime target, bool longFormat = { var minutes = difference.Hours * 60 + difference.Minutes; var seconds = difference.Seconds; - return longFormat ? minutes.ToString("00") + ":" + seconds.ToString("00") : minutes + ":" + seconds; + return minutes.ToString() + ":" + seconds.ToString("00"); } } } - private static void SetProgressbarLabelVisible(bool setToVisible = true) + private static void SetProgressBarLabelVisible(bool setToVisible = true) { var startingOpacity = setToVisible ? 0 : 100; var visibility = setToVisible ? Visibility.Visible : Visibility.Hidden; // Set the labels on and their opacity to 0 - CraftsCompletedLabel.Dispatcher.Invoke(() => CraftsCompletedLabel.Opacity = startingOpacity); - MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Opacity = startingOpacity); + TotalTimerLabel.Dispatcher.Invoke(() => TotalTimerLabel.Opacity = startingOpacity); CraftTimerLabel.Dispatcher.Invoke(() => CraftTimerLabel.Opacity = startingOpacity); + MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Opacity = startingOpacity); + FoodTimerLabel.Dispatcher.Invoke(() => FoodTimerLabel.Opacity = startingOpacity); if (setToVisible) { - CraftsCompletedLabel.Dispatcher.Invoke(() => CraftsCompletedLabel.Visibility = visibility); - MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Visibility = visibility); + TotalTimerLabel.Dispatcher.Invoke(() => TotalTimerLabel.Visibility = visibility); CraftTimerLabel.Dispatcher.Invoke(() => CraftTimerLabel.Visibility = visibility); + MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Visibility = visibility); + if (FoodEnabled) + FoodTimerLabel.Dispatcher.Invoke(() => FoodTimerLabel.Visibility = visibility); } // Fade in Bars on Separate Thread @@ -363,20 +419,26 @@ private static void SetProgressbarLabelVisible(bool setToVisible = true) var prog = i * jump; if (!setToVisible) prog = 1 - prog; - CraftsCompletedLabel.Dispatcher.Invoke(() => CraftsCompletedLabel.Opacity = prog); - MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Opacity = prog); + TotalTimerLabel.Dispatcher.Invoke(() => TotalTimerLabel.Opacity = prog); CraftTimerLabel.Dispatcher.Invoke(() => CraftTimerLabel.Opacity = prog); + MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Opacity = prog); + if (FoodEnabled) + FoodTimerLabel.Dispatcher.Invoke(() => FoodTimerLabel.Opacity = prog); Thread.Sleep(TICK_TIME); } - CraftsCompletedLabel.Dispatcher.Invoke(() => CraftsCompletedLabel.Opacity = 1); - MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Opacity = 1); + TotalTimerLabel.Dispatcher.Invoke(() => TotalTimerLabel.Opacity = 1); CraftTimerLabel.Dispatcher.Invoke(() => CraftTimerLabel.Opacity = 1); + MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Opacity = 1); + if (FoodEnabled) + FoodTimerLabel.Dispatcher.Invoke(() => FoodTimerLabel.Opacity = 1); if (!setToVisible) { - CraftsCompletedLabel.Dispatcher.Invoke(() => CraftsCompletedLabel.Visibility = visibility); - MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Visibility = visibility); + TotalTimerLabel.Dispatcher.Invoke(() => TotalTimerLabel.Visibility = visibility); CraftTimerLabel.Dispatcher.Invoke(() => CraftTimerLabel.Visibility = visibility); + MacroTimerLabel.Dispatcher.Invoke(() => MacroTimerLabel.Visibility = visibility); + if (FoodEnabled) + FoodTimerLabel.Dispatcher.Invoke(() => FoodTimerLabel.Visibility = visibility); } }; Task.Run(action); @@ -454,20 +516,24 @@ private static void UpdateProgressBar(ProgressBar prog, double p) private static void DropProgressToZero() { var tickCount = FADE_TIME / TICK_TIME; - var overallProgress = GetProgressBarValue(ProgressOverall); - var overallProgressStep = overallProgress / tickCount; + var totalProgress = GetProgressBarValue(ProgressTotal); + var totalProgressStep = totalProgress / tickCount; var craftProgress = GetProgressBarValue(ProgressCraft); var craftProgressStep = craftProgress / tickCount; var macroProgress = GetProgressBarValue(ProgressMacro); var macroProgressStep = macroProgress / tickCount; + var foodProgress = GetProgressBarValue(ProgressFood); + var foodProgressStep = foodProgress / tickCount; for (int i = 0; i < tickCount; i++) { - overallProgress -= overallProgressStep; + totalProgress -= totalProgressStep; craftProgress -= craftProgressStep; macroProgress -= macroProgressStep; - UpdateProgressBar(ProgressOverall, overallProgress); + foodProgress -= foodProgressStep; + UpdateProgressBar(ProgressTotal, totalProgress); UpdateProgressBar(ProgressCraft, craftProgress); UpdateProgressBar(ProgressMacro, macroProgress); + UpdateProgressBar(ProgressFood, foodProgress); Thread.Sleep(TICK_TIME); } } @@ -486,7 +552,7 @@ private static void DispatchActionLabel(Label label, Action action) private static double GetProgressBarValue(ProgressBar progress) { - Func func = () => { return (progress.Value/progress.Maximum); }; + Func func = () => { return (progress.Value / progress.Maximum); }; var value = progress.Dispatcher.Invoke(func); return value; } diff --git a/AutoSynthesis/Classes/UI Classes/HotkeyProcessor.cs b/AutoSynthesis/Classes/UI Classes/HotkeyProcessor.cs index f2f6f8a..c900308 100644 --- a/AutoSynthesis/Classes/UI Classes/HotkeyProcessor.cs +++ b/AutoSynthesis/Classes/UI Classes/HotkeyProcessor.cs @@ -203,8 +203,8 @@ private static void SetDictionaryValues() KeyCodeToVKCode.Add(Key.Escape, VirtualKeyCode.END); KeyCodeToVKCode.Add(Key.F, VirtualKeyCode.VK_F); KeyCodeToVKCode.Add(Key.F1, VirtualKeyCode.F1); - KeyCodeToVKCode.Add(Key.F10, VirtualKeyCode.F2); - KeyCodeToVKCode.Add(Key.F11, VirtualKeyCode.F3); + KeyCodeToVKCode.Add(Key.F10, VirtualKeyCode.F10); + KeyCodeToVKCode.Add(Key.F11, VirtualKeyCode.F11); KeyCodeToVKCode.Add(Key.F12, VirtualKeyCode.F12); KeyCodeToVKCode.Add(Key.F2, VirtualKeyCode.F2); KeyCodeToVKCode.Add(Key.F3, VirtualKeyCode.F3); diff --git a/AutoSynthesis/DataTypes/Crafting/Hotkey.cs b/AutoSynthesis/DataTypes/Crafting/Hotkey.cs index 6dea96b..87318dc 100644 --- a/AutoSynthesis/DataTypes/Crafting/Hotkey.cs +++ b/AutoSynthesis/DataTypes/Crafting/Hotkey.cs @@ -11,9 +11,9 @@ class Hotkey { public VirtualKeyCode KeyCode { get; set; } public VirtualKeyCode[] ModKeyCodes { get; set; } - public int TimerInMiliseconds { get; set; } + public int TimerInMilliseconds { get; set; } public string Text { get; set; } - private const int DEFAULT_TIMER = 1; + private const int DEFAULT_TIMER = 50; @@ -21,13 +21,13 @@ public Hotkey(VirtualKeyCode keyCode, int timer = DEFAULT_TIMER) { KeyCode = keyCode; ModKeyCodes = null; - TimerInMiliseconds = timer; + TimerInMilliseconds = timer; } public Hotkey(VirtualKeyCode keyCode, VirtualKeyCode[] modKeyCodes, string text, int timer = DEFAULT_TIMER) { KeyCode = keyCode; ModKeyCodes = modKeyCodes; - TimerInMiliseconds = timer; + TimerInMilliseconds = timer; Text = text; } diff --git a/AutoSynthesis/DataTypes/SettingsContainer.cs b/AutoSynthesis/DataTypes/SettingsContainer.cs index a3f7bd4..ff9b4bb 100644 --- a/AutoSynthesis/DataTypes/SettingsContainer.cs +++ b/AutoSynthesis/DataTypes/SettingsContainer.cs @@ -10,13 +10,15 @@ class SettingsContainer { // contains settings metadata public int CraftCount { get; set; } // Set to 0 if endless craft - public int FoodDuration { get; set; } // if 40 min food, true + public int FoodCount { get; set; } + public int FoodDuration { get; set; } public bool CollectableCraft { get; set; } public int StartingFoodTime { get; set; } public int StartingSyrupTime { get; set; } public int StartingDelay { get; set; } + public int EndingDelay { get; set; } - public SettingsContainer(int craftCount, bool collectableCraft, int foodDuration, int startingFoodTime, int startingSyrupTime, int startingDelay) + public SettingsContainer(int craftCount, int foodCount, bool collectableCraft, int foodDuration, int startingFoodTime, int startingSyrupTime, int startingDelay, int endingDelay) { if (craftCount < 0) { @@ -24,11 +26,13 @@ public SettingsContainer(int craftCount, bool collectableCraft, int foodDuration } CraftCount = craftCount; + FoodCount = foodCount; CollectableCraft = collectableCraft; FoodDuration = foodDuration; StartingFoodTime = startingFoodTime; StartingSyrupTime = startingSyrupTime; StartingDelay = startingDelay; + EndingDelay = endingDelay; } } } diff --git a/AutoSynthesis/MainWindow.xaml b/AutoSynthesis/MainWindow.xaml index f476796..e51354a 100644 --- a/AutoSynthesis/MainWindow.xaml +++ b/AutoSynthesis/MainWindow.xaml @@ -1,170 +1,197 @@  + x:Class="AutoSynthesis.MainWindow" + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:local="clr-namespace:AutoSynthesis" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + x:Name="window" + Title="AutoSynthesis" + HorizontalAlignment="Center" + VerticalAlignment="Center" + Closing="Window_Closing" + Deactivated="Window_Deactivated" + Icon="Resources/Icon/Icon.png" + ResizeMode="CanMinimize" + SizeToContent="WidthAndHeight" + mc:Ignorable="d"> - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + - + - - - - - - - + + + + + + + - + - - - - - - - - - - - - - + + + + + + + + + + + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + - + + + + + + - + + -