Skip to content

Commit

Permalink
Add --@script:... and --@disabled directives
Browse files Browse the repository at this point in the history
  • Loading branch information
hoontee committed Nov 14, 2023
1 parent e99ca87 commit f14140d
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 63 deletions.
Binary file modified Lync/Plugin.rbxm
Binary file not shown.
17 changes: 14 additions & 3 deletions Lync/RobloxPluginSource/Plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
]]
local VERSION = "Alpha 25"
local VERSION = "Alpha 26"

if not plugin or game:GetService("RunService"):IsRunning() and game:GetService("RunService"):IsClient() then return end

Expand Down Expand Up @@ -431,6 +431,17 @@ local function eval(value: any): any
end

local function setDetails(target: any, data: any)
if data.Context then
lpcall("Set Context " .. data.Context, function()
if data.Context == "Legacy" then
target.RunContext = Enum.RunContext.Legacy
elseif data.Context == "Client" then
target.RunContext = Enum.RunContext.Client
elseif data.Context == "Server" then
target.RunContext = Enum.RunContext.Server
end
end)
end
if data.Properties then
for property, value in data.Properties do
lpcall("Set Property " .. property, function()
Expand Down Expand Up @@ -511,7 +522,7 @@ local function buildPath(path: string)
not data
or data.Type == "Model" or data.Type == "JsonModel"
or data.Type == "Instance" and nextTarget.ClassName ~= data.ClassName
or data.Type == "Lua" and nextTarget.ClassName ~= (if data.Context == "Client" then "LocalScript" elseif data.Context == "Server" then "Script" else "ModuleScript")
or data.Type == "Lua" and nextTarget.ClassName ~= (if data.Context == "ModuleScript" then "ModuleScript" elseif data.Context == "LocalScript" then "LocalScript" else "Script")
then
if not pcall(function()
nextTarget.Parent = nil
Expand Down Expand Up @@ -565,7 +576,7 @@ local function buildPath(path: string)
end
elseif data.Type == "Lua" then
if createInstance then
local newInstance = Instance.new(if data.Context == "Client" then "LocalScript" elseif data.Context == "Server" then "Script" else "ModuleScript")
local newInstance = Instance.new(if data.Context == "ModuleScript" then "ModuleScript" elseif data.Context == "LocalScript" then "LocalScript" else "Script")
newInstance.Name = name
newInstance.Parent = target
target = newInstance
Expand Down
107 changes: 56 additions & 51 deletions Lync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
const VERSION = 'Alpha 25'
const VERSION = 'Alpha 26'

const { spawn, spawnSync } = require('child_process')
const fs = require('fs')
Expand Down Expand Up @@ -175,7 +175,7 @@ function localPathIsInit(localPath) {
const localPathParsed = path.parse(localPath)
const localPathName = localPathParsed.name.toLowerCase()
const localPathExt = localPathParsed.ext.toLowerCase()
return (localPathExt == '.lua' || localPathExt == '.luau') && (localPathName == 'init' || localPathName == 'init.client' || localPathName == 'init.server' || localPathName.endsWith('.init') || localPathName.endsWith('.init.client') || localPathName.endsWith('.init.server'))
return (localPathExt == '.lua' || localPathExt == '.luau') && (localPathName == 'init' || localPathName.endsWith('.init'))
}

/**
Expand Down Expand Up @@ -307,16 +307,58 @@ function assignMap(robloxPath, mapDetails, mtimeMs) {
/**
* @param {string} localPath
* @param {string} robloxPath
* @param {Object} properties
* @param {Object} attributes
* @param {string[]} tags
* @param {string?} metaLocalPath
* @param {string?} initPath
* @param {number} mtimeMs
*/
function mapLua(localPath, robloxPath, properties, attributes, tags, metaLocalPath, initPath, mtimeMs) {
function mapLua(localPath, robloxPath, attributes, tags, metaLocalPath, initPath, mtimeMs) {
if (localPathIsIgnored(localPath)) return
const context = (localPath.endsWith('.client.lua') || localPath.endsWith('.client.luau')) && 'Client' || (localPath.endsWith('.server.lua') || localPath.endsWith('.server.luau')) && 'Server' || 'Module'

const properties = {}
let context = 'ModuleScript'

// Scan for directives (run context, disabled)
try {
const directives = fs.readFileSync(localPath, { encoding: 'utf-8' }).split(/[\r\n]+/)
for (const index in directives) {
const line = directives[index].toLowerCase()
if (line.startsWith('--@')) {
if (line.startsWith('--@script:')) {
if (line == '--@script:legacy') {
context = 'Legacy'
} else if (line == '--@script:server') {
context = 'Server'
} else if (line == '--@script:client') {
context = 'Client'
} else if (line == '--@script:localscript') {
context = 'LocalScript'
} else {
console.error(fileError(localPath), yellow('Invalid run context directive:'), green(line))
}
} else if (line == '--@disabled') {
properties.Enabled = false
} else {
console.error(fileError(localPath), yellow('Unknown directive:'), green(line))
}
} else if (!line.startsWith('--!')) {
break
}
}
} catch (err) {
console.error(fileError(localPath), yellow(err))
}

if (context == 'ModuleScript') {
if ('Enabled' in properties) {
delete properties.Enabled
console.error(fileError(localPath), yellow('Cannot use'), green('--@disabled'), yellow('directive on ModuleScripts'))
}
} else {
if (!('Enabled' in properties)) properties.Enabled = true
}

assignMap(robloxPath, {
'Type': 'Lua',
'Context': context,
Expand Down Expand Up @@ -359,10 +401,9 @@ async function mapDirectory(localPath, robloxPath, flag) {
// Lua Meta Files
if (localPathExt == '.lua' || localPathExt == '.luau' || localPathExt == '.txt' || localPathExt == '.csv') {
let luaMeta;
const title = (localPathExt == '.lua' || localPathExt == '.luau') && (localPathName.endsWith('.client') || localPathName.endsWith('.server')) && localPathParsed.name.slice(0, -7) || localPathParsed.name
const metaLocalPathJson = localPath.slice(0, localPath.lastIndexOf('/')) + '/' + title + '.meta.json'
const metaLocalPathYaml = localPath.slice(0, localPath.lastIndexOf('/')) + '/' + title + '.meta.yaml'
const metaLocalPathToml = localPath.slice(0, localPath.lastIndexOf('/')) + '/' + title + '.meta.toml'
const metaLocalPathJson = localPath.slice(0, localPath.lastIndexOf('/')) + '/' + localPathParsed.name + '.meta.json'
const metaLocalPathYaml = localPath.slice(0, localPath.lastIndexOf('/')) + '/' + localPathParsed.name + '.meta.yaml'
const metaLocalPathToml = localPath.slice(0, localPath.lastIndexOf('/')) + '/' + localPathParsed.name + '.meta.toml'
if (fs.existsSync(metaLocalPathJson)) {
luaMeta = validateJson('Meta', metaLocalPathJson, fs.readFileSync(metaLocalPathJson, { encoding: 'utf-8' }))
metaLocalPath = metaLocalPathJson
Expand All @@ -385,8 +426,8 @@ async function mapDirectory(localPath, robloxPath, flag) {
// Lua
if (localPathExt == '.lua' || localPathExt == '.luau') {
let newRobloxPath = robloxPath
if (flag != 'JSON' && flag != 'Modified') newRobloxPath = robloxPathParsed.dir + '/' + ((localPathName.endsWith('.client') || localPathName.endsWith('.server')) && localPathParsed.name.slice(0, -7) || localPathParsed.name)
mapLua(localPath, newRobloxPath, properties, attributes, tags, metaLocalPath, undefined, localPathStats.mtimeMs)
if (flag != 'JSON' && flag != 'Modified') newRobloxPath = robloxPathParsed.dir + '/' + localPathParsed.name
mapLua(localPath, newRobloxPath, attributes, tags, metaLocalPath, undefined, localPathStats.mtimeMs)

// Models
} else if (localPathExt == '.rbxm' || localPathExt == '.rbxmx') {
Expand Down Expand Up @@ -524,31 +565,15 @@ async function mapDirectory(localPath, robloxPath, flag) {

// Lync-Style Init Lua
if (fs.existsSync(localPath + '/' + localPathParentName + '.init.lua')) {
mapLua(localPath + '/' + localPathParentName + '.init.lua', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/' + localPathParentName + '.init.client.lua')) {
mapLua(localPath + '/' + localPathParentName + '.init.client.lua', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/' + localPathParentName + '.init.server.lua')) {
mapLua(localPath + '/' + localPathParentName + '.init.server.lua', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
mapLua(localPath + '/' + localPathParentName + '.init.lua', robloxPath, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/' + localPathParentName + '.init.luau')) {
mapLua(localPath + '/' + localPathParentName + '.init.luau', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/' + localPathParentName + '.init.client.luau')) {
mapLua(localPath + '/' + localPathParentName + '.init.client.luau', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/' + localPathParentName + '.init.server.luau')) {
mapLua(localPath + '/' + localPathParentName + '.init.server.luau', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
mapLua(localPath + '/' + localPathParentName + '.init.luau', robloxPath, attributes, tags, undefined, localPath, localPathStats.mtimeMs)

// Rojo-Style Init Lua
} else if (fs.existsSync(localPath + '/init.lua')) {
mapLua(localPath + '/init.lua', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/init.client.lua')) {
mapLua(localPath + '/init.client.lua', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/init.server.lua')) {
mapLua(localPath + '/init.server.lua', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
mapLua(localPath + '/init.lua', robloxPath, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/init.luau')) {
mapLua(localPath + '/init.luau', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/init.client.luau')) {
mapLua(localPath + '/init.client.luau', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
} else if (fs.existsSync(localPath + '/init.server.luau')) {
mapLua(localPath + '/init.server.luau', robloxPath, properties, attributes, tags, undefined, localPath, localPathStats.mtimeMs)
mapLua(localPath + '/init.luau', robloxPath, attributes, tags, undefined, localPath, localPathStats.mtimeMs)

// Folders
} else if (flag != 'JSON') {
Expand All @@ -573,17 +598,9 @@ async function mapDirectory(localPath, robloxPath, flag) {
case 'init.meta.yaml':
case 'init.meta.toml':
case localPathParentNameLower + '.init.lua':
case localPathParentNameLower + '.init.client.lua':
case localPathParentNameLower + '.init.server.lua':
case localPathParentNameLower + '.init.luau':
case localPathParentNameLower + '.init.client.luau':
case localPathParentNameLower + '.init.server.luau':
case 'init.lua':
case 'init.client.lua':
case 'init.server.lua':
case 'init.luau':
case 'init.client.luau':
case 'init.server.luau':
break
default:
const filePathNext = localPath + '/' + dirNext
Expand Down Expand Up @@ -1123,21 +1140,9 @@ async function changedJson() {
if (fs.existsSync(localPathParsed.dir + '/' + title + '.lua')) {
delete map[key]
await mapDirectory(localPath, title + '.lua')
} else if (fs.existsSync(localPathParsed.dir + '/' + title + '.client.lua')) {
delete map[key]
await mapDirectory(localPath, title + '.client.lua')
} else if (fs.existsSync(localPathParsed.dir + '/' + title + '.server.lua')) {
delete map[key]
await mapDirectory(localPath, title + '.server.lua')
} else if (fs.existsSync(localPathParsed.dir + '/' + title + '.luau')) {
delete map[key]
await mapDirectory(localPath, title + '.luau')
} else if (fs.existsSync(localPathParsed.dir + '/' + title + '.client.luau')) {
delete map[key]
await mapDirectory(localPath, title + '.client.luau')
} else if (fs.existsSync(localPathParsed.dir + '/' + title + '.server.luau')) {
delete map[key]
await mapDirectory(localPath, title + '.server.luau')
} else {
console.error(fileError(localPath), yellow('Stray meta file'))
return
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
--@script:legacy
-- This is a script with a JSON meta file

print(require(script.Parent:WaitForChild("ExampleModule")))
Expand Down
5 changes: 2 additions & 3 deletions Sample Project/Assets/Workspace/ExampleScript.meta.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"properties": {
"Enabled": true,
"RunContext": ["Enum.RunContext.Client"]
"attributes": {
"Number": 1
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--@script:server
--@disabled
-- This is a script with a YAML meta file

print(require(script.Parent:WaitForChild("ExampleModule")))
Expand Down
5 changes: 2 additions & 3 deletions Sample Project/Assets/Workspace/ExampleScript2.meta.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
properties:
Enabled: false
RunContext: ["Enum.RunContext.Client"]
attributes:
Number: 2
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
--@script:client
--@disabled
-- This is a script with a TOML meta file

print(require(script.Parent:WaitForChild("ExampleModule")))
Expand Down
5 changes: 2 additions & 3 deletions Sample Project/Assets/Workspace/ExampleScript3.meta.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
[properties]
Enabled = false
RunContext = [ "Enum.RunContext.Client" ]
[attributes]
Number = 3

0 comments on commit f14140d

Please sign in to comment.