Started on splitting off classic strings manager

This commit is contained in:
Nyerguds 2023-05-16 21:25:03 +02:00
parent 1daefa275b
commit c245c7a9be
10 changed files with 123 additions and 54 deletions

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28307.1231
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CnCTDRAMapEditor", "CnCTDRAMapEditor\CnCTDRAMapEditor.csproj", "{397CEF00-8930-4EC8-B15F-F7CF7193FB22}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CCFileSystem", "CCFileSystem\CCFileSystem.csproj", "{925C0B51-E256-4A5C-8B01-F46AA90982D5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,18 @@ Global
{397CEF00-8930-4EC8-B15F-F7CF7193FB22}.Release|Any CPU.Build.0 = Release|Any CPU
{397CEF00-8930-4EC8-B15F-F7CF7193FB22}.Release|x64.ActiveCfg = Release|x64
{397CEF00-8930-4EC8-B15F-F7CF7193FB22}.Release|x64.Build.0 = Release|x64
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Debug|x64.ActiveCfg = Debug|x64
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Debug|x64.Build.0 = Debug|x64
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Gold|Any CPU.ActiveCfg = Debug|Any CPU
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Gold|Any CPU.Build.0 = Debug|Any CPU
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Gold|x64.ActiveCfg = Debug|x64
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Gold|x64.Build.0 = Debug|x64
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Release|Any CPU.Build.0 = Release|Any CPU
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Release|x64.ActiveCfg = Release|x64
{925C0B51-E256-4A5C-8B01-F46AA90982D5}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -325,6 +325,7 @@
<Compile Include="Interface\IFeedBackHandler.cs" />
<Compile Include="Interface\IArchiveManager.cs" />
<Compile Include="Interface\IGamePlugin.cs" />
<Compile Include="Interface\IGameTextManager.cs" />
<Compile Include="Interface\INamedType.cs" />
<Compile Include="Interface\ITeamColor.cs" />
<Compile Include="Interface\ITechno.cs" />
@ -598,6 +599,7 @@
<Compile Include="Utility\CRC.cs" />
<Compile Include="Utility\ExtensionMethods.cs" />
<Compile Include="Utility\GameTextManager.cs" />
<Compile Include="Utility\GameTextManagerClassic.cs" />
<Compile Include="Utility\GeneralUtils.cs" />
<Compile Include="Utility\GenericBooleanTypeConverter.cs" />
<Compile Include="Utility\ImageUtils.cs" />

View File

@ -130,7 +130,7 @@ namespace MobiusEditor
public static TextureManager TheTextureManager;
public static TilesetManager TheTilesetManager;
public static ITeamColorManager TheTeamColorManager;
public static GameTextManager TheGameTextManager;
public static IGameTextManager TheGameTextManager;
public static readonly string RootSaveDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), @"CnCRemastered\Local_Custom_Maps");

View File

@ -12,5 +12,6 @@ namespace MobiusEditor.Interface
bool LoadArchive(string archivePath);
bool FileExists(string path);
Stream OpenFile(string path);
void Reset(GameType gameType);
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MobiusEditor.Interface
{
public interface IGameTextManager
{
String this[string key] { get; }
void Reset(GameType gameType);
}
}

View File

@ -1299,14 +1299,16 @@ namespace MobiusEditor
private static IGamePlugin LoadNewPlugin(GameType gameType, bool isTdMegaMap, string[] modPaths, bool noImage)
{
// Resetting to a specific game type will take care of classic mode.
Globals.TheGameTextManager.Reset(gameType);
Globals.TheArchiveManager.Reset(gameType);
Globals.TheTextureManager.ExpandModPaths = modPaths;
Globals.TheTextureManager.Reset();
Globals.TheTilesetManager.ExpandModPaths = modPaths;
Globals.TheTilesetManager.Reset();
Globals.TheTeamColorManager.ExpandModPaths = modPaths;
IGamePlugin plugin = null;
// Reset will take care of the colours in classic mode.
Globals.TheTeamColorManager.Reset(gameType);
IGamePlugin plugin = null;
if (gameType == GameType.TiberianDawn)
{
Globals.TheTeamColorManager.Load(@"DATA\XML\CNCTDTEAMCOLORS.XML");

View File

@ -19,40 +19,54 @@ using System.Text;
namespace MobiusEditor.Utility
{
public class GameTextManager
public class GameTextManager: IGameTextManager
{
private IArchiveManager fileManager;
private Dictionary<GameType, string> gameTextPaths;
private readonly Dictionary<string, string> gameText = new Dictionary<string, string>();
public string this[string textId] => gameText.TryGetValue(textId, out string text) ? text : textId;
public GameTextManager(IArchiveManager megafileManager, string gameTextFile)
public void Reset(GameType gameType)
{
using (var stream = megafileManager.OpenFile(gameTextFile))
using (var reader = new BinaryReader(stream))
using (var unicodeReader = new BinaryReader(stream, Encoding.Unicode))
using (var asciiReader = new BinaryReader(stream, Encoding.ASCII))
// Do nothing.
if (gameTextPaths.TryGetValue(gameType, out string gameTextFile))
{
var numStrings = reader.ReadUInt32();
var stringSizes = new (uint textSize, uint idSize)[numStrings];
var strings = new string[numStrings];
for (var i = 0; i < numStrings; ++i)
using (var stream = fileManager.OpenFile(gameTextFile))
using (var reader = new BinaryReader(stream))
using (var unicodeReader = new BinaryReader(stream, Encoding.Unicode))
using (var asciiReader = new BinaryReader(stream, Encoding.ASCII))
{
reader.ReadUInt32();
stringSizes[i] = (reader.ReadUInt32(), reader.ReadUInt32());
}
var numStrings = reader.ReadUInt32();
var stringSizes = new (uint textSize, uint idSize)[numStrings];
var strings = new string[numStrings];
for (var i = 0; i < numStrings; ++i)
{
strings[i] = new string(unicodeReader.ReadChars((int)stringSizes[i].textSize));
}
for (var i = 0; i < numStrings; ++i)
{
reader.ReadUInt32();
stringSizes[i] = (reader.ReadUInt32(), reader.ReadUInt32());
}
for (var i = 0; i < numStrings; ++i)
{
var textId = new string(asciiReader.ReadChars((int)stringSizes[i].idSize));
gameText[textId] = strings[i];
for (var i = 0; i < numStrings; ++i)
{
strings[i] = new string(unicodeReader.ReadChars((int)stringSizes[i].textSize));
}
for (var i = 0; i < numStrings; ++i)
{
var textId = new string(asciiReader.ReadChars((int)stringSizes[i].idSize));
gameText[textId] = strings[i];
}
}
}
}
public GameTextManager(IArchiveManager fileManager, Dictionary<GameType, string> gameTextPaths)
{
this.fileManager = fileManager;
this.gameTextPaths = gameTextPaths;
}
}
}

View File

@ -76,6 +76,11 @@ namespace MobiusEditor.Utility
return null;
}
public void Reset(GameType gameType)
{
// Do nothing.
}
public IEnumerator<string> GetEnumerator()
{
return filenames.GetEnumerator();

View File

@ -162,7 +162,7 @@ namespace MobiusEditor.Utility
// First attempt to find the texture in an archive
if (tga == null)
{
using (var fileStream = megafileManager.OpenFile(archivePath))
using (Stream fileStream = megafileManager.OpenFile(archivePath))
{
LoadTgaFromZipFileStream(fileStream, name, ref tga, ref metadata);
}
@ -170,7 +170,7 @@ namespace MobiusEditor.Utility
// Next attempt to load a standalone file
if (tga == null)
{
using (var fileStream = megafileManager.OpenFile(filename))
using (Stream fileStream = megafileManager.OpenFile(filename))
{
// megafileManager.OpenFile might return null if not found, so always check on this.
// The Load???FromFileStream functions do this check internally.
@ -182,7 +182,7 @@ namespace MobiusEditor.Utility
if (tga != null)
{
var meta = Path.ChangeExtension(filename, ".meta");
using (var metaStream = megafileManager.OpenFile(meta))
using (Stream metaStream = megafileManager.OpenFile(meta))
{
if (metaStream != null)
{
@ -309,7 +309,7 @@ namespace MobiusEditor.Utility
}
if (bitmap == null)
{
using (var fileStream = megafileManager.OpenFile(ddsFilename))
using (Stream fileStream = megafileManager.OpenFile(ddsFilename))
{
bitmap = LoadDDSFromFileStream(fileStream);
}

View File

@ -61,37 +61,54 @@ namespace MobiusEditor.Utility
}
if (xmlDoc == null)
{
xmlDoc = new XmlDocument();
xmlDoc.Load(megafileManager.OpenFile(xmlPath));
}
foreach (XmlNode fileNode in xmlDoc.SelectNodes("TilesetFiles/File"))
{
string xmlFile = Path.Combine(Path.GetDirectoryName(xmlPath), fileNode.InnerText);
XmlDocument fileXmlDoc = null;
if (ExpandModPaths != null && ExpandModPaths.Length > 0)
using (Stream xmlStream = megafileManager.OpenFile(xmlPath))
{
for (int i = 0; i < ExpandModPaths.Length; ++i)
if (xmlStream != null)
{
string modXmlPath = Path.Combine(ExpandModPaths[i], xmlFile);
if (modXmlPath != null && File.Exists(modXmlPath))
{
fileXmlDoc = new XmlDocument();
fileXmlDoc.Load(modXmlPath);
break;
}
xmlDoc = new XmlDocument();
xmlDoc.Load(xmlStream);
}
}
if (fileXmlDoc == null)
}
if (xmlDoc != null)
{
foreach (XmlNode fileNode in xmlDoc.SelectNodes("TilesetFiles/File"))
{
fileXmlDoc = new XmlDocument();
fileXmlDoc.Load(megafileManager.OpenFile(xmlFile));
}
foreach (XmlNode tilesetNode in fileXmlDoc.SelectNodes("Tilesets/TilesetTypeClass"))
{
var tileset = new Tileset(textureManager);
tileset.Load(tilesetNode.OuterXml, texturesPath);
tilesets[tilesetNode.Attributes["name"].Value] = tileset;
string xmlFile = Path.Combine(Path.GetDirectoryName(xmlPath), fileNode.InnerText);
XmlDocument fileXmlDoc = null;
if (ExpandModPaths != null && ExpandModPaths.Length > 0)
{
for (int i = 0; i < ExpandModPaths.Length; ++i)
{
string modXmlPath = Path.Combine(ExpandModPaths[i], xmlFile);
if (modXmlPath != null && File.Exists(modXmlPath))
{
fileXmlDoc = new XmlDocument();
fileXmlDoc.Load(modXmlPath);
break;
}
}
}
if (fileXmlDoc == null)
{
using (Stream xmlStream = megafileManager.OpenFile(xmlFile))
{
if (xmlStream != null)
{
fileXmlDoc = new XmlDocument();
fileXmlDoc.Load(xmlStream);
}
}
}
if (fileXmlDoc != null)
{
foreach (XmlNode tilesetNode in fileXmlDoc.SelectNodes("Tilesets/TilesetTypeClass"))
{
var tileset = new Tileset(textureManager);
tileset.Load(tilesetNode.OuterXml, texturesPath);
tilesets[tilesetNode.Attributes["name"].Value] = tileset;
}
}
}
}
}