From f678359fe652be8d4b0daebd284b49d5743fe3ce Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 11 Mar 2020 19:52:05 +0000 Subject: [PATCH] Switched to local saving for UI data --- README.md | 10 --- cl_radar.lua | 27 ++++---- fxmanifest.lua | 1 - sv_saving.lua | 183 ------------------------------------------------- 4 files changed, 13 insertions(+), 208 deletions(-) delete mode 100644 sv_saving.lua diff --git a/README.md b/README.md index 4092e0e..3e5e9b1 100644 --- a/README.md +++ b/README.md @@ -22,16 +22,6 @@ Although these can be viewed ingame through the operator manual, the default key | Toggle keylock | L | Toggles the keylock state. When enabled, none of the keybinds will work until keylock is toggled again. | | Toggle keybind set | K | Toggles between the full and small keybind sets for locking/unlocking the radar/plate reader. | -## Changing the UI file save type -As the UI can be moved and scaled, the system also saves the UI data as it is set by the user. By default, this identifier type uses `license` to name the JSON files saved in the `saves` folder. This can easily be changed by opening the `sv_saving.lua` file, then looking for the following line of code at the top: -```lua -DATASAVE.idType = "license" -``` -All you need to do is change the value, so for example, if I wanted the save files to be saved using Steam IDs, I would change the code to look like this: -```lua -DATASAVE.idType = "steam" -``` - ## Script configuration All of the configuration for the Wraith ARS 2X is done inside the `config.lua` file, below is a copy of the configuration file. All of the options have comments to describe what they do, along with the available options you can set. You have the ability to change the key binds for the large and small key set, the default operator menu options, and the default UI element scale and safezone. ```lua diff --git a/cl_radar.lua b/cl_radar.lua index 707949a..8148c78 100644 --- a/cl_radar.lua +++ b/cl_radar.lua @@ -64,23 +64,22 @@ local spawned = false -- the player spawns AddEventHandler( "playerSpawned", function() if ( not spawned ) then - -- Ask the server to get the player's saved UI data - TriggerServerEvent( "wk:getUiData" ) + -- Try and get the saved UI data + local uiData = GetResourceKvpString( "wk_wars2x_ui_data" ) + + -- If the data exists, then we send it off! + if ( uiData ~= nil ) then + SendNUIMessage( { _type = "loadUiSettings", data = json.decode( uiData ) } ) + + -- If the data doesn't exist, then we send the defaults + else + SendNUIMessage( { _type = "setUiDefaults", data = CONFIG.uiDefaults } ) + end + spawned = true end end ) --- Grabs the saved UI data sent by the server and forwards it to the NUI side -RegisterNetEvent( "wk:loadUiData" ) -AddEventHandler( "wk:loadUiData", function( data ) - SendNUIMessage( { _type = "loadUiSettings", data = data } ) -end ) - -RegisterNetEvent( "wk:setUiDefaults" ) -AddEventHandler( "wk:setUiDefaults", function() - SendNUIMessage( { _type = "setUiDefaults", data = CONFIG.uiDefaults } ) -end ) - --[[---------------------------------------------------------------------------------- Player info variables @@ -1433,7 +1432,7 @@ end ) -- Runs when the JavaScript side sends the UI data for saving RegisterNUICallback( "saveUiData", function( data, cb ) - TriggerServerEvent( "wk:saveUiData", data ) + SetResourceKvp( "wk_wars2x_ui_data", json.encode( data ) ) end ) diff --git a/fxmanifest.lua b/fxmanifest.lua index fb189b8..d3f9ab1 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -58,7 +58,6 @@ ui_page "nui/radar.html" -- Run the server scripts server_script "sv_version_check.lua" -server_script "sv_saving.lua" server_script "sv_exports.lua" server_export "TogglePlateLock" diff --git a/sv_saving.lua b/sv_saving.lua deleted file mode 100644 index 0cb3ee1..0000000 --- a/sv_saving.lua +++ /dev/null @@ -1,183 +0,0 @@ ---[[--------------------------------------------------------------------------------------- - - Wraith ARS 2X - Created by WolfKnight - - For discussions, information on future updates, and more, join - my Discord: https://discord.gg/fD4e6WD - - MIT License - - Copyright (c) 2020 WolfKnight - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. - ----------------------------------------------------------------------------------------]]-- - -local DATASAVE = {} -DATASAVE.dir = "saves" - --- Change this to whatever ID type you want to use for saving user data --- Options are: --- - steam --- - license --- - xbl --- - live --- - discord --- - fivem --- - ip -DATASAVE.idType = "license" - --- Whether or not to print messages -DATASAVE.printMessages = true - --- Saves the data for the given player into the saves folder within the resource -function DATASAVE:SavePlayerData( src, data ) - -- Get the player's identifier - local id = self:GetIdentifier( src ) - - -- Save the JSON file into the saves folder - SaveResourceFile( GetCurrentResourceName(), self.dir .. "/" .. id .. ".json", json.encode( data ), -1 ) - - -- Print out a message in the console to say the player's UI data has been saved - self:Print( "Saved UI data for " .. GetPlayerName( src ) .. " (ID: " .. src .. ")" ) -end - --- Attempts to retrieve the UI data for the given player -function DATASAVE:GetPlayerData( src ) - -- Get the player's identifier - local id = self:GetIdentifier( src ) - - -- Try to grab the raw data from the player's JSON file - local rawData = LoadResourceFile( GetCurrentResourceName(), self.dir .. "/" .. id .. ".json" ) - - -- In the event there is no file for the player, return nil - if ( rawData == nil ) then - return nil - end - - -- Decode the JSON data into a Lua table - local data = json.decode( rawData ) - - -- Return the data - return data -end - --- Checks that the given data is valid, helps to stop modified data from being sent through the save system -function DATASAVE:CheckDataIsValid( data ) - -- First we check to make sure the data being passed is actually a table - if ( type( data ) ~= "table" ) then return false end - - -- Then we check to make sure that the data has only 3 elements, "remote", "radar", "reader" and "safezone" - local c = 0 - for _ in pairs( data ) do c = c + 1 end - - -- If there isn't 4 elements, then the data isn't valid - if ( c ~= 4 ) then return false end - - return true -end - --- Gets the identifier for the given player based on the identifier type specified at the top -function DATASAVE:GetIdentifier( src ) - -- Get the number of identifiers the player has - local max = GetNumPlayerIdentifiers( src ) - - -- Iterate through the identifier numerical range - for i = 0, max do - -- Get the current identifier - local id = GetPlayerIdentifier( src, i ) - - -- In the event the identifier is nil, report it to the server console and return nil - if ( id == nil ) then - self:Print( "^1It appears there was an error trying to find the specified ID (" .. self.idType .. ") for player " .. GetPlayerName( source ) ) - return nil - end - - -- If we find the identifier type set in DATASAVE.idType, then we have the identifier - if ( string.find( id, self.idType, 1 ) ) then - -- Split the identifier so we just get the actual identifier - local split = self:SplitString( id, ":" ) - - -- Return the identifier - return split[2] - end - end - - -- In the event we get nothing, return nil - return nil -end - --- Your typical split string function! -function DATASAVE:SplitString( inputstr, sep ) - if ( sep == nil ) then - sep = "%s" - end - - local t = {} - local i = 1 - - for str in string.gmatch( inputstr, "([^" .. sep .. "]+)" ) do - t[i] = str - i = i + 1 - end - - return t -end - --- Prints the given message with the resource name attached -function DATASAVE:Print( msg ) - if ( self.printMessages ) then - print( "^3[wk_wars2x] ^0" .. msg .. "^0" ) - end -end - --- Serverside event for saving a player's UI data -RegisterServerEvent( "wk:saveUiData" ) -AddEventHandler( "wk:saveUiData", function( data ) - -- Check to make sure that the data being sent by the client is valid - local valid = DATASAVE:CheckDataIsValid( data ) - - -- Only proceed if the data is actually valid - if ( valid ) then - DATASAVE:SavePlayerData( source, data ) - else - -- Print a message to the console saying the data sent by the client is not valid, this should rarely occur but it could be because - -- the client has modified the data being sent (mods/injections) - DATASAVE:Print( "^1Save data being sent from " .. GetPlayerName( source ) .. " (ID: " .. source .. ") is not valid, either something went wrong, or the player has modified the data being sent." ) - end -end ) - --- Serverside event for when the player loads in and the system needs to get their saved UI data -RegisterServerEvent( "wk:getUiData" ) -AddEventHandler( "wk:getUiData", function() - -- Try and get the player's data - local data = DATASAVE:GetPlayerData( source ) - - -- Send the client the data if we get any - if ( data ) then - TriggerClientEvent( "wk:loadUiData", source, data ) - else - -- When player's first use the radar, they won't have saved UI data - DATASAVE:Print( "Player " .. GetPlayerName( source ) .. " (ID: " .. source .. ") doesn't have a UI settings file." ) - - -- Tell the system to load the UI defaults for the client - TriggerClientEvent( "wk:setUiDefaults", source ) - end -end ) \ No newline at end of file