Skip to content

Commit

Permalink
Fix caching of systems in SystemList.
Browse files Browse the repository at this point in the history
Fix system cache invalidation after config change in SadConsole host app.
  • Loading branch information
highbyte committed Aug 8, 2024
1 parent 86cc41a commit e0a10d0
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using SadConsole.UI;
using SadConsole.UI.Controls;
using SadRogue.Primitives;
using Highbyte.DotNet6502.Systems.Commodore64.Config;
using Microsoft.Extensions.Logging;
using Highbyte.DotNet6502.Utils;
using AutoMapper;

namespace Highbyte.DotNet6502.App.SadConsole.ConfigUI;
public class C64MenuConsole : ControlsConsole
Expand All @@ -16,11 +16,13 @@ public class C64MenuConsole : ControlsConsole
private const int USABLE_HEIGHT = 12;

private readonly SadConsoleHostApp _sadConsoleHostApp;
private readonly IMapper _mapper;
private readonly ILogger _logger;

public C64MenuConsole(SadConsoleHostApp sadConsoleHostApp, ILoggerFactory loggerFactory) : base(CONSOLE_WIDTH, CONSOLE_HEIGHT)
public C64MenuConsole(SadConsoleHostApp sadConsoleHostApp, ILoggerFactory loggerFactory, IMapper mapper) : base(CONSOLE_WIDTH, CONSOLE_HEIGHT)
{
_sadConsoleHostApp = sadConsoleHostApp;
_mapper = mapper;
_logger = loggerFactory.CreateLogger(typeof(C64MenuConsole).Name);

Controls.ThemeColors = SadConsoleUISettings.ThemeColors;
Expand Down Expand Up @@ -240,6 +242,14 @@ private void C64ConfigButton_Click(object sender, EventArgs e)
{
IsDirty = true;
_sadConsoleHostApp.MenuConsole.IsDirty = true;

// Update the system config
_sadConsoleHostApp.UpdateSystemConfig(_sadConsoleHostApp.GetSystemConfig().Result);

//// Update the existing host system config, it is referenced from different objects (thus we cannot replace it with a new one).
//var orgHostSystemConfig = _sadConsoleHostApp.GetHostSystemConfig();
//_mapper.Map(hostSystemConfig, orgHostSystemConfig);

}
};
window.Show(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
Expand Down
13 changes: 12 additions & 1 deletion src/apps/Highbyte.DotNet6502.App.SadConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using Highbyte.DotNet6502.Impl.NAudio;
using Highbyte.DotNet6502.Systems.Logging.InMem;
using AutoMapper;

// Get config file
var builder = new ConfigurationBuilder()
Expand Down Expand Up @@ -50,6 +51,16 @@
{ GenericComputer.SystemName, emulatorConfig.GenericComputerHostConfig}
};


// TODO: Make Automapper configuration more generic, incorporate in classes that need it?
var mapperConfiguration = new MapperConfiguration(
cfg =>
{
cfg.CreateMap<C64HostConfig, C64HostConfig>();
}
);
var mapper = mapperConfiguration.CreateMapper();

// ----------
// Get systems
// ----------
Expand All @@ -68,5 +79,5 @@
// ----------
emulatorConfig.Validate(systemList);

var silkNetHostApp = new SadConsoleHostApp(systemList, loggerFactory, emulatorConfig, hostSystemConfigs, logStore, logConfig);
var silkNetHostApp = new SadConsoleHostApp(systemList, loggerFactory, emulatorConfig, hostSystemConfigs, logStore, logConfig, mapper);
silkNetHostApp.Run();
11 changes: 6 additions & 5 deletions src/apps/Highbyte.DotNet6502.App.SadConsole/SadConsoleHostApp.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AutoMapper;
using Highbyte.DotNet6502.App.SadConsole.ConfigUI;
using Highbyte.DotNet6502.App.SadConsole.SystemSetup;
using Highbyte.DotNet6502.Impl.NAudio;
Expand Down Expand Up @@ -30,7 +31,7 @@ public class SadConsoleHostApp : HostApp<SadConsoleRenderContext, SadConsoleInpu
private readonly DotNet6502InMemLogStore _logStore;
private readonly DotNet6502InMemLoggerConfiguration _logConfig;
private readonly ILoggerFactory _loggerFactory;
//private readonly IMapper _mapper;
private readonly IMapper _mapper;

// --------------------
// Other variables / constants
Expand Down Expand Up @@ -88,8 +89,8 @@ public SadConsoleHostApp(
Dictionary<string, IHostSystemConfig> hostSystemConfigs,
//IWindow window,
DotNet6502InMemLogStore logStore,
DotNet6502InMemLoggerConfiguration logConfig
//IMapper mapper
DotNet6502InMemLoggerConfiguration logConfig,
IMapper mapper

) : base("SadConsole", systemList, hostSystemConfigs, loggerFactory)
{
Expand All @@ -99,7 +100,7 @@ DotNet6502InMemLoggerConfiguration logConfig
_logConfig = logConfig;

_loggerFactory = loggerFactory;
//_mapper = mapper;
_mapper = mapper;
_logger = loggerFactory.CreateLogger(typeof(SadConsoleHostApp).Name);
}

Expand Down Expand Up @@ -225,7 +226,7 @@ public override void OnAfterSelectSystem()
// Create system specific menu console
if (SelectedSystemName == "C64")
{
_systemMenuConsole = new C64MenuConsole(this, _loggerFactory);
_systemMenuConsole = new C64MenuConsole(this, _loggerFactory, _mapper);
_systemMenuConsole.Position = (MENU_POSITION_X, _menuConsole.Height);
_sadConsoleScreen.Children.Add(_systemMenuConsole);
}
Expand Down
6 changes: 5 additions & 1 deletion src/libraries/Highbyte.DotNet6502.Systems/HostApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,13 @@ public void Stop()
_systemRunner!.Cleanup();
_systemRunner = default!;

EmulatorState = EmulatorState.Uninitialized;

// Make sure the cached System instance is removed, so it's created again next time (starting fresh).
_systemList.InvalidateSystemCache(SelectedSystemName);

OnAfterStop();

EmulatorState = EmulatorState.Uninitialized;
_logger.LogInformation($"System stopped: {_selectedSystemName}");
}
public virtual void OnAfterStop() { }
Expand Down
20 changes: 17 additions & 3 deletions src/libraries/Highbyte.DotNet6502.Systems/SystemList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ private async Task BuildAndCacheSystem(string systemName, string configurationVa

var cacheKey = BuildSystemCacheKey(systemName, configurationVariant);

if (_systemsCache.ContainsKey(cacheKey))
throw new DotNet6502Exception($"Internal error. Configuration for system {cacheKey} is already in cache.");

if (!await IsValidConfig(systemName, configurationVariant))
throw new DotNet6502Exception($"Internal error. Configuration for system {cacheKey} is invalid.");

if (_systemsCache.ContainsKey(cacheKey))
_systemsCache.Remove(cacheKey);

var systemConfig = await GetCurrentSystemConfig(systemName, configurationVariant);
var system = _systemConfigurers[systemName].BuildSystem(systemConfig);
Expand Down Expand Up @@ -167,7 +168,9 @@ public async Task<SystemRunner> BuildSystemRunner(
if (_getAudioHandlerContext == null)
throw new DotNet6502Exception("AudioHandlerContext has not been initialized. Call InitContext to initialize.");

await BuildAndCacheSystem(systemName, configurationVariant);
var cacheKey = BuildSystemCacheKey(systemName, configurationVariant);
if (!_systemsCache.ContainsKey(cacheKey))
await BuildAndCacheSystem(systemName, configurationVariant);

var system = await GetSystem(systemName, configurationVariant);
var systemConfig = await GetCurrentSystemConfig(systemName, configurationVariant);
Expand All @@ -193,6 +196,8 @@ public async Task<ISystemConfig> GetCurrentSystemConfig(string systemName, strin

public void ChangeCurrentSystemConfig(string systemName, ISystemConfig systemConfig, string configurationVariant = DEFAULT_CONFIGURATION_VARIANT)
{
// Make sure any cached version of the system is invalidated so it'll be re-recreated with new config.
InvalidateSystemCache(systemName, configurationVariant);
CacheSystemConfig(systemName, configurationVariant, systemConfig);
}

Expand Down Expand Up @@ -226,4 +231,13 @@ private string BuildSystemCacheKey(string systemName, string configurationVarian
{
return $"{systemName}_{configurationVariant}";
}

public void InvalidateSystemCache(string systemName, string configurationVariant = DEFAULT_CONFIGURATION_VARIANT)
{
var cacheKey = BuildSystemCacheKey(systemName, configurationVariant);
if (_systemsCache.ContainsKey(cacheKey))
{
_systemsCache.Remove(cacheKey);
}
}
}

0 comments on commit e0a10d0

Please sign in to comment.