/
PicoModelModule.h
143 lines (114 loc) · 3.02 KB
/
PicoModelModule.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#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"
#include <boost/algorithm/string/case_conv.hpp>
#include "PicoModelLoader.h"
#include "AseExporter.h"
#include "Lwo2Exporter.h"
#include "WavefrontExporter.h"
namespace model
{
class PicoModelModule :
public RegisterableModule
{
public:
// RegisterableModule implementation
const std::string& getName() const
{
static std::string _name("PicoModelModule");
return _name;
}
const StringSet& getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_MODELFORMATMANAGER);
}
return _dependencies;
}
void initialiseModule(const ApplicationContext& ctx)
{
PicoInit();
PicoSetMallocFunc(malloc);
PicoSetFreeFunc(free);
PicoSetPrintFunc(PicoPrintFunc);
PicoSetLoadFileFunc(PicoLoadFileFunc);
PicoSetFreeFileFunc(PicoFreeFileFunc);
// Register all importers available through picomodel
const picoModule_t** modules = PicoModuleList(0);
while (*modules != nullptr)
{
const picoModule_t* module = *modules++;
if (module->canload && module->load)
{
for (char* const* ext = module->defaultExts; *ext != 0; ++ext)
{
// greebo: File extension is expected to be UPPERCASE
std::string extension(*ext);
boost::algorithm::to_upper(extension);
GlobalModelFormatManager().registerImporter(
std::make_shared<PicoModelLoader>(module, extension)
);
}
}
}
GlobalModelFormatManager().registerExporter(std::make_shared<AseExporter>());
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);
}
};
}