Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Client] Version check
#1
 
           
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);
Reply
#2
Just one quick note, you don't have to copy a this reference pointer to push it into array.
Code:
local thiz = this;
versions.append(thiz);

You can always write code this way:
Code:
versions.append(this)

Also the script itself is good, but propably the better idea would be kicking the player from the server with the specified reason.
I know, that this script is more like an example for newbies, but it's worth to mention how this should be done Big Grin.
Anyway, good work.
Reply
#3
Thank you. Ofcourse the example with prints is not so good. But its very specify part for every server.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Client] GUI Creator Quarchodron 6 7,523 12.08.2019, 23:05
Last Post: Arkhan
  [New G2O][Client-Side] bindKey Patrix 6 8,971 14.11.2018, 02:32
Last Post: Patrix
  [0.1.4.*] [client/server] Simple anty-cheat Profesores 8 10,251 20.03.2018, 19:16
Last Post: Patrix
  [0.1.3] [client/server] BotCreator HammelGammel 22 26,572 08.02.2018, 22:02
Last Post: HammelGammel
  [0.1.3] [client/server] Utility Scripts HammelGammel 1 5,255 27.01.2018, 14:03
Last Post: HammelGammel
  [New G2O][Client-Side] Key Combination Patrix 1 3,784 23.12.2017, 13:16
Last Post: Patrix
  [CLIENT]Draw(Line/Color) Tommy 0 3,118 19.12.2017, 04:23
Last Post: Tommy
  [G2O v.0.1.0][Client-Side] Notice Board Quarchodron 0 3,140 27.06.2017, 14:28
Last Post: Quarchodron
  GUI Editor | Version 3.0 Kladvey 3 5,538 18.08.2016, 14:02
Last Post: Yzibz

Forum Jump:


Users browsing this thread: 1 Guest(s)