Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DLL.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "CommonConvert.h"
21 #include "PMDConvert.h"
22 #include "PSAConvert.h"
23 #include "StdSkeletons.h"
24 
25 #include <cstdarg>
26 #include <cassert>
27 
28 void default_logger(void*, int severity, const char* message)
29 {
30  fprintf(stderr, "[%d] %s\n", severity, message);
31 }
32 
34 static void* g_LoggerCBData = NULL;
35 
36 EXPORT void set_logger(LogFn logger, void* cb_data)
37 {
38  if (logger)
39  {
40  g_Logger = logger;
41  g_LoggerCBData = cb_data;
42  }
43  else
44  {
46  g_LoggerCBData = NULL;
47  }
48 }
49 
50 void Log(int severity, const char* msg, ...)
51 {
52  char buffer[1024];
53  va_list ap;
54  va_start(ap, msg);
55  vsnprintf(buffer, sizeof(buffer), msg, ap);
56  buffer[sizeof(buffer)-1] = '\0';
57  va_end(ap);
58 
59  g_Logger(g_LoggerCBData, severity, buffer);
60 }
61 
63 {
64  static const unsigned int bufferSize = 4096;
66  unsigned int bufferUsed;
67 
69  void* cb_data;
70 
72  : fn(fn), cb_data(cb_data), bufferUsed(0)
73  {
74  }
75 
77  {
78  // flush the buffer if it's not empty
79  if (bufferUsed > 0)
81  }
82 
83  virtual void operator() (const char* data, unsigned int length)
84  {
85  if (bufferUsed+length > bufferSize)
86  {
87  // will overflow buffer, so flush the buffer first
89  bufferUsed = 0;
90 
91  if (length > bufferSize)
92  {
93  // new data won't fit in buffer, so send it out unbuffered
94  fn(cb_data, data, length);
95  return;
96  }
97  }
98 
99  // append onto buffer
100  memcpy(buffer+bufferUsed, data, length);
101  bufferUsed += length;
102  assert(bufferUsed <= bufferSize);
103  }
104 };
105 
106 int convert_dae_to_whatever(const char* dae, OutputFn writer, void* cb_data, void(*conv)(const char*, OutputCB&, std::string&))
107 {
108  Log(LOG_INFO, "Starting conversion");
109 
110  FCollada::Initialize();
111 
112  std::string xmlErrors;
113  BufferedOutputCallback cb(writer, cb_data);
114  try
115  {
116  conv(dae, cb, xmlErrors);
117  }
118  catch (const ColladaException& e)
119  {
120  if (! xmlErrors.empty())
121  Log(LOG_ERROR, "%s", xmlErrors.c_str());
122 
123  Log(LOG_ERROR, "%s", e.what());
124 
125  FCollada::Release();
126 
127  return -2;
128  }
129 
130  FCollada::Release();
131 
132  if (! xmlErrors.empty())
133  {
134  Log(LOG_ERROR, "%s", xmlErrors.c_str());
135 
136  return -1;
137  }
138 
139  return 0;
140 }
141 
142 EXPORT int convert_dae_to_pmd(const char* dae, OutputFn pmd_writer, void* cb_data)
143 {
144  return convert_dae_to_whatever(dae, pmd_writer, cb_data, ColladaToPMD);
145 }
146 
147 EXPORT int convert_dae_to_psa(const char* dae, OutputFn psa_writer, void* cb_data)
148 {
149  return convert_dae_to_whatever(dae, psa_writer, cb_data, ColladaToPSA);
150 }
151 
152 EXPORT int set_skeleton_definitions(const char* xml, int length)
153 {
154  std::string xmlErrors;
155  try
156  {
157  Skeleton::LoadSkeletonDataFromXml(xml, length, xmlErrors);
158  }
159  catch (const ColladaException& e)
160  {
161  if (! xmlErrors.empty())
162  Log(LOG_ERROR, "%s", xmlErrors.c_str());
163 
164  Log(LOG_ERROR, "%s", e.what());
165 
166  return -1;
167  }
168 
169  return 0;
170 }
EXPORT int set_skeleton_definitions(const char *xml, int length)
Definition: DLL.cpp:152
EXPORT void set_logger(LogFn logger, void *cb_data)
Definition: DLL.cpp:36
static const unsigned int bufferSize
Definition: DLL.cpp:64
static LogFn g_Logger
Definition: DLL.cpp:33
virtual const char * what() const
Definition: CommonConvert.h:39
tuple xml
Definition: tests.py:119
#define LOG_ERROR
char buffer[bufferSize]
Definition: DLL.cpp:65
EXPORT int convert_dae_to_pmd(const char *dae, OutputFn pmd_writer, void *cb_data)
Definition: DLL.cpp:142
#define LOG_INFO
void default_logger(void *, int severity, const char *message)
Definition: DLL.cpp:28
BufferedOutputCallback(OutputFn fn, void *cb_data)
Definition: DLL.cpp:71
static void * g_LoggerCBData
Definition: DLL.cpp:34
virtual void operator()(const char *data, unsigned int length)
Definition: DLL.cpp:83
EXPORT int convert_dae_to_psa(const char *dae, OutputFn psa_writer, void *cb_data)
Definition: DLL.cpp:147
void(* LogFn)(void *cb_data, int severity, const char *text)
Definition: DLL.h:39
static void LoadSkeletonDataFromXml(const char *xmlData, size_t xmlLength, std::string &xmlErrors)
Initialises the global state with skeleton data loaded from the given XML data.
void ColladaToPMD(const char *input, OutputCB &output, std::string &xmlErrors)
Definition: PMDConvert.cpp:717
int convert_dae_to_whatever(const char *dae, OutputFn writer, void *cb_data, void(*conv)(const char *, OutputCB &, std::string &))
Definition: DLL.cpp:106
void(* OutputFn)(void *cb_data, const char *data, unsigned int length)
Definition: DLL.h:40
void ColladaToPSA(const char *input, OutputCB &output, std::string &xmlErrors)
Definition: PSAConvert.cpp:319
#define EXPORT
unsigned int bufferUsed
Definition: DLL.cpp:66
void Log(int severity, const char *msg,...)
Definition: DLL.cpp:50