From 84db36d3e812e64f79c9cc0e83c78b3c8f17a229 Mon Sep 17 00:00:00 2001 From: Pavel Penev Date: Mon, 19 Oct 2015 19:33:23 +0300 Subject: [PATCH] Check HVA files validity at load time --- OpenRA.Game/FileFormats/HvaReader.cs | 15 +++++++++++++-- OpenRA.Game/Graphics/VoxelLoader.cs | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/OpenRA.Game/FileFormats/HvaReader.cs b/OpenRA.Game/FileFormats/HvaReader.cs index 85fdf375a9..72424e4960 100644 --- a/OpenRA.Game/FileFormats/HvaReader.cs +++ b/OpenRA.Game/FileFormats/HvaReader.cs @@ -9,7 +9,10 @@ #endregion +using System; using System.IO; +using System.Linq; +using OpenRA.Graphics; namespace OpenRA.FileFormats { @@ -19,7 +22,7 @@ namespace OpenRA.FileFormats public readonly uint LimbCount; public readonly float[] Transforms; - public HvaReader(Stream s) + public HvaReader(Stream s, string fileName) { // Index swaps for transposing a matrix var ids = new byte[] { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14 }; @@ -31,6 +34,8 @@ namespace OpenRA.FileFormats // Skip limb names s.Seek(16 * LimbCount, SeekOrigin.Current); Transforms = new float[16 * FrameCount * LimbCount]; + + var testMatrix = new float[16]; for (var j = 0; j < FrameCount; j++) for (var i = 0; i < LimbCount; i++) { @@ -43,13 +48,19 @@ namespace OpenRA.FileFormats for (var k = 0; k < 12; k++) Transforms[c + ids[k]] = s.ReadFloat(); + + Array.Copy(Transforms, 16 * (LimbCount * j + i), testMatrix, 0, 16); + if (Util.MatrixInverse(testMatrix) == null) + throw new InvalidDataException( + "The transformation matrix for HVA file `{0}` section {1} frame {2} is invalid because it is not invertible!" + .F(fileName, i, j)); } } public static HvaReader Load(string filename) { using (var s = File.OpenRead(filename)) - return new HvaReader(s); + return new HvaReader(s, Path.GetFileName(filename)); } } } diff --git a/OpenRA.Game/Graphics/VoxelLoader.cs b/OpenRA.Game/Graphics/VoxelLoader.cs index 7fb70a9c57..afac3ab8e3 100644 --- a/OpenRA.Game/Graphics/VoxelLoader.cs +++ b/OpenRA.Game/Graphics/VoxelLoader.cs @@ -220,7 +220,7 @@ namespace OpenRA.Graphics using (var s = GlobalFileSystem.Open(files.First + ".vxl")) vxl = new VxlReader(s); using (var s = GlobalFileSystem.Open(files.Second + ".hva")) - hva = new HvaReader(s); + hva = new HvaReader(s, files.Second + ".hva"); return new Voxel(this, vxl, hva); }