forked from AlterGamingNetwork/mellotrainer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsv_data_saving.lua
251 lines (188 loc) · 6.51 KB
/
sv_data_saving.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
--[[--------------------------------------------------------------------------
*
* Mello Trainer
* (C) Michael Goodwin 2017
* http://github.com/thestonedturtle/mellotrainer/releases
*
* This menu used the Scorpion Trainer as a framework to build off of.
* https://github.com/pongo1231/ScorpionTrainer
* (C) Emre Cürgül 2017
*
* A lot of useful functionality has been converted from the lambda menu.
* https://lambda.menu
* (C) Oui 2017
*
* Additional Contributors:
* WolfKnight (https://forum.fivem.net/u/WolfKnight)
*
---------------------------------------------------------------------------]]
DATASAVE = {}
DATASAVE.dir = "mtsaves/"
function DATASAVE:DoesPathExist( path )
if ( type( path ) ~= "string" ) then return false end
local response = os.execute( "cd " .. path )
if ( response == true ) then
return true
end
return false
end
function DATASAVE:RunLaunchChecks()
local exists = self:DoesPathExist( "mtsaves" )
if ( not exists ) then
self:print( "mtsaves folder not found, attempting to create." )
os.execute( "mkdir mtsaves" )
else
self:print( "mtsaves folder found!" )
end
end
function DATASAVE:GetIdentifier( source, identifier )
local ids = GetPlayerIdentifiers( source )
for k, v in pairs( ids ) do
local id = stringsplit( v, ":" )
local start = id[1]
if ( start == identifier ) then
return id[2]
end
end
return nil
end
function DATASAVE:DoesFileExist( name )
local dir = self.dir .. name
local file = io.open( dir, "r" )
if ( file ~= nil ) then
io.close( file )
return true
else
return false
end
end
function DATASAVE:CreateFile( name )
local dir = self.dir .. name
local file, err = io.open( dir, 'w' )
if ( not file ) then self:print( err ) end
file:write( "{}" )
file:close()
end
function DATASAVE:LoadFile( name )
local dir = self.dir .. name
local file, err = io.open( dir, 'rb' )
if ( not file ) then self:print( err ) return nil end
local contents = file:read( "*all" )
contents = json.decode( contents )
file:close()
return contents
end
function DATASAVE:WriteToFile( name, data, index )
local dir = self.dir .. name
local fileTable = self:LoadFile( name )
if ( not fileTable ) then return end
if ( data == nil ) then
table.remove( fileTable, index ) -- table.remove reindexes all values after removing
else
fileTable[index] = data
end
local fileString = json.encode( fileTable )
local file, err = io.open( dir, 'w+' )
if ( not file ) then self:print( err ) return end
file:write( fileString )
file:close()
end
function DATASAVE:SendSavedData( file, type, source )
local event = "wk:RecieveSaved" .. string.gsub( type, "^%l", string.upper )
local data = self:LoadFile( file )
if ( next( data ) ~= nil ) then
TriggerClientEvent( event, source, data )
end
end
function DATASAVE:SendSaveData( source )
local id = self:GetIdentifier( source, "steam" )
if ( id ~= nil ) then
local vehicleData = {}
local skinData = {}
local vehFileName = id .. "_vehicles.txt"
local skinFileName = id .. "_skins.txt"
local trainerFileName = id .. "_toggles.txt"
vehicleData = self:LoadFile( vehFileName )
skinData = self:LoadFile( skinFileName )
trainerToggles = self:LoadFile( trainerFileName )
if ( next( vehicleData ) ~= nil ) then
TriggerClientEvent( 'wk:RecieveSavedVehicles', source, vehicleData )
end
if ( next( skinData ) ~= nil ) then
TriggerClientEvent( 'wk:RecieveSavedSkins', source, skinData )
end
if ( next( trainerToggles ) ~= nil ) then
TriggerClientEvent( 'wk:RecieveSavedToggles', source, trainerToggles)
end
else
self:print( "Attempted to load save data for " .. GetPlayerName( source ) .. ", but does not have a steam id." )
end
end
function DATASAVE:print( text )
RconPrint( "MELLOTRAINER: " .. text .. "\n" )
end
Citizen.CreateThread( function()
Citizen.Wait( 1000 ) -- just to reduce clutter in the console on startup
if ( Config.settings.localSaving ) then
DATASAVE:RunLaunchChecks()
else
DATASAVE:print( "Local Saving is currently turned off." )
end
end )
function DATASAVE:AddPlayerToDataSave( source )
local id = self:GetIdentifier( source, "steam" )
if ( id ~= nil ) then
local fileNames = {}
fileNames.vehicles = id .. "_vehicles.txt"
fileNames.skins = id .. "_skins.txt"
fileNames.toggles = id .. "_toggles.txt"
for key, filename in pairs( fileNames ) do
if ( self:DoesFileExist( filename ) )then
self:SendSavedData( filename, key, source )
else
self:print( "Creating " .. key .. " save file for " .. GetPlayerName( source) )
self:CreateFile( filename )
end
end
else
self:print( GetPlayerName( source ) .. " is not connecting with a steam id.\nPlayer will not have the ability to save/load." )
end
end
RegisterServerEvent( 'wk:DataSave' )
AddEventHandler( 'wk:DataSave', function( type, data, index )
if ( Config.settings.localSaving ) then
local id = DATASAVE:GetIdentifier( source, "steam" )
if ( id ~= nil ) then
local file = id .. "_" .. type .. ".txt"
DATASAVE:WriteToFile( file, data, index )
else
DATASAVE:print( GetPlayerName( source ) .. " attempted to save, but does not have a steam id." )
end
end
end )
RegisterServerEvent( 'wk:DataLoad' )
AddEventHandler( 'wk:DataLoad', function( type )
if ( Config.settings.localSaving ) then
local id = DATASAVE:GetIdentifier( source, "steam" )
if ( id ~= nil ) then
local file = id .. "_" .. type .. ".txt"
if ( DATASAVE:DoesFileExist( file ) ) then
DATASAVE:SendSavedData( file, type, source )
end
end
end
end )
function startsWith( string, start )
return string.sub( string, 1, string.len( start ) ) == start
end
function stringsplit( inputstr, sep )
if sep == nil then
sep = "%s"
end
local t = {} ; i = 1
for str in string.gmatch( inputstr, "([^" .. sep .. "]+)" ) do
t[i] = str
i = i + 1
end
return t
end