mirror of
https://github.com/MirrorNetworking/Mirror.git
synced 2024-11-18 11:00:32 +00:00
fix: #3144 Reader/Writer Texture2D null support & test to guarantee it never happens again
This commit is contained in:
parent
b709453057
commit
ae1c7c5fe4
@ -274,8 +274,12 @@ public static Texture2D ReadTexture2D(this NetworkReader reader)
|
|||||||
// TODO allocation protection when sending textures to server.
|
// TODO allocation protection when sending textures to server.
|
||||||
// currently can allocate 32k x 32k x 4 byte = 3.8 GB
|
// currently can allocate 32k x 32k x 4 byte = 3.8 GB
|
||||||
|
|
||||||
// read width & height
|
// support 'null' textures for [SyncVar]s etc.
|
||||||
|
// https://github.com/vis2k/Mirror/issues/3144
|
||||||
short width = reader.ReadShort();
|
short width = reader.ReadShort();
|
||||||
|
if (width == -1) return null;
|
||||||
|
|
||||||
|
// read height
|
||||||
short height = reader.ReadShort();
|
short height = reader.ReadShort();
|
||||||
Texture2D texture2D = new Texture2D(width, height);
|
Texture2D texture2D = new Texture2D(width, height);
|
||||||
|
|
||||||
|
@ -294,6 +294,15 @@ public static void WriteTexture2D(this NetworkWriter writer, Texture2D texture2D
|
|||||||
// TODO allocation protection when sending textures to server.
|
// TODO allocation protection when sending textures to server.
|
||||||
// currently can allocate 32k x 32k x 4 byte = 3.8 GB
|
// currently can allocate 32k x 32k x 4 byte = 3.8 GB
|
||||||
|
|
||||||
|
// support 'null' textures for [SyncVar]s etc.
|
||||||
|
// https://github.com/vis2k/Mirror/issues/3144
|
||||||
|
// simply send -1 for width.
|
||||||
|
if (texture2D == null)
|
||||||
|
{
|
||||||
|
writer.WriteShort(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// write dimensions first so reader can create the texture with size
|
// write dimensions first so reader can create the texture with size
|
||||||
// 32k x 32k short is more than enough
|
// 32k x 32k short is more than enough
|
||||||
writer.WriteShort((short)texture2D.width);
|
writer.WriteShort((short)texture2D.width);
|
||||||
|
@ -1484,5 +1484,19 @@ public void WriteTexture2D_normal()
|
|||||||
Assert.That(texture.height, Is.EqualTo(Texture2D.normalTexture.height));
|
Assert.That(texture.height, Is.EqualTo(Texture2D.normalTexture.height));
|
||||||
Assert.That(texture.GetPixels32().SequenceEqual(Texture2D.normalTexture.GetPixels32()));
|
Assert.That(texture.GetPixels32().SequenceEqual(Texture2D.normalTexture.GetPixels32()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test to prevent https://github.com/vis2k/Mirror/issues/3144
|
||||||
|
[Test]
|
||||||
|
public void WriteTexture2D_Null()
|
||||||
|
{
|
||||||
|
// write
|
||||||
|
NetworkWriter writer = new NetworkWriter();
|
||||||
|
writer.WriteTexture2D(null);
|
||||||
|
|
||||||
|
// read
|
||||||
|
NetworkReader reader = new NetworkReader(writer.ToArray());
|
||||||
|
Texture2D texture = reader.ReadTexture2D();
|
||||||
|
Assert.That(texture, Is.Null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user