-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Streamline image loading by using NSData for interop of the image byt…
…es. (#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.
- Loading branch information
1 parent
d58d55b
commit 469b3a5
Showing
27 changed files
with
302 additions
and
120 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Apple.Core.Runtime | ||
{ | ||
/// <summary> | ||
/// C# wrapper around NSData. | ||
/// </summary> | ||
public class NSData : NSObject | ||
{ | ||
/// <summary> | ||
/// Construct an NSData wrapper around an existing instance. | ||
/// </summary> | ||
/// <param name="pointer"></param> | ||
public NSData(IntPtr pointer) : base(pointer) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// The number of bytes contained by the data object. | ||
/// </summary> | ||
public UInt64 Length => Interop.NSData_GetLength(Pointer, NSException.ThrowOnExceptionCallback); | ||
|
||
/// <summary> | ||
/// Return the object's contents as a byte array. | ||
/// </summary> | ||
public byte[] Bytes | ||
{ | ||
get | ||
{ | ||
byte[] bytes = null; | ||
|
||
var length = (int)Length; | ||
var bytePtr = Interop.NSData_GetBytes(Pointer, NSException.ThrowOnExceptionCallback); | ||
if (length >= 0 && bytePtr != IntPtr.Zero) | ||
{ | ||
bytes = new byte[length]; | ||
Marshal.Copy(bytePtr, bytes, 0, length); | ||
} | ||
|
||
return bytes; | ||
} | ||
} | ||
|
||
private static class Interop | ||
{ | ||
[DllImport(InteropUtility.DLLName)] public static extern UInt64 NSData_GetLength(IntPtr nsDataPtr, NSExceptionCallback onException); | ||
[DllImport(InteropUtility.DLLName)] public static extern IntPtr NSData_GetBytes(IntPtr nsDataPtr, NSExceptionCallback onException); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/NSData.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/Texture2DExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Apple.Core.Runtime | ||
{ | ||
public static class Texture2DExtensions | ||
{ | ||
/// <summary> | ||
/// Create a Texture2D object from the contents of an NSData object. | ||
/// </summary> | ||
/// <param name="nsData"></param> | ||
/// <returns>The created texture if successful; null otherwise.</returns> | ||
public static Texture2D CreateFromNSData(NSData nsData) | ||
{ | ||
Texture2D texture = null; | ||
|
||
if (nsData?.Length > 0) | ||
{ | ||
texture = new Texture2D(1, 1); | ||
texture.LoadImage(nsData.Bytes); | ||
} | ||
|
||
return texture; | ||
} | ||
|
||
/// <summary> | ||
/// Create a Texture2D object from the contents of an NSData object referenced via IntPtr. | ||
/// </summary> | ||
/// <param name="nsDataPtr"></param> | ||
/// <returns>The created texture if successful; null otherwise.</returns> | ||
public static Texture2D CreateFromNSDataPtr(IntPtr nsDataPtr) | ||
{ | ||
return (nsDataPtr != IntPtr.Zero) ? CreateFromNSData(InteropReference.PointerCast<NSData>(nsDataPtr)) : null; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/Texture2DExtensions.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
23 changes: 23 additions & 0 deletions
23
plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Tests/Runtime/TestNSData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using NUnit.Framework; | ||
|
||
using Apple.Core.Runtime; | ||
using System; | ||
|
||
public class TestNSData | ||
{ | ||
[Test] | ||
public void TestBasicOperations() | ||
{ | ||
NSData emptyData = NSString.Empty.Utf8Data; | ||
Assert.AreEqual(emptyData.Length, 0); | ||
|
||
NSString emptyString = new NSString(emptyData); | ||
Assert.AreEqual(string.Empty, emptyString.ToString()); | ||
|
||
NSData testData = new NSString("test").Utf8Data; | ||
Assert.AreEqual(testData.Length, 4); | ||
|
||
NSString testString = new NSString(testData); | ||
Assert.AreEqual("test", testString.ToString()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Tests/Runtime/TestNSData.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/link.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
plug-ins/Apple.Core/Apple.Core_Unity/ProjectSettings/ProjectVersion.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
m_EditorVersion: 2022.3.14f1 | ||
m_EditorVersionWithRevision: 2022.3.14f1 (eff2de9070d8) | ||
m_EditorVersion: 2022.3.16f1 | ||
m_EditorVersionWithRevision: 2022.3.16f1 (d2c21f0ef2f1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// NSData.m | ||
// AppleCoreNative | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NSUInteger NSData_GetLength(void * nsDataPtr, void (* exceptionCallback)(void * nsExceptionPtr)) { | ||
@try { | ||
if (nsDataPtr != NULL) { | ||
NSData * nsData = (__bridge NSData *)nsDataPtr; | ||
return nsData.length; | ||
} | ||
} | ||
@catch (NSException * e) { | ||
exceptionCallback((void *)CFBridgingRetain(e)); | ||
} | ||
return 0; | ||
} | ||
|
||
const void * NSData_GetBytes(void * nsDataPtr, void (* exceptionCallback)(void * nsExceptionPtr)) { | ||
@try { | ||
if (nsDataPtr != NULL) { | ||
NSData * nsData = (__bridge NSData *)nsDataPtr; | ||
return nsData.bytes; | ||
} | ||
} | ||
@catch (NSException * e) { | ||
exceptionCallback((void *)CFBridgingRetain(e)); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.