LZW LSL Examples

From Second Life Wiki
Jump to navigation Jump to search

LZW LSL Examples

Description

The following LSL code snippets show examples of using the LZW engine by Haravikk Mistral. These examples require that you add any relevant functions and constants from the helpers code where noted.

States are used in these scripts for clarity, but are not necessary to the operation of the scripts themselves.

Examples

Compress a message

string myMsg = "Hello world! I am a lovely message waiting to be compressed! Hello world! I am a lovely message waiting to be compressed!";

// Add necessary functions from https://wiki.secondlife.com/wiki/LZW_LSL_Helpers here

default {
    state_entry() { // Send our message
        llResetTime();
        lslLZWCompressBase64ToBase64(
            LINK_THIS,
            llStringToBase64((myMsg = "") + myMsg),
            llGetOwner()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslLZWIsReply(y, id)) return;
        
        y = lslLZWGetReplyMode(y);
        if (y == LSLLZW_COMMAND_ERROR) 
            llOwnerSay("COMPRESS ERROR: "+msg);
        else if (y == LSLLZW_COMMAND_COMPRESS) 
            llOwnerSay("Compressed: "+msg);
    }
}

Decompress a message

string myMsg = "BIBlBsBsBvAgB3BvByBsBkAhAgBJAgBhBtEOAgEDB2EBB5AgBtBlBzBzBhBnBlEFBhBpB0BpBuBnAgB0EEBiEeBjBvBtBwByEZBzBlEKAgEAECEEEGEIEzENEPERETEVEXEwEcEeB3EgEiEkEmEoEqEsEuEwEyAhAA==";

// Add necessary functions from https://wiki.secondlife.com/wiki/LZW_LSL_Helpers here

default {
    state_entry() { // Send our message
        llResetTime();
        lslLZWDecompressBase64ToBase64(
            LINK_THIS,
            (myMsg = "") + myMsg,
            llGetOwner()
        );
    }
 
    link_message(integer x, integer y, string msg, key id) {
        if (!lslLZWIsReply(y, id)) return;
        
        y = lslLZWGetReplyMode(y);
        if (y == LSLLZW_COMMAND_ERROR) 
            llOwnerSay("COMPRESS ERROR: "+msg);
        else if (y == LSLLZW_COMMAND_DECOMPRESS) 
            llOwnerSay("Decompressed: "+llBase64ToString((msg = "") + msg));
    }
}