< Summary

Class:DCL.Helpers.UtilsDummyJsonUtilityFromArray[T]
Assembly:Utils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/Utils.cs
Covered lines:2
Uncovered lines:0
Coverable lines:2
Total lines:566
Line coverage:100% (2 of 2)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetFromJsonArray(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/Utils/Utils.cs

#LineLine coverage
 1#if UNITY_WEBGL && !UNITY_EDITOR
 2#define WEB_PLATFORM
 3#endif
 4
 5using System;
 6using System.Collections;
 7using System.Collections.Generic;
 8using System.Linq;
 9using DCL.Configuration;
 10using Newtonsoft.Json;
 11using TMPro;
 12using UnityEngine;
 13using UnityEngine.Assertions;
 14using UnityEngine.EventSystems;
 15using UnityEngine.Networking;
 16using UnityEngine.UI;
 17using Object = UnityEngine.Object;
 18
 19namespace DCL.Helpers
 20{
 21    public static class Utils
 22    {
 23        public static Dictionary<string, Material> staticMaterials;
 24
 25        public static Material EnsureResourcesMaterial(string path)
 26        {
 27            if (staticMaterials == null)
 28            {
 29                staticMaterials = new Dictionary<string, Material>();
 30            }
 31
 32            if (!staticMaterials.ContainsKey(path))
 33            {
 34                Material material = Resources.Load(path) as Material;
 35
 36                if (material != null)
 37                {
 38                    staticMaterials.Add(path, material);
 39                }
 40
 41                return material;
 42            }
 43
 44            return staticMaterials[path];
 45        }
 46
 47        public static void CleanMaterials(Renderer r)
 48        {
 49            if (r != null)
 50            {
 51                foreach (Material m in r.materials)
 52                {
 53                    if (m != null)
 54                    {
 55                        Material.Destroy(m);
 56                    }
 57                }
 58            }
 59        }
 60
 61        public static Vector2[] FloatArrayToV2List(float[] uvs)
 62        {
 63            Vector2[] uvsResult = new Vector2[uvs.Length / 2];
 64            int uvsResultIndex = 0;
 65
 66            for (int i = 0; i < uvs.Length;)
 67            {
 68                Vector2 tmpUv = Vector2.zero;
 69                tmpUv.x = uvs[i++];
 70                tmpUv.y = uvs[i++];
 71
 72                uvsResult[uvsResultIndex++] = tmpUv;
 73            }
 74
 75            return uvsResult;
 76        }
 77
 78        public static void ResetLocalTRS(this Transform t)
 79        {
 80            t.localPosition = Vector3.zero;
 81            t.localRotation = Quaternion.identity;
 82            t.localScale = Vector3.one;
 83        }
 84
 85        public static void SetToMaxStretch(this RectTransform t)
 86        {
 87            t.anchorMin = Vector2.zero;
 88            t.offsetMin = Vector2.zero;
 89            t.anchorMax = Vector2.one;
 90            t.offsetMax = Vector2.one;
 91            t.sizeDelta = Vector2.zero;
 92            t.anchoredPosition = Vector2.zero;
 93        }
 94
 95        public static void SetToCentered(this RectTransform t)
 96        {
 97            t.anchorMin = Vector2.one * 0.5f;
 98            t.offsetMin = Vector2.one * 0.5f;
 99            t.anchorMax = Vector2.one * 0.5f;
 100            t.offsetMax = Vector2.one * 0.5f;
 101            t.sizeDelta = Vector2.one * 100;
 102        }
 103
 104        public static void SetToBottomLeft(this RectTransform t)
 105        {
 106            t.anchorMin = Vector2.zero;
 107            t.offsetMin = Vector2.zero;
 108            t.anchorMax = Vector2.zero;
 109            t.offsetMax = Vector2.zero;
 110            t.sizeDelta = Vector2.one * 100;
 111        }
 112
 113        public static void ForceUpdateLayout(this RectTransform rt, bool delayed = true)
 114        {
 115            if (!rt.gameObject.activeInHierarchy)
 116                return;
 117
 118            if (delayed)
 119                CoroutineStarter.Start(ForceUpdateLayoutRoutine(rt));
 120            else
 121            {
 122                InverseTransformChildTraversal<RectTransform>(
 123                    (x) => { ForceRebuildLayoutImmediate(x); },
 124                    rt);
 125            }
 126        }
 127
 128        /// <summary>
 129        /// Reimplementation of the LayoutRebuilder.ForceRebuildLayoutImmediate() function (Unity UI API) for make it mo
 130        /// </summary>
 131        /// <param name="rectTransformRoot">Root from which to rebuild.</param>
 132        public static void ForceRebuildLayoutImmediate(RectTransform rectTransformRoot)
 133        {
 134            if (rectTransformRoot == null)
 135                return;
 136
 137            // NOTE(Santi): It seems to be very much cheaper to execute the next instructions manually than execute dire
 138            //              'LayoutRebuilder.ForceRebuildLayoutImmediate()', that theorically already contains these ins
 139            var layoutElements = rectTransformRoot.GetComponentsInChildren(typeof(ILayoutElement), true).ToList();
 140            layoutElements.RemoveAll(e => (e is Behaviour && !((Behaviour) e).isActiveAndEnabled) || e is TextMeshProUGU
 141            foreach (var layoutElem in layoutElements)
 142            {
 143                (layoutElem as ILayoutElement).CalculateLayoutInputHorizontal();
 144                (layoutElem as ILayoutElement).CalculateLayoutInputVertical();
 145            }
 146
 147            var layoutControllers = rectTransformRoot.GetComponentsInChildren(typeof(ILayoutController), true).ToList();
 148            layoutControllers.RemoveAll(e => e is Behaviour && !((Behaviour) e).isActiveAndEnabled);
 149            foreach (var layoutCtrl in layoutControllers)
 150            {
 151                (layoutCtrl as ILayoutController).SetLayoutHorizontal();
 152                (layoutCtrl as ILayoutController).SetLayoutVertical();
 153            }
 154        }
 155
 156        private static IEnumerator ForceUpdateLayoutRoutine(RectTransform rt)
 157        {
 158            yield return null;
 159
 160            InverseTransformChildTraversal<RectTransform>(
 161                (x) => { ForceRebuildLayoutImmediate(x); },
 162                rt);
 163        }
 164
 165        public static void InverseTransformChildTraversal<TComponent>(Action<TComponent> action, Transform startTransfor
 166            where TComponent : Component
 167        {
 168            if (startTransform == null)
 169                return;
 170
 171            foreach (Transform t in startTransform)
 172            {
 173                InverseTransformChildTraversal(action, t);
 174            }
 175
 176            var component = startTransform.GetComponent<TComponent>();
 177
 178            if (component != null)
 179            {
 180                action.Invoke(component);
 181            }
 182        }
 183
 184        public static void ForwardTransformChildTraversal<TComponent>(Func<TComponent, bool> action, Transform startTran
 185            where TComponent : Component
 186        {
 187            Assert.IsTrue(startTransform != null, "startTransform must not be null");
 188
 189            var component = startTransform.GetComponent<TComponent>();
 190
 191            if (component != null)
 192            {
 193                if (!action.Invoke(component))
 194                    return;
 195            }
 196
 197            foreach (Transform t in startTransform)
 198            {
 199                ForwardTransformChildTraversal(action, t);
 200            }
 201        }
 202
 203        public static T GetOrCreateComponent<T>(this GameObject gameObject) where T : Component
 204        {
 205            T component = gameObject.GetComponent<T>();
 206
 207            if (!component)
 208            {
 209                return gameObject.AddComponent<T>();
 210            }
 211
 212            return component;
 213        }
 214
 215        public static WebRequestAsyncOperation FetchTexture(string textureURL, bool isReadable, Action<Texture2D> OnSucc
 216        {
 217            //NOTE(Brian): This closure is called when the download is a success.
 218            void SuccessInternal(IWebRequestAsyncOperation request) { OnSuccess?.Invoke(DownloadHandlerTexture.GetConten
 219
 220            var asyncOp = Environment.i.platform.webRequest.GetTexture(
 221                url: textureURL,
 222                OnSuccess: SuccessInternal,
 223                OnFail: OnFail,
 224                isReadable: isReadable);
 225
 226            return asyncOp;
 227        }
 228
 229        public static bool SafeFromJsonOverwrite(string json, object objectToOverwrite)
 230        {
 231            try
 232            {
 233                JsonUtility.FromJsonOverwrite(json, objectToOverwrite);
 234            }
 235            catch (ArgumentException e)
 236            {
 237                Debug.LogError("ArgumentException Fail!... Json = " + json + " " + e.ToString());
 238                return false;
 239            }
 240
 241            return true;
 242        }
 243
 244        public static T FromJsonWithNulls<T>(string json) { return JsonConvert.DeserializeObject<T>(json); }
 245
 246        public static T SafeFromJson<T>(string json)
 247        {
 248            ProfilingEvents.OnMessageDecodeStart?.Invoke("Misc");
 249
 250            T returningValue = default(T);
 251
 252            if (!string.IsNullOrEmpty(json))
 253            {
 254                try
 255                {
 256                    returningValue = JsonUtility.FromJson<T>(json);
 257                }
 258                catch (ArgumentException e)
 259                {
 260                    Debug.LogError("ArgumentException Fail!... Json = " + json + " " + e.ToString());
 261                }
 262            }
 263
 264            ProfilingEvents.OnMessageDecodeEnds?.Invoke("Misc");
 265
 266            return returningValue;
 267        }
 268
 269        public static GameObject AttachPlaceholderRendererGameObject(Transform targetTransform)
 270        {
 271            var placeholderRenderer = GameObject.CreatePrimitive(PrimitiveType.Cube).GetComponent<MeshRenderer>();
 272
 273            placeholderRenderer.material = Resources.Load<Material>("Materials/AssetLoading");
 274            placeholderRenderer.transform.SetParent(targetTransform);
 275            placeholderRenderer.transform.localPosition = Vector3.zero;
 276            placeholderRenderer.name = "PlaceholderRenderer";
 277
 278            return placeholderRenderer.gameObject;
 279        }
 280
 281        public static void SafeDestroy(Object obj)
 282        {
 283            if (obj is Transform)
 284                return;
 285
 286#if UNITY_EDITOR
 287            if (Application.isPlaying)
 288                Object.Destroy(obj);
 289            else
 290                Object.DestroyImmediate(obj, false);
 291#else
 292                UnityEngine.Object.Destroy(obj);
 293#endif
 294        }
 295
 296        /**
 297         * Transforms a grid position into a world-relative 3d position
 298         */
 299        public static Vector3 GridToWorldPosition(float xGridPosition, float yGridPosition)
 300        {
 301            return new Vector3(
 302                x: xGridPosition * ParcelSettings.PARCEL_SIZE,
 303                y: 0f,
 304                z: yGridPosition * ParcelSettings.PARCEL_SIZE
 305            );
 306        }
 307
 308        /**
 309         * Transforms a world position into a grid position
 310         */
 311        public static Vector2Int WorldToGridPosition(Vector3 worldPosition)
 312        {
 313            return new Vector2Int(
 314                (int) Mathf.Floor(worldPosition.x / ParcelSettings.PARCEL_SIZE),
 315                (int) Mathf.Floor(worldPosition.z / ParcelSettings.PARCEL_SIZE)
 316            );
 317        }
 318
 319        public static Vector2 WorldToGridPositionUnclamped(Vector3 worldPosition)
 320        {
 321            return new Vector2(
 322                worldPosition.x / ParcelSettings.PARCEL_SIZE,
 323                worldPosition.z / ParcelSettings.PARCEL_SIZE
 324            );
 325        }
 326
 327        public static bool AproxComparison(this Color color1, Color color2, float tolerance = 0.01f) // tolerance of rou
 328        {
 329            if (Mathf.Abs(color1.r - color2.r) < tolerance
 330                && Mathf.Abs(color1.g - color2.g) < tolerance
 331                && Mathf.Abs(color1.b - color2.b) < tolerance)
 332            {
 333                return true;
 334            }
 335
 336            return false;
 337        }
 338
 339        public static T ParseJsonArray<T>(string jsonArray) where T : IEnumerable => DummyJsonUtilityFromArray<T>.GetFro
 340
 341        [Serializable]
 342        private class DummyJsonUtilityFromArray<T> where T : IEnumerable //UnityEngine.JsonUtility is really fast but ca
 343        {
 344            [SerializeField]
 345            private T value;
 346
 347            public static T GetFromJsonArray(string jsonArray)
 348            {
 5349                string newJson = $"{{ \"value\": {jsonArray}}}";
 5350                return JsonUtility.FromJson<DummyJsonUtilityFromArray<T>>(newJson).value;
 351            }
 352        }
 353
 354        private static int lockedInFrame = -1;
 355        public static bool LockedThisFrame() => lockedInFrame == Time.frameCount;
 356
 357        private static bool isCursorLocked;
 358        //NOTE(Brian): Made as an independent flag because the CI doesn't work well with the Cursor.lockState check.
 359        public static bool IsCursorLocked
 360        {
 361            get => isCursorLocked;
 362            private set
 363            {
 364                if (isCursorLocked == value) return;
 365                isCursorLocked = value;
 366                OnCursorLockChanged?.Invoke(isCursorLocked);
 367            }
 368        }
 369
 370        public static event Action<bool> OnCursorLockChanged;
 371
 372        public static void LockCursor()
 373        {
 374#if WEB_PLATFORM
 375            //TODO(Brian): Encapsulate all this mechanism to a new MouseLockController and branch
 376            //             behaviour using strategy pattern instead of this.
 377            if (IsCursorLocked)
 378            {
 379                return;
 380            }
 381#endif
 382            Cursor.visible = false;
 383            IsCursorLocked = true;
 384            Cursor.lockState = CursorLockMode.Locked;
 385            lockedInFrame = Time.frameCount;
 386
 387            EventSystem.current?.SetSelectedGameObject(null);
 388        }
 389
 390        public static void UnlockCursor()
 391        {
 392#if WEB_PLATFORM
 393            //TODO(Brian): Encapsulate all this mechanism to a new MouseLockController and branch
 394            //             behaviour using strategy pattern instead of this.
 395            if (!IsCursorLocked)
 396            {
 397                return;
 398            }
 399#endif
 400            Cursor.visible = true;
 401            IsCursorLocked = false;
 402            Cursor.lockState = CursorLockMode.None;
 403
 404            EventSystem.current?.SetSelectedGameObject(null);
 405        }
 406
 407        #region BROWSER_ONLY
 408
 409        //TODO(Brian): Encapsulate all this mechanism to a new MouseLockController and branch
 410        //             behaviour using strategy pattern instead of this.
 411        // NOTE: This should come from browser's pointerlockchange callback
 412        public static void BrowserSetCursorState(bool locked)
 413        {
 414            Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
 415
 416            IsCursorLocked = locked;
 417            Cursor.visible = !locked;
 418        }
 419
 420        #endregion
 421
 422        public static void DestroyAllChild(this Transform transform)
 423        {
 424            foreach (Transform child in transform)
 425            {
 426                Object.Destroy(child.gameObject);
 427            }
 428        }
 429
 430        public static List<Vector2Int> GetBottomLeftZoneArray(Vector2Int bottomLeftAnchor, Vector2Int size)
 431        {
 432            List<Vector2Int> coords = new List<Vector2Int>();
 433
 434            for (int x = bottomLeftAnchor.x; x < bottomLeftAnchor.x + size.x; x++)
 435            {
 436                for (int y = bottomLeftAnchor.y; y < bottomLeftAnchor.y + size.y; y++)
 437                {
 438                    coords.Add(new Vector2Int(x, y));
 439                }
 440            }
 441
 442            return coords;
 443        }
 444
 445        public static List<Vector2Int> GetCenteredZoneArray(Vector2Int center, Vector2Int size)
 446        {
 447            List<Vector2Int> coords = new List<Vector2Int>();
 448
 449            for (int x = center.x - size.x; x < center.x + size.x; x++)
 450            {
 451                for (int y = center.y - size.y; y < center.y + size.y; y++)
 452                {
 453                    coords.Add(new Vector2Int(x, y));
 454                }
 455            }
 456
 457            return coords;
 458        }
 459
 460        public static void DrawRectGizmo(Rect rect, Color color, float duration)
 461        {
 462            Vector3 tl2 = new Vector3(rect.xMin, rect.yMax, 0);
 463            Vector3 bl2 = new Vector3(rect.xMin, rect.yMin, 0);
 464            Vector3 tr2 = new Vector3(rect.xMax, rect.yMax, 0);
 465            Vector3 br2 = new Vector3(rect.xMax, rect.yMin, 0);
 466
 467            Debug.DrawLine(tl2, bl2, color, duration);
 468            Debug.DrawLine(tl2, tr2, color, duration);
 469            Debug.DrawLine(bl2, br2, color, duration);
 470            Debug.DrawLine(tr2, br2, color, duration);
 471        }
 472
 473        public static string ToUpperFirst(this string value)
 474        {
 475            if (!string.IsNullOrEmpty(value))
 476            {
 477                var capital = char.ToUpper(value[0]);
 478                value = capital + value.Substring(1);
 479            }
 480
 481            return value;
 482        }
 483
 484        public static Vector3 Sanitize(Vector3 value)
 485        {
 486            float x = float.IsInfinity(value.x) ? 0 : value.x;
 487            float y = float.IsInfinity(value.y) ? 0 : value.y;
 488            float z = float.IsInfinity(value.z) ? 0 : value.z;
 489
 490            return new Vector3(x, y, z);
 491        }
 492
 493        public static bool CompareFloats( float a, float b, float precision = 0.1f ) { return Mathf.Abs(a - b) < precisi
 494
 495        public static void Deconstruct<T1, T2>(this KeyValuePair<T1, T2> tuple, out T1 key, out T2 value)
 496        {
 497            key = tuple.Key;
 498            value = tuple.Value;
 499        }
 500
 501        /// <summary>
 502        /// Set a layer to the given transform and its child
 503        /// </summary>
 504        /// <param name="transform"></param>
 505        public static void SetLayerRecursively(Transform transform, int layer)
 506        {
 507            transform.gameObject.layer = layer;
 508            foreach (Transform child in transform)
 509            {
 510                SetLayerRecursively(child, layer);
 511            }
 512        }
 513
 514        /// <summary>
 515        /// Converts a linear float (between 0 and 1) into an exponential curve fitting for audio volume.
 516        /// </summary>
 517        /// <param name="volume">Linear volume float</param>
 518        /// <returns>Exponential volume curve float</returns>
 519        public static float ToVolumeCurve(float volume) { return volume * (2f - volume); }
 520
 521        /// <summary>
 522        /// Takes a linear volume value between 0 and 1, converts to exponential curve and maps to a value fitting for a
 523        /// </summary>
 524        /// <param name="volume">Linear volume (0 to 1)</param>
 525        /// <returns>Value for audio mixer group volume</returns>
 526        public static float ToAudioMixerGroupVolume(float volume) { return (ToVolumeCurve(volume) * 80f) - 80f; }
 527
 528        public static IEnumerator Wait(float delay, Action onFinishCallback)
 529        {
 530            yield return new WaitForSeconds(delay);
 531            onFinishCallback.Invoke();
 532        }
 533
 534        public static string GetHierarchyPath(this Transform transform)
 535        {
 536            if (transform.parent == null)
 537                return transform.name;
 538            return $"{transform.parent.GetHierarchyPath()}/{transform.name}";
 539        }
 540
 541        public static bool TryFindChildRecursively(this Transform transform, string name, out Transform foundChild)
 542        {
 543            foundChild = transform.Find(name);
 544            if (foundChild != null)
 545                return true;
 546
 547            foreach (Transform child in transform)
 548            {
 549                if (TryFindChildRecursively(child, name, out foundChild))
 550                    return true;
 551            }
 552            return false;
 553        }
 554
 555        public static bool IsPointerOverUIElement(Vector3 mousePosition)
 556        {
 557            var eventData = new PointerEventData(EventSystem.current);
 558            eventData.position = mousePosition;
 559            var results = new List<RaycastResult>();
 560            EventSystem.current.RaycastAll(eventData, results);
 561            return results.Count > 1;
 562        }
 563
 564        public static bool IsPointerOverUIElement() { return IsPointerOverUIElement(Input.mousePosition); }
 565    }
 566}

Methods/Properties

GetFromJsonArray(System.String)