diff --git a/README.md b/README.md index 19e6b36..572621b 100644 --- a/README.md +++ b/README.md @@ -1305,6 +1305,11 @@ state[:int_value] #=> 1234 state[:bool_value] #=> true state[:float_value] #=> 678.9 state[:string_value] #=> "Hoge Hoge" + +# A somewhat fuzzy matcher, the `is?` method, is available for shared states. +state[:a] #=> 'buz' +state[:a].is?('buz') #=> true +state[:a].is?(:buz) #=> true ``` ### Memory Usage in Ruby diff --git a/src/VitalRouter.Unity/Assets/InitTestScene638644545017947350.unity b/src/VitalRouter.Unity/Assets/InitTestScene638644545017947350.unity new file mode 100644 index 0000000..99776b1 --- /dev/null +++ b/src/VitalRouter.Unity/Assets/InitTestScene638644545017947350.unity @@ -0,0 +1,467 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!29 &1 +OcclusionCullingSettings: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_OcclusionBakeSettings: + smallestOccluder: 5 + smallestHole: 0.25 + backfaceThreshold: 100 + m_SceneGUID: 00000000000000000000000000000000 + m_OcclusionCullingData: {fileID: 0} +--- !u!104 &2 +RenderSettings: + m_ObjectHideFlags: 0 + serializedVersion: 9 + m_Fog: 0 + m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1} + m_FogMode: 3 + m_FogDensity: 0.01 + m_LinearFogStart: 0 + m_LinearFogEnd: 300 + m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1} + m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1} + m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1} + m_AmbientIntensity: 1 + m_AmbientMode: 0 + m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1} + m_SkyboxMaterial: {fileID: 10304, guid: 0000000000000000f000000000000000, type: 0} + m_HaloStrength: 0.5 + m_FlareStrength: 1 + m_FlareFadeSpeed: 3 + m_HaloTexture: {fileID: 0} + m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0} + m_DefaultReflectionMode: 0 + m_DefaultReflectionResolution: 128 + m_ReflectionBounces: 1 + m_ReflectionIntensity: 1 + m_CustomReflection: {fileID: 0} + m_Sun: {fileID: 0} + m_UseRadianceAmbientProbe: 0 +--- !u!157 &3 +LightmapSettings: + m_ObjectHideFlags: 0 + serializedVersion: 12 + m_GIWorkflowMode: 1 + m_GISettings: + serializedVersion: 2 + m_BounceScale: 1 + m_IndirectOutputScale: 1 + m_AlbedoBoost: 1 + m_EnvironmentLightingMode: 0 + m_EnableBakedLightmaps: 1 + m_EnableRealtimeLightmaps: 0 + m_LightmapEditorSettings: + serializedVersion: 12 + m_Resolution: 2 + m_BakeResolution: 40 + m_AtlasSize: 1024 + m_AO: 0 + m_AOMaxDistance: 1 + m_CompAOExponent: 1 + m_CompAOExponentDirect: 0 + m_ExtractAmbientOcclusion: 0 + m_Padding: 2 + m_LightmapParameters: {fileID: 0} + m_LightmapsBakeMode: 1 + m_TextureCompression: 1 + m_FinalGather: 0 + m_FinalGatherFiltering: 1 + m_FinalGatherRayCount: 256 + m_ReflectionCompression: 2 + m_MixedBakeMode: 2 + m_BakeBackend: 1 + m_PVRSampling: 1 + m_PVRDirectSampleCount: 32 + m_PVRSampleCount: 512 + m_PVRBounces: 2 + m_PVREnvironmentSampleCount: 256 + m_PVREnvironmentReferencePointCount: 2048 + m_PVRFilteringMode: 1 + m_PVRDenoiserTypeDirect: 1 + m_PVRDenoiserTypeIndirect: 1 + m_PVRDenoiserTypeAO: 1 + m_PVRFilterTypeDirect: 0 + m_PVRFilterTypeIndirect: 0 + m_PVRFilterTypeAO: 0 + m_PVREnvironmentMIS: 1 + m_PVRCulling: 1 + m_PVRFilteringGaussRadiusDirect: 1 + m_PVRFilteringGaussRadiusIndirect: 5 + m_PVRFilteringGaussRadiusAO: 2 + m_PVRFilteringAtrousPositionSigmaDirect: 0.5 + m_PVRFilteringAtrousPositionSigmaIndirect: 2 + m_PVRFilteringAtrousPositionSigmaAO: 1 + m_ExportTrainingData: 0 + m_TrainingDataDestination: TrainingData + m_LightProbeSampleCountMultiplier: 4 + m_LightingDataAsset: {fileID: 0} + m_LightingSettings: {fileID: 0} +--- !u!196 &4 +NavMeshSettings: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_BuildSettings: + serializedVersion: 3 + agentTypeID: 0 + agentRadius: 0.5 + agentHeight: 2 + agentSlope: 45 + agentClimb: 0.4 + ledgeDropHeight: 0 + maxJumpAcrossDistance: 0 + minRegionArea: 2 + manualCellSize: 0 + cellSize: 0.16666667 + manualTileSize: 0 + tileSize: 256 + buildHeightMesh: 0 + maxJobWorkers: 0 + preserveTilesOutsideBounds: 0 + debug: + m_Flags: 0 + m_NavMeshData: {fileID: 0} +--- !u!114 &30428838 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d44e6804bc58be84ea71a619b468f150, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &1514456513 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: f3e1b3cbf3fac6a459b1a602167ad311, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &1660149749 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 68f09f0f82599b5448579854e622a4c1, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!1 &1681694460 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1681694463} + - component: {fileID: 1681694462} + - component: {fileID: 1681694461} + m_Layer: 0 + m_Name: Code-based tests runner + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1681694461 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1681694460} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3cf5cb9e1ef590c48b1f919f2a7bd895, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &1681694462 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1681694460} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 102e512f651ee834f951a2516c1ea3b8, type: 3} + m_Name: + m_EditorClassIdentifier: + m_AssembliesWithTests: + - UnityEngine.TestRunner + - VitalRouter.Tests + testStartedEvent: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1681694461} + m_TargetAssemblyTypeName: UnityEngine.TestTools.TestRunner.Callbacks.PlayModeRunnerCallback, + UnityEngine.TestRunner + m_MethodName: TestStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 30428838} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.TestRunnerCallback, + UnityEditor.TestRunner + m_MethodName: TestStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1514456513} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.Api.CallbacksDelegatorListener, + UnityEditor.TestRunner + m_MethodName: TestStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1660149749} + m_TargetAssemblyTypeName: UnityEngine.TestRunner.Utils.TestRunCallbackListener, + UnityEngine.TestRunner + m_MethodName: TestStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + testFinishedEvent: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1681694461} + m_TargetAssemblyTypeName: UnityEngine.TestTools.TestRunner.Callbacks.PlayModeRunnerCallback, + UnityEngine.TestRunner + m_MethodName: TestFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 30428838} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.TestRunnerCallback, + UnityEditor.TestRunner + m_MethodName: TestFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1514456513} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.Api.CallbacksDelegatorListener, + UnityEditor.TestRunner + m_MethodName: TestFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1660149749} + m_TargetAssemblyTypeName: UnityEngine.TestRunner.Utils.TestRunCallbackListener, + UnityEngine.TestRunner + m_MethodName: TestFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + runStartedEvent: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1681694461} + m_TargetAssemblyTypeName: UnityEngine.TestTools.TestRunner.Callbacks.PlayModeRunnerCallback, + UnityEngine.TestRunner + m_MethodName: RunStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 30428838} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.TestRunnerCallback, + UnityEditor.TestRunner + m_MethodName: RunStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1514456513} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.Api.CallbacksDelegatorListener, + UnityEditor.TestRunner + m_MethodName: RunStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1660149749} + m_TargetAssemblyTypeName: UnityEngine.TestRunner.Utils.TestRunCallbackListener, + UnityEngine.TestRunner + m_MethodName: RunStarted + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + runFinishedEvent: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1681694461} + m_TargetAssemblyTypeName: UnityEngine.TestTools.TestRunner.Callbacks.PlayModeRunnerCallback, + UnityEngine.TestRunner + m_MethodName: RunFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 30428838} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.TestRunnerCallback, + UnityEditor.TestRunner + m_MethodName: RunFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1514456513} + m_TargetAssemblyTypeName: UnityEditor.TestTools.TestRunner.Api.CallbacksDelegatorListener, + UnityEditor.TestRunner + m_MethodName: RunFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + - m_Target: {fileID: 1660149749} + m_TargetAssemblyTypeName: UnityEngine.TestRunner.Utils.TestRunCallbackListener, + UnityEngine.TestRunner + m_MethodName: RunFinished + m_Mode: 0 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 + settings: + filters: + - assemblyNames: [] + groupNames: [] + categoryNames: [] + testNames: + - VitalRouter.Tests.MRubySharedStateTest.FuzzyMatcher + synchronousOnly: 0 + sceneBased: 0 + originalScene: Assets/Sandbox/SampleScene.unity + bootstrapScene: Assets/InitTestScene638644545017947350.unity + runInBackgroundValue: 1 + consoleErrorPaused: 0 + orderedTestNames: [] + featureFlags: + fileCleanUpCheck: 0 + requiresSplashScreen: 0 + strictDomainReload: 0 + retryCount: 0 + repeatCount: 0 + automated: 0 + randomOrderSeed: 0 +--- !u!4 &1681694463 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1681694460} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + 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!1660057539 &9223372036854775807 +SceneRoots: + m_ObjectHideFlags: 0 + m_Roots: + - {fileID: 1681694463} diff --git a/src/VitalRouter.Unity/Assets/InitTestScene638644545017947350.unity.meta b/src/VitalRouter.Unity/Assets/InitTestScene638644545017947350.unity.meta new file mode 100644 index 0000000..d3519c7 --- /dev/null +++ b/src/VitalRouter.Unity/Assets/InitTestScene638644545017947350.unity.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 20074cd99afac4a92b08cee0165f4f2c +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/MRubySharedState.cs b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/MRubySharedState.cs index a5ad2db..acc5f06 100644 --- a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/MRubySharedState.cs +++ b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/MRubySharedState.cs @@ -4,7 +4,7 @@ namespace VitalRouter.MRuby { - public class MRubySharedState + public unsafe class MRubySharedState { readonly MRubyContext context; readonly ConcurrentDictionary values = new(); @@ -14,10 +14,12 @@ public MRubySharedState(MRubyContext context) this.context = context; } - public unsafe void Remove(string key) + public IEnumerable Keys() => values.Keys; + + public void Remove(string key) { var keyMaxBytes = System.Text.Encoding.UTF8.GetMaxByteCount(key.Length + 1); - Span keyUtf8 = keyMaxBytes > 255 + var keyUtf8 = keyMaxBytes > 255 ? new byte[keyMaxBytes] : stackalloc byte[keyMaxBytes]; var keyBytesWritten = System.Text.Encoding.UTF8.GetBytes(key, keyUtf8); @@ -27,11 +29,13 @@ public unsafe void Remove(string key) { NativeMethods.MrbStateRemove(context.DangerousGetPtr(), keyPtr); } + values.TryRemove(key, out _); } - public unsafe void Clear() + public void Clear() { NativeMethods.MrbStateClear(context.DangerousGetPtr()); + values.Clear(); } public bool TryGet(string key, out T value) @@ -83,10 +87,10 @@ public void Set(string key, string value) Set(typeof(string), key, value); } - unsafe void Set(Type type, string key, object boxedValue) + void Set(Type type, string key, object boxedValue) { var keyMaxBytes = System.Text.Encoding.UTF8.GetMaxByteCount(key.Length + 1); - Span keyUtf8 = keyMaxBytes > 255 + var keyUtf8 = keyMaxBytes > 255 ? new byte[keyMaxBytes] : stackalloc byte[keyMaxBytes]; var keyBytesWritten = System.Text.Encoding.UTF8.GetBytes(key, keyUtf8); @@ -110,7 +114,7 @@ unsafe void Set(Type type, string key, object boxedValue) { var value = (string)boxedValue; var valueMaxBytes = System.Text.Encoding.UTF8.GetMaxByteCount(value.Length + 1); - Span valueUtf8 = valueMaxBytes > 255 + var valueUtf8 = valueMaxBytes > 255 ? new byte[valueMaxBytes] : stackalloc byte[valueMaxBytes]; var valueBytesWritten = System.Text.Encoding.UTF8.GetBytes(value, valueUtf8); diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-arm64/VitalRouter.MRuby.Native.so b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-arm64/VitalRouter.MRuby.Native.so index 0dd7ce5..5f56822 100755 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-arm64/VitalRouter.MRuby.Native.so and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-arm64/VitalRouter.MRuby.Native.so differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-x64/VitalRouter.MRuby.Native.so b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-x64/VitalRouter.MRuby.Native.so index 5ae0bf5..93cd222 100755 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-x64/VitalRouter.MRuby.Native.so and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/android-x64/VitalRouter.MRuby.Native.so differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-arm64/VitalRouter.MRuby.Native.a b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-arm64/VitalRouter.MRuby.Native.a index b7e8a21..4906669 100644 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-arm64/VitalRouter.MRuby.Native.a and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-arm64/VitalRouter.MRuby.Native.a differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-x64/VitalRouter.MRuby.Native.a b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-x64/VitalRouter.MRuby.Native.a index b15c818..91094fc 100644 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-x64/VitalRouter.MRuby.Native.a and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/ios-x64/VitalRouter.MRuby.Native.a differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-arm64/VitalRouter.MRuby.Native.so b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-arm64/VitalRouter.MRuby.Native.so index 92dd5ad..6e8551a 100755 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-arm64/VitalRouter.MRuby.Native.so and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-arm64/VitalRouter.MRuby.Native.so differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-x64/VitalRouter.MRuby.Native.so b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-x64/VitalRouter.MRuby.Native.so index 76edb91..70c7800 100755 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-x64/VitalRouter.MRuby.Native.so and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/linux-x64/VitalRouter.MRuby.Native.so differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-arm64/VitalRouter.MRuby.Native.dylib b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-arm64/VitalRouter.MRuby.Native.dylib index b6ea5c5..3c212f1 100755 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-arm64/VitalRouter.MRuby.Native.dylib and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-arm64/VitalRouter.MRuby.Native.dylib differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-universal/VitalRouter.MRuby.Native.dylib b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-universal/VitalRouter.MRuby.Native.dylib index d772c60..c3cdf0e 100755 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-universal/VitalRouter.MRuby.Native.dylib and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-universal/VitalRouter.MRuby.Native.dylib differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-x64/VitalRouter.MRuby.Native.dylib b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-x64/VitalRouter.MRuby.Native.dylib index 4945d87..07ff8dc 100755 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-x64/VitalRouter.MRuby.Native.dylib and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/macOS-x64/VitalRouter.MRuby.Native.dylib differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-arm64/VitalRouter.MRuby.Native.a b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-arm64/VitalRouter.MRuby.Native.a index fbb3a51..0165178 100644 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-arm64/VitalRouter.MRuby.Native.a and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-arm64/VitalRouter.MRuby.Native.a differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-x64/VitalRouter.MRuby.Native.a b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-x64/VitalRouter.MRuby.Native.a index 048d60d..1ca59e3 100644 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-x64/VitalRouter.MRuby.Native.a and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/visionos-x64/VitalRouter.MRuby.Native.a differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/wasm/VitalRouter.MRuby.Native.a b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/wasm/VitalRouter.MRuby.Native.a index 0ae75fc..719f3a9 100644 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/wasm/VitalRouter.MRuby.Native.a and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/wasm/VitalRouter.MRuby.Native.a differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/windows-x64/VitalRouter.MRuby.Native.dll b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/windows-x64/VitalRouter.MRuby.Native.dll index bf95d7e..d5e33b5 100644 Binary files a/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/windows-x64/VitalRouter.MRuby.Native.dll and b/src/VitalRouter.Unity/Assets/VitalRouter.MRuby/Runtime/Plugins/windows-x64/VitalRouter.MRuby.Native.dll differ diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.Tests/MRubySharedStateTest.cs b/src/VitalRouter.Unity/Assets/VitalRouter.Tests/MRubySharedStateTest.cs new file mode 100644 index 0000000..37f54b8 --- /dev/null +++ b/src/VitalRouter.Unity/Assets/VitalRouter.Tests/MRubySharedStateTest.cs @@ -0,0 +1,77 @@ +#if UNITY_2022_2_OR_NEWER +using NUnit.Framework; +using VitalRouter.MRuby; + +namespace VitalRouter.Tests +{ + [TestFixture] + public class MRubySharedStateTest + { + [Test] + public void GetSet() + { + using var context = MRubyContext.Create(Router.Default, new DummyCommandPreset()); + var sharedState = new MRubySharedState(context); + + sharedState.Set("string_value", "bar"); + Assert.That(sharedState.Get("string_value"), Is.EqualTo("bar")); + Assert.That(context.Evaluate("state[:string_value]"), Is.EqualTo("bar")); + + sharedState.Set("int_value", 123); + Assert.That(sharedState.Get("int_value"), Is.EqualTo(123)); + Assert.That(context.Evaluate("state[:int_value]"), Is.EqualTo(123)); + } + + [Test] + public void Remove() + { + using var context = MRubyContext.Create(Router.Default, new DummyCommandPreset()); + var sharedState = new MRubySharedState(context); + + sharedState.Set("a", "hoge"); + sharedState.Set("b", "fuga"); + sharedState.Remove("a"); + Assert.That(sharedState.TryGet("a", out _), Is.False); + Assert.That(sharedState.TryGet("b", out _), Is.True); + Assert.That(context.Evaluate("state[:a]"), Is.Null); + Assert.That(context.Evaluate("state[:b]"), Is.EqualTo("fuga")); + } + + [Test] + public void Clear() + { + using var context = MRubyContext.Create(Router.Default, new DummyCommandPreset()); + var sharedState = new MRubySharedState(context); + + sharedState.Set("string_value", "bar"); + sharedState.Clear(); + Assert.That(sharedState.TryGet("string_value", out _), Is.False); + Assert.That(context.Evaluate("state[:string_value]"), Is.Null); + } + + [Test] + public void FuzzyMatcher() + { + using var context = MRubyContext.Create(Router.Default, new DummyCommandPreset()); + var sharedState = new MRubySharedState(context); + + sharedState.Set("a", "bar"); + sharedState.Set("b", 123); + sharedState.Set("c", 1234.0f); + sharedState.Set("d", true); + sharedState.Set("e", false); + + Assert.That(context.Evaluate("state[:a].is?(:bar)"), Is.True); + Assert.That(context.Evaluate("state[:a].is?('bar')"), Is.True); + Assert.That(context.Evaluate("state[:b].is?(123)"), Is.True); + Assert.That(context.Evaluate("state[:c].is?(1234.0)"), Is.True); + Assert.That(context.Evaluate("state[:d].is?(true)"), Is.True); + Assert.That(context.Evaluate("state[:e].is?(false)"), Is.True); + Assert.That(context.Evaluate("state[:e].is?(nil)"), Is.True); + Assert.That(context.Evaluate("state[:e].is?(true)"), Is.False); + Assert.That(context.Evaluate("state[:f].is?(nil)"), Is.True); + Assert.That(context.Evaluate("state[:f].is?(false)"), Is.True); + } + } +} +#endif \ No newline at end of file diff --git a/src/VitalRouter.Unity/Assets/VitalRouter.Tests/MRubySharedStateTest.cs.meta b/src/VitalRouter.Unity/Assets/VitalRouter.Tests/MRubySharedStateTest.cs.meta new file mode 100644 index 0000000..76c9890 --- /dev/null +++ b/src/VitalRouter.Unity/Assets/VitalRouter.Tests/MRubySharedStateTest.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 071e43e98540406d87e3916a5c01aaab +timeCreated: 1728821884 \ No newline at end of file diff --git a/src/vitalrouter-mruby/mrblib/core_ext.rb b/src/vitalrouter-mruby/mrblib/core_ext.rb index fba9f01..19e0ca9 100644 --- a/src/vitalrouter-mruby/mrblib/core_ext.rb +++ b/src/vitalrouter-mruby/mrblib/core_ext.rb @@ -1,16 +1,40 @@ +class Object + def is?(other) = self == other + alias :is :is? +end + +class NilClass + def is?(other) = !other + alias :is :is? +end + +class FalseClass + def is?(other) = !other + alias :is :is? +end + +class TrueClass + def is?(other) = !!other + alias :is :is? +end + +class Symbol + def is?(other) = self == other.to_sym + alias :is :is? +end + +class String + def is?(other) = self == other.to_s + alias :is :is? +end + class Numeric - def secs - VitalRouter::TimeDuration.new self.to_f - end + def secs = VitalRouter::TimeDuration.new(self.to_f) alias :sec :secs - def millisecs - VitalRouter::TimeDuration.new self * 0.001 - end + def millisecs = VitalRouter::TimeDuration.new(self * 0.001) alias :millisec :millisecs - def frames - VitalRouter::FrameDuration.new self.to_i - end + def frames = VitalRouter::FrameDuration.new(self.to_i) alias :frame :frames end diff --git a/src/vitalrouter-mruby/mrblib/vitalrouter.rb b/src/vitalrouter-mruby/mrblib/vitalrouter.rb index 0237a59..4100e58 100644 --- a/src/vitalrouter-mruby/mrblib/vitalrouter.rb +++ b/src/vitalrouter-mruby/mrblib/vitalrouter.rb @@ -13,6 +13,14 @@ def [](key) def []=(key, val) (@variables ||= {})[key] = val end + + def delete(key) + @variables&.delete(key) + end + + def clear + @variables&.clear + end end class TimeDuration diff --git a/src/vitalrouter-mruby/src/vitalrouter-mruby.c b/src/vitalrouter-mruby/src/vitalrouter-mruby.c index 8456694..0c21908 100644 --- a/src/vitalrouter-mruby/src/vitalrouter-mruby.c +++ b/src/vitalrouter-mruby/src/vitalrouter-mruby.c @@ -136,14 +136,16 @@ extern void vitalrouter_mrb_state_set_string(vitalrouter_mrb_ctx *ctx, char *key shared_state_set(ctx->mrb, key, mrb_str_new_cstr(ctx->mrb, value)); } -extern void vitalrouter_mrb_state_remove(vitalrouter_mrb_ctx *ctx,char *key) { +extern void vitalrouter_mrb_state_remove(vitalrouter_mrb_ctx *ctx,char *key) +{ mrb_value self = mrb_obj_value(ctx->mrb->top_self); mrb_value state = mrb_funcall(ctx->mrb, self, "state", 0); mrb_value sym = mrb_symbol_value(mrb_intern_cstr(ctx->mrb, key)); mrb_funcall(ctx->mrb, state, "delete", 1, sym); } -extern void vitalrouter_mrb_state_clear(vitalrouter_mrb_ctx *ctx) { +extern void vitalrouter_mrb_state_clear(vitalrouter_mrb_ctx *ctx) +{ mrb_value self = mrb_obj_value(ctx->mrb->top_self); mrb_value state = mrb_funcall(ctx->mrb, self, "state", 0); mrb_funcall(ctx->mrb, state, "clear", 0, NULL); diff --git a/src/vitalrouter-mruby/src/vitalrouter-mruby.h b/src/vitalrouter-mruby/src/vitalrouter-mruby.h index 7745b03..729fc66 100644 --- a/src/vitalrouter-mruby/src/vitalrouter-mruby.h +++ b/src/vitalrouter-mruby/src/vitalrouter-mruby.h @@ -42,6 +42,7 @@ extern void vitalrouter_mrb_ctx_dispose(vitalrouter_mrb_ctx *ctx); extern void vitalrouter_mrb_callbacks_set(vitalrouter_mrb_ctx *ctx, vitalrouter_mrb_command_cb on_command, vitalrouter_mrb_error_cb on_error); + extern void vitalrouter_mrb_state_set_int32(vitalrouter_mrb_ctx *ctx, char *key,int32_t value); extern void vitalrouter_mrb_state_set_float(vitalrouter_mrb_ctx *ctx, char *key, float value); extern void vitalrouter_mrb_state_set_bool(vitalrouter_mrb_ctx *ctx, char *key,int32_t value);