21.08.2019, 10:06
(This post was last modified: 21.08.2019, 10:32 by Quarchodron.)
Hi, script to toturial about addons.
Sample version checker.
Code:
enum VERSION_CHECK {
NO_VERSION, // no registered version of given file
NO_FILE, // player dont have any version of file
NO_FILE_ALLOW_TO_PLAY, // player dont have file with allows him to play
VERSION_OTHER, // player have version other than latest
VERSION_OTHER_ALLOW_TO_PLAY // player have version other than latest allows him to play
LATEST_FILE, // player have latest version of file
}
addEvent("onVersionCheck")
///////////////////////////////////////////////////////////////
class fileVersion
{
versions = [];
//////////////////////////
file = null;
version = null;
md5Hash = null;
allowToPlay = null;
name = null;
constructor(_name, _file, _md5Hash, _version, _allowToPlay)
{
// Version information
name = _name;
file = _file;
md5Hash = _md5Hash;
version = _version;
allowToPlay = _allowToPlay;
// Add to table
local thiz = this;
versions.append(thiz);
}
// get All Files Versions
// # param file - string
static function getFileVersions(file)
{
local arg = [];
foreach(map in versions)
{
if(map.file == file)
{
arg.append(map);
}
}
return arg;
}
// check File
// # param file - string
static function checkFile(file)
{
local fileVersions = getFileVersions(file);
local md5File = fileMd5(file);
if(fileVersions.len() == 0)
{
callEvent("onVersionCheck",VERSION_CHECK.NO_VERSION, null);
return false;
}
/// SINGLE FILE VERSION SOLUTION
if(fileVersions.len() == 1)
{
local fileToCheck = fileVersions[0];
if(md5File == fileToCheck.md5Hash)
{
callEvent("onVersionCheck",VERSION_CHECK.LATEST_FILE, fileToCheck);
return true;
}else{
if(fileToCheck.allowToPlay == true)
{
callEvent("onVersionCheck",VERSION_CHECK.NO_FILE_ALLOW_TO_PLAY, fileToCheck);
return true;
}else{
callEvent("onVersionCheck",VERSION_CHECK.NO_FILE, fileToCheck);
}
}
return false;
}
/// MANY FILES VERSION SOLUTION
local lastVersion = null;
local toEnd = false;
if(md5File == null)
{
callEvent("onVersionCheck",VERSION_CHECK.NO_FILE, null);
return false;
}
foreach(v,k in fileVersions)
{
if(md5File == k.md5Hash)
{
lastVersion = k;
toEnd = true;
}else{
toEnd = false;
}
}
if(lastVersion == null)
{
callEvent("onVersionCheck",VERSION_CHECK.NO_FILE, fileToCheck[fileVersions.len()-1]);
return false;
}
if(toEnd == true)
{
callEvent("onVersionCheck",VERSION_CHECK.LATEST_FILE, lastVersion);
return true;
}else{
if(lastVersion.allowToPlay == true)
{
callEvent("onVersionCheck",VERSION_CHECK.VERSION_OTHER_ALLOW_TO_PLAY, lastVersion);
return true;
}else{
callEvent("onVersionCheck",VERSION_CHECK.VERSION_OTHER, lastVersion);
}
}
return false;
}
// check File Latest Version
// # param file - string
static function getLatestVersionFile(file)
{
local fileVersions = getFileVersions(file);
if(fileVersions.len() == 0)
{
return "";
}
return fileVersions[fileVersions.len()].version;
}
}
local function checkHandlerOnInit()
{
fileVersion("Elisium Addon", "Data/Elisium.vdf", "031bf560aad1145bf2f4ff772e36f624", "0.1", false);
fileVersion.checkFile("Data/Elisium.vdf");
}
addEventHandler("onInit", checkHandlerOnInit)
local function checkVersionHandler(error, version)
{
switch(error)
{
case VERSION_CHECK.NO_VERSION:
print("You check no registered version file!");
break;
case VERSION_CHECK.LATEST_FILE:
print("Congratulations! U Have latest file.");
break;
case VERSION_CHECK.NO_FILE:
print("You dont have file.");
break;
case VERSION_CHECK.NO_FILE_ALLOW_TO_PLAY:
print("You dont have file but you still can play.");
break;
case VERSION_CHECK.VERSION_OTHER:
local latestVersion = fileVersion.getLatestVersionFile(version.file);
print("You dont have latest version. Your version: "+version.version+". Download this one: "+latestVersion.name+" ("+latestVersion.version+")");
break;
case VERSION_CHECK.VERSION_OTHER_ALLOW_TO_PLAY:
local latestVersion = fileVersion.getLatestVersionFile(version.file);
print("You dont have latest version. Your version: "+version.version+". Download this one: "+latestVersion.name+" ("+latestVersion.version+") But you still can play.");
break;
}
}
addEventHandler("onVersionCheck", checkVersionHandler);