no need to make file writes, just insert using table.insert

This commit is contained in:
Ayane Satomi 2022-07-02 18:05:49 +08:00
parent 55fae22de5
commit 4b0953c113
No known key found for this signature in database
GPG key ID: 431E3C36BEBE204B

View file

@ -1,10 +1,10 @@
--@name Starfall-TTSv2 --@name Starfall-TTSv2
--@author Minori --@author Minori, Henke, Empy, et al.
--@client --@client
local remoteLanguageIndex = "https://raw.githubusercontent.com/sr229/metastruct-experiments/master/starfall_metadata/allowed_google_voices.json" local remoteLanguageIndex = "https://raw.githubusercontent.com/sr229/metastruct-experiments/master/starfall_metadata/allowed_google_voices.json"
local localLanguageIndex = "./tts_index.json" local localLanguageIndex = "./tts_index.json"
local languageIndex local languageIndex = {}
local errorLookup = { [2] = "Invalid language" } local errorLookup = { [2] = "Invalid language" }
local DEFAULT_LANGUAGE = "en-gb" local DEFAULT_LANGUAGE = "en-gb"
@ -14,45 +14,31 @@ if not owner() then return end
-- Check if client has permission -- Check if client has permission
if not hasPermission("bass.loadURL", "https://translate.google.com/translate_tts") then return end if not hasPermission("bass.loadURL", "https://translate.google.com/translate_tts") then return end
if not hasPermission("file.read", localLanguageIndedx) then return end if not hasPermission("file.read", localLanguageIndex) then return end
local function getRemoteLanguageIndex() local function getRemoteLanguageIndex()
print("Building language index. Please be patient.")
http.get(remoteLanguageIndex, function(body, len, hdrs, code) http.get(remoteLanguageIndex, function(body, len, hdrs, code)
if len > 0 then if len > 0 then
file.write(localLanguageIndex, body) local rawData = json.decode(body)
if rawData then
for i, v in pairs(rawData.voices) do
table.insert(languageIndex, i, v)
end
else
print("Error: Could not decode JSON")
end
print("TTS is now ready! Available voices are: ")
printTable(languageIndex)
print("Type ':<lang-id>' to switch to that language")
print("Type ';<text>' to use the TTS!")
print("Have fun :)")
end end
end) end)
end end
local function parseLanguageIndex()
print("Building language index, please be patient...")
getRemoteLanguageIndex()
local rawFile = file.read(localLanguageIndex)
while rawFile == nil do
-- do nothing while we wait for data
return
end
if not file.exists(localLanguageIndex) then
print("Parsing index failed.")
end
local rawTable = json.decode(rawFile)
languageIndex = rawTable.voices
if languageIndex ~= nil then
print("Index built successfully. You're now ready to use TTS.")
print("Available languages: ")
printTable(languageIndex)
-- Remove the local language index.
file.delete(localLanguageIndex)
-- print("DEBUG: languageIndex val: " .. tostring(languageIndex))
end
end
local function RequestTTS(txt, l, callback) 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) bass.loadURL("https://translate.google.com/translate_tts?ie=UTF-8&q=" .. txt .. "&tl=" .. l .. "&client=tw-ob", "3d", callback)
end end
@ -73,9 +59,9 @@ local function DoTTS(sound)
end end
-- check if the value exists in a table -- check if the value exists in a table
local function hasval(table, value) local function hasval(tab, val)
for _, v in pairs(table) do for _, v in pairs(tab) do
if v == value then if v == val then
return true return true
end end
end end
@ -83,7 +69,7 @@ local function hasval(table, value)
end end
parseLanguageIndex() getRemoteLanguageIndex()
hook.add("playerchat", "tts", function(ply, txt) hook.add("playerchat", "tts", function(ply, txt)
if ply ~= owner() then return end if ply ~= owner() then return end