Check HVA files validity at load time
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user