Skip to content

Commit 469b3a5

Browse files
AdamSzAppleGitHub Enterprise
authored andcommitted
Streamline image loading by using NSData for interop of the image bytes. (#18)
* Streamline image loading by using NSData for interop of the image bytes. * Add trailing newlines. * Fix some spacing issues called out in code review. * Handle nil in Texture2D.CreateFromNSDataPtr() * More spacing fixes.
1 parent d58d55b commit 469b3a5

File tree

27 files changed

+302
-120
lines changed

27 files changed

+302
-120
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace Apple.Core.Runtime
5+
{
6+
/// <summary>
7+
/// C# wrapper around NSData.
8+
/// </summary>
9+
public class NSData : NSObject
10+
{
11+
/// <summary>
12+
/// Construct an NSData wrapper around an existing instance.
13+
/// </summary>
14+
/// <param name="pointer"></param>
15+
public NSData(IntPtr pointer) : base(pointer)
16+
{
17+
}
18+
19+
/// <summary>
20+
/// The number of bytes contained by the data object.
21+
/// </summary>
22+
public UInt64 Length => Interop.NSData_GetLength(Pointer, NSException.ThrowOnExceptionCallback);
23+
24+
/// <summary>
25+
/// Return the object's contents as a byte array.
26+
/// </summary>
27+
public byte[] Bytes
28+
{
29+
get
30+
{
31+
byte[] bytes = null;
32+
33+
var length = (int)Length;
34+
var bytePtr = Interop.NSData_GetBytes(Pointer, NSException.ThrowOnExceptionCallback);
35+
if (length >= 0 && bytePtr != IntPtr.Zero)
36+
{
37+
bytes = new byte[length];
38+
Marshal.Copy(bytePtr, bytes, 0, length);
39+
}
40+
41+
return bytes;
42+
}
43+
}
44+
45+
private static class Interop
46+
{
47+
[DllImport(InteropUtility.DLLName)] public static extern UInt64 NSData_GetLength(IntPtr nsDataPtr, NSExceptionCallback onException);
48+
[DllImport(InteropUtility.DLLName)] public static extern IntPtr NSData_GetBytes(IntPtr nsDataPtr, NSExceptionCallback onException);
49+
}
50+
}
51+
}

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSData.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSString.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public NSString() : this(Interop.NSString_string()) { }
2525
/// Construct an NSString around a C# string.
2626
/// </summary>
2727
/// <param name="s"></param>
28-
public NSString(string s) : this(Interop.NSString_StringWithUTF8String(s)) { }
28+
public NSString(string s) : this(Interop.NSString_StringWithUtf8String(s)) { }
2929

3030
private static readonly NSString _empty = new NSString();
3131
public static NSString Empty => _empty;
@@ -43,11 +43,25 @@ public NSString(string s) : this(Interop.NSString_StringWithUTF8String(s)) { }
4343
/// <param name="s"></param>
4444
public static implicit operator NSString(string s) => new NSString(s);
4545

46+
/// <summary>
47+
/// Construct an NSString from UTF8 formatted NSData.
48+
/// </summary>
49+
/// <param name="data"></param>
50+
public NSString(NSData data) : this(Interop.NSString_StringWithUtf8Data(data.Pointer)) { }
51+
52+
/// <summary>
53+
/// Convert a C# string to UTF8 formatted NSData.
54+
/// </summary>
55+
public NSData Utf8Data => PointerCast<NSData>(Interop.NSString_Utf8Data(Pointer));
56+
public static explicit operator NSData(NSString s) => s.Utf8Data;
57+
4658
private static class Interop
4759
{
4860
[DllImport(InteropUtility.DLLName)] public static extern IntPtr NSString_string();
49-
[DllImport(InteropUtility.DLLName)] public static extern IntPtr NSString_StringWithUTF8String(string s);
61+
[DllImport(InteropUtility.DLLName)] public static extern IntPtr NSString_StringWithUtf8String(string s);
5062
[DllImport(InteropUtility.DLLName)] public static extern string NSString_Utf8String(IntPtr nsStringPtr);
63+
[DllImport(InteropUtility.DLLName)] public static extern IntPtr NSString_StringWithUtf8Data(IntPtr nsDataPtr);
64+
[DllImport(InteropUtility.DLLName)] public static extern IntPtr NSString_Utf8Data(IntPtr nsStringPtr);
5165
}
5266
}
5367
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace Apple.Core.Runtime
5+
{
6+
public static class Texture2DExtensions
7+
{
8+
/// <summary>
9+
/// Create a Texture2D object from the contents of an NSData object.
10+
/// </summary>
11+
/// <param name="nsData"></param>
12+
/// <returns>The created texture if successful; null otherwise.</returns>
13+
public static Texture2D CreateFromNSData(NSData nsData)
14+
{
15+
Texture2D texture = null;
16+
17+
if (nsData?.Length > 0)
18+
{
19+
texture = new Texture2D(1, 1);
20+
texture.LoadImage(nsData.Bytes);
21+
}
22+
23+
return texture;
24+
}
25+
26+
/// <summary>
27+
/// Create a Texture2D object from the contents of an NSData object referenced via IntPtr.
28+
/// </summary>
29+
/// <param name="nsDataPtr"></param>
30+
/// <returns>The created texture if successful; null otherwise.</returns>
31+
public static Texture2D CreateFromNSDataPtr(IntPtr nsDataPtr)
32+
{
33+
return (nsDataPtr != IntPtr.Zero) ? CreateFromNSData(InteropReference.PointerCast<NSData>(nsDataPtr)) : null;
34+
}
35+
}
36+
}

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/Texture2DExtensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using NUnit.Framework;
2+
3+
using Apple.Core.Runtime;
4+
using System;
5+
6+
public class TestNSData
7+
{
8+
[Test]
9+
public void TestBasicOperations()
10+
{
11+
NSData emptyData = NSString.Empty.Utf8Data;
12+
Assert.AreEqual(emptyData.Length, 0);
13+
14+
NSString emptyString = new NSString(emptyData);
15+
Assert.AreEqual(string.Empty, emptyString.ToString());
16+
17+
NSData testData = new NSString("test").Utf8Data;
18+
Assert.AreEqual(testData.Length, 4);
19+
20+
NSString testString = new NSString(testData);
21+
Assert.AreEqual("test", testString.ToString());
22+
}
23+
}

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Tests/Runtime/TestNSData.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/link.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<linker>
22
<assembly fullname="Apple.Core" preserve="all"/>
3+
<assembly fullname="Apple.Core.Runtime" preserve="all"/>
34
<assembly fullname="System.Collections" preserve="all"/>
45
<assembly fullname="System.Collections.Generic" preserve="all"/>
56
<assembly fullname="System.Reflection" preserve="all"/>

plug-ins/Apple.Core/Apple.Core_Unity/Packages/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"com.unity.2d.sprite": "1.0.0",
44
"com.unity.2d.tilemap": "1.0.0",
55
"com.unity.ai.navigation": "1.1.5",
6-
"com.unity.ide.rider": "3.0.26",
6+
"com.unity.ide.rider": "3.0.27",
77
"com.unity.ide.visualstudio": "2.0.22",
88
"com.unity.ide.vscode": "1.2.5",
99
"com.unity.test-framework": "1.1.33",

plug-ins/Apple.Core/Apple.Core_Unity/Packages/packages-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"url": "https://packages.unity.com"
3333
},
3434
"com.unity.ide.rider": {
35-
"version": "3.0.26",
35+
"version": "3.0.27",
3636
"depth": 0,
3737
"source": "registry",
3838
"dependencies": {

0 commit comments

Comments
 (0)