Nicer XML-writing system

This was SVN commit r1711.
This commit is contained in:
Ykkrosh
2005-01-11 20:15:39 +00:00
parent cc0acc8691
commit fc4a34d768
4 changed files with 348 additions and 101 deletions
+60 -100
View File
@@ -12,6 +12,7 @@
#include "Unit.h"
#include "ps/Xeromyces.h"
#include "ps/XMLWriter.h"
#include "lib/res/vfs.h"
@@ -201,7 +202,7 @@ bool CObjectEntry::Load(const char* filename)
XMBElementList children = root.getChildNodes();
for (int i = 0; i < children.Count; ++i) {
// Get node
XMBElement child = children.item(i);
int element_name = child.getNodeName();
@@ -220,15 +221,19 @@ bool CObjectEntry::Load(const char* filename)
m_Material = element_value;
else if (element_name == el_properties) {
XMBAttributeList attributes=child.getAttributes();
for (int j = 0; j < attributes.Count; ++j) {
XMBAttribute attrib=attributes.item(j);
int attrib_name = attrib.Name;
if (attrib_name == at_autoflatten) {
CStr str= (CStr) attrib.Value;
CStr str (attrib.Value);
m_Properties.m_AutoFlatten=str.ToInt() ? true : false;
} else if (attrib_name == at_castshadows) {
CStr str= (CStr) attrib.Value;
CStr str = (attrib.Value);
m_Properties.m_CastShadows=str.ToInt() ? true : false;
}
}
@@ -244,9 +249,9 @@ bool CObjectEntry::Load(const char* filename)
if (attributes.Count) {
Anim anim;
anim.m_AnimName = (CStr)attributes.getNamedItem(at_name);
anim.m_FileName = (CStr)attributes.getNamedItem(at_file);
CStr speedstr = (CStr)attributes.getNamedItem(at_speed);
anim.m_AnimName = attributes.getNamedItem(at_name);
anim.m_FileName = attributes.getNamedItem(at_file);
CStr speedstr = attributes.getNamedItem(at_speed);
anim.m_Speed=float(speedstr.ToInt())/100.0f;
if (anim.m_Speed<=0.0) anim.m_Speed=1.0f;
@@ -265,8 +270,8 @@ bool CObjectEntry::Load(const char* filename)
if (attributes.Count) {
Prop prop;
prop.m_PropPointName = (CStr)attributes.getNamedItem(at_attachpoint);
prop.m_ModelName = (CStr)attributes.getNamedItem(at_model);
prop.m_PropPointName = attributes.getNamedItem(at_attachpoint);
prop.m_ModelName = attributes.getNamedItem(at_model);
m_Props.push_back(prop);
}
@@ -278,72 +283,8 @@ bool CObjectEntry::Load(const char* filename)
}
// TODO: Make this less hacky, and maybe make it part of the VFS
void vfs_printf(Handle h, const char* fmt, ...)
{
va_list va;
va_start(va, fmt);
char buf[1024];
int r = vsnprintf(buf, sizeof(buf), fmt, va);
if (r < 0 || r >= sizeof(buf))
{
// TODO: Handle this properly
debug_warn("Argh - too much stuff in vfs_printf");
return;
}
char *bufp = buf;
if (vfs_io(h, strlen(buf), (void**)&bufp) < 0)
{
debug_warn("vfs_io failed");
}
}
bool CObjectEntry::Save(const char* filename)
{
/*
FILE* fp=fopen(filename,"w");
if (!fp) return false;
// write XML header
fprintf(fp,"<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"no\"?>\n\n");
fprintf(fp,"<!DOCTYPE Object SYSTEM \"/art/actors/object.dtd\">\n\n");
// write the object itself
fprintf(fp,"<!-- File automatically generated by ScEd -->\n");
fprintf(fp,"<Object>\n");
fprintf(fp,"\t<Name>%s</Name>\n",(const char*) m_Name);
fprintf(fp,"\t<ModelName>%s</ModelName>\n",(const char*) m_ModelName);
fprintf(fp,"\t<TextureName>%s</TextureName>\n",(const char*) m_TextureName);
if(m_Material.Trim(PS_TRIM_BOTH).Length())
fprintf(fp,"\t<Material>%s</Material>\n", (const char*)m_Material);
fprintf(fp,"\t<Properties\n");
fprintf(fp,"\t\tautoflatten=\"%d\"\n",m_Properties.m_AutoFlatten ? 1 : 0);
fprintf(fp,"\t\tcastshadows=\"%d\"\n",m_Properties.m_CastShadows ? 1 : 0);
fprintf(fp,"\t/>\n");
if (m_Animations.size()>0) {
fprintf(fp,"\t<Animations>\n");
for (uint i=0;i<m_Animations.size();i++) {
fprintf(fp,"\t\t<Animation name=\"%s\" file=\"%s\" speed=\"%d\"/>\n",(const char*) m_Animations[i].m_AnimName,(const char*) m_Animations[i].m_FileName,int(m_Animations[i].m_Speed*100));
}
fprintf(fp,"\t</Animations>\n");
}
if (m_Props.size()>0) {
fprintf(fp,"\t<Props>\n");
for (uint i=0;i<m_Props.size();i++) {
fprintf(fp,"\t\t<Prop attachpoint=\"%s\" model=\"%s\"/>\n",(const char*) m_Props[i].m_PropPointName,(const char*) m_Props[i].m_ModelName);
}
fprintf(fp,"\t</Props>\n");
}
fprintf(fp,"</Object>\n");
fclose(fp);
return true;
*/
Handle h = vfs_open(filename, FILE_WRITE|FILE_NO_AIO);
if (h <= 0)
{
@@ -351,43 +292,62 @@ bool CObjectEntry::Save(const char* filename)
return false;
}
// write XML header
vfs_printf(h,"<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"no\"?>\n\n");
vfs_printf(h,"<!DOCTYPE Object SYSTEM \"/art/actors/object.dtd\">\n\n");
XML_Start("iso-8859-1");
XML_Doctype("Object", "/art/actors/object.dtd");
// write the object itself
vfs_printf(h,"<!-- File automatically generated by ScEd -->\n");
vfs_printf(h,"<Object>\n");
vfs_printf(h,"\t<Name>%s</Name>\n",(const char*) m_Name);
vfs_printf(h,"\t<ModelName>%s</ModelName>\n",(const char*) m_ModelName);
vfs_printf(h,"\t<TextureName>%s</TextureName>\n",(const char*) m_TextureName);
XML_Comment("File automatically generated by ScEd");
if(m_Material.Trim(PS_TRIM_BOTH).Length())
vfs_printf(h,"\t<Material>%s</Material>\n", (const char*)m_Material);
{
XML_Element("Object");
vfs_printf(h,"\t<Properties\n");
vfs_printf(h,"\t\tautoflatten=\"%d\"\n",m_Properties.m_AutoFlatten ? 1 : 0);
vfs_printf(h,"\t\tcastshadows=\"%d\"\n",m_Properties.m_CastShadows ? 1 : 0);
vfs_printf(h,"\t/>\n");
XML_Setting("Name", m_Name);
XML_Setting("ModelName", m_ModelName);
XML_Setting("TextureName", m_TextureName);
if (m_Animations.size()>0) {
vfs_printf(h,"\t<Animations>\n");
for (uint i=0;i<m_Animations.size();i++) {
vfs_printf(h,"\t\t<Animation name=\"%s\" file=\"%s\" speed=\"%d\"/>\n",(const char*) m_Animations[i].m_AnimName,(const char*) m_Animations[i].m_FileName,int(m_Animations[i].m_Speed*100));
if(m_Material.Trim(PS_TRIM_BOTH).Length())
XML_Setting("Material", m_Material);
{
XML_Element("Properties");
XML_Attribute("autoflatten", m_Properties.m_AutoFlatten ? 1 : 0);
XML_Attribute("castshadows", m_Properties.m_CastShadows ? 1 : 0);
}
vfs_printf(h,"\t</Animations>\n");
}
if (m_Props.size()>0) {
vfs_printf(h,"\t<Props>\n");
for (uint i=0;i<m_Props.size();i++) {
vfs_printf(h,"\t\t<Prop attachpoint=\"%s\" model=\"%s\"/>\n",(const char*) m_Props[i].m_PropPointName,(const char*) m_Props[i].m_ModelName);
if (m_Animations.size()>0)
{
XML_Element("Animations");
for (uint i=0;i<m_Animations.size();i++)
{
XML_Element("Animation");
XML_Attribute("name", m_Animations[i].m_AnimName);
XML_Attribute("file", m_Animations[i].m_FileName);
XML_Attribute("speed", int(m_Animations[i].m_Speed*100));
}
}
if (m_Props.size()>0)
{
XML_Element("Props");
for (uint i=0;i<m_Props.size();i++)
{
XML_Element("Prop");
XML_Attribute("attachpoint", m_Props[i].m_PropPointName);
XML_Attribute("model", m_Props[i].m_ModelName);
}
}
vfs_printf(h,"\t</Props>\n");
}
vfs_printf(h,"</Object>\n");
if (! XML_StoreVFS(h))
{
// Error occurred while saving data
debug_warn("actor save failed");
vfs_close(h);
return false;
}
vfs_close(h);
return true;
}