public static void WriteMapModels(ARealmReversed realm, TerritoryType teriType, SaintCoinach.Graphics.Territory teri = null)
        {
            string name = teri != null ? teri.Name : teriType.Name;

            //Plot.Ward ward = Plot.StringToWard(teriType.Name);
            string outpath = Path.Combine(FFXIVHSPaths.GetRootDirectory(), name, "objects\\");
            string inpath  = Path.Combine(FFXIVHSPaths.GetRootDirectory(), name + "\\", name + ".json");

            if (!Directory.Exists(outpath))
            {
                Directory.CreateDirectory(outpath);
            }

            //string inpath = FFXIVHSPaths.GetWardJson(ward);
            if (!File.Exists(inpath))
            {
                throw new FileNotFoundException();
            }

            //string outpath = FFXIVHSPaths.GetWardObjectsDirectory(ward);

            string json = File.ReadAllText(inpath);

            Map map = JsonConvert.DeserializeObject <Map>(json);

            foreach (MapModel model in map.models.Values)
            {
                if (realm.Packs.TryGetFile(model.modelPath, out SaintCoinach.IO.File f))
                {
                    ObjectFileWriter.WriteObjectFile(outpath, (ModelFile)f);
                }
            }
        }
        public static void WriteMap(ARealmReversed realm, TerritoryType teriType, SaintCoinach.Graphics.Territory teri = null)
        {
            string name = teri != null ? teri.Name : teriType.Name;

            //Plot.Ward ward = Plot.StringToWard(teriType.Name);
            string outdir  = Path.Combine(FFXIVHSPaths.GetRootDirectory(), name + "\\");
            string outpath = Path.Combine(outdir, name + ".json");

            //string outpath = FFXIVHSPaths.GetWardJson(ward);

            if (!Directory.Exists(outdir))
            {
                Directory.CreateDirectory(outdir);
            }

            if (File.Exists(outpath))
            {
                WriteMapModels(realm, teriType, teri);
                return;
            }

            Map map = ReadTerritory(teriType, teri);

            string json = JsonConvert.SerializeObject(map, Formatting.Indented);

            File.WriteAllText(outpath, json);
        }
        public static void WriteMapModels(ARealmReversed realm, TerritoryType teriType)
        {
            Territory territory = Plot.StringToWard(teriType.Name);

            string inpath = FFXIVHSPaths.GetTerritoryJson(territory);

            if (!File.Exists(inpath))
            {
                throw new FileNotFoundException();
            }

            string outpath = FFXIVHSPaths.GetTerritoryObjectsDirectory(territory);

            string json = File.ReadAllText(inpath);

            Map map = JsonConvert.DeserializeObject <Map>(json);

            foreach (MapModel model in map.models.Values)
            {
                if (realm.Packs.TryGetFile(model.modelPath, out SaintCoinach.IO.File f))
                {
                    ObjectFileWriter.WriteObjectFile(outpath, (ModelFile)f);
                }
            }
        }
 /// <summary>
 /// Parses an SgbFile for model entries or further gimmicks, and adds groups
 /// to the given MapGroup.
 /// </summary>
 /// <param name="file"></param>
 /// <param name="parent"></param>
 /// <param name="models"></param>
 private static void AddSgbModelsToMap(ref Map map, ref MapGroup mg, SgbFile file)
 {
     foreach (var sgbGroup in file.Data.OfType <SgbGroup>())
     {
         foreach (var mdl in sgbGroup.Entries.OfType <SgbModelEntry>())
         {
             int modelId = map.TryAddUniqueModel(mdl.Model.Model.ToMapModel());
             mg.AddEntry(mdl.Model.ToMapModelEntry(modelId));
         }
     }
 }
        public static void WriteMap(ARealmReversed realm, TerritoryType teriType)
        {
            Territory territory = Plot.StringToWard(teriType.Name);

            string outpath = FFXIVHSPaths.GetTerritoryJson(territory);

            if (File.Exists(outpath))
            {
                WriteMapModels(realm, teriType);
                return;
            }

            Map map = ReadTerritory(teriType);

            string json = JsonConvert.SerializeObject(map, Formatting.Indented);

            File.WriteAllText(outpath, json);
        }
        /// <summary>
        /// Returns a Map containing all groups in the Territory instantiated via
        /// the given TerritoryType.
        /// </summary>
        /// <param name="teriType"></param>
        /// <returns></returns>
        private static Map ReadTerritory(TerritoryType teriType, SaintCoinach.Graphics.Territory teri = null)
        {
            Map map = new Map();

            teri = teri ?? new SaintCoinach.Graphics.Territory(teriType);

            if (teri.Terrain != null)
            {
                MapGroup terrainMapGroup = new MapGroup(MapGroup.GroupType.TERRAIN, "teri");
                terrainMapGroup.groupTransform = Transform.Empty;

                foreach (TransformedModel mdl in teri.Terrain.Parts)
                {
                    int modelId = map.TryAddUniqueModel(mdl.Model.ToMapModel());
                    terrainMapGroup.AddEntry(mdl.ToMapModelEntry(modelId));
                }
                map.AddMapGroup(terrainMapGroup);
            }

            foreach (LgbFile lgbFile in teri.LgbFiles)
            {
                var validGroups = lgbFile.Groups.Where(_ => !EventCheck(_.Name)).Select(_ => _);

                foreach (LgbGroup lgbGroup in validGroups)
                {
                    MapGroup lgbMapGroup = new MapGroup(MapGroup.GroupType.LGB, lgbGroup.Name);
                    lgbMapGroup.groupTransform = Transform.Empty;

                    foreach (var mdl in lgbGroup.Entries.OfType <LgbModelEntry>())
                    {
                        int modelId = map.TryAddUniqueModel(mdl.Model.Model.ToMapModel());
                        lgbMapGroup.AddEntry(mdl.Model.ToMapModelEntry(modelId));
                    }

                    foreach (var gim in lgbGroup.Entries.OfType <LgbGimmickEntry>())
                    {
                        MapGroup gimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(gim.Name, gim.Gimmick.File.Path));
                        gimMapGroup.groupTransform = TransformFromGimmickHeader(gim.Header);

                        AddSgbModelsToMap(ref map, ref gimMapGroup, gim.Gimmick);

                        foreach (var rootGimGroup in gim.Gimmick.Data.OfType <SgbGroup>())
                        {
                            foreach (var rootGimEntry in rootGimGroup.Entries.OfType <SgbGimmickEntry>())
                            {
                                if (rootGimEntry.Gimmick != null)
                                {
                                    MapGroup rootGimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(rootGimEntry.Name, rootGimEntry.Gimmick.File.Path));
                                    rootGimMapGroup.groupTransform = TransformFromGimmickHeader(rootGimEntry.Header);

                                    AddSgbModelsToMap(ref map, ref rootGimMapGroup, rootGimEntry.Gimmick);

                                    foreach (var subGimGroup in rootGimEntry.Gimmick.Data.OfType <SgbGroup>())
                                    {
                                        foreach (var subGimEntry in subGimGroup.Entries.OfType <SgbGimmickEntry>())
                                        {
                                            MapGroup subGimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(subGimEntry.Name, subGimEntry.Gimmick.File.Path));
                                            subGimMapGroup.groupTransform = TransformFromGimmickHeader(subGimEntry.Header);

                                            AddSgbModelsToMap(ref map, ref subGimMapGroup, subGimEntry.Gimmick);

                                            rootGimMapGroup.AddGroup(subGimMapGroup);
                                        }
                                    }
                                    gimMapGroup.AddGroup(rootGimMapGroup);
                                }
                            }
                        }
                        lgbMapGroup.AddGroup(gimMapGroup);
                    }

                    foreach (var eobj in lgbGroup.Entries.OfType <LgbEventObjectEntry>())
                    {
                        if (eobj.Gimmick == null)
                        {
                            continue;
                        }

                        MapGroup gimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(eobj.Name, eobj.Gimmick.File.Path));
                        gimMapGroup.groupTransform = TransformFromGimmickHeader(eobj.Header);

                        AddSgbModelsToMap(ref map, ref gimMapGroup, eobj.Gimmick);

                        foreach (var rootGimGroup in eobj.Gimmick.Data.OfType <SgbGroup>())
                        {
                            foreach (var rootGimEntry in rootGimGroup.Entries.OfType <SgbGimmickEntry>())
                            {
                                if (rootGimEntry.Gimmick != null)
                                {
                                    MapGroup rootGimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(rootGimEntry.Name, rootGimEntry.Gimmick.File.Path));
                                    rootGimMapGroup.groupTransform = TransformFromGimmickHeader(rootGimEntry.Header);

                                    AddSgbModelsToMap(ref map, ref rootGimMapGroup, rootGimEntry.Gimmick);

                                    foreach (var subGimGroup in rootGimEntry.Gimmick.Data.OfType <SgbGroup>())
                                    {
                                        foreach (var subGimEntry in subGimGroup.Entries.OfType <SgbGimmickEntry>())
                                        {
                                            MapGroup subGimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(subGimEntry.Name, subGimEntry.Gimmick.File.Path));
                                            subGimMapGroup.groupTransform = TransformFromGimmickHeader(subGimEntry.Header);

                                            AddSgbModelsToMap(ref map, ref subGimMapGroup, subGimEntry.Gimmick);

                                            rootGimMapGroup.AddGroup(subGimMapGroup);
                                        }
                                    }
                                    gimMapGroup.AddGroup(rootGimMapGroup);
                                }
                            }
                            foreach (var sgb1cEntry in rootGimGroup.Entries.OfType <SgbGroup1CEntry>())
                            {
                                if (sgb1cEntry.Gimmick != null)
                                {
                                    MapGroup rootGimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(sgb1cEntry.Name, sgb1cEntry.Gimmick.File.Path));
                                    rootGimMapGroup.groupTransform = Transform.Empty;

                                    AddSgbModelsToMap(ref map, ref rootGimMapGroup, sgb1cEntry.Gimmick);

                                    foreach (var subGimGroup in sgb1cEntry.Gimmick.Data.OfType <SgbGroup>())
                                    {
                                        foreach (var subGimEntry in subGimGroup.Entries.OfType <SgbGimmickEntry>())
                                        {
                                            MapGroup subGimMapGroup = new MapGroup(MapGroup.GroupType.SGB, GetGimmickName(subGimEntry.Name, subGimEntry.Gimmick.File.Path));
                                            subGimMapGroup.groupTransform = TransformFromGimmickHeader(subGimEntry.Header);

                                            AddSgbModelsToMap(ref map, ref subGimMapGroup, subGimEntry.Gimmick);

                                            rootGimMapGroup.AddGroup(subGimMapGroup);
                                        }
                                    }
                                    gimMapGroup.AddGroup(rootGimMapGroup);
                                }
                            }
                        }
                        lgbMapGroup.AddGroup(gimMapGroup);
                    }
                    map.AddMapGroup(lgbMapGroup);
                }
            }
            return(map);
        }