From 52789a6a9e91baa233db54cb927b5e1ad970ddda Mon Sep 17 00:00:00 2001 From: Pyseph Date: Mon, 17 Oct 2022 16:16:27 +0200 Subject: [PATCH] v1.13.0 - Remove ClientCaster:GetPing - Remove ClientCaster:GetMaxPingExhaustion - Remove ClientCaster:SetMaxPingExhaustion - Update raycast data serialization to take up less space --- .github/workflows/ReleaseBuild.yml | 57 ------------------ lib/ClientHandler.client.luau | 8 +-- lib/init.luau | 96 +++++++----------------------- testing.project.json | 9 ++- testing/Testing.server.luau | 38 ------------ testing/client/Client.client.luau | 25 ++++++++ testing/server/Testing.server.luau | 54 +++++++++++++++++ 7 files changed, 114 insertions(+), 173 deletions(-) delete mode 100644 .github/workflows/ReleaseBuild.yml delete mode 100644 testing/Testing.server.luau create mode 100644 testing/client/Client.client.luau create mode 100644 testing/server/Testing.server.luau diff --git a/.github/workflows/ReleaseBuild.yml b/.github/workflows/ReleaseBuild.yml deleted file mode 100644 index e481a92..0000000 --- a/.github/workflows/ReleaseBuild.yml +++ /dev/null @@ -1,57 +0,0 @@ -name: Publish Release -on: - push: - branches: - - main - tags-ignore: - - workflows - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - # This workflow contains a single job called "build" - build: - # The type of runner that the job will run on - runs-on: windows-latest - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v3 - - - name: release - uses: actions/create-release@v1 - id: create_release - with: - draft: false - prerelease: false - release_name: "Release" - tag_name: ver-${{ github.sha }} - body: "" - env: - GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} - - - name: Install Foreman - uses: rojo-rbx/setup-foreman@v1 - with: - token: ${{ secrets.RELEASE_TOKEN }} - - run: foreman install - - - name: Build release.rbxm - run: rojo build -o release.rbxm default.project.json - - - name: Upload release.rbxm - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} - with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./release.rbxm - asset_name: release.rbxm - asset_content_type: application/rbxm - - - name: Create a Release - uses: elgohr/Github-Release-Action@v4 - env: - GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN }} - with: - title: Release diff --git a/lib/ClientHandler.client.luau b/lib/ClientHandler.client.luau index c1dd494..6bf8c04 100644 --- a/lib/ClientHandler.client.luau +++ b/lib/ClientHandler.client.luau @@ -207,10 +207,10 @@ end local function SerializeResult(Result) return { - Instance = Result.Instance, - Position = Result.Position, - Material = Result.Material, - Normal = Result.Normal, + Result.Instance, + Result.Position, + Result.Material, + Result.Normal, } end local function DeserializeParams(Input) diff --git a/lib/init.luau b/lib/init.luau index 97819a8..7b57cae 100644 --- a/lib/init.luau +++ b/lib/init.luau @@ -75,29 +75,6 @@ local PingRemote = ReplicatedStorage:FindFirstChild("ClientCast-Ping") local Signal = require(script.Signal) -local function SafeRemoteInvoke(RemoteFunction, Player, MaxYield) - local ThreadResumed = false - local Thread = coroutine.running() - - task.spawn(function() - local TimestampStart = time() - RemoteFunction:InvokeClient(Player) - local TimestampEnd = time() - - ThreadResumed = true - task.spawn(Thread, math.min(TimestampEnd - TimestampStart, MaxYield)) - end) - - task.delay(MaxYield, function() - if not ThreadResumed then - ThreadResumed = true - task.spawn(Thread, MaxYield) - end - end) - - return coroutine.yield() -end - local function SerializeParams(Params) return { FilterDescendantsInstances = Params.FilterDescendantsInstances, @@ -120,11 +97,6 @@ local function AssertClass(Object, ExpectedClass, Message) error(string.format(Message, ExpectedClass, Object.Class), 4) end end -local function AssertNaN(Object, Message) - if Object ~= Object then - error(string.format(Message, "number", typeof(Object)), 4) - end -end local function IsValidOwner(Value, Error) local IsInstance = IsA(Value, "Instance") if not IsInstance and Value ~= nil then @@ -133,18 +105,30 @@ local function IsValidOwner(Value, Error) error(Error or "SetOwner only takes player or 'nil' instance as an argument.", 4) end end -local function IsValid(SerializedResult) +local function DeserializeRaycastResult(SerializedResult) if not IsA(SerializedResult, "table") then return false end - return ( - SerializedResult.Instance ~= nil - and (SerializedResult.Instance:IsA("BasePart") or SerializedResult.Instance:IsA("Terrain")) - ) - and IsA(SerializedResult.Position, "Vector3") - and IsA(SerializedResult.Material, "EnumItem") - and IsA(SerializedResult.Normal, "Vector3") + local HitInstance, Position, Material, Normal = unpack(SerializedResult) + local IsValid = ( + HitInstance ~= nil + and (HitInstance:IsA("BasePart") or HitInstance:IsA("Terrain")) + ) + and IsA(Position, "Vector3") + and IsA(Material, "EnumItem") + and IsA(Normal, "Vector3") + + if IsValid then + return true, { + Instance = HitInstance, + Position = Position, + Material = Material, + Normal = Normal, + } + else + return false + end end local Replication = {} @@ -164,12 +148,13 @@ function ReplicationBase:Start() }) local LocalizedConnection - LocalizedConnection = ReplicationRemote.OnServerEvent:Connect(function(Player, CasterId, Code, RaycastResult) + LocalizedConnection = ReplicationRemote.OnServerEvent:Connect(function(Player, CasterId, Code, SerializedRaycastResult) if self.Caster.Disabled or self.Caster._UniqueId ~= CasterId then return end + local IsValid, RaycastResult = DeserializeRaycastResult(SerializedRaycastResult) - if Player == Owner and IsValid(RaycastResult) and (Code == "Any" or Code == "Humanoid") then + if Player == Owner and IsValid and (Code == "Any" or Code == "Humanoid") then local Humanoid if Code == "Humanoid" then local Model = RaycastResult.Instance:FindFirstAncestorOfClass("Model") @@ -432,41 +417,6 @@ end function ClientCaster:GetOwner() return self.Owner end ---[=[ - Determines how long the ClientCaster:GetPing() method can yield for before resuming, to protect against potential exploiting. - Default exhaustion time is 1. - @param Time number -]=] -function ClientCaster:SetMaxPingExhaustion(Time) - AssertType(Time, "number", "Unexpected argument #1 to 'ClientCaster.SetMaxPingExhaustion' (%s expected, got %s)") - AssertNaN(Time, "Unexpected argument #1 to 'ClientCaster.SetMaxPingExhaustion' (%s expected, got NaN)") - if Time < 0.1 then - error("The max ping exhaustion time passed to 'ClientCaster.SetMaxPingExhaustion' must be longer than 0.1", 3) - return - end - - self._ExhaustionTime = Time -end ---[=[ - Returns how long the ClientCaster:GetPing() method can yield before resuming. - @return number -]=] -function ClientCaster:GetMaxPingExhaustion() - return self._ExhaustionTime -end ---[=[ - If the ClientCaster object has a set Owner, it will return the ping of that player by calculating delay between client-server. - NOTE This is a yielding function, and it will yield until it gets the players ping. - @return number - @yields -]=] -function ClientCaster:GetPing() - if self.Owner == nil then - return 0 - end - - return SafeRemoteInvoke(PingRemote, self.Owner, self._ExhaustionTime) -end --[=[ Sets this ClientCaster's object which it will raycast from to Object. @param Object Instance diff --git a/testing.project.json b/testing.project.json index 43fa254..36e6284 100644 --- a/testing.project.json +++ b/testing.project.json @@ -10,7 +10,14 @@ }, "ServerScriptService": { "$className": "ServerScriptService", - "$path": "testing" + "$path": "testing/server" + }, + "StarterPlayer": { + "$className": "StarterPlayer", + "StarterPlayerScripts": { + "$className": "StarterPlayerScripts", + "$path": "testing/client" + } } } } \ No newline at end of file diff --git a/testing/Testing.server.luau b/testing/Testing.server.luau deleted file mode 100644 index 8179a1f..0000000 --- a/testing/Testing.server.luau +++ /dev/null @@ -1,38 +0,0 @@ -local ReplicatedStorage = game:GetService("ReplicatedStorage") -local Players = game:GetService("Players") - -local TestingPlayer = Players:GetPlayers()[1] or Players.PlayerAdded:Wait() - -local ClientCast = require(ReplicatedStorage.ClientCast):: {} - -local KillPart = Instance.new("Part") -KillPart.Anchored = true -KillPart.CanCollide = false -KillPart.CFrame = CFrame.new(0, 1, 0) -KillPart.Parent = workspace - -local function GenerateAttachment(Position) - local Attachment = Instance.new("Attachment") - Attachment.Name = "DmgPoint" - Attachment.Position = Position - Attachment.Parent = KillPart -end - -for X = -2, 2 do - GenerateAttachment(Vector3.new(X, 0, 0)) -end - -local ClientCaster = ClientCast.new(KillPart, RaycastParams.new(), TestingPlayer) -local Debounce = {} -ClientCaster.HumanoidCollided:Connect(function(_, HitHumanoid) - if Debounce[HitHumanoid] then - return - end - Debounce[HitHumanoid] = true - print("Ow!") - HitHumanoid:TakeDamage(10) - - wait(0.5) - Debounce[HitHumanoid] = false -end) -ClientCaster:Start() \ No newline at end of file diff --git a/testing/client/Client.client.luau b/testing/client/Client.client.luau new file mode 100644 index 0000000..a051151 --- /dev/null +++ b/testing/client/Client.client.luau @@ -0,0 +1,25 @@ +local Players = game:GetService("Players") +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local LocalPlayer = Players.LocalPlayer +local PlayerGui = LocalPlayer:WaitForChild("PlayerGui") + +local ToggleUI = Instance.new("ScreenGui") + +local ToggleButton = Instance.new("TextButton") +ToggleButton.Size = UDim2.new(0, 100, 0, 50) +ToggleButton.Position = UDim2.new(0, 0, 0, 0) +ToggleButton.Text = "Toggle" +ToggleButton.TextColor3 = Color3.new(1, 0, 0) +ToggleButton.TextScaled = true +ToggleButton.Parent = ToggleUI + +ToggleUI.Parent = PlayerGui + +local Remote = ReplicatedStorage:WaitForChild("RemoteEvent") +local Enabled = false +ToggleButton.MouseButton1Click:Connect(function() + Enabled = not Enabled + ToggleButton.TextColor3 = Enabled and Color3.new(0, 1, 0) or Color3.new(1, 0, 0) + Remote:FireServer() +end) \ No newline at end of file diff --git a/testing/server/Testing.server.luau b/testing/server/Testing.server.luau new file mode 100644 index 0000000..94591d9 --- /dev/null +++ b/testing/server/Testing.server.luau @@ -0,0 +1,54 @@ +local Players = game:GetService("Players") +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local ClientCast = require(ReplicatedStorage.ClientCast) + +local RemoteEvent = Instance.new("RemoteEvent") +RemoteEvent.Parent = ReplicatedStorage + +local TestPart = Instance.new("Part") +TestPart.Anchored = true +TestPart.CanCollide = false +TestPart.CFrame = CFrame.new(0, 2, 0) +TestPart.Parent = workspace + +for X = -2, 2 do + local Attachment = Instance.new("Attachment") + Attachment.Name = "DmgPoint" + Attachment.Position = Vector3.new(X, 0, 0) + Attachment.Parent = TestPart +end + +local Table = {} -- Player = Caster + +local function StartCasting(Player) + local CurrentCaster = Table[Player] + if CurrentCaster.Disabled then + CurrentCaster:Start() + else + CurrentCaster:Stop() + end +end + +Players.PlayerAdded:Connect(function(Player) + Player.CharacterAdded:Connect(function(Character) + local Humanoid = Character:WaitForChild("Humanoid", 1) + + local ClientCaster = ClientCast.new(TestPart, RaycastParams.new()) + ClientCaster:SetOwner(Player) + ClientCaster._Debug = true + + ClientCaster.HumanoidCollided:Connect(function(_, HitHumanoid) + print("Hit model:" .. HitHumanoid.Parent.Name) + end) + + Table[Player] = ClientCaster + + Humanoid.Died:Connect(function() + ClientCaster:Destroy() + Table[Player] = nil + end) + end) +end) + +RemoteEvent.OnServerEvent:Connect(StartCasting) \ No newline at end of file