Skip to content

Commit

Permalink
Merge pull request #987 from vrc-get/upgrade-litedb
Browse files Browse the repository at this point in the history
upgrade litedb
  • Loading branch information
anatawa12 committed May 17, 2024
2 parents 3f7b88e + c6e7343 commit 9772d13
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 164 deletions.
22 changes: 9 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions vrc-get-gui/build.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
use crate::build_target_info::{TargetFamily, TargetInformation};
use std::io::Write;
use std::path::PathBuf;

mod build_target_info;

fn main() {
tauri_build::build();

let target_info = TargetInformation::from_triple(std::env::var("TARGET").unwrap().as_str());

if target_info.family == TargetFamily::Linux {
if std::env::var("TARGET").unwrap().contains("linux") {
// start stop gc is not supported by dotnet.
println!("cargo:rustc-link-arg=-Wl,-z,nostart-stop-gc");
} else if target_info.family == TargetFamily::Windows {
// "/merge:.modules=.rdata" "/merge:.unbox=.text"
println!("cargo:rustc-link-arg=/merge:.modules=.rdata");
println!("cargo:rustc-link-arg=/merge:.unbox=.text");
}

build_templates();
Expand Down
1 change: 0 additions & 1 deletion vrc-get-gui/build_target_info.rs

This file was deleted.

8 changes: 4 additions & 4 deletions vrc-get-gui/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ struct TauriProject {
path: String,
project_type: TauriProjectType,
unity: String,
last_modified: u64,
created_at: u64,
last_modified: i64,
created_at: i64,
favorite: bool,
is_exists: bool,
}
Expand Down Expand Up @@ -587,8 +587,8 @@ impl TauriProject {
.unity_version()
.map(|v| v.to_string())
.unwrap_or_else(|| "unknown".into()),
last_modified: project.last_modified().as_millis_since_epoch(),
created_at: project.crated_at().as_millis_since_epoch(),
last_modified: project.last_modified().timestamp_millis(),
created_at: project.crated_at().timestamp_millis(),
favorite: project.favorite(),
is_exists,
}
Expand Down
4 changes: 3 additions & 1 deletion vrc-get-vpm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ uuid = { version = "1.8.0", features = ["v4"] }
lazy_static = "1.4.0"
enum-map = "2.7.3"

vrc-get-litedb = { version = "0.1.4", optional = true }
vrc-get-litedb = { version = "0.2.0", optional = true }
tokio = { version = "1.37.0", features = ["fs", "process"], optional = true }
serde_path_to_error = "0.1.16"
serde-value = "0.7.0"
bson = "2.10.0"
serde_repr = "0.1.19"

[target."cfg(windows)".dependencies]
dirs-sys = "0.4.1"
Expand Down
116 changes: 65 additions & 51 deletions vrc-get-vpm/src/environment/project_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ use crate::io::{EnvironmentIo, FileSystemProjectIo, ProjectIo};
use crate::utils::PathBufExt;
use crate::version::UnityVersion;
use crate::{io, Environment, HttpClient, ProjectType, UnityProject};
use bson::oid::ObjectId;
use bson::DateTime;
use futures::future::join_all;
use log::error;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::{Component, Path, PathBuf};
use vrc_get_litedb::{DateTime, Project};

pub(crate) static COLLECTION: &str = "projects";

impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {
pub async fn migrate_from_settings_json(&mut self) -> io::Result<()> {
Expand All @@ -26,9 +30,9 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {
.iter()
.map(|x| x.as_ref())
.collect::<HashSet<_>>();

let db_projects = db
.get_projects()?
.into_vec()
.get_values::<UserProject>(COLLECTION)?
.into_iter()
.map(|x| (x.path().to_owned(), x))
.collect::<std::collections::HashMap<_, _>>();
Expand All @@ -47,18 +51,17 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {
let (project_type, unity_version) = get_project_type(&self.io, project.as_ref())
.await
.unwrap_or((ProjectType::Unknown, None));
db.insert_project(&Project::new(
(*project).into(),
unity_version.map(|x| x.to_string().into()),
project_type.into(),
))?;
db.insert(
COLLECTION,
&UserProject::new((*project).into(), unity_version, project_type),
)?;
}
}

// remove deleted projects
for project in db_projects.iter() {
if !projects.contains(project.0.as_str()) {
db.delete_project(project.1.id())?;
for (project_path, project) in db_projects.iter() {
if !projects.contains(project_path.as_str()) {
db.delete(COLLECTION, project.id)?;
}
}

Expand All @@ -68,7 +71,7 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {
pub async fn sync_with_real_projects(&mut self, skip_not_found: bool) -> io::Result<()> {
let db = self.get_db()?; // ensure the database connection is initialized

let mut projects = db.get_projects()?;
let mut projects = db.get_values::<UserProject>(COLLECTION)?;

let changed_projects = join_all(
projects
Expand All @@ -78,14 +81,14 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {
.await;

for project in changed_projects.iter().flatten() {
db.update_project(project)?;
db.update(COLLECTION, project)?;
}

async fn update_project_with_actual_data<'a>(
io: &impl EnvironmentIo,
project: &'a mut Project,
project: &'a mut UserProject,
skip_not_found: bool,
) -> Option<&'a Project> {
) -> Option<&'a UserProject> {
match update_project_with_actual_data_inner(io, project, skip_not_found).await {
Ok(Some(project)) => Some(project),
Ok(None) => None,
Expand All @@ -98,9 +101,9 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {

async fn update_project_with_actual_data_inner<'a>(
io: &impl EnvironmentIo,
project: &'a mut Project,
project: &'a mut UserProject,
skip_not_found: bool,
) -> io::Result<Option<&'a Project>> {
) -> io::Result<Option<&'a UserProject>> {
let path = project.path().as_ref();

if !io.is_dir(path).await {
Expand All @@ -114,17 +117,16 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {

let loaded_project = UnityProject::load(io.new_project_io(path)).await?;
if let Some(unity_version) = loaded_project.unity_version() {
let unity_version = unity_version.to_string().into_boxed_str();
if project.unity_version() != Some(&unity_version) {
if project.unity_version() != Some(unity_version) {
changed = true;
project.set_unity_version(Some(unity_version));
project.unity_version = Some(unity_version);
}
}

let project_type = loaded_project.detect_project_type().await?;
if project.project_type() != project_type.into() {
if project.project_type() != project_type {
changed = true;
project.set_project_type(project_type.into());
project.project_type = project_type;
}

Ok(if changed { Some(project) } else { None })
Expand All @@ -135,13 +137,7 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {

// TODO: return wrapper type instead?
pub fn get_projects(&self) -> io::Result<Vec<UserProject>> {
Ok(self
.get_db()?
.get_projects()?
.into_vec()
.into_iter()
.map(UserProject::new)
.collect())
Ok(self.get_db()?.get_values(COLLECTION)?)
}

pub fn update_project_last_modified(&mut self, project_path: &Path) -> io::Result<()> {
Expand All @@ -152,28 +148,28 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {
normalize_path(&std::env::current_dir().unwrap().joined(project_path))
};

let mut project = db.get_projects()?;
let mut project = db.get_values::<UserProject>(COLLECTION)?;
let Some(project) = project
.iter_mut()
.find(|x| Path::new(x.path()) == project_path)
else {
return Ok(());
};

project.set_last_modified(DateTime::now());
db.update_project(project)?;
project.last_modified = DateTime::now();
db.update(COLLECTION, project)?;

Ok(())
}

pub fn update_project(&mut self, project: &UserProject) -> io::Result<()> {
Ok(self.get_db()?.update_project(&project.project)?)
Ok(self.get_db()?.update(COLLECTION, &project)?)
}

pub fn remove_project(&mut self, project: &UserProject) -> io::Result<()> {
let db = self.get_db()?;

db.delete_project(project.project.id())?;
db.delete(COLLECTION, project.id)?;
self.settings.remove_user_project(project.path());

Ok(())
Expand All @@ -200,13 +196,9 @@ impl<T: HttpClient, IO: EnvironmentIo> Environment<T, IO> {

let project_type = project.detect_project_type().await?;

let new_project = Project::new(
path.into(),
unity_version.to_string().into_boxed_str().into(),
project_type.into(),
);
let new_project = UserProject::new(path.into(), Some(unity_version), project_type);

self.get_db()?.insert_project(&new_project)?;
self.get_db()?.insert(COLLECTION, &new_project)?;
self.settings.add_user_project(path);

Ok(())
Expand All @@ -231,17 +223,40 @@ fn normalize_path(input: &Path) -> PathBuf {
result
}

#[derive(Serialize, Deserialize)]
pub struct UserProject {
project: Project,
#[serde(rename = "_id")]
id: ObjectId,
#[serde(rename = "Path")]
path: Box<str>,
#[serde(rename = "UnityVersion")]
unity_version: Option<UnityVersion>,
#[serde(rename = "CreatedAt")]
created_at: DateTime,
#[serde(rename = "LastModified")]
last_modified: DateTime,
#[serde(rename = "Type")]
project_type: ProjectType,
#[serde(rename = "Favorite")]
favorite: bool,
}

impl UserProject {
fn new(project: Project) -> Self {
Self { project }
fn new(path: Box<str>, unity_version: Option<UnityVersion>, project_type: ProjectType) -> Self {
let now = DateTime::now();
Self {
id: ObjectId::new(),
path,
unity_version,
created_at: now,
last_modified: now,
project_type,
favorite: false,
}
}

pub fn path(&self) -> &str {
self.project.path()
self.path.as_ref()
}

pub fn name(&self) -> &str {
Expand All @@ -253,27 +268,26 @@ impl UserProject {
}

pub fn crated_at(&self) -> DateTime {
self.project.created_at()
self.created_at
}

pub fn last_modified(&self) -> DateTime {
// TODO: provide our wrapper type
self.project.last_modified()
self.last_modified
}

pub fn unity_version(&self) -> Option<UnityVersion> {
UnityVersion::parse(self.project.unity_version()?)
self.unity_version
}

pub fn project_type(&self) -> ProjectType {
self.project.project_type().into()
self.project_type
}

pub fn favorite(&self) -> bool {
self.project.favorite()
self.favorite
}

pub fn set_favorite(&mut self, favorite: bool) {
self.project.set_favorite(favorite);
self.favorite = favorite;
}
}

0 comments on commit 9772d13

Please sign in to comment.