Difference between revisions of "LZW LSL Examples"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
Line 9: Line 9:
== Examples ==
== Examples ==
=== Compress a message ===
=== Compress a message ===
<lsl>string myMsg = "Hello world! I am a lovely message waiting to be compressed! Hello world! I am a lovely message waiting to be compressed!";
<source lang="lsl2">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
// Add necessary functions from https://wiki.secondlife.com/wiki/LZW_LSL_Helpers here
Line 32: Line 32:
             llOwnerSay("Compressed: "+msg);
             llOwnerSay("Compressed: "+msg);
     }
     }
}</lsl>
}</source>


=== Decompress a message ===
=== Decompress a message ===
<lsl>string myMsg = "BIBlBsBsBvAgB3BvByBsBkAhAgBJAgBhBtEOAgEDB2EBB5AgBtBlBzBzBhBnBlEFBhBpB0BpBuBnAgB0EEBiEeBjBvBtBwByEZBzBlEKAgEAECEEEGEIEzENEPERETEVEXEwEcEeB3EgEiEkEmEoEqEsEuEwEyAhAA==";
<source lang="lsl2">string myMsg = "BIBlBsBsBvAgB3BvByBsBkAhAgBJAgBhBtEOAgEDB2EBB5AgBtBlBzBzBhBnBlEFBhBpB0BpBuBnAgB0EEBiEeBjBvBtBwByEZBzBlEKAgEAECEEEGEIEzENEPERETEVEXEwEcEeB3EgEiEkEmEoEqEsEuEwEyAhAA==";


// Add necessary functions from https://wiki.secondlife.com/wiki/LZW_LSL_Helpers here
// Add necessary functions from https://wiki.secondlife.com/wiki/LZW_LSL_Helpers here
Line 58: Line 58:
             llOwnerSay("Decompressed: "+llBase64ToString((msg = "") + msg));
             llOwnerSay("Decompressed: "+llBase64ToString((msg = "") + msg));
     }
     }
}</lsl>
}</source>

Latest revision as of 15:21, 11 July 2015

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