diff --git a/Assets/Plugins/libtest_cloud_point.dll b/Assets/Plugins/libtest_cloud_point.dll new file mode 100644 index 0000000..75411f0 Binary files /dev/null and b/Assets/Plugins/libtest_cloud_point.dll differ diff --git a/Assets/Plugins/libtest_cloud_point.dll.a b/Assets/Plugins/libtest_cloud_point.dll.a new file mode 100644 index 0000000..8cf4120 Binary files /dev/null and b/Assets/Plugins/libtest_cloud_point.dll.a differ diff --git a/Assets/Plugins/libtest_cloud_point.dll.a.meta b/Assets/Plugins/libtest_cloud_point.dll.a.meta new file mode 100644 index 0000000..793dc01 --- /dev/null +++ b/Assets/Plugins/libtest_cloud_point.dll.a.meta @@ -0,0 +1,27 @@ +fileFormatVersion: 2 +guid: 06a7b17ecdbb5c9449a742b22a92e15e +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/libtest_cloud_point.dll.meta b/Assets/Plugins/libtest_cloud_point.dll.meta new file mode 100644 index 0000000..5c38bfa --- /dev/null +++ b/Assets/Plugins/libtest_cloud_point.dll.meta @@ -0,0 +1,27 @@ +fileFormatVersion: 2 +guid: 0085411ee3293a54fa12baa31d1d7515 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 1 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index 7eebe4e..aa0c059 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -746,6 +746,50 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &409312059 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 409312061} + - component: {fileID: 409312062} + m_Layer: 0 + m_Name: TestThread + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &409312061 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 409312059} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: -5.6778817, y: -23.816536, z: 1.4010417} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &409312062 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 409312059} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a311d9ad88850f649bf10fa4ce965a3d, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &410087039 GameObject: m_ObjectHideFlags: 0 @@ -2383,3 +2427,4 @@ SceneRoots: - {fileID: 1537229630} - {fileID: 763126626} - {fileID: 670812665} + - {fileID: 409312061} diff --git a/Assets/Scripts/BlendShaderController.cs b/Assets/Scripts/BlendShaderController.cs index 8fe712e..b94f2de 100644 --- a/Assets/Scripts/BlendShaderController.cs +++ b/Assets/Scripts/BlendShaderController.cs @@ -49,7 +49,7 @@ public class BlendShaderController : MonoBehaviour void InitializeComputeBuffer() { - computeBuffer = new ComputeBuffer(currentRes * currentRes, 1 * sizeof(float));//2 dimensional array of 1 value type of double + computeBuffer = new ComputeBuffer(currentRes * currentRes, 1 * sizeof(float));//2 dimensional array of 1 value type of float } void InitializeTextures() diff --git a/Assets/Scripts/CrpcApi.cs b/Assets/Scripts/CrpcApi.cs new file mode 100644 index 0000000..078d0c4 --- /dev/null +++ b/Assets/Scripts/CrpcApi.cs @@ -0,0 +1,148 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace CloudPointRpc +{ + public static partial class CrpcTestApi + { + private const string DllName = "test_cloud_point"; + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate IntPtr RpcStringCallback(IntPtr rpcString); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_test_init(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_test_deinit(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_test_add_method(RpcStringCallback cb, IntPtr name); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern int crpc_test_remove_method(IntPtr name); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_test_schedule_call(IntPtr name); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_test_change_duration(ulong durationMs); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern ulong crpc_test_duration(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_test_auto_call(uint state); + } + + public static partial class CrpcServerApi + { + private const string DllName = "libcloud_point_rpc"; + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr crpc_str_get_data(IntPtr rpcString); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern ulong crpc_str_get_size(IntPtr rpcString); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern IntPtr crpc_str_create(byte[] data, ulong size); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_str_destroy(IntPtr rpcString); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)] + public static extern void crpc_init(byte[] configPath); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_deinit(); + + [DllImport(DllName, CallingConvention = CallingConvention.Cdecl)] + public static extern void crpc_add_method(CrpcTestApi.RpcStringCallback cb, IntPtr name); + } + + public sealed class RpcString : IDisposable + { + private IntPtr _handle; + private bool _disposed; + + public IntPtr Handle => _handle; + + public RpcString(string data) + { + var bytes = Encoding.UTF8.GetBytes(data); + _handle = CrpcServerApi.crpc_str_create(bytes, (ulong)bytes.Length); + } + + internal RpcString(IntPtr handle) + { + _handle = handle; + } + + public string Data + { + get + { + if (_disposed || _handle == IntPtr.Zero) + return string.Empty; + + var dataPtr = CrpcServerApi.crpc_str_get_data(_handle); + var size = CrpcServerApi.crpc_str_get_size(_handle); + + if (dataPtr == IntPtr.Zero || size == 0) + return string.Empty; + + return Marshal.PtrToStringAnsi(dataPtr, (int)size) ?? string.Empty; + } + } + + public void Dispose() + { + if (!_disposed && _handle != IntPtr.Zero) + { + CrpcServerApi.crpc_str_destroy(_handle); + _handle = IntPtr.Zero; + _disposed = true; + } + } + + ~RpcString() + { + Dispose(); + } + } + + public static class CrpcTestExtensions + { + public static void CrpcTestAddMethod(CrpcTestApi.RpcStringCallback cb, string name) + { + using var nameRpc = new RpcString(name); + CrpcTestApi.crpc_test_add_method(cb, nameRpc.Handle); + } + + public static int CrpcTestRemoveMethod(string name) + { + using var nameRpc = new RpcString(name); + return CrpcTestApi.crpc_test_remove_method(nameRpc.Handle); + } + + public static void CrpcTestScheduleCall(string name) + { + using var nameRpc = new RpcString(name); + CrpcTestApi.crpc_test_schedule_call(nameRpc.Handle); + } + + public static void CrpcAddMethod(CrpcTestApi.RpcStringCallback cb, string name) + { + using var nameRpc = new RpcString(name); + CrpcServerApi.crpc_add_method(cb, nameRpc.Handle); + } + + public static void CrpcInit(string configPath) + { + var bytes = Encoding.UTF8.GetBytes(configPath + '\0'); + CrpcServerApi.crpc_init(bytes); + } + } +} diff --git a/Assets/Scripts/CrpcApi.cs.meta b/Assets/Scripts/CrpcApi.cs.meta new file mode 100644 index 0000000..346c21f --- /dev/null +++ b/Assets/Scripts/CrpcApi.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 54dd0ebb0da474f4d8367649ea1b09f0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/RpcTest.cs b/Assets/Scripts/RpcTest.cs new file mode 100644 index 0000000..97773a4 --- /dev/null +++ b/Assets/Scripts/RpcTest.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using UnityEngine; + +using CloudPointRpc; + +public class RpcTest : MonoBehaviour +{ + private static Queue _callbackTasks = new Queue(); + + [AOT.MonoPInvokeCallback(typeof(CrpcTestApi.RpcStringCallback))] + public static IntPtr RpcStringCallback(IntPtr rpcString) + { + Debug.Log("smth recieved"); + + var rpc = new RpcString(rpcString); + var message = rpc.Data; + rpc.Dispose(); + + var task = new Task(() => Debug.Log($"[RpcTest] Callback received: {message}")); + + lock (_callbackTasks) + { + _callbackTasks.Enqueue(task); + } + + task.Wait(); + var responce = new RpcString("1"); + return responce.Handle; + } + + void Start() + { + CrpcTestApi.crpc_test_init(); + CrpcTestExtensions.CrpcTestAddMethod(RpcStringCallback, "test_method"); + Debug.Log("[RpcTest] Initialized and method added"); + } + + private void Update() + { + lock (_callbackTasks) + { + while (_callbackTasks.Count > 0) + { + var task = _callbackTasks.Dequeue(); + task.Start(); + } + } + } + + private void OnDestroy() + { + lock (_callbackTasks) + { + _callbackTasks.Clear(); + } + + CrpcTestApi.crpc_test_deinit(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/RpcTest.cs.meta b/Assets/Scripts/RpcTest.cs.meta new file mode 100644 index 0000000..56201c4 --- /dev/null +++ b/Assets/Scripts/RpcTest.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a311d9ad88850f649bf10fa4ce965a3d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: