using System; using System.Threading.Tasks; using Cysharp.Threading.Tasks; using UnityEngine; namespace Ca2d.Toolkit { public static class Guard { /* IgnoreException Utils */ private static void IgnoreNoTarget() { Debug.LogError("Ignore Exception should run with executor!"); } #region IgnoreException(Action) /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method public static void IgnoreException(Action exec) { if (exec == null) { IgnoreNoTarget(); return; } try { exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method public static T IgnoreException(Func exec) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } return default(T); } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method public static async UniTask IgnoreExceptionAsync(Func exec) { if (exec == null) { IgnoreNoTarget(); return; } try { await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method public static async UniTask IgnoreExceptionAsync(Func> exec) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return await exec.Invoke(); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(this Source, Action) /// /// Ignore s raised with this method and log them into console directly. /// /// The source of this operation /// Executor of source executor public static void IgnoreException(this TSource source, Action exec) { if (exec == null) { IgnoreNoTarget(); return; } try { exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// The source of this operation /// Executor of source executor public static T IgnoreException(this TSource source, Func exec) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } return default(T); } /// /// Ignore s raised with this method and log them into console directly. /// /// The source of this operation /// Executor of source executor public static async UniTask IgnoreExceptionAsync(this TSource source, Func exec) { if (exec == null) { IgnoreNoTarget(); return; } try { await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// The source of this operation /// Executor of source executor public static async UniTask IgnoreExceptionAsync(this TSource source, Func> exec) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return await exec.Invoke(source); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(Action, P1) /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method public static void IgnoreException(Action exec, TP1 p1) { if (exec == null) { IgnoreNoTarget(); return; } try { exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method public static T IgnoreException(Func exec, TP1 p1) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } return default(T); } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method public static async UniTask IgnoreExceptionAsync(Func exec, TP1 p1) { if (exec == null) { IgnoreNoTarget(); return; } try { await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method public static async UniTask IgnoreExceptionAsync(Func> exec, TP1 p1) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return await exec.Invoke(p1); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(Action, P1, P2) /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method public static void IgnoreException(Action exec, TP1 p1, TP2 p2) { if (exec == null) { IgnoreNoTarget(); return; } try { exec.Invoke(p1, p2); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method public static T IgnoreException(Func exec, TP1 p1, TP2 p2) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return exec.Invoke(p1, p2); } catch (Exception e) { Debug.LogError(e); } return default(T); } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method public static async UniTask IgnoreExceptionAsync(Func exec, TP1 p1, TP2 p2) { if (exec == null) { IgnoreNoTarget(); return; } try { await exec.Invoke(p1, p2); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method public static async UniTask IgnoreExceptionAsync( Func> exec, TP1 p1, TP2 p2) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return await exec.Invoke(p1, p2); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(Action, P1, P2, P3) /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method public static void IgnoreException( Action exec, TP1 p1, TP2 p2, TP3 p3) { if (exec == null) { IgnoreNoTarget(); return; } try { exec.Invoke(p1, p2, p3); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method public static T IgnoreException( Func exec, TP1 p1, TP2 p2, TP3 p3) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return exec.Invoke(p1, p2, p3); } catch (Exception e) { Debug.LogError(e); } return default(T); } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method public static async UniTask IgnoreExceptionAsync( Func exec, TP1 p1, TP2 p2, TP3 p3) { if (exec == null) { IgnoreNoTarget(); return; } try { await exec.Invoke(p1, p2, p3); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method public static async UniTask IgnoreExceptionAsync( Func> exec, TP1 p1, TP2 p2, TP3 p3) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return await exec.Invoke(p1, p2, p3); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(Action, P1, P2, P3, P4) /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method public static void IgnoreException( Action exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4) { if (exec == null) { IgnoreNoTarget(); return; } try { exec.Invoke(p1, p2, p3, p4); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method public static T IgnoreException( Func exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return exec.Invoke(p1, p2, p3, p4); } catch (Exception e) { Debug.LogError(e); } return default(T); } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method public static async UniTask IgnoreExceptionAsync( Func exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4) { if (exec == null) { IgnoreNoTarget(); return; } try { await exec.Invoke(p1, p2, p3, p4); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method public static async UniTask IgnoreExceptionAsync( Func> exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return await exec.Invoke(p1, p2, p3, p4); } catch (Exception e) { Debug.LogError(e); } return default(T); } #endregion #region IgnoreException(Action, P1, P2, P3, P4, P5) /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method /// Fifth parameter of this method public static void IgnoreException( Action exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4, TP5 p5) { if (exec == null) { IgnoreNoTarget(); return; } try { exec.Invoke(p1, p2, p3, p4, p5); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method /// Fifth parameter of this method public static T IgnoreException( Func exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4, TP5 p5) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return exec.Invoke(p1, p2, p3, p4, p5); } catch (Exception e) { Debug.LogError(e); } return default(T); } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method /// Fifth parameter of this method public static async UniTask IgnoreExceptionAsync( Func exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4, TP5 p5) { if (exec == null) { IgnoreNoTarget(); return; } try { await exec.Invoke(p1, p2, p3, p4, p5); } catch (Exception e) { Debug.LogError(e); } } /// /// Ignore s raised with this method and log them into console directly. /// /// Executor of method /// First parameter os this method /// Second parameter of this method /// Third parameter of this method /// Fourth parameter of this method /// Fifth parameter of this method public static async UniTask IgnoreExceptionAsync( Func> exec, TP1 p1, TP2 p2, TP3 p3, TP4 p4, TP5 p5) { if (exec == null) { IgnoreNoTarget(); return default(T); } try { return await exec.Invoke(p1, p2, p3, p4, p5); } 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 LoseRequirementException(typeof(T)); 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 LoseRequirementException(typeof(T)); } return go; } public static TSrc RequireComponent(this TSrc source, out T component) where TSrc : Component { if (!source.TryGetComponent(out component)) { throw new LoseRequirementException(typeof(T)); } 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 } }