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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use path_slash::PathExt;
use std::path::{Path, PathBuf};

/// Build mode of the crate
#[derive(Copy, Clone, Debug)]
pub enum BuildMode {
    Debug,
    Release,
}

/// A builder type that holds all necessary information about the project to
/// generate files in all the right places.
#[derive(Default)]
pub struct Builder {
    godot_project_dir: Option<PathBuf>,
    godot_resource_output_dir: Option<PathBuf>,
    target_dir: Option<PathBuf>,
    lib_name: Option<String>,
    build_mode: Option<BuildMode>,
}

impl Builder {
    /// Construct a new Builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// **REQUIRED** Set the path to the root of the Godot project.
    pub fn with_godot_project_dir(&mut self, dir: impl AsRef<Path>) {
        let dir = dir.as_ref().to_path_buf();

        self.godot_project_dir = Some(dir);
    }

    /// **REQUIRED** Set the path to the root of the Godot project.
    pub fn godot_project_dir(mut self, dir: impl AsRef<Path>) -> Self {
        self.with_godot_project_dir(dir);
        self
    }

    /// Set the path to the directory inside the Godot project to which the
    /// generates files should be saved.
    pub fn with_godot_resource_output_dir(&mut self, dir: impl AsRef<Path>) {
        let dir = dir.as_ref().to_path_buf();

        self.godot_resource_output_dir = Some(dir);
    }

    /// Set the path to the directory inside the Godot project to which the
    /// generates files should be saved.
    pub fn godot_resource_output_dir(mut self, dir: impl AsRef<Path>) -> Self {
        self.with_godot_resource_output_dir(dir);
        self
    }

    /// Set the path to the `target` directory in which cargo creates build
    /// artefacts.
    pub fn with_target_dir(&mut self, dir: impl AsRef<Path>) {
        let dir = dir.as_ref().to_path_buf();

        self.target_dir = Some(dir);
    }

    /// Set the path to the `target` directory in which cargo creates build
    /// artefacts.
    pub fn target_dir(mut self, dir: impl AsRef<Path>) -> Self {
        self.with_target_dir(dir);
        self
    }

    /// Set the name of the crate.
    pub fn with_lib_name(&mut self, name: impl AsRef<str>) {
        let name = name.as_ref().to_string();

        self.lib_name = Some(name);
    }

    /// Set the name of the crate.
    pub fn lib_name(mut self, name: impl AsRef<str>) -> Self {
        self.with_lib_name(name);
        self
    }

    /// Set the build mode of the crate.
    ///
    /// This will affect the path the `gdnlib` resource points to.
    pub fn with_build_mode(&mut self, mode: BuildMode) {
        self.build_mode = Some(mode);
    }

    /// Set the build mode of the crate.
    ///
    /// This will affect the path the `gdnlib` resource points to.
    pub fn build_mode(mut self, mode: BuildMode) -> Self {
        self.with_build_mode(mode);
        self
    }

    /// Build and generate files for the crate and all `classes`.
    ///
    /// # Panics
    ///
    /// This function panics if the `godot_project_dir` has not been set.
    pub fn build(self, classes: crate::scan::Classes) -> Result<(), std::io::Error> {
        let lib_name = self
            .lib_name
            .or_else(|| std::env::var("CARGO_PKG_NAME").ok())
            .expect("Package name not given and unable to find");
        let godot_project_dir = self
            .godot_project_dir
            .and_then(|path| dunce::canonicalize(path).ok())
            .expect("Godot project dir not given");
        let godot_resource_output_dir = self
            .godot_resource_output_dir
            .and_then(|path| dunce::canonicalize(path).ok())
            .unwrap_or_else(|| godot_project_dir.join("native"));
        let target_dir = self
            .target_dir
            .and_then(|path| dunce::canonicalize(path).ok())
            .or_else(|| {
                let dir = std::env::var("CARGO_TARGET_DIR").ok()?;
                dunce::canonicalize(PathBuf::from(dir)).ok()
            })
            .or_else(|| {
                let dir = std::env::var("OUT_DIR").ok()?;
                let out_path = PathBuf::from(&dir);

                // target/{debug/release}/build/{crate}/out
                dunce::canonicalize(out_path.join("../../../../")).ok()
            })
            .expect("Target dir not given and unable to find");
        let build_mode = self
            .build_mode
            .or_else(|| {
                let profile = std::env::var("PROFILE").ok()?;
                match profile.as_str() {
                    "release" => Some(BuildMode::Release),
                    "debug" => Some(BuildMode::Debug),
                    _ => None,
                }
            })
            .expect("Build mode not given and unable to find");

        std::fs::create_dir_all(&godot_resource_output_dir)?;

        let gdnlib_path = godot_resource_output_dir.join(format!("{}.gdnlib", lib_name));

        {
            let target_base_path = target_dir;

            let target_rel_path = pathdiff::diff_paths(&target_base_path, &godot_project_dir)
                .expect("Unable to create relative path between Godot project and library output");

            let prefix;
            let output_path;

            if target_rel_path.starts_with("../") {
                // not in the project folder, use an absolute path
                prefix = "";
                output_path = target_base_path;
            } else {
                // output paths are inside the project folder, use a `res://` path
                prefix = "res://";
                output_path = target_rel_path;
            };

            let binaries = common_binary_outputs(&output_path, build_mode, &lib_name);

            let file_exists = gdnlib_path.exists() && gdnlib_path.is_file();

            if !file_exists {
                let gdnlib = generate_gdnlib(prefix, binaries);
                std::fs::write(&gdnlib_path, gdnlib)?;
            }
        }

        let rel_gdnlib_path = pathdiff::diff_paths(&gdnlib_path, &godot_project_dir)
            .expect("Unable to create relative path between Godot project and library output");

        let prefix;
        let output_path;

        if rel_gdnlib_path.starts_with("../") {
            // not in the project folder, use an absolute path
            prefix = "";
            output_path = &gdnlib_path;
        } else {
            // output paths are inside the project folder, use a `res://` path
            prefix = "res://";
            output_path = &rel_gdnlib_path;
        };

        for name in classes {
            let path = godot_resource_output_dir.join(format!("{}.gdns", &name));

            let file_exists = path.exists() && path.is_file();

            if !file_exists {
                let content = generate_gdns(&prefix, &output_path, &name);
                std::fs::write(&path, content)?;
            }
        }

        Ok(())
    }
}

struct Binaries {
    x11: PathBuf,
    osx: PathBuf,
    // TODO
    // ios: PathBuf,
    windows: PathBuf,
    android_aarch64: PathBuf,
    android_armv7: PathBuf,
    android_x86: PathBuf,
    android_x86_64: PathBuf,
}

fn common_binary_outputs(target: &Path, mode: BuildMode, name: &str) -> Binaries {
    let mode_path = match mode {
        BuildMode::Debug => "debug",
        BuildMode::Release => "release",
    };

    // NOTE: If a crate has a hyphen in the name, at least on Linux the resulting library
    // will have it replaced with an underscore. I assume other platforms do the same?
    let name = name.replace("-", "_");

    Binaries {
        x11: target.join(mode_path).join(format!("lib{}.so", name)),
        osx: target.join(mode_path).join(format!("lib{}.dylib", name)),

        windows: target.join(mode_path).join(format!("{}.dll", name)),
        android_armv7: target
            .join("armv7-linux-androideabi")
            .join(mode_path)
            .join(format!("lib{}.so", name)),
        android_aarch64: target
            .join("aarch64-linux-android")
            .join(mode_path)
            .join(format!("lib{}.so", name)),
        android_x86: target
            .join("i686-linux-android")
            .join(mode_path)
            .join(format!("lib{}.so", name)),
        android_x86_64: target
            .join("x86_64-linux-android")
            .join(mode_path)
            .join(format!("lib{}.so", name)),
    }
}

fn generate_gdnlib(path_prefix: &str, binaries: Binaries) -> String {
    format!(
        r#"[entry]
Android.armeabi-v7a="{prefix}{android_armv7}"
Android.arm64-v8a="{prefix}{android_aarch64}"
Android.x86="{prefix}{android_x86}"
Android.x86_64="{prefix}{android_x86_64}"
X11.64="{prefix}{x11}"
OSX.64="{prefix}{osx}"
Windows.64="{prefix}{win}"

[dependencies]

Android.armeabi-v7a=[  ]
Android.arm64-v8a=[  ]
Android.x86=[  ]
Android.x86_64=[  ]
X11.64=[  ]
OSX.64=[  ]

[general]

singleton=false
load_once=true
symbol_prefix="godot_"
reloadable=true"#,
        prefix = path_prefix,
        android_armv7 = binaries.android_armv7.to_slash_lossy(),
        android_aarch64 = binaries.android_aarch64.to_slash_lossy(),
        android_x86 = binaries.android_x86.to_slash_lossy(),
        android_x86_64 = binaries.android_x86_64.to_slash_lossy(),
        x11 = binaries.x11.to_slash_lossy(),
        osx = binaries.osx.to_slash_lossy(),
        win = binaries.windows.to_slash_lossy(),
    )
}

fn generate_gdns(path_prefix: &str, gdnlib_path: &Path, name: &str) -> String {
    format!(
        r#"[gd_resource type="NativeScript" load_steps=2 format=2]

[ext_resource path="{prefix}{gdnlib}" type="GDNativeLibrary" id=1]

[resource]
class_name = "{name}"
script_class_name = "{name}"
library = ExtResource( 1 )
"#,
        prefix = path_prefix,
        gdnlib = gdnlib_path.to_slash_lossy(),
        name = name,
    )
}