• R/O
  • HTTP
  • SSH
  • HTTPS

automap: Commit

Automap (OSS) GIT software repository


Commit MetaInfo

Revisão616ccfe0975a1ed4bd75cfc7ef276eca05bf2080 (tree)
Hora2020-04-01 14:07:42
Autormelchior <melchior@user...>
Commitermelchior

Mensagem de Log

Pre-RC0: Pass 2; JSON Field metadata dynamic generation.
[Main chunk metadata _values_ not updated yet; TODO]

Mudança Sumário

Diff

--- a/Automap/Data/ColumnMeta.cs
+++ b/Automap/Data/ColumnMeta.cs
@@ -102,26 +102,6 @@ namespace Automap
102102 this.YMax = mapChunk.YMax;
103103 }
104104
105- public void Write(StreamWriter stream, ICoreClientAPI ClientApi)
106- {
107- // this is gross i hate this
108- stream.Write("['{0}_{1}',[",
109- Location.X,
110- Location.Y
111- );
112- stream.Write("'{0}',", Location.PrettyCoords(ClientApi));
113- stream.Write("'{0}',", ChunkAge);
114- stream.Write("'{0}',", Temperature.ToString("F3"));
115- stream.Write("'{0}',", YMax);
116- stream.Write("'{0}',", Fertility.ToString("F3"));
117- stream.Write("'{0}',", ForestDensity.ToString("F3"));
118- stream.Write("'{0}',", Rainfall.ToString("F3"));
119- stream.Write("'{0}',", ShrubDensity.ToString("F3"));
120- stream.Write("'{0}',", AirBlocks);
121- stream.Write("'{0}',", NonAirBlocks);
122- stream.Write("]]");
123- }
124-
125105 [ProtoBeforeSerialization]
126106 private void PrepareData()
127107 {
--- a/Automap/Subsystems/AutomapSystem.cs
+++ b/Automap/Subsystems/AutomapSystem.cs
@@ -104,8 +104,6 @@ namespace Automap
104104 outputText.Write(staticMap.ToText());
105105 outputText.Flush();
106106
107- jsonPreBuilt = JsonGenerator.MakePreBuiltJSON();
108-
109107 Prefill_POI_Designators();
110108 startChunkColumn = new Vec2i((ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.X / chunkSize), (ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Z / chunkSize));
111109 chunkTopMetadata = new ColumnsMetadata(startChunkColumn);
--- a/Automap/Subsystems/JsonGenerator.cs
+++ b/Automap/Subsystems/JsonGenerator.cs
@@ -4,9 +4,9 @@ using System.IO;
44 using System.Linq;
55 using System.Reflection;
66 using System.Text;
7-using System.Text.RegularExpressions;
87
98 using Newtonsoft.Json;
9+using Newtonsoft.Json.Linq;
1010
1111 using Vintagestory.API.Client;
1212 using Vintagestory.API.Common;
@@ -16,7 +16,6 @@ using Vintagestory.API.MathTools;
1616 using Vintagestory.Common;
1717
1818
19-
2019 namespace Automap
2120 {
2221 public class JsonGenerator
@@ -26,6 +25,7 @@ namespace Automap
2625 private string path { get; set; }
2726 private readonly int chunkSize;
2827
28+ internal JArray ColumnMeta_fieldNames,PointsOfInterest_fieldNames,EntitiesOfInterest_fieldNames;
2929
3030 public JsonGenerator(ICoreClientAPI _ClientAPI, ILogger _Logger, string _path )
3131 {
@@ -33,6 +33,11 @@ namespace Automap
3333 this.Logger = _Logger;
3434 this.path = _path;
3535 this.chunkSize = ClientAPI.World.BlockAccessor.ChunkSize;
36+
37+ ColumnMeta_fieldNames = Dynamic_Names<ColumnMeta>( );
38+ PointsOfInterest_fieldNames = Dynamic_Names<PointOfInterest>( );
39+ EntitiesOfInterest_fieldNames = Dynamic_Names<EntityOfInterest>( );
40+ Logger.VerboseDebug("JSON Ready");
3641 }
3742
3843
@@ -56,6 +61,15 @@ namespace Automap
5661 jsonWriter.DateFormatHandling = DateFormatHandling.IsoDateFormat;
5762 jsonWriter.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
5863
64+ /*
65+ViewFrame.chunks
66+vfc ={};
67+// the basic metadata right now
68+vfc.edges = [northmostedge, eastmost, west, south];
69+vfc.chunkMetadataNames=[...]; // the implementation with reflection
70+vfc.chunkMetadata = new Map([['the keys we have', [...metadatas in the same order as the names, stringified(maybe not for rocks thats complicated)],...]);
71+*/
72+
5973 using (jsonWriter) {
6074 jsonWriter.WriteRaw("ViewFrame.chunks={};\n");
6175 jsonWriter.WriteRaw("ViewFrame.chunks.worldSeedNum=");
@@ -77,23 +91,27 @@ namespace Automap
7791 jsonWriter.WriteValue(chunkSize);
7892 jsonWriter.WriteRaw(";\n");
7993
80- jsonWriter.WriteRaw("ViewFrame.chunks.northMostChunk=");
94+ jsonWriter.WriteRaw("ViewFrame.chunks.edges=");//whats NEWS
95+ jsonWriter.WriteStartArray( );
8196 jsonWriter.WriteValue(chunkTopMetadata.North_mostChunk);
97+ jsonWriter.WriteValue(chunkTopMetadata.East_mostChunk);
98+ jsonWriter.WriteValue(chunkTopMetadata.South_mostChunk);
99+ jsonWriter.WriteValue(chunkTopMetadata.West_mostChunk);
100+ jsonWriter.WriteEndArray( );
82101 jsonWriter.WriteRaw(";\n");
83-
84- jsonWriter.WriteRaw("ViewFrame.chunks.southMostChunk=");
85- jsonWriter.WriteValue(chunkTopMetadata.South_mostChunk);
102+
103+ jsonWriter.WriteRaw("ViewFrame.chunks.chunkMetadataNames=");
104+ ColumnMeta_fieldNames.WriteTo(jsonWriter);
86105 jsonWriter.WriteRaw(";\n");
87106
88- jsonWriter.WriteRaw("ViewFrame.chunks.westMostChunk=");
89- jsonWriter.WriteValue(chunkTopMetadata.West_mostChunk);
107+ jsonWriter.WriteRaw("ViewFrame.chunks.pointsOfInterestNames=");
108+ PointsOfInterest_fieldNames.WriteTo(jsonWriter);
90109 jsonWriter.WriteRaw(";\n");
91110
92- jsonWriter.WriteRaw("ViewFrame.chunks.eastMostChunk=");
93- jsonWriter.WriteValue(chunkTopMetadata.East_mostChunk);
111+ jsonWriter.WriteRaw("ViewFrame.chunks.entityOfInterestNames=");
112+ EntitiesOfInterest_fieldNames.WriteTo(jsonWriter);
94113 jsonWriter.WriteRaw(";\n");
95114
96-
97115 //MAP object format - [key, value]: key is "x_y"
98116 jsonWriter.WriteRaw("ViewFrame.chunks.chunkMetadata=");
99117 jsonWriter.WriteStartConstructor("Map");
@@ -101,6 +119,7 @@ namespace Automap
101119
102120
103121 foreach (var shard in chunkTopMetadata) {
122+ //TODO:Flatten to array of values...Dynamism!
104123 jsonWriter.WriteStartArray( );//Start tuple
105124 jsonWriter.WriteValue($"{shard.Location.X}_{shard.Location.Y}");//Key of Tuple
106125
@@ -223,7 +242,7 @@ namespace Automap
223242
224243 jsonWriter.WriteValue(@this.Location.PrettyCoords(ClientAPI));
225244
226- jsonWriter.WriteValue(@this.Notes);//put more escaping in Java-script if needed
245+ jsonWriter.WriteValue(@this.Notes.Replace('\\', ' '));//put more escaping in Java-script if needed
227246
228247 jsonWriter.WriteValue(@this.Timestamp);
229248
@@ -245,74 +264,36 @@ namespace Automap
245264
246265 jsonWriter.WriteValue(@this.Location.PrettyCoords(ClientAPI));
247266
248- jsonWriter.WriteValue(@this.Notes);//put more escaping in Java-script if needed
267+ jsonWriter.WriteValue(@this.Notes.Replace('\\', ' '));//put more escaping in Java-script if needed
249268
250269 jsonWriter.WriteValue(@this.Timestamp);
251270
252- jsonWriter.WriteValue(@this.EntityId);
271+ //jsonWriter.WriteValue(@this.EntityId);
253272
254273 jsonWriter.WriteEndArray( );
255274 jsonWriter.WriteEndArray( );
256275 }
257276
258- /// <summary>
259- /// Dynamically reflect Points-of-Something fields for metadata descriptors
260- /// </summary>
261- /// <returns>Json-array names of fields for use in map display.</returns>
262- public string MakePreBuiltJSON( )
277+
278+ internal JArray Dynamic_Names<TData>( ) where TData : struct
263279 {
264- var builder = new StringBuilder(512);
265- builder.Append("ViewFrame.chunks={};\n");
266- builder.AppendFormat("ViewFrame.chunks.worldSeedNum='{0}';",
267- ClientAPI.World.Seed
268- );
269- builder.AppendFormat("ViewFrame.chunks.chunkSize={0};",
270- chunkSize
271- );
272-
273- builder.Append("ViewFrame.chunks.chunkMetadataNames=[");
274- var fields = typeof(ColumnMeta).GetFields( );
275- var attsToSort = new List<DisplayNameAttribute>( );
276- // this is so gross
277- foreach (var f in fields) {
278- var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
279- if (att != null) {
280- attsToSort.Add(( DisplayNameAttribute )att);
280+ Dictionary<byte, string> fieldNames = new Dictionary<byte, string>( );
281+
282+ foreach (var fieldInfo in typeof(TData).GetFields(BindingFlags.Instance | BindingFlags.Public)) {
283+ DisplayNameAttribute displayName = fieldInfo.GetCustomAttribute<DisplayNameAttribute>();
284+ if (displayName != null)
285+ {
286+ if (!fieldNames.ContainsKey(displayName.order))
287+ {//No duplicates, no overwrites
288+ fieldNames.Add(displayName.order, displayName.name);
289+ }
290+ }
281291 }
282292
293+ return new JArray(fieldNames.OrderBy(kf => kf.Key).Select(kf => kf.Value).ToArray());
283294 }
284- foreach (var att in attsToSort.OrderBy(a => a.order))
285- builder.AppendFormat("'{0}',", att.name);
286- builder.Append("];\n");
287-
288- builder.Append("ViewFrame.chunks.pointsOfInterestNames=[");
289- fields = typeof(PointOfInterest).GetFields( );
290- attsToSort = new List<DisplayNameAttribute>( );
291-
292- foreach (var f in fields) {
293- var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
294- if (att != null)
295- attsToSort.Add(( DisplayNameAttribute )att);
296- }
297- foreach (var att in attsToSort.OrderBy(a => a.order))
298- builder.AppendFormat("'{0}',", att.name);
299- builder.Append("];\n");
300-
301- builder.Append("ViewFrame.chunks.entityOfInterestNames=[");
302- fields = typeof(EntityOfInterest).GetFields( );
303- attsToSort = new List<DisplayNameAttribute>( );
304-
305- foreach (var f in fields) {
306- var att = f.GetCustomAttribute(typeof(DisplayNameAttribute));
307- if (att != null)
308- attsToSort.Add(( DisplayNameAttribute )att);
309- }
310- foreach (var att in attsToSort.OrderBy(a => a.order))
311- builder.AppendFormat("'{0}',", att.name);
312- builder.Append("];\n");
313295
314- return builder.ToString( );
315- }
296+
316297 }
317298 }
318299
Show on old repository browser