< Summary

Class:BuilderInWorldUtils
Assembly:BuilderInWorldUtils
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Utils/BuilderInWorldUtils.cs
Covered lines:52
Uncovered lines:128
Coverable lines:180
Total lines:397
Line coverage:28.8% (52 of 180)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetLandOwnershipType(...)0%2100%
GetLandOwnershipType(...)0%6200%
SnapFilterEulerAngles(...)0%2100%
ClosestNumber(...)0%12300%
GetSceneSize(...)0%12300%
CalculateUnityMiddlePoint(...)0%660100%
CreateFloorSceneObject()0%2100%
ConvertMappingsToDictionary(...)0%6200%
DrawScreenRect(...)0%2100%
DrawScreenRectBorder(...)0%2100%
GetScreenRect(...)0%2100%
GetViewportBounds(...)0%2100%
IsWithInSelectionBounds(...)0%2100%
IsWithInSelectionBounds(...)0%2100%
IsBoundInsideCamera(...)0%2100%
IsPointInsideCamera(...)0%12300%
IsPointInsideCamera(...)0%2100%
CopyGameObjectStatus(...)0%3.143075%
IsPointerOverMaskElement(...)0%2100%
IsPointerOverUIElement(...)0%2100%
IsPointerOverUIElement()0%2100%
ConvertEntityToJSON(...)0%9.665042.86%
ConvertJSONToEntityData(...)0%110100%
RemoveGroundEntities(...)0%12300%
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/BuilderInWorldUtils.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 BuilderInWorldUtils
 22{
 23    public static LandRole GetLandOwnershipType(List<LandWithAccess> lands, ParcelScene scene)
 24    {
 025        LandWithAccess filteredLand = lands.FirstOrDefault(land => scene.sceneData.basePosition == land.baseCoords);
 026        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    {
 066        Vector2Int size = new Vector2Int(0, 0);
 067        int lastXCoordinate = 0;
 68        int lastYCoordinate = 0;
 69
 070        foreach (var parcel in parcelScene.sceneData.parcels)
 71        {
 072            if (parcel.x == lastXCoordinate)
 073                size.y++;
 74            else
 075                size.x++;
 76
 077            lastXCoordinate = parcel.x;
 078            lastYCoordinate = parcel.y;
 79        }
 80
 081        return size;
 82    }
 83
 84    public static Vector3 CalculateUnityMiddlePoint(ParcelScene parcelScene)
 85    {
 86        Vector3 position;
 87
 888        float totalX = 0f;
 889        float totalY = 0f;
 890        float totalZ = 0f;
 91
 892        int minX = int.MaxValue;
 893        int minY = int.MaxValue;
 894        int maxX = int.MinValue;
 895        int maxY = int.MinValue;
 96
 3297        foreach (Vector2Int vector in parcelScene.sceneData.parcels)
 98        {
 899            totalX += vector.x;
 8100            totalZ += vector.y;
 8101            if (vector.x < minX)
 8102                minX = vector.x;
 8103            if (vector.y < minY)
 8104                minY = vector.y;
 8105            if (vector.x > maxX)
 8106                maxX = vector.x;
 8107            if (vector.y > maxY)
 8108                maxY = vector.y;
 109        }
 110
 8111        float centerX = totalX / parcelScene.sceneData.parcels.Length;
 8112        float centerZ = totalZ / parcelScene.sceneData.parcels.Length;
 113
 8114        position.x = centerX;
 8115        position.y = totalY;
 8116        position.z = centerZ;
 117
 8118        position = WorldStateUtils.ConvertScenePositionToUnityPosition(parcelScene);
 119
 8120        position.x += ParcelSettings.PARCEL_SIZE / 2;
 8121        position.z += ParcelSettings.PARCEL_SIZE / 2;
 122
 8123        return position;
 124    }
 125
 126    public static CatalogItem CreateFloorSceneObject()
 127    {
 0128        CatalogItem floorSceneObject = new CatalogItem();
 0129        floorSceneObject.id = BuilderInWorldSettings.FLOOR_ID;
 130
 0131        floorSceneObject.model = BuilderInWorldSettings.FLOOR_MODEL;
 0132        floorSceneObject.name = BuilderInWorldSettings.FLOOR_NAME;
 0133        floorSceneObject.assetPackName = BuilderInWorldSettings.FLOOR_ASSET_PACK_NAME;
 134
 0135        floorSceneObject.contents = new Dictionary<string, string>();
 136
 0137        floorSceneObject.contents.Add(BuilderInWorldSettings.FLOOR_GLTF_KEY, BuilderInWorldSettings.FLOOR_GLTF_VALUE);
 0138        floorSceneObject.contents.Add(BuilderInWorldSettings.FLOOR_TEXTURE_KEY, BuilderInWorldSettings.FLOOR_TEXTURE_VAL
 139
 0140        floorSceneObject.metrics = new SceneObject.ObjectMetrics();
 141
 0142        return floorSceneObject;
 143    }
 144
 145    public static Dictionary<string, string> ConvertMappingsToDictionary(ContentServerUtils.MappingPair[] contents)
 146    {
 0147        Dictionary<string, string> mappingDict = new Dictionary<string, string>();
 148
 0149        foreach (ContentServerUtils.MappingPair mappingPair in contents)
 150        {
 0151            mappingDict.Add(mappingPair.file, mappingPair.hash);
 152        }
 153
 0154        return mappingDict;
 155    }
 156
 157    public static void DrawScreenRect(Rect rect, Color color)
 158    {
 0159        GUI.color = color;
 0160        GUI.DrawTexture(rect, Texture2D.whiteTexture);
 0161    }
 162
 163    public static void DrawScreenRectBorder(Rect rect, float thickness, Color color)
 164    {
 165        //Top
 0166        DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color);
 167        // Left
 0168        DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color);
 169        // Right
 0170        DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color);
 171        // Bottom
 0172        DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color);
 0173    }
 174
 175    public static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2)
 176    {
 177        // Move origin from bottom left to top left
 0178        screenPosition1.y = Screen.height - screenPosition1.y;
 0179        screenPosition2.y = Screen.height - screenPosition2.y;
 180        // Calculate corners
 0181        var topLeft = Vector3.Min(screenPosition1, screenPosition2);
 0182        var bottomRight = Vector3.Max(screenPosition1, screenPosition2);
 183        // Create Rect
 0184        return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
 185    }
 186
 187    public static Bounds GetViewportBounds(Camera camera, Vector3 screenPosition1, Vector3 screenPosition2)
 188    {
 0189        var v1 = camera.ScreenToViewportPoint(screenPosition1);
 0190        var v2 = camera.ScreenToViewportPoint(screenPosition2);
 0191        var min = Vector3.Min(v1, v2);
 0192        var max = Vector3.Max(v1, v2);
 0193        min.z = camera.nearClipPlane;
 0194        max.z = camera.farClipPlane;
 195
 0196        var bounds = new Bounds();
 197
 0198        bounds.SetMinMax(min, max);
 0199        return bounds;
 200    }
 201
 0202    public static bool IsWithInSelectionBounds(Transform transform, Vector3 lastClickMousePosition, Vector3 mousePositio
 203
 204    public static bool IsWithInSelectionBounds(Vector3 point, Vector3 lastClickMousePosition, Vector3 mousePosition)
 205    {
 0206        Camera camera = Camera.main;
 0207        var viewPortBounds = GetViewportBounds(camera, lastClickMousePosition, mousePosition);
 0208        return viewPortBounds.Contains(camera.WorldToViewportPoint(point));
 209    }
 210
 211    public static bool IsBoundInsideCamera(Bounds bound)
 212    {
 0213        Vector3[] points = { bound.max, bound.center, bound.min };
 0214        return IsPointInsideCamera(points);
 215    }
 216
 217    public static bool IsPointInsideCamera(Vector3[] points)
 218    {
 0219        foreach (Vector3 point in points)
 220        {
 0221            if (IsPointInsideCamera(point))
 0222                return true;
 223        }
 0224        return false;
 225    }
 226
 227    public static bool IsPointInsideCamera(Vector3 point)
 228    {
 0229        Vector3 topRight = new Vector3(Screen.width, Screen.height, 0);
 0230        var viewPortBounds = GetViewportBounds(Camera.main, Vector3.zero, topRight);
 0231        return viewPortBounds.Contains(Camera.main.WorldToViewportPoint(point));
 232    }
 233
 234    public static void CopyGameObjectStatus(GameObject gameObjectToCopy, GameObject gameObjectToReceive, bool copyParent
 235    {
 2236        if (copyParent)
 0237            gameObjectToReceive.transform.SetParent(gameObjectToCopy.transform.parent);
 238
 2239        gameObjectToReceive.transform.position = gameObjectToCopy.transform.position;
 240
 2241        if (localRotation)
 0242            gameObjectToReceive.transform.localRotation = gameObjectToCopy.transform.localRotation;
 243        else
 2244            gameObjectToReceive.transform.rotation = gameObjectToCopy.transform.rotation;
 245
 2246        gameObjectToReceive.transform.localScale = gameObjectToCopy.transform.lossyScale;
 2247    }
 248
 249    public static bool IsPointerOverMaskElement(LayerMask mask)
 250    {
 251        RaycastHit hitInfo;
 0252        UnityEngine.Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 0253        return Physics.Raycast(mouseRay, out hitInfo, 5555, mask);
 254    }
 255
 256    public static bool IsPointerOverUIElement(Vector3 mousePosition)
 257    {
 0258        var eventData = new PointerEventData(EventSystem.current);
 0259        eventData.position = mousePosition;
 0260        var results = new List<RaycastResult>();
 0261        EventSystem.current.RaycastAll(eventData, results);
 0262        return results.Count > 2;
 263    }
 264
 0265    public static bool IsPointerOverUIElement() { return IsPointerOverUIElement(Input.mousePosition); }
 266
 267    public static string ConvertEntityToJSON(IDCLEntity entity)
 268    {
 4269        EntityData builderInWorldEntityData = new EntityData();
 4270        builderInWorldEntityData.entityId = entity.entityId;
 271
 272
 12273        foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> keyValuePair in entity.components)
 274        {
 2275            if (keyValuePair.Key == CLASS_ID_COMPONENT.TRANSFORM)
 276            {
 2277                EntityData.TransformComponent entityComponentModel = new EntityData.TransformComponent();
 278
 2279                entityComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform.
 2280                entityComponentModel.rotation = entity.gameObject.transform.localRotation.eulerAngles;
 2281                entityComponentModel.scale = entity.gameObject.transform.localScale;
 282
 2283                builderInWorldEntityData.transformComponent = entityComponentModel;
 2284            }
 285            else
 286            {
 0287                ProtocolV2.GenericComponent entityComponentModel = new ProtocolV2.GenericComponent();
 0288                entityComponentModel.componentId = (int) keyValuePair.Key;
 0289                entityComponentModel.data = keyValuePair.Value.GetModel();
 290
 0291                builderInWorldEntityData.components.Add(entityComponentModel);
 292            }
 293        }
 294
 8295        foreach (KeyValuePair<Type, ISharedComponent> keyValuePair in entity.sharedComponents)
 296        {
 0297            if (keyValuePair.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE)
 298            {
 0299                EntityData.NFTComponent nFTComponent = new EntityData.NFTComponent();
 0300                NFTShape.Model model = (NFTShape.Model) keyValuePair.Value.GetModel();
 301
 0302                nFTComponent.id = keyValuePair.Value.id;
 0303                nFTComponent.color = new ColorRepresentation(model.color);
 0304                nFTComponent.assetId = model.assetId;
 0305                nFTComponent.src = model.src;
 0306                nFTComponent.style = model.style;
 307
 0308                builderInWorldEntityData.nftComponent = nFTComponent;
 0309            }
 310            else
 311            {
 0312                ProtocolV2.GenericComponent entityComponentModel = new ProtocolV2.GenericComponent();
 0313                entityComponentModel.componentId = keyValuePair.Value.GetClassId();
 0314                entityComponentModel.data = keyValuePair.Value.GetModel();
 0315                entityComponentModel.classId = keyValuePair.Value.id;
 316
 0317                builderInWorldEntityData.sharedComponents.Add(entityComponentModel);
 318            }
 319        }
 320
 321
 4322        return JsonConvert.SerializeObject(builderInWorldEntityData);
 323    }
 324
 2325    public static EntityData ConvertJSONToEntityData(string json) { return JsonConvert.DeserializeObject<EntityData>(jso
 326
 327    public static List<DCLBuilderInWorldEntity> RemoveGroundEntities(List<DCLBuilderInWorldEntity> entityList)
 328    {
 0329        List<DCLBuilderInWorldEntity> newList = new List<DCLBuilderInWorldEntity>();
 330
 0331        foreach (DCLBuilderInWorldEntity entity in entityList)
 332        {
 0333            if (entity.isFloor)
 334                continue;
 335
 0336            newList.Add(entity);
 337        }
 338
 0339        return newList;
 340    }
 341
 342    public static List<DCLBuilderInWorldEntity> FilterEntitiesBySmartItemComponentAndActions(List<DCLBuilderInWorldEntit
 343    {
 0344        List<DCLBuilderInWorldEntity> newList = new List<DCLBuilderInWorldEntity>();
 345
 0346        foreach (DCLBuilderInWorldEntity entity in entityList)
 347        {
 0348            if (!entity.HasSmartItemComponent() || !entity.HasSmartItemActions())
 349                continue;
 350
 0351            newList.Add(entity);
 352        }
 353
 0354        return newList;
 355    }
 356
 357    public static void CopyRectTransform(RectTransform original, RectTransform rectTransformToCopy)
 358    {
 0359        original.anchoredPosition = rectTransformToCopy.anchoredPosition;
 0360        original.anchorMax = rectTransformToCopy.anchorMax;
 0361        original.anchorMin = rectTransformToCopy.anchorMin;
 0362        original.offsetMax = rectTransformToCopy.offsetMax;
 0363        original.offsetMin = rectTransformToCopy.offsetMin;
 0364        original.sizeDelta = rectTransformToCopy.sizeDelta;
 0365        original.pivot = rectTransformToCopy.pivot;
 0366    }
 367
 368    public static WebRequestAsyncOperation MakeGetCall(string url, Action<string> functionToCall, Dictionary<string, str
 369    {
 0370        return Environment.i.platform.webRequest.Get(
 371            url: url,
 372            OnSuccess: (webRequestResult) =>
 373            {
 0374                if (functionToCall != null)
 375                {
 0376                    byte[] byteArray = webRequestResult.downloadHandler.data;
 0377                    string result = System.Text.Encoding.UTF8.GetString(byteArray);
 0378                    functionToCall?.Invoke(result);
 379                }
 0380            },
 381            OnFail: (webRequestResult) =>
 382            {
 0383                Debug.Log(webRequestResult.error);
 0384            },
 385            headers: headers);
 386    }
 387
 388    public static void ConfigureEventTrigger(EventTrigger eventTrigger, EventTriggerType eventType, UnityAction<BaseEven
 389    {
 2149390        EventTrigger.Entry entry = new EventTrigger.Entry();
 2149391        entry.eventID = eventType;
 2149392        entry.callback.AddListener(call);
 2149393        eventTrigger.triggers.Add(entry);
 2149394    }
 395
 5279396    public static void RemoveEventTrigger(EventTrigger eventTrigger, EventTriggerType eventType) { eventTrigger.triggers
 397}

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.ParcelScene)
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[DCLBuilderInWorldEntity])
FilterEntitiesBySmartItemComponentAndActions(System.Collections.Generic.List[DCLBuilderInWorldEntity])
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)