From b12ae753983863ab90bc4a5b638b526ea038263f Mon Sep 17 00:00:00 2001 From: Ayane Satomi Date: Fri, 16 Sep 2022 18:57:06 +0800 Subject: [PATCH] Hide TTS chip on serverside --- starfallex/starfall_tts_gimpy.lua | 203 ++++++++++++++++-------------- 1 file changed, 106 insertions(+), 97 deletions(-) diff --git a/starfallex/starfall_tts_gimpy.lua b/starfallex/starfall_tts_gimpy.lua index e12756a..f636426 100644 --- a/starfallex/starfall_tts_gimpy.lua +++ b/starfallex/starfall_tts_gimpy.lua @@ -1,118 +1,127 @@ --@name Starfall-TTS --@author Minori, Henke, Empy, et al. --@include https://gist.githubusercontent.com/sr229/5e6f3a5b03181704a871207c14706499/raw/2ee5b6cfb3a875a5ad2024a38d0131cfb5278785/url-encode.lua as henke.txt ---@client - +--@shared local remoteLanguageIndex = "https://raw.githubusercontent.com/sr229/metastruct-experiments/master/starfall_metadata/allowed_google_voices.json" local languageIndex = {} -local errorLookup = { [2] = "Invalid language" } + +local errorLookup = { + [2] = "Invalid language" +} local DEFAULT_LANGUAGE = "en-gb" local currentLang = DEFAULT_LANGUAGE require("henke.txt") --- Check if client has permission -if not hasPermission("bass.loadURL", "https://translate.google.com/translate_tts") then return end +if SERVER then + chip():setNoDraw(true) +end -local function getRemoteLanguageIndex() - if not hasPermission("http.get", remoteLanguageIndex) then return end +if CLIENT then + -- Check if client has permission + if not hasPermission("bass.loadURL", "https://translate.google.com/translate_tts") then return end - print("Building language index. Please be patient...") - http.get(remoteLanguageIndex, function(body, len, hdrs, code) - if len > 0 then - local rawData = json.decode(body) + local function getRemoteLanguageIndex() + if not hasPermission("http.get", remoteLanguageIndex) then return end + print("Building language index. Please be patient...") - if rawData then - for i, v in pairs(rawData.voices) do - table.insert(languageIndex, i, v) + http.get(remoteLanguageIndex, function(body, len, hdrs, code) + if len > 0 then + local rawData = json.decode(body) + + if rawData then + for i, v in pairs(rawData.voices) do + table.insert(languageIndex, i, v) + end + else + error("Could not decode JSON") end - else - error("Could not decode JSON") + + print("TTS is now ready! Available voices are: ") + printTable(languageIndex) + print("Type ':' to switch to that language.\nType ';' to use the TTS!") + print("Have fun :)") end - - print("TTS is now ready! Available voices are: ") - printTable(languageIndex) - print("Type ':' to switch to that language.\nType ';' to use the TTS!") - print("Have fun :)") - end - end) -end - -local function RequestTTS(txt, l, callback) - bass.loadURL("https://translate.google.com/translate_tts?ie=UTF-8&q=" .. txt .. "&tl=" .. l .. "&client=tw-ob", "3d", callback) -end - -local function DoTTS(sound) - -- we dispose the current reference, then we create a new one - if soundref then soundref:stop() end - soundref = sound - - --local soundLength = sound:getLength() - hook.add("think", "soundFollow", function() - if sound:isValid() then sound:setPos(owner():getPos()) else - hook.remove("think", "soundFollow") - end - end) - - sound:play() -end - --- check if the value exists in a table -local function hasval(tab, val) - for _, v in pairs(tab) do - if v == val then - return true - end - end - return false -end - - -getRemoteLanguageIndex() - -hook.add("playerchat", "tts", function(ply, txt) - if ply ~= owner() then return end - - if string.sub(txt, 1, 1) == ":" then - local l = string.gsub(txt, ":", "") - if #l > 1 then - local lastLang = currentLang - - if hasval(languageIndex, string.lower(l)) then - currentLang = l - print("Language set to " .. l) - else - print("Language " .. l .. " not found. Check available languages for valid ones.") - currentLang = lastLang - end - end + end) end - if string.sub(txt, 1, 1) ~= ";" then return end + local function RequestTTS(txt, l, callback) + bass.loadURL("https://translate.google.com/translate_tts?ie=UTF-8&q=" .. txt .. "&tl=" .. l .. "&client=tw-ob", "3d", callback) + end - txt = string.sub(txt, 2) - if #txt < 1 then return end - - txt = urlencode(txt) - - RequestTTS(txt, currentLang, function(snd, err, name) - if not snd then - error("error: " .. errorLookup[err]) - - if err == 2 then - -- reset everything!! - currentLang = DEFAULT_LANGUAGE - - RequestTTS(txt, currentLang, function(s, e, n) - if not snd then return end - - DoTTS(snd) - end) - end - - return + local function DoTTS(sound) + -- we dispose the current reference, then we create a new one + if soundref then + soundref:stop() end - DoTTS(snd) + soundref = sound + + --local soundLength = sound:getLength() + hook.add("think", "soundFollow", function() + if sound:isValid() then + sound:setPos(owner():getPos()) + else + hook.remove("think", "soundFollow") + end + end) + + sound:play() + end + + -- check if the value exists in a table + local function hasval(tab, val) + for _, v in pairs(tab) do + if v == val then return true end + end + + return false + end + + getRemoteLanguageIndex() + + hook.add("playerchat", "tts", function(ply, txt) + if ply ~= owner() then return end + + if string.sub(txt, 1, 1) == ":" then + local l = string.gsub(txt, ":", "") + + if #l > 1 then + local lastLang = currentLang + + if hasval(languageIndex, string.lower(l)) then + currentLang = l + print("Language set to " .. l) + else + print("Language " .. l .. " not found. Check available languages for valid ones.") + currentLang = lastLang + end + end + end + + if string.sub(txt, 1, 1) ~= ";" then return end + txt = string.sub(txt, 2) + if #txt < 1 then return end + txt = urlencode(txt) + + RequestTTS(txt, currentLang, function(snd, err, name) + if not snd then + error("error: " .. errorLookup[err]) + + if err == 2 then + -- reset everything!! + currentLang = DEFAULT_LANGUAGE + + RequestTTS(txt, currentLang, function(s, e, n) + if not snd then return end + DoTTS(snd) + end) + end + + return + end + + DoTTS(snd) + end) end) -end) \ No newline at end of file +end \ No newline at end of file