-- Constants local C = {} -- Format strings C.formatString = { keyValue = "%s: %s", } -- Pattern strings C.patternString = { leadingSpaces = "^%s*", trailingSpaces = "%s*$", } -- Tooltips, labels, messages, constant strings C.constantString = { empty = "", commandOn = "on", commandOff = "off", chatCommandPrefix = "/%a", derenderAvatars = "Derender avatars", } -- Util module chunk local Util = {} --[[ Turnery-like implementation for inline conditions params: #boolean condition - the boolean expression tested #exp if_true - the expression returned when condition is true #exp if_fale - the expression returned when condition is false ]] function Util:fif(condition, if_true, if_false) if condition then return if_true else return if_false end end -- Runtime values for the viewer session local runtime = { -- Derender avatars derenderAvatars = false, -- Derendered avatar ids derenderedId = {}, } --[[ Toggle derender avatars params: #string commannd - the state to set ]] local function SetDerenderAvatars(command) if command ~= nil and (command == C.constantString.commandOn or command == C.constantString.commandOff) then runtime.derenderAvatars = not runtime.derenderAvatars if not runtime.derenderAvatars then local id while #runtime.derenderedId > 0 do id = table.remove(runtime.derenderedId) if not DerenderObject(id, false) then print(string.format(C.formatString.keyValue, "Failed to clear derendered id", id)) end end end end end --[[ Set derender avatars params: #string text - the current chat text #number commandLength - the command string length ]] local function HandleDerenderAvatars(text, commandLength) if text:len() > commandLength then local option = (text:sub(commandLength + 1) :gsub(C.patternString.leadingSpaces, C.constantString.empty) :gsub(C.patternString.trailingSpaces, C.constantString.empty)):lower() SetDerenderAvatars(option) end OpenNotification(0, string.format(C.formatString.keyValue, C.constantString.derenderAvatars, Util:fif(runtime.derenderAvatars, C.constantString.commandOn, C.constantString.commandOff))) end --[[ Handle avatar rezzing params: #string id - the UUID of the avatar rezzed ]] function OnAvatarRezzing(id) if not IsAgentFriend(id) and runtime.derenderAvatars then if DerenderObject(id, true) then table.insert(runtime.derenderedId, id) else print(string.format(C.formatString.keyValue, "Failed to add derender id", id)) end end end --[[ Handle chat send event params: #string text - the incoming string commands: /da [on|off] - Set derender avatars return: #string ]] function OnSendChat(text) local chatCommand = { DA = "da", } if text:sub(1, 2):find(C.constantString.chatCommandPrefix) ~= nil then if text:find(chatCommand.DA, 2, true) == 2 then HandleDerenderAvatars(text, 1 + chatCommand.DA:len()) else return text end return C.constantString.empty else return text end end