using System; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using UnityEngine; namespace Ca2d.Toolkit { public static class Guard { /* IgnoreException Utils */ #region IgnoreException(Action) public static void IgnoreException(Action exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } } public static T IgnoreException(Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async UniTask IgnoreException(Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } } public static async UniTask IgnoreException(Func> exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async Task IgnoreException(Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } } public static async Task IgnoreException(Func> exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async ValueTask IgnoreException(Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } } public static async ValueTask IgnoreException(Func> exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(this Source, Action) public static void IgnoreException(TSource source, Action exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } } public static T IgnoreException(TSource source, Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async UniTask IgnoreException(TSource source, Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } } public static async UniTask IgnoreException(TSource source, Func> exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async Task IgnoreException(TSource source, Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } } public static async Task IgnoreException(TSource source, Func> exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async ValueTask IgnoreException(this TSource source, Func exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } } public static async ValueTask IgnoreException(this TSource source, Func> exec) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(Action, P1) public static void IgnoreException(Action exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } } public static T IgnoreException(Func exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async UniTask IgnoreException(Func exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } } public static async UniTask IgnoreException(Func> exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async Task IgnoreException(Func exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } } public static async Task IgnoreException(Func> exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } return default(T); } public static async ValueTask IgnoreException(Func exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } } public static async ValueTask IgnoreException(Func> exec, TP1 p1) { try { if (exec == null) throw new ArgumentNullException( nameof(exec), "Ignore Exception should run with executor!"); return await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion /* UnityObject Related */ #region RequireComponent public static bool ValidateOptional(this Option opt, out T component) where T : Component { if (opt.Enabled) { if (opt.Value == null) throw new Exception(); component = opt.Value; return true; } component = null; return false; } public static GameObject RequestComponent(this GameObject go, out T component) where T : Component { if (!go.TryGetComponent(out component)) { component = go.AddComponent(); } return go; } public static GameObject RequestComponent(this TSrc source, out T component) where TSrc : Component where T : Component { var go = source.gameObject; if (!go.TryGetComponent(out component)) { component = go.AddComponent(); } return go; } public static GameObject RequireComponent(this GameObject go, out T component) { if (!go.TryGetComponent(out component)) { throw new Exception(); } return go; } public static TSrc RequireComponent(this TSrc source, out T component) where TSrc : Component { if (!source.TryGetComponent(out component)) { throw new Exception(); } return source; } public static T GetOrAddComponent(this GameObject go) where T : Component { if (!go.TryGetComponent(out var comp)) { comp = go.AddComponent(); } return comp; } public static T GetOrAddComponent(this Component component) where T : Component { var go = component.gameObject; if (!go.TryGetComponent(out var comp)) { comp = go.AddComponent(); } return comp; } #endregion } }