From 87d22b312ba9e45b6b4e9f9cca8480a17afbf49a Mon Sep 17 00:00:00 2001 From: Vasiliy Doylov Date: Wed, 18 Jun 2025 16:38:02 +0300 Subject: [PATCH] Utils: Add `type` json serialization workaround Signed-off-by: Vasiliy Doylov --- src/logic/meson.build | 5 +++++ src/logic/utils.vala | 46 +++++++++++++++++++++++++++++++++++++++++++ src/meson.build | 2 ++ 3 files changed, 53 insertions(+) create mode 100644 src/logic/meson.build create mode 100644 src/logic/utils.vala diff --git a/src/logic/meson.build b/src/logic/meson.build new file mode 100644 index 0000000..71ba976 --- /dev/null +++ b/src/logic/meson.build @@ -0,0 +1,5 @@ +singularity_sources += [ + 'logic/utils.vala', +] + +singularity_deps += dependency('json-glib-1.0') diff --git a/src/logic/utils.vala b/src/logic/utils.vala new file mode 100644 index 0000000..21db77e --- /dev/null +++ b/src/logic/utils.vala @@ -0,0 +1,46 @@ +/* utils.vala + * + * Copyright 2025 Vasiliy Doylov (NekoCWD) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +/* + * Collection of ugly workarounds and hacks + */ +namespace Singularity.Utils { + /* + * In vala we can't use `type` as field name, but we need to use it + * to make out saves compatible with SingBox configuration. + */ + public void fix_type(ref Json.Node node) { + const string TYPE_NAME_SRC = "type-name"; + const string TYPE_NAME_DST = "type"; + + var obj = node.get_object(); + if (obj.has_member(TYPE_NAME_SRC)) { + var type_name = obj.get_member(TYPE_NAME_SRC); + obj.set_member(TYPE_NAME_DST, type_name); + obj.remove_member(TYPE_NAME_SRC); + } else if (obj.has_member(TYPE_NAME_DST)) { + var type_name = obj.get_member(TYPE_NAME_DST); + obj.set_member(TYPE_NAME_SRC, type_name); + obj.remove_member(TYPE_NAME_DST); + } else { + warning("Object has no type to fix it"); + } + } +} diff --git a/src/meson.build b/src/meson.build index d8cc02e..d7da0af 100644 --- a/src/meson.build +++ b/src/meson.build @@ -10,6 +10,8 @@ singularity_deps = [ dependency('libadwaita-1', version: '>= 1.4'), ] +subdir('logic') + blueprints = custom_target( 'blueprints', input: files(