Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Registry variables. Sharing values by multiple script processes.
#1
LA scripts run in separate processes. Even the same script runs in new process each time. Each process has its own static variables, and they disappear when the process ends. To share values between processes, can be used a file, registry, shared memory, etc. This class uses the registry.

Example.
 
Code:
Copy      Help
/*/ c \RegistryVariables.cs; /*/

print.clear();

var rv = new RegistryVariables();
rv.SetInt("i", 10);
rv.SetLong("q", -2);
rv.SetString("s", "test");
rv.SetBinary("b", [1, 2, 3, 4]);
rv.SetObject("p", new POINT(5, 6));
//print.it(rv.Exists("p"));
//rv.DeleteAll();
//rv.Delete("s");

print.it(rv.GetInt("i"), rv.GetLong("q"), rv.GetString("s"), rv.GetBinary("b"), rv.GetObject("p", out POINT p), p);

To clear the key when LA starts, add this code in a script that runs when LA starts (see Options > Workspace > Run...):
Code:
Copy      Help
/*/ role editorExtension; c \RegistryVariables.cs; /*/
new RegistryVariables().DeleteAll();
 

 
Code:
Copy      Help
// class "RegistryVariables.cs"
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Encodings.Web;

/// <summary>
///
Sets/gets values in a registry key.
/// </summary>
///
<example>
///
<code><![CDATA[
/// /*/ c \RegistryVariables.cs; /*/
/// var rv = new RegistryVariables();
/// rv.SetInt("i", 10);
/// rv.SetLong("q", -2);
/// rv.SetString("s", "test");
/// rv.SetBinary("b", [1, 2, 3, 4]);
/// rv.SetObject("p", new POINT(5, 6));
/// print.it(rv.GetInt("i"), rv.GetLong("q"), rv.GetString("s"), rv.GetBinary("b"), rv.GetObject("p", out POINT p), p);
/// ]]></code>
///
To clear the key when LA starts, add this code in a script that runs when LA starts (see Options > Workspace > Run...):
/// <code><![CDATA[
/// /*/ role editorExtension; c \RegistryVariables.cs; /*/
/// new RegistryVariables().DeleteAll();
/// ]]></code>
///
</example>
public class RegistryVariables {
    string _key;
    
    /// <summary>
    ///
Allows to specify the registry key.
    /// </summary>
    ///
<param name="key">Registry key. Must start with <c>@"HKEY_CURRENT_USER\"</c>.</param>
    public RegistryVariables(string key = @"HKEY_CURRENT_USER\Software\My\Variables") {
        if (!key.Starts(@"HKEY_CURRENT_USER\")) throw new ArgumentException("key must start with @\"HKEY_CURRENT_USER\".");
        _key = key;
    }

    
    /// <summary>
    ///
Sets an int (DWORD) value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="value">Value.</param>
    public void SetInt(string name, int value) => Registry.SetValue(_key, name, value);
    
    /// <summary>
    ///
Gets an int (DWORD) value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="defaultValue">Return this value if the registry key or value does not exist or is not DWORD.</param>
    public int GetInt(string name, int defaultValue = 0) => Registry.GetValue(_key, name, null) is int r ? r : defaultValue;
    
    /// <summary>
    ///
Sets a long (QWORD) value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="value">Value.</param>
    public void SetLong(string name, long value) => Registry.SetValue(_key, name, value, RegistryValueKind.QWord);
    
    /// <summary>
    ///
Gets a long (QWORD) value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="defaultValue">Return this value if the registry key or value does not exist or is not QWORD.</param>
    public long GetLong(string name, long defaultValue = 0) => Registry.GetValue(_key, name, null) is long r ? r : defaultValue;
    
    /// <summary>
    ///
Sets a string value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="value">Value.</param>
    public void SetString(string name, string value) => Registry.SetValue(_key, name, value);
    
    /// <summary>
    ///
Gets a string value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<returns>null if the registry key or value does not exist or is not string.</returns>
    public string GetString(string name) => Registry.GetValue(_key, name, null) as string;
    
    /// <summary>
    ///
Sets a binary data value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="value">Value.</param>
    public void SetBinary(string name, byte[] value) => Registry.SetValue(_key, name, value);
    
    /// <summary>
    ///
Gets a binary data value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<returns>null if the registry key or value does not exist or is not binary.</returns>
    public byte[] GetBinary(string name) => Registry.GetValue(_key, name, null) as byte[];
    
    /// <summary>
    ///
JSON-serializes an object of type <b>T</b>, and sets a string value.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="value">Value.</param>
    public void SetObject<T>(string name, T value) {
        var s = JsonSerializer.Serialize(value, s_jsOptions.Value);
        SetString(name, s);
    }

    
    /// <summary>
    ///
Gets a string value, and JSON-deserializes to object of type <b>T</b>.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<param name="value">Receives the object, or <c>default(T)</c> if returns false.</param>
    ///
<returns>false if the registry key or value does not exist or is not string.</returns>
    public bool GetObject<T>(string name, out T value) {
        if (GetString(name) is not string s) { value = default(T); return false; }
        value = JsonSerializer.Deserialize<T>(s, s_jsOptions.Value);
        return true;
    }

    
    static readonly Lazy<JsonSerializerOptions> s_jsOptions = new(() => new() {
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        IncludeFields = true,
        IgnoreReadOnlyFields = true,
        IgnoreReadOnlyProperties = true,
        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
    });

    
    /// <summary>
    ///
Registry value exists?
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    ///
<returns>true if exists.</returns>
    public bool Exists(string name) => Registry.GetValue(_key, name, null) is not null;
    
    /// <summary>
    ///
Deletes a registry value if exists.
    /// </summary>
    ///
<param name="name">Registry value name.</param>
    public void Delete(string name) {
        using var k = Registry.CurrentUser.OpenSubKey(_key[18..], true);
        k?.DeleteValue(name, throwOnMissingValue: false);
    }

    
    /// <summary>
    ///
Deletes all values from the registry key.
    /// </summary>
    public void DeleteAll() {
        Registry.CurrentUser.DeleteSubKeyTree(_key[18..], throwOnMissingSubKey: false);
    }
}


Messages In This Thread
Registry variables. Sharing values by multiple script processes. - by Gintaras - 09-27-2024, 08:36 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)