Core - Add 'DLCPackageRegistry.cs'

This commit is contained in:
miku-666
2025-11-15 19:09:09 +01:00
parent e2df19ccf9
commit 8a3c45260d
3 changed files with 40 additions and 9 deletions

View File

@@ -40,8 +40,7 @@ namespace PckStudio.Core.DLC
/// </summary>
public string PreferredLanguage { get; private set; }
private readonly IDictionary<int, IDLCPackage> _openPackages = new Dictionary<int, IDLCPackage>();
private readonly IDictionary<int, LOCFile> _localisationFiles = new Dictionary<int, LOCFile>();
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)

View File

@@ -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<int, IDLCPackage> _openPackages = new Dictionary<int, IDLCPackage>();
private readonly IDictionary<int, LOCFile> _localisationFiles = new Dictionary<int, LOCFile>();
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];
}
}

View File

@@ -71,6 +71,7 @@
<Compile Include="DLC\DLCMashUpPackage.cs" />
<Compile Include="DLC\DLCMiniGamePackage.cs" />
<Compile Include="DLC\DLCPackage.cs" />
<Compile Include="DLC\DLCPackageRegistry.cs" />
<Compile Include="DLC\DLCSkinPackage.cs" />
<Compile Include="DLC\DLCTexturePackage.cs" />
<Compile Include="DLC\UnknownDLCPackage.cs" />