Skip to content

Commit

Permalink
Reload resources when toggling a resource pack add-on
Browse files Browse the repository at this point in the history
Previously, resource pack add-ons required a game restart to have their custom assets loaded.

Now, the resource reloading support added in 2379967, is used to our advantage to allow reloading all resources directly when toggling a resource pack.
  • Loading branch information
Vankata453 committed Dec 2, 2024
1 parent bb70ca3 commit b776feb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/addon/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ Addon::overrides_data() const
bool
Addon::requires_restart() const
{
// Determines if the add-on requires a restart.
return m_type == LANGUAGEPACK || m_type == RESOURCEPACK;
// Determines if the add-on requires a restart to function after enabled.
return m_type == LANGUAGEPACK;
}

/* EOF */
27 changes: 9 additions & 18 deletions src/addon/addon_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "supertux/globals.hpp"
#include "supertux/menu/addon_menu.hpp"
#include "supertux/menu/menu_storage.hpp"
#include "supertux/resources.hpp"
#include "util/file_system.hpp"
#include "util/gettext.hpp"
#include "util/log.hpp"
Expand Down Expand Up @@ -563,13 +564,6 @@ AddonManager::enable_addon(const AddonId& addon_id)
break;
}

// Only mount resource packs on startup (AddonManager initialization).
if (addon.get_type() == Addon::RESOURCEPACK && m_initialized)
{
addon.set_enabled(true);
return;
}

log_debug << "Adding archive \"" << addon.get_install_filename() << "\" to search path" << std::endl;
if (PHYSFS_mount(addon.get_install_filename().c_str(), mountpoint.c_str(), !addon.overrides_data()) == 0)
{
Expand All @@ -581,9 +575,11 @@ AddonManager::enable_addon(const AddonId& addon_id)
else
{
if (addon.get_type() == Addon::LANGUAGEPACK)
{
PHYSFS_enumerate(addon.get_id().c_str(), add_to_dictionary_path, nullptr);
}

if (m_initialized && addon.overrides_data())
Resources::reload_all();

addon.set_enabled(true);
}
}
Expand All @@ -600,13 +596,6 @@ AddonManager::disable_addon(const AddonId& addon_id)
}
else
{
// Don't unmount resource packs. Disabled resource packs will not be mounted on next startup.
if (addon.get_type() == Addon::RESOURCEPACK)
{
addon.set_enabled(false);
return;
}

log_debug << "Removing archive \"" << addon.get_install_filename() << "\" from search path" << std::endl;
if (PHYSFS_unmount(addon.get_install_filename().c_str()) == 0)
{
Expand All @@ -618,9 +607,11 @@ AddonManager::disable_addon(const AddonId& addon_id)
else
{
if (addon.get_type() == Addon::LANGUAGEPACK)
{
PHYSFS_enumerate(addon.get_id().c_str(), remove_from_dictionary_path, nullptr);
}

if (m_initialized && addon.overrides_data())
Resources::reload_all();

addon.set_enabled(false);
}
}
Expand Down
12 changes: 2 additions & 10 deletions src/supertux/menu/debug_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
#include "editor/editor.hpp"
#include "gui/item_action.hpp"
#include "gui/item_stringselect.hpp"
#include "sprite/sprite_manager.hpp"
#include "supertux/debug.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "supertux/resources.hpp"
#include "supertux/tile_manager.hpp"
#include "util/gettext.hpp"
#include "util/log.hpp"
#include "video/texture_manager.hpp"
Expand Down Expand Up @@ -77,14 +75,8 @@ DebugMenu::DebugMenu() :
[](bool value){ g_debug.set_use_bitmap_fonts(value); });
add_toggle(-1, _("Show Tile IDs in Editor Toolbox"), &g_debug.show_toolbox_tile_ids);

add_entry(_("Reload Resources"), []{
TextureManager::current()->reload();

Resources::load(true);
SpriteManager::current()->reload();
TileManager::current()->reload();
})
.set_help(_("Reloads all fonts, textures, sprites and tilesets."));
add_entry(_("Reload Resources"), &Resources::reload_all)
.set_help(_("Reloads all fonts, textures, sprites and tilesets."));

add_entry(_("Dump Texture Cache"), []{ TextureManager::current()->debug_print(get_logging_instance()); });

Expand Down
12 changes: 12 additions & 0 deletions src/supertux/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@
#include "supertux/debug.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "supertux/tile_manager.hpp"
#include "video/bitmap_font.hpp"
#include "video/font.hpp"
#include "video/surface.hpp"
#include "video/texture_manager.hpp"
#include "video/ttf_font.hpp"
#include "video/ttf_surface_manager.hpp"

void
Resources::reload_all()
{
TextureManager::current()->reload();

Resources::load(true);
SpriteManager::current()->reload();
TileManager::current()->reload();
}

std::unique_ptr<MouseCursor> Resources::mouse_cursor;

FontPtr Resources::default_font;
Expand Down
3 changes: 3 additions & 0 deletions src/supertux/resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class MouseCursor;

class Resources final
{
public:
static void reload_all();

public:
static std::unique_ptr<MouseCursor> mouse_cursor;

Expand Down
11 changes: 10 additions & 1 deletion src/video/texture_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,16 @@ TextureManager::reload()
// Reload surfaces
for (auto& surface : m_surfaces)
{
SDLSurfacePtr surface_new = create_image_surface(surface.first);
SDLSurfacePtr surface_new;
try
{
surface_new = create_image_surface(surface.first);
}
catch (const std::exception& err)
{
log_warning << "Couldn't load texture '" << surface.first << "' (now using dummy texture): " << err.what() << std::endl;
surface_new = create_dummy_surface();
}
surface.second.reset(surface_new);
}

Expand Down

0 comments on commit b776feb

Please sign in to comment.