Exemple #1
0
        /// <summary>Preload an asset from the content pack if necessary.</summary>
        /// <param name="contentPack">The content pack.</param>
        /// <param name="key">The asset key.</param>
        /// <returns>Returns whether any assets were preloaded.</returns>
        public bool PreloadIfNeeded(IContentPack contentPack, string key)
        {
            key = this.GetRealPath(contentPack, key) ?? throw new FileNotFoundException($"The file '{key}' does not exist in the {contentPack.Manifest.Name} content patch folder.");
            bool anyLoaded = false;

            // PNG asset
            if (this.IsPngPath(key))
            {
                string actualAssetKey = contentPack.GetActualAssetKey(key);
                if (!this.PngTextureCache.ContainsKey(actualAssetKey))
                {
                    this.PngTextureCache[actualAssetKey] = contentPack.LoadAsset <Texture2D>(key);
                    anyLoaded = true;
                }
            }

            // map PNG tilesheets
            if (this.TryLoadMap(contentPack, key, out Map map))
            {
                string relativeRoot = contentPack.GetActualAssetKey(""); // warning: this depends on undocumented SMAPI implementation details
                foreach (TileSheet tilesheet in map.TileSheets)
                {
                    // ignore if not a PNG in the content pack
                    if (!tilesheet.ImageSource.StartsWith(relativeRoot) || Path.GetExtension(tilesheet.ImageSource).Equals(".png", StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    // ignore if local file doesn't exist
                    string relativePath = this.GetRealPath(contentPack, tilesheet.ImageSource.Substring(relativeRoot.Length + 1));
                    if (relativePath == null)
                    {
                        continue;
                    }

                    // load asset
                    string actualAssetKey = contentPack.GetActualAssetKey(relativePath);
                    if (!this.PngTextureCache.ContainsKey(actualAssetKey))
                    {
                        this.PngTextureCache[actualAssetKey] = contentPack.LoadAsset <Texture2D>(relativePath);
                        anyLoaded = true;
                    }
                }
            }

            return(anyLoaded);
        }
 /// <summary>Preload an asset from the content pack if necessary.</summary>
 /// <typeparam name="T">The asset type.</typeparam>
 /// <param name="contentPack">The content pack.</param>
 /// <param name="key">The asset key.</param>
 public void PreloadIfNeeded <T>(IContentPack contentPack, string key)
 {
     if (this.IsPng <T>(key))
     {
         string actualAssetKey = contentPack.GetActualAssetKey(key);
         if (!this.PngTextureCache.ContainsKey(actualAssetKey))
         {
             this.PngTextureCache[actualAssetKey] = contentPack.LoadAsset <Texture2D>(key);
         }
     }
 }
        /// <summary>Get an asset from the content pack of <see cref="PngTextureCache"/>.</summary>
        /// <typeparam name="T">The asset type.</typeparam>
        /// <param name="contentPack">The content pack.</param>
        /// <param name="key">The asset key.</param>
        public T Load <T>(IContentPack contentPack, string key)
        {
            // load from PNG cache if applicable
            if (this.IsPng <T>(key))
            {
                string actualAssetKey = contentPack.GetActualAssetKey(key);
                if (this.PngTextureCache.TryGetValue(actualAssetKey, out Texture2D texture))
                {
                    return((T)(object)texture);
                }
            }

            // load from pack
            return(contentPack.LoadAsset <T>(key));
        }
        public static void ApplyLocation(IContentPack contentPack, Configs.Location location)
        {
            try
            {
                GameLocation loc;
                string       mapPath = contentPack.GetActualAssetKey(location.FileName);
                switch (location.Type)
                {
                case "Cellar":
                    loc = new Cellar(mapPath, location.MapName);
                    break;

                case "BathHousePool":
                    loc = new BathHousePool(mapPath, location.MapName);
                    break;

                case "Decoratable":
                    loc = new Locations.DecoratableLocation(mapPath, location.MapName);
                    break;

                case "Desert":
                    loc = new Locations.Desert(mapPath, location.MapName);
                    break;

                case "Greenhouse":
                    loc = new Greenhouse(mapPath, location.MapName);
                    break;

                case "Sewer":
                    loc = new Locations.Sewer(mapPath, location.MapName);
                    break;

                default:
                    loc = new GameLocation(mapPath, location.MapName);
                    break;
                }

                loc.IsOutdoors = location.Outdoor;
                loc.IsFarm     = location.Farmable;
                Game1.locations.Add(loc);
            }
            catch (Exception err)
            {
                ModEntry.Logger.ExitGameImmediately("Unable to add custom location, a unexpected error occured: " + location, err);
            }
        }
Exemple #5
0
        /// <summary>Get an asset from the content pack of <see cref="PngTextureCache"/>.</summary>
        /// <typeparam name="T">The asset type.</typeparam>
        /// <param name="contentPack">The content pack.</param>
        /// <param name="key">The asset key.</param>
        public T Load <T>(IContentPack contentPack, string key)
        {
            key = this.GetRealPath(contentPack, key) ?? throw new FileNotFoundException($"The file '{key}' does not exist in the {contentPack.Manifest.Name} content patch folder.");

            // load from PNG cache if applicable
            if (typeof(T) == typeof(Texture2D) && this.IsPngPath(key))
            {
                string actualAssetKey = contentPack.GetActualAssetKey(key);
                if (this.PngTextureCache.TryGetValue(actualAssetKey, out Texture2D texture))
                {
                    return((T)(object)texture);
                }
            }

            // load from pack
            return(contentPack.LoadAsset <T>(key));
        }
 public static void ApplyOverride(IContentPack contentPack, Override obj)
 {
     try
     {
         for (int i = 0; i < Game1.locations.Count; i++)
         {
             GameLocation location = Game1.locations[i];
             if (location.Name == obj.MapName)
             {
                 Game1.locations[i] = (GameLocation)Activator.CreateInstance(Game1.getLocationFromName(obj.MapName).GetType(), contentPack.GetActualAssetKey(obj.FileName), obj.MapName);
                 break;
             }
         }
     }
     catch (Exception err)
     {
         ModEntry.Logger.ExitGameImmediately("Unable to override location, a unexpected error occured: " + obj, err);
     }
 }