mirror of
https://github.com/Earu/EasyChat.git
synced 2025-03-04 03:13:20 -05:00
Add a way for server owners to not run certain modules
This commit is contained in:
parent
c08ede907d
commit
2e8392a3ad
@ -1,11 +1,24 @@
|
||||
EC_MODULE_PATH = "easychat/modules/"
|
||||
|
||||
local ignore_list = {}
|
||||
local MODULE_IGNORE_LIST_PATH = "easychat/module_ignore_list.txt"
|
||||
if file.Exists(MODULE_IGNORE_LIST_PATH, "DATA") then
|
||||
local file_contents = file.Read(MODULE_IGNORE_LIST_PATH, "DATA")
|
||||
local lines = ("\r?\n"):Explode(file_contents)
|
||||
for _, line in pairs(lines) do
|
||||
line = line:Trim()
|
||||
if #line > 0 then
|
||||
ignore_list[line] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local modules = {}
|
||||
|
||||
local color_good = Color(0, 160, 220)
|
||||
local color_bad = Color(255, 127, 127)
|
||||
local color_default = Color(244, 167, 66)
|
||||
local module_loaded_count, module_failed_count = 0, 0
|
||||
local module_loaded_count, module_failed_count, module_ignored_count = 0, 0, 0
|
||||
|
||||
local function add_module(name, file_name, callback)
|
||||
name = isstring(name) and name or file_name
|
||||
@ -24,39 +37,59 @@ local function module_error(file_name, err, where)
|
||||
module_failed_count = module_failed_count + 1
|
||||
end
|
||||
|
||||
local function module_ignore(file_name, where)
|
||||
MsgC(color_default, "[EasyChat | " .. where .. "] ⮞ ", color_default, "Ignored " .. file_name .. "\n")
|
||||
module_ignored_count = module_ignored_count + 1
|
||||
end
|
||||
|
||||
local function load_modules(path)
|
||||
path = path or EC_MODULE_PATH
|
||||
|
||||
local start_time = SysTime()
|
||||
local shared_files = (file.Find(path .. "*.lua", "LUA"))
|
||||
for _, file_name in pairs(shared_files) do
|
||||
AddCSLuaFile(path .. file_name)
|
||||
local module = CompileFile(path .. file_name)
|
||||
local succ, module_name = xpcall(module, function(err)
|
||||
module_error(file_name, err, "SH")
|
||||
end)
|
||||
local file_path = path .. file_name
|
||||
if not ignore_list[file_path] then
|
||||
AddCSLuaFile(file_path)
|
||||
local module = CompileFile(file_path)
|
||||
local succ, module_name = xpcall(module, function(err)
|
||||
module_error(file_name, err, "SH")
|
||||
end)
|
||||
|
||||
if succ then
|
||||
add_module(module_name, file_name, module)
|
||||
if succ then
|
||||
add_module(module_name, file_name, module)
|
||||
end
|
||||
else
|
||||
module_ignore(file_name, "SH")
|
||||
end
|
||||
end
|
||||
|
||||
if SERVER then
|
||||
local server_files = (file.Find(path .. "server/*.lua", "LUA"))
|
||||
for _, file_name in pairs(server_files) do
|
||||
local module = CompileFile(path .. "server/" .. file_name)
|
||||
local succ, module_name = xpcall(module, function(err)
|
||||
module_error(file_name, err, "SV")
|
||||
end)
|
||||
local file_path = path .. "server/" .. file_name
|
||||
if not ignore_list[file_path] then
|
||||
local module = CompileFile(file_path)
|
||||
local succ, module_name = xpcall(module, function(err)
|
||||
module_error(file_name, err, "SV")
|
||||
end)
|
||||
|
||||
if succ then
|
||||
add_module(module_name, file_name, module)
|
||||
if succ then
|
||||
add_module(module_name, file_name, module)
|
||||
end
|
||||
else
|
||||
module_ignore(file_name, "SV")
|
||||
end
|
||||
end
|
||||
|
||||
local client_files = (file.Find(path .. "client/*.lua", "LUA"))
|
||||
for _, file_name in pairs(client_files) do
|
||||
AddCSLuaFile(path .. "client/" .. file_name)
|
||||
local file_path = path .. "client/" .. file_name
|
||||
if not ignore_list[file_path] then
|
||||
AddCSLuaFile(file_path)
|
||||
else
|
||||
module_ignore(file_name, "CL")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -74,9 +107,9 @@ local function load_modules(path)
|
||||
end
|
||||
end
|
||||
|
||||
MsgC(color_default, "[EasyChat] ⮞ ", color_good, ("Loaded %d modules successfully, %d modules failed to load.\n"):format(module_loaded_count, module_failed_count))
|
||||
MsgC(color_default, "[EasyChat] ⮞ ", color_good, ("Loaded %d modules successfully, %d ignored, %d failed to load.\n"):format(module_loaded_count, module_ignored_count, module_failed_count))
|
||||
MsgC(color_default, "[EasyChat] ⮞ ", color_good, ("Initialized in %fs\n"):format(SysTime() - start_time))
|
||||
module_loaded_count, module_failed_count = 0, 0
|
||||
module_loaded_count, module_failed_count, module_ignored_count = 0, 0, 0
|
||||
end
|
||||
|
||||
local function get_modules()
|
||||
|
@ -212,6 +212,61 @@ local function create_default_settings()
|
||||
|
||||
settings:AddSpacer(category_name)
|
||||
|
||||
local setting_ignored_modules = settings:AddSetting(category_name, "list", "Ignored Modules")
|
||||
local ignored_modules_list = setting_ignored_modules.List
|
||||
ignored_modules_list:SetMultiSelect(true)
|
||||
ignored_modules_list:AddColumn("Path (Relative to Lua folder)")
|
||||
|
||||
local function build_ignore_module_list()
|
||||
ignored_modules_list:Clear()
|
||||
|
||||
for _, module_path in pairs(EasyChat.Config.ModuleIgnoreList) do
|
||||
ignored_modules_list:AddLine(module_path)
|
||||
end
|
||||
end
|
||||
|
||||
build_ignore_module_list()
|
||||
hook.Add("ECServerConfigUpdate", ignored_modules_list, build_ignore_module_list)
|
||||
|
||||
local setting_ignore_module = settings:AddSetting(category_name, "action", "Ignore Module")
|
||||
setting_ignore_module:SetImage("icon16/shield.png")
|
||||
setting_ignore_module.DoClick = function()
|
||||
EasyChat.AskForInput("Ignore Module (Type a path relative to the Lua folder)", function(ignored_module_path)
|
||||
local old_list = table.Copy(EasyChat.Config.ModuleIgnoreList)
|
||||
local current_list = EasyChat.Config.ModuleIgnoreList
|
||||
table.insert(current_list, ignored_module_path)
|
||||
|
||||
local succ, err = EasyChat.Config:WriteModuleIgnoreList(current_list)
|
||||
if not succ then
|
||||
EasyChat.Config.ModuleIgnoreList = old_list
|
||||
notification.AddLegacy(err, NOTIFY_ERROR, 3)
|
||||
surface.PlaySound("buttons/button11.wav")
|
||||
end
|
||||
end, false):SetWide(600)
|
||||
end
|
||||
|
||||
local setting_unignore_module = settings:AddSetting(category_name, "action", "Unignore Module")
|
||||
setting_unignore_module:SetImage("icon16/shield.png")
|
||||
setting_unignore_module.DoClick = function()
|
||||
local old_list = table.Copy(EasyChat.Config.ModuleIgnoreList)
|
||||
local current_list = EasyChat.Config.ModuleIgnoreList
|
||||
|
||||
local lines = ignored_modules_list:GetSelected()
|
||||
for _, line in pairs(lines) do
|
||||
local ignore_path = line:GetColumnText(1)
|
||||
table.RemoveByValue(current_list, ignore_path)
|
||||
end
|
||||
|
||||
local succ, err = EasyChat.Config:WriteModuleIgnoreList(current_list)
|
||||
if not succ then
|
||||
EasyChat.Config.ModuleIgnoreList = old_list
|
||||
notification.AddLegacy(err, NOTIFY_ERROR, 3)
|
||||
surface.PlaySound("buttons/button11.wav")
|
||||
end
|
||||
end
|
||||
|
||||
settings:AddSpacer(category_name)
|
||||
|
||||
local setting_disable_modules = settings:AddSetting(category_name, "action", EC_NO_MODULES:GetBool() and "Run Modules" or "Disallow Modules")
|
||||
setting_disable_modules.DoClick = function() EC_NO_MODULES:SetBool(not EC_NO_MODULES:GetBool()) end
|
||||
|
||||
|
@ -9,6 +9,7 @@ local NET_WRITE_SETTING_OVERRIDE = "EASY_CHAT_SERVER_SETTING_WRITE_OVERRIDE"
|
||||
local NET_WRITE_TAGS_IN_NAMES = "EASY_CHAT_TAGS_IN_NAMES"
|
||||
local NET_WRITE_TAGS_IN_MESSAGES = "EASY_CHAT_TAGS_IN_MESSAGES"
|
||||
local NET_WRITE_PLY_NAME = "EASY_CHAT_WRITE_PLY_NAME"
|
||||
local NET_MODULE_IGNORE_LIST = "EASY_CHAT_MODULE_IGNORE_LIST"
|
||||
|
||||
local default_config = {
|
||||
OverrideClientSettings = true,
|
||||
@ -58,6 +59,7 @@ if SERVER then
|
||||
util.AddNetworkString(NET_WRITE_TAGS_IN_NAMES)
|
||||
util.AddNetworkString(NET_WRITE_TAGS_IN_MESSAGES)
|
||||
util.AddNetworkString(NET_WRITE_PLY_NAME)
|
||||
util.AddNetworkString(NET_MODULE_IGNORE_LIST)
|
||||
|
||||
local CONFIG_PATH = "easychat/server_config.json"
|
||||
function config:Save()
|
||||
@ -124,8 +126,19 @@ if SERVER then
|
||||
|
||||
config:Load()
|
||||
|
||||
local MODULE_IGNORE_LIST_PATH = "easychat/module_ignore_list.txt"
|
||||
net.Receive(NET_SEND_CONFIG, function(_, ply)
|
||||
config:Send(ply, false)
|
||||
|
||||
if ply:IsAdmin() and file.Exists(MODULE_IGNORE_LIST_PATH, "DATA") then
|
||||
EasyChat.RunOnNextFrame(function()
|
||||
local file_contents = file.Read(MODULE_IGNORE_LIST_PATH, "DATA")
|
||||
local paths = ("\r?\n"):Explode(file_contents)
|
||||
net.Start(NET_MODULE_IGNORE_LIST)
|
||||
net.WriteTable(paths)
|
||||
net.Send(ply)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
local function restricted_receive(net_string, callback)
|
||||
@ -228,12 +241,30 @@ if SERVER then
|
||||
|
||||
EasyChat.SafeHookRun("ECPlayerNameChange", ply, target_ply, target_ply:Nick(), name)
|
||||
end)
|
||||
|
||||
restricted_receive(NET_MODULE_IGNORE_LIST, function(_, ply)
|
||||
local ignore_paths = net.ReadTable()
|
||||
if file.Exists(MODULE_IGNORE_LIST_PATH, "DATA") then
|
||||
file.Delete(MODULE_IGNORE_LIST_PATH)
|
||||
end
|
||||
|
||||
file.Write(MODULE_IGNORE_LIST_PATH, table.concat(ignore_paths, "\n"))
|
||||
|
||||
EasyChat.Print(("%s changed the module ignore list, a restart is required"):format(ply))
|
||||
EasyChat.Warn("EasyChat's module ignore list updated. A restart is required.")
|
||||
EasyChat.RunOnNextFrame(function()
|
||||
net.Start(NET_MODULE_IGNORE_LIST)
|
||||
net.WriteTable(ignore_paths)
|
||||
net.Broadcast()
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
if CLIENT then
|
||||
local ADMIN_WARN = "You need to be an admin to do that"
|
||||
|
||||
local config = default_config
|
||||
config.ModuleIgnoreList = {}
|
||||
EasyChat.Config = config
|
||||
|
||||
hook.Add("InitPostEntity", TAG, function()
|
||||
@ -282,6 +313,12 @@ if CLIENT then
|
||||
EasyChat.SafeHookRun("ECServerConfigUpdate", EasyChat.Config)
|
||||
end)
|
||||
|
||||
net.Receive(NET_MODULE_IGNORE_LIST, function()
|
||||
local paths = net.ReadTable()
|
||||
EasyChat.Config.ModuleIgnoreList = paths
|
||||
EasyChat.SafeHookRun("ECServerConfigUpdate", EasyChat.Config)
|
||||
end)
|
||||
|
||||
function config:WriteUserGroup(user_group, tag, emote_name, emote_size, emote_provider)
|
||||
if not LocalPlayer():IsAdmin() then return false, ADMIN_WARN end
|
||||
|
||||
@ -402,4 +439,14 @@ if CLIENT then
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function config:WriteModuleIgnoreList(paths)
|
||||
if not LocalPlayer():IsAdmin() then return false, ADMIN_WARN end
|
||||
|
||||
net.Start(NET_MODULE_IGNORE_LIST)
|
||||
net.WriteTable(paths)
|
||||
net.SendToServer()
|
||||
|
||||
return true
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user