Game Development Community

Deleting files

by Dylan Sale · in Torque Game Engine · 02/17/2004 (7:38 am) · 4 replies

Hey guys.

Just wondering if anyone knew if torque was able to delete files within its game directories or whether I have to add support for it. Ive had a look over the file scripting functions but I cant see anything to do with deleting files there, so I assume it doesnt have support at the moment. Correct me if I am wrong however ;)


Dylan

#1
02/17/2004 (8:02 am)
Here's a console function I wrote a long long time ago. I forget if I included any headers but here you go:

ConsoleFunction(deleteFile, bool, 2, 2, "deleteFile(fileName)")
{
   argc;
   // in a writeable directory?
   char file[1024];
   Con::expandScriptFilename(file, sizeof(file), argv[1]);
   if(!ResourceManager->isValidWriteFileName(file))
   {
      Con::errorf("Unable to delete file '%s'...", argv[1]);
      return(false);
   }
   
   // free the resource from the manager and delete the file
   ResourceObject* obj = ResourceManager->find(file);
   if(!obj)
   {
      Con::errorf("Unable to delete file '%s'...", argv[1]);
      return(false);
   }
   else
   {
      // free the resource
      ResourceManager->freeResource(obj);
      // delete it from the sytem
      if(!dFileDelete(file))
      {
         Con::errorf("Unable to delete the file '%s'...", argv[1]);
         return(false);
      }
   }
   
   // return success
   Con::warnf("Deleting file '%s'...", argv[1]);
   return(true);
}

As you can see the heart of it all is the dFileDelete call which can be traced back into the Platform:: functionality where it is actually implemented by the various platforms. I'm also checking for the file in the resource manager because I dont want to delete anything that isn't loaded by the game for security reasons.

Hope that helps.
#2
02/17/2004 (8:05 am)
Yes, yes it does help. I was just in the process of researching how to stop people deleting files outside of the game (having found the dFileDelete function). Thank you, you have also shown me something new about the Resource Manager :D

Oh and it works straight out of the box ;)
#3
02/17/2004 (8:09 am)
No problem.

If you find any problems with it please let me know so I can fix my version too :)
#4
09/21/2007 (5:49 am)
Wrapped this up as a resource here. Thread closed now... :-)