public void Refresh() { // Load the table if exists string destination = $"{m_ConfigFile.dataPath}/{_tableName}.json"; if (!File.Exists(destination)) { return; } // clear out existing data _data.Clear(); var stream = GetStream(destination); var token = GetTableData(stream); var stru = new TableStruct { Table = new Table(), Ujson = _ujson, Data = _data }; ParseData(token, ref stru); _ujson = stru.Ujson; _data = stru.Data; }
/** * */ public static Table GetTable(string tableName) { var tblStruct = new TableStruct { Table = new Table(), Ujson = { JsonColumnEntries = new List <JsonEntry>() }, Data = new List <TableField>() }; DatabaseConfig config = Fetch(); if (config == null) { #if UNITY_EDITOR Debug.LogWarning("Config file not found. Have you created the database config file?"); #else throw new ArgumentException($"{tableName} couldn't be found!"); #endif return(tblStruct.Table); } string destination = $"{config.dataPath}/{tableName}.json"; if (!File.Exists(destination)) #if UNITY_EDITOR { Debug.LogWarning($"{tableName} couldn't be found!"); } #else { throw new ArgumentException($"{tableName} couldn't be found!"); } #endif var table = ParseData(GetTableData(GetStream(destination)), ref tblStruct); return(table); }
/// <summary> /// The table data is read from the JSON token and then inserted /// in the table struct. /// </summary> /// <param name="tableData"></param> /// <param name="tableStruct"></param> /// <returns></returns> private static Table ParseData(JToken tableData, ref TableStruct tableStruct) { tableStruct.Table.id = tableData["id"]?.ToObject <string>(); tableStruct.Table.metadata = tableData["metadata"]?.ToObject <TableMetaData>(); JArray entities = tableData["data"].Value <JArray>(); // set the count of the amount of entities in the json tableStruct.Ujson.EntityCount = (uint)entities.Count; uint i = 0; // add ID if we dont have one // Debug.Log(rowProperties["id"]); // Debug.Log(rowProperties["id"] == null); // if (rowProperties["id"] == null) // { // result.Fields.Add(new TableRowInfo{ ColumnName = "id", ColumnID = i }, new TableField // { // Data = entityID // }); // i++; // } // Loop through all the entities foreach (var el in entities) { var rowParameters = el.Children <JProperty>(); TableRow tblRow = new TableRow { RowId = i }; if (i == 0) { // Set the amount of columns existing in all the entities tableStruct.Ujson.ColumnCount = (uint)rowParameters.Count(); // We need to story all the data in one large array // _ujson.JsonColumnEntries.Capacity = (int)(_ujson.ColumnCount * _ujson.EntityCount); } uint j = 0; foreach (var entity in rowParameters) { TableField field = new TableField(); switch (entity.Value.Type) { case JTokenType.Boolean: field.Data = entity.ToObject <bool>(); // Debug.Log("Bool"); break; case JTokenType.Integer: field.Data = entity.Value.ToObject <double>(); // Debug.Log("Number"); break; case JTokenType.String: field.Data = entity.Value.ToObject <string>(); // Debug.Log("String"); break; case JTokenType.Object: field.Data = entity.Value.ToObject <JObject>(); // Debug.Log(field.Data.ToString()); break; } // From there we need store the entries JsonEntry entry = new JsonEntry { Name = entity.Name, Type = entity.Value.Type }; tableStruct.Ujson.JsonColumnEntries.Add(entry); tableStruct.Data.Add(field); // Add the field to the table row as well. tblRow.Fields.Add(new TableRowInfo { ColumnName = entity.Name, ColumnID = j }, field); j++; } tableStruct.Table.Rows.Add(i, tblRow); i++; } return(tableStruct.Table); }