Files
@ 4778889395e8
Branch filter:
Location: ATITD-Tools/Desert-Paint-Codex/ViewModels/RecipeGeneratorViewModel.cs - annotation
4778889395e8
15.3 KiB
text/x-csharp
Change the displayed permeutation count to display using locale-specific number formatting (with comma / dot separators). Fix a crash when starting a new round of recipe generation after recipe generation completed during a previous run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 4778889395e8 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 4778889395e8 4778889395e8 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 4778889395e8 4778889395e8 4778889395e8 4778889395e8 4778889395e8 4778889395e8 4778889395e8 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 4778889395e8 40eaee10ae56 4778889395e8 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 40eaee10ae56 | using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;
using Avalonia.Threading;
using DesertPaintCodex.Models;
using DesertPaintCodex.Services;
using ReactiveUI;
namespace DesertPaintCodex.ViewModels
{
public class RecipeGeneratorViewModel : ViewModelBase
{
private const long UpdateInterval = 200; // ms
private const long SaveInterval = 30000; // ms
private const string PaintStateFile = "dp_generator_state";
private const string RibbonStateFile = "dp_generator_ribbon_state";
private int _selectedView = 0;
public int SelectedView { get => _selectedView; private set => this.RaiseAndSetIfChanged(ref _selectedView, value); }
private float _progress = 0;
public float Progress { get => _progress; private set => this.RaiseAndSetIfChanged(ref _progress, value); }
private string _permutationCount = "0";
public string PermutationCount { get => _permutationCount; private set => this.RaiseAndSetIfChanged(ref _permutationCount, value); }
private DateTime _mostRecentTime;
public DateTime MostRecentTime { get => _mostRecentTime; private set => this.RaiseAndSetIfChanged(ref _mostRecentTime, value); }
private GeneratorRecipe? _mostRecentRecipe;
public GeneratorRecipe? MostRecentRecipe { get => _mostRecentRecipe; private set => this.RaiseAndSetIfChanged(ref _mostRecentRecipe, value); }
private int _productIndex = 0;
public int ProductIndex { get => _productIndex; private set => this.RaiseAndSetIfChanged(ref _productIndex, value); }
private int _maxReagents;
public int MaxReagents { get => _maxReagents; private set => this.RaiseAndSetIfChanged(ref _maxReagents, value); }
private int _maxConcentration;
public int MaxConcentration { get => _maxConcentration; private set => this.RaiseAndSetIfChanged(ref _maxConcentration, value); }
private int _fullQuantity;
public int FullQuantity { get => _fullQuantity; private set => this.RaiseAndSetIfChanged(ref _fullQuantity, value); }
private int _fullQuantityDepth;
public int FullQuantityDepth { get => _fullQuantityDepth; private set => this.RaiseAndSetIfChanged(ref _fullQuantityDepth, value); }
private int _maxConcentrationMax;
public int MaxConcentrationMax { get => _maxConcentrationMax; private set => this.RaiseAndSetIfChanged(ref _maxConcentrationMax, value); }
private int _fullQuantitynMax;
public int FullQuantityMax { get => _fullQuantitynMax; private set => this.RaiseAndSetIfChanged(ref _fullQuantitynMax, value); }
#region Flags
private bool _isPaused = false;
public bool IsPaused { get => _isPaused; private set => this.RaiseAndSetIfChanged(ref _isPaused, value); }
private bool _isInProgress = false;
public bool IsInProgress { get => _isInProgress; private set => this.RaiseAndSetIfChanged(ref _isInProgress, value); }
private bool _isRunning = false;
public bool IsRunning { get => _isRunning; private set => this.RaiseAndSetIfChanged(ref _isRunning, value); }
private bool _canStart = false;
public bool CanStart { get => _canStart; private set => this.RaiseAndSetIfChanged(ref _canStart, value); }
private bool _canClear = false;
public bool CanClear { get => _canClear; private set => this.RaiseAndSetIfChanged(ref _canClear, value); }
#endregion // Flags
#region Collections
public ObservableCollection<Reagent> Reagents { get; } = new();
public ObservableCollection<GeneratorRecipe> AllRecipes { get; } = new();
#endregion // Collections
private readonly RecipeGenerator _generator;
private DateTime _lastSave = DateTime.Now;
private readonly PlayerProfile _profile;
private uint _minConcentration;
private readonly ConcurrentQueue<PaintRecipe> _pendingNewRecipes = new();
private volatile int _newRecipeCount;
private volatile bool _updatesAvailable;
private readonly DispatcherTimer _updateTimer;
private bool _ribbonMode;
private bool _unsavedRecipes;
private bool _saving;
public RecipeGeneratorViewModel()
{
List<string> reagentNames = ReagentService.Names;
foreach (string name in reagentNames)
{
Reagents.Add(ReagentService.GetReagent(name));
}
SettingsService.Get("Generator.RibbonMode", out bool ribbonMode, false);
ProductIndex = ribbonMode ? 1 : 0;
_ribbonMode = ribbonMode;
PlayerProfile? profile = ProfileManager.CurrentProfile;
Debug.Assert(profile != null);
_profile = profile;
_profile.LoadRecipes();
_generator = new RecipeGenerator();
_generator.Progress += OnProgress;
_generator.NewRecipe += OnNewRecipe;
_generator.Finished += OnGeneratorStopped;
SettingsService.Get("Generator.Logging", out bool logGenerator, false);
if (logGenerator)
{
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
_generator.Log = System.IO.Path.Combine(logDir, "dpl_generator.txt");
}
if (ribbonMode)
{
InitStateForRibbons();
}
else
{
InitStateForPaint();
}
_updateTimer = new DispatcherTimer {Interval = TimeSpan.FromMilliseconds(UpdateInterval)};
_updateTimer.Tick += Update;
this.WhenAnyValue(x => x.ProductIndex).Subscribe(_ => ChangeMode());
}
public void Start()
{
IsInProgress = true;
IsRunning = true;
ReagentService.SaveProfileReagents(_profile.ReagentFile);
SaveSettings(_ribbonMode ? "Ribbon" : "Paint");
_updateTimer.Start();
SelectedView = 1;
_generator.BeginRecipeGeneration(
_minConcentration,
(uint)MaxConcentration,
1,
(uint)MaxReagents,
(uint)FullQuantityDepth,
(uint)FullQuantity);
}
public void End()
{
IsPaused = false;
IsRunning = false;
IsInProgress = false;
_updateTimer.Stop();
_profile.SaveRecipes();
CanClear = true;
}
public void Pause()
{
IsPaused = true;
IsRunning = false;
_updateTimer.Stop();
_generator.Stop();
}
public void Resume()
{
IsPaused = false;
IsRunning = true;
_updateTimer.Start();
SelectedView = 1;
_generator.ResumeRecipeGeneration();
}
public async Task Clear()
{
bool result = await ShowYesNoBox("Clear Recipes", "Are you sure you want to clear your recipes?");
if (result)
{
CanClear = false;
if (_ribbonMode)
{
_profile.ClearRibbonRecipes();
}
else
{
_profile.ClearPaintRecipes();
}
foreach (GeneratorRecipe recipe in AllRecipes)
{
recipe.ClearRecipe();
}
_generator.Reset();
}
}
private void InitStateForPaint()
{
MaxConcentrationMax = 20;
FullQuantityMax = 20;
InitState("Paint", PaintRecipe.PaintRecipeMinConcentration, _profile.Recipes, PaintStateFile);
}
private void InitStateForRibbons()
{
MaxConcentrationMax = 100;
FullQuantityMax = 100;
InitState("Ribbon", PaintRecipe.RibbonRecipeMinConcentration, _profile.RibbonRecipes, RibbonStateFile);
}
private void InitState(string mode, uint minConcentration, Dictionary<string, PaintRecipe> recipes, string stateFileName)
{
_minConcentration = minConcentration;
_generator.InitRecipes(recipes);
string stateFile = System.IO.Path.Combine(_profile.Directory, stateFileName);
if (System.IO.File.Exists(stateFile))
{
_generator.LoadState(stateFile);
// Always set these, or the values will be invalid
MaxReagents = (int)_generator.MaxReagents;
MaxConcentration = (int)_generator.MaxConcentration;
FullQuantity = (int)_generator.FullQuantity;
FullQuantityDepth = (int)_generator.FullQuantityDepth;
if (_generator.CanResume)
{
IsInProgress = true;
IsPaused = true;
SaveSettings(mode);
}
else
{
IsInProgress = false;
IsPaused = false;
CanClear = true;
}
}
else if (mode == "Paint")
{
LoadPaintSettings();
}
else
{
LoadRibbonSettings();
}
AllRecipes.Clear();
foreach (PaintColor color in PaletteService.Colors)
{
GeneratorRecipe genRecipe = new(color);
if (recipes.TryGetValue(color.Name, out PaintRecipe? recipe)) // && recipe.IsValidForConcentration(minConcentration)) // This check is redundant, since they're checked when loading.
{
genRecipe.DraftRecipe(recipe);
}
AllRecipes.Add(genRecipe);
}
}
#region Event Handlers
private void OnProgress(object? sender, EventArgs args)
{
_newRecipeCount = (int)_generator.RecipeCount;
_updatesAvailable = true;
}
private void OnNewRecipe(object? sender, NewRecipeEventArgs args)
{
PaintRecipe recipe = new(args.Recipe);
_pendingNewRecipes.Enqueue(recipe);
_updatesAvailable = true;
}
private void OnGeneratorStopped(object? sender, EventArgs args)
{
SaveState();
if (_saving)
{
_generator.ResumeRecipeGeneration();
_lastSave = DateTime.Now;
_saving = false;
return;
}
if (IsPaused) return;
End();
}
private void Update(object? sender, EventArgs e)
{
if (!_updatesAvailable) return;
if (_saving) return;
_updatesAvailable = false;
DateTime now = DateTime.Now;
// Update test count.
PermutationCount = $"{_newRecipeCount:n0}";
// Pull in new recipes.
if (!_pendingNewRecipes.IsEmpty)
{
GeneratorRecipe? lastRecipe = null;
while (_pendingNewRecipes.TryDequeue(out PaintRecipe? newRecipe))
{
string recipeColor = PaletteService.FindNearest(newRecipe.ReactedColor);
foreach (GeneratorRecipe recipe in AllRecipes)
{
if (recipe.Color.Name != recipeColor) continue;
recipe.DraftRecipe(newRecipe);
lastRecipe = recipe;
if (_ribbonMode)
{
_profile.SetRibbonRecipe(recipeColor, newRecipe);
}
else
{
_profile.SetRecipe(recipeColor, newRecipe);
}
break;
}
}
// If at least one recipe was processed, let's check to see if it's time to save, and
// highlight that recipe.
if (lastRecipe != null)
{
_unsavedRecipes = true;
MostRecentRecipe = lastRecipe;
MostRecentTime = now;
}
}
// Save if it is time.
if (!((now - _lastSave).TotalMilliseconds > SaveInterval)) return;
if (_unsavedRecipes)
{
_profile.SaveRecipes();
_unsavedRecipes = false;
}
_saving = true;
_generator.Stop();
}
#endregion
private void SavePaintSettings()
{
SaveSettings("Paint");
}
private void LoadPaintSettings()
{
LoadSettings("Paint", 5, 20, 20, 4);
}
private void SaveRibbonSettings()
{
SaveSettings("Ribbon");
}
private void LoadRibbonSettings()
{
LoadSettings("Ribbon", 5, 75, 75, 4);
}
private void SaveSettings(string mode)
{
SettingsService.Set($"Generator.{mode}.MaxReagents", MaxReagents);
SettingsService.Set($"Generator.{mode}.MaxConcentration", MaxConcentration);
SettingsService.Set($"Generator.{mode}.FullQuantity", FullQuantity);
SettingsService.Set($"Generator.{mode}.FulLQuantityDepth", FullQuantityDepth);
}
private void LoadSettings(string mode, int defaultMaxReagents, int defaultMaxConcentration,
int defaultFullQuantity, int defaultFullQuantityDepth)
{
SettingsService.Get($"Generator.{mode}.MaxReagents", out int maxReagents, defaultMaxReagents);
SettingsService.Get($"Generator.{mode}.MaxConcentration", out int maxConcentration, defaultMaxConcentration);
SettingsService.Get($"Generator.{mode}.FullQuantity", out int fullQuantity, defaultFullQuantity);
SettingsService.Get($"Generator.{mode}.FullQuantityDepth", out int fullQuantityDepth, defaultFullQuantityDepth);
MaxReagents = maxReagents;
MaxConcentration = maxConcentration;
FullQuantity = fullQuantity;
FullQuantityDepth = fullQuantityDepth;
}
private void SaveState()
{
_generator.SaveState(System.IO.Path.Combine(_profile.Directory,
_ribbonMode ? RibbonStateFile : PaintStateFile));
}
private void ChangeMode()
{
if (ProductIndex == 0)
{
if (!_ribbonMode) return;
_ribbonMode = false;
InitStateForPaint();
}
else
{
if (_ribbonMode) return;
_ribbonMode = true;
InitStateForRibbons();
}
}
}
}
|