using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading; using Hjg.Pngcs; using Hjg.Pngcs.Chunks; using Newtonsoft.Json; using ProtoBuf; using Vintagestory.API.Client; using Vintagestory.API.Common; using Vintagestory.API.Config; using Vintagestory.API.Datastructures; using Vintagestory.API.MathTools; using Vintagestory.Common; namespace Automap { public class AutomapSystem { private Thread cartographer_thread; private ICoreClientAPI ClientAPI { get; set; } private ILogger Logger { get; set; } private IChunkRenderer ChunkRenderer { get; set; } private JsonGenerator JsonGenerator { get; set; } private const string _mapPath = @"Maps"; private const string _chunkPath = @"Chunks"; private const string _domain = @"automap"; private const string chunkFile_filter = @"*_*.png"; private const string poiFileName = @"poi_binary"; private const string eoiFileName = @"eoi_binary"; private const string pointsTsvFileName = @"points_of_interest.tsv"; private static Regex chunkShardRegex = new Regex(@"(?[\d]+)_(?[\d]+).png", RegexOptions.Singleline); private ConcurrentDictionary columnCounter = new ConcurrentDictionary(3, 150); private ColumnsMetadata chunkTopMetadata; private PointsOfInterest POIs = new PointsOfInterest(); private EntitiesOfInterest EOIs = new EntitiesOfInterest(); internal Dictionary BlockID_Designators { get; private set; } internal Dictionary Entity_Designators { get; private set; } internal Dictionary RockIdCodes { get; private set; } internal CommandType CurrentState { get; set; } //Run status, Chunks processed, stats, center of map.... private uint nullChunkCount, nullMapCount, updatedChunksTotal; private Vec2i startChunkColumn; private readonly int chunkSize; private string path; private IAsset staticMap; private PersistedConfiguration configuration; public static string AutomapStatusEventKey = @"AutomapStatus"; public static string AutomapCommandEventKey = @"AutomapCommand"; public AutomapSystem(ICoreClientAPI clientAPI, ILogger logger, PersistedConfiguration config) { this.ClientAPI = clientAPI; this.Logger = logger; chunkSize = ClientAPI.World.BlockAccessor.ChunkSize; ClientAPI.Event.LevelFinalize += EngageAutomap; configuration = config; //TODO:Choose which one from GUI this.ChunkRenderer = new StandardRenderer(clientAPI, logger); //Listen on bus for commands ClientAPI.Event.RegisterEventBusListener(CommandListener, 1.0, AutomapSystem.AutomapCommandEventKey); if (configuration.Autostart) { CurrentState = CommandType.Run; Logger.Debug("Autostart is Enabled."); } } #region Internals private void EngageAutomap() { path = ClientAPI.GetOrCreateDataPath(_mapPath); path = ClientAPI.GetOrCreateDataPath(Path.Combine(path, "World_" + ClientAPI.World.Seed));//Add name of World too...'ServerApi.WorldManager.CurrentWorldName' JsonGenerator = new JsonGenerator(ClientAPI, Logger, path); string mapFilename = Path.Combine(path, "automap.html"); StreamWriter outputText = new StreamWriter(File.Open(mapFilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)); staticMap = ClientAPI.World.AssetManager.Get(new AssetLocation(_domain, "config/automap.html")); outputText.Write(staticMap.ToText()); outputText.Flush(); 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); Logger.Notification("AUTOMAP Start {0}", startChunkColumn); Reload_Metadata(); ClientAPI.Event.ChunkDirty += ChunkAChanging; cartographer_thread = new Thread(Cartographer) { Name = "Cartographer", Priority = ThreadPriority.Lowest, IsBackground = true }; ClientAPI.Event.RegisterGameTickListener(AwakenCartographer, 6000); } private void ChunkAChanging(Vec3i chunkCoord, IWorldChunk chunk, EnumChunkDirtyReason reason) { Vec2i topPosition = new Vec2i(chunkCoord.X, chunkCoord.Z); columnCounter.AddOrUpdate(topPosition, 1, (key, colAct) => colAct + 1); } private void AwakenCartographer(float delayed) { if (CurrentState == CommandType.Run && (ClientAPI.IsGamePaused != false || ClientAPI.IsShuttingDown != true)) { #if DEBUG Logger.VerboseDebug("Cartographer re-trigger from [{0}]", cartographer_thread.ThreadState); #endif if (cartographer_thread.ThreadState.HasFlag(ThreadState.Unstarted)) { cartographer_thread.Start(); } else if (cartographer_thread.ThreadState.HasFlag(ThreadState.WaitSleepJoin)) { //Time to (re)write chunk shards cartographer_thread.Interrupt(); } //#if DEBUG //ClientAPI.TriggerChatMessage($"Automap {updatedChunksTotal} Updates - MAX (N:{chunkTopMetadata.North_mostChunk},S:{chunkTopMetadata.South_mostChunk},E:{chunkTopMetadata.East_mostChunk}, W:{chunkTopMetadata.West_mostChunk} - TOTAL: {chunkTopMetadata.Count})"); //#endif } else if (CurrentState == CommandType.Snapshot) { //TODO: Snapshot generator second thread... } } private void Cartographer() { wake: Logger.VerboseDebug("Cartographer thread awoken"); try { uint ejectedItem = 0; uint updatedChunks = 0; uint updatedPixels = 0; //-- Should dodge enumerator changing underfoot....at a cost. if (!columnCounter.IsEmpty) { var tempSet = columnCounter.ToArray().OrderByDescending(kvp => kvp.Value); UpdateEntityMetadata(); foreach (var mostActiveCol in tempSet) { var mapChunk = ClientAPI.World.BlockAccessor.GetMapChunk(mostActiveCol.Key); if (mapChunk == null) { Logger.Warning("SKIP CHUNK: ({0}) - Map Chunk NULL!", mostActiveCol.Key); nullMapCount++; columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem); continue; } ColumnMeta chunkMeta; if (chunkTopMetadata.Contains(mostActiveCol.Key)) { chunkMeta = chunkTopMetadata[mostActiveCol.Key]; #if DEBUG Logger.VerboseDebug("Loaded chunk {0}", mostActiveCol.Key); //Console.WriteLine($"Load {mostActiveCol.Key}"); #endif } else { chunkMeta = CreateColumnMetadata(mostActiveCol, mapChunk); #if DEBUG Logger.VerboseDebug("Created chunk {0}", mostActiveCol.Key); //Console.WriteLine($"Created chunk {mostActiveCol.Key}"); #endif } ProcessChunkBlocks(mostActiveCol.Key, mapChunk, ref chunkMeta); PngWriter pngWriter = SetupPngImage(mostActiveCol.Key, ref chunkMeta); ChunkRenderer.GenerateChunkPngShard(mostActiveCol.Key, mapChunk, chunkMeta, pngWriter, out updatedPixels); if (updatedPixels > 0) { #if DEBUG Logger.VerboseDebug("Wrote chunk shard: ({0}) - Edits#:{1}, Pixels#:{2}", mostActiveCol.Key, mostActiveCol.Value, updatedPixels); #endif updatedChunks++; chunkTopMetadata.Update(chunkMeta); columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem); } else { columnCounter.TryRemove(mostActiveCol.Key, out ejectedItem); Logger.VerboseDebug("Un-painted chunk: ({0}) ", mostActiveCol.Key); } } //Cleanup persisted Metadata... chunkTopMetadata.ClearMetadata( ); } UpdateStatus(this.updatedChunksTotal, this.nullChunkCount, updatedChunks); if (updatedChunks > 0) { //What about chunk updates themselves; a update bitmap isn't kept... updatedChunksTotal += updatedChunks; JsonGenerator.GenerateJSONMetadata(chunkTopMetadata, startChunkColumn, POIs, EOIs, RockIdCodes); updatedChunks = 0; } //Then sleep until interupted again, and repeat Logger.VerboseDebug("Thread '{0}' about to sleep indefinitely.", Thread.CurrentThread.Name); Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException) { Logger.VerboseDebug("Thread '{0}' interupted [awoken]", Thread.CurrentThread.Name); goto wake; } catch (ThreadAbortException) { Logger.VerboseDebug("Thread '{0}' aborted.", Thread.CurrentThread.Name); } finally { Logger.VerboseDebug("Thread '{0}' executing finally block.", Thread.CurrentThread.Name); PersistPointsData(); } } private void UpdateStatus(uint totalUpdates, uint voidChunks, uint delta) { StatusData updateData = new StatusData(totalUpdates, voidChunks, delta, CommandType.Run); this.ClientAPI.Event.PushEvent(AutomapStatusEventKey, updateData); } private void Prefill_POI_Designators() { this.BlockID_Designators = new Dictionary(); this.Entity_Designators = new Dictionary(); this.RockIdCodes = Helpers.ArbitrarytBlockIdHunter(ClientAPI, new AssetLocation(GlobalConstants.DefaultDomain, "rock-"), EnumBlockMaterial.Stone); //Add special marker types for BlockID's of "Interest", overwrite colour, and method Reload_POI_Designators(); } private void Reload_POI_Designators() { Logger.VerboseDebug("Connecting {0} Configured Block-Designators", configuration.BlockDesignators.Count); foreach (var designator in configuration.BlockDesignators) { var blockIDs = Helpers.ArbitrarytBlockIdHunter(ClientAPI, designator.Pattern, designator.Material); if (blockIDs.Count > 0) { Logger.VerboseDebug("Designator {0} has {1} associated blockIDs", designator.ToString(), blockIDs.Count); } foreach (var entry in blockIDs) { BlockID_Designators.Add(entry.Key, designator); } } this.ChunkRenderer.BlockID_Designators = BlockID_Designators; Logger.VerboseDebug("Connecting {0} Configured Entity-Designators", configuration.EntityDesignators.Count); foreach (var designator in configuration.EntityDesignators) { //Get Variants first, from EntityTypes...better be populated! var matched = ClientAPI.World.EntityTypes.FindAll(entp => entp.Code.BeginsWith(designator.Pattern.Domain, designator.Pattern.Path)); foreach (var match in matched) { Logger.VerboseDebug("Linked Entity: {0} Designator: {1}", match.Code, designator); this.Entity_Designators.Add(match.Code, designator); } //EntityProperties props = ClientAPI.World.GetEntityType(designator.Pattern); } } /// /// Store Points/Entity of Interest /// private void PersistPointsData( ) { //POI and EOI raw dump files ~ WRITE em! //var poiRawFile = File. string poiPath = Path.Combine(path, poiFileName); string eoiPath = Path.Combine(path, eoiFileName); if (this.POIs.Count > 0) { using (var poiFile = File.OpenWrite(poiPath)) { Serializer.Serialize(poiFile, this.POIs); } } if (this.EOIs.Count > 0) { using (var eoiFile = File.OpenWrite(eoiPath)) { Serializer.Serialize(eoiFile, this.EOIs); } } //Create Easy to Parse TSV file for tool/human use.... string pointsTsvPath = Path.Combine(path, pointsTsvFileName); using (var tsvWriter = new StreamWriter(pointsTsvPath, false, Encoding.UTF8)) { tsvWriter.WriteLine("Name\tDescription\tLocation\tTime\t"); foreach (var point in this.POIs) { tsvWriter.Write(point.Name + "\t"); tsvWriter.Write(point.Notes + "\t"); tsvWriter.Write(point.Location.PrettyCoords(ClientAPI) + "\t"); tsvWriter.Write(point.Timestamp.ToString("u")+"\t"); tsvWriter.WriteLine(); } foreach (var entity in this.EOIs) { tsvWriter.Write(entity.Name + "\t"); tsvWriter.Write(entity.Notes + "\t"); tsvWriter.Write(entity.Location.PrettyCoords(ClientAPI) + "\t"); tsvWriter.Write(entity.Timestamp.ToString("u") + "\t"); tsvWriter.WriteLine( ); } tsvWriter.WriteLine(); tsvWriter.Flush( ); } } private ColumnMeta CreateColumnMetadata(KeyValuePair mostActiveCol, IMapChunk mapChunk) { ColumnMeta data = new ColumnMeta(mostActiveCol.Key.Copy(), ClientAPI, (byte) chunkSize); BlockPos equivBP = new BlockPos(mostActiveCol.Key.X * chunkSize, mapChunk.YMax, mostActiveCol.Key.Y * chunkSize); var climate = ClientAPI.World.BlockAccessor.GetClimateAt(equivBP); data.UpdateFieldsFrom(climate, mapChunk, TimeSpan.FromHours(ClientAPI.World.Calendar.TotalHours)); return data; } /// /// Reload chunk bounds from chunk shards /// /// The metadata. private void Reload_Metadata() { var worldmapDir = new DirectoryInfo(path); if (!worldmapDir.Exists) { #if DEBUG Logger.VerboseDebug("Could not open world map directory"); #endif return; } var shardFiles = worldmapDir.GetFiles(chunkFile_filter); if (shardFiles.Length > 0) { #if DEBUG Logger.VerboseDebug("Metadata reloading from {0} shards", shardFiles.Length); #endif foreach (var shardFile in shardFiles) { if (shardFile.Length < 1024) continue; var result = chunkShardRegex.Match(shardFile.Name); if (result.Success) { int X_chunk_pos = int.Parse(result.Groups["X"].Value); int Z_chunk_pos = int.Parse(result.Groups["Z"].Value); try { using (var fileStream = shardFile.OpenRead()) { PngReader pngRead = new PngReader(fileStream); pngRead.ReadSkippingAllRows(); pngRead.End(); //Parse PNG chunks for METADATA in shard PngMetadataChunk metadataFromPng = pngRead.GetChunksList().GetById1(PngMetadataChunk.ID) as PngMetadataChunk; var column = metadataFromPng.ChunkMetadata; if (column.PrettyLocation == null) column = column.Reload(ClientAPI); chunkTopMetadata.Add(column); } } catch (PngjException someEx) { Logger.Error("PNG Corruption file '{0}' - Reason: {1}", shardFile.Name, someEx); continue; } } } } //POI and EOI raw dump files ~ reload em! //var poiRawFile = File. string poiPath = Path.Combine(path, poiFileName); string eoiPath = Path.Combine(path, eoiFileName); if (File.Exists(poiPath)) { using (var poiFile = File.OpenRead(poiPath)) { this.POIs = Serializer.Deserialize(poiFile); Logger.VerboseDebug("Reloaded {0} POIs from file.", this.POIs.Count); } } if (File.Exists(eoiPath)) { using (var eoiFile = File.OpenRead(eoiPath)) { this.EOIs = Serializer.Deserialize(eoiFile); Logger.VerboseDebug("Reloaded {0} EOIs from file.", this.EOIs.Count); } } } private PngWriter SetupPngImage(Vec2i coord, ref ColumnMeta metadata) { ImageInfo imageInf = new ImageInfo(chunkSize, chunkSize, 8, false); string filename = $"{coord.X}_{coord.Y}.png"; filename = Path.Combine(path, filename); PngWriter pngWriter = FileHelper.CreatePngWriter(filename, imageInf, true); PngMetadata meta = pngWriter.GetMetadata(); meta.SetTimeNow(); meta.SetText("Chunk_X", coord.X.ToString("D")); meta.SetText("Chunk_Y", coord.Y.ToString("D")); //Setup specialized meta-data PNG chunks here... PngMetadataChunk pngChunkMeta = new PngMetadataChunk(pngWriter.ImgInfo) { ChunkMetadata = metadata }; pngWriter.GetChunksList().Queue(pngChunkMeta); pngWriter.CompLevel = 5;// 9 is the maximum compression but thats too high for the small benefit it gives pngWriter.CompressionStrategy = Hjg.Pngcs.Zlib.EDeflateCompressStrategy.Huffman; return pngWriter; } /// /// Does the heavy lifting of Scanning columns of chunks - scans for BlockEntity, creates Heightmap and stats... /// /// Chunk Coordinate /// Map chunk. /// Chunk metadata private void ProcessChunkBlocks(Vec2i key, IMapChunk mapChunk, ref ColumnMeta chunkMeta) { int targetChunkY = mapChunk.YMax / chunkSize;//Surface ... for (; targetChunkY > 0; targetChunkY--) { WorldChunk chunkData = ClientAPI.World.BlockAccessor.GetChunk(key.X, targetChunkY, key.Y) as WorldChunk; if (chunkData == null || chunkData.BlockEntities == null) { #if DEBUG Logger.VerboseDebug("Chunk null or empty X{0} Y{1} Z{2}", key.X, targetChunkY, key.Y); #endif nullChunkCount++; continue; } /*************** Chunk Entities Scanning *********************/ if (chunkData.BlockEntities != null && chunkData.BlockEntities.Length > 0) { #if DEBUG Logger.VerboseDebug("Surface@ {0} = BlockEntities: {1}", key, chunkData.BlockEntities.Length); #endif foreach (var blockEnt in chunkData.BlockEntities) { if (blockEnt != null && blockEnt.Block != null && BlockID_Designators.ContainsKey(blockEnt.Block.BlockId)) { var designator = BlockID_Designators[blockEnt.Block.BlockId]; designator.SpecialAction(ClientAPI, POIs, blockEnt.Pos.Copy(), blockEnt.Block); } } } /********************* Chunk/Column BLOCKs scanning ****************/ //Heightmap, Stats, block tally chunkData.Unpack(); int X_index, Y_index, Z_index; X_index = Y_index = Z_index = 0; //Ensure ChunkData Metadata fields arn't null...due to being tossed out if (chunkMeta.HeightMap == null) { chunkMeta.HeightMap = new ushort[chunkSize, chunkSize]; } if (chunkMeta.RockRatio == null) { chunkMeta.RockRatio = new Dictionary(10); } do { do { do { /* Encode packed indicie (y * chunksize + z) * chunksize + x */ var indicie = Helpers.ChunkBlockIndicie16(X_index, Y_index, Z_index); int aBlockId = chunkData.Blocks[indicie]; if (aBlockId == 0) {//Air chunkMeta.AirBlocks++; continue; } if (RockIdCodes.ContainsKey(aBlockId)) { if (chunkMeta.RockRatio.ContainsKey(aBlockId)) { chunkMeta.RockRatio[aBlockId]++; } else { chunkMeta.RockRatio.Add(aBlockId, 1); } } chunkMeta.NonAirBlocks++; //Heightmap if (chunkMeta.HeightMap[X_index, Z_index] == 0) { chunkMeta.HeightMap[X_index, Z_index] = (ushort) (Y_index + (targetChunkY * chunkSize)); } } while (X_index++ < (chunkSize - 1)); X_index = 0; } while (Z_index++ < (chunkSize - 1)); Z_index = 0; } while (Y_index++ < (chunkSize - 1)); } } private void UpdateEntityMetadata() { Logger.Debug("Presently {0} Entities", ClientAPI.World.LoadedEntities.Count); //Mabey scan only for 'new' entities by tracking ID in set? foreach (var loadedEntity in ClientAPI.World.LoadedEntities.ToArray()) { #if DEBUG //Logger.VerboseDebug($"ENTITY: ({loadedEntity.Value.Code}) = #{loadedEntity.Value.EntityId} {loadedEntity.Value.State} {loadedEntity.Value.LocalPos} <<<<<<<<<<<<"); #endif var dMatch = Entity_Designators.SingleOrDefault(se => se.Key.Equals(loadedEntity.Value.Code)); if (dMatch.Value != null) { dMatch.Value.SpecialAction(ClientAPI, this.EOIs, loadedEntity.Value.LocalPos.AsBlockPos.Copy(), loadedEntity.Value); } } } private void AddNote(string notation) { var playerNodePoi = new PointOfInterest() { Name = "Note", Location = ClientAPI.World.Player.Entity.LocalPos.AsBlockPos.Copy(), Notes = notation, Timestamp = DateTime.UtcNow, }; this.POIs.AddReplace(playerNodePoi); } private void CommandListener(string eventName, ref EnumHandling handling, IAttribute data) { Logger.VerboseDebug("MsgBus RX: AutomapCommandMsg: {0}", data.ToJsonToken()); CommandData cmdData = data as CommandData; if (CurrentState != CommandType.Snapshot) { switch (cmdData.State) { case CommandType.Run: case CommandType.Stop: if (CurrentState != cmdData.State) { CurrentState = cmdData.State; AwakenCartographer(0.0f); } break; case CommandType.Snapshot: CurrentState = CommandType.Stop; //Snapshot starts a second thread/process... break; case CommandType.Notation: //Add to POI list where player location AddNote(cmdData.Notation); break; } } #if DEBUG ClientAPI.TriggerChatMessage($"Automap commanded to: {cmdData.State} "); #endif } #endregion } }