Иконка ресурса

FREE SoundBox : 2.0.1 2.0.1

Нет прав для скачивания
C#:
// Все права принадлежат дискорд сообществу https://discord.gg/VgNHPpNrz6
using System.Linq;
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using Oxide.Core;
using UnityEngine;
using System.Collections;
using Oxide.Game.Rust.Cui;
using System.Globalization;
using Oxide.Core.Plugins;
using Oxide.Core.Configuration;
using VLB;
using Network;
using UnityEngine.Networking;
using Facepunch;
namespace Oxide.Plugins {
    [Info("SoundBox", "Razor", "2.0.1")]
    [Description("Play some streams")]
    public class SoundBox : RustPlugin {
        [PluginReference]
        Plugin ImageLibrary;
        private static SoundBox _;
        static System.Random random = new System.Random();
        public static Dictionary<string, FollowInfo> BoomBoxPlayerFollow = new Dictionary<string, FollowInfo>();
        private const string permAdmin = "soundbox.admin";
        private const string permUse = "soundbox.use";
        public class FollowInfo {
            public BoomBox boombox;
            public string CurrentName;
            public string CurrentUrl;
            public bool IsOn;
        }
#region Init
        private void Init() {
            _ = this;
            RegisterPermissions();
        }
        private void OnServerInitialized() {
            InvokeHandler.Instance.StartCoroutine(DownloadSound());
            LoadImages();
        }
        private void LoadImages(int tries = 0) {
            if (tries > 10) {
                PrintWarning($"ImageLibrary failed to load images.Unloading Plugin!");
                covalence.Server.Command($"o.unload {Name}");
                return;
            }
            PrintWarning($"Waiting on ImageLibrary to load!");
            if (ImageLibrary == null) {
                tries++;
                timer.Once(10f, () => {
                    LoadImages(tries);
                }
                );
                return;
            }
            Dictionary<string, string> limagelist = new Dictionary<string, string>();
            limagelist.Add("SoundBoxBackgroundImage", configData.settings.BackgroundImage);
            PrintWarning("Sending Image List To LimgeLibrary");
            ImageLibrary?.Call("ImportImageList", Name, limagelist, 0UL, true, new Action(imagesReady));
        }
        private static string BackgroundImage;
        private static string ImageAdminPicks;
        private static string ImageHeader;
        private static string ImageSongButton;
        private static string ImageRemoveButton;
        private void imagesReady() {
            BackgroundImage = ImageLibrary?.Call<string>("GetImage", "SoundBoxBackgroundImage");
            PrintWarning("Images Sent to ImageLibrary And Ready");
        }
        private void Unload() {
            foreach (BasePlayer player in BasePlayer.activePlayerList.ToList()) CuiHelper.DestroyUi(player, MainPanel);
            _ = null;
        }
        private void RegisterPermissions() {
            permission.RegisterPermission(permAdmin, this);
            permission.RegisterPermission(permUse, this);
        }
#endregion Init
#region Config
        private ConfigData configData;
        class ConfigData {
            [JsonProperty(PropertyName = "Image Links")]
            public Settings settings {
                get;
                set;
            }
            [JsonProperty(PropertyName = "General Settings")]
            public Settings2 settings2 {
                get;
                set;
            }
            [JsonProperty(PropertyName = "Musical Genres And Urls")]
            public Genres genres {
                get;
                set;
            }
            public class Settings {
                [JsonProperty(PropertyName = "Background Image URL")]
                public string BackgroundImage {
                    get;
                    set;
                }
            }
            public class Settings2 {
                [JsonProperty(PropertyName = "Allow players to have there own list and URL's")]
                public bool AlowPlayerPicks {
                    get;
                    set;
                }
                [JsonProperty(PropertyName = "Players URL's must contain")]
                public List<string> MustContain {
                    get;
                    set;
                }
                [JsonProperty(PropertyName = "Players URL's can not contain")]
                public List<string> BlockedUrls {
                    get;
                    set;
                }
                [JsonProperty(PropertyName = "Total allowed URL's the player can have in his list")]
                public int TotalAllowedPlayerUrls {
                    get;
                    set;
                }
            }
            public class Genres {
                [JsonProperty(PropertyName = "Musical genres")]
                public Dictionary<string, List<genresInfo>> lists {
                    get;
                    set;
                }
            }
            public Oxide.Core.VersionNumber Version {
                get;
                set;
            }
        }
        protected override void LoadConfig() {
            base.LoadConfig();
            configData = Config.ReadObject<ConfigData>();
            if (configData.Version < Version) UpdateConfigValues();
            Config.WriteObject(configData, true);
        }
        protected override void LoadDefaultConfig() => configData = GetBaseConfig();
        private ConfigData GetBaseConfig() {
            return new ConfigData {
                settings = new ConfigData.Settings {
                    BackgroundImage = "https://i.ibb.co/wpv4GZx/soundbox.jpg"  //https://i.ibb.co/JKs9163/background.png
                }
                , settings2 = new ConfigData.Settings2 {
                    MustContain = new List<string>() {
                        "http"
                    }
                    , BlockedUrls = new List<string>() {
                        ".php", ".htm", "?"
                    }
                    , TotalAllowedPlayerUrls = 150
                }
                , genres = new ConfigData.Genres {
                    lists = new Dictionary<string, List<genresInfo>>() {
                        {
                            "Rock", new List<genresInfo>() {
                            }
                        }
                        , {
                            "Disco", new List<genresInfo>() {
                            }
                        }
                        , {
                            "Pop", new List<genresInfo>() {
                            }
                        }
                        , {
                            "Hip hop", new List<genresInfo>() {
                            }
                        }
                        , {
                            "Jazz", new List<genresInfo>() {
                            }
                        }
                        , {
                            "Blues", new List<genresInfo>() {
                            }
                        }
                        , {
                            "Rap", new List<genresInfo>() {
                            }
                        }
                    }
                }
                , Version = Version
            }
            ;
        }
        protected override void SaveConfig() => Config.WriteObject(configData, true);
        private void UpdateConfigValues() {
            PrintWarning("Config update detected! Updating config values...");
            ConfigData baseConfig = GetBaseConfig();
            if (configData.Version < new VersionNumber(2, 0, 0)) configData = baseConfig;
            configData.Version = Version;
            PrintWarning("Config update completed!");
        }
        public class genresInfo {
            [JsonProperty(PropertyName = "NAME")]
            public string urlName;
            [JsonProperty(PropertyName = "URL")]
            public string URL;
            [JsonProperty(PropertyName = "Contry code")]
            public string CountryCode;
        }
#endregion Config
#region Data
        public class TheData {
            public Dictionary<string, List<genresInfo>> SavedURLS = new Dictionary<string, List<genresInfo>>();
        }
        private bool DataFileExists(string path) {
            return Interface.Oxide.DataFileSystem.ExistsDatafile($"{Name}/{path}");
        }
        private DynamicConfigFile GetDataFile(string path) {
            return Interface.Oxide.DataFileSystem.GetFile($"{Name}/{path}");
        }
        private bool TryLoadUrlList(string filename, out TheData data) {
            if (DataFileExists(filename)) {
                var file = GetDataFile(filename);
                try {
                    data = file.ReadObject<TheData>();
                    return true;
                }
                catch (Exception ex) {
                    PrintError($"Error reading data from {file.Filename}: ${ex.Message}");
                }
            }
            data = new TheData();
            return false;
        }
        private bool TryLoadUrlCountry(string filename, out List<netRadio> data) {
            if (DataFileExists(filename)) {
                var file = GetDataFile(filename);
                try {
                    data = file.ReadObject<List<netRadio>>();
                    return true;
                }
                catch (Exception ex) {
                    PrintError($"Error reading data from {file.Filename}: ${ex.Message}");
                }
            }
            data = new List<netRadio>();
            return false;
        }
        void SaveData(string filename, TheData data) {
            GetDataFile(filename).WriteObject(data);
        }
        void SaveDataUrl(string filename, List<netRadio> data) {
            GetDataFile(filename).WriteObject(data);
        }
        public class netRadio {
            public string name;
            public string url;
            public string tags;
        }
#endregion Data
#region Hooks
        private void CanDismountEntity(BasePlayer player, BaseMountable entity) {
            ModularCar vehicle = entity?.VehicleParent() as ModularCar;
            if (vehicle != null) {
                DestroyUI(player);
            }
        }
        private object OnBoomboxToggle(BoomBox boombox, BasePlayer player, bool toggle) {
            if (player == null || boombox == null || boombox.BaseEntity == null || !permission.UserHasPermission(player.UserIDString, permUse) && !permission.UserHasPermission(player.UserIDString, permAdmin)) return null;
            if (boombox.BaseEntity is HeldBoomBox && !toggle) return null;
            if (!BoomBoxPlayerFollow.TryGetValue(player.UserIDString, out var myboombox)) {
                myboombox = new FollowInfo() {
                    boombox = boombox, CurrentName = "", CurrentUrl = "", IsOn = toggle
                }
                ;
                BoomBoxPlayerFollow.Add(player.UserIDString, myboombox);
            }
            else myboombox.boombox = boombox;
            myboombox.IsOn = toggle;
            BuildSounBoxUI(player);
            return false;
        }
#endregion
#region Commands
        public void ChangeUrl(BoomBox boxController, string url, BasePlayer player, bool autoReplay = true) {
            bool wasPlaying = false;
            if (url.ToLower().Contains("dropbox") && !url.ToLower().Contains("dropboxusercontent")) {
                url = url.Replace("dropbox", "dl.dropboxusercontent").Replace("www.", "").Replace("?dl=0", "").Replace("?dl=1", "").Trim();
            }
            if (boxController != null) {
                if (boxController.IsOn()) {
                    wasPlaying = true;
                    boxController.ServerTogglePlay(false);
                }
                if (player != null) boxController.AssignedRadioBy = player.userID;
                boxController.CurrentRadioIp = url;
                boxController.baseEntity.ClientRPC<string>((Connection)null, "OnRadioIPChanged", url);
                timer.Once(0.1f, () => {
                    if (boxController != null) {
                        if (autoReplay && wasPlaying && !boxController.IsOn()) boxController.ServerTogglePlay(true);
                        boxController.baseEntity.SendNetworkUpdateImmediate(true);
                    }
                }
                );
            }
        }
        private string CombineWords(string oldstring) {
            string new_text = oldstring.Replace(" ", "_");
            return new_text;
        }
#endregion HooksAndCommands
#region New UI
        public const string MainPanel = "SoundBox.MainPanel";
        public const string MainPanelJon = "SoundBox.MainPanelJon";
        public const string MainPanelScroll = "SoundBox.MainPanelScroll";
        public const string MainPanelScroll1 = "SoundBox.MainPanelScroll1";
        public const string MainPanelScroll2 = "SoundBox.MainPanelScroll2";
        public const string MainBodyPanel = "SoundBox.MainBodyPanel";
        public const string MainPanelNav = "SoundBox.MainPanelNav";
        public const string MainPanelSub = "SoundBox.MainPanelSub";
        public const string MainPanelSub2 = "SoundBox.MainPanelSub2";
        public const string MainPanelSubSide = "SoundBox.MainPanelSubSide";
        public const string MainPanelSubSide1 = "SoundBox.MainPanelSubSide1";
        public const string MainPanelSubSide2 = "SoundBox.MainPanelSubSide2";
        public const string ButtonCUI = "SoundBox.ButtonCUI";
        public const string MainPanelPlay = "SoundBox.MainPanelPlay";
        public const string MainPanelPlayPanel = "SoundBox.MainPanelPlayPanel";
        public static ulong SecretKey = GenerateKey();
        public static ulong GenerateKey() => (ulong)UnityEngine.Random.Range(1, 9999999999999999);
        private const string command = "soundboxuicommand";
        private const string commandname = "soundboxuicommandname";
        private const string commandurl = "soundboxuicommandurl";
        private void BuildSounBoxUI(BasePlayer player, string config = "", int page = 1) {
            if (!BoomBoxPlayerFollow.TryGetValue(player.UserIDString, out var boombox)) return;
            CuiElementContainer mainPanel = CreatePanel(MainPanel, "Overlay", "0 0 0 0", "0 0", "1 1", "0 0", "0 0");
            AddPanel(mainPanel, MainPanel, MainBodyPanel, "0 0 0 0.9", "0 0", "1 1", "0 0", "0 0");
            AddButton(mainPanel, MainBodyPanel, ButtonCUI, "", 14, "0 0 0 0", "0 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} close", "0 0", "1 1", "0 0", "0 0");
            AddPanel(mainPanel, MainBodyPanel, MainPanelSub, "0.192 0.180 0.184 0.9", "0.2 0.15", "0.8 0.85", "0 0", "0 0");
            AddImageUrl(mainPanel, MainPanelSub, "SoundBox.ImageBackground", BackgroundImage, "1 1 1 1", "0 0", "1 1");
            AddPanel(mainPanel, MainPanelSub, "SoundBox.Line", "0.541 0 0.768 1", "0.05 0.883", "0.95 0.889", "0 0", "0 0");
            CuiHelper.AddUi(player, mainPanel);
            if (string.IsNullOrEmpty(config)) config = configData.genres.lists.Keys.First();
            BuildHeader(player, config);
            BuildAdminList(player, config, page);
            BuildHeaderButtons(player, config, page);
            PlayButton(player, boombox);
        }
        private void PlayButton(BasePlayer player, FollowInfo box) {
            CuiElementContainer mainPanel = CreatePanel(MainPanelPlay, MainPanelSub, "0 0 0 0", "0 0", "1 0.1", "0 0", "0 0");
            AddButton(mainPanel, MainPanelPlay, MainPanelPlayPanel, "", 0, "0 0 0 0", "0 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} toggleplay", "0.48 0.25", "0.52 0.75");
            CreateElement(mainPanel, MainPanelPlayPanel, $"SoundBox.Play", box.boombox.IsOn() ? "assets/icons/occupied.png" : "assets/icons/maximum.png", "0 0", "1 1", box.boombox.IsOn() ? "0.8 0 0 1" : "0.549 0.725 0.239 1", "");
            CuiHelper.AddUi(player, mainPanel);
        }
        private void BuildHeader(BasePlayer player, string config) {
            CuiElementContainer mainPanel = CreatePanel(MainPanelNav, MainPanelSub, "0.541 0 0.768 1", "0 0.95", "1 1");
            AddButton(mainPanel, MainPanelNav, "AdminPanel.Close2", lang.GetMessage("ExitMenu", this, player.UserIDString), 15, "1 1 0 1", "0 0 0 0", TextAnchor.MiddleRight, $"{command} {SecretKey} close", "0.8 0", "0.99 1");
            AddButton(mainPanel, MainPanelNav, "AdminPanel.Current", lang.GetMessage("SoundBox", this, player.UserIDString), 15, "1 1 0 1", "0 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey}", "0.4 0", "0.6 1");
            CuiHelper.AddUi(player, mainPanel);
        }
        private void BuildHeaderButtons(BasePlayer player, string config, int page = 1) {
            CuiElementContainer mainPanel = CreatePanel(MainPanelJon, MainPanelSub, "0 0 0 0", "0.09 0.897", "0.98 0.942");
            bool HasAdmin = permission.UserHasPermission(player.UserIDString, permAdmin);
            if (HasAdmin || configData.settings2.TotalAllowedPlayerUrls > 0) {
                AddButton(mainPanel, MainPanelNav, $"SoundBox.GearButton", "", 0, "1 1 1 1", "0.8 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} addurl {page - 1} {config}", "0.01 0.18", "0.03 0.82");
                CreateElement(mainPanel, $"SoundBox.GearButton", $"SoundBox.Gear", "assets/icons/gear.png", "0 0", "1 1", "1 1 1 1", "");
            }
            double a = 0, b = 0, c = 0.15, d = 1;
            int total = 0, count = 0;
            if (page + 6 > configData.genres.lists.Count) page = configData.genres.lists.Count - 5;
            else AddButton(mainPanel, MainPanelJon, $"SoundBox.pagenext", ">>", 20, "1 1 1 1", "0.8 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} pagenext {page + 1} {config}", "0.93 0", "0.96 1", "0 0", "0 0");
            if (page > 1) AddButton(mainPanel, MainPanelJon, $"SoundBox.pageprev", "<<", 20, "1 1 1 1", "0.8 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} pagenext {page - 1} {config}", "-0.035 0", "-0.005 1", "0 0", "0 0");
            foreach (var item in configData.genres.lists) {
                count++;
                if (count < page) continue;
                total++;
                AddButton(mainPanel, MainPanelJon, $"SoundBox.genres{total}", lang.GetMessage(item.Key, this, player.UserIDString), 15, "1 1 1 1", item.Key == config ? "0.054 0.529 0.8 1" : "0.549 0.725 0.239 0.85", TextAnchor.MiddleCenter, $"{command} {SecretKey} genres {page} {item.Key}", $"{a} {b}", $"{c} {d}", "0 0", "0 0");
                a += 0.155;
                c += 0.155;
                if (total > 5) break;
            }
            CuiHelper.AddUi(player, mainPanel);
        }
        private void BuildAdminList(BasePlayer player, string config, int page = 1) {
            CuiElementContainer mainPanel = CreatePanel(MainPanelSubSide, MainPanelSub, "0 0 0 0", "0 0.1", "1 0.85", "0 0", "0 0");
            AddPanel(mainPanel, MainPanelSubSide, MainPanelScroll, "0.760 0.760 0.760 0", "0 0", "1 1", "0 0", "0 0");
            //Needed to make mouse scrole work Heck If i Know Why
            Add_CuiScrollView(mainPanel, MainPanelScroll1, MainPanelScroll, "0 0", "1 1", "0 -5000", "0 0");
            AddPanel(mainPanel, MainPanelScroll1, MainPanelScroll2, "0.760 0.760 0.760 0", "0 0", "1 1", "0 0", "0 0");
            bool HasAdmin = permission.UserHasPermission(player.UserIDString, permAdmin);
            double a = 0.07, b = 0.996, c = 0.31, d = 1;
            int count = 0;
            int total = 0;
            if (!TryLoadUrlList($"PlayerSaves/{player.UserIDString}", out var data)) {
                data.SavedURLS = new Dictionary<string, List<genresInfo>>();
                SaveData($"PlayerSaves/{player.UserIDString}", data);
            }
            string code = GetLanfPlayer(player);
            if (data.SavedURLS.TryGetValue(config, out var list)) {
                foreach (var item in list) {
                    total++;
                    AddButton(mainPanel, MainPanelScroll2, $"SoundBox.url1{total}", $"{item.urlName}", 15, "1 1 1 1", "0.8 0.403 0.054 1", TextAnchor.MiddleCenter, $"{command} {SecretKey} station {item.URL} {item.urlName}", $"{a} {b}", $"{c} {d}", "0 0", "0 0");
                    AddButton(mainPanel, $"SoundBox.url1{total}", $"SoundBox.delete1{total}", "x", 9, "1 1 1 1", "0.8 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} userdelete {total - 1} {page} {config}", "0.95 0.5", "1 1", "0 0", "0 0");
                    count++;
                    a += 0.3;
                    c += 0.3;
                    if (count == 3) {
                        a = 0.07;
                        b -= 0.006;
                        c = 0.31;
                        d -= 0.006;
                        count = 0;
                    }
                }
            }
            total = 0;
            foreach (var item in configData.genres.lists[config]) {
                total++;
                if (!string.IsNullOrEmpty(item.CountryCode) && item.CountryCode != code) continue;
                AddButton(mainPanel, MainPanelScroll2, $"SoundBox.urlList{total}", $"{item.urlName}", 15, "1 1 1 1", "0.8 0.403 0.054 1", TextAnchor.MiddleCenter, $"{command} {SecretKey} station {item.URL}", $"{a} {b}", $"{c} {d}", "0 0", "0 0");
                if (HasAdmin) AddButton(mainPanel, $"SoundBox.urlList{total}", $"SoundBox.KillButton{total}", "x", 9, "1 1 1 1", "0.8 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} admindelete {total - 1} {page} {config}", "0.95 0.5", "1 1", "0 0", "0 0");
                count++;
                a += 0.3;
                c += 0.3;
                if (count == 3) {
                    a = 0.07;
                    b -= 0.006;
                    c = 0.31;
                    d -= 0.006;
                    count = 0;
                }
            }
            total = 0;
            if (TryLoadUrlCountry($"StreamLists/{GetLanfPlayer(player)}/{config}", out var dataList)) {
                foreach (var crap in dataList) {
                    total++;
                    AddButton(mainPanel, MainPanelScroll2, $"SoundBox.urlList{total}", $"{crap.name}", 15, "1 1 1 1", "0.8 0.403 0.054 1", TextAnchor.MiddleCenter, $"{command} {SecretKey} station {crap.url}", $"{a} {b}", $"{c} {d}", "0 0", "0 0");
                    count++;
                    a += 0.3;
                    c += 0.3;
                    if (count == 3) {
                        a = 0.07;
                        b -= 0.006;
                        c = 0.31;
                        d -= 0.006;
                        count = 0;
                    }
                }
            }
            CuiHelper.AddUi(player, mainPanel);
        }
        private void BuildAddUrl(BasePlayer player, string config, int page) {
            bool HasAdmin = permission.UserHasPermission(player.UserIDString, permAdmin);
            CuiElementContainer mainPanel = CreatePanel(MainPanel, "Overlay", "0 0 0 0", "0 0", "1 1", "0 0", "0 0");
            AddPanel(mainPanel, MainPanel, MainBodyPanel, "0 0 0 0.9", "0 0", "1 1", "0 0", "0 0");
            AddButton(mainPanel, MainBodyPanel, ButtonCUI, "", 14, "0 0 0 0", "0 0 0 0", TextAnchor.MiddleCenter, $"{command} {SecretKey} closeadd {page} {config}", "0 0", "1 1", "0 0", "0 0");
            AddPanel(mainPanel, MainBodyPanel, MainPanelSub, "0.054 0.529 0.8 1", "0.2 0.15", "0.8 0.85", "0 0", "0 0");
            CreateLable(mainPanel, MainPanelSub, "SoundBox.Information", lang.GetMessage("AddUrl", this, player.UserIDString), 18, "1 1 1 1", TextAnchor.UpperLeft, "0.2 0.5", "0.8 0.9");
            CreateLable(mainPanel, MainPanelSub, "SoundBox.Information", lang.GetMessage("AddUrlName", this, player.UserIDString), 18, "1 1 1 1", TextAnchor.MiddleLeft, "0.2 0.5", "0.5 0.55");
            AddPanel(mainPanel, MainPanelSub, "SoundBox.TextBoxName", "1 1 1 1", "0.2 0.45", "0.4 0.5", "0 0", "0 0");
            AddTextBox(mainPanel, "SoundBox.TextBoxName", "SoundBox.TextBoxName1", "0 0 0 1", "", 15, TextAnchor.MiddleLeft, 25, $"{commandname}", "0.005 0", "1 1");
            CreateLable(mainPanel, MainPanelSub, "SoundBox.InformationUrl", lang.GetMessage("AddUrLink", this, player.UserIDString), 18, "1 1 1 1", TextAnchor.MiddleLeft, "0.2 0.37", "0.5 0.42");
            AddPanel(mainPanel, MainPanelSub, "SoundBox.TextBoxUrl", "1 1 1 1", "0.2 0.32", "0.75 0.37", "0 0", "0 0");
            AddTextBox(mainPanel, "SoundBox.TextBoxUrl", "SoundBox.TextBoxUrl1", "0 0 0 1", "", 15, TextAnchor.MiddleLeft, 500, $"{commandurl}", "0.005 0", "1 1");
            AddButton(mainPanel, MainPanelSub, "SoundBox.TextBoxSave", lang.GetMessage("Save", this, player.UserIDString), 16, "1 1 1 1", "0.549 0.725 0.239 1", TextAnchor.MiddleCenter, $"{command} {SecretKey} saveurl {page} {config}", "0.2 0.15", "0.3 0.2", "0 0", "0 0");
            AddButton(mainPanel, MainPanelSub, "SoundBox.TextBoxCancel", lang.GetMessage("Cancel", this, player.UserIDString), 16, "1 1 1 1", "0.8 0 0 1", TextAnchor.MiddleCenter, $"{command} {SecretKey} closeadd {page} {config}", "0.7 0.15", "0.8 0.2", "0 0", "0 0");
            if (HasAdmin) AddButton(mainPanel, MainPanelSub, "SoundBox.TextBoxAdminSave", lang.GetMessage("AdminSave", this, player.UserIDString), 16, "1 1 1 1", "0.549 0.725 0.239 1", TextAnchor.MiddleCenter, $"{command} {SecretKey} adminsaveurl {page} {config}", "0.45 0.15", "0.55 0.2", "0 0", "0 0");
            CuiHelper.AddUi(player, mainPanel);
            BuildHeaderUrl(player, config, page);
        }
        private void BuildHeaderUrl(BasePlayer player, string config, int page) {
            CuiElementContainer mainPanel = CreatePanel(MainPanelNav, MainPanelSub, "0.541 0 0.768 1", "0 0.95", "1 1");
            AddButton(mainPanel, MainPanelNav, "AdminPanel.Close2", lang.GetMessage("ExitMenu", this, player.UserIDString), 15, "1 1 0 1", "0 0 0 0", TextAnchor.MiddleRight, $"{command} {SecretKey} closeadd {page} {config}", "0.8 0", "0.99 1");
            CreateLable(mainPanel, MainPanelNav, "SoundBox.InformationDisplay", string.Format(lang.GetMessage("AddingFor", this, player.UserIDString), config.ToUpper()), 15, "1 1 1 1", TextAnchor.MiddleCenter, "0.3 0", "0.7 1");
            AddButton(mainPanel, MainPanelNav, "AdminPanel.Current", lang.GetMessage("SoundBox", this, player.UserIDString), 15, "1 1 0 1", "0 0 0 0", TextAnchor.MiddleLeft, $"{command} {SecretKey}", "0.01 0", "0.16 1");
            CuiHelper.AddUi(player, mainPanel);
        }
#endregion
#region Ui Commands
        private void DestroyUI(BasePlayer player) {
            if (player != null) {
                CuiHelper.DestroyUi(player, MainPanel);
                if (!BoomBoxPlayerFollow.ContainsKey(player.UserIDString)) BoomBoxPlayerFollow.Remove(player.UserIDString);
            }
        }
        [ConsoleCommand(command)]
        private void UiActionCommand(ConsoleSystem.Arg arg) {
            if (arg == null || arg.Args == null || arg.Args.Length < 2) return;
            BasePlayer player = arg.Player();
            if (!ulong.TryParse(arg.Args[0], out var key)) return;
            if (key != SecretKey) return;
            if (BoomBoxPlayerFollow.TryGetValue(player.UserIDString, out var boombox) && boombox.boombox == null) {
                DestroyUI(player);
            }
            if (player != null) {
                switch (arg.Args[1]) {
                    case "close" : {
                        DestroyUI(player);
                        break;
                    }
                    case "genres" : {
                        int position = Int32.Parse(arg.Args[2]);
                        string config = string.Join(" ", arg.Args.Skip(3).ToArray()).Trim();
                        BuildHeaderButtons(player, config, position);
                        BuildAdminList(player, config, position);
                        break;
                    }
                    case "toggleplay" : {
                        boombox.boombox.ServerTogglePlay(!boombox.boombox.IsOn());
                        PlayButton(player, boombox);
                        break;
                    }
                    case "station" : {
                        ChangeUrl(boombox.boombox, arg.Args[2], player);
                        string name = string.Join(" ", arg.Args.Skip(3).ToArray()).Trim();
                        boombox.CurrentName = name;
                        boombox.CurrentUrl = arg.Args[2];
                        break;
                    }
                    case "pagenext" : {
                        int position = Int32.Parse(arg.Args[2]);
                        string config = string.Join(" ", arg.Args.Skip(3).ToArray()).Trim();
                        BuildHeaderButtons(player, config, position);
                        break;
                    }
                    case "addurl" : {
                        int position = Int32.Parse(arg.Args[2]);
                        string config = string.Join(" ", arg.Args.Skip(3).ToArray()).Trim();
                        BuildAddUrl(player, config, position);
                        break;
                    }
                    case "admindelete" : {
                        int position = Int32.Parse(arg.Args[2]);
                        int position2 = Int32.Parse(arg.Args[3]);
                        string config = string.Join(" ", arg.Args.Skip(4).ToArray()).Trim();
                        configData.genres.lists[config].RemoveAt(position);
                        SaveConfig();
                        BuildSounBoxUI(player, config, position2);
                        break;
                    }
                    case "userdelete" : {
                        int position = Int32.Parse(arg.Args[2]);
                        int position2 = Int32.Parse(arg.Args[3]);
                        string config = string.Join(" ", arg.Args.Skip(4).ToArray()).Trim();
                        if (!TryLoadUrlList($"PlayerSaves/{player.UserIDString}", out var data)) return;
                        data.SavedURLS[config].RemoveAt(position);
                        SaveData($"PlayerSaves/{player.UserIDString}", data);
                        BuildSounBoxUI(player, config, position2);
                        break;
                    }
                    case "closeadd" : {
                        int position = Int32.Parse(arg.Args[2]);
                        string config = string.Join(" ", arg.Args.Skip(3).ToArray()).Trim();
                        BuildSounBoxUI(player, config, position);
                        break;
                    }
                    case "saveurl" : {
                        if (!inputtext.TryGetValue(player.UserIDString, out var name) || string.IsNullOrEmpty(name)) {
                            SendReply(player, lang.GetMessage("errorName", this, player.UserIDString));
                            return;
                        }
                        if (!inputtexturl.TryGetValue(player.UserIDString, out var url) || string.IsNullOrEmpty(url)) {
                            SendReply(player, lang.GetMessage("errorUrl", this, player.UserIDString));
                            return;
                        }
                        foreach (var keyContain in configData.settings2.MustContain) {
                            if (!url.Contains(keyContain)) {
                                SendReply(player, lang.GetMessage("MustContain", this, player.UserIDString), keyContain);
                                return;
                            }
                        }
                        foreach (var keyNot in configData.settings2.BlockedUrls) {
                            if (url.Contains("ropbox.com")) break;
                            if (url.Contains(keyNot)) {
                                SendReply(player, lang.GetMessage("CanNotContain", this, player.UserIDString), keyNot);
                                return;
                            }
                        }
                        if (!TryLoadUrlList($"PlayerSaves/{player.UserIDString}", out var data)) data.SavedURLS = new Dictionary<string, List<genresInfo>>();
                        int position = Int32.Parse(arg.Args[2]);
                        string config = string.Join(" ", arg.Args.Skip(3).ToArray()).Trim();
                        if (!data.SavedURLS.ContainsKey(config)) data.SavedURLS.Add(config, new List<genresInfo>());
                        data.SavedURLS[config].Add(new genresInfo() {
                            urlName = name, URL = url
                        }
                        );
                        SaveData($"PlayerSaves/{player.UserIDString}", data);
                        BuildSounBoxUI(player, config, position);
                        break;
                    }
                    case "adminsaveurl" : {
                        if (!inputtext.TryGetValue(player.UserIDString, out var name) || string.IsNullOrEmpty(name)) {
                            SendReply(player, lang.GetMessage("errorName", this, player.UserIDString));
                            return;
                        }
                        if (!inputtexturl.TryGetValue(player.UserIDString, out var url) || string.IsNullOrEmpty(url)) {
                            SendReply(player, lang.GetMessage("errorUrl", this, player.UserIDString));
                            return;
                        }
                        int position = Int32.Parse(arg.Args[2]);
                        string config = string.Join(" ", arg.Args.Skip(3).ToArray()).Trim();
                        configData.genres.lists[config].Add(new genresInfo() {
                            URL = url, urlName = name, CountryCode = GetLanfPlayer(player)
                        }
                        );
                        SaveConfig();
                        BuildSounBoxUI(player, config, position);
                        break;
                    }
                    default : break;
                }
            }
        }
#endregion
#region UI Helpers
        private Dictionary<string, string> inputtext = new Dictionary<string, string>();
        [ConsoleCommand(commandname)]
        private void InputTextCallback(ConsoleSystem.Arg arg) {
            BasePlayer player = arg.Player();
            if (arg.Args.Length <= 0) {
                if (inputtext.ContainsKey(player.UserIDString)) inputtext.Remove(player.UserIDString);
                return;
            }
            if (inputtext.ContainsKey(player.UserIDString)) inputtext[player.UserIDString] = string.Join(" ", arg.Args);
            else inputtext.Add(player.UserIDString, string.Join(" ", arg.Args));
        }
        private Dictionary<string, string> inputtexturl = new Dictionary<string, string>();
        [ConsoleCommand(commandurl)]
        private void InputTextCallbackurl(ConsoleSystem.Arg arg) {
            BasePlayer player = arg.Player();
            if (arg.Args.Length <= 0) {
                if (inputtexturl.ContainsKey(player.UserIDString)) inputtexturl.Remove(player.UserIDString);
                return;
            }
            if (inputtexturl.ContainsKey(player.UserIDString)) inputtexturl[player.UserIDString] = string.Join(" ", arg.Args);
            else inputtexturl.Add(player.UserIDString, string.Join(" ", arg.Args));
        }
        private static CuiElementContainer CreatePanel(string panelName, string parent, string color, string AnchorMin = "0.5 0", string AnchorMax = "0.5 0", string OffsetMin = "0 0", string OffsetMax = "0 0") {
            return new CuiElementContainer {
                new CuiElement {
                    Parent = parent, Name = panelName, DestroyUi = panelName, Components = {
                        new CuiImageComponent {
                            Color = color
                        }
                        , new CuiRectTransformComponent {
                            AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax,
                        }
                    }
                }
            }
            ;
        }
        private static void AddPanel(CuiElementContainer container, string panelName, string panelButton, string color = "0.33 0.33 0.33 0.90", string AnchorMin = "0 0", string AnchorMax = "0 0", string OffsetMin = "-400 -200", string OffsetMax = "400 200") {
            container.Add(new CuiPanel {
                CursorEnabled = true, Image = {
                    Color = color
                }
                , RectTransform = {
                    AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax,
                }
            }
            , panelName, panelButton, panelButton);
        }
        private static void CreateCountdown(CuiElementContainer container, string parent, int time, string text = "%TIME_LEFT%", int size = 12, string command = "", string color = "0.60 255 0 0.68", string AnchorMin = "0.5 0", string AnchorMax = "0.5 0", string OffsetMin = "0 0", string OffsetMax = "0 0") {
            container.Add(new CuiElement {
                Parent = parent, Components = {
                    new CuiCountdownComponent {
                        StartTime = time, EndTime = 0, Step = 1, Command = command
                    }
                    , new CuiTextComponent {
                        Text = $"{text}", FontSize = size, Align = TextAnchor.MiddleCenter, Font = "robotocondensed-regular.ttf", Color = color
                    }
                    , new CuiRectTransformComponent {
                        AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax
                    }
                    ,
                }
            }
            );
        }
        private static void CreateLable(CuiElementContainer container, string parent, string panelN, string message, int size, string color, TextAnchor anchor, string ancorMin, string ancorMax, string OffsetMin = "0 0", string OffsetMax = "0 0") {
            container.Add(new CuiLabel {
                Text = {
                    Text = message, FontSize = size, Align = anchor, Color = color, Font = "RobotoCondensed-Bold.ttf"
                }
                , RectTransform = {
                    AnchorMin = ancorMin, AnchorMax = ancorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax
                }
            }
            , parent, panelN);
        }
        private static void CreateElement(CuiElementContainer container, string parent, string name, string png, string ancorMin, string ancorMax, string color, string material = "assets/content/ui/uibackgroundblur-ingamemenu.mat") {
            container.Add(new CuiElement {
                Parent = parent, Name = name, DestroyUi = name, Components = {
                    new CuiImageComponent {
                        Sprite = png, Color = color
                    }
                    , new CuiRectTransformComponent {
                        AnchorMin = ancorMin, AnchorMax = ancorMax
                    }
                }
            }
            );
        }
        private static void AddButton(CuiElementContainer container, string panelName, string panelButton, string text, int testSize, string colorT, string colorB, TextAnchor anchor = TextAnchor.MiddleCenter, string usaageCommand = "", string AnchorMin = "0 0", string AnchorMax = "0 0", string OffsetMin = "0 0", string OffsetMax = "0 0") {
            container.Add(new CuiButton {
                Text = {
                    Text = text, FontSize = testSize, Align = anchor, Color = colorT
                }
                , Button = {
                    Command = usaageCommand, Color = colorB
                }
                , RectTransform = {
                    AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax,
                }
            }
            , panelName, panelButton);
        }
        private static void AddTextBox(CuiElementContainer container, string parent, string panelName, string Color, string text, int size, TextAnchor Align, int charsLimit, string command, string AnchorMin = "0 0", string AnchorMax = "0 0", string OffsetMin = "0 0", string OffsetMax = "0 0") {
            container.Add(new CuiElement {
                Name = panelName, Parent = parent, Components = {
                    new CuiInputFieldComponent {
                        NeedsKeyboard = true, Text = text, CharsLimit = charsLimit, Color = Color, IsPassword = false, Command = command, Font = "robotocondensed-regular.ttf", FontSize = size, Align = Align
                    }
                    , new CuiRectTransformComponent {
                        AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax
                    }
                }
            }
            );
        }
        private static void AddImage(CuiElementContainer container, string parent, string panelName, int itemID, ulong skinID, string color, string AnchorMin = "0 0", string AnchorMax = "1 1", string OffsetMin = "0 0", string OffsetMax = "0 0") {
            container.Add(new CuiElement {
                Name = panelName, Parent = parent, Components = {
                    new CuiRectTransformComponent {
                        AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax
                    }
                    , new CuiImageComponent {
                        ItemId = itemID, SkinId = skinID, Color = color
                    }
                }
            }
            );
        }
        private static void AddImageUrl(CuiElementContainer container, string parent, string panelName, string image, string ColorB, string AnchorMin = "0 0", string AnchorMax = "1 1", string OffsetMin = "0 0", string OffsetMax = "0 0") {
            container.Add(new CuiElement {
                Name = panelName, Parent = parent, DestroyUi = panelName, Components = {
                    new CuiRawImageComponent {
                        Png = image, Color = ColorB
                    }
                    , new CuiRectTransformComponent {
                        AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax
                    }
                    ,
                }
            }
            );
        }
        private static void AddImageAvatar(CuiElementContainer container, string parent, string panelName, string image, string ColorB, string AnchorMin = "0 0", string AnchorMax = "1 1", string OffsetMin = "0 0", string OffsetMax = "0 0") {
            container.Add(new CuiElement {
                Name = panelName, Parent = parent, DestroyUi = panelName, Components = {
                    new CuiRawImageComponent {
                        SteamId = image, Color = ColorB
                    }
                    , new CuiRectTransformComponent {
                        AnchorMin = AnchorMin, AnchorMax = AnchorMax, OffsetMin = OffsetMin, OffsetMax = OffsetMax
                    }
                    ,
                }
            }
            );
        }
        private void Add_CuiScrollView(CuiElementContainer container, string name, string parent, string contentAnchorMin, string contentAnchorMax, string contentOffsetMin, string contentOffsetMax) {
            container.Add(new CuiElement {
                Name = name, Parent = parent, DestroyUi = name, Components = {
                    new CuiScrollViewComponent {
                        Vertical = true, Horizontal = false, ScrollSensitivity = 5f, ContentTransform = new CuiRectTransform {
                            AnchorMin = contentAnchorMin, AnchorMax = contentAnchorMax, OffsetMin = contentOffsetMin, OffsetMax = contentOffsetMax
                        }
                        , VerticalScrollbar = new CuiScrollbar {
                            Invert = false, TrackColor = "0 0 0 0.5", HandleColor = "0.760 0.760 0.760 0.2", HighlightColor = "0.760 0.760 0.760 0.2", PressedColor = "0.760 0.760 0.760 0.2",
                        }
                    }
                }
            }
            );
        }
#endregion
#region Search For Stream
        public List<string> country = new List<string>();
        private IEnumerator DownloadSound(string info = "http://89.58.16.19/json/stations") {
            Dictionary<string, List<netRadio>> list = Pool.Get<Dictionary<string, List<netRadio>>>();
            Dictionary<string, Dictionary<string, List<netRadio>>> listType = Pool.Get<Dictionary<string, Dictionary<string, List<netRadio>>>>();
            UnityWebRequest www = UnityWebRequest.Get(info);
            yield return www.SendWebRequest();
            if (www.isNetworkError || www.isHttpError) {
                PrintWarning(string.Format("Failed to download! Error: {0}", www.error));
                www.Dispose();
                yield break;
            }
            if (www?.downloadHandler?.data != null) {
                byte[] bytes = www.downloadHandler.data;
                yield return new WaitForSeconds(1);
                if (bytes.Length > 0) {
                    var json = JsonConvert.DeserializeObject<List<urlClass>>(www.downloadHandler.text);
                    if (json != null) {
                        foreach (var key in json) {
                            if (!string.IsNullOrEmpty(key.url_resolved) && !string.IsNullOrEmpty(key.name) && !string.IsNullOrEmpty(key.countrycode) && !string.IsNullOrEmpty(key.codec) && key.codec == "MP3") {
                                string code = key.countrycode.ToLower();
                                if (!list.ContainsKey(code)) list.Add(code, new List<netRadio>());
                                list[code].Add(new netRadio() {
                                    name = key.name, url = key.url_resolved, tags = key.tags
                                }
                                );
                                if (!string.IsNullOrEmpty(key.tags)) {
                                    foreach (var configKey in configData.genres.lists) {
                                        if (key.tags.Contains(configKey.Key, StringComparison.OrdinalIgnoreCase)) {
                                            if (!listType.ContainsKey(code)) listType.Add(code, new Dictionary<string, List<netRadio>>());
                                            if (!listType[code].ContainsKey(configKey.Key)) listType[code].Add(configKey.Key, new List<netRadio>());
                                            listType[code][configKey.Key].Add(new netRadio() {
                                                name = key.name, url = key.url_resolved, tags = key.tags
                                            }
                                            );
                                        }
                                    }
                                }
                            }
                        }
                        PrintWarning("Finished download 
// ... [Code preview truncated due to length. Full source in attached file] ...

Автор
15FPS
Скачиваний
0
Просмотры
1
Первый выпуск
Обновление
Оценка
0.00 звёзд 0 оценок
Сверху