public bool TryGetTileInfo(TerrainTile r, out TerrainTileInfo info) { if (!Templates.TryGetValue(r.Type, out var tpl) || !tpl.Contains(r.Index)) { info = null; return(false); } info = tpl[r.Index]; return(info != null); }
protected virtual TerrainTileInfo LoadTileInfo(ITerrainInfo terrainInfo, MiniYaml my) { var tile = new TerrainTileInfo(); FieldLoader.Load(tile, my); // Terrain type must be converted from a string to an index tile.GetType().GetField(nameof(tile.TerrainType)).SetValue(tile, terrainInfo.GetTerrainIndex(my.Value)); // Fall back to the terrain-type color if necessary var overrideColor = terrainInfo.TerrainTypes[tile.TerrainType].Color; if (tile.MinColor == default) { tile.GetType().GetField(nameof(tile.MinColor)).SetValue(tile, overrideColor); } if (tile.MaxColor == default) { tile.GetType().GetField(nameof(tile.MaxColor)).SetValue(tile, overrideColor); } return(tile); }
void IUtilityCommand.Run(Utility utility, string[] args) { // HACK: The engine code assumes that Game.modData is set. var modData = Game.ModData = utility.ModData; var imageField = typeof(TerrainTemplateInfo).GetField("Image"); var pickAnyField = typeof(TerrainTemplateInfo).GetField("PickAny"); var tileInfoField = typeof(TerrainTemplateInfo).GetField("tileInfo", BindingFlags.NonPublic | BindingFlags.Instance); var terrainTypeField = typeof(TerrainTileInfo).GetField("TerrainType"); var terrainLeftColorField = typeof(TerrainTileInfo).GetField("LeftColor"); var terrainRightColorField = typeof(TerrainTileInfo).GetField("RightColor"); var empty = new Size(0, 0); var single = new int2(1, 1); var exts = new[] { "" }.Concat(args[1].Split(',')); foreach (var t in modData.Manifest.TileSets) { var ts = new TileSet(modData.DefaultFileSystem, t); var frameCache = new FrameCache(modData.DefaultFileSystem, modData.SpriteLoaders); Console.WriteLine("Tileset: " + ts.Name); foreach (var template in ts.Templates.Values) { // Find the sprite associated with this template foreach (var ext in exts) { Stream s; if (modData.DefaultFileSystem.TryOpen(template.Images[0] + ext, out s)) { s.Dispose(); } else { continue; } // Rewrite the template image (normally readonly) using reflection imageField.SetValue(template, template.Images[0] + ext); // Fetch the private tileInfo array so that we can write new entries var tileInfo = (TerrainTileInfo[])tileInfoField.GetValue(template); // Open the file and search for any implicit frames var allFrames = frameCache[template.Images[0]]; var frames = template.Frames != null?template.Frames.Select(f => allFrames[f]).ToArray() : allFrames; // Resize array for new entries if (frames.Length > template.TilesCount) { var ti = new TerrainTileInfo[frames.Length]; Array.Copy(tileInfo, ti, template.TilesCount); tileInfoField.SetValue(template, ti); tileInfo = ti; } for (var i = 0; i < template.TilesCount; i++) { if (template[i] == null && frames[i] != null && frames[i].Size != empty) { tileInfo[i] = new TerrainTileInfo(); var ti = ts.GetTerrainIndex("Clear"); terrainTypeField.SetValue(tileInfo[i], ti); terrainLeftColorField.SetValue(tileInfo[i], ts[ti].Color); terrainRightColorField.SetValue(tileInfo[i], ts[ti].Color); Console.WriteLine("Fixing entry for {0}:{1}", template.Images[0], i); } } if (template.TilesCount > 1 && template.Size == single) { pickAnyField.SetValue(template, true); } } } ts.Save(t); } }
void IUtilityCommand.Run(Utility utility, string[] args) { // HACK: The engine code assumes that Game.modData is set. var modData = Game.ModData = utility.ModData; var imageField = typeof(TerrainTemplateInfo).GetField("Image"); var pickAnyField = typeof(TerrainTemplateInfo).GetField("PickAny"); var tileInfoField = typeof(TerrainTemplateInfo).GetField("tileInfo", BindingFlags.NonPublic | BindingFlags.Instance); var terrainTypeField = typeof(TerrainTileInfo).GetField("TerrainType"); var terrainLeftColorField = typeof(TerrainTileInfo).GetField("LeftColor"); var terrainRightColorField = typeof(TerrainTileInfo).GetField("RightColor"); var empty = new Size(0, 0); var single = new int2(1, 1); var exts = new[] { "" }.Concat(args[1].Split(',')); foreach (var t in modData.Manifest.TileSets) { var ts = new TileSet(modData.DefaultFileSystem, t); var frameCache = new FrameCache(modData.DefaultFileSystem, modData.SpriteLoaders); Console.WriteLine("Tileset: " + ts.Name); foreach (var template in ts.Templates.Values) { // Find the sprite associated with this template foreach (var ext in exts) { Stream s; if (modData.DefaultFileSystem.TryOpen(template.Images[0] + ext, out s)) s.Dispose(); else continue; // Rewrite the template image (normally readonly) using reflection imageField.SetValue(template, template.Images[0] + ext); // Fetch the private tileInfo array so that we can write new entries var tileInfo = (TerrainTileInfo[])tileInfoField.GetValue(template); // Open the file and search for any implicit frames var allFrames = frameCache[template.Images[0]]; var frames = template.Frames != null ? template.Frames.Select(f => allFrames[f]).ToArray() : allFrames; // Resize array for new entries if (frames.Length > template.TilesCount) { var ti = new TerrainTileInfo[frames.Length]; Array.Copy(tileInfo, ti, template.TilesCount); tileInfoField.SetValue(template, ti); tileInfo = ti; } for (var i = 0; i < template.TilesCount; i++) { if (template[i] == null && frames[i] != null && frames[i].Size != empty) { tileInfo[i] = new TerrainTileInfo(); var ti = ts.GetTerrainIndex("Clear"); terrainTypeField.SetValue(tileInfo[i], ti); terrainLeftColorField.SetValue(tileInfo[i], ts[ti].Color); terrainRightColorField.SetValue(tileInfo[i], ts[ti].Color); Console.WriteLine("Fixing entry for {0}:{1}", template.Images[0], i); } } if (template.TilesCount > 1 && template.Size == single) pickAnyField.SetValue(template, true); } } ts.Save(t); } }
MiniYaml SaveTileInfo(TerrainTileInfo t, TileSet tileSet) { return(new MiniYaml(tileSet.TerrainInfo[t.TerrainType].Type, new List <MiniYamlNode>())); }
void Export(string outputDir) { var dir = Platform.ResolvePath("^", outputDir); Directory.CreateDirectory(dir); var tilesetName = txtTilesetName.Text; var tilesetID = txtID.Text; var tilesetExt = txtExt.Text; if (tilesetName.Length < 1) { tilesetName = "Temperat"; } if (tilesetID.Length < 1) { tilesetID = "TEMPERAT"; } if (tilesetExt.Length < 1) { tilesetExt = ".tem"; } // Create a Tileset definition // TODO: Pull this info from the GUI var tilesetFile = tilesetName.ToLower(); if (tilesetFile.Length < 8) { tilesetFile = tilesetName.ToLower() + ".yaml"; } else { tilesetFile = tilesetName.ToLower().Substring(0, 8) + ".yaml"; } var tileset = new TileSet( name: tilesetName, id: tilesetID.ToUpper(), terrainInfo: terrainType); // List of files to add to the mix file var fileList = new List <string>(); // Export tile artwork foreach (var t in surface1.Templates) { fileList.Add(ExportTemplate(t, surface1.Templates.IndexOf(t), tilesetExt, dir)); } // Add the templates ushort cur = 0; templates.Clear(); foreach (var tp in surface1.Templates) { var tiles = new byte[tp.Width * tp.Height]; foreach (var t in tp.Cells) { var ttype = terrainType[surface1.TerrainTypes[t.Key.X, t.Key.Y]].Type; var idx = (t.Key.X - tp.Left) + tp.Width * (t.Key.Y - tp.Top); tiles[idx] = tileset.GetTerrainIndex(ttype); } var template = new TerrainTemplateInfo( id: cur, images: new[] { "{0}{1:00}".F(txtTilesetName.Text, cur) }, size: new int2(tp.Width, tp.Height), tiles: tiles); // HACK: This part of the TileSet code is broken upstream var field = template.GetType() .GetField("tileInfo", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); field.SetValue(template, tiles.Select(t => { var info = new TerrainTileInfo(); info.GetType().GetField("TerrainType").SetValue(info, t); return(info); }).ToArray()); templates[cur] = template; cur++; } SaveTileSet(tileset, Path.Combine(dir, tilesetFile)); Console.WriteLine("Finished export"); }
bool ITerrainInfo.TryGetTerrainInfo(TerrainTile r, out TerrainTileInfo info) { return(TryGetTileInfo(r, out info)); }