天天看點

tinyxml讀xml檔案,轉化位元組,c#解析

xml

<?xml version="1.0"?>
<!-- 屬性,SceneName:場景名稱;SceneID:場景編号;PlanName: 工藝名稱;PlanID:工藝編号。-->
<Plan SceneName="desk" SceneID="A01" PlanName="Plan01" PlanID="P01">
	<!-- 屬性,ProcessName:工序名稱;ProcessID:工序編号;WorkerName:勞工名稱;WorkerID:勞工編号。-->
	<Process ProcessName="process01" ProcessID="AO1" WorkerName="Michael Wang" WorkerID="123">
		<Step StepName="p1" StepID="P1" Start="0" End="1000">
			<Object ObjectName="leg3" ObjectID="P03" Start="0" End="800">
				<Curve Name="Position" ID="pos0-1">
					<KeyFrame inTangent="1" outTangent="0" TangentMode="136" time="10.05">x y z</KeyFrame>
					<KeyFrame inTangent="2" outTangent="0" TangentMode="137" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="3" outTangent="0" TangentMode="138" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="4" outTangent="0" TangentMode="139" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="5" outTangent="0" TangentMode="140" time="0.15">x y z</KeyFrame>
				</Curve>
				<Curve Name="Rotation" ID="ros0-1">
					<KeyFrame inTangent="6" outTangent="0" TangentMode="136" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="7" outTangent="0" TangentMode="136" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="8" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
				</Curve>                                                       
				<Curve Name="Scale" ID="scale0-1">                                        
					<KeyFrame inTangent="9" outTangent="0" TangentMode="150" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="10" outTangent="0" TangentMode="151" time="0.10">x y z</KeyFrame>
				</Curve>
			</Object>
			<!--操作的物體,可以有多個,但必須有-->
			<Object ObjectName="leg4" ObjectID="P04" Start="10" End="8200">
				<Curve Name="Position" ID="pos1-1">
					<KeyFrame inTangent="11" outTangent="0" TangentMode="136" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="12" outTangent="0" TangentMode="136" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="13" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="14" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="15" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
				</Curve>
				<Curve Name="Rotation" ID="ros1-1">
					<KeyFrame inTangent="16" outTangent="0" TangentMode="136" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="17" outTangent="0" TangentMode="136" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="18" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
				</Curve>                                                       
				<Curve Name="Scale" ID="scale1-1">                                          
					<KeyFrame inTangent="19" outTangent="0" TangentMode="150" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="20" outTangent="0" TangentMode="151" time="0.10">x y z</KeyFrame>
				</Curve>
			</Object>
			<Object ObjectName="leg3" ObjectID="P03" Start="0" End="800">
				<Curve Name="Position" ID="pos2-1">
					<KeyFrame inTangent="21" outTangent="0" TangentMode="136" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="22" outTangent="0" TangentMode="136" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="23" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="24" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="25" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
				</Curve>
				<Curve Name="Rotation" ID="ros2-1">
					<KeyFrame inTangent="26" outTangent="0" TangentMode="136" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="27" outTangent="0" TangentMode="136" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="28" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
				</Curve>                                                       
				<Curve Name="Scale" ID="scale2-1">                                         
					<KeyFrame inTangent="29" outTangent="0" TangentMode="150" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="30" outTangent="0" TangentMode="151" time="0.10">x y z</KeyFrame>
				</Curve>
			</Object>

			<!--工具、工裝,可以有多個,也可以沒有-->
			<Tools name="tool1" ID="T01" Start="500" End="1000">
				<Curve Name="Position">
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
				</Curve>
				<Curve Name="Rotation">
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.10">x y z</KeyFrame>
					<KeyFrame inTangent="0" outTangent="0" TangentMode="136" time="0.15">x y z</KeyFrame>
				</Curve>                                                       
				<Curve Name="Scale">                                           
					<KeyFrame inTangent="0" outTangent="0" TangentMode="150" time="0.05">x y z</KeyFrame>
					<KeyFrame inTangent="0" outTangent="0" TangentMode="151" time="0.10">x y z</KeyFrame>
				</Curve>
			</Tools>
			
			<!--手,可以有多個,也可以沒有
				<Hands name="left" ID="Left" Start="500" End="1000">
					<Translate PointID="1" Start="500" End="1000">100 200 300</Translate>
					<Translate PointID="2" Start="500" End="1000">-50 0 30</Translate>
					<Translate PointID="5" Start="500" End="1000">30 50 60</Translate>
				</Hands>
			-->
			
			<!--對特定的工藝,result内的元素是明确的
				<Result>
					<Isfinished name="螺紋安裝是否完成" value="yes/no"/>
					<Isfinished name="螺紋安裝力矩" value="0.01牛米"/>
					<Isfinished name="螺紋安裝縫隙" value="0.01mm"/>
				</Result>
			-->
		</Step>
	</Process>
</Plan>           

AO.h

// ************************************************************************************************
// 解析AO xml需要使用的類 
// ************************************************************************************************
class AOBase
{
protected:
	NodeType					nodeType;		// 節點類型
	int								counts;			// bytearray的大小
	char								name[LEN_NAME];	// 節點名稱
	char								ID[LEN_ID];		// 節點ID

public:
	NodeType type() { return nodeType; }		// 傳回節點類型

	void SetName(const char * _name);			// 設定節點名稱
	void SetID(const char * _name);				// 設定節點ID

	virtual int byte_size();										// 節點大小,每一個子類均需要繼承
	int base_byte_size();									// 節點大小,每一個子類均需要繼承
	virtual void ToCharArray(char buf[], int&  index);			// 轉換為位元組碼

	AOBase( NodeType type) : nodeType(type), counts(0) {};		// 構造函數
	AOBase(char * buf, int&   index);							// 根據位元組碼構造
	virtual ~AOBase();											// 析構函數
};

struct KeyFrame
{
	int		inTangent;
	int		outTangent;
	int		tangentMode;

	double		time;
	double		x;
	double		y;
	double		z;

	KeyFrame(XMLElement *element);
	KeyFrame(char* buf, int& index);
	int byte_size();
	void ToCharArray(char buf[], int&  index);
};

class AOCurve : public AOBase
{
private:
	vector<KeyFrame>    keyFrames;

public:
	virtual int byte_size();									// 節點大小,每一個子類均需要繼承
	virtual void ToCharArray(char buf[], int&  index);			// 轉換為位元組碼
	void ReadKFValues(XMLElement *element);			//把xml中的keyframe對應的值讀進來

	AOCurve(XMLElement *element);
	AOCurve(char * buf, int&   index);							// 根據位元組碼構造
	virtual ~AOCurve();
};

// 三種,包括模型、工具、手,必須先指定節點類型
class AOObject : public AOBase
{
private:
	int start;
	int end;

public:
	vector<AOCurve> curves;
	
	virtual int byte_size();									// 節點大小,每一個子類均需要繼承
	virtual void ToCharArray(char buf[], int&  index);

	AOObject(char * data, int&  index);

	AOObject(XMLElement *element);
	~AOObject();
};

class AOStep : public AOBase
{
private:
	int start;
	int end;

public:
	vector<AOObject> objects;

	virtual int byte_size();									// 節點大小,每一個子類均需要繼承
	virtual void ToCharArray(char buf[], int&  index);

	AOStep(XMLElement *element);
	AOStep(char * data, int&  index);
	~AOStep();
};

class AOProcess : public AOBase
{
private:
	char				worker_name[LEN_NAME];					// 勞工名稱
	char				worker_ID[LEN_ID];						// 勞工ID

public:
	vector<AOStep>		steps;

	virtual int byte_size();									// 節點大小,每一個子類均需要繼承
	virtual void ToCharArray(char buf[], int&  index);

	AOProcess(XMLElement *element);
	AOProcess(char * data, int&  index);
	~AOProcess();
};


class AOPlan : public AOBase
{
private:
	vector<AOProcess>	processes;
	char				scene_name[LEN_NAME];		// 工藝規程名稱
	char				scene_ID[LEN_ID];			// 工藝規程ID

public:
	void SetSceneName(const char * _name);
	void SetSceneID(const char * _name);

	bool ReadAOXML(const char* file);

	virtual int byte_size();									// 節點大小,每一個子類均需要繼承
	virtual void ToCharArray(char buf[], int&  index);

	AOPlan();
	AOPlan(char * data, int&  index);
	~AOPlan();
};
           
#include "Scene.h"
#include <stdio.h>  
#include <string.h>

// *********************************************************************************************************
// class Base_NameID
// *********************************************************************************************************
AOBase::AOBase(char* buf, int&  index)
{
	// get value of nodetype
	int  len = sizeof(NodeType);
	memcpy_s(&nodeType, len, &buf[index], len);
	index += len;

	// all count
	len = sizeof(__int32);
	memcpy_s(&counts, len, &buf[index], len);
	index += len;

	
	//解析name的實際長
	len = sizeof(__int32);
	int num_name = 0;
	memcpy_s(&num_name, len, &buf[index], len);
	index += len;

	//按實際長解析name
	len = num_name;
	memcpy_s(&name, len, &buf[index], len);
	index += len;
	

	len = sizeof(__int32);
	int num_ID = 0;
	memcpy_s(&num_ID, len, &buf[index], len);				// 拷貝零件ID
	index += len;

	len = num_ID;
	memcpy_s(ID, len, &buf[index], len);				// 拷貝零件ID
	index += len;
}

AOBase::~AOBase()
{

};

void AOBase::SetName(const char * _name)
{
	strncpy_s(name, _name, LEN_NAME);
}

void AOBase::SetID(const char * _ID)
{
	strncpy_s(ID, _ID, LEN_NAME);
}
int AOBase::base_byte_size()									// 節點大小,每一個子類均需要繼承
{
	int len = sizeof(__int32) * 2 + sizeof(char)* LEN_NAME + sizeof(char)*LEN_ID;
	return len;
}

int AOBase::byte_size()
{
	return base_byte_size();
}

void AOBase::ToCharArray(char buf[], int&  index)
{
	// nodeType
	int len = sizeof(NodeType);
	memcpy_s(&(buf[index]), len, &nodeType, len);
	index += len;

	//counts 
	len = sizeof(__int32);
	memcpy_s(&(buf[index]), len, &counts, len);
	index += len;

	//name
	len = sizeof(__int32);
	int count_name = strlen(name) + 1;
	memcpy_s(&(buf[index]), len, &count_name, len);
	index += len;

	len = count_name;
	memcpy_s(&(buf[index]), len, name, len);
	index += len;

	//ID
	len = sizeof(__int32);
	int count_ID = strlen(ID) + 1;
	memcpy_s(&(buf[index]), len, &count_ID, len);
	index += len;

	len = count_ID;
	memcpy_s(&(buf[index]), len, ID, len);
	index += len;
}

// ****************************************************************************************************************
// struct KeyFrame
// ****************************************************************************************************************
KeyFrame::KeyFrame(XMLElement *element)
{
	inTangent = atoi(element->Attribute("inTangent"));
	outTangent = atoi(element->Attribute("outTangent"));
	tangentMode = atoi(element->Attribute("TangentMode"));
	//tangentMode = element->IntAttribute ("TangentMode");
	 time = element->DoubleAttribute("time");

	string value = element->Value();
	int n1 = value.find_first_of(' ');
	int n2 = value.find_last_of(' ');

	string x1 = value.substr(0, n1 - 1);
	string y1 = value.substr(n1 + 1, n2 - 1);
	string z1= value.substr(n2 + 1, value.size() - 1);

	x = atof(x1.c_str());
	y = atof(y1.c_str());
	z = atof(z1.c_str());
}

int KeyFrame::byte_size()
{
	return 3 * sizeof(__int32) + 4 * sizeof(double);
}

KeyFrame::KeyFrame(char* buf, int& index)
{
	int len = sizeof(__int32);
	memcpy_s(&inTangent, len, &buf[index], len);
	index += len;

	memcpy_s(&outTangent, len, &buf[index], len);
	index += len;

	memcpy_s(&tangentMode, len, &buf[index], len);
	index += len;

	len = sizeof(double);
	memcpy_s(&time, len, &buf[index], len);
	index += len;

	memcpy_s(&x, len, &buf[index], len);
	index += len;

	memcpy_s(&y, len, &buf[index], len);
	index += len;

	memcpy_s(&z, len, &buf[index], len);
	index += len;
}
void KeyFrame::ToCharArray(char buf[], int&  index)
{
	int len = sizeof(__int32);
	memcpy_s(&buf[index], len, &inTangent, len);
	index += len;

	memcpy_s(&buf[index], len, &outTangent, len);
	index += len;

	memcpy_s(&buf[index], len, &tangentMode, len);
	index += len;

	len = sizeof(double);
	memcpy_s(&buf[index], len, &time, len);
	index += len;

	memcpy_s(&buf[index], len, &x, len);
	index += len;

	memcpy_s(&buf[index], len, &y, len);
	index += len;

	memcpy_s(&buf[index], len, &z, len);
	index += len;
}


// ****************************************************************************************************************
// class AOObject
// ****************************************************************************************************************
AOCurve::AOCurve(XMLElement *element) : AOBase(NODE_NULL)
{
	 XMLElement *ele = element->FirstChildElement();

		if (strcmp("Position", element->Attribute("Name")) == 0)
		{
			nodeType = NODE_CURVE_POSITION;
			strncpy_s(name, element->Attribute("Name"), LEN_NAME);
			strncpy_s(ID, element->Attribute("ID"), LEN_NAME);
			ReadKFValues(ele);
		}
		else if (strcmp("Rotation", element->Attribute("Name")) == 0)
 		{
			nodeType = NODE_CURVE_ROTATION;
			strncpy_s(name, element->Attribute("Name"), LEN_NAME);
			strncpy_s(ID, element->Attribute("ID"), LEN_NAME);
			ReadKFValues(ele);
		}
		else			// scale
		{
			nodeType = NODE_CURVE_SCALE;
			strncpy_s(name, element->Attribute("Name"), LEN_NAME);
			strncpy_s(ID, element->Attribute("ID"), LEN_NAME);
			ReadKFValues(ele);
		}
}

AOCurve::AOCurve(char* buf, int& index) : AOBase(buf, index)
{
	int pos = index;
	int len = sizeof(__int32);
	int count = 0;// keyframes.size()
	memcpy_s(&count, len, &buf[pos], len);
	pos += len;

	for (size_t i = 0; i < count; i++)
	{
		KeyFrame keyFrame(buf, index);
		keyFrames.push_back(keyFrame);
		pos += keyFrame.byte_size();
	}
	index = pos;
}

AOCurve::~AOCurve()
{

}

void AOCurve::ReadKFValues(XMLElement *element)		//把xml中的keyframe對應的值讀進來
{
	while (element != NULL)
	{
		keyFrames.push_back(KeyFrame(element));
		element = element->NextSiblingElement();
	}
}

int AOCurve::byte_size()
{
	int len = base_byte_size() + sizeof(__int32);
	for (size_t i = 0; i < keyFrames.size(); i++)
	{
		len += keyFrames[i].byte_size();
	}
	return  len;
}

void AOCurve::ToCharArray(char buf[], int& index)
{
	AOBase::ToCharArray(buf, index);
	// copy x to buf
	//int pos = index;
	int len = sizeof(__int32);
	int count = keyFrames.size();
	memcpy_s(&buf[index], len, &count, len);
	index += len;

	for (size_t i = 0; i < count; i++)
	{
		keyFrames[i].ToCharArray(buf, index);
		//index += keyFrames[i].byte_size();
	}
}



// ****************************************************************************************************************
// class AOObject
// ****************************************************************************************************************
AOObject::AOObject(XMLElement *element) : AOBase(NODE_OBJECT)
{
	SetID(element->Attribute("ObjectName"));
	SetName(element->Attribute("ObjectID"));
	start = atoi(element->Attribute("Start"));
	end = atoi(element->Attribute("End"));
}

AOObject::AOObject(char* data, int& index) : AOBase(data, index)
{
	int len = sizeof(__int32);
	memcpy_s(&start, len, &data[index], len);
	index += len;

	memcpy_s(&end, len, &data[index], len);
	index += len;

	int count =0;
	memcpy_s(&count, len, &(data[index]), len);
	index += len;

	for (int i = 0; i < count; i++)
	{
		curves.push_back(AOCurve(data, index));
		//index += curves[curves.size() - 1].byte_size();
	}
}

AOObject::~AOObject()
{
}

int AOObject::byte_size()
{
	//容器大小需要寫嗎
	int len = base_byte_size() + 3*sizeof(__int32);

	for (size_t i = 0; i < curves.size(); i++)
	{
		len += curves[i].byte_size();
	}
	return len;			
}

void AOObject::ToCharArray(char buf[], int& index)
{
	AOBase::ToCharArray(buf, index);
	
	int len = sizeof(__int32);
	memcpy_s(&(buf[index]), len, &start, len);
	index += len;

	memcpy_s(&(buf[index]), len, &end, len);
	index += len;

	int count = curves.size();
	memcpy_s(&(buf[index]), len, &count, len);
	index += len;

	for (size_t i = 0; i < count; i++)
	{
		 curves[i].ToCharArray(buf, index);
		// index += curves[i].byte_size();
	}
}

// ****************************************************************************************************************
// class AOStep
// ****************************************************************************************************************
AOStep::AOStep(char* data, int& index) : AOBase(data, index)
{
	int len = sizeof(__int32);
	memcpy_s(&start, len, &data[index], len);
	index += len;

	memcpy_s(&end, len, &data[index], len);
	index += len;

	int count = 0;
	memcpy_s(&count, len, &data[index], len);
	index += len;

	for (size_t i = 0; i < count; i++)
	{
		objects.push_back(AOObject(data, index));
		//index += objects[objects.size() - 1].byte_size();
	}

}

AOStep::AOStep(XMLElement *element) : AOBase(NODE_STEP)
{
	SetID(element->Attribute("StepName"));
	SetName(element->Attribute("StepID"));
	start = atoi(element->Attribute("Start"));
	end = atoi(element->Attribute("End"));
}

AOStep::~AOStep()
{
}

int AOStep::byte_size()
{
	int len = base_byte_size();
	len += 3 * sizeof(__int32);

	for (size_t i = 0; i < objects.size(); i++)
	{
		len += objects[i].byte_size();
	}
	return len;			// 有問題
}

void AOStep::ToCharArray(char buf[], int& index)
{
	AOBase::ToCharArray(buf, index);

	int len = sizeof(__int32);
	memcpy_s(&(buf[index]), len, &start, len);
	index += len;

	memcpy_s(&(buf[index]), len, &end, len);
	index += len;

	int count = objects.size();
	memcpy_s(&buf[index], len, &count, len);
	index += len;

	for (size_t i = 0; i < count; i++)
	{
		objects[i].ToCharArray(buf, index);
		//index += objects[i].byte_size();
	}

}
// **************************************************************************************************************
// class AOProcess
// ************************************************************************************************************
AOProcess::AOProcess(XMLElement *element) : AOBase(NodeType::NODE_PROCESS)
{
	SetID(element->Attribute("ProcessID")); 
	SetName(element->Attribute("ProcessName"));
	
	strncpy_s(worker_name, element->Attribute("WorkerName"), LEN_NAME);
	strncpy_s(worker_ID, element->Attribute("WorkerID"), LEN_ID);
}

AOProcess::~AOProcess()
{

}

AOProcess::AOProcess(char* data, int& index) : AOBase(data, index)
{

	int len = sizeof(__int32);
	int num_name = 0;
	memcpy_s(&num_name, len, &data[index], len);
	index += len;

	len = num_name;
	memcpy_s(&worker_name, len, &data[index], len);
	index += len;

	int num_ID = 0;
	memcpy_s(&num_ID, len, &data[index], len);				// 拷貝零件ID
	index += len;

	len = num_ID;
	memcpy_s(worker_ID, len, &data[index], len);				// 拷貝零件ID
	index += len;

	int count = 0;
	len = sizeof(__int32);
	memcpy_s(&count, len, &(data[index]), len);
	index += len;

	for (int i = 0; i < count; i++)
	{
		steps.push_back(AOStep(data, index));
		//index += steps[steps.size() - 1].byte_size();
	}
}

int AOProcess::byte_size()
{
	int len = base_byte_size();
	len += sizeof(char)*(LEN_NAME + LEN_ID);

	for (size_t i = 0; i < steps.size(); i++)
	{
		len +=	steps[i].byte_size();
	}
	return len;			
}

void AOProcess::ToCharArray(char buf[], int& index)
{
	AOBase::ToCharArray(buf, index);

	int len = sizeof(__int32);
	int count_name = strlen(worker_name) + 1;
	memcpy_s(&(buf[index]), len, &count_name, len);
	index += len;

	len = count_name;
	memcpy_s(&(buf[index]), len, worker_name, len);
	index += len;

	int count_ID = strlen(worker_ID) + 1;
	memcpy_s(&(buf[index]), len, &count_ID, len);
	index += sizeof(__int32);

	len = count_ID;
	memcpy_s(&(buf[index]), len, worker_ID, len);
	index += len;

	int count = steps.size();
	memcpy_s(&(buf[index]), len, &count, len);
	index += len;

	for (size_t i = 0; i < count; i++)
	{
		steps[i].ToCharArray(buf, index);
		//index += steps[i].byte_size();
	}
}

// **********************************************************************************************
// class AssemblyOrder
// *********************************************************************************************
AOPlan::AOPlan() : AOBase(NODE_PLAN)
{
}

AOPlan::AOPlan(char * data, int& index) : AOBase(data, index)
{
	int count;
	int len = sizeof(int);
	memcpy_s(&count, len, &(data[index]), len);
	index += len;

	for (int i = 0; i < count; i++)
	{
		processes.push_back(AOProcess(data, index));
		//index += processes[processes.size() - 1].byte_size();
	}

	int num_name = 0;
	memcpy_s(&num_name, len, &data[index], len);
	index += len;

	len = num_name;
	memcpy_s(&scene_name, len, &data[index], len);
	index += len;

	int num_ID = 0;
	memcpy_s(&num_ID, len, &data[index], len);				// 拷貝零件ID
	index += len;

	len = num_ID;
	memcpy_s(scene_ID, len, &data[index], len);				// 拷貝零件ID
	index += len;
}

AOPlan::~AOPlan()
{
}

int AOPlan::byte_size()
{
	int len = base_byte_size();
	len += sizeof(__int32);

	for (size_t i = 0; i < processes.size(); i++)
	{
		len += processes[i].byte_size();
	}
	len += sizeof(char)*(LEN_NAME + LEN_ID);
	return len;			
}

void AOPlan::ToCharArray(char buf[], int& index)
{
	AOBase::ToCharArray(buf, index);

	int count = processes.size();
	int	len = sizeof(int);
	memcpy_s(&(buf[index]), len, &count, len);
	index += len;

	for (size_t i = 0; i < count; i++)
	{
		processes[i].ToCharArray(buf, index);
		//index += processes[i].byte_size();
	}

	int count_name = strlen(scene_name) + 1;
	memcpy_s(&(buf[index]), len, &count_name, len);
	index += len;

	len = count_name;
	memcpy_s(&(buf[index]), len, scene_name, len);
	index += len;

	int count_ID = strlen(scene_ID) + 1;
	memcpy_s(&(buf[index]), len, &count_ID, len);
	index += sizeof(__int32);

	len = count_ID;
	memcpy_s(&(buf[index]), len, scene_ID, len);
	index += len;

	/
	//to send all length
	len = sizeof(__int32);
	counts = index;
	memcpy_s(&(buf[len]), len, &counts, len);
}

void AOPlan::SetSceneName(const char * _name)
{
	strncpy_s(scene_name, _name, LEN_NAME);
}

void AOPlan::SetSceneID(const char * _ID)
{
	strncpy_s(scene_ID, _ID, LEN_ID);
}

bool AOPlan::ReadAOXML(const char* file)
{
	tinyxml2::XMLDocument docR;
	docR.LoadFile(file);
	if (docR.Error())
	{
		cout << "ERROR! XML file error!" << endl;
		return false;
	}

	//docR.SaveFile("Formatted_AO.xml");
	XMLElement *ele_AO = docR.RootElement();									// ele_AO:plan節點

	SetID(ele_AO->Attribute("PlanID"));
	SetName(ele_AO->Attribute("PlanName"));
	SetSceneID(ele_AO->Attribute("SceneName"));
	SetSceneName(ele_AO->Attribute("SceneID"));

	XMLElement *ele_process = ele_AO->FirstChildElement("Process");				// 第一個process子節點
	while (ele_process != NULL)													// 如果孩子節點不為空
	{
		if (strcmp("Process", ele_process->Value()) == 0)
		{
			//AOProcess AOP(ele_process);
			//processes.push_back(AOP);

			processes.push_back(AOProcess(ele_process));
			AOProcess& process = processes[processes.size() - 1];

			XMLElement *ele_step = ele_process->FirstChildElement();
			while (ele_step != NULL)
			{
				if (strcmp("Step", ele_step->Value()) == 0)
				{
					process.steps.push_back(AOStep(ele_step));
					AOStep& step = process.steps[process.steps.size() - 1];

					XMLElement *ele_gameobject = ele_step->FirstChildElement();
					while (ele_gameobject != NULL)										// 如果孩子節點不為空
					{
						if (strcmp("Object", ele_gameobject->Value()) == 0)
						{
							step.objects.push_back(AOObject(ele_gameobject));
							AOObject& object = step.objects[step.objects.size() - 1];

							XMLElement *ele_curve = ele_gameobject->FirstChildElement();
							while (ele_curve != NULL)										// 如果孩子節點不為空
							{
								if (strcmp("Curve", ele_curve->Value()) == 0)
								{
									object.curves.push_back(AOCurve(ele_curve));
								}
								ele_curve = ele_curve->NextSiblingElement();
							}
						}
						ele_gameobject = ele_gameobject->NextSiblingElement();
					}
				}
				ele_step = ele_step->NextSiblingElement();
			}
		}
		ele_process = ele_process->NextSiblingElement();
	}
	return  true;
}           
//encode  計算總共占了多少位元組
	char buf[DEFAULT_BUFLEN];
	memset(buf, 0, sizeof(char)*DEFAULT_BUFLEN);
	int buflen = 0;
	
	m_pAORead->ToCharArray(buf, buflen);
	int ret = send(ctParam->ClientSocket, buf, buflen, 0);           
public class AOBase
	{
		public DataType     NodeType; 
		public int 			Counts;
		public string 		Name;
		public string 		ID;	

		public AOBase(byte[] data, ref int index)
		{
			NodeType = (DataType)BitConverter.ToInt32(data, index);

			Debug.Log ("Nodetype is " + NodeType);

			index += sizeof(DataType);	
			Counts = BitConverter.ToInt32 (data, index);
			Debug.Log ("Counts is " + Counts);
		
			int num_name = 0;
			index += sizeof(Int32);
			num_name = BitConverter.ToInt32 (data, index);

			index += sizeof(Int32);
			Name = System.Text.Encoding.Default.GetString (data, index, num_name);
			Debug.Log ("Name is " + Name);

			int num_id = 0;
			index += num_name;
			num_id = BitConverter.ToInt32 (data, index);

			index += sizeof(Int32);
			ID = System.Text.Encoding.Default.GetString (data, index, num_id);
			Debug.Log ("ID is " + ID);

			index += num_id;
		}



	}

	public class KeyFrame
	{
		public int inTangent;
		public int outTangent;
		public int tangentMode;

		public double time;
		public double x;
		public double y;
		public double z;

		public KeyFrame(byte[] data, ref int index)
		{
			inTangent = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("inTangent" + inTangent);

			outTangent = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("outTangent" + outTangent);

			tangentMode = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("tangentMode" + tangentMode);

			time = BitConverter.ToDouble (data, index);
			index += sizeof(double);
			Debug.Log ("time" + time);

			x = BitConverter.ToDouble (data, index);
			index += sizeof(double);
			Debug.Log ("x" + x);

			y = BitConverter.ToDouble (data, index);
			index += sizeof(double);
			Debug.Log ("y" + x);

			z = BitConverter.ToDouble (data, index);
			index += sizeof(double);
			Debug.Log ("z" + x);

		}
	}

	public class AOCurve:AOBase
	{
		public List<KeyFrame> AOkeyFrames;

		public AOCurve(byte[] data, ref int index):base(data, ref index)
		{

			int count = BitConverter.ToInt32 (data, index);
			Debug.Log ("AOkeyFrames count" + count);

			index += sizeof(Int32);
			AOkeyFrames = new List<KeyFrame>();
			for (int i = 0; i < count; i++) {
				AOkeyFrames.Add (new KeyFrame(data, ref index));
			}
		}

	}

	public class AOObject:AOBase
	{
		public int start;
		public int end;
		public List<AOCurve> curves;

		public AOObject(byte[] data, ref int index):base(data, ref index)
		{
			start = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("AOObject start" + start);

			end = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("AOObject end" + end);

			int count = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("AOCurve count" + count);

			curves = new List<AOCurve> ();
			for (int i = 0; i < count; i++) {
				curves.Add (new AOCurve(data, ref index));
			}
		}

	}

	public class AOStep:AOBase
	{
		public int start;
		public int end;
		public List<AOObject> objects;

		public AOStep(byte[] data, ref int index):base(data, ref index)
		{
			start = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("AOStep start" + start);

			end = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("AOStep end" + end);

			int count = BitConverter.ToInt32 (data, index);
			index += sizeof(Int32);
			Debug.Log ("AOObject count" + count);

			objects = new List<AOObject> ();
			for (int i = 0; i < count; i++) {
				objects.Add (new AOObject(data, ref index));
			}
		}
		
	}

	public class AOProcess: AOBase
	{
		public string 	Worker_Name;
		public string 	Worker_ID;	
		public List<AOStep> steps;

		public AOProcess (byte[] data, ref int index):base(data, ref index)
		{
			int num_name = 0;
			num_name = BitConverter.ToInt32 (data, index);

			index += sizeof(Int32);
			Worker_Name = System.Text.Encoding.Default.GetString (data, index, num_name);
			//Debug.Log ("Worker_Name is " + Worker_Name);

			int num_id = 0;
			index += num_name;
			//Debug.Log (" num_name  is " + num_name);
			num_id = BitConverter.ToInt32 (data, index);
			//Debug.Log (" AOProcess num_id is " + num_id);
			index += sizeof(Int32);

			//Debug.Log (" AOProcess index is " + index);
			Worker_ID = System.Text.Encoding.Default.GetString (data, index, num_id);
			//Debug.Log ("Worker_ID is " + Worker_ID);

			index += num_id;
			int count = BitConverter.ToInt32 (data, index);
			Debug.Log ("AOStep count" + count);

			index += sizeof(Int32);
			steps = new List<AOStep> ();
			for (int i = 0; i < count; i++) {
				//Debug.Log ("Here" + index);
				steps.Add (new AOStep(data, ref index));
			}
		}
	}




	public class AOPlan: AOBase
	{
		public List<AOProcess> processes;
		public string 	Scene_Name;
		public string 	Scene_ID;	

		public AOPlan(byte[] data,  int index = 0):base(data, ref index)
		{
			int len = sizeof(Int32);
			int count = BitConverter.ToInt32 (data, index);
			index += len;

			Debug.Log ("AOProcess count" + count);

			processes = new List<AOProcess>();
			for (int i = 0; i < count; i++) {
				processes.Add (new AOProcess(data, ref index));
			}


			int num_name = 0;
			num_name = BitConverter.ToInt32 (data, index);

			index += sizeof(Int32);
			Scene_Name = System.Text.Encoding.Default.GetString (data, index, num_name);
			Debug.Log ("Scene_Name is " + Scene_Name);

			int num_id = 0;
			index += num_name;
			num_id = BitConverter.ToInt32 (data, index);

			index += sizeof(Int32);
			Scene_ID = System.Text.Encoding.Default.GetString (data, index, num_id);
			Debug.Log ("Scene_ID is " + Scene_ID);
		}
	}

           
上一篇: tinyxml讀xml