--######################################################################################################### -- IRLUA PLUGIN HELPER FUNCTIONS By MicroByte (Revised by Centauri Soldier) ||@#$|| VERSION 2.4 ||$#@|| --######################################################################################################### _ShowErrorEventContext=true-- set this to false to disable 'EventContext' display --######################################################################################################### local IRLUA_PLUGIN_ERROR = error; --######################################################################################################### -- Sets a Global error message in the runtime engine. IRLUA_PLUGIN_SetGlobalErrorMessage = function(nCode, sMessage) if _tblErrorMessages[nCode] then if _ShowErrorEventContext then local sEventContext=Debug.GetEventContext() IRLUA_PLUGIN_ERROR("Error code "..nCode.." already in use, please use another.\r\n\r\nEventContext: "..sEventContext,2) else IRLUA_PLUGIN_ERROR("Error code "..nCode.." already in use, please use another.",2) end else _tblErrorMessages[nCode]=sMessage end end --######################################################################################################### -- Checks the number of arguments in the table and throws a syntax error If there are Not enough. -- This is useful For checking the number of arguments available To your aciton. local IRLUA_PLUGIN_CheckNumArgs = function(tbArgs,nArgs) local nCount=table.getn(tbArgs) if nCount < nArgs then if _ShowErrorEventContext then local sEventContext=Debug.GetEventContext() IRLUA_PLUGIN_ERROR(nArgs.." Arguments expected, "..nCount.." Arguments passed.\r\n\r\nEventContext: "..sEventContext,3) else IRLUA_PLUGIN_ERROR(nArgs.." Arguments expected, "..nCount.." Arguments passed.",3) end end end --######################################################################################################### -- Checks the value at a given argument table position To see if it is any of the specified types. -- If Not it throws a syntax error. --Possible variable types[ boolean, function, nil, number, string, table, thread, userdata] local IRLUA_PLUGIN_CheckTypes = function(tbArgs,nArg,tTypes) local sType = type(tbArgs[nArg]); local nTotalTypes = Table.Count(tTypes); local nStrikes = 0; local sAllowedTypes = ""; for nIndex, sAllowedType in tTypes do if nIndex < (nTotalTypes - 1) then sAllowedTypes = sAllowedTypes.." "..sAllowedType..","; elseif nIndex == nTotalTypes - 1 then sAllowedTypes = sAllowedTypes.." "..sAllowedType; elseif nIndex == nTotalTypes then if nTotalTypes == 1 then sAllowedTypes = " "..sAllowedType; else sAllowedTypes = sAllowedTypes.." or "..sAllowedType; end end if sType ~= String.Lower(sAllowedType) then nStrikes = nStrikes + 1; end end if nStrikes == nTotalTypes then if _ShowErrorEventContext then local sEventContext=Debug.GetEventContext() IRLUA_PLUGIN_ERROR("bad argument #" .. nArg .. ", must be a"..sAllowedTypes..", you passed a "..sType..".\r\n\r\nEventContext: "..sEventContext,3) else IRLUA_PLUGIN_ERROR("bad argument #" .. nArg .. ", must be a"..sAllowedTypes..", you passed a "..sType..".",3) end else return tbArgs[nArg] end end --######################################################################################################### -- Ensures that your table contains data of only the specified type. If it does not then false is returned. -- This will see nil values as subtables declarations and will, therefore, ignore them. local IRLUA_PLUGIN_CheckTableVars = function(tTable,tVarTypes) local bRet = true; local nTypes = Table.Count(tVarTypes); local nStrikes = 0; for nIndex, sItem in tTable do nStrikes = 0; for nType, sType in tVarTypes do if type(sItem) ~= sType then nStrikes = nStrikes + 1; end end if nStrikes >= nTypes then bRet = false; break; end end return bRet end --######################################################################################################### --ERROR PLUGIN DETAILS local ERROR_PLUGIN_NAME = ""; --######################################################################################################### --Displays a custom error message using the plugin and function name and lists the event context as well. local ERROR = function(sFunctionName,sMessage,nEmbedLevel) if not nEmbedLevel then nEmbedLevel = 0; elseif type(nEmbedLevel) ~= "number" then nEmbedLevel = 0; end if type(sFunctionName) ~= "string" then sFunctionName = ""; end local sFunctionNameCode = ""; if String.Replace(sFunctionName, " ", "", false) ~= "" then sFunctionNameCode = ", function \""..sFunctionName.."()\""; end IRLUA_PLUGIN_ERROR("\r\nError in \""..ERROR_PLUGIN_NAME.."\" plugin"..sFunctionNameCode.."\r\n\r\n"..sMessage.."\r\n\r\nEventContext: "..Debug.GetEventContext(),3 + nEmbedLevel); end --######################################################################################################### -- END IRLUA PLUGIN HELPER FUNCTIONS --######################################################################################################### --[[ --Example Usage function TEST(...) -- this function requires 3 arguments of differenet types. -- check the number of arguments IRLUA_PLUGIN_CheckNumArgs(arg,3); local junk1 = IRLUA_PLUGIN_CheckTypes(arg,1,{"string", "table"}); local junk2 = IRLUA_PLUGIN_CheckTypes(arg,2,{"number", "table"}); local junk3 = IRLUA_PLUGIN_CheckTypes(arg,3,{"nil", "number"}); Dialog.Message("Test", "Run Through"); local bRet = IRLUA_PLUGIN_CheckTableVars(junk1,{"string","number"}); if bRet then Dailog.Message("Good", "Table data checks out"); elseif bRet == false then Dailog.Message("Error", "Bad table data"); end end NOTES ON ERROR FUNCTION add the following to each function... local sCallingFunction = "MyFunction"; ..where "MyFunction" is the name of the calling function. NOTE: the sCallingFunction variable can be called whatever you like. It is argument number one for the ERROR function EXAMPLE: ERROR(sCallingFunction, "The world cannot be saved by a plugin"); If the error is inside an embedded function (a function within a function) then use a number for the third agument. The number is the depth of the ERROR message call (o for being inside one function, 1 for being inside a function within another function and so on). If you are not calling the ERROR function from within an ebedded function then you do not need the third argument, it can be nil. ]] --[[ NOTES ON ERROR CODE IMPLEMENTATION --EDIT: I forgot to mention, also make heavy use of Application.SetLastError in your actions, try and use predefined error codes but if you need to add error codes use IRLUA_PLUGIN_SetGlobalErrorMessage with a unique number range --example ( if you run the below script the message will read "Some error message" --Code: IRLUA_PLUGIN_SetGlobalErrorMessage(0023114, "Some error message") IRLUA_PLUGIN_SetGlobalErrorMessage(0023113, "Some other error message") Application.SetLastError(0023114) -- Test for error error = Application.GetLastError(); if (error ~= 0) then Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION); end ]]