214 lines
8.2 KiB
Luau
214 lines
8.2 KiB
Luau
-- PlayerSettings (LocalScript, StarterPlayerScripts): applies the Settings window choices on this client.
|
|
-- The values live in the player profile on the server (OrnamentPlayerData.Settings) and arrive as player
|
|
-- attributes Set_Master / Set_Music / Set_Effects / Set_UI (0..1), Set_MusicOn, Set_Lang ("auto" / "en" / "ru").
|
|
-- The Settings window sets the same attributes locally while the player drags (instant feedback) and sends them
|
|
-- to the server when the player stops.
|
|
--
|
|
-- SOUND: three local SoundGroups in SoundService; every Sound without a group is put into one of them:
|
|
-- Settings_Music - the background music (SoundService.BackgroundMusic / the Music folder)
|
|
-- Settings_UI - interface clicks (SoundService.UISounds) and sounds inside PlayerGui (notification sounds)
|
|
-- Settings_Effects - everything else (machines, fireworks, footsteps, other players)
|
|
-- group volume = Master x own level; music off = Settings_Music volume 0.
|
|
--
|
|
-- LANGUAGE:
|
|
-- auto - texts are left alone, Roblox auto-translates them to the device language (AutoLocalize on)
|
|
-- en - AutoLocalize off: the original English text
|
|
-- ru - AutoLocalize off and every text is translated with ReplicatedStorage.OrnamentMachine.LangRU:
|
|
-- whole text first, then "%d"-style templates, then the English pieces texts are glued from (longest
|
|
-- first). Texts in PlayerGui and in world GUIs (signs, boards) and proximity prompts are watched, so texts
|
|
-- the game sets later (timers, notifications) are translated too. The English original is remembered per
|
|
-- object, switching back restores it. An object with the attribute NoTranslate is never touched.
|
|
local Players = game:GetService("Players")
|
|
local SoundService = game:GetService("SoundService")
|
|
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
|
|
|
local player = Players.LocalPlayer
|
|
local playerGui = player:WaitForChild("PlayerGui")
|
|
|
|
local function attr(n, d) local v = player:GetAttribute(n); if v == nil then return d end return v end
|
|
|
|
---------------------------------------------------------------- sound
|
|
local function group(name)
|
|
local g = SoundService:FindFirstChild(name)
|
|
if not g then
|
|
g = Instance.new("SoundGroup")
|
|
g.Name = name
|
|
g.Parent = SoundService
|
|
end
|
|
return g
|
|
end
|
|
local G_MUSIC, G_FX, G_UI = group("Settings_Music"), group("Settings_Effects"), group("Settings_UI")
|
|
local OURS = { [G_MUSIC] = true, [G_FX] = true, [G_UI] = true }
|
|
local musicFolder = SoundService:FindFirstChild("Music")
|
|
local uiFolder = SoundService:FindFirstChild("UISounds")
|
|
|
|
local function classify(s)
|
|
if not s:IsA("Sound") then return end
|
|
if s.SoundGroup and not OURS[s.SoundGroup] then return end -- e.g. MutedCharacterSounds (Footsteps)
|
|
local g
|
|
if s.Name == "BackgroundMusic" or (musicFolder and s:IsDescendantOf(musicFolder)) then g = G_MUSIC
|
|
elseif (uiFolder and s:IsDescendantOf(uiFolder)) or s:IsDescendantOf(playerGui) then g = G_UI
|
|
else g = G_FX end
|
|
if s.SoundGroup ~= g then s.SoundGroup = g end
|
|
end
|
|
for _, root in { workspace, SoundService, playerGui } do
|
|
for _, d in root:GetDescendants() do classify(d) end
|
|
root.DescendantAdded:Connect(function(d) if d:IsA("Sound") then task.defer(classify, d) end end)
|
|
end
|
|
|
|
local function applySound()
|
|
local master = attr("Set_Master", 1)
|
|
G_MUSIC.Volume = attr("Set_MusicOn", true) and master * attr("Set_Music", 1) or 0
|
|
G_FX.Volume = master * attr("Set_Effects", 1)
|
|
G_UI.Volume = master * attr("Set_UI", 1)
|
|
end
|
|
for _, n in { "Set_Master", "Set_Music", "Set_Effects", "Set_UI", "Set_MusicOn" } do
|
|
player:GetAttributeChangedSignal(n):Connect(applySound)
|
|
end
|
|
applySound()
|
|
|
|
---------------------------------------------------------------- language
|
|
local PAIRS = require(ReplicatedStorage:WaitForChild("OrnamentMachine"):WaitForChild("LangRU"))
|
|
local exact, pieces, templates = {}, {}, {}
|
|
-- a string.format template -> Lua pattern (each %d / %02d / %.2f / %s = one capture) or -> a "%s"-only format
|
|
local function scanFormat(s, onSpec, onPercent, onChar)
|
|
local out, i = {}, 1
|
|
while i <= #s do
|
|
local c = s:sub(i, i)
|
|
local spec = c == "%" and s:match("^%%[%d%.]*[dfs]", i)
|
|
if s:sub(i, i + 1) == "%%" then table.insert(out, onPercent); i += 2
|
|
elseif spec then table.insert(out, onSpec); i += #spec
|
|
else table.insert(out, onChar(c)); i += 1 end
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
local function esc(c) return c:find("[%^%$%(%)%.%[%]%*%+%-%?%%]") and ("%" .. c) or c end
|
|
for _, p in PAIRS do
|
|
local en, ru = p[1], p[2]
|
|
if exact[en] == nil then
|
|
exact[en] = ru
|
|
if en:find("%%[%d%.]*[dfs]") then
|
|
-- "+1 in %d:%02d · up to %d" -> "^%+1 in (.-):(.-) · up to (.-)$"; the Russian text gets the pieces in order
|
|
table.insert(templates, {
|
|
pat = "^" .. scanFormat(en, "(.-)", "%%", esc) .. "$",
|
|
ru = scanFormat(ru, "%s", "%%", function(c) return c end),
|
|
})
|
|
elseif utf8.len(en) and utf8.len(en) >= 4 and en:find("%a%a") then
|
|
table.insert(pieces, { en, ru })
|
|
end
|
|
end
|
|
end
|
|
table.sort(pieces, function(a, b) return #a[1] > #b[1] end)
|
|
|
|
local function replaceAll(s, a, b)
|
|
local out, i = {}, 1
|
|
while true do
|
|
local x, y = s:find(a, i, true)
|
|
if not x then break end
|
|
table.insert(out, s:sub(i, x - 1))
|
|
table.insert(out, b)
|
|
i = y + 1
|
|
end
|
|
if i == 1 then return s end
|
|
table.insert(out, s:sub(i))
|
|
return table.concat(out)
|
|
end
|
|
|
|
local memo, memoN = {}, 0
|
|
local function translate(s)
|
|
if type(s) ~= "string" or s == "" or not s:find("%a") then return s end
|
|
local hit = exact[s] or memo[s]
|
|
if hit then return hit end
|
|
local r
|
|
for _, t in templates do
|
|
local caps = { s:match(t.pat) }
|
|
if #caps > 0 then
|
|
for i, c in caps do caps[i] = translate(c) end
|
|
local ok, v = pcall(string.format, t.ru, table.unpack(caps))
|
|
if ok then r = v break end
|
|
end
|
|
end
|
|
if not r then
|
|
r = s
|
|
for _, p in pieces do
|
|
if r:find(p[1], 1, true) then r = replaceAll(r, p[1], p[2]) end
|
|
end
|
|
end
|
|
if memoN > 4000 then memo, memoN = {}, 0 end
|
|
memo[s] = r
|
|
memoN += 1
|
|
return r
|
|
end
|
|
|
|
local lang = "auto"
|
|
local PROPS = { TextLabel = { "Text" }, TextButton = { "Text" }, TextBox = { "PlaceholderText" }, ProximityPrompt = { "ActionText", "ObjectText" } }
|
|
local tracked = setmetatable({}, { __mode = "k" }) -- obj -> { [prop] = english, ["=" .. prop] = what we wrote }
|
|
|
|
local function render(obj, prop, rec)
|
|
local en = rec[prop]
|
|
local out = (lang == "ru" and not obj:GetAttribute("NoTranslate")) and translate(en) or en
|
|
rec["=" .. prop] = out
|
|
if obj[prop] ~= out then obj[prop] = out end
|
|
end
|
|
local function onChanged(obj, prop)
|
|
local rec = tracked[obj]
|
|
local cur = obj[prop]
|
|
if rec["=" .. prop] == cur then return end -- our own write
|
|
rec[prop] = cur -- the game set a new (English) text
|
|
render(obj, prop, rec)
|
|
end
|
|
local function watch(obj)
|
|
local props = PROPS[obj.ClassName]
|
|
if not props or tracked[obj] then return end
|
|
local rec = {}
|
|
tracked[obj] = rec
|
|
for _, prop in props do
|
|
rec[prop] = obj[prop]
|
|
rec["=" .. prop] = obj[prop]
|
|
obj:GetPropertyChangedSignal(prop):Connect(function() onChanged(obj, prop) end)
|
|
if lang == "ru" then render(obj, prop, rec) end
|
|
end
|
|
end
|
|
local function setAuto(obj)
|
|
if obj:IsA("GuiObject") or obj:IsA("LayerCollector") then obj.AutoLocalize = (lang == "auto") and not obj:GetAttribute("NoTranslate") end
|
|
end
|
|
|
|
local started = false
|
|
local function start()
|
|
if started then return end
|
|
started = true
|
|
local function hook(root, worldOnly)
|
|
local function add(d)
|
|
if worldOnly and not (d:IsA("ProximityPrompt") or d:FindFirstAncestorWhichIsA("LayerCollector")) then return end
|
|
setAuto(d)
|
|
watch(d)
|
|
end
|
|
for _, d in root:GetDescendants() do add(d) end
|
|
root.DescendantAdded:Connect(add)
|
|
end
|
|
hook(playerGui, false)
|
|
hook(workspace, true)
|
|
end
|
|
|
|
local function applyLang()
|
|
local new = attr("Set_Lang", "auto")
|
|
if new ~= "auto" and new ~= "en" and new ~= "ru" then new = "auto" end
|
|
if new == lang and started then return end
|
|
lang = new
|
|
if lang ~= "auto" then start() end
|
|
if not started then return end
|
|
for obj, rec in tracked do
|
|
setAuto(obj)
|
|
for _, prop in PROPS[obj.ClassName] or {} do
|
|
if rec[prop] ~= nil then render(obj, prop, rec) end
|
|
end
|
|
end
|
|
for _, root in { playerGui, workspace } do
|
|
for _, d in root:GetDescendants() do
|
|
if d:IsA("LayerCollector") or (root == playerGui and d:IsA("GuiObject")) then setAuto(d) end
|
|
end
|
|
end
|
|
end
|
|
player:GetAttributeChangedSignal("Set_Lang"):Connect(applyLang)
|
|
applyLang()
|