Skip to content

Commit e77581c

Browse files
Support indexers with multiple parameters
1 parent d429295 commit e77581c

5 files changed

Lines changed: 236 additions & 19 deletions

File tree

Unity/Assets/NativeScript/Bindings.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ delegate void InitDelegate(
299299
IntPtr unityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingle,
300300
IntPtr unityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3,
301301
IntPtr unityEngineVector3Methodop_UnaryNegationUnityEngineVector3,
302+
IntPtr unityEngineMatrix4x4PropertyGetItem,
303+
IntPtr unityEngineMatrix4x4PropertySetItem,
302304
IntPtr releaseUnityEngineRaycastHit,
303305
int ReleaseUnityEngineRaycastHit,
304306
IntPtr unityEngineRaycastHitPropertyGetPoint,
@@ -465,6 +467,8 @@ static extern void Init(
465467
IntPtr unityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingle,
466468
IntPtr unityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3,
467469
IntPtr unityEngineVector3Methodop_UnaryNegationUnityEngineVector3,
470+
IntPtr unityEngineMatrix4x4PropertyGetItem,
471+
IntPtr unityEngineMatrix4x4PropertySetItem,
468472
IntPtr releaseUnityEngineRaycastHit,
469473
int ReleaseUnityEngineRaycastHit,
470474
IntPtr unityEngineRaycastHitPropertyGetPoint,
@@ -542,6 +546,8 @@ IntPtr systemExceptionConstructorSystemString
542546
delegate void UnityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingleDelegate(ref UnityEngine.Vector3 thiz, float newX, float newY, float newZ);
543547
delegate UnityEngine.Vector3 UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3Delegate(ref UnityEngine.Vector3 a, ref UnityEngine.Vector3 b);
544548
delegate UnityEngine.Vector3 UnityEngineVector3Methodop_UnaryNegationUnityEngineVector3Delegate(ref UnityEngine.Vector3 a);
549+
delegate float UnityEngineMatrix4x4PropertyGetItemDelegate(ref UnityEngine.Matrix4x4 thiz, int row, int column);
550+
delegate void UnityEngineMatrix4x4PropertySetItemDelegate(ref UnityEngine.Matrix4x4 thiz, int row, int column, float value);
545551
delegate void ReleaseUnityEngineRaycastHitDelegate(int handle);
546552
delegate UnityEngine.Vector3 UnityEngineRaycastHitPropertyGetPointDelegate(int thisHandle);
547553
delegate void UnityEngineRaycastHitPropertySetPointDelegate(int thisHandle, ref UnityEngine.Vector3 value);
@@ -639,6 +645,8 @@ public static void Open(
639645
Marshal.GetFunctionPointerForDelegate(new UnityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingleDelegate(UnityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingle)),
640646
Marshal.GetFunctionPointerForDelegate(new UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3Delegate(UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3)),
641647
Marshal.GetFunctionPointerForDelegate(new UnityEngineVector3Methodop_UnaryNegationUnityEngineVector3Delegate(UnityEngineVector3Methodop_UnaryNegationUnityEngineVector3)),
648+
Marshal.GetFunctionPointerForDelegate(new UnityEngineMatrix4x4PropertyGetItemDelegate(UnityEngineMatrix4x4PropertyGetItem)),
649+
Marshal.GetFunctionPointerForDelegate(new UnityEngineMatrix4x4PropertySetItemDelegate(UnityEngineMatrix4x4PropertySetItem)),
642650
Marshal.GetFunctionPointerForDelegate(new ReleaseUnityEngineRaycastHitDelegate(ReleaseUnityEngineRaycastHit)),
643651
1000,
644652
Marshal.GetFunctionPointerForDelegate(new UnityEngineRaycastHitPropertyGetPointDelegate(UnityEngineRaycastHitPropertyGetPoint)),
@@ -1257,6 +1265,43 @@ static UnityEngine.Vector3 UnityEngineVector3Methodop_UnaryNegationUnityEngineVe
12571265
}
12581266
}
12591267

1268+
[MonoPInvokeCallback(typeof(UnityEngineMatrix4x4PropertyGetItemDelegate))]
1269+
static float UnityEngineMatrix4x4PropertyGetItem(ref UnityEngine.Matrix4x4 thiz, int row, int column)
1270+
{
1271+
try
1272+
{
1273+
var returnValue = thiz[row, row];
1274+
return returnValue;
1275+
}
1276+
catch (System.NullReferenceException ex)
1277+
{
1278+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
1279+
return default(float);
1280+
}
1281+
catch (System.Exception ex)
1282+
{
1283+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
1284+
return default(float);
1285+
}
1286+
}
1287+
1288+
[MonoPInvokeCallback(typeof(UnityEngineMatrix4x4PropertySetItemDelegate))]
1289+
static void UnityEngineMatrix4x4PropertySetItem(ref UnityEngine.Matrix4x4 thiz, int row, int column, float value)
1290+
{
1291+
try
1292+
{
1293+
thiz[row, column] = column;
1294+
}
1295+
catch (System.NullReferenceException ex)
1296+
{
1297+
NativeScript.Bindings.SetCsharpExceptionSystemNullReferenceException(NativeScript.Bindings.ObjectStore.Store(ex));
1298+
}
1299+
catch (System.Exception ex)
1300+
{
1301+
NativeScript.Bindings.SetCsharpException(NativeScript.Bindings.ObjectStore.Store(ex));
1302+
}
1303+
}
1304+
12601305
[MonoPInvokeCallback(typeof(ReleaseUnityEngineRaycastHitDelegate))]
12611306
static void ReleaseUnityEngineRaycastHit(int handle)
12621307
{

Unity/Assets/NativeScript/Editor/GenerateBindings.cs

Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ class JsonMethod
5454
class JsonPropertyGet
5555
{
5656
public bool IsReadOnly = true;
57+
public string[] ParamTypes;
5758
public string[] Exceptions;
5859
}
5960

6061
[Serializable]
6162
class JsonPropertySet
6263
{
6364
public bool IsReadOnly;
65+
public string[] ParamTypes;
6466
public string[] Exceptions;
6567
}
6668

@@ -1813,21 +1815,50 @@ static void AppendProperty(
18131815
Assembly[] assemblies,
18141816
StringBuilders builders)
18151817
{
1816-
PropertyInfo property = enclosingType.GetProperty(
1817-
jsonProperty.Name);
1818-
Type propertyType = OverrideGenericType(
1819-
property.PropertyType,
1820-
typeGenericArgumentTypes,
1821-
typeParams);
18221818
JsonPropertyGet jsonPropertyGet = jsonProperty.Get;
18231819
if (jsonPropertyGet != null)
18241820
{
1825-
Type[] exceptionTypes = GetTypes(
1826-
jsonPropertyGet.Exceptions,
1827-
assemblies);
1828-
MethodInfo getMethod = property.GetGetMethod();
1821+
PropertyInfo property = null;
1822+
MethodInfo getMethod = null;
1823+
if (jsonPropertyGet.ParamTypes != null)
1824+
{
1825+
PropertyInfo[] properties = enclosingType.GetProperties();
1826+
foreach (PropertyInfo curProperty in properties)
1827+
{
1828+
// Name must match
1829+
if (curProperty.Name != jsonProperty.Name)
1830+
{
1831+
continue;
1832+
}
1833+
1834+
// Must have a get method
1835+
getMethod = curProperty.GetGetMethod();
1836+
if (getMethod == null)
1837+
{
1838+
continue;
1839+
}
1840+
1841+
// All parameters must match
1842+
if (CheckParametersMatch(
1843+
jsonPropertyGet.ParamTypes,
1844+
getMethod.GetParameters()))
1845+
{
1846+
property = curProperty;
1847+
break;
1848+
}
1849+
}
1850+
}
1851+
else
1852+
{
1853+
property = enclosingType.GetProperty(jsonProperty.Name);
1854+
getMethod = property.GetGetMethod();
1855+
}
1856+
18291857
if (getMethod != null)
18301858
{
1859+
Type[] exceptionTypes = GetTypes(
1860+
jsonPropertyGet.Exceptions,
1861+
assemblies);
18311862
ParameterInfo[] parameters = ConvertParameters(
18321863
getMethod.GetParameters());
18331864
OverrideGenericParameterTypes(
@@ -1844,21 +1875,58 @@ static void AppendProperty(
18441875
jsonPropertyGet.IsReadOnly,
18451876
enclosingType,
18461877
typeParams,
1847-
propertyType,
1878+
property.PropertyType,
18481879
indent,
18491880
exceptionTypes,
18501881
builders);
18511882
}
18521883
}
1884+
18531885
JsonPropertySet jsonPropertySet = jsonProperty.Set;
18541886
if (jsonPropertySet != null)
18551887
{
1856-
Type[] exceptionTypes = GetTypes(
1857-
jsonPropertySet.Exceptions,
1858-
assemblies);
1888+
PropertyInfo property = null;
1889+
MethodInfo setMethod = null;
1890+
if (jsonPropertySet.ParamTypes != null)
1891+
{
1892+
PropertyInfo[] properties = enclosingType.GetProperties();
1893+
foreach (PropertyInfo curProperty in properties)
1894+
{
1895+
// Name must match
1896+
if (curProperty.Name != jsonProperty.Name)
1897+
{
1898+
continue;
1899+
}
1900+
1901+
// Must have a set method
1902+
setMethod = curProperty.GetSetMethod();
1903+
if (setMethod == null)
1904+
{
1905+
continue;
1906+
}
1907+
1908+
// All parameters must match
1909+
if (CheckParametersMatch(
1910+
jsonPropertySet.ParamTypes,
1911+
setMethod.GetParameters()))
1912+
{
1913+
property = curProperty;
1914+
break;
1915+
}
1916+
}
1917+
}
1918+
else
1919+
{
1920+
property = enclosingType.GetProperty(jsonProperty.Name);
1921+
setMethod = property.GetSetMethod();
1922+
}
1923+
18591924
MethodInfo method = property.GetSetMethod();
18601925
if (method != null)
18611926
{
1927+
Type[] exceptionTypes = GetTypes(
1928+
jsonPropertySet.Exceptions,
1929+
assemblies);
18621930
ParameterInfo[] parameters = ConvertParameters(
18631931
method.GetParameters());
18641932
OverrideGenericParameterTypes(
@@ -1875,7 +1943,7 @@ static void AppendProperty(
18751943
jsonPropertySet.IsReadOnly,
18761944
enclosingType,
18771945
typeParams,
1878-
propertyType,
1946+
property.PropertyType,
18791947
indent,
18801948
exceptionTypes,
18811949
builders);
@@ -3454,10 +3522,17 @@ static void AppendGetter(
34543522
enclosingType,
34553523
methodIsStatic,
34563524
builders.CsharpFunctions);
3457-
if (parameters.Length == 1)
3525+
if (parameters.Length > 0)
34583526
{
34593527
builders.CsharpFunctions.Append('[');
3460-
builders.CsharpFunctions.Append(parameters[0].Name);
3528+
for (int i = 0; i < parameters.Length; ++i)
3529+
{
3530+
builders.CsharpFunctions.Append(parameters[0].Name);
3531+
if (i != parameters.Length-1)
3532+
{
3533+
builders.CsharpFunctions.Append(", ");
3534+
}
3535+
}
34613536
builders.CsharpFunctions.Append("]");
34623537
}
34633538
else
@@ -3636,10 +3711,17 @@ static void AppendSetter(
36363711
enclosingType,
36373712
methodIsStatic,
36383713
builders.CsharpFunctions);
3639-
if (parameters.Length == 2)
3714+
if (parameters.Length > 1)
36403715
{
36413716
builders.CsharpFunctions.Append('[');
3642-
builders.CsharpFunctions.Append(parameters[0].Name);
3717+
for (int i = 0, end = parameters.Length-1; i < end; ++i)
3718+
{
3719+
builders.CsharpFunctions.Append(parameters[i].Name);
3720+
if (i != end-1)
3721+
{
3722+
builders.CsharpFunctions.Append(", ");
3723+
}
3724+
}
36433725
builders.CsharpFunctions.Append("] = ");
36443726
builders.CsharpFunctions.Append(parameters[1].Name);
36453727
}

Unity/Assets/NativeScriptTypes.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,27 @@
232232
}
233233
]
234234
},
235+
{
236+
"Name": "UnityEngine.Matrix4x4",
237+
"Properties": [
238+
{
239+
"Name": "Item",
240+
"Get": {
241+
"ParamTypes": [
242+
"System.Int32",
243+
"System.Int32"
244+
]
245+
},
246+
"Set": {
247+
"ParamTypes": [
248+
"System.Int32",
249+
"System.Int32",
250+
"System.Single"
251+
]
252+
}
253+
}
254+
]
255+
},
235256
{
236257
"Name": "UnityEngine.RaycastHit",
237258
"MaxSimultaneous": 1000,

Unity/CppSource/NativeScript/Bindings.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ namespace Plugin
6868
void (*UnityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingle)(UnityEngine::Vector3* thiz, float newX, float newY, float newZ);
6969
UnityEngine::Vector3 (*UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3)(UnityEngine::Vector3& a, UnityEngine::Vector3& b);
7070
UnityEngine::Vector3 (*UnityEngineVector3Methodop_UnaryNegationUnityEngineVector3)(UnityEngine::Vector3& a);
71+
float (*UnityEngineMatrix4x4PropertyGetItem)(UnityEngine::Matrix4x4* thiz, int32_t row, int32_t column);
72+
void (*UnityEngineMatrix4x4PropertySetItem)(UnityEngine::Matrix4x4* thiz, int32_t row, int32_t column, float value);
7173
void (*ReleaseUnityEngineRaycastHit)(int32_t handle);
7274
UnityEngine::Vector3 (*UnityEngineRaycastHitPropertyGetPoint)(int32_t thisHandle);
7375
void (*UnityEngineRaycastHitPropertySetPoint)(int32_t thisHandle, UnityEngine::Vector3& value);
@@ -1685,6 +1687,38 @@ namespace UnityEngine
16851687
}
16861688
}
16871689

1690+
namespace UnityEngine
1691+
{
1692+
Matrix4x4::Matrix4x4()
1693+
{
1694+
}
1695+
1696+
float Matrix4x4::GetItem(int32_t row, int32_t column)
1697+
{
1698+
auto returnValue = Plugin::UnityEngineMatrix4x4PropertyGetItem(this, row, column);
1699+
if (Plugin::unhandledCsharpException)
1700+
{
1701+
System::Exception* ex = Plugin::unhandledCsharpException;
1702+
Plugin::unhandledCsharpException = nullptr;
1703+
ex->ThrowReferenceToThis();
1704+
delete ex;
1705+
}
1706+
return returnValue;
1707+
}
1708+
1709+
void Matrix4x4::SetItem(int32_t row, int32_t column, float value)
1710+
{
1711+
Plugin::UnityEngineMatrix4x4PropertySetItem(this, row, column, value);
1712+
if (Plugin::unhandledCsharpException)
1713+
{
1714+
System::Exception* ex = Plugin::unhandledCsharpException;
1715+
Plugin::unhandledCsharpException = nullptr;
1716+
ex->ThrowReferenceToThis();
1717+
delete ex;
1718+
}
1719+
}
1720+
}
1721+
16881722
namespace UnityEngine
16891723
{
16901724
RaycastHit::RaycastHit(std::nullptr_t n)
@@ -3013,6 +3047,8 @@ DLLEXPORT void Init(
30133047
void (*unityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingle)(UnityEngine::Vector3* thiz, float newX, float newY, float newZ),
30143048
UnityEngine::Vector3 (*unityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3)(UnityEngine::Vector3& a, UnityEngine::Vector3& b),
30153049
UnityEngine::Vector3 (*unityEngineVector3Methodop_UnaryNegationUnityEngineVector3)(UnityEngine::Vector3& a),
3050+
float (*unityEngineMatrix4x4PropertyGetItem)(UnityEngine::Matrix4x4* thiz, int32_t row, int32_t column),
3051+
void (*unityEngineMatrix4x4PropertySetItem)(UnityEngine::Matrix4x4* thiz, int32_t row, int32_t column, float value),
30163052
void (*releaseUnityEngineRaycastHit)(int32_t handle),
30173053
int32_t refCountsLenUnityEngineRaycastHit,
30183054
UnityEngine::Vector3 (*unityEngineRaycastHitPropertyGetPoint)(int32_t thisHandle),
@@ -3075,6 +3111,8 @@ DLLEXPORT void Init(
30753111
Plugin::UnityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingle = unityEngineVector3MethodSetSystemSingle_SystemSingle_SystemSingle;
30763112
Plugin::UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3 = unityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3;
30773113
Plugin::UnityEngineVector3Methodop_UnaryNegationUnityEngineVector3 = unityEngineVector3Methodop_UnaryNegationUnityEngineVector3;
3114+
Plugin::UnityEngineMatrix4x4PropertyGetItem = unityEngineMatrix4x4PropertyGetItem;
3115+
Plugin::UnityEngineMatrix4x4PropertySetItem = unityEngineMatrix4x4PropertySetItem;
30783116
Plugin::ReleaseUnityEngineRaycastHit = releaseUnityEngineRaycastHit;
30793117
Plugin::RefCountsLenUnityEngineRaycastHit = refCountsLenUnityEngineRaycastHit;
30803118
Plugin::RefCountsUnityEngineRaycastHit = new int32_t[refCountsLenUnityEngineRaycastHit]();

0 commit comments

Comments
 (0)