示例#1
0
 public static void Parse(string filepath, MainLocationManifest1_2 config)
 {
     // Print mod info into the log
     AdvancedLocationLoaderMod.Logger.Log((config.About.ModName == null ? "Legacy Mod" : config.About.ModName) + ", version `" + config.About.Version + "` by " + config.About.Author, LogLevel.Info);
     // Parse locations
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Locations` section...", LogLevel.Trace);
     if (config.Locations != null)
     {
         foreach (Location loc in config.Locations)
         {
             if (LocationChecks(filepath, loc.FileName, loc.MapName))
             {
                 if (!LocationTypes.Contains(loc.Type))
                 {
                     AdvancedLocationLoaderMod.Logger.Log("Unknown location Type, using `Default` instead: " + loc.ToString(), LogLevel.Error);
                     loc.Type = "Default";
                 }
                 loc.FileName = Path.Combine(filepath, loc.FileName);
                 Compound.Locations.Add(loc);
             }
         }
     }
     // Parse overrides
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Overrides` section...", LogLevel.Trace);
     if (config.Overrides != null)
     {
         foreach (Override ovr in config.Overrides)
         {
             if (LocationChecks(filepath, ovr.FileName, ovr.MapName))
             {
                 ovr.FileName = Path.Combine(filepath, ovr.FileName);
                 Compound.Overrides.Add(ovr);
             }
         }
     }
     // Parse redirects
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Redirects` section...", LogLevel.Trace);
     if (config.Redirects != null)
     {
         foreach (Redirect red in config.Redirects)
         {
             if (FileCheck(Game1.content.RootDirectory, red.FromFile) && FileCheck(filepath, red.ToFile))
             {
                 red.ToFile = Path.Combine(filepath, red.ToFile);
                 Compound.Redirects.Add(red);
             }
         }
     }
     // Parse tilesheets
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Tilesheets` section...", LogLevel.Trace);
     if (config.Tilesheets != null)
     {
         foreach (Tilesheet sht in config.Tilesheets)
         {
             if (sht.FileName != null)
             {
                 if (sht.Seasonal)
                 {
                     if (!FileCheck(filepath, sht.FileName + "_spring"))
                     {
                         continue;
                     }
                     if (!FileCheck(filepath, sht.FileName + "_summer"))
                     {
                         continue;
                     }
                     if (!FileCheck(filepath, sht.FileName + "_fall"))
                     {
                         continue;
                     }
                     if (!FileCheck(filepath, sht.FileName + "_winter"))
                     {
                         continue;
                     }
                 }
                 else if (!FileCheck(filepath, sht.FileName))
                 {
                     continue;
                 }
             }
             if (!TilesheetCache.ContainsKey(sht.MapName))
             {
                 TilesheetCache.Add(sht.MapName, new List <string>());
             }
             TilesheetCache[sht.MapName].Add(sht.SheetId);
             if (sht.FileName != null)
             {
                 sht.FileName = Path.Combine(filepath, sht.FileName);
             }
             Compound.Tilesheets.Add(sht);
         }
     }
     // Parse tiles
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `tiles` section...", LogLevel.Trace);
     if (config.Tiles != null)
     {
         foreach (Tile til in config.Tiles)
         {
             if (!Layers.Contains(til.LayerId))
             {
                 AdvancedLocationLoaderMod.Logger.Log("Cannot place tile `" + til.ToString() + "` on unknown layer: " + til.LayerId, LogLevel.Error);
             }
             else
             {
                 Compound.Tiles.Add(til);
             }
         }
     }
     // Parse properties
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Properties` section...", LogLevel.Trace);
     if (config.Properties != null)
     {
         foreach (Property pro in config.Properties)
         {
             if (!Layers.Contains(pro.LayerId))
             {
                 AdvancedLocationLoaderMod.Logger.Log("Cannot apply property `" + pro.ToString() + "` to tile unknown layer: " + pro.LayerId, LogLevel.Error);
             }
             else
             {
                 Compound.Properties.Add(pro);
             }
         }
     }
     // Parse warps
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Warps` section...", LogLevel.Trace);
     if (config.Warps != null)
     {
         foreach (Warp war in config.Warps)
         {
             Compound.Warps.Add(war);
         }
     }
     // Parse conditionals
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Conditionals` section...", LogLevel.Trace);
     if (config.Conditionals != null)
     {
         foreach (Conditional con in config.Conditionals)
         {
             if (con.Item < -1)
             {
                 AdvancedLocationLoaderMod.Logger.Log("Unable to parse conditional, it references a null item: " + con.ToString(), LogLevel.Error);
             }
             else if (con.Amount < 1)
             {
                 AdvancedLocationLoaderMod.Logger.Log("Unable to validate conditional, the item amount is less then 1: " + con.ToString(), LogLevel.Error);
             }
             else if (Conditionals.Contains(con.Name))
             {
                 AdvancedLocationLoaderMod.Logger.Log("Unable to validate conditional, another condition with this name already exists: " + con.ToString(), LogLevel.Error);
             }
             else
             {
                 Configs.Compound.Conditionals.Add(con);
                 Conditionals.Add(con.Name);
             }
         }
     }
     // Parse minecarts
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Teleporters` section...", LogLevel.Trace);
     if (config.Teleporters != null)
     {
         foreach (TeleporterList min in config.Teleporters)
         {
             bool add = true;
             foreach (TeleporterList tes in Compound.Teleporters)
             {
                 if (tes.ListName == min.ListName)
                 {
                     add = false;
                     foreach (TeleporterDestination dest in min.Destinations)
                     {
                         if (tes.Destinations.TrueForAll(a => { return(!a.Equals(dest)); }))
                         {
                             tes.Destinations.Add(dest);
                         }
                         else
                         {
                             AdvancedLocationLoaderMod.Logger.Log("Unable to add teleporter destination for the `" + min.ListName + "` teleporter, the destination already exists: `" + dest.ToString(), LogLevel.Error);
                         }
                     }
                     AdvancedLocationLoaderMod.Logger.Log("Teleporter updated: " + tes.ToString(), LogLevel.Trace);
                     break;
                 }
             }
             if (add)
             {
                 Compound.Teleporters.Add(min);
                 AdvancedLocationLoaderMod.Logger.Log("Teleporter created: " + min.ToString(), LogLevel.Trace);
             }
         }
     }
     // Parse shops
     AdvancedLocationLoaderMod.Logger.Log("Parsing the `Shops` section...", LogLevel.Trace);
     if (config.Shops != null)
     {
         foreach (string shop in config.Shops)
         {
             string path = Path.Combine(filepath, shop + ".json");
             if (!File.Exists(path))
             {
                 AdvancedLocationLoaderMod.Logger.Log("Unable to load shop, file does not exist: " + path, LogLevel.Error);
             }
             else
             {
                 try
                 {
                     ShopConfig cfg = JsonConvert.DeserializeObject <ShopConfig>(File.ReadAllText(path));
                     cfg.Portrait = Path.Combine(filepath, cfg.Portrait);
                     Configs.Compound.Shops.Add(shop, cfg);
                 }
                 catch (Exception err)
                 {
                     AdvancedLocationLoaderMod.Logger.Log(LogLevel.Error, "Could not load shop due to unexpected error: " + path, err);
                     continue;
                 }
             }
         }
     }
 }
示例#2
0
        public static void Load(string filepath)
        {
            AdvancedLocationLoaderMod.Logger.Log("Converting legacy 1.1 manifest to new 1.2 format...", LogLevel.Debug);
            LocationConfig1_1 Config;

            try
            {
                Config = JsonConvert.DeserializeObject <LocationConfig1_1>(File.ReadAllText(filepath));
            }
            catch (Exception err)
            {
                AdvancedLocationLoaderMod.Logger.Log(LogLevel.Error, "Unable to load legacy manifest, json cannot be parsed: " + filepath, err);
                return;
            }
            MainLocationManifest1_2 Updated = new MainLocationManifest1_2();

            // Prepare the 1.2 properties
            Updated.LoaderVersion = "1.2.0";
            AdvancedLocationLoaderMod.Logger.Log("Converting the `about` section", LogLevel.Trace);
            // Convert the `about` section
            Updated.About = new About();
            if (Config.about.ContainsKey("author"))
            {
                Updated.About.Author = Config.about["author"];
            }
            if (Config.about.ContainsKey("description"))
            {
                Updated.About.Description = Config.about["description"];
            }
            if (Config.about.ContainsKey("version"))
            {
                Updated.About.Version = Config.about["version"];
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `locations` section", LogLevel.Trace);
            // Convert the `locations` section
            if (Config.locations != null)
            {
                Updated.Locations = new List <Location>();
                foreach (Dictionary <string, string> loc in Config.locations)
                {
                    Location newLoc = new Location();
                    newLoc.Farmable = loc.ContainsKey("farmable") ? Convert.ToBoolean(loc["farmable"]) : false;
                    newLoc.Outdoor  = loc.ContainsKey("outdoor") ? Convert.ToBoolean(loc["outdoor"]) : false;
                    newLoc.FileName = loc["file"];
                    newLoc.MapName  = loc["name"];
                    newLoc.Type     = "Default";
                    Updated.Locations.Add(newLoc);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `overrides` section", LogLevel.Trace);
            // Convert the `overrides` section
            if (Config.overrides != null)
            {
                Updated.Overrides = new List <Override>();
                foreach (Dictionary <string, string> ovr in Config.overrides)
                {
                    Override newOvr = new Override();
                    newOvr.FileName = ovr["file"];
                    newOvr.MapName  = ovr["name"];
                    Updated.Overrides.Add(newOvr);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `tilesheets` section", LogLevel.Trace);
            // Convert the `tilesheets` section
            if (Config.tilesheets != null)
            {
                Updated.Tilesheets = new List <Tilesheet>();
                foreach (Dictionary <string, string> sheet in Config.tilesheets)
                {
                    Tilesheet newSheet = new Tilesheet();
                    newSheet.FileName = sheet["file"];
                    newSheet.MapName  = sheet["map"];
                    newSheet.SheetId  = sheet["sheet"];
                    newSheet.Seasonal = sheet.ContainsKey("seasonal") ? Convert.ToBoolean(sheet["seasonal"]) : false;
                    Updated.Tilesheets.Add(newSheet);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `tiles` section", LogLevel.Trace);
            // Convert the `tiles` section
            if (Config.tiles != null)
            {
                Updated.Tiles = new List <Tile>();
                foreach (Dictionary <string, string> tile in Config.tiles)
                {
                    Tile newTile = new Tile();
                    newTile.TileX   = Convert.ToInt32(tile["x"]);
                    newTile.TileY   = Convert.ToInt32(tile["y"]);
                    newTile.MapName = tile["map"];
                    newTile.LayerId = tile["layer"];
                    newTile.SheetId = tile.ContainsKey("sheet") ? tile["sheet"] : null;
                    if (tile.ContainsKey("interval"))
                    {
                        newTile.Interval    = Convert.ToInt32(tile["interval"]);
                        newTile.TileIndexes = new List <string>(tile["tileIndex"].Split(',')).ConvertAll(Convert.ToInt32).ToArray();
                    }
                    else
                    {
                        newTile.TileIndex = Convert.ToInt32(tile["tileIndex"]);
                    }
                    newTile.Conditions = tile.ContainsKey("conditions") ? tile["conditions"] : null;
                    Updated.Tiles.Add(newTile);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `properties` section", LogLevel.Trace);
            // Convert the `properties` section
            if (Config.properties != null)
            {
                Updated.Properties = new List <Property>();
                foreach (List <string> prop in Config.properties)
                {
                    Property newProp = new Property();
                    newProp.MapName = prop[0];
                    newProp.LayerId = prop[1];
                    newProp.TileX   = Convert.ToInt32(prop[2]);
                    newProp.TileY   = Convert.ToInt32(prop[3]);
                    newProp.Key     = prop[4];
                    newProp.Value   = prop[5];
                    Updated.Properties.Add(newProp);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `warps` section", LogLevel.Trace);
            // Convert the `warps` section
            if (Config.warps != null)
            {
                Updated.Warps = new List <Warp>();
                foreach (List <string> warp in Config.warps)
                {
                    Warp newWarp = new Warp();
                    newWarp.MapName    = warp[0];
                    newWarp.TileX      = Convert.ToInt32(warp[1]);
                    newWarp.TileY      = Convert.ToInt32(warp[2]);
                    newWarp.TargetName = warp[3];
                    newWarp.TargetX    = Convert.ToInt32(warp[4]);
                    newWarp.TargetY    = Convert.ToInt32(warp[5]);
                    Updated.Warps.Add(newWarp);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `conditions` section", LogLevel.Trace);
            // Convert the `conditions` into the new `Conditionals` section
            if (Config.conditions != null)
            {
                Updated.Conditionals = new List <Conditional>();
                foreach (KeyValuePair <string, Dictionary <string, string> > cond in Config.conditions)
                {
                    Conditional newCond = new Conditional();
                    newCond.Name     = cond.Key;
                    newCond.Item     = Convert.ToInt32(cond.Value["item"]);
                    newCond.Amount   = Convert.ToInt32(cond.Value["amount"]);
                    newCond.Question = cond.Value["question"];
                    Updated.Conditionals.Add(newCond);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `minecarts` section", LogLevel.Trace);
            // Convert the `minecarts` section
            if (Config.minecarts != null)
            {
                Updated.Teleporters = new List <TeleporterList>();
                foreach (KeyValuePair <string, Dictionary <string, List <string> > > set in Config.minecarts)
                {
                    TeleporterList newSet = new TeleporterList();
                    newSet.ListName = set.Key;
                    foreach (KeyValuePair <string, List <string> > dest in set.Value)
                    {
                        TeleporterDestination newDest = new TeleporterDestination();
                        newDest.ItemText  = dest.Key;
                        newDest.MapName   = dest.Value[0];
                        newDest.TileX     = Convert.ToInt32(dest.Value[1]);
                        newDest.TileY     = Convert.ToInt32(dest.Value[2]);
                        newDest.Direction = Convert.ToInt32(dest.Value[3]);
                        newSet.Destinations.Add(newDest);
                    }
                    Updated.Teleporters.Add(newSet);
                }
            }
            AdvancedLocationLoaderMod.Logger.Log("Converting the `shops` section", LogLevel.Trace);
            // Convert the `shops` section
            Updated.Shops = Config.shops;
            // Remove empty fields
            if (Updated.Conditionals.Count == 0)
            {
                Updated.Conditionals = null;
            }
            if (Updated.Locations.Count == 0)
            {
                Updated.Locations = null;
            }
            if (Updated.Overrides.Count == 0)
            {
                Updated.Overrides = null;
            }
            if (Updated.Properties.Count == 0)
            {
                Updated.Properties = null;
            }
            if (Updated.Redirects.Count == 0)
            {
                Updated.Redirects = null;
            }
            if (Updated.Shops.Count == 0)
            {
                Updated.Shops = null;
            }
            if (Updated.Teleporters.Count == 0)
            {
                Updated.Teleporters = null;
            }
            if (Updated.Tiles.Count == 0)
            {
                Updated.Tiles = null;
            }
            if (Updated.Tilesheets.Count == 0)
            {
                Updated.Tilesheets = null;
            }
            if (Updated.Warps.Count == 0)
            {
                Updated.Warps = null;
            }
            AdvancedLocationLoaderMod.Logger.Log("Saving converted manifest to file...", LogLevel.Trace);
            // Save and then parse the updated config
            File.WriteAllText(filepath, JsonConvert.SerializeObject(Updated, new JsonSerializerSettings()
            {
                Formatting = Formatting.Indented, NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore
            }));
            AdvancedLocationLoaderMod.Logger.Log("Loading the converted manifest", LogLevel.Trace);
            Loader1_2.Load(filepath);
        }