this.YMax = mapChunk.YMax;
}
- public void Write(StreamWriter stream, ICoreClientAPI ClientApi)
- {
- // this is gross i hate this
- stream.Write("['{0}_{1}',[",
- Location.X,
- Location.Y
- );
- stream.Write("'{0}',", Location.PrettyCoords(ClientApi));
- stream.Write("'{0}',", ChunkAge);
- stream.Write("'{0}',", Temperature.ToString("F3"));
- stream.Write("'{0}',", YMax);
- stream.Write("'{0}',", Fertility.ToString("F3"));
- stream.Write("'{0}',", ForestDensity.ToString("F3"));
- stream.Write("'{0}',", Rainfall.ToString("F3"));
- stream.Write("'{0}',", ShrubDensity.ToString("F3"));
- stream.Write("'{0}',", AirBlocks);
- stream.Write("'{0}',", NonAirBlocks);
- stream.Write("]]");
- }
-
[ProtoBeforeSerialization]
private void PrepareData()
{
using System.Linq;
using System.Reflection;
using System.Text;
-using System.Text.RegularExpressions;
using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.Common;
-
namespace Automap
{
public class JsonGenerator
private string path { get; set; }
private readonly int chunkSize;
+ internal JArray ColumnMeta_fieldNames,PointsOfInterest_fieldNames,EntitiesOfInterest_fieldNames;
public JsonGenerator(ICoreClientAPI _ClientAPI, ILogger _Logger, string _path )
{
this.Logger = _Logger;
this.path = _path;
this.chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
+
+ ColumnMeta_fieldNames = Dynamic_Names<ColumnMeta>( );
+ PointsOfInterest_fieldNames = Dynamic_Names<PointOfInterest>( );
+ EntitiesOfInterest_fieldNames = Dynamic_Names<EntityOfInterest>( );
+ Logger.VerboseDebug("JSON Ready");
}
jsonWriter.DateFormatHandling = DateFormatHandling.IsoDateFormat;
jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
+ /*
+ViewFrame.chunks
+vfc ={};
+// the basic metadata right now
+vfc.edges = [northmostedge, eastmost, west, south];
+vfc.chunkMetadataNames=[...]; // the implementation with reflection
+vfc.chunkMetadata = new Map([['the keys we have', [...metadatas in the same order as the names, stringified(maybe not for rocks thats complicated)],...]);
+*/
+
using (jsonWriter) {
jsonWriter.WriteRaw("ViewFrame.chunks={};\n");
jsonWriter.WriteRaw("ViewFrame.chunks.worldSeedNum=");
jsonWriter.WriteValue(chunkSize);
jsonWriter.WriteRaw(";\n");
- jsonWriter.WriteRaw("ViewFrame.chunks.northMostChunk=");
+ jsonWriter.WriteRaw("ViewFrame.chunks.edges=");//whats NEWS
+ jsonWriter.WriteStartArray( );
jsonWriter.WriteValue(chunkTopMetadata.North_mostChunk);
+ jsonWriter.WriteValue(chunkTopMetadata.East_mostChunk);
+ jsonWriter.WriteValue(chunkTopMetadata.South_mostChunk);
+ jsonWriter.WriteValue(chunkTopMetadata.West_mostChunk);
+ jsonWriter.WriteEndArray( );
jsonWriter.WriteRaw(";\n");
-
- jsonWriter.WriteRaw("ViewFrame.chunks.southMostChunk=");
- jsonWriter.WriteValue(chunkTopMetadata.South_mostChunk);
+
+ jsonWriter.WriteRaw("ViewFrame.chunks.chunkMetadataNames=");
+ ColumnMeta_fieldNames.WriteTo(jsonWriter);
jsonWriter.WriteRaw(";\n");
- jsonWriter.WriteRaw("ViewFrame.chunks.westMostChunk=");
- jsonWriter.WriteValue(chunkTopMetadata.West_mostChunk);
+ jsonWriter.WriteRaw("ViewFrame.chunks.pointsOfInterestNames=");
+ PointsOfInterest_fieldNames.WriteTo(jsonWriter);
jsonWriter.WriteRaw(";\n");
- jsonWriter.WriteRaw("ViewFrame.chunks.eastMostChunk=");
- jsonWriter.WriteValue(chunkTopMetadata.East_mostChunk);
+ jsonWriter.WriteRaw("ViewFrame.chunks.entityOfInterestNames=");
+ EntitiesOfInterest_fieldNames.WriteTo(jsonWriter);
jsonWriter.WriteRaw(";\n");
-
//MAP object format - [key, value]: key is "x_y"
jsonWriter.WriteRaw("ViewFrame.chunks.chunkMetadata=");
jsonWriter.WriteStartConstructor("Map");
foreach (var shard in chunkTopMetadata) {
+ //TODO:Flatten to array of values...Dynamism!
jsonWriter.WriteStartArray( );//Start tuple
jsonWriter.WriteValue($"{shard.Location.X}_{shard.Location.Y}");//Key of Tuple
jsonWriter.WriteValue(@this.Location.PrettyCoords(ClientAPI));
- jsonWriter.WriteValue(@this.Notes);//put more escaping in Java-script if needed
+ jsonWriter.WriteValue(@this.Notes.Replace('\\', ' '));//put more escaping in Java-script if needed
jsonWriter.WriteValue(@this.Timestamp);
jsonWriter.WriteValue(@this.Location.PrettyCoords(ClientAPI));
- jsonWriter.WriteValue(@this.Notes);//put more escaping in Java-script if needed
+ jsonWriter.WriteValue(@this.Notes.Replace('\\', ' '));//put more escaping in Java-script if needed
jsonWriter.WriteValue(@this.Timestamp);
- jsonWriter.WriteValue(@this.EntityId);
+ //jsonWriter.WriteValue(@this.EntityId);
jsonWriter.WriteEndArray( );
jsonWriter.WriteEndArray( );
}
- /// <summary>
- /// Dynamically reflect Points-of-Something fields for metadata descriptors
- /// </summary>
- /// <returns>Json-array names of fields for use in map display.</returns>
- public string MakePreBuiltJSON( )
+
+ internal JArray Dynamic_Names<TData>( ) where TData : struct
{
- var builder = new StringBuilder(512);
- builder.Append("ViewFrame.chunks={};\n");
- builder.AppendFormat("ViewFrame.chunks.worldSeedNum='{0}';",
- ClientAPI.World.Seed
- );
- builder.AppendFormat("ViewFrame.chunks.chunkSize={0};",
- chunkSize
- );
-
- builder.Append("ViewFrame.chunks.chunkMetadataNames=[");
- var fields = typeof(ColumnMeta).GetFields( );
- var attsToSort = new List<DisplayNameAttribute>( );
- // this is so gross
- foreach (var f in fields) {
- var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
- if (att != null) {
- attsToSort.Add(( DisplayNameAttribute )att);
+ Dictionary<byte, string> fieldNames = new Dictionary<byte, string>( );
+
+ foreach (var fieldInfo in typeof(TData).GetFields(BindingFlags.Instance | BindingFlags.Public)) {
+ DisplayNameAttribute displayName = fieldInfo.GetCustomAttribute<DisplayNameAttribute>();
+ if (displayName != null)
+ {
+ if (!fieldNames.ContainsKey(displayName.order))
+ {//No duplicates, no overwrites
+ fieldNames.Add(displayName.order, displayName.name);
+ }
+ }
}
+ return new JArray(fieldNames.OrderBy(kf => kf.Key).Select(kf => kf.Value).ToArray());
}
- foreach (var att in attsToSort.OrderBy(a => a.order))
- builder.AppendFormat("'{0}',", att.name);
- builder.Append("];\n");
-
- builder.Append("ViewFrame.chunks.pointsOfInterestNames=[");
- fields = typeof(PointOfInterest).GetFields( );
- attsToSort = new List<DisplayNameAttribute>( );
-
- foreach (var f in fields) {
- var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
- if (att != null)
- attsToSort.Add(( DisplayNameAttribute )att);
- }
- foreach (var att in attsToSort.OrderBy(a => a.order))
- builder.AppendFormat("'{0}',", att.name);
- builder.Append("];\n");
-
- builder.Append("ViewFrame.chunks.entityOfInterestNames=[");
- fields = typeof(EntityOfInterest).GetFields( );
- attsToSort = new List<DisplayNameAttribute>( );
-
- foreach (var f in fields) {
- var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
- if (att != null)
- attsToSort.Add(( DisplayNameAttribute )att);
- }
- foreach (var att in attsToSort.OrderBy(a => a.order))
- builder.AppendFormat("'{0}',", att.name);
- builder.Append("];\n");
- return builder.ToString( );
- }
+
}
}