Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

superman: more initial improvements #577

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

ds1-e
Copy link
Contributor

@ds1-e ds1-e commented Nov 19, 2024

  • Cleanup some unused stuff
  • Fully remove element data (uses custom data system from now on)
  • Introduce some security changes:
    -> localPlayer can now only change (force sync to server) it's own data
    -> Ensure valid dataKey and dataValue, by checking whether they match types (string & boolean), and allowed key names

Copy link
Contributor

@Fernando-A-Rocha Fernando-A-Rocha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! This is much needed.

I've left some comments.

Also, I don't think setPedRotation and getPedRotation should be used (CHandleSuperman.lua) as these functions are deprecated.

[gameplay]/superman/meta.xml Outdated Show resolved Hide resolved
Comment on lines +5 to +11
<script src="config/ShSupermanConfig.lua" type="shared" cache="false"/>
<script src="data/ShData.lua" type="shared" cache="false"/>
<script src="data/CData.lua" type="client" cache="false"/>
<script src="data/SData.lua" type="server"/>

<script src="CHandleSuperman.lua" type="client" cache="false"/>
<script src="SHandleSuperman.lua" type="server"/>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think cache="false" is wanted for public official resources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used, e.g in admin and internetradio

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is (and should) be used in critical scripts to harden security, but I'm not sure this resource qualifies. It would be better to allow caching to reduce bandwidth/download size.

-- Static global variables
local thisResource = getThisResource()
-- Static variables

local serverGravity = getGravity()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable can be named with capital letters as it's static and is not updated in the code, like the settings (it's a constant). This is personal preference / good practice, that I recommend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only use upper case for global variables/tables. This is clear sign that you're dealing with global and not local.

setElementRotation(playerElement, 0, 0, 0)
setElementCollisionsEnabled(playerElement, true)
self.rotations[playerElement] = nil
self.previousVelocity[playerElement] = nil
end

function angleDiff(angle1, angle2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be made local as it's only used in this scriptfile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self:restorePlayer(playerElement)
end
end

-- onEnter: superman cant enter vehicles
-- There's a fix (serverside) for players glitching other players' vehicles by warping into them while superman is active, causing them to flinch into air and get stuck.
function showWarning()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please assure function scopes are as optimized as possible. This one, and other Superman:... functions can be made local, afaik

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really want touch those for now, if anything i wanted to fix it in next PR, to get rid of table approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright sur for a future PR

[gameplay]/superman/CHandleSuperman.lua Show resolved Hide resolved
addEvent("onServerSupermanSetData", true)
addEventHandler("onServerSupermanSetData", root, onServerSupermanSetData)

function onPlayerResourceStartSyncSuperman(startedResource)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This func can be local

Copy link
Contributor Author

@ds1-e ds1-e Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't over localize anything possible, only helper functions which aren't used besides certain .lua file. Do note that Lua has hardcoded limit of 200 local variables and 60 upvalues. But will change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to localize within this script only the functions that u won't use outside of it, staying way below the limit. It's not exaggeration

end
addEventHandler("onPlayerResourceStart", root, onPlayerResourceStartSyncSuperman)

function onPlayerQuitClearSupermanReceiver()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This func can be local

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,11 @@
function onClientSupermanSync(supermanServerData)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These funcs can be local

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gameplay]/superman/config/ShSupermanConfig.lua Outdated Show resolved Hide resolved
@Fernando-A-Rocha
Copy link
Contributor

Mostly good now 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants