summaryrefslogtreecommitdiff
path: root/plugins/svd/src/settings.rs
blob: 7f57f0bfe40c1b48075bb1d98919b8e9369f00a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use binaryninja::binary_view::BinaryView;
use binaryninja::settings::{QueryOptions, Settings};
use serde_json::json;
use std::path::PathBuf;

#[derive(Debug, Clone)]
pub struct LoadSettings {
    pub add_backing_regions: bool,
    pub add_comments: bool,
    pub auto_load_file: Option<PathBuf>,
}

impl LoadSettings {
    pub const ADD_BACKING_REGIONS_DEFAULT: bool = true;
    pub const ADD_BACKING_REGIONS_SETTING: &'static str = "analysis.svd.addBackingRegions";
    pub const ADD_COMMENTS_DEFAULT: bool = true;
    pub const ADD_COMMENTS_SETTING: &'static str = "analysis.svd.addComments";
    pub const AUTO_LOAD_FILE_DEFAULT: &'static str = "";
    pub const AUTO_LOAD_FILE_SETTING: &'static str = "analysis.svd.autoLoadFile";

    pub fn register() {
        let bn_settings = Settings::new();

        let add_backing_region_props = json!({
            "title" : "Add Backing Regions",
            "type" : "boolean",
            "default" : Self::ADD_BACKING_REGIONS_DEFAULT,
            "description" : "Whether to add backing regions. Backing regions allow you to write to the underlying memory of a view, but will take up space in the BNDB.",
        });
        bn_settings.register_setting_json(
            Self::ADD_BACKING_REGIONS_SETTING,
            &add_backing_region_props.to_string(),
        );

        let add_comments_props = json!({
            "title" : "Add Comments",
            "type" : "boolean",
            "default" : Self::ADD_COMMENTS_DEFAULT,
            "description" : "Whether to add comments. If you see comment placement is off, try disabling this.",
        });
        bn_settings
            .register_setting_json(Self::ADD_COMMENTS_SETTING, &add_comments_props.to_string());

        let file_props = json!({
            "title" : "SVD File",
            "type" : "string",
            "default" : Self::AUTO_LOAD_FILE_DEFAULT,
            "description" : "The SVD File to automatically load when opening the view.",
            "uiSelectionAction" : "file"
        });
        bn_settings.register_setting_json(Self::AUTO_LOAD_FILE_SETTING, &file_props.to_string());
    }

    pub fn from_view_settings(view: &BinaryView) -> Self {
        let mut load_settings = LoadSettings::default();
        let settings = Settings::new();
        let mut query_opts = QueryOptions::new_with_view(view);
        if settings.contains(Self::ADD_BACKING_REGIONS_SETTING) {
            load_settings.add_backing_regions =
                settings.get_bool_with_opts(Self::ADD_BACKING_REGIONS_SETTING, &mut query_opts);
        }
        if settings.contains(Self::ADD_COMMENTS_SETTING) {
            load_settings.add_comments =
                settings.get_bool_with_opts(Self::ADD_COMMENTS_SETTING, &mut query_opts);
        }
        if settings.contains(Self::AUTO_LOAD_FILE_SETTING) {
            let path_str =
                settings.get_string_with_opts(Self::AUTO_LOAD_FILE_SETTING, &mut query_opts);
            if !path_str.is_empty() {
                let path = PathBuf::from(path_str.to_string());
                load_settings.auto_load_file = Some(path);
            }
        }
        load_settings
    }
}

impl Default for LoadSettings {
    fn default() -> Self {
        Self {
            add_backing_regions: Self::ADD_BACKING_REGIONS_DEFAULT,
            add_comments: Self::ADD_COMMENTS_DEFAULT,
            auto_load_file: None,
        }
    }
}