/// <summary> /// Returns the column data as an IArray. /// If the column is null, returns null. /// </summary> public static IArray <T> GetColumnData <T>(this Document doc, INamedBuffer ec) { if (ec == null) { return(null); } var type = typeof(T); if (ec is NamedBuffer <double> doubles) { var vals = doubles.AsArray <double>().ToIArray(); if (type == typeof(double)) { return(vals as IArray <T>); } if (type == typeof(float)) { return(vals.Select(v => (float)v) as IArray <T>); } if (type == typeof(int)) { return(vals.Select(v => (int)v) as IArray <T>); } if (type == typeof(byte)) { return(vals.Select(v => (byte)v) as IArray <T>); } if (type == typeof(bool)) { return(vals.Select(v => v != 0) as IArray <T>); } throw new Exception($"Cannot cast doubles to {type}"); } if (ec is NamedBuffer <int> strings && type == typeof(string)) { var vals = strings.AsArray <int>().ToIArray(); if (type == typeof(string)) { return(vals.Select(doc.GetString) as IArray <T>); } throw new Exception($"Cannot cast strings to {type}"); } if (ec is NamedBuffer <int> ints) { var vals = ints.AsArray <int>().ToIArray(); if (type == typeof(int)) { return(vals as IArray <T>); } throw new Exception("Can only get integer indexes from an integer entity column"); } throw new Exception($"Unrecognized column type {ec.GetType()}"); }
public static void AssertEquals(INamedBuffer b1, INamedBuffer b2) { Assert.AreEqual(b1.Name, b2.Name); // We can't expect all buffers to be the same, because of non-determinism of parallelism when generating string table. //Assert.AreEqual(b1.Bytes.Length, b2.Bytes.Length); //Assert.AreEqual(b1.Bytes.ToArray(), b2.Bytes.ToArray()); }
public static void ExportEntityTable(INamedBuffer buffer, string entityFolder, string[] strings) { var buffers = buffer.Unpack(); var baseName = Util.ToValidFileName(buffer.Name); var fileName = Path.Combine(entityFolder, baseName + ".csv"); var et = new SerializableEntityTable(); foreach (var b in buffers) { if (b.Name.StartsWith(VimConstants.IndexColumnNamePrefix)) { et.IndexColumns.Add(b.StripPrefix().ToTypedBuffer <int>()); } else if (b.Name.StartsWith(VimConstants.NumberColumnNamePrefix)) { et.NumericColumns.Add(b.StripPrefix().ToTypedBuffer <double>()); } else if (b.Name.StartsWith(VimConstants.StringColumnNamePrefix)) { et.StringColumns.Add(b.StripPrefix().ToTypedBuffer <int>()); } else if (b.Name == VimConstants.PropertiesBufferName) { et.Properties = b.AsSpan <SerializableProperty>().ToArray(); } else { throw new Exception($"{b.Name} is not a recognized entity table buffer"); } } var numRows = et.GetNumRows(); var rows = Enumerable .Range(0, numRows) .Select(row => et.GetEntityTableRowValues(row, strings)) .Prepend(et.GetColumnNames()); File.WriteAllLines(fileName, rows.Select(CsvUtil.ToCsvRow)); if (et.Properties.Length > 0) { var d = new Dictionary <int, Dictionary <string, string> >(); foreach (var p in et.Properties) { if (!d.ContainsKey(p.EntityIndex)) { d.Add(p.EntityIndex, new Dictionary <string, string>()); } var key = p.Name >= 0 ? strings[p.Name] : ""; var val = p.Value >= 0 ? strings[p.Value] : ""; d[p.EntityIndex].AddOrReplace(key, val); } JsonUtil.ToJsonFile(d, Path.Combine(entityFolder, baseName) + ".json"); } }
public static void ExportAsset(INamedBuffer asset, string assetFolder) { var ext = Path.GetExtension(asset.Name); var extPos = asset.Name.LastIndexOf('.'); var baseName = extPos >= 0 ? asset.Name.Substring(0, extPos) : asset.Name; var validName = Util.ToValidFileName(baseName); var assetFile = Path.Combine(assetFolder, validName + ext); File.WriteAllBytes(assetFile, asset.Bytes.ToArray()); }
public DocumentBuilder AddAsset(INamedBuffer b) => AddAsset(b.Name, b.ToBytes());
public static EntityTable GetRelatedTable(this INamedBuffer <int> ic, Document doc) => doc.GetTable(ic.GetRelatedTableName());
public static string GetRelatedTableName(this INamedBuffer <int> ic) => GetRelatedTableNameFromColumnName(ic.Name);
public static string GetFieldName(this INamedBuffer <int> ic) => GetFieldNameFromColumnName(ic.Name);
public static bool IsTexture(this INamedBuffer buffer) => buffer.Name.StartsWith(TexturePrefix);
public static string GetTextureFileName(this INamedBuffer buffer) => GetPlatformInvariantFilenameAndExtension(buffer.Name);
public static string GetTableQualifiedName(this INamedBuffer c, EntityTable table) => $"{table.Name}:{c.GetShortName()}";
public static string GetShortName(this INamedBuffer c) => c.Name.SimplifiedName();
public static INamedBuffer StripPrefix(this INamedBuffer b) => b.ToNamedBuffer(b.Name.SimplifiedName());