Difference between revisions of "LlGenerateKey"

From Second Life Wiki
Jump to: navigation, search
m (Stripping library code)
Line 3: Line 3:
 
|func_desc=Generates a key using Type 3 (MD5) UUID generation to create a unique key using region-name, object-key, service and variable.
 
|func_desc=Generates a key using Type 3 (MD5) UUID generation to create a unique key using region-name, object-key, service and variable.
 
|return_type=key
 
|return_type=key
|return_text=The generated key
+
|Return_text=generated.
 
|p1_type=string|p1_name=service|p1_desc=The service, object, function, or whatever else this key may represent.
 
|p1_type=string|p1_name=service|p1_desc=The service, object, function, or whatever else this key may represent.
 
|p2_type=string|p2_name=variable|p2_desc=Any variable(s) relevant to the service that uniquely distinguish it.
 
|p2_type=string|p2_name=variable|p2_desc=Any variable(s) relevant to the service that uniquely distinguish it.
|mode=user
+
|mode=pre-release
|cat1=Examples
+
|cat2=Library
+
 
|examples=In a two-prim linked-set put the following script (adding generateKey where noted) into the child-prim:<lsl>integer requestID = 0;
 
|examples=In a two-prim linked-set put the following script (adding generateKey where noted) into the child-prim:<lsl>integer requestID = 0;
 
// Add generateKey here!!
 
  
 
default {
 
default {
Line 19: Line 15:
 
             1234,  
 
             1234,  
 
             "I am a request",  
 
             "I am a request",  
             generateKey("echo", (string)requestID++)
+
             llGenerateKey("echo", (string)requestID++)
 
         );
 
         );
 
     }
 
     }
Line 33: Line 29:
 
     }
 
     }
 
}</lsl>Simply touch the child-prim to use, enjoy!
 
}</lsl>Simply touch the child-prim to use, enjoy!
 +
|cat1=Key
 +
|cat2=Encryption
 
}}
 
}}
 
= Implementation =
 
<lsl>key generateKey(string service, string variable) {
 
    return (key)llInsertString(
 
        llInsertString(
 
            llInsertString(
 
                llInsertString(
 
                    llMD5String(
 
                        "secondlife://" + llGetRegionName()  + "/" +
 
                            (string)llGetKey() + "/" +
 
                            (string)llGetLinkNumber() + "/" +
 
                            llGetScriptName() + "/" +
 
                            service + "/" + variable,
 
                        0 // This is reserved by specification, will
 
                          // be increased with new/different versions.
 
                    ),
 
                    8,
 
                    "-"
 
                ),
 
                13,
 
                "-"
 
            ),
 
            18,
 
            "-"
 
        ),
 
        23,
 
        "-"
 
    );
 
}</lsl>
 
 
= Optimisation =
 
Instead of always calling [[llGetRegionName|llGetRegionName()]], [[llGetKey|llGetKey()]], [[llGetLinkNumber|llGetLinkNumber()]], and [[llGetScriptName|llGetScriptName()]], you may wish to cache their return values into a <code>uri</code> variable, and only dynamically add the service and variable parameters each-time. Remember to update this variable when you know it's components will have changed, using the [[changed|changed()]], [[on_rez|on_rez()]], and/or [[attach|attach()]] events.
 
 
Here is an example of the caching version of the function:
 
<lsl>string uri;
 
key generateKey(string service, string variable) {
 
    if (uri == "")
 
        uri =  "secondlife://" + llGetRegionName()  + "/" +
 
            (string)llGetKey() + "/" +
 
            (string)llGetLinkNumber() + "/" +
 
            llGetScriptName() + "/";
 
    return (key)llInsertString(
 
        llInsertString(
 
            llInsertString(
 
                llInsertString(
 
                    llMD5String(
 
                        uri + service + "/" + variable,
 
                        0 // This is reserved by specification, will
 
                          // be increased with new/different versions.
 
                    ),
 
                    8,
 
                    "-"
 
                ),
 
                13,
 
                "-"
 
            ),
 
            18,
 
            "-"
 
        ),
 
        23,
 
        "-"
 
    );
 
}
 
 
integer counter = 0;
 
default {
 
    state_entry() {
 
        llSetTimerEvent(10.0);
 
    }
 
 
    on_rez(integer x) { uri = ""; }
 
    attach(key id) { uri = ""; }
 
    changed(integer changes) {
 
        if (changes & (CHANGED_REGION | CHANGED_INVENTORY)) uri = "";
 
    }
 
 
    timer() {
 
        llOwnerSay("Random key of the moment is \"" + (string)generateKey("counter", (string)(counter++)) + "\"");
 
    }
 
}</lsl>
 
 
{{LSLC|Examples}}
 
[[Category:LSL_Functions]]
 

Revision as of 21:19, 5 April 2012

Emblem-important-red.png Pre-release Documentation Warning!

This function is not available yet. This documentation was written prior to its final release so it may not match the final implementation.

Summary

Function: key llGenerateKey( string service, string variable );

Generates a key using Type 3 (MD5) UUID generation to create a unique key using region-name, object-key, service and variable.
Returns the key generated.

• string service The service, object, function, or whatever else this key may represent.
• string variable Any variable(s) relevant to the service that uniquely distinguish it.

Examples

In a two-prim linked-set put the following script (adding generateKey where noted) into the child-prim:<lsl>integer requestID = 0;

default {

   touch_start(integer x) {
       llMessageLinked(
           LINK_ROOT, 
           1234, 
           "I am a request", 
           llGenerateKey("echo", (string)requestID++)
       );
   }
   link_message(integer x, integer y, string msg, key id) {
       if (y == 1234) 
           llOwnerSay("Request: " + (string)id + " = " + msg);
   }

}</lsl>And the following script in the root-prim:<lsl>default {

   link_message(integer x, integer y, string msg, key id) {
       if (y == 1234) // Echo, send straight back
           llMessageLinked(x, y, msg, id);
   }
}</lsl>Simply touch the child-prim to use, enjoy!

Deep Notes

Search JIRA for related Issues

Signature

function key llGenerateKey( string service, string variable );