Skip to content

Commit

Permalink
Move ArchiveFile adaptor code to PicoModelModule since this is the la…
Browse files Browse the repository at this point in the history
…st remaining place where this is needed.

Remove the deprecated interface from ifilesystem.h.
  • Loading branch information
codereader committed Sep 11, 2017
1 parent babb967 commit 15b301f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 81 deletions.
9 changes: 0 additions & 9 deletions include/ifilesystem.h
Expand Up @@ -101,15 +101,6 @@ class VirtualFileSystem :
/// This is a variant of openTextFile taking an absolute path as argument.
virtual ArchiveTextFilePtr openTextFileInAbsolutePath(const std::string& filename) = 0;

/// \brief Opens the file identified by \p filename and reads it into \p buffer, or sets *\p buffer to 0 if not found.
/// Returns the size of the buffer allocated, or undefined value if *\p buffer is 0;
/// The caller must free the allocated buffer by calling \c freeFile
/// \deprecated Deprecated - use \c openFile.
virtual std::size_t loadFile(const std::string& filename, void **buffer) = 0;
/// \brief Frees the buffer returned by \c loadFile.
/// \deprecated Deprecated.
virtual void freeFile(void *p) = 0;

/// \brief Calls the visitor function for each file under \p basedir matching \p extension.
/// Use "*" as \p extension to match all file extensions.
virtual void forEachFile(const std::string& basedir,
Expand Down
104 changes: 63 additions & 41 deletions plugins/model/PicoModelModule.h
@@ -1,10 +1,13 @@
#pragma once

#include "idatastream.h"
#include "iarchive.h"
#include "imodule.h"
#include "imodel.h"
#include "itextstream.h"
#include "ifilesystem.h"

#include "os/path.h"
#include <stdio.h>
#include "picomodel.h"

Expand All @@ -14,47 +17,6 @@
#include "Lwo2Exporter.h"
#include "WavefrontExporter.h"

typedef unsigned char byte;

void PicoPrintFunc(int level, const char *str)
{
if (str == 0)
return;

switch (level)
{
case PICO_NORMAL:
rMessage() << str << std::endl;
break;

case PICO_VERBOSE:
//rMessage() << "PICO_VERBOSE: " << str << std::endl;
break;

case PICO_WARNING:
rError() << "PICO_WARNING: " << str << std::endl;
break;

case PICO_ERROR:
rError() << "PICO_ERROR: " << str << std::endl;
break;

case PICO_FATAL:
rError() << "PICO_FATAL: " << str << std::endl;
break;
}
}

void PicoLoadFileFunc(char *name, byte **buffer, int *bufSize)
{
*bufSize = static_cast<int>(GlobalFileSystem().loadFile(name, (void**)buffer));
}

void PicoFreeFileFunc(void* file)
{
GlobalFileSystem().freeFile(file);
}

namespace model
{

Expand Down Expand Up @@ -116,6 +78,66 @@ class PicoModelModule :
GlobalModelFormatManager().registerExporter(std::make_shared<Lwo2Exporter>());
GlobalModelFormatManager().registerExporter(std::make_shared<WavefrontExporter>());
}

private:

static void PicoPrintFunc(int level, const char *str)
{
if (str == nullptr) return;

switch (level)
{
case PICO_NORMAL:
rMessage() << str << std::endl;
break;

case PICO_VERBOSE:
//rMessage() << "PICO_VERBOSE: " << str << std::endl;
break;

case PICO_WARNING:
rError() << "PICO_WARNING: " << str << std::endl;
break;

case PICO_ERROR:
rError() << "PICO_ERROR: " << str << std::endl;
break;

case PICO_FATAL:
rError() << "PICO_FATAL: " << str << std::endl;
break;
}
}

static void PicoLoadFileFunc(char *name, unsigned char** buffer, int *bufSize)
{
std::string fixedFilename(os::standardPathWithSlash(name));

ArchiveFilePtr file = GlobalFileSystem().openFile(fixedFilename);

if (!file)
{
*buffer = nullptr;
*bufSize = 0;
return;
}

// Allocate one byte more for the trailing zero
*buffer = reinterpret_cast<unsigned char*>(malloc(file->size() + 1));

// we need to end the buffer with a 0
(*buffer)[file->size()] = '\0';

*bufSize = static_cast<int>(file->getInputStream().read(
reinterpret_cast<InputStream::byte_type*>(*buffer),
file->size()
));
}

static void PicoFreeFileFunc(void* file)
{
free(file);
}
};

}
28 changes: 0 additions & 28 deletions plugins/vfspk3/Doom3FileSystem.cpp
Expand Up @@ -220,34 +220,6 @@ ArchiveTextFilePtr Doom3FileSystem::openTextFileInAbsolutePath(const std::string
return ArchiveTextFilePtr();
}

std::size_t Doom3FileSystem::loadFile(const std::string& filename, void **buffer) {
std::string fixedFilename(os::standardPathWithSlash(filename));

ArchiveFilePtr file = openFile(fixedFilename);

if (file != NULL) {
// Allocate one byte more for the trailing zero
*buffer = malloc(file->size()+1);

// we need to end the buffer with a 0
((char*) (*buffer))[file->size()] = 0;

std::size_t length = file->getInputStream().read(
reinterpret_cast<InputStream::byte_type*>(*buffer),
file->size()
);

return length;
}

*buffer = NULL;
return 0;
}

void Doom3FileSystem::freeFile(void *p) {
free(p);
}

void Doom3FileSystem::forEachFile(const std::string& basedir,
const std::string& extension,
const VisitorFunc& visitorFunc,
Expand Down
3 changes: 0 additions & 3 deletions plugins/vfspk3/Doom3FileSystem.h
Expand Up @@ -41,9 +41,6 @@ class Doom3FileSystem :
ArchiveFilePtr openFileInAbsolutePath(const std::string& filename) override;
ArchiveTextFilePtr openTextFileInAbsolutePath(const std::string& filename) override;

std::size_t loadFile(const std::string& filename, void **buffer) override;
void freeFile(void *p) override;

// Call the specified callback function for each file matching extension
// inside basedir.
void forEachFile(const std::string& basedir, const std::string& extension,
Expand Down

0 comments on commit 15b301f

Please sign in to comment.