Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

issue about Memory VFS already contains file xxx when re-enable a plugin

<< < (2/2)

ollydbg:

--- Quote from: MortenMacFly on February 17, 2016, 02:05:51 pm ---
--- Quote from: ollydbg on February 17, 2016, 02:18:32 am ---But this function is protected, so we can't access it, this gives the dilemma:

--- End quote ---
Except if we make our own class cbMemoryFSHandlerBase and derive from it... if that's feasible in this context.

--- End quote ---
Why do we need a cbMemoryFSHandlerBase?

I think we can directly derive from the wxMemoryFSHandler, and make a static function, like:


--- Code: ---class WXDLLIMPEXP_BASE cbMemoryFSHandler : public wxMemoryFSHandler
{
    static void AddFile(const wxString& filename,
                        const void *binarydata,
                        size_t size)
    {
        // add our code here
        if ( !wxMemoryFSHandlerBase::CheckDoesntExist(filename) )
            wxMemoryFSHandlerBase::RemoveFile(filename);

        // do what the base class do
        wxMemoryFSHandler::AddFile(filename, binarydata, size);
    }
}

--- End code ---

Is the above code prototype correct?

ollydbg:
Hi, Morten, here is the patch to solve the error message from wx


--- Code: ---commit edb7615a930263862941e94c4326e834ab1fcb1d
Author: asmwarrior <asmwarrior@gmail.com>
Date:   Wed Feb 17 23:12:52 2016 +0800

    test patch to avoid the error message from wx that when a resoure file
    is already in the memory file system, and a new one try to load again.
   
    See discussion here:
    http://forums.codeblocks.org/index.php/topic,20952.0.html

diff --git a/src/sdk/manager.cpp b/src/sdk/manager.cpp
index 1122b0e..68663a5 100644
--- a/src/sdk/manager.cpp
+++ b/src/sdk/manager.cpp
@@ -48,6 +48,35 @@
 #include "ccmanager.h"
 #include "debuggermanager.h"
 
+
+class DLLIMPORT cbMemoryFSHandler : public wxMemoryFSHandler
+{
+public:
+    static void AddFile(const wxString& filename,
+                        const void *binarydata,
+                        size_t size);
+    static void RemoveFile(const wxString& filename);
+};
+/* static */
+void cbMemoryFSHandler::AddFile(const wxString& filename,
+                    const void *binarydata,
+                    size_t size)
+{
+    // if the old file exists, remove the old one, and add it later
+    if ( m_Hash.count(filename) )
+        wxMemoryFSHandler::RemoveFile(filename);
+
+    // do what the base class do
+    wxMemoryFSHandler::AddFile(filename, binarydata, size);
+}
+
+/* static */
+void cbMemoryFSHandler::RemoveFile(const wxString& filename)
+{
+    // if the old file exists, remove it
+    if ( m_Hash.count(filename) )
+        wxMemoryFSHandler::RemoveFile(filename);
+}
 static Manager* s_ManagerInstance = nullptr;
 
 
@@ -511,15 +540,12 @@ bool Manager::LoadResource(const wxString& file)
     }
 
     // The code below forces a reload of the resource
-    // Currently unused...
 
-//    {
-//        wxMemoryFSHandler::RemoveFile(file);
-//    }
-//#if wxABI_VERSION > 20601
-//    // unload old resources with the same name
-//    wxXmlResource::Get()->Unload(memoryFile);
-//#endif
+    // this will remove the file if it is already loaded
+    cbMemoryFSHandler::RemoveFile(file);
+
+    // unload old resources with the same name
+    wxXmlResource::Get()->Unload(memoryFile);
 
     wxFile f(resourceFile, wxFile::read);
     char *buf = nullptr;
@@ -530,7 +556,7 @@ bool Manager::LoadResource(const wxString& file)
         buf = new char[len];
         f.Read(buf, len);
         {
-            wxMemoryFSHandler::AddFile(file, buf, len);
+            cbMemoryFSHandler::AddFile(file, buf, len);
         }
         if ( !wxXmlResource::Get()->Load(memoryFile) )
             Get()->GetLogManager()->LogError(_("Manager failed to load XRC resource '") + resourceFile + _("'."));
diff --git a/src/src/app.cpp b/src/src/app.cpp
index 837d974..169e360 100644
--- a/src/src/app.cpp
+++ b/src/src/app.cpp
@@ -76,6 +76,15 @@
 inline wxString GetResourcesDir(){ return wxEmptyString; }
 #endif
 
+class DLLIMPORT cbMemoryFSHandler : public wxMemoryFSHandler
+{
+public:
+    static void AddFile(const wxString& filename,
+                        const void *binarydata,
+                        size_t size);
+    static void RemoveFile(const wxString& filename);
+};
+
 namespace
 {
 bool s_Loading = false;
@@ -588,7 +597,7 @@ bool CodeBlocksApp::OnInit()
 
     // we'll do this once and for all at startup
     wxFileSystem::AddHandler(new wxZipFSHandler);
-    wxFileSystem::AddHandler(new wxMemoryFSHandler);
+    wxFileSystem::AddHandler(new cbMemoryFSHandler);
     wxXmlResource::Get()->InsertHandler(new wxToolBarAddOnXmlHandler);
     wxXmlResource::Get()->InsertHandler(new wxScrollingDialogXmlHandler);
     wxInitAllImageHandlers();


--- End code ---

It works fine.

But I believe there are some better solutions.

MortenMacFly:

--- Quote from: ollydbg on February 17, 2016, 02:46:57 pm ---Is the above code prototype correct?

--- End quote ---
Yes, should be fine. I just copied&pasted from your code snippet.

ollydbg:

--- Quote from: MortenMacFly on February 17, 2016, 04:05:30 pm ---
--- Quote from: ollydbg on February 17, 2016, 02:46:57 pm ---Is the above code prototype correct?

--- End quote ---
Yes, should be fine. I just copied&pasted from your code snippet.

--- End quote ---
I have just make a patch file to implement this feature, you can have a loot at my previous post in this thread. :)

Navigation

[0] Message Index

[*] Previous page

Go to full version