< Summary

Class:BIWUtils
Assembly:BuilderInWorldUtils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Utils/BIWUtils.cs
Covered lines:96
Uncovered lines:93
Coverable lines:189
Total lines:405
Line coverage:50.7% (96 of 189)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetLandOwnershipType(...)0%110100%
GetLandOwnershipType(...)0%6200%
SnapFilterEulerAngles(...)0%2100%
ClosestNumber(...)0%12300%
GetSceneSize(...)0%660100%
CalculateUnityMiddlePoint(...)0%88096.77%
CreateFloorSceneObject()0%2100%
ConvertMappingsToDictionary(...)0%6200%
DrawScreenRect(...)0%2100%
DrawScreenRectBorder(...)0%2100%
GetScreenRect(...)0%2100%
GetViewportBounds(...)0%110100%
IsWithinSelectionBounds(...)0%2100%
IsWithinSelectionBounds(...)0%110100%
IsBoundInsideCamera(...)0%2100%
IsPointInsideCamera(...)0%12300%
IsPointInsideCamera(...)0%2100%
CopyGameObjectStatus(...)0%3.143075%
IsPointerOverMaskElement(...)0%110100%
IsPointerOverUIElement(...)0%110100%
IsPointerOverUIElement()0%110100%
ConvertEntityToJSON(...)0%9.665042.86%
ConvertJSONToEntityData(...)0%110100%
RemoveGroundEntities(...)0%330100%
FilterEntitiesBySmartItemComponentAndActions(...)0%20400%
CopyRectTransform(...)0%2100%
MakeGetCall(...)0%6200%
ConfigureEventTrigger(...)0%110100%
RemoveEventTrigger(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Utils/BIWUtils.cs

#LineLine coverage
 1using DCL;
 2using DCL.Components;
 3using DCL.Models;
 4using System;
 5using System.Collections;
 6using System.Collections.Generic;
 7using UnityEngine;
 8using UnityEngine.EventSystems;
 9using Newtonsoft.Json;
 10using Newtonsoft.Json.Linq;
 11using DCL.Configuration;
 12using static ProtocolV2;
 13using Environment = DCL.Environment;
 14using System.IO;
 15using System.Linq;
 16using System.Runtime.Serialization.Formatters.Binary;
 17using DCL.Controllers;
 18using UnityEngine.Networking;
 19using UnityEngine.Events;
 20
 21public static partial class BIWUtils
 22{
 23    public static LandRole GetLandOwnershipType(List<LandWithAccess> lands, ParcelScene scene)
 24    {
 725        LandWithAccess filteredLand = lands.FirstOrDefault(land => scene.sceneData.basePosition == land.baseCoords);
 726        return GetLandOwnershipType(filteredLand);
 27    }
 28
 29    public static LandRole GetLandOwnershipType(LandWithAccess land)
 30    {
 031        if (land != null)
 032            return land.role;
 033        return LandRole.OWNER;
 34    }
 35
 36    public static Vector3 SnapFilterEulerAngles(Vector3 vectorToFilter, float degrees)
 37    {
 038        vectorToFilter.x = ClosestNumber(vectorToFilter.x, degrees);
 039        vectorToFilter.y = ClosestNumber(vectorToFilter.y, degrees);
 040        vectorToFilter.z = ClosestNumber(vectorToFilter.z, degrees);
 41
 042        return vectorToFilter;
 43    }
 44
 45    static float ClosestNumber(float n, float m)
 46    {
 47        // find the quotient
 048        int q = Mathf.RoundToInt(n / m);
 49
 50        // 1st possible closest number
 051        float n1 = m * q;
 52
 53        // 2nd possible closest number
 054        float n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1));
 55
 56        // if true, then n1 is the required closest number
 057        if (Math.Abs(n - n1) < Math.Abs(n - n2))
 058            return n1;
 59
 60        // else n2 is the required closest number
 061        return n2;
 62    }
 63
 64    public static Vector2Int GetSceneSize(ParcelScene parcelScene)
 65    {
 1366        int minX = Int32.MaxValue;
 1367        int maxX = Int32.MinValue;
 1368        int minY = Int32.MaxValue;
 1369        int maxY = Int32.MinValue;
 70
 6871        foreach (var parcel in parcelScene.sceneData.parcels)
 72        {
 2173            if (parcel.x > maxX)
 1574                maxX = parcel.x;
 2175            if (parcel.x < minX)
 1576                minX = parcel.x;
 77
 2178            if (parcel.y > maxY)
 1579                maxY = parcel.y;
 2180            if (parcel.y < minY)
 1581                minY = parcel.y;
 82        }
 83
 1384        int sizeX = maxX - minX + 1;
 1385        int sizeY = maxY - minY + 1;
 1386        return new Vector2Int(sizeX, sizeY);
 87    }
 88
 89    public static Vector3 CalculateUnityMiddlePoint(IParcelScene parcelScene)
 90    {
 91        Vector3 position;
 92
 10993        float totalX = 0f;
 10994        float totalY = 0f;
 10995        float totalZ = 0f;
 96
 10997        int minX = int.MaxValue;
 10998        int minY = int.MaxValue;
 10999        int maxX = int.MinValue;
 109100        int maxY = int.MinValue;
 101
 109102        if (parcelScene.sceneData == null || parcelScene.sceneData.parcels == null)
 0103            return Vector3.zero;
 104
 436105        foreach (Vector2Int vector in parcelScene.sceneData.parcels)
 106        {
 109107            totalX += vector.x;
 109108            totalZ += vector.y;
 109109            if (vector.x < minX)
 109110                minX = vector.x;
 109111            if (vector.y < minY)
 109112                minY = vector.y;
 109113            if (vector.x > maxX)
 109114                maxX = vector.x;
 109115            if (vector.y > maxY)
 109116                maxY = vector.y;
 117        }
 118
 109119        float centerX = totalX / parcelScene.sceneData.parcels.Length;
 109120        float centerZ = totalZ / parcelScene.sceneData.parcels.Length;
 121
 109122        position.x = centerX;
 109123        position.y = totalY;
 109124        position.z = centerZ;
 125
 109126        position = WorldStateUtils.ConvertScenePositionToUnityPosition(parcelScene);
 127
 109128        position.x += ParcelSettings.PARCEL_SIZE / 2;
 109129        position.z += ParcelSettings.PARCEL_SIZE / 2;
 130
 109131        return position;
 132    }
 133
 134    public static CatalogItem CreateFloorSceneObject()
 135    {
 0136        CatalogItem floorSceneObject = new CatalogItem();
 0137        floorSceneObject.id = BIWSettings.FLOOR_ID;
 138
 0139        floorSceneObject.model = BIWSettings.FLOOR_MODEL;
 0140        floorSceneObject.name = BIWSettings.FLOOR_NAME;
 0141        floorSceneObject.assetPackName = BIWSettings.FLOOR_ASSET_PACK_NAME;
 142
 0143        floorSceneObject.contents = new Dictionary<string, string>();
 144
 0145        floorSceneObject.contents.Add(BIWSettings.FLOOR_GLTF_KEY, BIWSettings.FLOOR_GLTF_VALUE);
 0146        floorSceneObject.contents.Add(BIWSettings.FLOOR_TEXTURE_KEY, BIWSettings.FLOOR_TEXTURE_VALUE);
 147
 0148        floorSceneObject.metrics = new SceneObject.ObjectMetrics();
 149
 0150        return floorSceneObject;
 151    }
 152
 153    public static Dictionary<string, string> ConvertMappingsToDictionary(ContentServerUtils.MappingPair[] contents)
 154    {
 0155        Dictionary<string, string> mappingDict = new Dictionary<string, string>();
 156
 0157        foreach (ContentServerUtils.MappingPair mappingPair in contents)
 158        {
 0159            mappingDict.Add(mappingPair.file, mappingPair.hash);
 160        }
 161
 0162        return mappingDict;
 163    }
 164
 165    public static void DrawScreenRect(Rect rect, Color color)
 166    {
 0167        GUI.color = color;
 0168        GUI.DrawTexture(rect, Texture2D.whiteTexture);
 0169    }
 170
 171    public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
 172    {
 173        //Top
 0174        DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
 175        // Left
 0176        DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
 177        // Right
 0178        DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
 179        // Bottom
 0180        DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
 0181    }
 182
 183    public static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
 184    {
 185        // Move origin from bottom left to top left
 0186        screenPosition1.y = Screen.height - screenPosition1.y;
 0187        screenPosition2.y = Screen.height - screenPosition2.y;
 188        // Calculate corners
 0189        var topLeft = Vector3.Min(screenPosition1, screenPosition2);
 0190        var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
 191        // Create Rect
 0192        return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
 193    }
 194
 195    public static Bounds GetViewportBounds(Camera camera, Vector3 screenPosition1, Vector3 screenPosition2)
 196    {
 2197        var v1 = camera.ScreenToViewportPoint(screenPosition1);
 2198        var v2 = camera.ScreenToViewportPoint(screenPosition2);
 2199        var min = Vector3.Min(v1, v2);
 2200        var max = Vector3.Max(v1, v2);
 2201        min.z = camera.nearClipPlane;
 2202        max.z = camera.farClipPlane;
 203
 2204        var bounds = new Bounds();
 205
 2206        bounds.SetMinMax(min, max);
 2207        return bounds;
 208    }
 209
 0210    public static bool IsWithinSelectionBounds(Transform transform, Vector3 lastClickMousePosition, Vector3 mousePositio
 211
 212    public static bool IsWithinSelectionBounds(Vector3 point, Vector3 lastClickMousePosition, Vector3 mousePosition)
 213    {
 2214        Camera camera = Camera.main;
 2215        var viewPortBounds = GetViewportBounds(camera, lastClickMousePosition, mousePosition);
 2216        return viewPortBounds.Contains(camera.WorldToViewportPoint(point));
 217    }
 218
 219    public static bool IsBoundInsideCamera(Bounds bound)
 220    {
 0221        Vector3[] points = { bound.max, bound.center, bound.min };
 0222        return IsPointInsideCamera(points);
 223    }
 224
 225    public static bool IsPointInsideCamera(Vector3[] points)
 226    {
 0227        foreach (Vector3 point in points)
 228        {
 0229            if (IsPointInsideCamera(point))
 0230                return true;
 231        }
 0232        return false;
 233    }
 234
 235    public static bool IsPointInsideCamera(Vector3 point)
 236    {
 0237        Vector3 topRight = new Vector3(Screen.width, Screen.height, 0);
 0238        var viewPortBounds = GetViewportBounds(Camera.main, Vector3.zero, topRight);
 0239        return viewPortBounds.Contains(Camera.main.WorldToViewportPoint(point));
 240    }
 241
 242    public static void CopyGameObjectStatus(GameObject gameObjectToCopy, GameObject gameObjectToReceive, bool copyParent
 243    {
 5244        if (copyParent)
 0245            gameObjectToReceive.transform.SetParent(gameObjectToCopy.transform.parent);
 246
 5247        gameObjectToReceive.transform.position = gameObjectToCopy.transform.position;
 248
 5249        if (localRotation)
 0250            gameObjectToReceive.transform.localRotation = gameObjectToCopy.transform.localRotation;
 251        else
 5252            gameObjectToReceive.transform.rotation = gameObjectToCopy.transform.rotation;
 253
 5254        gameObjectToReceive.transform.localScale = gameObjectToCopy.transform.lossyScale;
 5255    }
 256
 257    public static bool IsPointerOverMaskElement(LayerMask mask)
 258    {
 259        RaycastHit hitInfo;
 2260        UnityEngine.Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 2261        return Physics.Raycast(mouseRay, out hitInfo, 5555, mask);
 262    }
 263
 264    public static bool IsPointerOverUIElement(Vector3 mousePosition)
 265    {
 1266        var eventData = new PointerEventData(EventSystem.current);
 1267        eventData.position = mousePosition;
 1268        var results = new List<RaycastResult>();
 1269        EventSystem.current.RaycastAll(eventData, results);
 1270        return results.Count > 2;
 271    }
 272
 1273    public static bool IsPointerOverUIElement() { return IsPointerOverUIElement(Input.mousePosition); }
 274
 275    public static string ConvertEntityToJSON(IDCLEntity entity)
 276    {
 4277        EntityData builderInWorldEntityData = new EntityData();
 4278        builderInWorldEntityData.entityId = entity.entityId;
 279
 280
 10281        foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> keyValuePair in entity.components)
 282        {
 1283            if (keyValuePair.Key == CLASS_ID_COMPONENT.TRANSFORM)
 284            {
 1285                EntityData.TransformComponent entityComponentModel = new EntityData.TransformComponent();
 286
 1287                entityComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform.
 1288                entityComponentModel.rotation = entity.gameObject.transform.localRotation.eulerAngles;
 1289                entityComponentModel.scale = entity.gameObject.transform.localScale;
 290
 1291                builderInWorldEntityData.transformComponent = entityComponentModel;
 1292            }
 293            else
 294            {
 0295                ProtocolV2.GenericComponent entityComponentModel = new ProtocolV2.GenericComponent();
 0296                entityComponentModel.componentId = (int) keyValuePair.Key;
 0297                entityComponentModel.data = keyValuePair.Value.GetModel();
 298
 0299                builderInWorldEntityData.components.Add(entityComponentModel);
 300            }
 301        }
 302
 8303        foreach (KeyValuePair<Type, ISharedComponent> keyValuePair in entity.sharedComponents)
 304        {
 0305            if (keyValuePair.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE)
 306            {
 0307                EntityData.NFTComponent nFTComponent = new EntityData.NFTComponent();
 0308                NFTShape.Model model = (NFTShape.Model) keyValuePair.Value.GetModel();
 309
 0310                nFTComponent.id = keyValuePair.Value.id;
 0311                nFTComponent.color = new ColorRepresentation(model.color);
 0312                nFTComponent.assetId = model.assetId;
 0313                nFTComponent.src = model.src;
 0314                nFTComponent.style = model.style;
 315
 0316                builderInWorldEntityData.nftComponent = nFTComponent;
 0317            }
 318            else
 319            {
 0320                ProtocolV2.GenericComponent entityComponentModel = new ProtocolV2.GenericComponent();
 0321                entityComponentModel.componentId = keyValuePair.Value.GetClassId();
 0322                entityComponentModel.data = keyValuePair.Value.GetModel();
 0323                entityComponentModel.classId = keyValuePair.Value.id;
 324
 0325                builderInWorldEntityData.sharedComponents.Add(entityComponentModel);
 326            }
 327        }
 328
 329
 4330        return JsonConvert.SerializeObject(builderInWorldEntityData);
 331    }
 332
 3333    public static EntityData ConvertJSONToEntityData(string json) { return JsonConvert.DeserializeObject<EntityData>(jso
 334
 335    public static List<BIWEntity> RemoveGroundEntities(List<BIWEntity> entityList)
 336    {
 8337        List<BIWEntity> newList = new List<BIWEntity>();
 338
 44339        foreach (BIWEntity entity in entityList)
 340        {
 14341            if (entity.isFloor)
 342                continue;
 343
 14344            newList.Add(entity);
 345        }
 346
 8347        return newList;
 348    }
 349
 350    public static List<BIWEntity> FilterEntitiesBySmartItemComponentAndActions(List<BIWEntity> entityList)
 351    {
 0352        List<BIWEntity> newList = new List<BIWEntity>();
 353
 0354        foreach (BIWEntity entity in entityList)
 355        {
 0356            if (!entity.HasSmartItemComponent() || !entity.HasSmartItemActions())
 357                continue;
 358
 0359            newList.Add(entity);
 360        }
 361
 0362        return newList;
 363    }
 364
 365    public static void CopyRectTransform(RectTransform original, RectTransform rectTransformToCopy)
 366    {
 0367        original.anchoredPosition = rectTransformToCopy.anchoredPosition;
 0368        original.anchorMax = rectTransformToCopy.anchorMax;
 0369        original.anchorMin = rectTransformToCopy.anchorMin;
 0370        original.offsetMax = rectTransformToCopy.offsetMax;
 0371        original.offsetMin = rectTransformToCopy.offsetMin;
 0372        original.sizeDelta = rectTransformToCopy.sizeDelta;
 0373        original.pivot = rectTransformToCopy.pivot;
 0374    }
 375
 376    public static WebRequestAsyncOperation MakeGetCall(string url, Action<string> functionToCall, Dictionary<string, str
 377    {
 0378        return Environment.i.platform.webRequest.Get(
 379            url: url,
 380            OnSuccess: (webRequestResult) =>
 381            {
 0382                if (functionToCall != null)
 383                {
 0384                    byte[] byteArray = webRequestResult.downloadHandler.data;
 0385                    string result = System.Text.Encoding.UTF8.GetString(byteArray);
 0386                    functionToCall?.Invoke(result);
 387                }
 0388            },
 389            OnFail: (webRequestResult) =>
 390            {
 0391                Debug.Log(webRequestResult.error);
 0392            },
 393            headers: headers);
 394    }
 395
 396    public static void ConfigureEventTrigger(EventTrigger eventTrigger, EventTriggerType eventType, UnityAction<BaseEven
 397    {
 3774398        EventTrigger.Entry entry = new EventTrigger.Entry();
 3774399        entry.eventID = eventType;
 3774400        entry.callback.AddListener(call);
 3774401        eventTrigger.triggers.Add(entry);
 3774402    }
 403
 9154404    public static void RemoveEventTrigger(EventTrigger eventTrigger, EventTriggerType eventType) { eventTrigger.triggers
 405}

Methods/Properties

GetLandOwnershipType(System.Collections.Generic.List[LandWithAccess], DCL.Controllers.ParcelScene)
GetLandOwnershipType(LandWithAccess)
SnapFilterEulerAngles(UnityEngine.Vector3, System.Single)
ClosestNumber(System.Single, System.Single)
GetSceneSize(DCL.Controllers.ParcelScene)
CalculateUnityMiddlePoint(DCL.Controllers.IParcelScene)
CreateFloorSceneObject()
ConvertMappingsToDictionary(DCL.ContentServerUtils/MappingPair[])
DrawScreenRect(UnityEngine.Rect, UnityEngine.Color)
DrawScreenRectBorder(UnityEngine.Rect, System.Single, UnityEngine.Color)
GetScreenRect(UnityEngine.Vector3, UnityEngine.Vector3)
GetViewportBounds(UnityEngine.Camera, UnityEngine.Vector3, UnityEngine.Vector3)
IsWithinSelectionBounds(UnityEngine.Transform, UnityEngine.Vector3, UnityEngine.Vector3)
IsWithinSelectionBounds(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Vector3)
IsBoundInsideCamera(UnityEngine.Bounds)
IsPointInsideCamera(UnityEngine.Vector3[])
IsPointInsideCamera(UnityEngine.Vector3)
CopyGameObjectStatus(UnityEngine.GameObject, UnityEngine.GameObject, System.Boolean, System.Boolean)
IsPointerOverMaskElement(UnityEngine.LayerMask)
IsPointerOverUIElement(UnityEngine.Vector3)
IsPointerOverUIElement()
ConvertEntityToJSON(DCL.Models.IDCLEntity)
ConvertJSONToEntityData(System.String)
RemoveGroundEntities(System.Collections.Generic.List[BIWEntity])
FilterEntitiesBySmartItemComponentAndActions(System.Collections.Generic.List[BIWEntity])
CopyRectTransform(UnityEngine.RectTransform, UnityEngine.RectTransform)
MakeGetCall(System.String, System.Action[String], System.Collections.Generic.Dictionary[String,String])
ConfigureEventTrigger(UnityEngine.EventSystems.EventTrigger, UnityEngine.EventSystems.EventTriggerType, UnityEngine.Events.UnityAction[BaseEventData])
RemoveEventTrigger(UnityEngine.EventSystems.EventTrigger, UnityEngine.EventSystems.EventTriggerType)