Example #1
0
        public static void ApplyTilesheet(IContentHelper coreContentHelper, IContentPack contentPack, Tilesheet tilesheet, Map map)
        {
            int stage  = 0;
            int branch = 0;
            int skip   = 0;

            try
            {
                stage++; // 1
                if (tilesheet.FileName == null)
                {
                    skip = 1;
                }
                else
                {
                    skip = 2;
                    string fakepath = Path.Combine("AdvancedLocationLoader/FakePath_paths_objects", tilesheet.FileName);
                    if (tilesheet.Seasonal)
                    {
                        fakepath = fakepath.Replace("all_sheet_paths_objects", Path.Combine("all_sheet_paths_objects", Game1.currentSeason));
                    }
                    stage++; // 2
                    if (!Processors.MappingCache.ContainsKey(tilesheet.FileName))
                    {
                        string toAssetPath = contentPack.GetRelativePath(
                            fromAbsolutePath: ModEntry.SHelper.DirectoryPath,
                            toLocalPath: tilesheet.Seasonal ? ($"{tilesheet.FileName}_{Game1.currentSeason}") : tilesheet.FileName
                            );
                        coreContentHelper.RegisterXnbReplacement(fakepath, toAssetPath);
                        Processors.MappingCache[tilesheet.FileName] = fakepath;
                    }

                    stage++; // 3
                    if (map.GetTileSheet(tilesheet.SheetId) != null)
                    {
                        branch = 1;
                        map.GetTileSheet(tilesheet.SheetId).ImageSource = fakepath;
                    }
                    else
                    {
                        branch = 2;
                        Texture2D sheet = Game1.content.Load <Texture2D>(fakepath);
                        map.AddTileSheet(new TileSheet(tilesheet.SheetId, map, fakepath, new Size((int)Math.Ceiling(sheet.Width / 16.0), (int)Math.Ceiling(sheet.Height / 16.0)), new Size(16, 16)));
                    }
                }

                stage++; // 4 (skip 2)
                if (tilesheet.Properties.Count > 0)
                {
                    TileSheet sheet = map.GetTileSheet(tilesheet.SheetId);
                    foreach (string prop in tilesheet.Properties.Keys)
                    {
                        sheet.Properties[prop] = tilesheet.Properties[prop];
                    }
                }
            }
            catch (Exception err)
            {
                ModEntry.Logger.ExitGameImmediately($"Unable to patch tilesheet, a unexpected error occured at stage {stage}{(skip > 0 ? ("-" + skip) : "")}{(branch > 0 ? ("-" + branch) : "")}: {tilesheet}", err);
            }
        }