From cbdcf2d6c673ebe81e7908de2cc6689a0c3321a7 Mon Sep 17 00:00:00 2001 From: pypy Date: Sun, 1 Nov 2020 19:55:20 +0900 Subject: [PATCH] add SharedVariable --- Browser.cs | 1 + MainForm.cs | 1 + SharedVariable.cs | 88 +++++++++++++++++++++++++++++++++++++++++++++++ VRCX.csproj | 1 + VRForm.cs | 2 ++ 5 files changed, 93 insertions(+) create mode 100644 SharedVariable.cs diff --git a/Browser.cs b/Browser.cs index bcd49f13..d0007c9f 100644 --- a/Browser.cs +++ b/Browser.cs @@ -32,6 +32,7 @@ namespace VRCX CamelCaseJavascriptNames = false }; JavascriptObjectRepository.Register("VRCX", VRCX.Instance, true, options); + JavascriptObjectRepository.Register("SharedVariable", SharedVariable.Instance, true, options); JavascriptObjectRepository.Register("VRCXStorage", VRCXStorage.Instance, false, options); JavascriptObjectRepository.Register("SQLite", SQLite.Instance, true, options); } diff --git a/MainForm.cs b/MainForm.cs index 6d9a3792..00e136df 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -52,6 +52,7 @@ namespace VRCX CamelCaseJavascriptNames = false }; Browser.JavascriptObjectRepository.Register("VRCX", VRCX.Instance, true, options); + Browser.JavascriptObjectRepository.Register("SharedVariable", SharedVariable.Instance, true, options); Browser.JavascriptObjectRepository.Register("VRCXStorage", VRCXStorage.Instance, false, options); Browser.JavascriptObjectRepository.Register("SQLite", SQLite.Instance, true, options); Browser.JavascriptObjectRepository.Register("LogWatcher", LogWatcher.Instance, true, options); diff --git a/SharedVariable.cs b/SharedVariable.cs new file mode 100644 index 00000000..93010df6 --- /dev/null +++ b/SharedVariable.cs @@ -0,0 +1,88 @@ +// Copyright(c) 2020 pypy. All rights reserved. +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +using System.Collections.Generic; +using System.Threading; + +namespace VRCX +{ + public class SharedVariable + { + public static SharedVariable Instance { get; private set; } + private readonly ReaderWriterLockSlim m_MapLock; + private readonly Dictionary m_Map; + + static SharedVariable() + { + Instance = new SharedVariable(); + } + + public SharedVariable() + { + m_MapLock = new ReaderWriterLockSlim(); + m_Map = new Dictionary(); + } + + public void Clear() + { + m_MapLock.EnterWriteLock(); + try + { + if (m_Map.Count > 0) + { + m_Map.Clear(); + } + } + finally + { + m_MapLock.ExitWriteLock(); + } + } + + public bool Remove(string key) + { + m_MapLock.EnterWriteLock(); + try + { + return m_Map.Remove(key); + } + finally + { + m_MapLock.ExitWriteLock(); + } + } + + public string Get(string key) + { + m_MapLock.EnterReadLock(); + try + { + if (m_Map.TryGetValue(key, out string value) == true) + { + return value; + } + } + finally + { + m_MapLock.ExitReadLock(); + } + + return string.Empty; + } + + public void Set(string key, string value) + { + m_MapLock.EnterWriteLock(); + try + { + m_Map[key] = value; + } + finally + { + m_MapLock.ExitWriteLock(); + } + } + } +} \ No newline at end of file diff --git a/VRCX.csproj b/VRCX.csproj index e18ae069..bbd2864e 100644 --- a/VRCX.csproj +++ b/VRCX.csproj @@ -132,6 +132,7 @@ + Form diff --git a/VRForm.cs b/VRForm.cs index 20807554..39b2552f 100644 --- a/VRForm.cs +++ b/VRForm.cs @@ -47,9 +47,11 @@ namespace VRCX CamelCaseJavascriptNames = false }; Browser1.JavascriptObjectRepository.Register("VRCX", VRCX.Instance, true, options); + Browser1.JavascriptObjectRepository.Register("SharedVariable", SharedVariable.Instance, true, options); Browser1.JavascriptObjectRepository.Register("VRCXStorage", VRCXStorage.Instance, false, options); Browser1.JavascriptObjectRepository.Register("SQLite", SQLite.Instance, true, options); Browser2.JavascriptObjectRepository.Register("VRCX", VRCX.Instance, true, options); + Browser2.JavascriptObjectRepository.Register("SharedVariable", SharedVariable.Instance, true, options); Browser2.JavascriptObjectRepository.Register("VRCXStorage", VRCXStorage.Instance, false, options); Browser2.JavascriptObjectRepository.Register("SQLite", SQLite.Instance, true, options); Browser1.IsBrowserInitializedChanged += (A, B) =>