Skip to content

EncryptionExample

JabDoesThings edited this page Nov 17, 2024 · 1 revision

Server-Side

local ModLoader = require 'asledgehammer/modloader/ModLoader';
local ZedCrypt = require 'asledgehammer/encryption/ZedCrypt';

if not isServer() then return end

(function()

    local module = 'FoobarExample';
    local info = function(msg)
        print('[' .. module .. '] :: ' .. msg);
    end

    local notFound = function(module, path)
        info(
            'File not installed: Zomboid/Lua/ModLoader/mods/' 
            .. module .. '/' .. path
        );
    end

    local onServerStart = function()

        -- Load the client-side code and cache it as encrypted.
        ModLoader.requestServerFile(

            -- File: '~/Zomboid/Lua/ModLoader/mods/FoobarExample/FoobarExample_Client.lua'
            module,
            'FoobarExample_Client.lua',
            
            -- Cache this result for clients as to not grab the file again.
            -- (If the file is dynamic, set to false)
            true,

            -- The callback function.
            function(module, path, result, data)
                
                -- Handle the RESULT_FILE_NOT_FOUND situation.
                if result == ModLoader.RESULT_FILE_NOT_FOUND then
                    notFound(module, path);
                    return;
                end

            -- Return the file's data as encrypted. This will be the cached result.
            -- You can send this to the client and with the key the client can 
            -- decrypt the file's contents and use them more securely!
            local key = 'FoobarExample_key';

            return ZedCrypt.encrypt(data, key);

        end);

    end

    Events.OnServerStarted.Add(onServerStart);
end)();

Client-Side

local ModLoader = require 'asledgehammer/modloader/ModLoader';
local ZedCrypt = require 'asledgehammer/encryption/ZedCrypt';

if not isClient() then return end

local module = 'FoobarExample';

-- (To keep all prints clean and contextual)
local info = function(msg)
    print('[' .. module .. '] :: ' .. msg);
end

local notFound = function(module, path)
    info(
        'File not installed: Zomboid/Lua/ModLoader/mods/' 
        .. module .. '/' .. path
    );
end

--- @param module string The module folder.
--- @param path string The path in the module to the file.
--- @param result 0 | 1 
        - ModLoader.RESULT_FILE_NOT_FOUND
        - ModLoader.RESULT_SUCCESS
--- @param data string | nil The data retrieved from the server.
local callback = function(module, path, result, data)
    
    -- Handle non-installed / missing result.
    if result == ModLoader.RESULT_FILE_NOT_FOUND then
        info('File not installed on server. Ignoring..');
        return;
    end

    -- Unpackage the code.
    info('Unpacking..');
    local timeThen = getTimeInMillis();
    local decryptedData = ZedCrypt.decrypt(data, 'FoobarExample_key');
    local delta = getTimeInMillis() - timeThen;
    info('Unpacked in ' .. delta .. ' ms.');

    -- Invoke the code.
    loadstring(decryptedData)();

end;

--- @type boolean
--- 
--- If true, the server will cache the file so when
--- called again it'll be ready.
local cache = true; 

-- Request the file:
--   ~/Zomboid/Lua/ModLoader/mods/FoobarExample/Foobar_Client.lua
ModLoader:requestServerFile('FoobarExample', 'Foobar_Client.lua', cache, callback);

Clone this wiki locally