Skip to content

Commit

Permalink
增加Unity3D下LuaValue的setObject方法
Browse files Browse the repository at this point in the history
  • Loading branch information
vimfung committed Mar 25, 2019
1 parent 306e205 commit fdd0987
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 24 deletions.
29 changes: 29 additions & 0 deletions Source/Unity3D/UnityCommon/LuaScriptCoreForUnity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "LuaObjectDescriptor.h"
#include "StringUtils.h"
#include "LuaScriptController.h"
#include "LuaTable.hpp"

#if defined (__cplusplus)
extern "C" {
Expand Down Expand Up @@ -694,6 +695,34 @@ extern "C" {
}
}

extern int tableSetObject(int tableId,
const char *keyPath,
const void *object,
const void **result)
{
LuaTable *table = dynamic_cast<LuaTable *>(LuaObjectManager::SharedInstance() -> getObject(tableId));
if (table != NULL)
{
LuaObjectDecoder *decoder = new LuaObjectDecoder(table -> getContext(), object);

LuaValue *value = dynamic_cast<LuaValue *>(decoder -> readObject());
table -> setObject(keyPath, value);

decoder -> release();

LuaValue *resultValue = LuaValue::TableValue(table);

LuaObjectManager::SharedInstance() -> putObject(resultValue);

int bufSize = LuaObjectEncoder::encodeObject(table -> getContext(), resultValue, result);
resultValue -> release();

return bufSize;
}

return 0;
}

#if defined (__cplusplus)
}
#endif
15 changes: 15 additions & 0 deletions Source/Unity3D/UnityCommon/LuaScriptCoreForUnity.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ extern "C" {
const void *params,
int scriptControllerId);


/**
Table设置指定键名对象
@param tableId table标识
@param keyPath 键名路径
@param object 对象
@param result 返回新的字典集合
@return 返回值的缓存长度
*/
LuaScriptCoreApi extern int tableSetObject(int tableId,
const char *keyPath,
const void *object,
const void **result);

#if defined (__cplusplus)
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public class LuaValue : LuaBaseObject
{
private LuaValueType _type;
private object _value;
private int _tableId;
private LuaContext _context;

~LuaValue ()
{
if (_tableId > 0)
{
NativeUtils.releaseObject (_tableId);
}
}

/// <summary>
/// 初始化一个Nil值
Expand Down Expand Up @@ -302,6 +312,16 @@ public override void serialization (LuaObjectEncoder encoder)
{
base.serialization (encoder);

if (_context != null)
{
encoder.writeInt32 (_context.objectId);
}
else
{
encoder.writeInt32 (0);
}

encoder.writeInt32 (_tableId);
encoder.writeInt16 ((Int16)type);

switch (type)
Expand Down Expand Up @@ -398,6 +418,10 @@ public LuaValueType type
public LuaValue (LuaObjectDecoder decoder)
: base(decoder)
{

int contextId = decoder.readInt32 ();
_context = LuaContext.getContext (contextId);
_tableId = decoder.readInt32 ();
_type = (LuaValueType)decoder.readInt16 ();
_value = null;

Expand Down Expand Up @@ -618,5 +642,41 @@ public object toObject()

return _value;
}

/// <summary>
/// 设置对象
/// </summary>
/// <param name="keyPath">键名路径.</param>
/// <param name="value">值.</param>
public void setObject(String keyPath, object value)
{
if (_context != null && _tableId > 0 && type == LuaValueType.Map)
{
LuaValue objectValue = new LuaValue (value);

IntPtr resultPtr = IntPtr.Zero;

IntPtr valuePtr = IntPtr.Zero;
LuaObjectEncoder encoder = new LuaObjectEncoder (_context);
encoder.writeObject (objectValue);

byte[] bytes = encoder.bytes;
valuePtr = Marshal.AllocHGlobal (bytes.Length);
Marshal.Copy (bytes, 0, valuePtr, bytes.Length);

int bufferLen = NativeUtils.tableSetObject (_tableId, keyPath, valuePtr, resultPtr);

if (bufferLen > 0)
{
LuaValue resultValue = LuaObjectDecoder.DecodeObject (resultPtr, bufferLen, _context) as LuaValue;
_value = resultValue._value;
}

if (valuePtr != IntPtr.Zero)
{
Marshal.FreeHGlobal (valuePtr);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,25 @@ internal extern static int registerType(
[DllImport("LuaScriptCore-Unity-OSX")]
internal extern static void runThread (int nativeContextId, IntPtr function, IntPtr arguments, int scriptControllerId);

/// <summary>
/// 设置指定table的键值
/// </summary>
/// <returns>缓存大小</returns>
/// <param name="tableId">table标识.</param>
/// <param name="keyPath">键名路径.</param>
/// <param name="value">值.</param>
/// <param name="resultBuffer">返回缓存,用于同步LuaValue中的Map对象.</param>
[DllImport("LuaScriptCore-Unity-OSX")]
internal extern static int tableSetObject (int tableId, string keyPath, IntPtr value, IntPtr resultBuffer);

/// <summary>
/// 设置Unity调试日志接口,用于Lua中输出日志到Unity的编辑器控制台, Editor特有。
/// </summary>
/// <param name="fp">方法回调</param>
[DllImport("LuaScriptCore-Unity-OSX")]
private extern static void setUnityDebugLog (IntPtr fp);


#elif UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN

/// <summary>
Expand Down Expand Up @@ -526,6 +538,17 @@ internal extern static int registerType(
[DllImport("LuaScriptCore-Unity-Win64")]
internal extern static void runThread (int nativeContextId, IntPtr function, IntPtr arguments, int scriptControllerId);

/// <summary>
/// 设置指定table的键值
/// </summary>
/// <returns>缓存大小</returns>
/// <param name="tableId">table标识.</param>
/// <param name="keyPath">键名路径.</param>
/// <param name="value">值.</param>
/// <param name="resultBuffer">返回缓存,用于同步LuaValue中的Map对象.</param>
[DllImport("LuaScriptCore-Unity-Win64")]
internal extern static int tableSetObject (int tableId, string keyPath, IntPtr value, IntPtr resultBuffer);

/// <summary>
/// 设置Unity调试日志接口,用于Lua中输出日志到Unity的编辑器控制台, Editor特有。
/// </summary>
Expand Down Expand Up @@ -752,6 +775,17 @@ internal extern static int registerType(
[DllImport("__Internal")]
internal extern static void runThread (int nativeContextId, IntPtr function, IntPtr arguments, int scriptControllerId);

/// <summary>
/// 设置指定table的键值
/// </summary>
/// <returns>缓存大小</returns>
/// <param name="tableId">table标识.</param>
/// <param name="keyPath">键名路径.</param>
/// <param name="value">值.</param>
/// <param name="resultBuffer">返回缓存,用于同步LuaValue中的Map对象.</param>
[DllImport("__Internal")]
internal extern static int tableSetObject (int tableId, string keyPath, IntPtr value, IntPtr resultBuffer);

#elif UNITY_ANDROID

/// <summary>
Expand Down Expand Up @@ -969,6 +1003,17 @@ internal extern static int registerType(
/// <param name="scriptControllerId">脚本控制器标识</param>
[DllImport("LuaScriptCore-Unity-Android")]
internal extern static void runThread (int nativeContextId, IntPtr function, IntPtr arguments, int scriptControllerId);

/// <summary>
/// 设置指定table的键值
/// </summary>
/// <returns>缓存大小</returns>
/// <param name="tableId">table标识.</param>
/// <param name="keyPath">键名路径.</param>
/// <param name="value">值.</param>
/// <param name="resultBuffer">返回缓存,用于同步LuaValue中的Map对象.</param>
[DllImport("LuaScriptCore-Unity-Android")]
internal extern static int tableSetObject (int tableId, string keyPath, IntPtr value, IntPtr resultBuffer);
#endif
}
}
Loading

0 comments on commit fdd0987

Please sign in to comment.