Skip to content

Commit

Permalink
Merge branch 'release/v0.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bux committed Oct 4, 2018
2 parents 027e7d0 + 53df288 commit 8041dad
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@
alt="BIF Thread" />
</a>
<a href="https://github.com/bux/tabler/releases">
<img src="http://img.shields.io/badge/Version-0.7.0-green.svg?style=flat"
<img src="http://img.shields.io/badge/Version-0.8.0-green.svg?style=flat"
alt="Version" />
</a>
<a href="https://github.com/bux/tabler/issues">
Expand Down
16 changes: 6 additions & 10 deletions README_RU.md
@@ -1,5 +1,5 @@
<p align="center">
<img src="https://github.com/bux578/tabler/blob/master/tabler/Content/Icon-256.png"
<img src="https://github.com/bux/tabler/blob/master/tabler/Content/Icon-256.png"
width="192" />
</p>
<h1 align="center">Arma 3 Помошник Перевода</h1>
Expand All @@ -8,16 +8,12 @@
<img src="https://img.shields.io/badge/BIF-Тема-lightgrey.svg?style=flat"
alt="BIF Тема" />
</a>
<a href="https://github.com/bux578/tabler/releases">
<img src="http://img.shields.io/badge/Версия-0.6.0-green.svg?style=flat"
<a href="https://github.com/bux/tabler/releases">
<img src="http://img.shields.io/badge/Версия-0.8.0-green.svg?style=flat"
alt="Версия" />
</a>
<a href="https://github.com/bux578/tabler/releases/download/v0.6.0/tabler-v0.6.0.zip">
<img src="http://img.shields.io/badge/Скачать-973_КБ-blue.svg?style=flat"
alt="Скачать" />
</a>
<a href="https://github.com/bux578/tabler/issues">
<img src="http://img.shields.io/github/issues-raw/bux578/tabler.svg?style=flat&label=Задачи"
<a href="https://github.com/bux/tabler/issues">
<img src="http://img.shields.io/github/issues-raw/bux/tabler.svg?style=flat&label=Задачи"
alt="Задачи" />
</a>
<a href="http://creativecommons.org/licenses/by-sa/4.0/deed.ru">
Expand All @@ -40,5 +36,5 @@
</ul>
<!--
<hr>
<sub><strong>tabler</strong> за авторством от <a xmlns:cc="http://creativecommons.org/ns#" href="https://github.com/bux578" property="cc:attributionName" rel="cc:attributionURL">bux578</a> носит лицензию <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons «Атрибуция — На тех же условиях» 4.0 Всемирная</a></sub><br /><a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Лицензия Creative Commons" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png" /></a>
<sub><strong>tabler</strong> за авторством от <a xmlns:cc="http://creativecommons.org/ns#" href="https://github.com/bux" property="cc:attributionName" rel="cc:attributionURL">bux</a> носит лицензию <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">Creative Commons «Атрибуция — На тех же условиях» 4.0 Всемирная</a></sub><br /><a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/"><img alt="Лицензия Creative Commons" style="border-width:0" src="https://i.creativecommons.org/l/by-sa/4.0/80x15.png" /></a>
-->
1 change: 1 addition & 0 deletions tabler/Classes/ModInfoContainer.cs
Expand Up @@ -8,5 +8,6 @@ public class ModInfoContainer {

public string Name { get; set; }
public FileInfo FileInfoStringTable { get; set; }
public bool FileHasBom { get; set; }
}
}
25 changes: 25 additions & 0 deletions tabler/Helper/FileHelper.cs
@@ -0,0 +1,25 @@
using System.IO;
using System.Linq;
using System.Text;

namespace tabler
{
public static class FileHelper
{
public static bool FileHasBom(Stream stream)
{
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
var bytes = ms.ToArray();

// be nice and reset position
stream.Position = 0;

var enc = new UTF8Encoding(true);
var preamble = enc.GetPreamble();
return preamble.Where((p, i) => p == bytes[i]).Any();
}
}
}
}
185 changes: 122 additions & 63 deletions tabler/Helper/XmlHelper.cs
Expand Up @@ -6,69 +6,88 @@
using System.Xml;
using System.Xml.Linq;

namespace tabler {
public class XmlHelper {
namespace tabler
{
public class XmlHelper
{
private const string KEY_NAME = "Key";
private const string ID_NAME = "ID";
private const string PACKAGE_NAME = "Package";

public TranslationComponents ParseXmlFiles(List<FileInfo> allStringTablePaths) {
public TranslationComponents ParseXmlFiles(List<FileInfo> allStringTablePaths)
{
var lstHeader = new System.Collections.Concurrent.ConcurrentBag<string>();

var allModInfos = new System.Collections.Concurrent.ConcurrentBag<ModInfoContainer>();

var transComp = new TranslationComponents();

Parallel.ForEach(allStringTablePaths, (currentFile) => {
var modInfo = new ModInfoContainer {
Parallel.ForEach(allStringTablePaths, (currentFile) =>
{
var modInfo = new ModInfoContainer
{
FileInfoStringTable = currentFile,
Name = currentFile.Directory.Name
};
XDocument xdoc;
using (var sr = new StreamReader(currentFile.FullName))
{
try {
xdoc = XDocument.Load(currentFile.FullName);
} catch (XmlException xmlException) {
throw new GenericXmlException("", currentFile.FullName, xmlException.Message);
}
modInfo.FileHasBom = FileHelper.FileHasBom(sr.BaseStream);
IEnumerable<XElement> keys = xdoc.Descendants().Where(x => x.Name == KEY_NAME);
XDocument xdoc;
var dicKeyWithTranslations = new Dictionary<string, Dictionary<string, string>>();
try
{
xdoc = LoadFromStream(sr.BaseStream);
}
catch (XmlException xmlException)
{
throw new GenericXmlException("", currentFile.FullName, xmlException.Message);
}
// all keys
foreach (XElement key in keys) {
string currentKeyId = key.Attribute(ID_NAME).Value;
IEnumerable<XElement> keys = xdoc.Descendants().Where(x => x.Name == KEY_NAME);
var dicTranslations = new Dictionary<string, string>();
var dicKeyWithTranslations = new Dictionary<string, Dictionary<string, string>>();
// all languages of a key
foreach (XElement language in key.Descendants()) {
string languageName = language.Name.ToString();
// all keys
foreach (XElement key in keys)
{
string currentKeyId = key.Attribute(ID_NAME).Value;
if (dicTranslations.ContainsKey(languageName)) {
throw new DuplicateKeyException(languageName, currentFile.FullName, currentKeyId);
}
var dicTranslations = new Dictionary<string, string>();
// all languages of a key
foreach (XElement language in key.Descendants())
{
string languageName = language.Name.ToString();
dicTranslations.Add(languageName, language.Value);
if (dicTranslations.ContainsKey(languageName))
{
throw new DuplicateKeyException(languageName, currentFile.FullName, currentKeyId);
}
dicTranslations.Add(languageName, language.Value);
// save all the languages
if (lstHeader.Contains(languageName) == false) {
lstHeader.Add(languageName);
// save all the languages
if (lstHeader.Contains(languageName) == false)
{
lstHeader.Add(languageName);
}
}
}
if (dicKeyWithTranslations.ContainsKey(currentKeyId)) {
throw new DuplicateKeyException(currentKeyId, currentFile.FullName, currentKeyId);
if (dicKeyWithTranslations.ContainsKey(currentKeyId))
{
throw new DuplicateKeyException(currentKeyId, currentFile.FullName, currentKeyId);
}
dicKeyWithTranslations.Add(currentKeyId, dicTranslations);
}
dicKeyWithTranslations.Add(currentKeyId, dicTranslations);
}
modInfo.Values = dicKeyWithTranslations;
allModInfos.Add(modInfo);
modInfo.Values = dicKeyWithTranslations;
allModInfos.Add(modInfo);
}
});

transComp.AllModInfo = allModInfos.OrderBy(mic => mic.Name).ToList();
Expand All @@ -78,12 +97,15 @@ public class XmlHelper {
}


public void UpdateXmlFiles(List<FileInfo> filesByNameInDirectory, List<ModInfoContainer> lstModInfos) {
public void UpdateXmlFiles(List<FileInfo> filesByNameInDirectory, List<ModInfoContainer> lstModInfos)
{

Parallel.ForEach(filesByNameInDirectory, (currentFileInfo) => {
Parallel.ForEach(filesByNameInDirectory, (currentFileInfo) =>
{
ModInfoContainer foundModInfo = lstModInfos.FirstOrDefault(x => x.Name.ToLowerInvariant() == currentFileInfo.Directory.Name.ToLowerInvariant());
if (foundModInfo == null) {
if (foundModInfo == null)
{
return;
}
Expand All @@ -93,10 +115,13 @@ public class XmlHelper {
bool changed = false;
//for each id in excel
//Values(ID)(LANGUAGE)
foreach (var currentID in foundModInfo.Values) {
foreach (var currentID in foundModInfo.Values)
{
//for each language
foreach (var currentLanguage in currentID.Value) {
if (UpdateOrInsertValue(xdoc, currentID.Key, currentLanguage.Key, currentLanguage.Value)) {
foreach (var currentLanguage in currentID.Value)
{
if (UpdateOrInsertValue(xdoc, currentID.Key, currentLanguage.Key, currentLanguage.Value))
{
changed = true;
}
}
Expand All @@ -105,51 +130,64 @@ public class XmlHelper {
// Now check if someone deleted a row
IEnumerable<XElement> keysInXml = xdoc.Descendants().Where(x => x.Name == KEY_NAME);
foreach (XElement currentKeyElement in keysInXml.ToList()) {
foreach (XElement currentKeyElement in keysInXml.ToList())
{
string currentKeyId = currentKeyElement.Attribute(ID_NAME).Value;
if (foundModInfo.Values.Keys.Contains(currentKeyId)) {
if (foundModInfo.Values.Keys.Contains(currentKeyId))
{
// hmm, forgot what this was for
var a = true;
} else {
}
else
{
currentKeyElement.Remove();
changed = true;
}
}
if (changed) {
var xmlSettings = new XmlWriterSettings {
if (changed)
{
var xmlSettings = new XmlWriterSettings
{
Indent = true,
IndentChars = " "
IndentChars = " ",
Encoding = new System.Text.UTF8Encoding(foundModInfo.FileHasBom)
};
var configHelper = new ConfigHelper();
var settings = configHelper.GetSettings();
if (settings != null) {
if (settings != null)
{
if (settings.IndentationSettings == IndentationSettings.Spaces) {
if (settings.IndentationSettings == IndentationSettings.Spaces)
{
var indentChars = "";
for (int i = 0; i < settings.TabSize; i++) {
for (int i = 0; i < settings.TabSize; i++)
{
indentChars += " ";
}
xmlSettings.IndentChars = indentChars;
}
if (settings.IndentationSettings == IndentationSettings.Tabs) {
if (settings.IndentationSettings == IndentationSettings.Tabs)
{
xmlSettings.IndentChars = "\t";
}
if (settings.RemoveEmptyNodes) {
if (settings.RemoveEmptyNodes)
{
xdoc.Descendants().Where(d => d.IsEmpty || String.IsNullOrWhiteSpace(d.Value)).Remove();
}
}
using (var writer = XmlWriter.Create(currentFileInfo.FullName, xmlSettings)) {
using (var writer = XmlWriter.Create(currentFileInfo.FullName, xmlSettings))
{
xdoc.Save(writer);
}
File.AppendAllText(currentFileInfo.FullName, Environment.NewLine);
Expand All @@ -158,16 +196,20 @@ public class XmlHelper {
}


private bool UpdateOrInsertValue(XDocument xdoc, string id, string language, string value) {
private bool UpdateOrInsertValue(XDocument xdoc, string id, string language, string value)
{
var changed = false;
//get keys
List<XElement> keys = (from xel in xdoc.Descendants() where xel.Name.ToString().ToLowerInvariant() == KEY_NAME.ToLowerInvariant() select xel).ToList();

XElement parent;

if (keys.Any()) {
if (keys.Any())
{
parent = keys.FirstOrDefault().Parent;
} else {
}
else
{
parent = (from xel in xdoc.Descendants() where xel.Name.ToString().ToLowerInvariant() == PACKAGE_NAME.ToLowerInvariant() select xel).FirstOrDefault();
}

Expand All @@ -177,7 +219,8 @@ public class XmlHelper {
//get language


if (xID == null) {
if (xID == null)
{
//new create
//Too tired to make that in 1 single block :D
var xelKeyNew = new XElement(KEY_NAME);
Expand All @@ -191,20 +234,28 @@ public class XmlHelper {
XElement xLanguage = (from xel in xID.Descendants() where xel.Name.ToString().ToLowerInvariant() == language.ToLowerInvariant() select xel).FirstOrDefault();


if (xLanguage != null) {
if (xLanguage != null)
{
//exist -> update (or delete)
if (xLanguage.Value != value) {
if (string.IsNullOrEmpty(value)) {
if (xLanguage.Value != value)
{
if (string.IsNullOrEmpty(value))
{
xLanguage.Remove();
} else {
}
else
{
xLanguage.Value = value;
}

changed = true;
}
} else {
}
else
{
// don't add a new language if the value is empty
if (!string.IsNullOrEmpty(value)) {
if (!string.IsNullOrEmpty(value))
{
xID.Add(new XElement(language, value));

changed = true;
Expand All @@ -213,5 +264,13 @@ public class XmlHelper {

return changed;
}

private XDocument LoadFromStream(Stream stream)
{
using (var xmlReader = XmlReader.Create(stream))
{
return XDocument.Load(xmlReader);
}
}
}
}

0 comments on commit 8041dad

Please sign in to comment.