diff --git a/res/modules/internal/audio_input.lua b/res/modules/internal/audio_input.lua new file mode 100644 index 00000000..3540e88b --- /dev/null +++ b/res/modules/internal/audio_input.lua @@ -0,0 +1,27 @@ +local audio_input_tokens_store = {} +audio.input = {} + +local _gui_confirm = gui.confirm +local _base64_encode_urlsafe = base64.encode_urlsafe +local _random_bytes = random.bytes +local _debug_pack_by_frame = debug.get_pack_by_frame +local _audio_fetch_input = audio.fetch_input + +function audio.fetch_input(token, size) + if audio_input_tokens_store[token] then + return _audio_fetch_input(size) + end + error("access denied") +end + +local GRAND_PERMISSION_MSG = "Grant '%{0}' pack audio recording permission?" + +function audio.input.request_open(callback) + local token = _base64_encode_urlsafe(_random_bytes(18)) + local caller = _debug_pack_by_frame(1) + _gui_confirm(gui.str(GRAND_PERMISSION_MSG):gsub("%%{0}", caller), function() + audio_input_tokens_store[token] = caller + callback(token) + menu:reset() + end) +end diff --git a/res/scripts/hud.lua b/res/scripts/hud.lua index a5aad766..73355ddf 100644 --- a/res/scripts/hud.lua +++ b/res/scripts/hud.lua @@ -134,6 +134,10 @@ function on_hud_open() audio.play_sound_2d("test-sound", 2.0, 1.0, "ui") end +audio.input.request_open(function(token) + input_access_token = token +end) + function on_hud_render() if hud.hand_controller then hud.hand_controller() @@ -141,6 +145,8 @@ function on_hud_render() update_hand() end - local bytes = audio.fetch_input() - stream:feed(bytes) + if input_access_token then + local bytes = audio.fetch_input(input_access_token) + stream:feed(bytes) + end end diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index cb25150b..d0077b6f 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -179,6 +179,7 @@ end require "core:internal/maths_inline" require "core:internal/debugging" +require "core:internal/audio_input" asserts = require "core:internal/asserts" events = require "core:internal/events" diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index 783e054e..30343d7d 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -2,7 +2,7 @@ local _ffi = ffi local _debug_getinfo = debug.getinfo function crc32(bytes, chksum) - local chksum = chksum or 0 + chksum = chksum or 0 local length = #bytes if type(bytes) == "table" then diff --git a/res/texts/ru_RU.txt b/res/texts/ru_RU.txt index 03ee8322..cc6de475 100644 --- a/res/texts/ru_RU.txt +++ b/res/texts/ru_RU.txt @@ -25,7 +25,8 @@ Grant %{0} pack modification permission?=Выдать разрешение на Error at line %{0}=Ошибка на строке %{0} Run=Запустить Filter=Фильтр -Are you sure you want to open the link: =Вы уверены, что хотите открыть ссылку: +Are you sure you want to open the link: =Вы уверены, что хотите открыть ссылку: +Grant '%{0}' pack audio recording permission?=Выдать паку '%{0}' разрешение на запись звука? editor.info.tooltip=CTRL+S - Сохранить\nCTRL+R - Запустить\nCTRL+Z - Отменить\nCTRL+Y - Повторить devtools.traceback=Стек вызовов (от последнего)