diff --git a/PckStudio.Core/DLC/DLCManager.cs b/PckStudio.Core/DLC/DLCManager.cs index 87de4dac..83f51e39 100644 --- a/PckStudio.Core/DLC/DLCManager.cs +++ b/PckStudio.Core/DLC/DLCManager.cs @@ -40,8 +40,7 @@ namespace PckStudio.Core.DLC /// public string PreferredLanguage { get; private set; } - private readonly IDictionary _openPackages = new Dictionary(); - private readonly IDictionary _localisationFiles = new Dictionary(); + private readonly DLCPackageRegistry _packageRegistry = new DLCPackageRegistry(); private readonly Random _rng = new Random(); private ByteOrder _byteOrder; private ConsolePlatform _platform; @@ -86,8 +85,7 @@ namespace PckStudio.Core.DLC LOCFile localisation = new LOCFile(); localisation.AddLanguage(PreferredLanguage); localisation.AddLocKey(PACKAGE_DISPLAYNAME_ID, name); - _localisationFiles.Add(identifier, localisation); - _openPackages.Add(identifier, package); + _packageRegistry.RegisterPackage(identifier, package, localisation); return package; } @@ -121,8 +119,8 @@ namespace PckStudio.Core.DLC return new UnknownDLCPackage(fileInfo.Name, pckFile); } - if (_openPackages.ContainsKey(identifier)) - return _openPackages[identifier]; + if (_packageRegistry.ContainsPackage(identifier)) + return _packageRegistry[identifier]; LOCFile localisation = pckFile.GetAssetsByType(PckAssetType.LocalisationFile).FirstOrDefault()?.GetData(new LOCFileReader()); if (localisation is null) @@ -131,15 +129,14 @@ namespace PckStudio.Core.DLC IDLCPackage package = ScanForPackageType(fileInfo, identifier, pckFile, localisation, fileReader); if (package.GetDLCPackageType() != DLCPackageType.Invalid) { - _localisationFiles.Add(identifier, localisation); - _openPackages.Add(identifier, package); + _packageRegistry.RegisterPackage(identifier, package, localisation); } return package; } internal LOCFile GetLocalisation(int identifier) { - return _localisationFiles.ContainsKey(identifier) ? _localisationFiles[identifier] : default; + return _packageRegistry.ContainsPackage(identifier) ? _packageRegistry.GetLocalisation(identifier) : default; } private bool IsValidPckFile(FileInfo fileInfo) diff --git a/PckStudio.Core/DLC/DLCPackageRegistry.cs b/PckStudio.Core/DLC/DLCPackageRegistry.cs new file mode 100644 index 00000000..c38cc740 --- /dev/null +++ b/PckStudio.Core/DLC/DLCPackageRegistry.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OMI.Formats.Languages; +using PckStudio.Core.Interfaces; + +namespace PckStudio.Core.DLC +{ + internal sealed class DLCPackageRegistry + { + private readonly IDictionary _openPackages = new Dictionary(); + private readonly IDictionary _localisationFiles = new Dictionary(); + + public IDLCPackage this[int id] => _openPackages[id]; + + public bool RegisterPackage(int identifier, IDLCPackage package, LOCFile localisation) + { + if (_openPackages.ContainsKey(identifier)) + return false; + if (_localisationFiles.ContainsKey(identifier)) + _localisationFiles.Remove(identifier); + _localisationFiles.Add(identifier, localisation); + _openPackages.Add(identifier, package); + return true; + } + + internal bool ContainsPackage(int identifier) => _openPackages.ContainsKey(identifier) && _localisationFiles.ContainsKey(identifier); + + internal LOCFile GetLocalisation(int identifier) => _localisationFiles[identifier]; + } +} diff --git a/PckStudio.Core/PckStudio.Core.csproj b/PckStudio.Core/PckStudio.Core.csproj index 6cff8f3b..44a712b3 100644 --- a/PckStudio.Core/PckStudio.Core.csproj +++ b/PckStudio.Core/PckStudio.Core.csproj @@ -71,6 +71,7 @@ +