Move chrome extraction utility to common and reuse code
This commit is contained in:
committed by
Matthias Mailänder
parent
b267374d20
commit
6386e96134
@@ -17,7 +17,7 @@ using OpenRA.FileSystem;
|
||||
|
||||
namespace OpenRA.Mods.Common.UpdateRules
|
||||
{
|
||||
using YamlFileSet = List<(IReadWritePackage, string, List<MiniYamlNodeBuilder>)>;
|
||||
using YamlFileSet = List<(IReadWritePackage Package, string File, List<MiniYamlNodeBuilder> Nodes)>;
|
||||
|
||||
public static class UpdateUtils
|
||||
{
|
||||
@@ -219,19 +219,19 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
var mapRulesNode = yaml.NodeWithKeyOrDefault("Rules");
|
||||
if (mapRulesNode != null)
|
||||
foreach (var f in LoadExternalMapYaml(modData, mapRulesNode.Value, externalFilenames))
|
||||
if (!modRules.Any(m => m.Item1 == f.Item1 && m.Item2 == f.Item2))
|
||||
if (!modRules.Any(m => m.Package == f.Package && m.File == f.File))
|
||||
modRules.Add(f);
|
||||
|
||||
var mapWeaponsNode = yaml.NodeWithKeyOrDefault("Weapons");
|
||||
if (mapWeaponsNode != null)
|
||||
foreach (var f in LoadExternalMapYaml(modData, mapWeaponsNode.Value, externalFilenames))
|
||||
if (!modWeapons.Any(m => m.Item1 == f.Item1 && m.Item2 == f.Item2))
|
||||
if (!modWeapons.Any(m => m.Package == f.Package && m.File == f.File))
|
||||
modWeapons.Add(f);
|
||||
|
||||
var mapSequencesNode = yaml.NodeWithKeyOrDefault("Sequences");
|
||||
if (mapSequencesNode != null)
|
||||
foreach (var f in LoadExternalMapYaml(modData, mapSequencesNode.Value, externalFilenames))
|
||||
if (!modSequences.Any(m => m.Item1 == f.Item1 && m.Item2 == f.Item2))
|
||||
if (!modSequences.Any(m => m.Package == f.Package && m.File == f.File))
|
||||
modSequences.Add(f);
|
||||
}
|
||||
}
|
||||
@@ -298,8 +298,8 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
if (transform == null)
|
||||
yield break;
|
||||
|
||||
foreach (var file in files)
|
||||
foreach (var node in file.Item3)
|
||||
foreach (var (_, _, nodes) in files)
|
||||
foreach (var node in nodes)
|
||||
if (node.Key != null)
|
||||
foreach (var manualStep in ApplyChromeTransformInner(modData, node, transform))
|
||||
yield return manualStep;
|
||||
@@ -310,8 +310,8 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
if (transform == null)
|
||||
yield break;
|
||||
|
||||
foreach (var file in files)
|
||||
foreach (var node in file.Item3)
|
||||
foreach (var (_, _, nodes) in files)
|
||||
foreach (var node in nodes)
|
||||
if (node.Key != null)
|
||||
foreach (var manualStep in transform(modData, node))
|
||||
yield return manualStep;
|
||||
@@ -328,14 +328,14 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
{
|
||||
public static void Save(this YamlFileSet files)
|
||||
{
|
||||
foreach (var file in files)
|
||||
foreach (var (package, file, nodes) in files)
|
||||
{
|
||||
if (file.Item1 == null)
|
||||
if (package == null)
|
||||
continue;
|
||||
|
||||
var textData = Encoding.UTF8.GetBytes(file.Item3.WriteToString());
|
||||
if (!Enumerable.SequenceEqual(textData, file.Item1.GetStream(file.Item2).ReadAllBytes()))
|
||||
file.Item1.Update(file.Item2, textData);
|
||||
var textData = Encoding.UTF8.GetBytes(nodes.WriteToString());
|
||||
if (!Enumerable.SequenceEqual(textData, package.GetStream(file).ReadAllBytes()))
|
||||
package.Update(file, textData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,10 +14,14 @@ using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA.FileSystem;
|
||||
using OpenRA.Mods.Common.UpdateRules;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.UtilityCommands
|
||||
namespace OpenRA.Mods.Common.UtilityCommands
|
||||
{
|
||||
using YamlFileSet = List<(IReadWritePackage Package, string File, List<MiniYamlNodeBuilder> Nodes)>;
|
||||
|
||||
sealed class ExtractChromeStringsCommand : IUtilityCommand
|
||||
{
|
||||
string IUtilityCommand.Name { get { return "--extract-chrome-strings"; } }
|
||||
@@ -51,7 +55,8 @@ namespace OpenRA.UtilityCommands
|
||||
|
||||
var unsortedCandidates = new List<TranslationCandidate>();
|
||||
var groupedCandidates = new Dictionary<HashSet<string>, List<TranslationCandidate>>();
|
||||
var chromeFiles = new List<(string Path, List<MiniYamlNodeBuilder> Nodes)>();
|
||||
|
||||
var yamlSet = new YamlFileSet();
|
||||
|
||||
// Get all translations.
|
||||
foreach (var chrome in layout)
|
||||
@@ -60,7 +65,7 @@ namespace OpenRA.UtilityCommands
|
||||
var chromePath = Path.Combine(chromePackage.Name, chromeName);
|
||||
|
||||
var yaml = MiniYaml.FromFile(chromePath, false).ConvertAll(n => new MiniYamlNodeBuilder(n));
|
||||
chromeFiles.Add((chromePath, yaml));
|
||||
yamlSet.Add(((IReadWritePackage)chromePackage, chromeName, yaml));
|
||||
|
||||
var translationCandidates = new List<TranslationCandidate>();
|
||||
foreach (var node in yaml)
|
||||
@@ -125,7 +130,7 @@ namespace OpenRA.UtilityCommands
|
||||
groupedCandidates[newHash] = new List<TranslationCandidate>() { candidate };
|
||||
}
|
||||
|
||||
// Write to translation and yaml files.
|
||||
// Write to translation files.
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(fluentPath));
|
||||
using (var fluentWriter = new StreamWriter(fluentPath, append: true))
|
||||
{
|
||||
@@ -184,11 +189,7 @@ namespace OpenRA.UtilityCommands
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var chromeFile in chromeFiles)
|
||||
{
|
||||
using (var chromeLayoutWriter = new StreamWriter(chromeFile.Path))
|
||||
chromeLayoutWriter.WriteLine(chromeFile.Nodes.WriteToString());
|
||||
}
|
||||
yamlSet.Save();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user