CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

GameTextManager.cs (2451B)


      1 //
      2 // Copyright 2020 Electronic Arts Inc.
      3 //
      4 // The Command & Conquer Map Editor and corresponding source code is free 
      5 // software: you can redistribute it and/or modify it under the terms of 
      6 // the GNU General Public License as published by the Free Software Foundation, 
      7 // either version 3 of the License, or (at your option) any later version.
      8 
      9 // The Command & Conquer Map Editor and corresponding source code is distributed 
     10 // in the hope that it will be useful, but with permitted additional restrictions 
     11 // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT 
     12 // distributed with this program. You should have received a copy of the 
     13 // GNU General Public License along with permitted additional restrictions 
     14 // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
     15 using System.Collections.Generic;
     16 using System.IO;
     17 using System.Text;
     18 
     19 namespace MobiusEditor.Utility
     20 {
     21     public class GameTextManager
     22     {
     23         private readonly Dictionary<string, string> gameText = new Dictionary<string, string>();
     24 
     25         public string this[string textId] => gameText.TryGetValue(textId, out string text) ? text : textId;
     26 
     27         public GameTextManager(MegafileManager megafileManager, string gameTextFile)
     28         {
     29             using (var stream = megafileManager.Open(gameTextFile))
     30             using (var reader = new BinaryReader(stream))
     31             using (var unicodeReader = new BinaryReader(stream, Encoding.Unicode))
     32             using (var asciiReader = new BinaryReader(stream, Encoding.ASCII))
     33             {
     34                 var numStrings = reader.ReadUInt32();
     35                 var stringSizes = new (uint textSize, uint idSize)[numStrings];
     36                 var strings = new string[numStrings];
     37 
     38                 for (var i = 0; i < numStrings; ++i)
     39                 {
     40                     reader.ReadUInt32();
     41                     stringSizes[i] = (reader.ReadUInt32(), reader.ReadUInt32());
     42                 }
     43 
     44                 for (var i = 0; i < numStrings; ++i)
     45                 {
     46                     strings[i] = new string(unicodeReader.ReadChars((int)stringSizes[i].textSize));
     47                 }
     48 
     49                 for (var i = 0; i < numStrings; ++i)
     50                 {
     51                     var textId = new string(asciiReader.ReadChars((int)stringSizes[i].idSize));
     52                     gameText[textId] = strings[i];
     53                 }
     54             }
     55         }
     56     }
     57 }