LCOV - code coverage report
Current view: top level - source/collada - DLL.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 61 69 88.4 %
Date: 2026-12-03 01:19:39 Functions: 6 10 60.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 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 "DLL.h"
      21             : 
      22             : #include "CommonConvert.h"
      23             : #include "PMDConvert.h"
      24             : #include "PSAConvert.h"
      25             : #include "StdSkeletons.h"
      26             : 
      27             : #include <FCollada.h>
      28             : #include <cassert>
      29             : #include <cstdarg>
      30             : #include <cstdio>
      31             : #include <cstring>
      32             : #include <string>
      33             : 
      34           0 : void default_logger(void*, int severity, const char* message)
      35             : {
      36           0 :     fprintf(stderr, "[%d] %s\n", severity, message);
      37           0 : }
      38             : 
      39             : static LogFn g_Logger = &default_logger;
      40             : static void* g_LoggerCBData = NULL;
      41             : 
      42          19 : EXPORT void set_logger(LogFn logger, void* cb_data)
      43             : {
      44          19 :     if (logger)
      45             :     {
      46          13 :         g_Logger = logger;
      47          13 :         g_LoggerCBData = cb_data;
      48             :     }
      49             :     else
      50             :     {
      51           6 :         g_Logger = &default_logger;
      52           6 :         g_LoggerCBData = NULL;
      53             :     }
      54          19 : }
      55             : 
      56          45 : void Log(int severity, const char* msg, ...)
      57             : {
      58          45 :     char buffer[1024];
      59          45 :     va_list ap;
      60          45 :     va_start(ap, msg);
      61          45 :     vsnprintf(buffer, sizeof(buffer), msg, ap);
      62          45 :     buffer[sizeof(buffer)-1] = '\0';
      63          45 :     va_end(ap);
      64             : 
      65          45 :     g_Logger(g_LoggerCBData, severity, buffer);
      66          45 : }
      67             : 
      68             : struct BufferedOutputCallback : public OutputCB
      69             : {
      70             :     static const unsigned int bufferSize = 4096;
      71             :     char buffer[bufferSize];
      72             :     unsigned int bufferUsed;
      73             : 
      74             :     OutputFn fn;
      75             :     void* cb_data;
      76             : 
      77           6 :     BufferedOutputCallback(OutputFn fn, void* cb_data)
      78           6 :         : fn(fn), cb_data(cb_data), bufferUsed(0)
      79             :     {
      80             :     }
      81             : 
      82           6 :     ~BufferedOutputCallback()
      83           6 :     {
      84             :         // flush the buffer if it's not empty
      85           6 :         if (bufferUsed > 0)
      86           5 :             fn(cb_data, buffer, bufferUsed);
      87           6 :     }
      88             : 
      89        2517 :     virtual void operator() (const char* data, unsigned int length)
      90             :     {
      91        2517 :         if (bufferUsed+length > bufferSize)
      92             :         {
      93             :             // will overflow buffer, so flush the buffer first
      94           3 :             fn(cb_data, buffer, bufferUsed);
      95           3 :             bufferUsed = 0;
      96             : 
      97           3 :             if (length > bufferSize)
      98             :             {
      99             :                 // new data won't fit in buffer, so send it out unbuffered
     100           0 :                 fn(cb_data, data, length);
     101           0 :                 return;
     102             :             }
     103             :         }
     104             : 
     105             :         // append onto buffer
     106        2517 :         memcpy(buffer+bufferUsed, data, length);
     107        2517 :         bufferUsed += length;
     108        2517 :         assert(bufferUsed <= bufferSize);
     109             :     }
     110             : };
     111             : 
     112           6 : int convert_dae_to_whatever(const char* dae, OutputFn writer, void* cb_data, void(*conv)(const char*, OutputCB&, std::string&))
     113             : {
     114           6 :     Log(LOG_INFO, "Starting conversion");
     115             : 
     116           6 :     FCollada::Initialize();
     117             : 
     118           6 :     std::string xmlErrors;
     119           6 :     BufferedOutputCallback cb(writer, cb_data);
     120           6 :     try
     121             :     {
     122           6 :         conv(dae, cb, xmlErrors);
     123             :     }
     124           1 :     catch (const ColladaException& e)
     125             :     {
     126           1 :         if (! xmlErrors.empty())
     127           1 :             Log(LOG_ERROR, "%s", xmlErrors.c_str());
     128             : 
     129           1 :         Log(LOG_ERROR, "%s", e.what());
     130             : 
     131           1 :         FCollada::Release();
     132             : 
     133           1 :         return -2;
     134           1 :     }
     135             : 
     136           5 :     FCollada::Release();
     137             : 
     138           5 :     if (! xmlErrors.empty())
     139             :     {
     140           0 :         Log(LOG_ERROR, "%s", xmlErrors.c_str());
     141             : 
     142             :         return -1;
     143             :     }
     144             : 
     145             :     return 0;
     146           7 : }
     147             : 
     148           6 : EXPORT int convert_dae_to_pmd(const char* dae, OutputFn pmd_writer, void* cb_data)
     149             : {
     150           6 :     return convert_dae_to_whatever(dae, pmd_writer, cb_data, ColladaToPMD);
     151             : }
     152             : 
     153           0 : EXPORT int convert_dae_to_psa(const char* dae, OutputFn psa_writer, void* cb_data)
     154             : {
     155           0 :     return convert_dae_to_whatever(dae, psa_writer, cb_data, ColladaToPSA);
     156             : }
     157             : 
     158           7 : EXPORT int set_skeleton_definitions(const char* xml, int length)
     159             : {
     160           7 :     std::string xmlErrors;
     161           7 :     try
     162             :     {
     163           7 :         Skeleton::LoadSkeletonDataFromXml(xml, length, xmlErrors);
     164             :     }
     165           1 :     catch (const ColladaException& e)
     166             :     {
     167           1 :         if (! xmlErrors.empty())
     168           1 :             Log(LOG_ERROR, "%s", xmlErrors.c_str());
     169             : 
     170           1 :         Log(LOG_ERROR, "%s", e.what());
     171             : 
     172           1 :         return -1;
     173           1 :     }
     174             : 
     175             :     return 0;
     176           7 : }

Generated by: LCOV version 1.16