diff --git a/src/TSMapEditor/CCEngine/CCFileManager.cs b/src/TSMapEditor/CCEngine/CCFileManager.cs index 9eba653c..0f047446 100644 --- a/src/TSMapEditor/CCEngine/CCFileManager.cs +++ b/src/TSMapEditor/CCEngine/CCFileManager.cs @@ -93,7 +93,16 @@ private bool LoadMixFile(string name) { // Logger.Log("Loading MIX file " + name + " from existing MIX file " + Path.GetFileName(value.MixFile.FilePath)); var mixFile = new MixFile(value.MixFile, value.Offset); - mixFile.Parse(); + + try + { + mixFile.Parse(); + } + catch (Exception ex) + { + Logger.Log($"Failed to parse MIX file {name}: {ex.Message}"); + } + AddMix(mixFile); return true; } @@ -129,7 +138,16 @@ private bool LoadMixFromDirectories(string name) Logger.Log("Loading MIX file " + name + " from " + searchDir); var mixFile = new MixFile(); - mixFile.Parse(Path.Combine(searchDir, name)); + + try + { + mixFile.Parse(Path.Combine(searchDir, name)); + } + catch (Exception ex) + { + Logger.Log($"Failed to parse MIX file {name}: {ex.Message}"); + } + AddMix(mixFile); return true; diff --git a/src/TSMapEditor/CCEngine/MixFile.cs b/src/TSMapEditor/CCEngine/MixFile.cs index 09cb00f9..9159e915 100644 --- a/src/TSMapEditor/CCEngine/MixFile.cs +++ b/src/TSMapEditor/CCEngine/MixFile.cs @@ -103,6 +103,11 @@ public void Parse(Stream stream = null) MixFileHeader header = new MixFileHeader(buffer); + // A zero file count means the (possibly encrypted) index came out garbage. + // Fail loudly rather than silently registering an empty MIX. + if (header.FileCount == 0) + throw new MixParseException($"Invalid MIX index (fileCount=0, encrypted={isEncrypted})."); + bodyOffset = INDEX_POSITION + MixFileEntry.SIZE_OF_FILE_ENTRY * header.FileCount; if (isEncrypted) @@ -259,14 +264,14 @@ public MixFileHeader(byte[] buffer) if (buffer.Length < SIZE_OF_HEADER) throw new ArgumentException("buffer is not long enough"); - FileCount = BitConverter.ToInt16(buffer, 0); + FileCount = BitConverter.ToUInt16(buffer, 0); BodySize = BitConverter.ToInt32(buffer, 2); } /// /// The number of files in the MIX file. /// - public short FileCount { get; private set; } + public ushort FileCount { get; private set; } /// /// The size of the MIX file, excluding the header and index.