Skip to content

Commit

Permalink
Initial Implimentation
Browse files Browse the repository at this point in the history
  • Loading branch information
sasial-dev committed Aug 3, 2023
1 parent 34024d8 commit e132109
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 12 deletions.
1 change: 1 addition & 0 deletions plugin/modules/flipper
Submodule flipper added at 4cf7a0
1 change: 1 addition & 0 deletions plugin/modules/promise
Submodule promise added at 7fb09d
1 change: 1 addition & 0 deletions plugin/modules/roact
Submodule roact added at f7d2f1
1 change: 1 addition & 0 deletions plugin/modules/t
Submodule t added at f643b5
1 change: 1 addition & 0 deletions plugin/modules/testez
Submodule testez added at 25d957
6 changes: 5 additions & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{glob::Glob, resolution::UnresolvedValue};
use crate::{glob::Glob, resolution::UnresolvedValue, snapshot_middleware::ScriptContextType};

static PROJECT_FILENAME: &str = "default.project.json";

Expand Down Expand Up @@ -73,6 +73,10 @@ pub struct Project {
#[serde(skip_serializing_if = "Option::is_none")]
pub serve_address: Option<IpAddr>,

/// The mode to use when mapping scripts onto Roblox
#[serde(default)]
pub script_type: ScriptContextType,

/// A list of globs, relative to the folder the project file is in, that
/// match files that should be excluded if Rojo encounters them.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand Down
56 changes: 45 additions & 11 deletions src/snapshot_middleware/lua.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{path::Path, str};

use anyhow::Context;
use anyhow::{Context, format_err};
use maplit::hashmap;
use memofs::{IoResultExt, Vfs};
use rbx_dom_weak::types::Enum;
use serde::{Deserialize, Serialize};

use crate::snapshot::{InstanceContext, InstanceMetadata, InstanceSnapshot};

Expand All @@ -12,6 +14,20 @@ use super::{
util::match_trailing,
};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub enum ScriptContextType {
#[default]
Class,
RunContext
}

#[derive(Debug)]
enum ScriptType {
Server,
Client,
Module,
}

/// Core routine for turning Lua files into snapshots.
pub fn snapshot_lua(
context: &InstanceContext,
Expand All @@ -20,36 +36,54 @@ pub fn snapshot_lua(
) -> anyhow::Result<Option<InstanceSnapshot>> {
let file_name = path.file_name().unwrap().to_string_lossy();

let (class_name, instance_name) = if let Some(name) = match_trailing(&file_name, ".server.lua")
let is_runcontext_enabled = true; //project.script_type == ScriptType::RunContext;

let run_context = &rbx_reflection_database::get().enums.get("RunContext").ok_or_else(|| format_err!("Unable to get RunContext enums!"))?.items;

let (script_type, instance_name) = if let Some(name) = match_trailing(&file_name, ".server.lua")
{
("Script", name)
(ScriptType::Server, name)
} else if let Some(name) = match_trailing(&file_name, ".client.lua") {
("LocalScript", name)
(ScriptType::Client, name)
} else if let Some(name) = match_trailing(&file_name, ".lua") {
("ModuleScript", name)
(ScriptType::Module, name)
} else if let Some(name) = match_trailing(&file_name, ".server.luau") {
("Script", name)
(ScriptType::Server, name)
} else if let Some(name) = match_trailing(&file_name, ".client.luau") {
("LocalScript", name)
(ScriptType::Client, name)
} else if let Some(name) = match_trailing(&file_name, ".luau") {
("ModuleScript", name)
(ScriptType::Module, name)
} else {
return Ok(None);
};

let (class_name, run_context) = match (is_runcontext_enabled, script_type) {
(true, ScriptType::Server) => ("Script", run_context.get("Server")),
(true, ScriptType::Client) => ("Script", run_context.get("Client")),
(false, ScriptType::Server) => ("Script", run_context.get("Legacy")),
(false, ScriptType::Client) => ("LocalScript", None),
(_, ScriptType::Module) => ("ModuleScript", None),
};

let contents = vfs.read(path)?;
let contents_str = str::from_utf8(&contents)
.with_context(|| format!("File was not valid UTF-8: {}", path.display()))?
.to_owned();

let mut properties = hashmap! {
"Source".to_owned() => contents_str.into(),
};

if let Some(run_context) = run_context {
properties.insert("RunContext".to_owned(), Enum::from_u32(run_context.to_owned()).into());
}

let meta_path = path.with_file_name(format!("{}.meta.json", instance_name));

let mut snapshot = InstanceSnapshot::new()
.name(instance_name)
.class_name(class_name)
.properties(hashmap! {
"Source".to_owned() => contents_str.into(),
})
.properties(properties)
.metadata(
InstanceMetadata::new()
.instigating_source(path)
Expand Down
1 change: 1 addition & 0 deletions src/snapshot_middleware/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use self::{
};

pub use self::project::snapshot_project_node;
pub use self::lua::ScriptContextType;

/// The main entrypoint to the snapshot function. This function can be pointed
/// at any path and will return something if Rojo knows how to deal with it.
Expand Down

0 comments on commit e132109

Please sign in to comment.