137 lines
4.0 KiB
C#
137 lines
4.0 KiB
C#
#nullable enable
|
|
namespace mbts.config {
|
|
using bts;
|
|
|
|
using Google.Protobuf;
|
|
|
|
using System;
|
|
using System.Diagnostics.Contracts;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
|
|
public static class Configuration {
|
|
static object _config_lock = new();
|
|
|
|
static State _state = State.Uninitialized;
|
|
|
|
enum State {
|
|
Uninitialized,
|
|
Error,
|
|
OK
|
|
}
|
|
|
|
public static T get<T>() where T : new() =>
|
|
get<T>(_default_type_key<T>());
|
|
|
|
public static T get<T>(string key) where T : new() {
|
|
var buffer = _get_buffer(key);
|
|
|
|
// Default only protobufs serialize to no bytes
|
|
if (buffer is null) {
|
|
return new();
|
|
}
|
|
|
|
using var ms = new MemoryStream(buffer);
|
|
try {
|
|
return typeof(IMessage).IsAssignableFrom(typeof(T)) ? proto3_impl() : proto2_impl();
|
|
} catch (Exception ex) {
|
|
Log.assert(false, _fmt_deserialization_error(typeof(T), key, ex));
|
|
}
|
|
return new();
|
|
|
|
T proto2_impl() =>
|
|
ProtoBuf.Serializer.Deserialize<T>(ms);
|
|
|
|
T proto3_impl() {
|
|
var rv = new T();
|
|
((IMessage)rv).MergeFrom(ms);
|
|
return rv;
|
|
}
|
|
}
|
|
|
|
public static void initialize(string? config_source = null) {
|
|
if (_state == State.OK) {
|
|
return;
|
|
}
|
|
// Considered doing this async on a background thread but it wouldn't do any good. The next
|
|
// guy down the line to ask for config will just block until this completes.
|
|
try {
|
|
BTS.init(config_source);
|
|
} catch (ApplicationException) {
|
|
_state = State.Error;
|
|
throw;
|
|
}
|
|
_state = State.OK;
|
|
}
|
|
|
|
public static void initialize_for_test(string config = "") {
|
|
_raw_initialize_for_test(config);
|
|
// still need init_spark_lib
|
|
BTS.init();
|
|
_state = State.OK;
|
|
}
|
|
|
|
public static unsafe bool try_initialize(out string error) {
|
|
_reset();
|
|
try {
|
|
BTS.init();
|
|
} catch (ApplicationException e) {
|
|
error = e.Message;
|
|
return false;
|
|
}
|
|
error = string.Empty;
|
|
return true;
|
|
}
|
|
|
|
[DllImport("libspark")]
|
|
internal static extern int run_config_query(
|
|
string[] keysv,
|
|
int keysc,
|
|
string[] excluded_keysv,
|
|
int excluded_keysc,
|
|
bool all,
|
|
bool list_keys,
|
|
bool verbose);
|
|
|
|
[Pure]
|
|
static string _default_type_key<T>() =>
|
|
typeof(T).FullName ?? throw new InvalidOperationException();
|
|
|
|
[Pure]
|
|
static string _fmt_deserialization_error(Type type, string key, Exception error) =>
|
|
$"Failed to deserialize config '{key}' as {type}: {error}";
|
|
|
|
static byte[]? _get_buffer(string key_name) {
|
|
/// The native get_buffer (<see cref="_raw_get_buffer(string, ref int)"/>) is not thread
|
|
/// safe, so this call must be protected from re-entrancy.
|
|
lock (_config_lock) {
|
|
if (_state == State.Uninitialized) { initialize(); }
|
|
if (_state == State.Error) { return null; }
|
|
|
|
int length = 0;
|
|
byte[] buffer;
|
|
IntPtr pbuffer = _raw_get_buffer(key_name, ref length);
|
|
if (length > 0) {
|
|
buffer = new byte[length];
|
|
Marshal.Copy(pbuffer, buffer, 0, length);
|
|
return buffer;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
[DllImport("libspark", EntryPoint = "get_buffer")]
|
|
static extern IntPtr _raw_get_buffer(string key_name, ref int length);
|
|
|
|
[DllImport("libspark", EntryPoint = "initialize_nothrow")]
|
|
static extern bool _initialize_nothrow(StringBuilder buffer, int length);
|
|
|
|
[DllImport("libspark", EntryPoint = "initialize_for_test")]
|
|
static extern void _raw_initialize_for_test(string buffer);
|
|
|
|
[DllImport("libspark", EntryPoint = "reset")]
|
|
static extern void _reset();
|
|
}
|
|
}
|