diff options
author | David Walter Seikel | 2012-02-22 05:11:17 +1000 |
---|---|---|
committer | David Walter Seikel | 2012-02-22 05:11:17 +1000 |
commit | f1083eb3d14bd4177f700dd68ee87c0418cd93f9 (patch) | |
tree | bde3eb18c2a0f3dbb031a7d11180f3fdd8b408e3 /LuaSL/src | |
parent | Create constants.lsl at run time from LSL.lua, and use the same infrastructur... (diff) | |
download | SledjHamr-f1083eb3d14bd4177f700dd68ee87c0418cd93f9.zip SledjHamr-f1083eb3d14bd4177f700dd68ee87c0418cd93f9.tar.gz SledjHamr-f1083eb3d14bd4177f700dd68ee87c0418cd93f9.tar.bz2 SledjHamr-f1083eb3d14bd4177f700dd68ee87c0418cd93f9.tar.xz |
Rejig how the LSL functions are defined. My metatable ideas did not pan out, can't get the function name AND it's arguments at the same time that way.
Diffstat (limited to 'LuaSL/src')
-rw-r--r-- | LuaSL/src/LSL.lua | 306 |
1 files changed, 146 insertions, 160 deletions
diff --git a/LuaSL/src/LSL.lua b/LuaSL/src/LSL.lua index 2f337a5..53b5d72 100644 --- a/LuaSL/src/LSL.lua +++ b/LuaSL/src/LSL.lua | |||
@@ -57,13 +57,74 @@ end | |||
57 | 57 | ||
58 | -- LSL function and constant creation stuff. | 58 | -- LSL function and constant creation stuff. |
59 | 59 | ||
60 | local args2string -- Pre declare this. | ||
61 | local functions = {} | ||
62 | local mt = {} | ||
63 | |||
64 | local function value2string(value, Type) | ||
65 | local temp = "" | ||
66 | |||
67 | if "float" == Type then temp = temp .. value | ||
68 | elseif "integer" == Type then temp = temp .. value | ||
69 | elseif "key" == Type then temp = "\"" .. value .. "\"" | ||
70 | elseif "list" == Type then temp = "[" .. args2string(true, unpack(value)) .. "]" | ||
71 | elseif "table" == Type then temp = "[" .. args2string(true, unpack(value)) .. "]" | ||
72 | elseif "string" == Type then temp = "\"" .. value .. "\"" | ||
73 | elseif "rotation" == Type then temp = "<" .. value.x .. ", " .. value.y .. ", " .. value.z .. ", " .. value.s .. ">" | ||
74 | elseif "vector" == Type then temp = "<" .. value.x .. ", " .. value.y .. ", " .. value.z .. ">" | ||
75 | else | ||
76 | temp = temp .. value | ||
77 | end | ||
78 | return temp | ||
79 | end | ||
80 | |||
81 | function args2string(doType, ...) | ||
82 | local temp = "" | ||
83 | local first = true | ||
84 | |||
85 | for j,w in ipairs( {...} ) do | ||
86 | if first then first = false else temp = temp .. ", " end | ||
87 | if doType then | ||
88 | temp = temp .. value2string(w, type(w)) | ||
89 | else | ||
90 | temp = temp .. w | ||
91 | end | ||
92 | end | ||
93 | return temp | ||
94 | end | ||
95 | |||
96 | function mt.callAndReturn(name, ... ) | ||
97 | print("mt.callAndReturn(" .. name .. "(" .. args2string(true, ...) .. "))") | ||
98 | end | ||
99 | |||
100 | function mt.callAndWait(name, ... ) | ||
101 | local func = functions[name] | ||
102 | |||
103 | print("mt.callAndWait(" .. name .. "(" .. args2string(true, ...) .. "))") | ||
104 | |||
105 | if "float" == func.Type then return 0.0 | ||
106 | elseif "integer" == func.Type then return 0 | ||
107 | elseif "key" == func.Type then return LSL.NULL_KEY | ||
108 | elseif "list" == func.Type then return {} | ||
109 | elseif "string" == func.Type then return "" | ||
110 | elseif "rotation" == func.Type then return LSL.ZERO_ROTATION | ||
111 | elseif "vector" == func.Type then return LSL.ZERO_VECTOR | ||
112 | end | ||
113 | return nil | ||
114 | end | ||
115 | |||
60 | local function newConst(Type, name, value) | 116 | local function newConst(Type, name, value) |
61 | LSL[name] = value | 117 | LSL[name] = value |
62 | return { Type = Type, name = name } | 118 | return { Type = Type, name = name } |
63 | end | 119 | end |
64 | 120 | ||
65 | local function newFunc(Type, ... ) | 121 | local function newFunc(Type, name, ... ) |
66 | return { Type = Type, args = {...} } | 122 | functions[name] = { Type = Type, args = {...} } |
123 | if "" == Type then | ||
124 | LSL[name] = function(...) mt.callAndReturn(name, ... ) end | ||
125 | else | ||
126 | LSL[name] = function(...) return mt.callAndWait(name, ... ) end | ||
127 | end | ||
67 | end | 128 | end |
68 | 129 | ||
69 | 130 | ||
@@ -243,117 +304,114 @@ local constants = | |||
243 | 304 | ||
244 | -- ll*() function definitions | 305 | -- ll*() function definitions |
245 | 306 | ||
246 | local functions = | ||
247 | { | ||
248 | -- LSL avatar functions | 307 | -- LSL avatar functions |
249 | llAvatarOnSitTarget = newFunc("key"), | 308 | newFunc("key", "llAvatarOnSitTarget") |
250 | llGetAnimationList = newFunc("list", "key id"), | 309 | newFunc("list", "llGetAnimationList", "key id") |
251 | llGetPermissions = newFunc("integer"), | 310 | newFunc("integer", "llGetPermissions") |
252 | llGetPermissionsKey = newFunc("key"), | 311 | newFunc("key", "llGetPermissionsKey") |
253 | llKey2Name = newFunc("string", "key avatar"), | 312 | newFunc("string", "llKey2Name", "key avatar") |
254 | llRequestPermissions = newFunc("", "key avatar", "integer perms"), | 313 | newFunc("", "llRequestPermissions", "key avatar", "integer perms") |
255 | llSameGroup = newFunc("integer", "key avatar"), | 314 | newFunc("integer", "llSameGroup", "key avatar") |
256 | llStartAnimation = newFunc("", "string anim"), | 315 | newFunc("", "llStartAnimation", "string anim") |
257 | llStopAnimation = newFunc("", "string anim"), | 316 | newFunc("", "llStopAnimation", "string anim") |
258 | llUnSit = newFunc("", "key avatar"), | 317 | newFunc("", "llUnSit", "key avatar") |
259 | 318 | ||
260 | -- LSL collision / detect / sensor functions | 319 | -- LSL collision / detect / sensor functions |
261 | llDetectedGroup = newFunc("key", "integer index"), | 320 | newFunc("key", "llDetectedGroup", "integer index") |
262 | llDetectedKey = newFunc("key", "integer index"), | 321 | newFunc("key", "llDetectedKey", "integer index") |
263 | 322 | ||
264 | -- LSL communications functions | 323 | -- LSL communications functions |
265 | llDialog = newFunc("", "key avatar", "string caption", "list arseBackwardsMenu", "integer channel"), | 324 | newFunc("", "llDialog", "key avatar", "string caption", "list arseBackwardsMenu", "integer channel") |
266 | llListen = newFunc("integer", "integer channel", "string name", "key id", "string msg"), | 325 | newFunc("integer", "llListen", "integer channel", "string name", "key id", "string msg") |
267 | llListenRemove = newFunc("", "integer handle"), | 326 | newFunc("", "llListenRemove", "integer handle") |
268 | llOwnerSay = newFunc("", "string text"), | 327 | newFunc("", "llOwnerSay", "string text") |
269 | llSay = newFunc("", "integer channel", "string text"), | 328 | newFunc("", "llSay", "integer channel", "string text") |
270 | llShout = newFunc("", "integer channel", "string text"), | 329 | newFunc("", "llShout", "integer channel", "string text") |
271 | llWhisper = newFunc("", "integer channel", "string text"), | 330 | newFunc("", "llWhisper", "integer channel", "string text") |
272 | llMessageLinked = newFunc("", "integer link", "integer num", "string text", "key aKey"), | 331 | newFunc("", "llMessageLinked", "integer link", "integer num", "string text", "key aKey") |
273 | 332 | ||
274 | -- LSL inventory functions. | 333 | -- LSL inventory functions. |
275 | llGetInventoryName = newFunc("string", "integer Type", "integer index"), | 334 | newFunc("string", "llGetInventoryName", "integer Type", "integer index") |
276 | llGetInventoryNumber = newFunc("integer", "integer Type"), | 335 | newFunc("integer", "llGetInventoryNumber", "integer Type") |
277 | llGetInventoryType = newFunc("integer", "string name"), | 336 | newFunc("integer", "llGetInventoryType", "string name") |
278 | llGetNotecardLine = newFunc("key", "string name", "integer index"), | 337 | newFunc("key", "llGetNotecardLine", "string name", "integer index") |
279 | llRezAtRoot = newFunc("", "string name", "vector position", "vector velocity", "rotation rot", "integer channel"), | 338 | newFunc("", "llRezAtRoot", "string name", "vector position", "vector velocity", "rotation rot", "integer channel") |
280 | llRezObject = newFunc("", "string name", "vector position", "vector velocity", "rotation rot", "integer channel"), | 339 | newFunc("", "llRezObject", "string name", "vector position", "vector velocity", "rotation rot", "integer channel") |
281 | 340 | ||
282 | -- LSL list functions. | 341 | -- LSL list functions. |
283 | llCSV2List = newFunc("list", "string text"), | 342 | newFunc("list", "llCSV2List", "string text") |
284 | llDeleteSubList = newFunc("list", "list l", "integer start", "integer End"), | 343 | newFunc("list", "llDeleteSubList", "list l", "integer start", "integer End") |
285 | llDumpList2String = newFunc("string", "list l", "string separator"), | 344 | newFunc("string", "llDumpList2String", "list l", "string separator") |
286 | llGetListLength = newFunc("integer", "list l"), | 345 | newFunc("integer", "llGetListLength", "list l") |
287 | llList2CSV = newFunc("string", "list l"), | 346 | newFunc("string", "llList2CSV", "list l") |
288 | llList2Float = newFunc("float", "list l", "integer index"), | 347 | newFunc("float", "llList2Float", "list l", "integer index") |
289 | llList2Integer = newFunc("integer", "list l", "integer index"), | 348 | newFunc("integer", "llList2Integer", "list l", "integer index") |
290 | llList2Key = newFunc("key", "list l", "integer index"), | 349 | newFunc("key", "llList2Key", "list l", "integer index") |
291 | llList2List = newFunc("list", "list l", "integer start", "integer End"), | 350 | newFunc("list", "llList2List", "list l", "integer start", "integer End") |
292 | llList2String = newFunc("string", "list l", "integer index"), | 351 | newFunc("string", "llList2String", "list l", "integer index") |
293 | llList2Rotation = newFunc("rotation", "list l", "integer index"), | 352 | newFunc("rotation", "llList2Rotation", "list l", "integer index") |
294 | llList2Vector = newFunc("vector", "list l", "integer index"), | 353 | newFunc("vector", "llList2Vector", "list l", "integer index") |
295 | llListFindList = newFunc("integer", "list l", "list l1"), | 354 | newFunc("integer", "llListFindList", "list l", "list l1") |
296 | llListInsertList = newFunc("list", "list l", "list l1", "integer index"), | 355 | newFunc("list", "llListInsertList", "list l", "list l1", "integer index") |
297 | llListReplaceList = newFunc("list", "list l", "list part", "integer start", "integer End"), | 356 | newFunc("list", "llListReplaceList", "list l", "list part", "integer start", "integer End") |
298 | llListSort = newFunc("list", "list l", "integer stride", "integer ascending"), | 357 | newFunc("list", "llListSort", "list l", "integer stride", "integer ascending") |
299 | llParseString2List = newFunc("list", "string In", "list l", "list l1"), | 358 | newFunc("list", "llParseString2List", "string In", "list l", "list l1") |
300 | llParseStringKeepNulls = newFunc("list", "string In", "list l", "list l1"), | 359 | newFunc("list", "llParseStringKeepNulls", "string In", "list l", "list l1") |
301 | 360 | ||
302 | -- LSL math functions | 361 | -- LSL math functions |
303 | llEuler2Rot = newFunc("rotation", "vector vec"), | 362 | newFunc("rotation", "llEuler2Rot", "vector vec") |
304 | llFrand = newFunc("float", "float max"), | 363 | newFunc("float", "llFrand", "float max") |
305 | llPow = newFunc("float", "float number", "float places"), | 364 | newFunc("float", "llPow", "float number", "float places") |
306 | llRot2Euler = newFunc("vector", "rotation rot"), | 365 | newFunc("vector", "llRot2Euler", "rotation rot") |
307 | llRound = newFunc("integer", "float number"), | 366 | newFunc("integer", "llRound", "float number") |
308 | 367 | ||
309 | -- LSL media functions | 368 | -- LSL media functions |
310 | llPlaySound = newFunc("", "string name", "float volume"), | 369 | newFunc("", "llPlaySound", "string name", "float volume") |
311 | 370 | ||
312 | -- LSL object / prim functions | 371 | -- LSL object / prim functions |
313 | llDie = newFunc(""), | 372 | newFunc("", "llDie") |
314 | llGetKey = newFunc("key"), | 373 | newFunc("key", "llGetKey") |
315 | llGetLinkNumber = newFunc("integer"), | 374 | newFunc("integer", "llGetLinkNumber") |
316 | llGetObjectDesc = newFunc("string"), | 375 | newFunc("string", "llGetObjectDesc") |
317 | llGetObjectName = newFunc("string"), | 376 | newFunc("string", "llGetObjectName") |
318 | llGetOwner = newFunc("key"), | 377 | newFunc("key", "llGetOwner") |
319 | llSetObjectDesc = newFunc("", "string text"), | 378 | newFunc("", "llSetObjectDesc", "string text") |
320 | llSetObjectName = newFunc("", "string text"), | 379 | newFunc("", "llSetObjectName", "string text") |
321 | llSetPrimitiveParams = newFunc("", "list params"), | 380 | newFunc("", "llSetPrimitiveParams", "list params") |
322 | llSetSitText = newFunc("", "string text"), | 381 | newFunc("", "llSetSitText", "string text") |
323 | llSetText = newFunc("", "string text", "vector colour", "float alpha"), | 382 | newFunc("", "llSetText", "string text", "vector colour", "float alpha") |
324 | llSitTarget = newFunc("", "vector pos", "rotation rot"), | 383 | newFunc("", "llSitTarget", "vector pos", "rotation rot") |
325 | 384 | ||
326 | -- LSL rotation / scaling / translation functions | 385 | -- LSL rotation / scaling / translation functions |
327 | llGetPos = newFunc("vector", ""), | 386 | newFunc("vector", "llGetPos") |
328 | llGetRot = newFunc("rotation", ""), | 387 | newFunc("rotation", "llGetRot") |
329 | llSetPos = newFunc("", "vector pos"), | 388 | newFunc("", "llSetPos", "vector pos") |
330 | llSetRot = newFunc("", "rotation rot"), | 389 | newFunc("", "llSetRot", "rotation rot") |
331 | llSetScale = newFunc("", "vector scale"), | 390 | newFunc("", "llSetScale", "vector scale") |
332 | 391 | ||
333 | -- LSL script functions | 392 | -- LSL script functions |
334 | llGetFreeMemory = newFunc("integer", ""), | 393 | newFunc("integer", "llGetFreeMemory") |
335 | llGetScriptName = newFunc("string", ""), | 394 | newFunc("string", "llGetScriptName") |
336 | llResetOtherScript = newFunc("", "string name"), | 395 | newFunc("", "llResetOtherScript", "string name") |
337 | llResetScript = newFunc("", ""), | 396 | newFunc("", "llResetScript") |
338 | llSetScriptState = newFunc("", "string name", "integer running"), | 397 | newFunc("", "llSetScriptState", "string name", "integer running") |
339 | 398 | ||
340 | -- LSL string functions | 399 | -- LSL string functions |
341 | llGetSubString = newFunc("string", "string text", "integer start", "integer End"), | 400 | newFunc("string", "llGetSubString", "string text", "integer start", "integer End") |
342 | llStringLength = newFunc("integer", "string text"), | 401 | newFunc("integer", "llStringLength", "string text") |
343 | llStringTrim = newFunc("string", "string text", "integer type"), | 402 | newFunc("string", "llStringTrim", "string text", "integer type") |
344 | llSubStringIndex = newFunc("integer", "string text", "string sub"), | 403 | newFunc("integer", "llSubStringIndex", "string text", "string sub") |
345 | 404 | ||
346 | -- LSL texture functions | 405 | -- LSL texture functions |
347 | llGetAlpha = newFunc("float", "integer side"), | 406 | newFunc("float", "llGetAlpha", "integer side") |
348 | llSetAlpha = newFunc("", "float alpha", "integer side"), | 407 | newFunc("", "llSetAlpha", "float alpha", "integer side") |
349 | llSetColor = newFunc("", "vector colour", "integer side"), | 408 | newFunc("", "llSetColor", "vector colour", "integer side") |
350 | 409 | ||
351 | -- LSL time functions | 410 | -- LSL time functions |
352 | llGetTime = newFunc("float", ""), | 411 | newFunc("float", "llGetTime") |
353 | llResetTime = newFunc("", ""), | 412 | newFunc("", "llResetTime") |
354 | llSetTimerEvent = newFunc("", "float seconds"), | 413 | newFunc("", "llSetTimerEvent", "float seconds") |
355 | llSleep = newFunc("", "float seconds"), | 414 | newFunc("", "llSleep", "float seconds") |
356 | } | ||
357 | 415 | ||
358 | 416 | ||
359 | -- TODO - fake this for now. | 417 | -- TODO - fake this for now. |
@@ -636,43 +694,6 @@ function LSL.vectorTypecast(x) | |||
636 | end | 694 | end |
637 | 695 | ||
638 | 696 | ||
639 | -- glue stuff | ||
640 | |||
641 | local args2string -- Pre declare this. | ||
642 | local mt = {} | ||
643 | |||
644 | local function value2string(value, Type) | ||
645 | local temp = "" | ||
646 | |||
647 | if "float" == Type then temp = temp .. value | ||
648 | elseif "integer" == Type then temp = temp .. value | ||
649 | elseif "key" == Type then temp = "\"" .. value .. "\"" | ||
650 | elseif "list" == Type then temp = "[" .. args2string(true, unpack(value)) .. "]" | ||
651 | elseif "table" == Type then temp = "[" .. args2string(true, unpack(value)) .. "]" | ||
652 | elseif "string" == Type then temp = "\"" .. value .. "\"" | ||
653 | elseif "rotation" == Type then temp = "<" .. value.x .. ", " .. value.y .. ", " .. value.z .. ", " .. value.s .. ">" | ||
654 | elseif "vector" == Type then temp = "<" .. value.x .. ", " .. value.y .. ", " .. value.z .. ">" | ||
655 | else | ||
656 | temp = temp .. value | ||
657 | end | ||
658 | return temp | ||
659 | end | ||
660 | |||
661 | function args2string(doType, ...) | ||
662 | local temp = "" | ||
663 | local first = true | ||
664 | |||
665 | for j,w in ipairs( {...} ) do | ||
666 | if first then first = false else temp = temp .. ", " end | ||
667 | if doType then | ||
668 | temp = temp .. value2string(w, type(w)) | ||
669 | else | ||
670 | temp = temp .. w | ||
671 | end | ||
672 | end | ||
673 | return temp | ||
674 | end | ||
675 | |||
676 | function LSL.gimmeLSL() | 697 | function LSL.gimmeLSL() |
677 | for i,v in ipairs(constants) do | 698 | for i,v in ipairs(constants) do |
678 | local value = LSL[v.name] | 699 | local value = LSL[v.name] |
@@ -691,40 +712,5 @@ function LSL.gimmeLSL() | |||
691 | LSL.EOF = "\n\n\n" -- Fix this up now. | 712 | LSL.EOF = "\n\n\n" -- Fix this up now. |
692 | end | 713 | end |
693 | 714 | ||
694 | function mt.callAndReturn( ... ) | ||
695 | print("mt.callAndReturn(" .. mt.name .. "(" .. args2string(true, ...) .. "))") | ||
696 | end | ||
697 | |||
698 | function mt.callAndWait( ... ) | ||
699 | local func = functions[mt.name] | ||
700 | |||
701 | print("mt.callAndWait(" .. mt.name .. "(" .. args2string(true, ...) .. "))") | ||
702 | |||
703 | if "float" == func.Type then return 0.0 | ||
704 | elseif "integer" == func.Type then return 0 | ||
705 | elseif "key" == func.Type then return LSL.NULL_KEY | ||
706 | elseif "list" == func.Type then return {} | ||
707 | elseif "string" == func.Type then return "" | ||
708 | elseif "rotation" == func.Type then return LSL.ZERO_ROTATION | ||
709 | elseif "vector" == func.Type then return LSL.ZERO_VECTOR | ||
710 | end | ||
711 | return nil | ||
712 | end | ||
713 | |||
714 | function mt.__index(Table, key) | ||
715 | local func = functions[key] | ||
716 | |||
717 | -- This is hacky, but we should only be running one at a time. | ||
718 | mt.name = key | ||
719 | |||
720 | if "" == func.Type then | ||
721 | return mt.callAndReturn | ||
722 | else | ||
723 | return mt.callAndWait | ||
724 | end | ||
725 | end | ||
726 | |||
727 | setmetatable(LSL, mt) | ||
728 | |||
729 | 715 | ||
730 | return LSL; | 716 | return LSL; |