Skip to content

Commit

Permalink
Update legacy_scripts and keep_unknowns behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
DervexDev committed Apr 14, 2024
1 parent 47d4f4d commit 8664236
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 40 deletions.
5 changes: 2 additions & 3 deletions src/core/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,12 @@ impl Meta {
let context = Context {
sync_rules: project.sync_rules.clone(),
ignore_rules,
legacy_scripts: project.legacy_scripts,
legacy_scripts: project.legacy_scripts.unwrap_or(true),
};

Self {
source: Source::new(),
context,
keep_unknowns: project.keep_unknowns,
..Self::default()
}
}
}
Expand Down
38 changes: 25 additions & 13 deletions src/middleware/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use rbx_dom_weak::types::{Enum, Variant};
use std::{collections::HashMap, path::Path};

use super::FileType;
use crate::{core::snapshot::Snapshot, vfs::Vfs};
use crate::{
core::{meta::Context, snapshot::Snapshot},
vfs::Vfs,
};

#[derive(Debug, Clone, PartialEq)]
pub enum ScriptType {
Expand All @@ -12,16 +15,6 @@ pub enum ScriptType {
Module,
}

impl ScriptType {
fn as_class(&self) -> &'static str {
match self {
ScriptType::Server => "Script",
ScriptType::Client => "LocalScript",
ScriptType::Module => "ModuleScript",
}
}
}

impl From<FileType> for ScriptType {
fn from(file_type: FileType) -> Self {
match file_type {
Expand All @@ -34,13 +27,23 @@ impl From<FileType> for ScriptType {
}

#[profiling::function]
pub fn snapshot_lua(path: &Path, vfs: &Vfs, script_type: ScriptType) -> Result<Snapshot> {
let mut snapshot = Snapshot::new().with_class(script_type.as_class());
pub fn snapshot_lua(path: &Path, context: &Context, vfs: &Vfs, script_type: ScriptType) -> Result<Snapshot> {
let (class_name, run_context) = match (context.use_legacy_scripts(), &script_type) {
(false, ScriptType::Server) => ("Script", Some(Variant::Enum(Enum::from_u32(1)))),
(false, ScriptType::Client) => ("Script", Some(Variant::Enum(Enum::from_u32(2)))),
(true, ScriptType::Server) => ("Script", Some(Variant::Enum(Enum::from_u32(0)))),
(true, ScriptType::Client) => ("LocalScript", None),
(_, ScriptType::Module) => ("ModuleScript", None),
};

let mut snapshot = Snapshot::new().with_class(class_name);
let mut properties = HashMap::new();

let source = vfs.read_to_string(path)?;

if script_type != ScriptType::Module {
let mut overridden = false;

if let Some(line) = source.lines().next() {
if line.contains("--disable") {
properties.insert(String::from("Disabled"), Variant::Bool(true));
Expand All @@ -49,13 +52,22 @@ pub fn snapshot_lua(path: &Path, vfs: &Vfs, script_type: ScriptType) -> Result<S
if script_type == ScriptType::Server {
if line.contains("--server") {
properties.insert(String::from("RunContext"), Variant::Enum(Enum::from_u32(1)));
overridden = true;
} else if line.contains("--client") {
properties.insert(String::from("RunContext"), Variant::Enum(Enum::from_u32(2)));
overridden = true;
} else if line.contains("--plugin") {
properties.insert(String::from("RunContext"), Variant::Enum(Enum::from_u32(3)));
overridden = true;
}
}
}

if !overridden {
if let Some(run_context) = run_context {
properties.insert(String::from("RunContext"), run_context);
}
}
}

properties.insert(String::from("Source"), Variant::String(source));
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ impl Display for FileType {
}

impl FileType {
fn middleware(&self, path: &Path, vfs: &Vfs) -> Result<Snapshot> {
fn middleware(&self, path: &Path, context: &Context, vfs: &Vfs) -> Result<Snapshot> {
let result = match self {
FileType::Project => snapshot_project(path, vfs),
FileType::InstanceData => unreachable!(),
//
FileType::ServerScript | FileType::ClientScript | FileType::ModuleScript => {
snapshot_lua(path, vfs, self.clone().into())
snapshot_lua(path, context, vfs, self.clone().into())
}
//
FileType::StringValue => snapshot_txt(path, vfs),
Expand Down Expand Up @@ -145,7 +145,7 @@ fn new_snapshot_file(path: &Path, context: &Context, vfs: &Vfs) -> Result<Option
let file_type = resolved.file_type;
let name = resolved.name;

let mut snapshot = file_type.middleware(path, vfs)?;
let mut snapshot = file_type.middleware(path, context, vfs)?;

if file_type != FileType::Project {
snapshot.set_name(&name);
Expand All @@ -170,7 +170,7 @@ fn new_snapshot_file_child(path: &Path, context: &Context, vfs: &Vfs) -> Result<
let name = resolved.name;
let parent = path.get_parent();

let mut snapshot = file_type.middleware(path, vfs)?;
let mut snapshot = file_type.middleware(path, context, vfs)?;

if file_type != FileType::Project {
snapshot.set_name(&name);
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn new_snapshot_node(
context: &Context,
vfs: &Vfs,
) -> Result<Snapshot> {
if node.class_name.is_some() && node.path.is_some() && !node_path.is_root() {
if node.class_name.is_some() && node.path.is_some() {
bail!("Failed to load project: $className and $path cannot be set at the same time");
}

Expand Down Expand Up @@ -90,7 +90,7 @@ pub fn new_snapshot_node(
let meta = Meta::new()
.with_source(Source::project(name, path, node.clone(), node_path.clone()))
.with_context(context)
.with_keep_unknowns(node.keep_unknowns.unwrap_or_else(|| class != "Folder"));
.with_keep_unknowns(node.keep_unknowns.unwrap_or_else(|| util::is_service(&class)));

let mut snapshot = Snapshot::new()
.with_name(name)
Expand Down
16 changes: 3 additions & 13 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
ext::PathExt,
glob::Glob,
resolution::UnresolvedValue,
util, workspace,
workspace,
};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -64,18 +64,8 @@ pub struct Project {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub sync_rules: Vec<SyncRule>,

#[serde(
alias = "ignoreUnknownInstances",
default,
skip_serializing_if = "std::ops::Not::not"
)]
pub keep_unknowns: bool,
#[serde(
alias = "emitLegacyScripts",
default = "util::serde_true",
skip_serializing_if = "Clone::clone"
)]
pub legacy_scripts: bool,
#[serde(alias = "emitLegacyScripts", skip_serializing_if = "Option::is_none")]
pub legacy_scripts: Option<bool>,

#[serde(skip)]
pub path: PathBuf,
Expand Down
5 changes: 0 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,6 @@ pub fn get_yes() -> bool {
yes == "1"
}

/// Returns `true`, required for `default` serde derive
pub fn serde_true() -> bool {
true
}

/// Return line of code count from snapshot's properties
pub fn count_loc_from_properties(properties: &HashMap<String, Variant>) -> usize {
let mut loc = 0;
Expand Down

0 comments on commit 8664236

Please sign in to comment.