< Summary

Class:GDX.DataTables.DataBinding.SerializableTable
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/DataTables/DataBinding/SerializableTable.cs
Covered lines:0
Uncovered lines:88
Coverable lines:88
Total lines:188
Line coverage:0% (0 of 88)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:3
Method coverage:0% (0 of 3)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SerializableTable()0%2100%
SerializableTable(...)0%20400%
Update(...)0%1561200%

File(s)

./Packages/com.dotbunny.gdx/GDX/DataTables/DataBinding/SerializableTable.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System;
 6using System.Collections.Generic;
 7using UnityEngine;
 8
 9namespace GDX.DataTables.DataBinding
 10{
 11    /// <summary>
 12    ///     A serializable object used during import and export of <see cref="DataTableBase" /> based data.
 13    /// </summary>
 14    [Serializable]
 15    public class SerializableTable
 16    {
 17#pragma warning disable IDE1006
 18        // ReSharper disable InconsistentNaming
 19
 20        /// <summary>
 21        ///     The version of the data at the point of export.
 22        /// </summary>
 23        public ulong DataVersion;
 24
 25        /// <summary>
 26        ///     The structural version of the data at the point of export.
 27        /// </summary>
 28        public int StructureVersion;
 29
 30        /// <summary>
 31        ///     The column names.
 32        /// </summary>
 33        public string[] Headers;
 34
 35        /// <summary>
 36        ///     The column types.
 37        /// </summary>
 38        public string[] Types;
 39
 40        /// <summary>
 41        ///     The row data.
 42        /// </summary>
 43        public SerializableRow[] Rows;
 44
 45        // ReSharper enable InconsistentNaming
 46#pragma warning restore IDE1006
 47
 48        /// <summary>
 49        ///     Creates a new empty <see cref="SerializableTable" />.
 50        /// </summary>
 051        public SerializableTable()
 052        {
 053        }
 54
 55        /// <summary>
 56        ///     Creates a new <see cref="SerializableTable" /> from a <see cref="DataTableBase" />.
 57        /// </summary>
 58        /// <param name="dataTable">Populate the data set from this target.</param>
 059        public SerializableTable(DataTableBase dataTable)
 060        {
 061            ColumnDescription[] columnDescriptions = dataTable.GetAllColumnDescriptions();
 062            int columnCount = columnDescriptions.Length;
 63
 064            DataVersion = dataTable.GetDataVersion();
 065            StructureVersion = dataTable.GetStructureVersion();
 66
 67            // Build Headers
 068            Headers = new string[columnCount];
 069            Types = new string[columnCount];
 70
 071            for (int i = 0; i < columnCount; i++)
 072            {
 073                Headers[i] = columnDescriptions[i].Name;
 074                Types[i] = columnDescriptions[i].Type.GetLabel();
 075            }
 76
 077            RowDescription[] rowDescriptions = dataTable.GetAllRowDescriptions();
 078            int rowCount = rowDescriptions.Length;
 79
 080            Rows = new SerializableRow[rowCount];
 81
 082            for (int i = 0; i < rowCount; i++)
 083            {
 084                RowDescription rowDescription = rowDescriptions[i];
 85
 086                Rows[i] = new SerializableRow(columnCount)
 87                {
 88                    Identifier = rowDescription.Identifier, Name = rowDescription.Name
 89                };
 90
 091                for (int c = 0; c < columnCount; c++)
 092                {
 093                    ColumnDescription columnDescription = columnDescriptions[c];
 094                    Rows[i].Data[c] = dataTable.GetCellValueAsString(rowDescription.Identifier,
 95                        columnDescription.Identifier,
 96                        columnDescription.Type);
 097                }
 098            }
 099        }
 100
 101        /// <summary>
 102        ///     Update the target <paramref name="dataTable" /> based on the data found in the <see cref="SerializableTa
 103        /// </summary>
 104        /// <param name="dataTable">The <see cref="DataTableBase" /> to update.</param>
 105        /// <param name="removeRowIfNotFound">
 106        ///     Should rows not found in the <see cref="SerializableTable" /> be removed from the
 107        ///     <see cref="DataTableBase" />.
 108        /// </param>
 109        /// <returns>Was this operation successful?</returns>
 110        public bool Update(DataTableBase dataTable, bool removeRowIfNotFound = true)
 0111        {
 0112            int tableRowCount = dataTable.GetRowCount();
 0113            int tableColumnCount = dataTable.GetColumnCount();
 114
 0115            ColumnDescription[] columnDescriptions = dataTable.GetAllColumnDescriptions();
 0116            int neededColumnCount = columnDescriptions.Length;
 0117            if (neededColumnCount != Types.Length || neededColumnCount != Headers.Length)
 0118            {
 0119                Debug.LogError(
 120                    $"The importing data has {Types.Length.ToString()} columns where {neededColumnCount.ToString()} was 
 0121                return false;
 122            }
 123
 124            // Build a list of previous row ID, so we know what was removed
 0125            List<int> previousRowInternalIndices = new List<int>(tableRowCount);
 0126            RowDescription[] rowDescriptions = dataTable.GetAllRowDescriptions();
 0127            int rowDescriptionsLength = rowDescriptions.Length;
 0128            for (int i = 0; i < rowDescriptionsLength; i++)
 0129            {
 0130                previousRowInternalIndices.Add(rowDescriptions[i].Identifier);
 0131            }
 132
 0133            int fileRowCount = Rows.Length;
 0134            List<int> foundRowInternalIndices = new List<int>(tableRowCount);
 135
 0136            for (int i = 0; i < fileRowCount; i++)
 0137            {
 138                int rowIdentifier;
 139                string rowName;
 0140                if (Rows[i].Identifier == -1)
 0141                {
 0142                    rowName = Rows[i].Name;
 0143                    if (string.IsNullOrEmpty(rowName))
 0144                    {
 0145                        rowName = "Unnamed";
 0146                    }
 147
 0148                    rowIdentifier = dataTable.AddRow(rowName);
 0149                }
 150                else
 0151                {
 0152                    rowIdentifier = Rows[i].Identifier;
 0153                    rowName = Rows[i].Name;
 0154                }
 155
 0156                foundRowInternalIndices.Add(rowIdentifier);
 0157                dataTable.SetRowName(rowIdentifier, rowName);
 158
 0159                for (int j = 0; j < tableColumnCount; j++)
 0160                {
 0161                    dataTable.SetCellValueFromString(rowIdentifier, columnDescriptions[j].Identifier, Rows[i].Data[j],
 162                        columnDescriptions[j].Type);
 0163                }
 0164            }
 165
 166            // Remove indices that were not found any more?
 0167            if (removeRowIfNotFound)
 0168            {
 0169                int foundIndicesCount = foundRowInternalIndices.Count;
 0170                for (int i = 0; i < foundIndicesCount; i++)
 0171                {
 0172                    if (previousRowInternalIndices.Contains(foundRowInternalIndices[i]))
 0173                    {
 0174                        previousRowInternalIndices.Remove(foundRowInternalIndices[i]);
 0175                    }
 0176                }
 177
 0178                int indicesToRemove = previousRowInternalIndices.Count;
 0179                for (int i = 0; i < indicesToRemove; i++)
 0180                {
 0181                    dataTable.RemoveRow(previousRowInternalIndices[i]);
 0182                }
 0183            }
 184
 0185            return true;
 0186        }
 187    }
 188}