XmlLoadData: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 13: Line 13:
This function provides an automated way of loading your data from an [[XML]] file created by [[xmlSaveData]] <br>
This function provides an automated way of loading your data from an [[XML]] file created by [[xmlSaveData]] <br>
To call this function, see the [[call]] function or the tip in this resources page: [https://wiki.multitheftauto.com/wiki/XmlData#Simplifying_the_export Simplifying the export]
To call this function, see the [[call]] function or the tip in this resources page: [https://wiki.multitheftauto.com/wiki/XmlData#Simplifying_the_export Simplifying the export]


= Info =
= Info =
'''If you used [[xmlSaveData]] with passing the securityLevel I recommend to pick securityLevel as function argument here as well.'''
'''If you used [[xmlSaveData]] with passing the securityLevel I recommend to pick securityLevel as function argument here as well.'''<br>
'''For proper data loading you need to pass the same booleans/securityLevel as you did on xmlSaveData.'''
'''For proper data loading you need to pass the same booleans/securityLevel as you did on xmlSaveData.'''


= Syntax =
= Syntax =
Line 22: Line 24:
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool xmlLoadData ( fileName [, serverProtected, resourceProtected ] )
bool xmlLoadData ( fileName [, serverProtected, resourceProtected ] )
bool xmlLoadData ( fileName [, int securityLevel] )
bool xmlLoadData ( fileName [, int securityLevel ] )
</syntaxhighlight>
</syntaxhighlight>


Line 33: Line 35:


====Optional Arguments====
====Optional Arguments====
*'''serverProtected:''' If set to ''true'' it can only read the file created by the same server, otherwise it doesn't care which server created it.
*'''serverProtected:''' If set to ''true'' it can only read the file of the creator-server, otherwise it doesn't care which server created it.
*'''resourceProtected:''' If set to ''true'' it can only read the file created by the caller-resource
*'''resourceProtected:''' If set to ''true'' it can only read the file of the creator-resource, otherwise it doesn't care which resource created it.
or
or
*'''securityLevel:''' The level of security on which you want to store your data at. (Details on the resource page: [https://wiki.multitheftauto.com/wiki/XmlData#Security_Levels Security Levels])
*'''securityLevel:''' The level of security on which you want to store your data at. (Details on the resource page: [https://wiki.multitheftauto.com/wiki/XmlData#Security_Levels Security Levels])
Line 41: Line 43:


===Returns===
===Returns===
Returns ''true'' if successful, ''false'' otherwise.
Returns a ''table'' containing the files data, an empty ''table'' if there is no data stored inside the file, ''false'' and debug output in case if failure.


'''Note: There is no encryption parameter in this function, because xmlLoadData will load and use a files key if it is encrypted.'''
'''Note: There is no encryption parameter in this function, because xmlLoadData will load and use a files key if it is encrypted.'''
Line 48: Line 50:
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool xmlSaveData ( string fileName, table data [, bool encryptData = false, bool resourceProtected = false ] )
bool xmlLoadData ( fileName [, resourceProtected ] )
bool xmlLoadData ( fileName [, int securityLevel ] )
</syntaxhighlight>
</syntaxhighlight>


Line 59: Line 62:


====Optional Arguments====
====Optional Arguments====
*'''encryptData:''' If set to ''true'' the script will generate a random key and use it to encrypt your stored data. '''Note: If you want to store "sensitive data", always use encryption! (Account data should never be stored in serverside files - use a database instead!)'''
*'''resourceProtected:''' If set to ''true'' it can only read the file of the creator-resource, otherwise it doesn't care which resource created it.
*'''resourceProtected:''' If set to ''true'' the script will add the sourceResource name (so the name of the resource from which it got called) to the fileName, preventing it from getting read/overwritten/deleted by any other resource. If set to ''false'' you can use this script to call/send/modify tables from different resources without the use of events/other export functions.
or
or
*'''securityLevel:''' The level of security on which you want to store your data at. (Details on the resource page: [https://wiki.multitheftauto.com/wiki/XmlData#Security_Levels Security Levels])
*'''securityLevel:''' The level of security on which you want to store your data at. (Details on the resource page: [https://wiki.multitheftauto.com/wiki/XmlData#Security_Levels Security Levels])
Line 67: Line 69:


===Returns===
===Returns===
Returns ''true'' if successful, ''false'' otherwise.
Returns a ''table'' containing the files data, an empty ''table'' if there is no data stored inside the file, ''false'' and debug output in case if failure.


'''Note: There is no encryption parameter in this function, because xmlLoadData will load and use a files key if it is encrypted.'''
'''Note: There is no encryption parameter in this function, because xmlLoadData will load and use a files key if it is encrypted.'''
Line 75: Line 77:
== Example ==
== Example ==
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
This is the script where we specify the export shortcut and the data table which we want to store.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Lets assume we created a encrypted, serverProtected file called "settings" already.
local xml = exports.xmlData
local xml = exports.xmlData
local tblScriptSettings = {posX = 800, posY = 500, sizeX = 500, sizeY = 300, settings = {show = true, tab = 1}}
local tblSettings = xml:xmlLoadData("settings" true) -- serverProtected is true, resourceProtected false - false doesnt need to get passed here
 
-- OR you can do this:
xml:xmlSaveData("myFileName", tblScriptSettings , true, true) -- Save the data as "myFileName"
local tblSettings = xml:xmlLoadData("settings" 6) -- securityLevel 6 equals serverProtected, encrypted, not resouceProtected - the encryption bool will be ignored at xmlLoadData
</syntaxhighlight>
 
This will create a server-protected, encrypted file on the clients computer called "myFileName.xml". Also it will generate a random generated key and stores it.<br>
The created file will -(could, because encrypted with a random key)- like this:
 
<syntaxhighlight lang="xml">
<root posY="VgEN0+TERhg=" posX="mjX4Tj5u6oM=" sizeX="VgEN0+TERhg=" sizeY="htgOMaZurQQ=">
    <settings show="yeiNs/ne1Ks=" tab="OpijtdPvYqQ="></settings>
</root>
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 18:04, 12 September 2019


[[{{{image}}}|link=|]] Important Note:

This is an unofficial MTA function, so its not included into the game by default.
Its an exported function of the xmlData resource.
To access this function you need to have the resource running on your server.


This function provides an automated way of loading your data from an XML file created by xmlSaveData
To call this function, see the call function or the tip in this resources page: Simplifying the export


Info

If you used xmlSaveData with passing the securityLevel I recommend to pick securityLevel as function argument here as well.
For proper data loading you need to pass the same booleans/securityLevel as you did on xmlSaveData.


Syntax

Click to collapse [-]
Client
bool xmlLoadData ( fileName [, serverProtected, resourceProtected ] )
bool xmlLoadData ( fileName [, int securityLevel ] )
bool xmlSaveData ( string fileName, table data [, int securityLevel = 4] )

Required Arguments

  • fileName: The name of the file you want to create

Optional Arguments

  • serverProtected: If set to true it can only read the file of the creator-server, otherwise it doesn't care which server created it.
  • resourceProtected: If set to true it can only read the file of the creator-resource, otherwise it doesn't care which resource created it.

or

  • securityLevel: The level of security on which you want to store your data at. (Details on the resource page: Security Levels)

See xmlData Variables and Specified names for more detail.

Returns

Returns a table containing the files data, an empty table if there is no data stored inside the file, false and debug output in case if failure.

Note: There is no encryption parameter in this function, because xmlLoadData will load and use a files key if it is encrypted.

Click to collapse [-]
Server
bool xmlLoadData ( fileName [, resourceProtected ] )
bool xmlLoadData ( fileName [, int securityLevel ] )
bool xmlSaveData ( string fileName, table data [, int securityLevel = 4] )

Required Arguments

  • fileName: The name of the file you want to create

Optional Arguments

  • resourceProtected: If set to true it can only read the file of the creator-resource, otherwise it doesn't care which resource created it.

or

  • securityLevel: The level of security on which you want to store your data at. (Details on the resource page: Security Levels)

See xmlData Variables and Specified names for more detail.

Returns

Returns a table containing the files data, an empty table if there is no data stored inside the file, false and debug output in case if failure.

Note: There is no encryption parameter in this function, because xmlLoadData will load and use a files key if it is encrypted.


Example

Click to collapse [-]
Client
-- Lets assume we created a encrypted, serverProtected file called "settings" already.
local xml = exports.xmlData
local tblSettings = xml:xmlLoadData("settings" true) -- serverProtected is true, resourceProtected false - false doesnt need to get passed here
-- OR you can do this:
local tblSettings = xml:xmlLoadData("settings" 6) -- securityLevel 6 equals serverProtected, encrypted, not resouceProtected - the encryption bool will be ignored at xmlLoadData