1 /* Copyright (C) 2011 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "CommonConvert.h"
21 #include "PMDConvert.h"
22 #include "PSAConvert.h"
23 #include "StdSkeletons.h"
28 void default_logger(void*, int severity
, const char* message
)
30 fprintf(stderr
, "[%d] %s\n", severity
, message
);
33 static LogFn g_Logger
= &default_logger
;
34 static void* g_LoggerCBData
= NULL
;
36 EXPORT
void set_logger(LogFn logger
, void* cb_data
)
41 g_LoggerCBData
= cb_data
;
45 g_Logger
= &default_logger
;
46 g_LoggerCBData
= NULL
;
50 void Log(int severity
, const char* msg
, ...)
55 vsnprintf(buffer
, sizeof(buffer
), msg
, ap
);
56 buffer
[sizeof(buffer
)-1] = '\0';
59 g_Logger(g_LoggerCBData
, severity
, buffer
);
62 struct BufferedOutputCallback
: public OutputCB
64 static const unsigned int bufferSize
= 4096;
65 char buffer
[bufferSize
];
66 unsigned int bufferUsed
;
71 BufferedOutputCallback(OutputFn fn
, void* cb_data
)
72 : fn(fn
), cb_data(cb_data
), bufferUsed(0)
76 ~BufferedOutputCallback()
78 // flush the buffer if it's not empty
80 fn(cb_data
, buffer
, bufferUsed
);
83 virtual void operator() (const char* data
, unsigned int length
)
85 if (bufferUsed
+length
> bufferSize
)
87 // will overflow buffer, so flush the buffer first
88 fn(cb_data
, buffer
, bufferUsed
);
91 if (length
> bufferSize
)
93 // new data won't fit in buffer, so send it out unbuffered
94 fn(cb_data
, data
, length
);
100 memcpy(buffer
+bufferUsed
, data
, length
);
101 bufferUsed
+= length
;
102 assert(bufferUsed
<= bufferSize
);
106 int convert_dae_to_whatever(const char* dae
, OutputFn writer
, void* cb_data
, void(*conv
)(const char*, OutputCB
&, std::string
&))
108 Log(LOG_INFO
, "Starting conversion");
110 FCollada::Initialize();
112 std::string xmlErrors
;
113 BufferedOutputCallback
cb(writer
, cb_data
);
116 conv(dae
, cb
, xmlErrors
);
118 catch (const ColladaException
& e
)
120 if (! xmlErrors
.empty())
121 Log(LOG_ERROR
, "%s", xmlErrors
.c_str());
123 Log(LOG_ERROR
, "%s", e
.what());
132 if (! xmlErrors
.empty())
134 Log(LOG_ERROR
, "%s", xmlErrors
.c_str());
142 EXPORT
int convert_dae_to_pmd(const char* dae
, OutputFn pmd_writer
, void* cb_data
)
144 return convert_dae_to_whatever(dae
, pmd_writer
, cb_data
, ColladaToPMD
);
147 EXPORT
int convert_dae_to_psa(const char* dae
, OutputFn psa_writer
, void* cb_data
)
149 return convert_dae_to_whatever(dae
, psa_writer
, cb_data
, ColladaToPSA
);
152 EXPORT
int set_skeleton_definitions(const char* xml
, int length
)
154 std::string xmlErrors
;
157 Skeleton::LoadSkeletonDataFromXml(xml
, length
, xmlErrors
);
159 catch (const ColladaException
& e
)
161 if (! xmlErrors
.empty())
162 Log(LOG_ERROR
, "%s", xmlErrors
.c_str());
164 Log(LOG_ERROR
, "%s", e
.what());