|
| 1 | +--[[ |
| 2 | +Script Name: ThemeScriptExecutor |
| 3 | +Purpose: Execute module scripts based on the current theme. |
| 4 | +Created by Rawblocky |
| 5 | +--]] |
| 6 | + |
| 7 | +-- ========================== |
| 8 | +--> SERVICES |
| 9 | +-- ========================== |
| 10 | +local ChangeHistoryService = game:GetService("ChangeHistoryService") |
| 11 | +local RunService = game:GetService("RunService") |
| 12 | +local ScriptEditorService = game:GetService("ScriptEditorService") |
| 13 | +local PluginGuiService = game:GetService("PluginGuiService") |
| 14 | +-- <- |
| 15 | + |
| 16 | +-- ========================== |
| 17 | +--> MODULES |
| 18 | +-- ========================== |
| 19 | +local Configuration = require(script.Configuration) |
| 20 | +local ThemeIcons = require(script.ThemeIcons) |
| 21 | +-- <- |
| 22 | + |
| 23 | +-- ========================== |
| 24 | +--> VARIABLES |
| 25 | +-- ========================== |
| 26 | +local pluginId = Configuration.PluginId |
| 27 | + |
| 28 | +local defaultSource = 'return function(isInitialLaunch: boolean)\n\tprint("%s")\nend' |
| 29 | +local modes = { |
| 30 | + Light = { |
| 31 | + source = string.format(defaultSource, "Good morning!"), |
| 32 | + scriptName = "🌞 Light", |
| 33 | + }, |
| 34 | + Dark = { |
| 35 | + source = string.format(defaultSource, "Good evening!"), |
| 36 | + scriptName = "🌚 Dark", |
| 37 | + }, |
| 38 | +} |
| 39 | +-- <- |
| 40 | + |
| 41 | +if not RunService:IsEdit() then |
| 42 | + -- This plugin should only run in edit mode |
| 43 | + return |
| 44 | +end |
| 45 | + |
| 46 | +--> Wait for script permissions |
| 47 | +local function checkForScriptPermissions() |
| 48 | + return pcall(function() |
| 49 | + local temporaryScript = Instance.new("Script") |
| 50 | + temporaryScript.Parent = PluginGuiService |
| 51 | + temporaryScript:Destroy() |
| 52 | + end) |
| 53 | +end |
| 54 | + |
| 55 | +local hasScriptPermissions, _ = checkForScriptPermissions() |
| 56 | +if not hasScriptPermissions then |
| 57 | + warn( |
| 58 | + "ThemeScriptExecutor: Script permissions are required to run this plugin. Restart Studio after giving permissions." |
| 59 | + ) |
| 60 | + return |
| 61 | +end |
| 62 | +-- <- |
| 63 | + |
| 64 | +--> Save/Load functions |
| 65 | +local settingCache = {} |
| 66 | +local function getSetting(settingName: string) |
| 67 | + if settingCache[settingName] ~= nil then |
| 68 | + return settingCache[settingName] |
| 69 | + end |
| 70 | + local setting = plugin:GetSetting(pluginId .. "-" .. settingName) |
| 71 | + settingCache[settingName] = setting |
| 72 | + return setting |
| 73 | +end |
| 74 | +local function setSetting(settingName: string, ...) |
| 75 | + plugin:SetSetting(pluginId .. "-" .. settingName, ...) |
| 76 | + settingCache[settingName] = ... |
| 77 | +end |
| 78 | +-- <- |
| 79 | + |
| 80 | +local function getTheme() |
| 81 | + return tostring(settings().Studio.Theme) |
| 82 | +end |
| 83 | + |
| 84 | +local function getButtonImage(buttonName: string) |
| 85 | + return ThemeIcons[buttonName][getTheme()] |
| 86 | +end |
| 87 | + |
| 88 | +local toolbar = plugin:CreateToolbar("ThemeScriptExecutor") |
| 89 | +local scripts = {} |
| 90 | +local pluginButtons = {} |
| 91 | +local isInitialLaunch = true |
| 92 | + |
| 93 | +local moduleScriptContainer = Instance.new("Folder") |
| 94 | +moduleScriptContainer.Archivable = false |
| 95 | +moduleScriptContainer.Name = pluginId |
| 96 | +moduleScriptContainer.Parent = PluginGuiService |
| 97 | + |
| 98 | +local function themeChanged() |
| 99 | + for mode, button in pluginButtons do |
| 100 | + button.Icon = getButtonImage(mode .. "Button") |
| 101 | + end |
| 102 | + |
| 103 | + local currentTheme = getTheme() |
| 104 | + local ranScript |
| 105 | + for mode, moduleScript in scripts do |
| 106 | + if mode == currentTheme then |
| 107 | + ranScript = moduleScript |
| 108 | + break |
| 109 | + end |
| 110 | + end |
| 111 | + if not ranScript then |
| 112 | + return |
| 113 | + end |
| 114 | + |
| 115 | + -- Clone the script because the script could've been edited and |
| 116 | + -- the original function was cached |
| 117 | + local ranScriptClone = ranScript:Clone() |
| 118 | + task.spawn(require(ranScriptClone), isInitialLaunch) |
| 119 | + if isInitialLaunch then |
| 120 | + isInitialLaunch = false |
| 121 | + end |
| 122 | + ChangeHistoryService:SetWaypoint("Executed theme script") |
| 123 | +end |
| 124 | +for mode, data in modes do |
| 125 | + local moduleScript = Instance.new("ModuleScript") |
| 126 | + moduleScript.Name = data.scriptName |
| 127 | + moduleScript.Source = getSetting(mode) or data.source |
| 128 | + moduleScript.Parent = moduleScriptContainer |
| 129 | + scripts[mode] = moduleScript |
| 130 | + |
| 131 | + local pluginButton = toolbar:CreateButton( |
| 132 | + mode, |
| 133 | + string.format("Edit '%s' theme module", data.scriptName), |
| 134 | + getButtonImage(mode .. "Button") |
| 135 | + ) |
| 136 | + pluginButtons[mode] = pluginButton |
| 137 | + pluginButton.ClickableWhenViewportHidden = true |
| 138 | + |
| 139 | + pluginButton.Click:Connect(function() |
| 140 | + plugin:OpenScript(moduleScript) |
| 141 | + end) |
| 142 | +end |
| 143 | + |
| 144 | +ScriptEditorService.TextDocumentDidChange:Connect(function(document: ScriptDocument, changes: table) |
| 145 | + local changedScript = document:GetScript() |
| 146 | + local mode |
| 147 | + for mode2, moduleScript in scripts do |
| 148 | + if moduleScript == changedScript then |
| 149 | + mode = mode2 |
| 150 | + break |
| 151 | + end |
| 152 | + end |
| 153 | + if not mode then |
| 154 | + return |
| 155 | + end |
| 156 | + local newText = document:GetText() |
| 157 | + setSetting(mode, newText) |
| 158 | +end) |
| 159 | + |
| 160 | +settings().Studio.ThemeChanged:Connect(themeChanged) |
| 161 | +themeChanged() |
0 commit comments