Skip to content

Commit 06bece1

Browse files
committed
Fixed a bug with plugins marked as ESMs but labeled as ESPs and the inverse
1 parent 11dadfa commit 06bece1

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

ESPSharp/DataTypes/LoadOrderFormID.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public LoadOrderFormID(FormID fileFormID, ElderScrollsPlugin file)
1818
fileIndex = (uint)(file.Masters.Count - 1);
1919

2020
string masterName = file.Masters[(int)fileIndex];
21-
ElderScrollsPlugin master = ElderScrollsPlugin.LoadedPlugins.FirstOrDefault(esp => esp.Name == masterName);
21+
ElderScrollsPlugin master = ElderScrollsPlugin.LoadedPlugins.FirstOrDefault(esp => esp.FileName == masterName);
2222

2323
if (master == null)
2424
throw new Exception();

ESPSharp/ElderScrollsPlugin.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class ElderScrollsPlugin : IDisposable
2323
public static Dictionary<uint, List<RecordView>> LoadedRecordViews = new Dictionary<uint, List<RecordView>>();
2424
public static List<KeyValuePair<string, bool>> pluginLocations = new List<KeyValuePair<string, bool>>();
2525

26-
protected string name = "";
2726
public List<string> Masters = new List<string>();
2827
public RecordView Header;
2928
public List<Group> TopGroups = new List<Group>();
@@ -32,21 +31,21 @@ public class ElderScrollsPlugin : IDisposable
3231

3332
protected MemoryMappedFile mmf;
3433

35-
public string Name
34+
public string ProperName
3635
{
3736
get
3837
{
3938
if (Header.Flags.HasFlag(RecordFlag.IsMasterFile))
40-
return name + ".esm";
39+
return Name + ".esm";
4140
else
42-
return name + ".esp";
43-
}
44-
protected set
45-
{
46-
name = Path.GetFileNameWithoutExtension(value);
41+
return Name + ".esp";
4742
}
4843
}
4944

45+
public string Name { get; protected set; }
46+
47+
public string FileName { get; protected set; }
48+
5049
public ElderScrollsPlugin()
5150
{
5251
}
@@ -69,8 +68,12 @@ public void Read(string source)
6968

7069
public void WriteXML(string destinationFolder)
7170
{
72-
Header.Record.WriteXML(Path.Combine(destinationFolder, "Header.xml"), this);
71+
var headerLocation = Path.Combine(destinationFolder, "Header.xml");
72+
Header.Record.WriteXML(headerLocation, this);
7373
//.Where(g => g.ToString() != "Interior Cells" && g.ToString() != "Worldspaces" && g.ToString() != "Dialog Topics")
74+
var xml = XDocument.Load(headerLocation);
75+
xml.Element("Record").Add(new XElement("FileName", FileName));
76+
xml.Save(headerLocation);
7477
#if PARALLEL
7578
Parallel.ForEach(TopGroups, group =>
7679
{
@@ -91,12 +94,15 @@ public void WriteXML(string destinationFolder)
9194
public static ElderScrollsPlugin ReadXML(string sourceFolder)
9295
{
9396
ElderScrollsPlugin outPlug = new ElderScrollsPlugin();
94-
outPlug.name = Path.GetDirectoryName(sourceFolder);
95-
outPlug.Header = new RecordView(Path.Combine(sourceFolder, "Header.xml"));
97+
var headerLocation = Path.Combine(sourceFolder, "Header.xml");
98+
var xml = XDocument.Load(headerLocation);
99+
outPlug.FileName = xml.Element("Record").Element("FileName").Value;
100+
outPlug.Name = Path.GetDirectoryName(sourceFolder);
101+
outPlug.Header = new RecordView(headerLocation);
96102

97103
outPlug.ReadMasters();
98104

99-
outPlug.Masters.Add(outPlug.Name);
105+
outPlug.Masters.Add(outPlug.FileName);
100106

101107
foreach (var folder in Directory.EnumerateDirectories(sourceFolder, "*.*", SearchOption.TopDirectoryOnly))
102108
{
@@ -129,7 +135,8 @@ public void WriteBinary(ESPWriter writer)
129135

130136
public void ReadBinary(string file)
131137
{
132-
name = Path.GetFileNameWithoutExtension(file);
138+
FileName = Path.GetFileName(file);
139+
Name = Path.GetFileNameWithoutExtension(file);
133140
FileInfo fi = new FileInfo(file);
134141
mmf = MemoryMappedFile.CreateFromFile(file, FileMode.Open, Path.GetFileNameWithoutExtension(file), fi.Length, MemoryMappedFileAccess.Read);
135142

@@ -142,7 +149,7 @@ public void ReadBinary(string file)
142149

143150
ReadMasters();
144151

145-
Masters.Add(Name);
152+
Masters.Add(FileName);
146153

147154
while (reader.BaseStream.Position < reader.BaseStream.Length)
148155
{
@@ -200,7 +207,7 @@ protected void ReadMasters()
200207
{
201208
foreach (var masterData in headRec.MasterFiles)
202209
{
203-
ElderScrollsPlugin master = ElderScrollsPlugin.LoadedPlugins.FirstOrDefault(esp => esp.Name == masterData.FileName.Value);
210+
ElderScrollsPlugin master = ElderScrollsPlugin.LoadedPlugins.FirstOrDefault(esp => esp.FileName == masterData.FileName.Value);
204211

205212
if (master == null)
206213
{

PluginDecompiler/Program.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,13 @@ static void Main(string[] args)
6161
{
6262
string outDir = Path.GetFileNameWithoutExtension(file);
6363
ElderScrollsPlugin plugin = new ElderScrollsPlugin();
64+
6465
if (outDir == Path.GetFileName(file))
6566
{
66-
plugin = ElderScrollsPlugin.ReadXML(file);
67+
if (ElderScrollsPlugin.LoadedPlugins.Any(esp => esp.FileName == file))
68+
plugin = ElderScrollsPlugin.LoadedPlugins.First(esp => esp.FileName == file);
69+
else
70+
plugin = ElderScrollsPlugin.ReadXML(file);
6771

6872
string outFile;
6973
if (plugin.Header.Record.Flags.HasFlag(ESPSharp.Enums.Flags.RecordFlag.IsMasterFile))
@@ -86,7 +90,10 @@ static void Main(string[] args)
8690
}
8791
else
8892
{
89-
plugin.ReadBinary(file);
93+
if (ElderScrollsPlugin.LoadedPlugins.Any(esp => esp.FileName == file))
94+
plugin = ElderScrollsPlugin.LoadedPlugins.First(esp => esp.FileName == file);
95+
else
96+
plugin.ReadBinary(file);
9097

9198
if (Directory.Exists(outDir))
9299
Directory.Delete(outDir, true);
@@ -95,5 +102,6 @@ static void Main(string[] args)
95102
plugin.WriteXML(outDir);
96103
}
97104
}
105+
;
98106
}
99107
}

0 commit comments

Comments
 (0)