From 74dd2bf00d5ed3d15ca441625dfaa46395f45c2d Mon Sep 17 00:00:00 2001 From: metatablecat <132796135+metatablecat@users.noreply.github.com> Date: Thu, 8 Aug 2024 13:08:09 +0100 Subject: [PATCH] Upgrade repository state Add workflow checks for stylua, analysis --- .github/workflows/ci.yml | 39 ++++++++++++++++++++ .gitignore | 3 +- aftman.toml | 2 ++ scripts/analyse.sh | 11 ++++++ src/Class.luau | 2 +- src/Object.luau | 2 +- src/Service.luau | 2 +- src/init.luau | 16 ++++----- src/lib/Class.luau | 14 +++----- src/lib/Common.luau | 33 +++++++++-------- src/lib/Dispatcher.luau | 74 ++++++++++++++------------------------ src/lib/Error.luau | 77 +++++++++++++++++++++++++--------------- src/lib/Native.luau | 16 ++++----- src/lib/Object.luau | 40 ++++++++++----------- src/lib/Reflection.luau | 26 +++++++------- src/lib/Service.luau | 36 ++++++++----------- src/lib/Types.luau | 25 +++++++------ src/meta.luau | 19 ++++------ stylua.toml | 10 ++++++ 19 files changed, 244 insertions(+), 203 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 scripts/analyse.sh create mode 100644 stylua.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..44003ee --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,39 @@ +name: CI + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Install Aftman + uses: ok-nick/setup-aftman@v0.4.2 + with: + token: ${{ github.token }} + + - name: Analyze + run: sh scripts/analyse.sh + + style: + name: Styling + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Check code style + uses: JohnnyMorganz/stylua-action@v4 + with: + token: ${{ github.token }} + version: latest + args: --check src diff --git a/.gitignore b/.gitignore index a38c28a..b140903 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build/** sourcemap.json -build.project.json \ No newline at end of file +build.project.json +globalTypes.d.lua \ No newline at end of file diff --git a/aftman.toml b/aftman.toml index 7b5eb64..c9e636e 100644 --- a/aftman.toml +++ b/aftman.toml @@ -6,3 +6,5 @@ lune = "lune-org/lune@0.8.6" rojo = "rojo-rbx/rojo@7.4.1" darklua = "seaofvoices/darklua@0.13.1" +luau-lsp = "johnnymorganz/luau-lsp@1.32.1" +stylua = "johnnymorganz/stylua@0.20.0" \ No newline at end of file diff --git a/scripts/analyse.sh b/scripts/analyse.sh new file mode 100644 index 0000000..549f190 --- /dev/null +++ b/scripts/analyse.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +set -e + +curl -O https://raw.githubusercontent.com/JohnnyMorganz/luau-lsp/main/scripts/globalTypes.d.lua +rojo sourcemap default.project.json -o sourcemap.json + +luau-lsp analyze --definitions=globalTypes.d.lua --base-luaurc=.luaurc \ + --sourcemap=sourcemap.json --settings=.vscode/settings.json \ + --no-strict-dm-types --ignore src/base.luau \ + src/ diff --git a/src/Class.luau b/src/Class.luau index e598a05..7ebe37f 100644 --- a/src/Class.luau +++ b/src/Class.luau @@ -1,2 +1,2 @@ local Catwork = require(".") -return Catwork.Class \ No newline at end of file +return Catwork.Class diff --git a/src/Object.luau b/src/Object.luau index 4be0d41..76a2f10 100644 --- a/src/Object.luau +++ b/src/Object.luau @@ -1,2 +1,2 @@ local Catwork = require(".") -return Catwork.new \ No newline at end of file +return Catwork.new diff --git a/src/Service.luau b/src/Service.luau index 17d05d0..ddbabe1 100644 --- a/src/Service.luau +++ b/src/Service.luau @@ -1,2 +1,2 @@ local Catwork = require(".") -return Catwork.Service \ No newline at end of file +return Catwork.Service diff --git a/src/init.luau b/src/init.luau index d3e6507..6980377 100644 --- a/src/init.luau +++ b/src/init.luau @@ -1,13 +1,13 @@ -- metatablecatgames 2024 - Licensed under the MIT License --local RunService = game:GetService("RunService") local Common = require("./lib/Common") -local Service = require("./lib/Service") +local Metakeys = require("./meta") local Native = require("./lib/Native") +local Service = require("./lib/Service") local Types = require("./lib/Types") -local Metakeys = require("./meta") -local REFLECTION = require("./lib/Reflection") local ERROR = require("./lib/Error") +local REFLECTION = require("./lib/Reflection") local Catwork export type Object = Types.Object @@ -25,7 +25,7 @@ Catwork = setmetatable({ return Native.Object(params) end, - + Service = function(params: Types.ServiceCtorParams): Types.Service REFLECTION.ARG(1, "Catwork.Service", REFLECTION.TABLE, params) @@ -37,12 +37,12 @@ Catwork = setmetatable({ REFLECTION.ARG(2, "Catwork.Class", REFLECTION.FUNCTION, createFn) return Native.GetClassLike(name, createFn) - end -},{ - __tostring = function(self) return `Module(Catwork v{self.__VERSION})` end + end, +}, { + __tostring = function(self) return `Module(Catwork v{self.__VERSION})` end, }) table.freeze(Catwork) type Catwork = typeof(Catwork) if game and not Catwork.Plugin then print(Common.WelcomeMessage) end -return Catwork \ No newline at end of file +return Catwork diff --git a/src/lib/Class.luau b/src/lib/Class.luau index e9c49ba..3f72e89 100644 --- a/src/lib/Class.luau +++ b/src/lib/Class.luau @@ -1,7 +1,7 @@ local Common = require("./Common") local ERROR = require("./Error") local Metakeys = require("../meta") -local ENABLE_CLASSES_METAKEY = Metakeys.export "EnableClasses" +local ENABLE_CLASSES_METAKEY = Metakeys.export("EnableClasses") return function(service, name, createObject) -- just clones the template params and pushes it to the service if its nil @@ -11,18 +11,14 @@ return function(service, name, createObject) params.CreateObject = createObject params[Common.ClassHeader] = true - if not service[ENABLE_CLASSES_METAKEY] then - ERROR.SERVICE_NO_CLASSES(service.Name) - end - + if not service[ENABLE_CLASSES_METAKEY] then ERROR.SERVICE_NO_CLASSES(service.Name) end + if not Common.Flags.DONT_ASSIGN_OBJECT_MT then setmetatable(params, { - __tostring = function(self) - return `ServiceTemplate({self.Name})` - end, + __tostring = function(self) return `ServiceTemplate({self.Name})` end, }) end table.freeze(params) return params -end \ No newline at end of file +end diff --git a/src/lib/Common.luau b/src/lib/Common.luau index e640731..0871c88 100644 --- a/src/lib/Common.luau +++ b/src/lib/Common.luau @@ -1,18 +1,20 @@ -local Types = require("./Types") local ERROR = require("./Error") local Metakeys = require("../meta") +local Types = require("./Types") -local HttpService = if game then game:GetService("HttpService") else { - GenerateGUID = function(self, withBrackets) - local template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" - if withBrackets then template = `\{{template}\}` end +local HttpService = if game + then game:GetService("HttpService") + else { + GenerateGUID = function(self, withBrackets) + local template = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" + if withBrackets then template = `\{{template}\}` end - return string.gsub(template, "[xy]", function(c) - local v = (c == "x") and math.random(0, 15) or math.random(8, 11) - return string.format("%x", v) - end) - end -} + return string.gsub(template, "[xy]", function(c) + local v = (c == "x") and math.random(0, 15) or math.random(8, 11) + return string.format("%x", v) + end) + end, + } local VERSION = "0.5.0" local GUID_PATTERN = "^%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x$" @@ -43,19 +45,16 @@ local function getMetakeysAsStrings(tab) return metakeys end - -function Common.validateTable(tab, oName, rules: {[string]: string}) +function Common.validateTable(tab, oName, rules: { [string]: string }) for key, typof in rules do local optional = string.match(typof, OPT_PAT) - typof = if optional then optional else typof + typof = if optional then optional else typof local value = tab[key] if not value and optional then continue end local typeis = typeof(value) - if typeis ~= typof then - ERROR.BAD_TABLE_SHAPE(tab, oName, key, typof, typeis) - end + if typeis ~= typof then ERROR.BAD_TABLE_SHAPE(tab, oName, key, typof, typeis) end end return tab, getMetakeysAsStrings(tab) diff --git a/src/lib/Dispatcher.luau b/src/lib/Dispatcher.luau index 2e22b8d..6b58d97 100644 --- a/src/lib/Dispatcher.luau +++ b/src/lib/Dispatcher.luau @@ -10,10 +10,11 @@ local Dispatcher = {} local objectDispatchState = {} local OBJECT_DESTROYED_RETURN_MSG = "The object was destroyed" -local serviceStartState: {[Types.Service]: { - State: "suspended"|"running"|"finished", - HeldThreads: {thread} -}} = {} +local serviceStartState: { [Types.Service]: { + State: "suspended" | "running" | "finished", + HeldThreads: { thread }, +} } = + {} local function safeAsyncHandler(err) ERROR.DISPATCHER_SPAWN_ERR(ERROR.traceback(err)) @@ -22,7 +23,7 @@ end local function doServiceLoopForObject(object, service, state) local resumptionDelay = 0 - + while not state.Destroyed do local dt = if resumptionDelay and resumptionDelay < 0 then 0 else task.wait(resumptionDelay or 0) if state.Destroyed then break end -- fixes a bug where loops continue an extra tick after destruction @@ -30,16 +31,12 @@ local function doServiceLoopForObject(object, service, state) end end -function Dispatcher.getObjectState(o) - return objectDispatchState[o] -end +function Dispatcher.getObjectState(o) return objectDispatchState[o] end local function getObjectStateError(o) local state = Dispatcher.getObjectState(o) - - if not state then - ERROR.OBJECT_DESTROYED(o) - end + + if not state then ERROR.OBJECT_DESTROYED(o) end return state end @@ -49,8 +46,10 @@ local function free(state, ok, err, o, service) state.IsOK = ok state.ErrMsg = err - local dispatchers = state.Dispatchers; state.Dispatchers = {} - local held = state.HeldThreads; state.HeldThreads = {} + local dispatchers = state.Dispatchers + state.Dispatchers = {} + local held = state.HeldThreads + state.HeldThreads = {} for _, v in dispatchers do task.spawn(v, ok, err) @@ -75,7 +74,7 @@ local function serviceStartup(service) if not serviceState then serviceState = { State = "suspended", - HeldThreads = {} + HeldThreads = {}, } serviceStartState[service] = serviceState @@ -97,12 +96,7 @@ local function serviceStartup(service) end end -local function runObjectAction( - o, - spawnSignal, - service, - state -) +local function runObjectAction(o, spawnSignal, service, state) state.Spawned = true state.Thread = coroutine.running() @@ -117,9 +111,7 @@ local function runObjectAction( free(state, ok, err) - if service.Updating and o.Update then - task.spawn(doServiceLoopForObject, o, service, state) - end + if service.Updating and o.Update then task.spawn(doServiceLoopForObject, o, service, state) end return ok, err end @@ -153,15 +145,11 @@ local function spawnObject(object, service, state, asyncMode) local spawnSignal = service.Spawning serviceStartup(service) - if asyncMode then - object:HandleAsync(asyncMode) - end + if asyncMode then object:HandleAsync(asyncMode) end task.spawn(runObjectAction, object, spawnSignal, service, state) - if not asyncMode then - return object:Await() - end + if not asyncMode then return object:Await() end return nil end @@ -178,21 +166,15 @@ function Dispatcher.spawnObject(o, service, fPrivate, xpcallHandler, asyncHandle -- we would handle this from the service but its safer to handle here -- if an object is dead we just safely cancel it - if service ~= fPrivate.Service then - ERROR.SERVICE_INVALID_CLASS() - end + if service ~= fPrivate.Service then ERROR.SERVICE_INVALID_CLASS() end state.TimeoutDisabled = fPrivate.TimeoutDisabled state.AwaitFor = fPrivate.AwaitFor - + -- basically new logic for Spawn - if state.Spawned then - ERROR.DISPATCHER_ALREADY_SPAWNED(o) - end + if state.Spawned then ERROR.DISPATCHER_ALREADY_SPAWNED(o) end - if xpcallHandler then - state.XPC = xpcallHandler - end + if xpcallHandler then state.XPC = xpcallHandler end return spawnObject(o, fPrivate.Service, state, asyncHandler) end @@ -244,11 +226,9 @@ function Dispatcher.isSelfAsyncCall(o) if not state then return false end -- object is destroyed, will never self-await here. local co = coroutine.running() - - if state.Spawned and co == state.Thread then - return not state.Ready - end - + + if state.Spawned and co == state.Thread then return not state.Ready end + return false end @@ -270,7 +250,7 @@ function Dispatcher.initObjectState(o) AwaitFor = {}, HeldThreads = {}, - Dispatchers = {} + Dispatchers = {}, } objectDispatchState[o] = state @@ -283,4 +263,4 @@ function Dispatcher.getStateString(o) return if state.Ready then "spawned" else "pending" end -return Dispatcher \ No newline at end of file +return Dispatcher diff --git a/src/lib/Error.luau b/src/lib/Error.luau index 96e7399..0ba3152 100644 --- a/src/lib/Error.luau +++ b/src/lib/Error.luau @@ -6,7 +6,7 @@ local function findFirstNonCatworkFunc() -- travels up the trace until a non-Catwork func is found local depth = 1 local trace = 2 - + while true do local s = debug.info(trace, "s") trace += 1 @@ -26,11 +26,10 @@ local function findFirstNonCatworkFunc() end local function e(id, msg, severity) - -- if you're clicking through to this from the error, try finding the first -- script in the stack trace that is not a Catwork-related module. That's the -- script where this error originates from. - + return function(...) local m = `[Catwork:{id}] {string.format(msg, ...)}` if severity == "E" then @@ -43,7 +42,7 @@ end local function traceback(msg) -- same as debug.traceback but strips messages involving Catwork - local msgStack = {msg or "No output from Lua", "Traceback:"} + local msgStack = { msg or "No output from Lua", "Traceback:" } local depth = 1 while true do @@ -70,52 +69,74 @@ end local ErrorBuffer = { BAD_SELF_CALL = e("BAD_SELF_CALL", "Bad self call to %q, did you mean to use : instead of .?", "E"), BAD_ARG = e("BAD_ARG", "Bad argument number %s to function %q. Expected %s, got %s", "E"), - BAD_OBJECT = e("BAD_OBJECT", "Bad argument number %s to function %s. Type %s could not be converted into object %s.", "E"), + BAD_OBJECT = e( + "BAD_OBJECT", + "Bad argument number %s to function %s. Type %s could not be converted into object %s.", + "E" + ), BAD_CLASS = e("BAD_CLASS", "Class %s does not exist for Service %s.", "E"), - BAD_TABLE_SHAPE = e("BAD_TABLE_SHAPE", "Object %* cannot be converted to %s. Type of key %s is invalid. Expected %q, got %q.", "E"), + BAD_TABLE_SHAPE = e( + "BAD_TABLE_SHAPE", + "Object %* cannot be converted to %s. Type of key %s is invalid. Expected %q, got %q.", + "E" + ), GUID_IDS_NOT_ALLOWED = e("GUID_IDS_NOT_ALLOWED", "Cannot use Object ID %s, a new ID has been generated.", "W"), ANALYSIS_MODE_NOT_AVAILABLE = e("ANALYSIS_MODE_NOT_AVAILABLE", "Analysis mode cannot be used in %s", "E"), DISPATCHER_ALREADY_SPAWNED = e("DISPATCHER_ALREADY_SPAWNED", "Object %s has already been spawned.", "E"), DISPATCHER_SPAWN_ERR = e("DISPATCHER_SPAWN_ERR", "An object experienced an error while spawning: %s", "W"), - DISPATCHER_TIMEOUT = e("DISPATCHER_TIMEOUT", "Object %s is taking a long time to intialise. If this is intentional, disable with `[meta \"TimeoutDisabled\"] = true`", "W"), - - ACTION_OBJECT_NOT_READY = e("ACTION_OBJECT_NOT_READY", "Action %s cannot run yet because its object is not ready.", "E"), + DISPATCHER_TIMEOUT = e( + "DISPATCHER_TIMEOUT", + 'Object %s is taking a long time to intialise. If this is intentional, disable with `[meta "TimeoutDisabled"] = true`', + "W" + ), + + ACTION_OBJECT_NOT_READY = e( + "ACTION_OBJECT_NOT_READY", + "Action %s cannot run yet because its object is not ready.", + "E" + ), ACTION_OBJECT_DESTROYED = e("ACTION_OBJECT_DESTROYED", "Action %s cannot be ran on a destroyed object.", "E"), OBJECT_DESTROYED = e("OBJECT_DESTROYED", "Method cannot be used because Object %* was destroyed..", "E"), - OBJECT_SELF_AWAIT = e("OBJECT_SELF_AWAIT", "Object %s is awaiting upon itself and will never resolve. Use HandleAsync instead.", "W"), + OBJECT_SELF_AWAIT = e( + "OBJECT_SELF_AWAIT", + "Object %s is awaiting upon itself and will never resolve. Use HandleAsync instead.", + "W" + ), SERVICE_NO_CLASSES = e("SERVICE_NO_CLASSES", "Service %s does not implement classes.", "E"), SERVICE_INVALID_OBJECT = e("SERVICE_INVALID_OBJECT", "Service %s cannot spawn object %s", "E"), - SERVICE_UPDATING_DISABLED = e("SERVICE_UPDATING_DISABLED", "Updating is not enabled on service %s, yet it implements Updating. This can be fixed by adding `[meta \"EnableUpdating\"] = true` to your service definition.", "W"), + SERVICE_UPDATING_DISABLED = e( + "SERVICE_UPDATING_DISABLED", + 'Updating is not enabled on service %s, yet it implements Updating. This can be fixed by adding `[meta "EnableUpdating"] = true` to your service definition.', + "W" + ), -- Remove in 0.5.1 - FRAGMENT_DEPRECATED_MIGRATION = e("FRAGMENT_DEPRECATED_MIGRATION", "Catwork.Fragment is deprecated and no longer works, use Catwork.new instead.", "E"), + FRAGMENT_DEPRECATED_MIGRATION = e( + "FRAGMENT_DEPRECATED_MIGRATION", + "Catwork.Fragment is deprecated and no longer works, use Catwork.new instead.", + "E" + ), DEPRECATED = e("DEPRECATED", "Function %q is deprecated. Use %q instead.", "W"), INTERNAL = e("INTERNAL", "Error: %*. This is likely a known internal error, please report it!", "E"), traceback = traceback, - UNKNOWN = e("UNKNOWN", "Unknown Error", "E") + UNKNOWN = e("UNKNOWN", "Unknown Error", "E"), } +type ErrorTable = typeof(setmetatable( + {} :: typeof(ErrorBuffer), + {} :: { + __index: (ErrorTable, string) -> (...string) -> never, + } +)) - -type ErrorTable = typeof( - setmetatable( - {}::typeof(ErrorBuffer), - {}::{ - __index: (ErrorTable, string) -> (...string) -> never - } - ) -) - -local Error: ErrorTable = setmetatable({}::any, { - __index = function(self, k) - return ErrorBuffer[k] or ErrorBuffer.UNKNOWN - end +local Error: ErrorTable = setmetatable({} :: any, { + __index = function(self, k) return ErrorBuffer[k] or ErrorBuffer.UNKNOWN end, }) -return Error \ No newline at end of file +return Error diff --git a/src/lib/Native.luau b/src/lib/Native.luau index bf932d5..1968073 100644 --- a/src/lib/Native.luau +++ b/src/lib/Native.luau @@ -4,23 +4,19 @@ local Service = require("./Service") local meta = require("../meta").export -local native = Service { - [meta "EnableClasses"] = true, +local native = Service({ + [meta("EnableClasses")] = true, Name = "catwork", -} +}) local nativeAPI = {} -function nativeAPI.Object(params) - return native:Object(params) -end +function nativeAPI.Object(params) return native:Object(params) end function nativeAPI.GetClassLike(name, createFn) local template = native:Class(name, createFn) - return function(params) - return native:CreateObjectFromClass(template, params) - end + return function(params) return native:CreateObjectFromClass(template, params) end end -return nativeAPI \ No newline at end of file +return nativeAPI diff --git a/src/lib/Object.luau b/src/lib/Object.luau index 480a6c8..31ca745 100644 --- a/src/lib/Object.luau +++ b/src/lib/Object.luau @@ -1,7 +1,7 @@ if not task then task = require("@lune/task") end -local Dispatcher = require("./Dispatcher") local Common = require("./Common") +local Dispatcher = require("./Dispatcher") local ERROR = require("./Error") local REFLECTION = require("./Reflection") @@ -17,7 +17,7 @@ local function OBJECT_REFLECTION_TEST(object, oName) return object and object[Common.ObjectHeader], "BAD_SELF_CALL", oName end -return function(params: {[string]: any}, service, OBJECT_PRIVATE) +return function(params: { [string]: any }, service, OBJECT_PRIVATE) local raw, metakeys = Common.validateTable(params, "Object", OBJECT_PARAMS) local private = { @@ -25,46 +25,42 @@ return function(params: {[string]: any}, service, OBJECT_PRIVATE) FullID = "", Service = service, TimeoutDisabled = if metakeys.TimeoutDisabled then metakeys.TimeoutDisabled else false, - AwaitFor = metakeys.AwaitFor or {} + AwaitFor = metakeys.AwaitFor or {}, } OBJECT_PRIVATE[raw] = private raw[Common.ObjectHeader] = true Common.assignObjectID(raw, private, service) raw.Name = params.Name or `CatworkAsyncObject` - - function raw:Spawn(xpcallHandler, asyncHandler) - ERROR.DEPRECATED("Object.Spawn", "Service.SpawnObject") - end - + + function raw:Spawn(xpcallHandler, asyncHandler) ERROR.DEPRECATED("Object.Spawn", "Service.SpawnObject") end + function raw:Await() REFLECTION.CUSTOM(1, "Object.Await", self, OBJECT_REFLECTION_TEST) - + return Dispatcher.slotAwait(self) end - + function raw:HandleAsync(asyncHandler) REFLECTION.CUSTOM(1, "Object.HandleAsync", self, OBJECT_REFLECTION_TEST) REFLECTION.ARG(2, "Object.HandleAsync", REFLECTION.FUNCTION, asyncHandler) - + Dispatcher.slotHandleAsync(self, asyncHandler) end - + function raw:GetID(full: boolean?) REFLECTION.CUSTOM(1, "Object.GetID", self, OBJECT_REFLECTION_TEST) - REFLECTION.ARG(2, "Object.GetID", REFLECTION.OPT_BOOLEAN, full) - + REFLECTION.ARG(2, "Object.GetID", REFLECTION.OPT_BOOLEAN, full) + local private = OBJECT_PRIVATE[self] - if not private then - ERROR.OBJECT_DESTROYED(self) - end + if not private then ERROR.OBJECT_DESTROYED(self) end return full and OBJECT_PRIVATE[self].FullID or OBJECT_PRIVATE[self].ID end function raw:Destroy() REFLECTION.CUSTOM(1, "Object.Destroy", self, OBJECT_REFLECTION_TEST) - + if not self[Common.ObjectHeader] then ERROR.BAD_SELF_CALL("Object.Destroy") end local state = Dispatcher.getObjectState(self) @@ -85,7 +81,7 @@ return function(params: {[string]: any}, service, OBJECT_PRIVATE) function raw:GetState() REFLECTION.CUSTOM(1, "Object.GetState", self, OBJECT_REFLECTION_TEST) - + return Dispatcher.getStateString(self) end @@ -94,9 +90,9 @@ return function(params: {[string]: any}, service, OBJECT_PRIVATE) __tostring = function(self) local private = OBJECT_PRIVATE[self] return `CatworkAsyncObject({self.Name}::{private.FullID})` - end + end, }) end - + return raw -end \ No newline at end of file +end diff --git a/src/lib/Reflection.luau b/src/lib/Reflection.luau index 281dfd1..8832217 100644 --- a/src/lib/Reflection.luau +++ b/src/lib/Reflection.luau @@ -29,12 +29,11 @@ Reflection.OPT_FUNCTION = 11 Reflection.OPT_THREAD = 13 Reflection.OPT_USERDATA = 15 Reflection.OPT_VECTOR = 17 -Reflection.OPT_BUFFER = 19 - +Reflection.OPT_BUFFER = 19 local LUAU_NATIVE_TYPE = { -- Enum -> type() bindings - + [Reflection.BOOLEAN] = "boolean", [Reflection.NUMBER] = "number", [Reflection.STRING] = "string", @@ -54,26 +53,29 @@ end function Reflection.ARG(argIdx: number, functionName: string, enum: number, incoming: any) if enum == 0 then -- fast-path, enum val is NIL - if incoming ~= nil then - ERROR.BAD_ARG(argIdx, functionName, "nil", type(incoming)) - end - + if incoming ~= nil then ERROR.BAD_ARG(argIdx, functionName, "nil", type(incoming)) end + return end - + local isOpt = bit32.band(enum, 1) == 1 local typeVal = LUAU_NATIVE_TYPE[bit32.band(enum, 0b1111110)] - + if not typeVal then return end if isOpt and incoming == nil then return end - + local t = type(incoming) if t == typeVal then return end ERROR.BAD_ARG(argIdx, functionName, typeVal, t) end -function Reflection.CUSTOM(argIdx: number, functionName: string, incoming: any, assertion: (any, string, number) -> (boolean, ...any)) +function Reflection.CUSTOM( + argIdx: number, + functionName: string, + incoming: any, + assertion: (any, string, number) -> (boolean, ...any) +) reflectionCallbackHandler(assertion(incoming, functionName, argIdx)) end -return Reflection \ No newline at end of file +return Reflection diff --git a/src/lib/Service.luau b/src/lib/Service.luau index 9e9f8d8..c70f85d 100644 --- a/src/lib/Service.luau +++ b/src/lib/Service.luau @@ -1,14 +1,14 @@ if not task then task = require("@lune/task") end local Class = require("./Class") -local Dispatcher = require("./Dispatcher") -local Object = require("./Object") local Common = require("./Common") +local Dispatcher = require("./Dispatcher") local ERROR = require("./Error") -local REFLECTION = require("./Reflection") local Metakeys = require("../meta") +local Object = require("./Object") +local REFLECTION = require("./Reflection") -local ENABLE_CLASSES_METAKEY = Metakeys.export "EnableClasses" +local ENABLE_CLASSES_METAKEY = Metakeys.export("EnableClasses") local OBJECT_PRIVATE = {} local SERVICE_PARAMS = { @@ -19,7 +19,7 @@ local SERVICE_PARAMS = { ObjectAdded = "function?", ObjectRemoved = "function?", ClassAdded = "function?", - Updating = "function?" + Updating = "function?", } local function SERVICE_REFLECTION_TEST(service, fName) @@ -49,7 +49,7 @@ end -- Constructor return function(params) - local raw, metakeys = Common.validateTable(params, "Service", SERVICE_PARAMS) + local raw, metakeys = Common.validateTable(params, "Service", SERVICE_PARAMS) local enableClasses = (params.ClassAdded ~= nil) or (metakeys.EnableClasses or false) @@ -57,7 +57,7 @@ return function(params) raw[ENABLE_CLASSES_METAKEY] = enableClasses local enableUpdateLoop = if metakeys.EnableUpdating ~= nil then metakeys.EnableUpdating else true - + function raw:Class(name, createObject) REFLECTION.CUSTOM(1, "Service.Class", self, SERVICE_REFLECTION_TEST) REFLECTION.ARG(2, "Service.Class", REFLECTION.STRING, name) @@ -72,10 +72,8 @@ return function(params) REFLECTION.ARG(3, "Service.CreateObjectFromClass", REFLECTION.OPT_TABLE, initParams) if not self[ENABLE_CLASSES_METAKEY] then ERROR.SERVICE_NO_CLASSES(self.Name) end - if class.Service ~= self then - ERROR.BAD_CLASS(class.Name, self.Name) - end - + if class.Service ~= self then ERROR.BAD_CLASS(class.Name, self.Name) end + local params = initParams or {} params.Name = params.Name or class.Name class.CreateObject(params) @@ -108,16 +106,12 @@ return function(params) end if not raw.ObjectAdded then - function raw:ObjectAdded(object) - self:SpawnObject(object) - end + function raw:ObjectAdded(object) self:SpawnObject(object) end end if enableUpdateLoop then if not raw.Updating then - function raw:Updating(object, dt) - return object:Update(dt) - end + function raw:Updating(object, dt) return object:Update(dt) end end elseif raw.Updating then ERROR.SERVICE_UPDATING_DISABLED(raw.Name) @@ -126,12 +120,10 @@ return function(params) if not Common.Flags.DONT_ASSIGN_OBJECT_MT then setmetatable(raw, { - __tostring = function(self) - return `CatworkService({self.Name}; Classes: {self[ENABLE_CLASSES_METAKEY]})` - end, + __tostring = function(self) return `CatworkService({self.Name}; Classes: {self[ENABLE_CLASSES_METAKEY]})` end, }) end - + table.freeze(raw) return raw -end \ No newline at end of file +end diff --git a/src/lib/Types.luau b/src/lib/Types.luau index e251cd6..cdb59ad 100644 --- a/src/lib/Types.luau +++ b/src/lib/Types.luau @@ -2,13 +2,16 @@ -- for the time being, we should let most stuff pass -- if you experience problems, please file a Pull request -export type Metakey = typeof(setmetatable({}::{ - Key: A -}, {}::{ - __tostring: (Metakey) -> string -})) +export type Metakey = typeof(setmetatable( + {} :: { + Key: A, + }, + {} :: { + __tostring: (Metakey) -> string, + } +)) -type ObjectState = "dead"|"pending"|"spawned" +type ObjectState = "dead" | "pending" | "spawned" export type Object = { Name: string, GetID: (Object, full: boolean?) -> string, @@ -24,7 +27,7 @@ export type Object = { HandleAsync: (Object, asyncHandler: (boolean, string) -> ()?) -> (), } & A -export type BlankObject = Object<{[string]: any}> +export type BlankObject = Object<{ [string]: any }> export type Class = { Name: string, @@ -35,7 +38,7 @@ export type Class = { export type Service = { Name: string, Object: (Service, A) -> Object, - + Class: (Service, name: string, createObject: (Object) -> ()) -> Class, CreateObjectFromClass: (Service, Class, initParams: B?) -> Object, @@ -52,7 +55,7 @@ export type Service = { CreateObject: (Service, A) -> (), ObjectAdded: (Service, BlankObject) -> (), ObjectRemoved: (Service, BlankObject) -> (), - ClassAdded: (Service, Class) -> () + ClassAdded: (Service, Class) -> (), } export type ServiceCtorParams = { @@ -66,7 +69,7 @@ export type ServiceCtorParams = { ObjectRemoved: (Service, BlankObject) -> ()?, ClassAdded: (Service, Class) -> ()?, - [string|Metakey]: any + [string | Metakey]: any, } -return nil \ No newline at end of file +return nil diff --git a/src/meta.luau b/src/meta.luau index 3ad9881..792201f 100644 --- a/src/meta.luau +++ b/src/meta.luau @@ -5,25 +5,18 @@ local MetakeySymbolic = newproxy(false) local function metakey(key): Types.Metakey return setmetatable({ [MetakeySymbolic] = true, - Key = key + Key = key, }, { - __tostring = function(self) - return `Metakey<{self.Key}>` - end + __tostring = function(self) return `Metakey<{self.Key}>` end, }) end -type METAKEY_WELL_KNOWN = - "AwaitFor" - |"EnableClasses" - |"EnableUpdating" - |"PluginMetadata" - |"TimeoutDisabled" +type METAKEY_WELL_KNOWN = "AwaitFor" | "EnableClasses" | "EnableUpdating" | "PluginMetadata" | "TimeoutDisabled" local MetakeyMemoization = {} return { - export = function(rhs: METAKEY_WELL_KNOWN|string): Types.Metakey + export = function(rhs: METAKEY_WELL_KNOWN | string): Types.Metakey local key = MetakeyMemoization[rhs] if not key then key = metakey(rhs) @@ -33,5 +26,5 @@ return { return key end, - Symbol = MetakeySymbolic -} \ No newline at end of file + Symbol = MetakeySymbolic, +} diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..ceb6c00 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,10 @@ +column_width = 120 +line_endings = "Unix" +indent_type = "Tabs" +indent_width = 4 +quote_style = "AutoPreferDouble" +call_parentheses = "Always" +collapse_simple_statement = "Always" + +[sort_requires] +enabled = true