Gothic Online Forums
[Client] Version check - Printable Version

+- Gothic Online Forums (https://archive.gothic-online.com)
+-- Forum: Scripting (English) (https://archive.gothic-online.com/forum-11.html)
+--- Forum: Resources (https://archive.gothic-online.com/forum-14.html)
+---- Forum: Scripts (https://archive.gothic-online.com/forum-17.html)
+---- Thread: [Client] Version check (/thread-2669.html)



[Client] Version check - Quarchodron - 21.08.2019

 
           
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);



RE: [Client] Version check - Patrix - 25.08.2019

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.


RE: [Client] Version check - Quarchodron - 25.08.2019

Thank you. Ofcourse the example with prints is not so good. But its very specify part for every server.