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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use std::{
    io,
    path::{Path, PathBuf},
};

#[derive(Debug, Clone, Copy)]
enum ResolvedPaths {
    Production,
    Development,
}

const ROOT_MARKER: &str = "$ROOT";
const SYSTEM_MARKER: &str = "$SYSTEM";

#[derive(Clone)]
pub struct Paths {
    mode: ResolvedPaths,
    system_root: PathBuf,
    user_root: PathBuf,
}

impl Paths {
    fn find_dev_root(first_root: &Path) -> Option<PathBuf> {
        let bn = first_root.file_name().and_then(std::ffi::OsStr::to_str);

        if bn == Some("release") || bn == Some("debug") || bn == Some("deps") {
            // A Rust release dir?
            let mut current_root = first_root.parent();
            while let Some(root) = current_root {
                if root.file_name().and_then(std::ffi::OsStr::to_str) == Some("target") {
                    // We need the parent of this one
                    return root.parent().map(Path::to_owned);
                } else {
                    // Keep going up
                    current_root = root.parent();
                }
            }
        }

        None
    }

    fn find_bin_root(first_root: &Path) -> Option<PathBuf> {
        let bn = first_root.file_name().and_then(std::ffi::OsStr::to_str);

        if bn == Some("bin") {
            return first_root.parent().map(|path| {
                let mut p = path.to_owned();
                p.push("share");
                p.push("hyperion");
                p
            });
        }

        None
    }

    fn dev_user_root(user_root: Option<PathBuf>, dev_root: &Path) -> PathBuf {
        if let Some(user_root) = user_root {
            user_root
        } else {
            dev_root.to_owned()
        }
    }

    fn prod_user_root(user_root: Option<PathBuf>) -> PathBuf {
        if let Some(user_root) = user_root {
            user_root
        } else {
            dirs::config_dir()
                .map(|mut path| {
                    path.push("hyperion.rs");
                    path
                })
                .unwrap()
        }
    }

    pub fn new(user_root: Option<PathBuf>) -> io::Result<Self> {
        // Try to find the current exe
        let proc = std::env::current_exe()?;

        // Find the 2nd parent
        let first_root = proc.parent().unwrap();

        if let Some(dev_root) = Self::find_dev_root(first_root) {
            debug!(path = %dev_root.display(), "found development root");

            let user_root = Self::dev_user_root(user_root, &dev_root);
            debug!(path = %user_root.display(), "found user root");

            Ok(Self {
                mode: ResolvedPaths::Development,
                system_root: dev_root,
                user_root,
            })
        } else if let Some(bin_root) = Self::find_bin_root(first_root) {
            debug!(path = %bin_root.display(), "found production root");

            let user_root = Self::prod_user_root(user_root);
            debug!(path = %user_root.display(), "found user root");

            Ok(Self {
                mode: ResolvedPaths::Production,
                system_root: bin_root,
                user_root,
            })
        } else {
            debug!(path = %first_root.display(), "no root found, using binary");

            let user_root = Self::prod_user_root(user_root);
            debug!(path = %user_root.display(), "found user root");

            Ok(Self {
                mode: ResolvedPaths::Production,
                system_root: first_root.to_owned(),
                user_root,
            })
        }
    }

    pub fn resolve_path(&self, p: impl Into<PathBuf>) -> PathBuf {
        let p: PathBuf = p.into();

        if p.is_absolute() {
            // Don't transform absolute paths
            trace!(path = %p.display(), "left unchanged");
            p
        } else {
            let mut out_path = PathBuf::new();
            let mut components = p.components().peekable();

            if let Some(component) = components.peek() {
                let component = component.as_os_str().to_str();
                if component == Some(SYSTEM_MARKER) {
                    out_path.extend(&self.system_root);
                    components.next();

                    if let ResolvedPaths::Development = self.mode {
                        match components.peek().and_then(|cmp| cmp.as_os_str().to_str()) {
                            Some("webconfig") => {
                                // Webconfig mapping
                                components.next();
                                out_path.extend(&PathBuf::from("ext/hyperion.ng/assets/webconfig"));
                            }
                            Some("effects") => {
                                // Effects mapping
                                components.next();
                                out_path.extend(&PathBuf::from("ext/hyperion.ng/effects"));
                            }
                            _ => {
                                // No matching mapping
                            }
                        }
                    }
                } else if component == Some(ROOT_MARKER) {
                    out_path.extend(&self.user_root);
                    components.next();
                }
            }

            out_path.extend(components);

            trace!(src = %p.display(), dst = %out_path.display(), "remapped path");
            out_path
        }
    }
}