1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "stdgeorges.h"
22 #include "nel/misc/file.h"
23 #include "nel/misc/path.h"
24 #include "nel/misc/i_xml.h"
26 #include "nel/georges/u_form.h"
28 #include "nel/georges/form_loader.h"
29 #include "nel/georges/type.h"
30 #include "nel/georges/form.h"
31 #include "nel/georges/form_dfn.h"
37 using namespace NLMISC
;
43 // ***************************************************************************
45 void warning (bool exception
, const char *format
, ... );
47 // ***************************************************************************
49 // ***************************************************************************
51 UFormLoader
*UFormLoader::createLoader ()
53 return new CFormLoader
;
56 // ***************************************************************************
58 void UFormLoader::releaseLoader (UFormLoader
*loader
)
60 delete ((CFormLoader
*)loader
);
63 // ***************************************************************************
65 // ***************************************************************************
66 CFormLoader::~CFormLoader()
70 CType
*CFormLoader::loadType (const std::string
&filename
)
72 // Lower string filename
73 string lowerStr
= toLowerAscii(filename
);
74 lowerStr
= CFile::getFilename (lowerStr
);
76 // Already in the map ?
77 TTypeMap::iterator ite
= _MapType
.find (lowerStr
);
78 if (ite
!= _MapType
.end() && (ite
->second
!= NULL
) )
86 CType
*type
= new CType
;
92 string name
= CPath::lookup (filename
, false, false);
103 type
->read (read
.getRootNode ());
108 warning (false, "loadType", "Can't open the form file (%s).", filename
.c_str());
115 catch (const Exception
&e
)
118 warning (false, "loadType", "Error while loading the form (%s): %s", filename
.c_str(), e
.what());
128 // Insert a new entry
129 _MapType
[lowerStr
]= type
;
130 ite
= _MapType
.find (lowerStr
);
131 //CType *typeType = ite->second;
138 // ***************************************************************************
140 CFormDfn
*CFormLoader::loadFormDfn (const std::string
&filename
, bool forceLoad
)
142 // Lower string filename
143 string lowerStr
= toLowerAscii(filename
);
144 lowerStr
= CFile::getFilename (lowerStr
);
146 // Already in the map ?
147 TFormDfnMap::iterator ite
= _MapFormDfn
.find (lowerStr
);
148 if (ite
!= _MapFormDfn
.end() && ite
->second
)
150 // Return the pointer
155 // Create the formDfn
156 CFormDfn
*formDfn
= new CFormDfn
;
158 // Insert the form first
159 _MapFormDfn
[lowerStr
] = formDfn
;
165 string name
= CPath::lookup (filename
, false, false);
169 if (file
.open (name
))
171 // Init an xml stream
176 formDfn
->read (read
.getRootNode (), *this, forceLoad
, filename
);
181 warning (false, "loadFormDfn", "Can't open the form file (%s).", filename
.c_str());
183 // Delete the formDfn
186 _MapFormDfn
.erase (lowerStr
);
189 catch (const Exception
&e
)
192 warning (false, "loadFormDfn", "Error while loading the form (%s): %s", filename
.c_str(), e
.what());
194 // Delete the formDfn
197 _MapFormDfn
.erase (lowerStr
);
204 // ***************************************************************************
206 UForm
*CFormLoader::loadForm (const std::string
&filename
)
208 // Lower string filename
209 string lowerStr
= toLowerAscii((string
)filename
);
210 lowerStr
= CFile::getFilename (lowerStr
);
212 // Already in the map ?
213 TFormMap::iterator ite
= _MapForm
.find (lowerStr
);
214 if (ite
!= _MapForm
.end() && ite
->second
)
216 // Return the pointer
217 return (CForm
*)ite
->second
;
222 CForm
*form
= new CForm
;
224 // Insert the form first
225 _MapForm
[lowerStr
] = form
;
230 // Get the form DFN filename
231 string name
= CFile::getFilename (filename
);
232 string::size_type index
= name
.rfind ('.');
233 if (index
== string::npos
)
236 warning (false, "loadForm", "Form name is invalid (%s). It should have the extension of its DFN type.", name
.c_str ());
241 _MapForm
.erase (lowerStr
);
243 name
= name
.substr (index
+1);
247 CSmartPtr
<CFormDfn
> dfn
= loadFormDfn (name
, false);
251 name
= CPath::lookup (filename
, false, false);
255 if (file
.open (name
))
257 // Init an xml stream
262 form
->read (read
.getRootNode (), *this, dfn
, filename
);
267 warning (false, "loadForm", "Can't open the form file (%s).", filename
.c_str());
272 _MapForm
.erase (lowerStr
);
278 warning (false, "loadForm", "Can't open the dfn file (%s).", name
.c_str ());
283 _MapForm
.erase (lowerStr
);
286 catch (const Exception
&e
)
289 warning (false, "loadForm", "Error while loading the form (%s): %s", filename
.c_str(), e
.what());
294 _MapForm
.erase (lowerStr
);
301 // ***************************************************************************
303 UFormDfn
*CFormLoader::loadFormDfn (const std::string
&filename
)
305 return loadFormDfn (filename
, false);
308 // ***************************************************************************
310 UType
*CFormLoader::loadFormType (const std::string
&filename
)
312 return loadType (filename
);
315 // ***************************************************************************
317 void CFormLoader::warning (bool exception
, const std::string
&function
, const char *format
, ... ) const
319 // Make a buffer string
321 va_start( args
, format
);
323 vsnprintf( buffer
, 1024, format
, args
);
327 NLGEORGES::warning (exception
, "(CFormLoader::%s) : %s", function
.c_str(), buffer
);
330 // ***************************************************************************