From c7b816f4d0f99b4d25108b2dafeaa5e5a0ad7387 Mon Sep 17 00:00:00 2001 From: Lewin Kelly Date: Mon, 29 Jan 2024 23:49:16 +0000 Subject: [PATCH] Improve typing and formatting of files --- defs.d.lua | 40 +++++++++------------ luau/45284430.luau | 4 +-- luau/60595411.luau | 88 +++++++++++++++++++++++++--------------------- luau/73157242.luau | 81 ++++++++++++++++++++---------------------- luau/97188756.luau | 42 +++++++++++----------- 5 files changed, 125 insertions(+), 130 deletions(-) diff --git a/defs.d.lua b/defs.d.lua index 70cc72b..1996a47 100644 --- a/defs.d.lua +++ b/defs.d.lua @@ -3149,8 +3149,11 @@ declare class Vector3 Unit: Vector3 unit: Vector3 X: number + x: number Y: number + y: number Z: number + z: number function Angle(self, other: Vector3, axis: Vector3?): number function Cross(self, other: Vector3): Vector3 function Dot(self, other: Vector3): number @@ -3194,31 +3197,18 @@ end declare class CFrame lookVector: Vector3 - LookVector: Vector3 -- ? - Position: Vector3 - RightVector: Vector3 - Rotation: CFrame - UpVector: Vector3 + p: Vector3 X: number - XVector: Vector3 Y: number - YVector: Vector3 Z: number - ZVector: Vector3 - function GetComponents(self): (number, number, number, number, number, number, number, number, number, number, number, number) - function Inverse(self): CFrame - function Lerp(self, goal: CFrame, alpha: number): CFrame - function Orthonormalize(self): CFrame - function PointToObjectSpace(self, v3: Vector3): Vector3 - function PointToWorldSpace(self, v3: Vector3): Vector3 - function ToAxisAngle(self): (Vector3, number) - function ToEulerAnglesXYZ(self): (number, number, number) - function ToEulerAnglesYXZ(self): (number, number, number) - function ToObjectSpace(self, cf: CFrame): CFrame - function ToOrientation(self): (number, number, number) - function ToWorldSpace(self, cf: CFrame): CFrame - function VectorToObjectSpace(self, v3: Vector3): Vector3 - function VectorToWorldSpace(self, v3: Vector3): Vector3 + function inverse(self): CFrame + function pointToObjectSpace(self, v3: Vector3): Vector3 + function pointToWorldSpace(self, v3: Vector3): Vector3 + function toEulerAnglesXYZ(self): (number, number, number) + function toObjectSpace(self, cf: CFrame): CFrame + function toWorldSpace(self, cf: CFrame): CFrame + function vectorToObjectSpace(self, v3: Vector3): Vector3 + function vectorToWorldSpace(self, v3: Vector3): Vector3 function __add(self, other: Vector3): CFrame function __mul(self, other: CFrame): CFrame function __mul(self, other: Vector3): Vector3 @@ -6214,6 +6204,8 @@ declare class Terrain extends BasePart function WriteVoxels(self, region: Region3, resolution: number, materials: { any }, occupancy: { any }): nil function AutoWedgeCell(self, x: number, y: number, z: number): boolean + function SetWaterCell(self, x: number, y: number, z: number, waterForce: EnumWaterForce, waterDirection: EnumWaterDirection): nil + function SetCell(self, x: number, y: number, z: number, material: EnumCellMaterial, orientation: EnumCellOrientation): nil end declare class TriangleMeshPart extends BasePart @@ -7051,6 +7043,8 @@ declare class ScriptContext extends Instance function StopScriptProfiling(self): string function AddCoreScript(self, id: number, player: Player, name: string): nil + function RegisterLibrary(self, name: string, id: string): nil + function LibraryRegistrationComplete(self): nil end declare class ScriptDebugger extends Instance @@ -8903,7 +8897,7 @@ export type Red = { Load: (self: Red, Script: LuaSourceContainer) -> RedCore, } -declare LoadLibrary: ((libraryName: "RbxFusion") -> Fusion) & ((libraryName: "RbxRed") -> Red) +declare LoadLibrary: ((libraryName: "RbxFusion") -> Fusion) & ((libraryName: "RbxRed") -> Red) & ((libraryName: string) -> any) declare function settings(): GlobalSettings declare function UserSettings(): UserSettings diff --git a/luau/45284430.luau b/luau/45284430.luau index 85b65fd..8cc2234 100644 --- a/luau/45284430.luau +++ b/luau/45284430.luau @@ -2404,8 +2404,8 @@ RbxGui.CreateTutorial = function(name, tutorialKey, createButtons) end local function CreateBasicTutorialPage( - name, - handleResize, + name: string, + handleResize: (number, number) -> (), skipTutorial, giveDoneButton ) diff --git a/luau/60595411.luau b/luau/60595411.luau index b577e12..53b8e2b 100644 --- a/luau/60595411.luau +++ b/luau/60595411.luau @@ -111,25 +111,17 @@ end function JsonWriter:ParseString(s) self:Append '"' - self:Append(string.gsub(s, '[%z%c\\"/]', function(n) - local c = self.backslashes[n] - if c then - return c - end - return string.format("\\u%.4X", string.byte(n)) - end)) + local copy = string.gsub(s, '[%z%c\\"/]', function(n) + return self.backslashes[n] or string.format("\\u%.4X", string.byte(n)) + end) + self:Append(copy) self:Append '"' end function JsonWriter:IsArray(t) local count = 0 - local isindex = function(k) - if type(k) == "number" and k > 0 then - if math.floor(k) == k then - return true - end - end - return false + local function isindex(k) + return type(k) == "number" and k > 0 and math.floor(k) == k end for k, _ in pairs(t) do if not isindex(k) then @@ -451,9 +443,9 @@ function Null() end -------------------- End JSON Parser ------------------------ -local t = {} +local RbxUtility = {} -t.DecodeJSON = function(jsonString) +RbxUtility.DecodeJSON = function(jsonString: string) pcall(function() warn "RbxUtility.DecodeJSON is deprecated, please use Game:GetService('HttpService'):JSONDecode() instead." end) @@ -465,7 +457,7 @@ t.DecodeJSON = function(jsonString) return nil end -t.EncodeJSON = function(jsonTable) +RbxUtility.EncodeJSON = function(jsonTable: { [any]: any }) pcall(function() warn "RbxUtility.EncodeJSON is deprecated, please use Game:GetService('HttpService'):JSONEncode() instead." end) @@ -482,17 +474,17 @@ end --makes a wedge at location x, y, z --sets cell x, y, z to default material if parameter is provided, if not sets cell x, y, z to be whatever material it previously w --returns true if made a wedge, false if the cell remains a block -t.MakeWedge = function(x, y, z, _) +RbxUtility.MakeWedge = function(x, y, z, _) return game:GetService("Terrain"):AutoWedgeCell(x, y, z) end -t.SelectTerrainRegion = function( - regionToSelect, - colour, - selectEmptyCells, - selectionParent +RbxUtility.SelectTerrainRegion = function( + regionToSelect: Region3, + colour: BrickColor, + selectEmptyCells: boolean, + selectionParent: Instance ) - local terrain = game.Workspace:FindFirstChild "Terrain" + local terrain = game.Workspace:FindFirstChild "Terrain" :: Terrain if not terrain then return end @@ -500,14 +492,15 @@ t.SelectTerrainRegion = function( assert(regionToSelect) assert(colour) - if type(regionToSelect) ~= "Region3" then + -- can't use typeof + if not (regionToSelect.CFrame and regionToSelect.Size) then error( `regionToSelect (first arg), should be of type Region3, but is type {type( regionToSelect )}` ) end - if type(colour) ~= "BrickColor" then + if not (colour.Number and colour.r and colour.Color) then error( `color (second arg), should be of type BrickColor, but is type {type( colour @@ -669,7 +662,7 @@ t.SelectTerrainRegion = function( adornments.SelectionPart = selectionPart adornments.SelectionBox = selectionBox - updateSelection = function(newRegion, newColour) + updateSelection = function(newRegion: Region3, newColour) if newRegion and newRegion ~= lastRegion then lastRegion = newRegion selectionPart.Size = newRegion.Size @@ -737,7 +730,7 @@ Method :wait() Description: This call blocks until ]] -function t.CreateSignal() +function RbxUtility.CreateSignal() local this = {} local mBindableEvent = Instance.new "BindableEvent" @@ -800,7 +793,7 @@ function t.CreateSignal() end ------------------------------------------------- Sigal class End ------------------------------------------------------ - +-- this ones my favourite - Heliodex ------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------ @@ -894,7 +887,7 @@ Note: It is also perfectly legal to save a reference to the function returned by --the Create function need to be created as a functor, not a function, in order to support the Create.E syntax, so it --will be created in several steps rather than as a single function declaration. -local function Create_PrivImpl(objectType) +local function Create_PrivImpl(objectType: string) if type(objectType) ~= "string" then error("Argument of Create must be a string", 2) end @@ -902,7 +895,7 @@ local function Create_PrivImpl(objectType) --The first function call is a function call using Lua's single-string-argument syntax --The second function call is using Lua's single-table-argument syntax --Both can be chained together for the nice effect. - return function(dat) + return function(dat: { [string | number | { any } | any]: any }) --default to nothing, to handle the no argument given case dat = dat or {} @@ -944,7 +937,7 @@ local function Create_PrivImpl(objectType) obj[k.__eventname]:connect(v) --define constructor function - elseif k == t.Create then + elseif k == RbxUtility.Create then if type(v) ~= "function" then error( `Bad entry in Create body: Key \`[Create]\` should be paired with a constructor function, got: {v}`, @@ -978,7 +971,7 @@ local function Create_PrivImpl(objectType) end --now, create the functor: -t.Create = setmetatable({}, { +RbxUtility.Create = setmetatable({}, { __call = function(_, ...) return Create_PrivImpl(...) end, @@ -986,7 +979,7 @@ t.Create = setmetatable({}, { --and create the "Event.E" syntax stub. Really it's just a stub to construct a table which our Create --function can recognize as special. -t.Create.E = function(eventName) +RbxUtility.Create.E = function(eventName) return { __eventname = eventName } end @@ -1000,19 +993,28 @@ end ------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------------------------------------------------ -t.Help = function(funcNameOrFunc) +RbxUtility.Help = function(funcNameOrFunc) --input argument can be a string or a function. Should return a description (of arguments and expected side effects) - if funcNameOrFunc == "DecodeJSON" or funcNameOrFunc == t.DecodeJSON then + if + funcNameOrFunc == "DecodeJSON" + or funcNameOrFunc == RbxUtility.DecodeJSON + then return "Function DecodeJSON. " .. "Arguments: (string). " .. "Side effect: returns a table with all parsed JSON values" end - if funcNameOrFunc == "EncodeJSON" or funcNameOrFunc == t.EncodeJSON then + if + funcNameOrFunc == "EncodeJSON" + or funcNameOrFunc == RbxUtility.EncodeJSON + then return "Function EncodeJSON. " .. "Arguments: (table). " .. "Side effect: returns a string composed of argument table in JSON data format" end - if funcNameOrFunc == "MakeWedge" or funcNameOrFunc == t.MakeWedge then + if + funcNameOrFunc == "MakeWedge" + or funcNameOrFunc == RbxUtility.MakeWedge + then return "Function MakeWedge. " .. "Arguments: (x, y, z, [default material]). " .. "Description: Makes a wedge at location x, y, z. Sets cell x, y, z to default material if " @@ -1021,7 +1023,7 @@ t.Help = function(funcNameOrFunc) end if funcNameOrFunc == "SelectTerrainRegion" - or funcNameOrFunc == t.SelectTerrainRegion + or funcNameOrFunc == RbxUtility.SelectTerrainRegion then return "Function SelectTerrainRegion. " .. "Arguments: (regionToSelect, color, selectEmptyCells, selectionParent). " @@ -1033,7 +1035,10 @@ t.Help = function(funcNameOrFunc) .. "arguments to said function are a new region3 to select, and the adornment color (color arg is optional). " .. "Also returns a second function that takes no arguments and destroys the selection" end - if funcNameOrFunc == "CreateSignal" or funcNameOrFunc == t.CreateSignal then + if + funcNameOrFunc == "CreateSignal" + or funcNameOrFunc == RbxUtility.CreateSignal + then return "Function CreateSignal. " .. "Arguments: None. " .. "Returns: The newly created Signal object. This object is identical to the RBXScriptSignal class " @@ -1082,8 +1087,9 @@ t.Help = function(funcNameOrFunc) .. "Descrition: Create is a very powerfull function, whose description is too long to fit here, and " .. "is best described via example, please see the wiki page for a description of how to use it." end + return "No help available for this function" end --------------------------------------------Documentation Ends---------------------------------------------------------- -return t +return RbxUtility diff --git a/luau/73157242.luau b/luau/73157242.luau index e9b54da..aecfb50 100644 --- a/luau/73157242.luau +++ b/luau/73157242.luau @@ -426,26 +426,17 @@ local function getMouseTargetCFrame(targetPart) return targetPart.CFrame end -local function isBlocker(part) -- returns whether or not we want to cancel the stamp because we're blocked by this part - if not part then +local function isBlocker(part: Instance) -- returns whether or not we want to cancel the stamp because we're blocked by this part + if not part or not part.Parent or part:FindFirstChild "Humanoid" then return false - end - if not part.Parent then - return false - end - if part:FindFirstChild "Humanoid" then - return false - end - if + elseif part:FindFirstChild "RobloxStamper" or part:FindFirstChild "RobloxModel" then return true - end - if part:IsA "Part" and not part.CanCollide then - return false - end - if part == game.Lighting then + elseif + (part:IsA "Part" and not part.CanCollide) or (part == game.Lighting) + then return false end return isBlocker(part.Parent) @@ -453,7 +444,11 @@ end -- helper function to determine if a character can be pushed upwards by a certain amount -- character is 5 studs tall, we'll check a 1.5 x 1.5 x 4.5 box around char, with center .5 studs below torsocenter -local function spaceAboveCharacter(charTorso, newTorsoY, stampData) +local function spaceAboveCharacter( + charTorso: BasePart, + newTorsoY, + stampData: Instance +) local partsAboveChar = game.Workspace:FindPartsInRegion3( Region3.new( Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) @@ -461,7 +456,7 @@ local function spaceAboveCharacter(charTorso, newTorsoY, stampData) Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) + Vector3.new(0.75, 1.75, 0.75) ), - charTorso.Parent, + charTorso.Parent :: Instance, 100 ) @@ -474,30 +469,25 @@ local function spaceAboveCharacter(charTorso, newTorsoY, stampData) end end - if - clusterPartsInRegion( - Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) - - Vector3.new(0.75, 2.75, 0.75), - Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) - + Vector3.new(0.75, 1.75, 0.75) - ) - then - return false - end - - return true + return not clusterPartsInRegion( + Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) + - Vector3.new(0.75, 2.75, 0.75), + Vector3.new(charTorso.Position.X, newTorsoY, charTorso.Position.Z) + + Vector3.new(0.75, 1.75, 0.75) + ) end -local function findConfigAtMouseTarget(Mouse, stampData) +local function findConfigAtMouseTarget(Mouse: Mouse, stampData: Instance) -- *Critical Assumption* : -- This function assumes the target CF axes are orthogonal with the target bounding box faces -- And, it assumes the insert CF axes are orthongonal with the insert bounding box faces -- Therefore, insertion will not work with angled faces on wedges or other "non-block" parts, nor -- will it work for parts in a model that are not orthogonally aligned with the model's CF. + -- This can happen sometimes, return if so if not Mouse then return nil - end -- This can happen sometimes, return if so + end if not stampData then error "findConfigAtMouseTarget: stampData is nil" return nil @@ -572,7 +562,7 @@ local function findConfigAtMouseTarget(Mouse, stampData) if targetPart:IsA "Terrain" then if not cluster then - cluster = game.Workspace:FindFirstChild "Terrain" + cluster = game.Workspace:FindFirstChild "Terrain" :: Terrain end local cellID = cluster:WorldToCellPreferSolid(mouseHitInWorld) if hitPlane then @@ -1025,7 +1015,9 @@ RbxStamper.SetupStamperDragger = function( -- Init all state variables local gInitial90DegreeRotations = 0 - local stampData + local stampData: { + DisabledScripts: { LuaSourceContainer }?, + } local mouseTarget local errorBox = Instance.new "SelectionBox" @@ -1327,6 +1319,8 @@ RbxStamper.SetupStamperDragger = function( minBB += targetCFrame.p - currModelCFrame.p maxBB += targetCFrame.p - currModelCFrame.p + local clusterMat: Instance + -- don't drag into terrain if clusterPartsInRegion( @@ -1391,7 +1385,7 @@ RbxStamper.SetupStamperDragger = function( positionPartsAtCFrame3(targetCFrame, stampData.CurrentParts) lastTarget.CFrame = targetCFrame -- successful positioning, so update 'dat cframe if stampData.CurrentParts:FindFirstChild("ClusterMaterial", true) then - local clusterMat = + clusterMat = stampData.CurrentParts:FindFirstChild("ClusterMaterial", true) if clusterMat:IsA "Vector3Value" then lastTarget.TerrainOrientation = clusterMat.Value.Z @@ -1707,15 +1701,16 @@ RbxStamper.SetupStamperDragger = function( local parts = {} local decals = {} - stampData = {} - stampData.DisabledScripts = {} - stampData.TransparencyTable = {} - stampData.MaterialTable = {} - stampData.CanCollideTable = {} - stampData.AnchoredTable = {} - stampData.ArchivableTable = {} - stampData.DecalTransparencyTable = {} - stampData.SurfaceTypeTable = {} + stampData = { + DisabledScripts = {}, + TransparencyTable = {}, + MaterialTable = {}, + CanCollideTable = {}, + AnchoredTable = {}, + ArchivableTable = {}, + DecalTransparencyTable = {}, + SurfaceTypeTable = {}, + } collectParts(clone, parts, scripts, decals) diff --git a/luau/97188756.luau b/luau/97188756.luau index 6675f76..f1c9d5f 100644 --- a/luau/97188756.luau +++ b/luau/97188756.luau @@ -18,9 +18,10 @@ local function WaitForChild(parent, childName) return parent[childName] end -local function typedef(obj) - return obj -end +-- wtf +-- local function typedef(obj) +-- return obj +-- end local function IsPhone() local cGui = Game:GetService "CoreGui" @@ -51,7 +52,7 @@ while Player.Character == nil do wait(0.03) end local RbxUtility = LoadLibrary "RbxUtility" -local Gui = typedef(RbxUtility) +local Create = RbxUtility.Create local Camera = Game.Workspace.CurrentCamera -- Services @@ -127,7 +128,6 @@ local Input = { --------------------------------------------------- ------------------ Chat class -------------------- local Chat = { - ChatColors = { BrickColor.new "Bright red", BrickColor.new "Bright blue", @@ -1098,7 +1098,7 @@ end function Chat:ComputeSpaceString(pLabel) local nString = " " if not self.TempSpaceLabel then - self.TempSpaceLabel = Gui.Create "TextButton" { + self.TempSpaceLabel = Create "TextButton" { Size = UDim2.new( 0, pLabel.AbsoluteSize.X, @@ -1227,7 +1227,7 @@ function Chat:CreateMessage(cPlayer, message) end --else -- Haven't hit the mark yet, so keep creating - pLabel = Gui.Create "TextLabel" { + pLabel = Create "TextLabel" { Name = pName, Text = `{pName}:`, -- TextColor3 = pColor, @@ -1259,7 +1259,7 @@ function Chat:CreateMessage(cPlayer, message) nString = self.CachedSpaceStrings_List[pName] end - mLabel = Gui.Create "TextLabel" { + mLabel = Create "TextLabel" { Name = `{pName} - message`, -- Max is 3 lines Size = UDim2.new(1, 0, 0.5, 0), @@ -1353,7 +1353,7 @@ function Chat:CreateSafeChatOptions(list, rootButton) rootButton = rootButton or self.SafeChatButton for msg, _ in pairs(list) do if type(msg) == "string" then - local chatText = Gui.Create "TextButton" { + local chatText = Create "TextButton" { Name = msg, Text = msg, Size = UDim2.new(0, 100, 0, 20), @@ -1404,13 +1404,13 @@ function Chat:CreateSafeChatOptions(list, rootButton) end function Chat:CreateSafeChatGui() - self.SafeChatFrame = Gui.Create "Frame" { + self.SafeChatFrame = Create "Frame" { Name = "SafeChatFrame", Size = UDim2.new(1, 0, 1, 0), Parent = self.Gui, BackgroundTransparency = 1, - Gui.Create "ImageButton" { + Create "ImageButton" { Name = "SafeChatButton", Size = UDim2.new(0, 44, 0, 31), Position = UDim2.new(0, 1, 0.35, 0), @@ -1443,21 +1443,21 @@ end -- For touch devices we create a button instead function Chat:CreateTouchButton() - self.ChatTouchFrame = Gui.Create "Frame" { + self.ChatTouchFrame = Create "Frame" { Name = "ChatTouchFrame", Size = UDim2.new(0, 128, 0, 32), Position = UDim2.new(0, 88, 0, 0), BackgroundTransparency = 1, Parent = self.Gui, - Gui.Create "ImageButton" { + Create "ImageButton" { Name = "ChatLabel", Size = UDim2.new(0, 74, 0, 28), Position = UDim2.new(0, 0, 0, 0), BackgroundTransparency = 1, ZIndex = 2.0, }, - Gui.Create "ImageLabel" { + Create "ImageLabel" { Name = "Background", Size = UDim2.new(1, 0, 1, 0), Position = UDim2.new(0, 0, 0, 0), @@ -1468,7 +1468,7 @@ function Chat:CreateTouchButton() self.TapToChatLabel = self.ChatTouchFrame.ChatLabel self.TouchLabelBackground = self.ChatTouchFrame.Background - self.ChatBar = Gui.Create "TextBox" { + self.ChatBar = Create "TextBox" { Name = "ChatBar", Size = UDim2.new(1, 0, 0.2, 0), Position = UDim2.new(0, 0, 0.8, 800), @@ -1500,7 +1500,7 @@ function Chat:CreateChatBar() return GuiService.UseLuaChat end) if forceChatGUI or (status and result) then - self.ClickToChatButton = Gui.Create "TextButton" { + self.ClickToChatButton = Create "TextButton" { Name = "ClickToChat", Size = UDim2.new(1, 0, 0, 20), BackgroundTransparency = 1, @@ -1513,7 +1513,7 @@ function Chat:CreateChatBar() FontSize = Enum.FontSize.Size12, } - self.ChatBar = Gui.Create "TextBox" { + self.ChatBar = Create "TextBox" { Name = "ChatBar", Size = UDim2.new(1, 0, 0, 20), Position = UDim2.new(0, 0, 1, 0), @@ -1554,7 +1554,7 @@ end -- Done only once function Chat:CreateGui() self.Gui = WaitForChild(CoreGuiService, "RobloxGui") - self.Frame = Gui.Create "Frame" { + self.Frame = Create "Frame" { Name = "ChatFrame", --Size = self.Configuration.Size; Size = UDim2.new(0, 500, 0, 120), @@ -1565,7 +1565,7 @@ function Chat:CreateGui() Parent = self.Gui, Active = false, - Gui.Create "ImageLabel" { + Create "ImageLabel" { Name = "Background", Image = "http://banland.xyz/asset/?id=97120937", --96551212'; Size = UDim2.new(1.3, 0, 1.64, 0), @@ -1575,7 +1575,7 @@ function Chat:CreateGui() Visible = false, }, - Gui.Create "Frame" { + Create "Frame" { Name = "Border", Size = UDim2.new(1, 0, 0, 1), Position = UDim2.new(0, 0, 0.8, 0), @@ -1585,7 +1585,7 @@ function Chat:CreateGui() Visible = false, }, - Gui.Create "Frame" { + Create "Frame" { Name = "ChatRenderFrame", Size = UDim2.new(1.02, 0, 1.01, 0), Position = UDim2.new(0, 0, 0, 0),