Skip to content

Commit 86fb137

Browse files
committed
1、标注Unity平台的LuaExportsTypeAnnotation过时,新增LuaExclude标识。
2、修复Unity平台下数据转换报错崩溃问题
1 parent 0a72bb1 commit 86fb137

File tree

5 files changed

+92
-14
lines changed

5 files changed

+92
-14
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace cn.vimfung.luascriptcore
4+
{
5+
/// <summary>
6+
/// 用于排除C#类型导出到Lua的属性、方法
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.All)]
9+
public class LuaExclude : Attribute
10+
{
11+
12+
}
13+
}
14+

Source/Unity3D/UnityProject/Assets/Plugins/LuaScriptCore/LuaExclude.cs.meta

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

Source/Unity3D/UnityProject/Assets/Plugins/LuaScriptCore/LuaExportTypeAnnotation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace cn.vimfung.luascriptcore
44
{
5+
[Obsolete("Use LuaExclude attribute instead.")]
56
[AttributeUsage(AttributeTargets.Class)]
67
public class LuaExportTypeAnnotation : Attribute
78
{

Source/Unity3D/UnityProject/Assets/Plugins/LuaScriptCore/LuaExportsTypeManager.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ public void exportType(Type t, LuaContext context)
168168

169169
string methodName = string.Format ("{0}_{1}", m.Name, methodSignature.ToString ());
170170

171+
LuaExclude isExclude = Attribute.GetCustomAttribute (m, typeof(LuaExclude), true) as LuaExclude;
172+
if (isExclude != null)
173+
{
174+
//为过滤方法
175+
continue;
176+
}
177+
171178
if (m.IsStatic && m.IsPublic)
172179
{
173180
if (typeAnnotation != null
@@ -206,6 +213,13 @@ public void exportType(Type t, LuaContext context)
206213
PropertyInfo[] propertys = t.GetProperties (BindingFlags.Instance | BindingFlags.Public);
207214
foreach (PropertyInfo p in propertys)
208215
{
216+
LuaExclude isExclude = Attribute.GetCustomAttribute (p, typeof(LuaExclude), true) as LuaExclude;
217+
if (isExclude != null)
218+
{
219+
//为过滤方法
220+
continue;
221+
}
222+
209223
if (typeAnnotation != null
210224
&& typeAnnotation.excludeExportPropertyNames != null
211225
&& Array.IndexOf (typeAnnotation.excludeExportPropertyNames, p.Name) >= 0)

Source/Unity3D/UnityProject/Assets/Plugins/LuaScriptCore/LuaValue.cs

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,14 @@ public LuaValue (LuaObjectDecoder decoder)
445445
/// <returns>整型值</returns>
446446
public int toInteger()
447447
{
448-
return Convert.ToInt32 (_value);
448+
try
449+
{
450+
return Convert.ToInt32 (_value);
451+
}
452+
catch(FormatException ex)
453+
{
454+
return 0;
455+
}
449456
}
450457

451458
/// <summary>
@@ -454,7 +461,14 @@ public int toInteger()
454461
/// <returns>布尔值</returns>
455462
public bool toBoolean()
456463
{
457-
return Convert.ToBoolean (_value);
464+
try
465+
{
466+
return Convert.ToBoolean (_value);
467+
}
468+
catch (FormatException ex)
469+
{
470+
return false;
471+
}
458472
}
459473

460474
/// <summary>
@@ -463,7 +477,15 @@ public bool toBoolean()
463477
/// <returns>双精度浮点值</returns>
464478
public double toNumber()
465479
{
466-
return Convert.ToDouble (_value);
480+
try
481+
{
482+
return Convert.ToDouble (_value);
483+
}
484+
catch (FormatException ex)
485+
{
486+
return 0.0;
487+
}
488+
467489
}
468490

469491
/// <summary>
@@ -472,17 +494,24 @@ public double toNumber()
472494
/// <returns>二进制数据流</returns>
473495
public byte[] toData()
474496
{
475-
switch (_type)
497+
try
498+
{
499+
switch (_type)
500+
{
501+
case LuaValueType.Integer:
502+
return BitConverter.GetBytes (Convert.ToInt32 (_value));
503+
case LuaValueType.Number:
504+
return BitConverter.GetBytes (Convert.ToDouble(_value));
505+
case LuaValueType.String:
506+
return Encoding.UTF8.GetBytes (Convert.ToString(_value));
507+
case LuaValueType.Data:
508+
return (byte[])_value;
509+
default:
510+
return null;
511+
}
512+
}
513+
catch (FormatException ex)
476514
{
477-
case LuaValueType.Integer:
478-
return BitConverter.GetBytes (Convert.ToInt32 (_value));
479-
case LuaValueType.Number:
480-
return BitConverter.GetBytes (Convert.ToDouble(_value));
481-
case LuaValueType.String:
482-
return Encoding.UTF8.GetBytes (Convert.ToString(_value));
483-
case LuaValueType.Data:
484-
return (byte[])_value;
485-
default:
486515
return null;
487516
}
488517
}
@@ -493,7 +522,14 @@ public byte[] toData()
493522
/// <returns>字符串</returns>
494523
public string toString()
495524
{
496-
return Convert.ToString (_value);
525+
try
526+
{
527+
return Convert.ToString (_value);
528+
}
529+
catch (FormatException ex)
530+
{
531+
return null;
532+
}
497533
}
498534

499535
/// <summary>

0 commit comments

Comments
 (0)