tirbofish/dropbear
main / crates / eucalyptus-core / src / config.rs · 17939 bytes
crates/eucalyptus-core/src/config.rs
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! The eucalyptus configuration files and its metadata.
use crate::runtime::{Authoring, RuntimeSettings};
use crate::scene::SceneConfig;
use crate::states::{File, Folder, Node, ResourceType, SCENES, SOURCE};
use chrono::Utc;
use ron::ser::PrettyConfig;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};
/// The root config file, responsible for building and other metadata.
///
/// # Location
/// This file is {project_name}.eucp and is located at {project_dir}/{project_name}.eucp
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
pub struct ProjectConfig {
pub project_name: String,
pub project_path: PathBuf,
pub date_created: String,
pub date_last_accessed: String,
/// Semantic version of the project. Default is set to `0.1.0`
#[serde(default)]
pub project_version: String,
#[serde(default)]
pub authors: Authoring,
#[serde(default)]
pub runtime_settings: RuntimeSettings,
#[serde(default)]
pub last_opened_scene: Option<String>,
}
impl ProjectConfig {
/// Creates a new instance of the ProjectConfig. This function is typically used when creating
/// a new project, with it creating new defaults for everything.
pub fn new(project_name: String, project_path: impl AsRef<Path>) -> Self {
let date_created = format!("{}", Utc::now().format("%Y-%m-%d %H:%M:%S"));
let date_last_accessed = format!("{}", Utc::now().format("%Y-%m-%d %H:%M:%S"));
let mut result = Self {
project_name,
project_path: project_path.as_ref().to_path_buf(),
date_created,
date_last_accessed,
project_version: "0.1.0".to_string(),
last_opened_scene: None,
runtime_settings: Default::default(),
authors: Default::default(),
};
let _ = result.load_config_to_memory();
result
}
/// This function writes the [`ProjectConfig`] struct (and other PathBufs) to a file of the choice
/// under the PathBuf path parameter.
///
/// # Parameters
/// * path - The root **folder** of the project.
pub fn write_to(&mut self, path: impl AsRef<Path>) -> anyhow::Result<()> {
self.load_config_to_memory()?;
self.date_last_accessed = format!("{}", Utc::now().format("%Y-%m-%d %H:%M:%S"));
// self.assets = Assets::walk(path);
let ron_str = ron::ser::to_string_pretty(&self, PrettyConfig::default())
.map_err(|e| anyhow::anyhow!("RON serialization error: {}", e))?;
let config_path = path
.as_ref()
.join(format!("{}.eucp", self.project_name.clone().to_lowercase()));
self.project_path = path.as_ref().to_path_buf();
fs::write(&config_path, ron_str).map_err(|e| anyhow::anyhow!(e.to_string()))?;
Ok(())
}
/// Writes only the project `.eucp` file.
///
/// Unlike [`ProjectConfig::write_to`] / [`ProjectConfig::write_to_all`], this does **not**
/// reload or write scene/resource/source configs. This is intended for small editor-facing
/// settings updates (like per-model import scales) where reloading configs would be disruptive.
pub fn write_project_only(&mut self) -> anyhow::Result<()> {
log::debug!("Writing the project config");
self.date_last_accessed = format!("{}", Utc::now().format("%Y-%m-%d %H:%M:%S"));
let ron_str = ron::ser::to_string_pretty(&self, PrettyConfig::default())
.map_err(|e| anyhow::anyhow!("RON serialization error: {}", e))?;
let config_path = self
.project_path
.join(format!("{}.eucp", self.project_name.clone().to_lowercase()));
fs::write(&config_path, ron_str).map_err(|e| anyhow::anyhow!(e.to_string()))?;
log::debug!("Written to {}", config_path.display());
Ok(())
}
/// This function reads from the RON and traverses down the different folders to add more information
/// to the ProjectConfig, such as Assets location and other stuff.
///
/// # Parameters
/// * path - The root config **file** for the project
pub fn read_from(path: impl AsRef<Path>) -> anyhow::Result<Self> {
let path = path.as_ref().to_path_buf();
let ron_str = fs::read_to_string(&path)?;
let mut config: ProjectConfig = ron::de::from_str(ron_str.as_str())?;
config.project_path = path.parent().unwrap().to_path_buf();
log::info!("Loaded project!");
log::debug!("Loaded config info");
log::debug!("Updating with new content");
config.load_config_to_memory()?;
config.write_to_all()?;
log::debug!("Project config successfully updated!");
Ok(config)
}
/// This function loads a `source.eucc` or a `{scene}.eucs` config file into memory, allowing
/// you to reference and load the nodes located inside them.
pub fn load_config_to_memory(&mut self) -> anyhow::Result<()> {
let project_root = PathBuf::from(&self.project_path);
// src config
let mut source_config = SOURCE.write();
match SourceConfig::read_from(&project_root) {
Ok(source) => *source_config = source,
Err(e) => {
if let Some(io_err) = e.downcast_ref::<std::io::Error>() {
if io_err.kind() == std::io::ErrorKind::NotFound {
log::warn!("source.eucc not found, creating default.");
let default = SourceConfig {
path: project_root.join("src"),
nodes: vec![],
};
default.write_to(&project_root)?;
*source_config = default;
} else {
log::warn!("Failed to load source.eucc: {}", e);
}
} else {
log::warn!("Failed to load source.eucc: {}", e);
}
}
}
// scenes
let mut scene_configs = SCENES.write();
scene_configs.clear();
// iterate through each scene file in the folder
let scene_folder = &project_root.join("resources").join("scenes");
if !scene_folder.exists() {
fs::create_dir_all(scene_folder)?;
}
fn deal_with_bad_scene(
path: &Path,
e: &anyhow::Error,
_project_root: &Path,
) -> Option<SceneConfig> {
#[cfg(feature = "editor")]
{
let msg = format!(
"Failed to load scene file: {:?}\n\nError: {}\n\nWould you like to backup the corrupted file and create a new blank scene?\n(Select 'No' to exit the application)",
path.file_name().unwrap_or_default(),
e
);
log::error!("{}", msg);
let should_recover = rfd::MessageDialog::new()
.set_title("Scene loading error")
.set_description(&msg)
.set_buttons(rfd::MessageButtons::YesNo)
.set_level(rfd::MessageLevel::Error)
.show();
match should_recover {
rfd::MessageDialogResult::Yes | rfd::MessageDialogResult::Ok => {
let backup_path = path.with_extension("eucs.bak");
log::info!("Backing up bad scene to {:?}", backup_path);
if let Err(err) = fs::rename(path, &backup_path) {
log::error!("Failed to backup scene: {}", err);
rfd::MessageDialog::new()
.set_title("Backup Error")
.set_description(&format!("Failed to backup file: {}", err))
.show();
std::process::exit(1);
}
let name = path
.file_stem()
.unwrap_or_default()
.to_string_lossy()
.to_string();
let new_scene = SceneConfig::new(name, path.to_path_buf());
if let Err(err) = new_scene.write_to(_project_root) {
log::error!("Failed to write new scene: {}", err);
rfd::MessageDialog::new()
.set_title("Write Error")
.set_description(&format!(
"Failed to create new scene file: {}",
err
))
.show();
std::process::exit(1);
}
return Some(new_scene);
}
_ => {
std::process::exit(1);
}
}
}
#[cfg(not(feature = "editor"))]
{
panic!("Failed to load scene {:?}: {}", path, e);
}
}
fn collect_eucs_files(dir: &Path, out: &mut Vec<std::path::PathBuf>) {
let Ok(entries) = fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect_eucs_files(&path, out);
} else if path.extension().and_then(|s| s.to_str()) == Some("eucs") {
out.push(path);
}
}
}
let mut eucs_files = Vec::new();
collect_eucs_files(scene_folder, &mut eucs_files);
for path in eucs_files {
match SceneConfig::read_from(&path) {
Ok(scene) => {
log::debug!(
"Loaded scene config from file, added to SCENES entry: {}",
scene.scene_name
);
scene_configs.push(scene);
}
Err(e) => {
if let Some(io_err) = e.downcast_ref::<std::io::Error>() {
if io_err.kind() == std::io::ErrorKind::NotFound {
log::warn!("Scene file {:?} not found", path);
} else {
if let Some(first) = scene_configs.first() {
log::warn!(
"Unable to load scene {}: [{:?}], loading the first available scene [{}]",
path.display(),
&e,
first.scene_name
);
} else {
if let Some(scene) = deal_with_bad_scene(&path, &e, &project_root) {
scene_configs.push(scene);
}
}
}
} else {
if let Some(first) = scene_configs.first() {
log::warn!(
"Unable to load scene {}: [{:?}], loading the first available scene [{}]",
path.display(),
&e,
first.scene_name
);
} else {
if let Some(scene) = deal_with_bad_scene(&path, &e, &project_root) {
scene_configs.push(scene);
}
}
}
}
}
}
if scene_configs.is_empty() {
log::info!("No scenes found, creating default scene");
let default_scene =
SceneConfig::new("Default".to_string(), scene_folder.join("default.eucs"));
default_scene.write_to(&project_root)?;
self.last_opened_scene = Some(default_scene.scene_name.clone());
scene_configs.push(default_scene);
}
if let Some(ref last_scene_name) = self.last_opened_scene {
if let Some(pos) = scene_configs
.iter()
.position(|scene| &scene.scene_name == last_scene_name)
{
if pos != 0 {
let scene = scene_configs.remove(pos);
scene_configs.insert(0, scene);
}
} else if let Some(first) = scene_configs.first() {
self.last_opened_scene = Some(first.scene_name.clone());
}
} else if let Some(first) = scene_configs.first() {
self.last_opened_scene = Some(first.scene_name.clone());
}
crate::metadata::scan_and_generate_eucmeta(&project_root);
Ok(())
}
/// # Parameters
/// * path - The root folder of the project
pub fn write_to_all(&mut self) -> anyhow::Result<()> {
let path = self.project_path.clone();
{
let source_config = SOURCE.read();
source_config.write_to(&path)?;
}
{
let scene_configs = SCENES.read();
for scene in scene_configs.iter() {
scene.write_to(&path)?;
}
}
self.write_to(&path)?;
Ok(())
}
}
#[derive(Default, Debug, Serialize, Deserialize, Clone)]
pub struct SourceConfig {
/// The path to the resource folder.
pub path: PathBuf,
/// The files and folders of the assets
pub nodes: Vec<Node>,
}
impl SourceConfig {
/// Builds a source path from the ProjectConfiguration's project_path (or a string)
#[allow(dead_code)]
pub fn build_path(project_path: String) -> PathBuf {
PathBuf::from(project_path).join("src/source.eucc")
}
/// # Parameters
/// - path: The root **folder** of the project
pub fn write_to(&self, path: impl AsRef<Path>) -> anyhow::Result<()> {
let resource_dir = path.as_ref().join("src");
let updated_config = SourceConfig {
path: resource_dir.clone(),
nodes: collect_nodes(&resource_dir, path.as_ref(), vec!["scripts"].as_slice()),
};
let ron_str = ron::ser::to_string_pretty(&updated_config, PrettyConfig::default())
.map_err(|e| anyhow::anyhow!("RON serialisation error: {}", e))?;
let config_path = path.as_ref().join("src").join("source.eucc");
fs::create_dir_all(config_path.parent().unwrap())?;
fs::write(&config_path, ron_str).map_err(|e| anyhow::anyhow!(e.to_string()))?;
Ok(())
}
/// # Parameters
/// - path: The location to the **source.eucc** file
pub fn read_from(path: impl AsRef<Path>) -> anyhow::Result<Self> {
let config_path = path.as_ref().join("src").join("source.eucc");
let ron_str = fs::read_to_string(&config_path)?;
let config: SourceConfig = ron::de::from_str(&ron_str)
.map_err(|e| anyhow::anyhow!("RON deserialization error: {}", e))?;
Ok(config)
}
}
fn collect_nodes(
dir: impl AsRef<Path>,
project_path: impl AsRef<Path>,
exclude_list: &[&str],
) -> Vec<Node> {
let mut nodes = Vec::new();
if let Ok(entries) = fs::read_dir(dir) {
for entry in entries.flatten() {
let entry_path = entry.path();
let name = entry_path
.file_name()
.unwrap_or_default()
.to_string_lossy()
.to_string();
if entry_path.is_dir() && exclude_list.iter().any(|ex| ex.to_string() == *name) {
log::debug!("Skipped past folder {:?}", name);
continue;
}
if entry_path.is_dir() {
let folder_nodes = collect_nodes(&entry_path, project_path.as_ref(), exclude_list);
nodes.push(Node::Folder(Folder {
name,
path: entry_path.clone(),
nodes: folder_nodes,
}));
} else {
let parent_folder = entry_path
.parent()
.and_then(|p| p.file_name())
.map(|n| n.to_string_lossy().to_lowercase())
.unwrap_or_default();
let resource_type = if parent_folder.contains("model") {
ResourceType::Model
} else if parent_folder.contains("texture") {
ResourceType::Texture
} else if parent_folder.contains("shaders") {
ResourceType::Shader
} else if entry_path
.extension()
.map(|e| e.to_string_lossy().to_lowercase())
== Some("kt".to_string())
{
ResourceType::Script
} else if entry_path
.extension()
.map(|e| e.to_string_lossy().to_lowercase().contains("eu"))
.unwrap_or_default()
{
ResourceType::Config
} else {
ResourceType::Unknown
};
// Store relative path from the project root instead of absolute path
let relative_path = entry_path
.strip_prefix(project_path.as_ref())
.unwrap_or(&entry_path)
.to_path_buf();
nodes.push(Node::File(File::ResourceFile {
name,
path: relative_path,
resource_type,
}));
}
}
}
nodes
}