refacto: rendering engine singleton removal step 1 (filesystem)

Make the RenderingEngine filesystem member non accessible from everywhere

This permits also to determine that some lua code has directly a logic to extract zip file. Move this logic inside client, it's not the lua stack role to perform a such complex operation

Found also another irrlicht <1.8 compat code to remove
This commit is contained in:
Loic Blot 2021-04-28 10:22:13 +02:00 committed by Loïc Blot
parent bc1888ff21
commit e34d28af9f
5 changed files with 84 additions and 86 deletions

@ -97,6 +97,7 @@ Client::Client(
NodeDefManager *nodedef, NodeDefManager *nodedef,
ISoundManager *sound, ISoundManager *sound,
MtEventManager *event, MtEventManager *event,
RenderingEngine *rendering_engine,
bool ipv6, bool ipv6,
GameUI *game_ui GameUI *game_ui
): ):
@ -106,6 +107,7 @@ Client::Client(
m_nodedef(nodedef), m_nodedef(nodedef),
m_sound(sound), m_sound(sound),
m_event(event), m_event(event),
m_rendering_engine(rendering_engine),
m_mesh_update_thread(this), m_mesh_update_thread(this),
m_env( m_env(
new ClientMap(this, control, 666), new ClientMap(this, control, 666),
@ -660,8 +662,8 @@ bool Client::loadMedia(const std::string &data, const std::string &filename,
TRACESTREAM(<< "Client: Attempting to load image " TRACESTREAM(<< "Client: Attempting to load image "
<< "file \"" << filename << "\"" << std::endl); << "file \"" << filename << "\"" << std::endl);
io::IFileSystem *irrfs = RenderingEngine::get_filesystem(); io::IFileSystem *irrfs = m_rendering_engine->get_filesystem();
video::IVideoDriver *vdrv = RenderingEngine::get_video_driver(); video::IVideoDriver *vdrv = m_rendering_engine->get_video_driver();
io::IReadFile *rfile = irrfs->createMemoryReadFile( io::IReadFile *rfile = irrfs->createMemoryReadFile(
data.c_str(), data.size(), "_tempreadfile"); data.c_str(), data.size(), "_tempreadfile");
@ -728,6 +730,72 @@ bool Client::loadMedia(const std::string &data, const std::string &filename,
return false; return false;
} }
bool Client::extractZipFile(const char *filename, const std::string &destination)
{
auto fs = m_rendering_engine->get_filesystem();
if (!fs->addFileArchive(filename, false, false, io::EFAT_ZIP)) {
return false;
}
sanity_check(fs->getFileArchiveCount() > 0);
/**********************************************************************/
/* WARNING this is not threadsafe!! */
/**********************************************************************/
io::IFileArchive* opened_zip = fs->getFileArchive(fs->getFileArchiveCount() - 1);
const io::IFileList* files_in_zip = opened_zip->getFileList();
unsigned int number_of_files = files_in_zip->getFileCount();
for (unsigned int i=0; i < number_of_files; i++) {
std::string fullpath = destination;
fullpath += DIR_DELIM;
fullpath += files_in_zip->getFullFileName(i).c_str();
std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
if (!files_in_zip->isDirectory(i)) {
if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
fs->removeFileArchive(fs->getFileArchiveCount()-1);
return false;
}
io::IReadFile* toread = opened_zip->createAndOpenFile(i);
FILE *targetfile = fopen(fullpath.c_str(),"wb");
if (targetfile == NULL) {
fs->removeFileArchive(fs->getFileArchiveCount()-1);
return false;
}
char read_buffer[1024];
long total_read = 0;
while (total_read < toread->getSize()) {
unsigned int bytes_read =
toread->read(read_buffer,sizeof(read_buffer));
if ((bytes_read == 0 ) ||
(fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
{
fclose(targetfile);
fs->removeFileArchive(fs->getFileArchiveCount() - 1);
return false;
}
total_read += bytes_read;
}
fclose(targetfile);
}
}
fs->removeFileArchive(fs->getFileArchiveCount() - 1);
return true;
}
// Virtual methods from con::PeerHandler // Virtual methods from con::PeerHandler
void Client::peerAdded(con::Peer *peer) void Client::peerAdded(con::Peer *peer)
{ {
@ -1910,23 +1978,17 @@ scene::IAnimatedMesh* Client::getMesh(const std::string &filename, bool cache)
// Create the mesh, remove it from cache and return it // Create the mesh, remove it from cache and return it
// This allows unique vertex colors and other properties for each instance // This allows unique vertex colors and other properties for each instance
#if IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR > 8 io::IReadFile *rfile = m_rendering_engine->get_filesystem()->createMemoryReadFile(
io::IReadFile *rfile = RenderingEngine::get_filesystem()->createMemoryReadFile(
data.c_str(), data.size(), filename.c_str()); data.c_str(), data.size(), filename.c_str());
#else
Buffer<char> data_rw(data.c_str(), data.size()); // Const-incorrect Irrlicht
io::IReadFile *rfile = RenderingEngine::get_filesystem()->createMemoryReadFile(
*data_rw, data_rw.getSize(), filename.c_str());
#endif
FATAL_ERROR_IF(!rfile, "Could not create/open RAM file"); FATAL_ERROR_IF(!rfile, "Could not create/open RAM file");
scene::IAnimatedMesh *mesh = RenderingEngine::get_scene_manager()->getMesh(rfile); scene::IAnimatedMesh *mesh = m_rendering_engine->get_scene_manager()->getMesh(rfile);
rfile->drop(); rfile->drop();
if (!mesh) if (!mesh)
return nullptr; return nullptr;
mesh->grab(); mesh->grab();
if (!cache) if (!cache)
RenderingEngine::get_mesh_cache()->removeMesh(mesh); m_rendering_engine->get_mesh_cache()->removeMesh(mesh);
return mesh; return mesh;
} }

@ -45,6 +45,7 @@ struct ClientEvent;
struct MeshMakeData; struct MeshMakeData;
struct ChatMessage; struct ChatMessage;
class MapBlockMesh; class MapBlockMesh;
class RenderingEngine;
class IWritableTextureSource; class IWritableTextureSource;
class IWritableShaderSource; class IWritableShaderSource;
class IWritableItemDefManager; class IWritableItemDefManager;
@ -123,6 +124,7 @@ public:
NodeDefManager *nodedef, NodeDefManager *nodedef,
ISoundManager *sound, ISoundManager *sound,
MtEventManager *event, MtEventManager *event,
RenderingEngine *rendering_engine,
bool ipv6, bool ipv6,
GameUI *game_ui GameUI *game_ui
); );
@ -379,6 +381,9 @@ public:
// Insert a media file appropriately into the appropriate manager // Insert a media file appropriately into the appropriate manager
bool loadMedia(const std::string &data, const std::string &filename, bool loadMedia(const std::string &data, const std::string &filename,
bool from_media_push = false); bool from_media_push = false);
bool extractZipFile(const char *filename, const std::string &destination);
// Send a request for conventional media transfer // Send a request for conventional media transfer
void request_media(const std::vector<std::string> &file_requests); void request_media(const std::vector<std::string> &file_requests);
@ -469,6 +474,7 @@ private:
NodeDefManager *m_nodedef; NodeDefManager *m_nodedef;
ISoundManager *m_sound; ISoundManager *m_sound;
MtEventManager *m_event; MtEventManager *m_event;
RenderingEngine *m_rendering_engine;
MeshUpdateThread m_mesh_update_thread; MeshUpdateThread m_mesh_update_thread;

@ -1465,7 +1465,7 @@ bool Game::connectToServer(const GameStartData &start_data,
start_data.password, start_data.address, start_data.password, start_data.address,
*draw_control, texture_src, shader_src, *draw_control, texture_src, shader_src,
itemdef_manager, nodedef_manager, sound, eventmgr, itemdef_manager, nodedef_manager, sound, eventmgr,
connect_address.isIPv6(), m_game_ui.get()); RenderingEngine::get_instance(), connect_address.isIPv6(), m_game_ui.get());
client->m_simple_singleplayer_mode = simple_singleplayer_mode; client->m_simple_singleplayer_mode = simple_singleplayer_mode;

@ -59,10 +59,9 @@ public:
static RenderingEngine *get_instance() { return s_singleton; } static RenderingEngine *get_instance() { return s_singleton; }
static io::IFileSystem *get_filesystem() io::IFileSystem *get_filesystem()
{ {
sanity_check(s_singleton && s_singleton->m_device); return m_device->getFileSystem();
return s_singleton->m_device->getFileSystem();
} }
static video::IVideoDriver *get_video_driver() static video::IVideoDriver *get_video_driver()

@ -34,9 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serverlist.h" #include "serverlist.h"
#include "mapgen/mapgen.h" #include "mapgen/mapgen.h"
#include "settings.h" #include "settings.h"
#include "client/client.h"
#include <IFileArchive.h>
#include <IFileSystem.h>
#include "client/renderingengine.h" #include "client/renderingengine.h"
#include "network/networkprotocol.h" #include "network/networkprotocol.h"
@ -630,74 +628,7 @@ int ModApiMainMenu::l_extract_zip(lua_State *L)
if (ModApiMainMenu::mayModifyPath(absolute_destination)) { if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
fs::CreateAllDirs(absolute_destination); fs::CreateAllDirs(absolute_destination);
lua_pushboolean(L, getClient(L)->extractZipFile(zipfile, destination));
io::IFileSystem *fs = RenderingEngine::get_filesystem();
if (!fs->addFileArchive(zipfile, false, false, io::EFAT_ZIP)) {
lua_pushboolean(L,false);
return 1;
}
sanity_check(fs->getFileArchiveCount() > 0);
/**********************************************************************/
/* WARNING this is not threadsafe!! */
/**********************************************************************/
io::IFileArchive* opened_zip =
fs->getFileArchive(fs->getFileArchiveCount()-1);
const io::IFileList* files_in_zip = opened_zip->getFileList();
unsigned int number_of_files = files_in_zip->getFileCount();
for (unsigned int i=0; i < number_of_files; i++) {
std::string fullpath = destination;
fullpath += DIR_DELIM;
fullpath += files_in_zip->getFullFileName(i).c_str();
std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
if (!files_in_zip->isDirectory(i)) {
if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
fs->removeFileArchive(fs->getFileArchiveCount()-1);
lua_pushboolean(L,false);
return 1;
}
io::IReadFile* toread = opened_zip->createAndOpenFile(i);
FILE *targetfile = fopen(fullpath.c_str(),"wb");
if (targetfile == NULL) {
fs->removeFileArchive(fs->getFileArchiveCount()-1);
lua_pushboolean(L,false);
return 1;
}
char read_buffer[1024];
long total_read = 0;
while (total_read < toread->getSize()) {
unsigned int bytes_read =
toread->read(read_buffer,sizeof(read_buffer));
if ((bytes_read == 0 ) ||
(fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
{
fclose(targetfile);
fs->removeFileArchive(fs->getFileArchiveCount()-1);
lua_pushboolean(L,false);
return 1;
}
total_read += bytes_read;
}
fclose(targetfile);
}
}
fs->removeFileArchive(fs->getFileArchiveCount()-1);
lua_pushboolean(L,true);
return 1; return 1;
} }