OSDN Git Service

Pre-RC0: Pass 2; JSON Field metadata dynamic generation.
authormelchior <melchior@users.osdn.me>
Wed, 1 Apr 2020 05:07:42 +0000 (01:07 -0400)
committermelchior <melchior@users.osdn.me>
Wed, 1 Apr 2020 05:07:42 +0000 (01:07 -0400)
[Main chunk metadata _values_ not updated yet; TODO]

Automap/Data/ColumnMeta.cs
Automap/Subsystems/AutomapSystem.cs
Automap/Subsystems/JsonGenerator.cs

index 9fd9aef..5ee3945 100644 (file)
@@ -102,26 +102,6 @@ namespace Automap
                        this.YMax = mapChunk.YMax;
                }
 
                        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()
                {
                [ProtoBeforeSerialization]
                private void PrepareData()
                {
index 858a563..8cbfc20 100644 (file)
@@ -104,8 +104,6 @@ namespace Automap
                        outputText.Write(staticMap.ToText());
                        outputText.Flush();
 
                        outputText.Write(staticMap.ToText());
                        outputText.Flush();
 
-                       jsonPreBuilt = JsonGenerator.MakePreBuiltJSON();
-
                        Prefill_POI_Designators();
                        startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Z / chunkSize));
                        chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
                        Prefill_POI_Designators();
                        startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Z / chunkSize));
                        chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
index b74689f..116d03b 100644 (file)
@@ -4,9 +4,9 @@ using System.IO;
 using System.Linq;
 using System.Reflection;
 using System.Text;
 using System.Linq;
 using System.Reflection;
 using System.Text;
-using System.Text.RegularExpressions;
 
 using Newtonsoft.Json;
 
 using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
 
 using Vintagestory.API.Client;
 using Vintagestory.API.Common;
 
 using Vintagestory.API.Client;
 using Vintagestory.API.Common;
@@ -16,7 +16,6 @@ using Vintagestory.API.MathTools;
 using Vintagestory.Common;
 
 
 using Vintagestory.Common;
 
 
-
 namespace Automap
 {
        public class JsonGenerator
 namespace Automap
 {
        public class JsonGenerator
@@ -26,6 +25,7 @@ namespace Automap
                private string path { get; set; }
                private readonly int chunkSize;
 
                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 )
                {
 
                public JsonGenerator(ICoreClientAPI _ClientAPI,  ILogger _Logger, string _path )
                {
@@ -33,6 +33,11 @@ namespace Automap
                this.Logger = _Logger;
                this.path = _path;
                this.chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
                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");
                }
 
 
                }
 
 
@@ -56,6 +61,15 @@ namespace Automap
                jsonWriter.DateFormatHandling = DateFormatHandling.IsoDateFormat;
                jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
 
                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=");
                using (jsonWriter) {
                jsonWriter.WriteRaw("ViewFrame.chunks={};\n");
                jsonWriter.WriteRaw("ViewFrame.chunks.worldSeedNum=");
@@ -77,23 +91,27 @@ namespace Automap
                jsonWriter.WriteValue(chunkSize);
                jsonWriter.WriteRaw(";\n");
 
                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.North_mostChunk);
+               jsonWriter.WriteValue(chunkTopMetadata.East_mostChunk);
+               jsonWriter.WriteValue(chunkTopMetadata.South_mostChunk);                
+               jsonWriter.WriteValue(chunkTopMetadata.West_mostChunk);         
+               jsonWriter.WriteEndArray( );
                jsonWriter.WriteRaw(";\n");
                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(";\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(";\n");
 
-               jsonWriter.WriteRaw("ViewFrame.chunks.eastMostChunk=");
-               jsonWriter.WriteValue(chunkTopMetadata.East_mostChunk);
+               jsonWriter.WriteRaw("ViewFrame.chunks.entityOfInterestNames=");         
+               EntitiesOfInterest_fieldNames.WriteTo(jsonWriter);              
                jsonWriter.WriteRaw(";\n");
 
                jsonWriter.WriteRaw(";\n");
 
-
                //MAP object format - [key, value]: key is "x_y"
                jsonWriter.WriteRaw("ViewFrame.chunks.chunkMetadata=");
                jsonWriter.WriteStartConstructor("Map");
                //MAP object format - [key, value]: key is "x_y"
                jsonWriter.WriteRaw("ViewFrame.chunks.chunkMetadata=");
                jsonWriter.WriteStartConstructor("Map");
@@ -101,6 +119,7 @@ namespace Automap
 
 
                foreach (var shard in chunkTopMetadata) {
 
 
                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.WriteStartArray( );//Start tuple
                jsonWriter.WriteValue($"{shard.Location.X}_{shard.Location.Y}");//Key of Tuple
 
@@ -223,7 +242,7 @@ namespace Automap
 
                jsonWriter.WriteValue(@this.Location.PrettyCoords(ClientAPI));
 
 
                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.Timestamp);
 
@@ -245,74 +264,36 @@ namespace Automap
 
                jsonWriter.WriteValue(@this.Location.PrettyCoords(ClientAPI));
 
 
                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.Timestamp);
 
-               jsonWriter.WriteValue(@this.EntityId);          
+               //jsonWriter.WriteValue(@this.EntityId);                
 
                jsonWriter.WriteEndArray( );
                jsonWriter.WriteEndArray( );
                }
 
 
                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( );
-               }
+
        }
 }
 
        }
 }