gled-core 2.30.2

The engine of gled without a user interface: inputs, timing, rendering, Art-Net/DMX output and storage
Documentation
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
pub(crate) mod action;
pub(crate) mod collections;
pub(crate) mod git;
pub(crate) mod store;

pub(crate) use gled_model::{asset, asset_id, collection};

use self::{action::StorageAction, asset::*, asset_id::AssetId};
use crate::{
    action::UiAction, runtime::persistent_state::PersistentState, storage::collections::Collections,
};
use animation::Animation;
use collection::Collection;
use curve::Curve;
use git::Git;
use midi_controller::MidiController;
use once_cell::sync::Lazy;
use output_device::OutputDevice;
use palette::Palette;
use parking_lot::Mutex;
use project::Project;
use scene::Scene;
use std::str::FromStr;
use std::{
    fmt::Debug,
    fs::{read_to_string, remove_dir_all},
    path::PathBuf,
    sync::atomic::{AtomicBool, AtomicUsize, Ordering::Relaxed},
    thread::sleep,
    time::Duration,
};
use strum::Display;
use uuid::Uuid;

pub(crate) static STORAGE_DIR: Lazy<PathBuf> = Lazy::new(crate::paths::storage_dir);
static STORAGE_VERSION_FILE: Lazy<PathBuf> = Lazy::new(|| STORAGE_DIR.join("version"));
static WORKING: AtomicBool = AtomicBool::new(true);
static ERROR: Lazy<Mutex<Option<String>>> = Lazy::new(|| Mutex::new(None));
static LOADING: AtomicLoading = AtomicLoading::new(Loading::No);
static BRANCHES: Lazy<Mutex<Option<Branches>>> = Lazy::new(|| Mutex::new(None));
static STAGED_FILES: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug, Clone)]
pub(crate) struct Branches {
    pub(crate) available: Vec<String>,
    pub(crate) current: String,
}

#[derive(Display, Default, PartialEq, Eq)]
#[atomic_enum::atomic_enum]
pub(crate) enum Loading {
    #[default]
    #[strum(serialize = "Idling")]
    No,
    #[strum(serialize = "Nuking storage")]
    Nuking,
    #[strum(serialize = "Loading git repository")]
    GitRepository,
    #[strum(serialize = "Loading git branches")]
    GitBranches,
    #[strum(serialize = "Loading current git branch")]
    GitBranch,
    #[strum(serialize = "Loading animations")]
    Animations,
    #[strum(serialize = "Loading curves")]
    Curves,
    #[strum(serialize = "Loading output devices")]
    OutputDevices,
    #[strum(serialize = "Loading MIDI controllers")]
    MidiControllers,
    #[strum(serialize = "Loading palettes")]
    Palettes,
    #[strum(serialize = "Loading projects")]
    Projects,
    #[strum(serialize = "Loading scenes")]
    Scenes,
    #[strum(serialize = "Synchronizing")]
    Synchronizing,
}

impl Loading {
    pub(crate) fn set(self) {
        LOADING.store(self, Relaxed);
    }
}

pub(crate) fn working() -> bool {
    WORKING.load(Relaxed)
}

pub(crate) fn staged_files() -> usize {
    STAGED_FILES.load(Relaxed)
}

pub(crate) fn loading() -> Loading {
    LOADING.load(Relaxed)
}

pub(crate) fn is_loading() -> bool {
    loading() != Loading::No
}

pub(crate) fn error() -> Option<String> {
    ERROR.lock().clone()
}

pub(crate) fn branches() -> Option<Branches> {
    BRANCHES.lock().clone()
}

pub(crate) fn start_thread() {
    let actions = action::init();

    std::thread::Builder::new()
        .name("gled:storage".to_string())
        .spawn(move || {
            #[cfg(feature = "profiling")]
            profiling::register_thread!("storage");

            let mut retry_wait = std::time::Duration::from_secs(0);
            loop {
                sleep(retry_wait);
                retry_wait = std::time::Duration::from_secs(2);

                ERROR.lock().take();

                // Clear the queue
                while let Ok(Some(_)) = actions.try_recv() {}

                Loading::GitRepository.set();
                let mut git = match git::Git::open(PersistentState::default().git_url()) {
                    Ok(git) => git,
                    Err(err) => {
                        let err: String = format!("Could not open git: {err}");
                        tracing::error!("{err}");
                        ERROR.lock().replace(err);
                        continue;
                    }
                };

                StorageAction::LoadBranches.enqueue();
                StorageAction::CountStagedFiles.enqueue();
                StorageAction::LoadAssets.enqueue();

                while let Ok(action) = actions.recv() {
                    WORKING.store(true, Relaxed);

                    match action {
                        StorageAction::Nuke => {
                            ERROR.lock().take();
                            Loading::Nuking.set();
                            remove_dir_all(&*STORAGE_DIR).ok();
                            StorageAction::Restart.enqueue();
                        }
                        StorageAction::Restart => {
                            break;
                        }
                        StorageAction::Stop => {
                            return;
                        }
                        StorageAction::CountStagedFiles => match git.count_staged_files() {
                            Err(err) => {
                                ERROR
                                    .lock()
                                    .replace(format!("Error counting staged files: {err}"));
                                sleep(Duration::from_secs(1));
                                StorageAction::CountStagedFiles.enqueue();
                            }
                            Ok(count) => {
                                STAGED_FILES.store(count, Relaxed);
                                ERROR.lock().take();
                            }
                        },
                        StorageAction::Pull => {
                            if let Err(err) = git.pull() {
                                ERROR.lock().replace(format!("Error pulling: {err}"));
                                sleep(Duration::from_secs(1));
                                StorageAction::Pull.enqueue();
                            } else {
                                ERROR.lock().take();
                            }
                        }
                        StorageAction::Push => {
                            if let Err(err) = git.push() {
                                ERROR.lock().replace(format!("Error pushing: {err}"));
                                sleep(Duration::from_secs(1));
                                StorageAction::Push.enqueue();
                            } else {
                                ERROR.lock().take();
                            }
                        }
                        StorageAction::Commit { message } => {
                            if let Err(err) = git.commit(&message) {
                                ERROR
                                    .lock()
                                    .replace(format!("Error committing and pushing: {err}"));
                                sleep(Duration::from_secs(1));
                                StorageAction::Commit { message }.enqueue();
                            } else {
                                ERROR.lock().take();
                            }
                            StorageAction::CountStagedFiles.enqueue();
                        }
                        StorageAction::LoadBranches => {
                            BRANCHES.lock().take();

                            Loading::GitBranches.set();
                            let branches = match git.branches() {
                                Ok(branches) => branches,
                                Err(err) => {
                                    let err: String = format!("Could not get branches: {err}");
                                    tracing::error!("{err}");
                                    ERROR.lock().replace(err);
                                    continue;
                                }
                            };

                            Loading::GitBranch.set();
                            let current_branch = match git.current_branch() {
                                Ok(current_branch) => current_branch,
                                Err(err) => {
                                    let err: String = format!("Could not get current_branch: {err}");
                                    tracing::error!("{err}");
                                    ERROR.lock().replace(err);
                                    continue;
                                }
                            };

                            *BRANCHES.lock() = Some(Branches {
                                available: branches,
                                current: current_branch,
                            });

                            ERROR.lock().take();
                        }
                        StorageAction::LoadAssets => {
                            let version_file = STORAGE_DIR.join("version");
                            match read_to_string(version_file).ok().and_then(|version| {
                                semver::Version::parse(version.trim())
                                    .map_err(|err| tracing::error!("Could not parse version file: {err:?}"))
                                    .ok()
                            }) {
                                Some(version) => {
                                    if version
                                        > semver::Version::parse(env!("CARGO_PKG_VERSION"))
                                            .expect("Could not parse cargo pkg version")
                                    {
                                        tracing::error!(
                                            "Gled version is too old. Please update to the latest version."
                                        );
                                        ERROR.lock().replace("Gled version is too old. Please update to the latest version.".to_string());
                                        continue;
                                    }
                                }
                                None => {
                                    write_storage_version_file(&mut git);
                                }
                            }

                            if let Err(err) = mkdirp::mkdirp(STORAGE_DIR.join("svg")) {
                                ERROR
                                    .lock()
                                    .replace(format!("Could not create svg templates folder: {err}"));
                                continue;
                            }

                            let mut collections = Collections::default();

                            Loading::Animations.set();
                            collections.insert::<Animation>(load_collection::<Animation>());

                            Loading::Curves.set();
                            collections.insert::<Curve>(load_collection::<Curve>());

                            Loading::OutputDevices.set();
                            collections.insert::<OutputDevice>(load_collection::<OutputDevice>());

                            Loading::MidiControllers.set();
                            collections.insert::<MidiController>(load_collection::<MidiController>());

                            Loading::Palettes.set();
                            collections.insert::<Palette>(load_collection::<Palette>());

                            Loading::Projects.set();
                            collections.insert::<Project>(load_collection::<Project>());

                            Loading::Scenes.set();
                            collections.insert::<Scene>(load_collection::<Scene>());

                            Loading::Synchronizing.set();
                            collections.save();

                            Loading::No.set();
                        }
                        StorageAction::SwitchBranch(branch) => match git.switch_branch(&branch) {
                            Ok(_) => {
                                StorageAction::Restart.enqueue();
                                StorageAction::LoadBranches.enqueue();
                                StorageAction::CountStagedFiles.enqueue();
                                StorageAction::LoadAssets.enqueue();
                            }
                            Err(err) => {
                                ERROR
                                    .lock()
                                    .replace(format!("Error switching branch: {err}"));
                            }
                        },
                        StorageAction::SaveAsset {
                            dir_name,
                            uuid,
                            json,
                        } => {
                            if let Err(err) = git.write_asset(&asset_path(uuid, dir_name), json) {
                                UiAction::Error(format!("Could not write asset: {err}")).enqueue();
                            }

                            write_storage_version_file(&mut git);

                            StorageAction::CountStagedFiles.enqueue();
                        }
                        StorageAction::DeleteAsset { uuid, dir_name } => {
                            if let Err(err) = git.delete_asset(&asset_path(uuid, dir_name)) {
                                tracing::error!("Could not delete asset: {err:?}");
                            }

                            StorageAction::CountStagedFiles.enqueue();
                        }
                    }

                    WORKING.store(false, Relaxed);
                }
            }
        })
    .expect("Could not spawn storage thread");
}

pub(crate) fn asset_path(id: Uuid, dir_name: &str) -> PathBuf {
    STORAGE_DIR.join(dir_name).join(format!("{id}.json"))
}

pub(crate) fn write_storage_version_file(git: &mut Git) {
    static STORAGE_VERSION_WRITTEN: AtomicBool = AtomicBool::new(false);

    if STORAGE_VERSION_WRITTEN.load(Relaxed) {
        return;
    }

    if let Err(err) = std::fs::write(
        STORAGE_VERSION_FILE.as_path(),
        format!("{}\n", env!("CARGO_PKG_VERSION")),
    ) {
        tracing::error!("Could not write storage version file: {err}");
        ERROR
            .lock()
            .replace(format!("Could not write storage version file: {err}"));
        StorageAction::Stop.enqueue();
        return;
    }

    if let Err(err) = git.add(STORAGE_VERSION_FILE.as_path()) {
        tracing::error!("Could not add storage version file to git: {err}");
        ERROR
            .lock()
            .replace(format!("Could not add storage version file to git: {err}"));
        StorageAction::Stop.enqueue();
        return;
    }

    STORAGE_VERSION_WRITTEN.store(true, Relaxed);
}

/// Reads every asset of a kind from the storage directory.
fn load_collection<T: AssetTrait>() -> Collection<T> {
    let path = STORAGE_DIR.join(T::DIR_NAME);
    let Ok(directory) = path.read_dir() else {
        return Collection::default();
    };

    let folders = directory
        .into_iter()
        .filter_map(|entry| {
            let entry = entry
                .map_err(|err| tracing::warn!("Could not read dir entry: {err:?}"))
                .ok()?;
            let file_type = entry
                .file_type()
                .map_err(|err| tracing::warn!("Could not get file type: {err:?}"))
                .ok()?;

            if file_type.is_file() {
                let id = Uuid::from_str(
                    entry
                        .file_name()
                        .into_string()
                        .map_err(|err| {
                            tracing::warn!("Could not convert file name to string: {err:?}")
                        })
                        .ok()?
                        .strip_suffix(".json")?,
                )
                .map_err(|err| tracing::warn!("Could not parse UUID: {err:?}"))
                .ok()?;

                let asset = Asset::<T>::read(
                    std::fs::File::open(entry.path()).ok()?,
                    AssetId::from_uuid(id),
                )
                .map_err(|err| {
                    UiAction::Error(format!(
                        "Could not read/parse asset {}: {err:?}",
                        entry.path().display()
                    ))
                    .enqueue();
                })
                .ok()?;

                return Some((id, asset));
            }

            None
        })
        .collect();

    Collection::from_assets(folders)
}