using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; namespace Funique { /// /// Tool collection
/// ------------------------------------------------
/// 工具組 ///
public sealed class Tool : IToolUtility { // 屬性 #region Property internal const string WorkerName = "ffmpeg"; /// /// Get the environemnt variable "ffmpeg_path" to access exe file path
/// ------------------------------------------------
/// 存取系統使者者環境變數 "ffmpeg_path" ffmpeg.exe 執行檔案 檔案路徑 ///
internal static string ffmpegPath { get { return Environment.GetEnvironmentVariable("ffmpeg_path", EnvironmentVariableTarget.User); } } #endregion /// /// Singleton for tool
/// ------------------------------------------------
/// 唯一工具物件 ///
public static IToolUtility Instance { get { if (_Instance == null) _Instance = new Tool(); return _Instance; } } static Tool _Instance = null; #region Interface: IToolUtility public bool CheckSameVideoName(VideoInfo[] videoList, string videoName) => videoList.CheckSameVideoName(videoName); public bool CheckSameVideoName(List videoList, string videoName) => videoList.CheckSameVideoName(videoName); public string FilterVideoName(string videoSourceName) => videoSourceName.FilterVideoName(); public float FilterVideoTime(string VideoSourceName) => VideoSourceName.FilterVideoTime(); public double TranslateTime(string timeString) => timeString.TranslateTime(); public string FilterBtnName(string VideoSourceName) => VideoSourceName.FilterBtnName(); public string MergeTime(string name, double time) => name.MergeTime(time); public string DetectStereoMode(string videoFilterName) => videoFilterName.DetectStereoMode(); public int RandomID(Dictionary PlayerInfoDic, int memberLimit) => PlayerInfoDic.RandomID(memberLimit); public string RandomIP(Dictionary PlayerInfoDic, int memberLimit) => PlayerInfoDic.RandomIP(memberLimit); public bool CheckRepeatID(Dictionary PlayerInfoDic, int ID) => PlayerInfoDic.CheckRepeatID(ID); public bool CheckRepeatIP(Dictionary PlayerInfoDic, string IP) => PlayerInfoDic.CheckRepeatIP(IP); public bool CheckRaycast(string tag) => tag.CheckRaycast(); public IList MoveListItem(IList list, int indexA, int indexB) => list.MoveListItem(indexA, indexB); public Vector3 GetMenuUIPosition(Transform canvas, Vector2 mousePosition) => canvas.GetMenuUIPosition(mousePosition); public bool DetectInWindows(RectTransform target) => target.DetectInWindows(); public string GetSelfIPAddress() => UtilityExtensions.GetSelfIPAddress(); public string[] GetSelfIPAddresses() => UtilityExtensions.GetSelfIPAddresses(); public void AddBtnEvent(GameObject BtnObj, UnityAction BtnAction, EventTriggerType TriggerType) { EventTrigger trigger; if (BtnObj.GetComponent() == null) { trigger = BtnObj.AddComponent(); } else { trigger = BtnObj.GetComponent(); } EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = TriggerType; entry.callback.AddListener((eventData) => { BtnAction(); }); trigger.triggers.Add(entry); } public void AddBtnEvent(GameObject BtnObj, UnityAction BtnAction, T ActionString, EventTriggerType TriggerType) { EventTrigger trigger; if (BtnObj.GetComponent() == null) { trigger = BtnObj.AddComponent(); } else { trigger = BtnObj.GetComponent(); } EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = TriggerType; entry.callback.AddListener((eventData) => { BtnAction(ActionString); }); trigger.triggers.Add(entry); } public Texture2D TextureCompression(Texture2D texture, float rate) { int num = (int)((float)texture.width * rate); int num2 = (int)((float)texture.height * rate); Texture2D texture2D = new Texture2D(num, num2, texture.format, mipChain: false); Graphics.CopyTexture(texture, 0, texture.mipmapCount, 0, 0, texture.width, texture.height, texture2D, 0, texture2D.mipmapCount, texture2D.width, texture2D.height); texture2D.Apply(); return texture2D; } public void ObjShowInHMD(Transform header, Transform objTrans, float Len, float offsetAngles) { header.eulerAngles += new Vector3(0f, offsetAngles, 0f); objTrans.position = header.position + header.forward * Len; objTrans.position = new Vector3(objTrans.position.x, header.position.y, objTrans.position.z); } public Sprite ReadImg(string imageName) { Texture2D texture2D = ReadTexture(imageName); return Sprite.Create(texture2D, new Rect(0f, 0f, texture2D.width, texture2D.height), Vector2.zero); } public Texture2D ReadTexture(string imageName, string imgFolder = null) { Texture2D texture2D = new Texture2D(1, 1, TextureFormat.RGBA32, mipChain: false); byte[] array = new byte[0]; string text = string.Empty; if (!string.IsNullOrEmpty(imgFolder)) { text = Path.Combine(Application.streamingAssetsPath, imgFolder, imageName); } else { string buffer = string.Empty; if (FileSearch(Application.streamingAssetsPath + "/FuniqueImage/", imageName, out buffer)) { text = buffer; } } if (File.Exists(text)) { array = File.ReadAllBytes(text); } if (array == null) return null; texture2D.LoadImage(array); return texture2D; } public string FFmpeg(FFmpegPlace place, string arg) { switch (place) { case FFmpegPlace.None: return string.Empty; default: case FFmpegPlace.StreamingAsset: return arg.FFmpeg_StreamingAsset(); case FFmpegPlace.environmentVariable: return arg.FFmpeg(); } } public void FFmpegSetVideoInfo(ref VideoInfo video, FFmpegPlace place) => video.FFmpegSetVideoInfo(place); public double FFmpegDurationTime(string ffmpegMsg) => ffmpegMsg.FFmpegDurationTime(); public string FFmpegAudioChannel(string ffmpegMsg) => ffmpegMsg.FFmpegAudioChannel(); public bool FileSearch(string sDir, string fileName) => sDir.FileSearch(fileName); public bool FileSearch(string sDir, string fileName, out string fullpath) => sDir.FileSearch(fileName, out fullpath); public void ResetCalculation(GameObject targetObj, float len) => targetObj.ResetCalculation(len); public IPAddress[] GetIPRange(IPAddress p1, IPAddress p2, int mask) { uint pp1 = BitConverter.ToUInt32(p1.GetAddressBytes().Reverse().ToArray(), 0); uint pp2 = BitConverter.ToUInt32(p2.GetAddressBytes().Reverse().ToArray(), 0); uint pre = pp1 >> mask << mask; uint sub1 = pp1 << mask >> mask; uint sub2 = pp2 << mask >> mask; uint min = Math.Min(sub1, sub2); uint max = Math.Max(sub1, sub2); List result = new List(); for (uint i = min; i < max; i++) { result.Add(new IPAddress(BitConverter.GetBytes(pre + i).Reverse().ToArray())); } return result.ToArray(); } #endregion public bool DetectInWindows(GameObject target) { float width = target.GetComponent().sizeDelta.x; float posX = target.GetComponent().anchoredPosition.x + width; Vector2 TopCanvas = target.GetComponentInParent().GetComponent().sizeDelta; if (posX > TopCanvas.x) return false; else return true; } public Texture2D ReadTexture(string imageName, ref Texture2D tex) { // Texture2D tex = new Texture2D(1, 1, TextureFormat.RGBA32, false); byte[] bytes; string imagePath = imageName; bool find = File.Exists(imageName); if (find) FuniqueLogger.Log($"Image found! {imageName}", "Funique Core"); else FuniqueLogger.WarningLog($"Image cannot be find: {imageName}", "Funique Core"); bytes = File.ReadAllBytes(imagePath); tex.LoadImage(bytes, true); if (bytes == null) { FuniqueLogger.ErrorLog($"bytes = null", "Funique Core"); } return tex; } public void AddEventTrigger(GameObject Object, Action TriggerAction, EventTriggerType TriggerType) { EventTrigger trigger; if (Object.GetComponent() == null) { trigger = Object.AddComponent(); } else { trigger = Object.GetComponent(); } EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = TriggerType; entry.callback.AddListener((eventData) => { TriggerAction(); }); trigger.triggers.Add(entry); } } }