Skip to content

Commit

Permalink
+Implemented label addition when using XMSBT import.
Browse files Browse the repository at this point in the history
+Added some new icons for the XMSBT functions.
~Fixed a bug with UMSBT extraction where the overwrite choice was being ignored.
  • Loading branch information
IcySon55 committed Aug 27, 2016
1 parent 4a407e5 commit 76b759d
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 137 deletions.
2 changes: 1 addition & 1 deletion App.config
Expand Up @@ -48,7 +48,7 @@
<applicationSettings>
<MsbtEditor.Properties.Settings>
<setting name="ApplicationName" serializeAs="String">
<value>MSBT Editor Reloaded v0.9.3</value>
<value>MSBT Editor Reloaded v0.9.4</value>
</setting>
</MsbtEditor.Properties.Settings>
</applicationSettings>
Expand Down
79 changes: 48 additions & 31 deletions MSBT.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace MsbtEditor
Expand Down Expand Up @@ -796,49 +797,56 @@ public string ExportToCSV(string filename)
return result;
}

public string ExportXMSBT(string filename)
public string ExportXMSBT(string filename, bool overwrite = true)
{
string result = "Successfully exported to XMSBT.";

try
{
if (HasLabels)
if (!System.IO.File.Exists(filename) || (System.IO.File.Exists(filename) && overwrite))
{
XmlDocument xmlDocument = new XmlDocument();
if (HasLabels)
{
XmlDocument xmlDocument = new XmlDocument();

XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Encoding = FileEncoding;
xmlSettings.Indent = true;
xmlSettings.IndentChars = "\t";
xmlSettings.CheckCharacters = false;
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Encoding = FileEncoding;
xmlSettings.Indent = true;
xmlSettings.IndentChars = "\t";
xmlSettings.CheckCharacters = false;

XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", FileEncoding.WebName, null);
xmlDocument.AppendChild(xmlDeclaration);
XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", FileEncoding.WebName, null);
xmlDocument.AppendChild(xmlDeclaration);

XmlElement xmlRoot = xmlDocument.CreateElement("xmsbt");
xmlDocument.AppendChild(xmlRoot);
XmlElement xmlRoot = xmlDocument.CreateElement("xmsbt");
xmlDocument.AppendChild(xmlRoot);

foreach (Label lbl in LBL1.Labels)
{
XmlElement xmlEntry = xmlDocument.CreateElement("entry");
xmlRoot.AppendChild(xmlEntry);
foreach (Label lbl in LBL1.Labels)
{
XmlElement xmlEntry = xmlDocument.CreateElement("entry");
xmlRoot.AppendChild(xmlEntry);

XmlAttribute xmlLabelAttribute = xmlDocument.CreateAttribute("label");
xmlLabelAttribute.Value = lbl.Name;
xmlEntry.Attributes.Append(xmlLabelAttribute);
XmlAttribute xmlLabelAttribute = xmlDocument.CreateAttribute("label");
xmlLabelAttribute.Value = lbl.Name;
xmlEntry.Attributes.Append(xmlLabelAttribute);

XmlElement xmlString = xmlDocument.CreateElement("string");
xmlString.InnerText = FileEncoding.GetString(lbl.String.Value).Replace("\n", "\r\n").TrimEnd('\0').Replace("\0", @"\0");
xmlEntry.AppendChild(xmlString);
}
XmlElement xmlString = xmlDocument.CreateElement("text");
xmlString.InnerText = FileEncoding.GetString(lbl.String.Value).Replace("\n", "\r\n").TrimEnd('\0').Replace("\0", @"\0");
xmlEntry.AppendChild(xmlString);
}

System.IO.StreamWriter stream = new StreamWriter(filename, false, FileEncoding);
xmlDocument.Save(XmlWriter.Create(stream, xmlSettings));
stream.Close();
System.IO.StreamWriter stream = new StreamWriter(filename, false, FileEncoding);
xmlDocument.Save(XmlWriter.Create(stream, xmlSettings));
stream.Close();
}
else
{
result = "XMSBT does not currently support files without an LBL1 section.";
}
}
else
{
result = "XMSBT does not currently support files without an LBL1 section.";
result = filename + " already exists and overwrite was set to false.";
}
}
catch (Exception ex)
Expand All @@ -849,7 +857,7 @@ public string ExportXMSBT(string filename)
return result;
}

public string ImportXMSBT(string filename)
public string ImportXMSBT(string filename, bool addNew = false)
{
string result = "Successfully imported from XMSBT.";

Expand All @@ -862,15 +870,24 @@ public string ImportXMSBT(string filename)

XmlNode xmlRoot = xmlDocument.SelectSingleNode("/xmsbt");

Dictionary<string, Label> labels = new Dictionary<string, Label>();
foreach (Label lbl in LBL1.Labels)
labels.Add(lbl.Name, lbl);

foreach (XmlNode entry in xmlRoot.SelectNodes("entry"))
{
string label = entry.Attributes["label"].Value;
byte[] str = FileEncoding.GetBytes(entry.SelectSingleNode("string").InnerText.Replace("\r\n", "\n").Replace(@"\0", "\0") + "\0");
byte[] str = FileEncoding.GetBytes(entry.SelectSingleNode("text").InnerText.Replace("\r\n", "\n").Replace(@"\0", "\0") + "\0");

foreach (Label lbl in LBL1.Labels)
if (labels.ContainsKey(label))
labels[label].String.Value = str;
else if (addNew)
{
if (lbl.Name == label)
if (label.Trim().Length <= MSBT.LabelMaxLength && Regex.IsMatch(label.Trim(), MSBT.LabelFilter))
{
Label lbl = AddLabel(label);
lbl.String.Value = str;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions MsbtEditor.csproj
Expand Up @@ -129,6 +129,8 @@
<None Include="images\find.ico" />
<Content Include="images\menu-about.png" />
<Content Include="images\menu-add.png" />
<Content Include="images\menu-batch-export.png" />
<Content Include="images\menu-batch-import.png" />
<Content Include="images\menu-copy.png" />
<Content Include="images\menu-delete.png" />
<Content Include="images\menu-down.png" />
Expand All @@ -137,6 +139,7 @@
<None Include="images\menu-extract.png" />
<Content Include="images\menu-field-properties.png" />
<None Include="images\menu-find.png" />
<None Include="images\menu-import.png" />
<Content Include="images\menu-new.png" />
<Content Include="images\menu-open.png" />
<Content Include="images\menu-paste.png" />
Expand Down
2 changes: 1 addition & 1 deletion Properties/AssemblyInfo.cs
Expand Up @@ -31,4 +31,4 @@
//
// È possibile specificare tutti i valori oppure impostare valori predefiniti per i numeri relativi alla revisione e alla build
// usando l'asterisco '*' come illustrato di seguito:
[assembly: AssemblyVersion("0.9.3.0")]
[assembly: AssemblyVersion("0.9.4.0")]
30 changes: 30 additions & 0 deletions Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Properties/Resources.resx
Expand Up @@ -127,6 +127,12 @@
<data name="menu_add" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\menu-add.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="menu_batch_export" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\menu-batch-export.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="menu_batch_import" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\menu-batch-import.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="menu_copy" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\menu-copy.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand All @@ -151,6 +157,9 @@
<data name="menu_find" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\menu-find.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="menu_import" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\menu-import.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="menu_new" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\images\menu-new.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Properties/Settings.settings
Expand Up @@ -6,7 +6,7 @@
<Value Profile="(Default)" />
</Setting>
<Setting Name="ApplicationName" Type="System.String" Scope="Application">
<Value Profile="(Default)">MSBT Editor Reloaded v0.9.3</Value>
<Value Profile="(Default)">MSBT Editor Reloaded v0.9.4</Value>
</Setting>
<Setting Name="BG4OpenDirectory" Type="System.String" Scope="User">
<Value Profile="(Default)" />
Expand Down
7 changes: 4 additions & 3 deletions README.md
@@ -1,5 +1,5 @@
# MSBT Editor Reloaded
This is a heavily modified version of Exelix11's MSBT file editor.
I creataed this editor a while ago to support a translation project I was planning. It has since grown beyond anything I could have imagined and is now used by many in the modding community on all sorts of projects. I hope you find it useful in your own projects!

### Supported MSBT Sections
* LBL1 - Full support
Expand All @@ -26,8 +26,9 @@ This is a heavily modified version of Exelix11's MSBT file editor.
### XMSBT Translation Tools
* This is the MSBT translator's dream feature! This format allows translators to work together and collaborate on the same file(s) without losing any work due to files being overwritten or files being impossible to merge easily.
* Export an open MSBT file to the human readable XMSBT (XML) format.
* Import XMSBT files into an open MSBT file. Text is imported by matching the label names. Names that do not match are not imported.
* I plan to add batch functionality for XMSBT in a later version.
* Import XMSBT files into an open MSBT file. Text is imported by matching the label names.
* Batch Export and Batch Import support all of the same features as the single file tools.
* To use the batch tools, keep the MSBT and XMSBT files in the same directory. (Export does this automatically, and Import expects it.)

### Other Features
* Find - I've added the ability to search for strings in the file making translation that much easier.
Expand Down
15 changes: 9 additions & 6 deletions UMSBT.cs
Expand Up @@ -63,16 +63,19 @@ public static string Extract(string filename, string path, bool overwrite = true
{
FileEntry fe = entries[i];
string extension = ".msbt";
string outFile = Path.Combine(path, fe.Index.ToString("00000000") + extension);

Debug.Print("[" + fe.Offset.ToString("X8") + "] " + fe.Index + extension);

FileInfo fi = new FileInfo(filename);
FileStream fsr = new FileStream(Path.Combine(path, fe.Index.ToString("00000000") + extension), FileMode.Create, FileAccess.Write, FileShare.None);
BinaryWriterX bw = new BinaryWriterX(fsr);
if (!File.Exists(outFile) || (File.Exists(outFile) && overwrite))
{
FileStream fsr = new FileStream(outFile, FileMode.Create, FileAccess.Write, FileShare.None);
BinaryWriterX bw = new BinaryWriterX(fsr);

br.BaseStream.Seek(fe.Offset, SeekOrigin.Begin);
bw.Write(br.ReadBytes((int)fe.Size));
bw.Close();
br.BaseStream.Seek(fe.Offset, SeekOrigin.Begin);
bw.Write(br.ReadBytes((int)fe.Size));
bw.Close();
}
}

result = entries.Count + " files were found and " + entries.Count + " files were successfully extracted!";
Expand Down

0 comments on commit 76b759d

Please sign in to comment.