2023-11-12 01:59:17 +08:00
using CodeWalker.GameFiles ;
using SharpDX ;
2017-09-21 18:33:05 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
2019-01-19 01:31:13 +08:00
using System.Text.RegularExpressions ;
2017-09-21 18:33:05 +08:00
using System.Threading.Tasks ;
using System.Xml ;
2023-11-12 01:59:17 +08:00
using System.Xml.Linq ;
2017-09-21 18:33:05 +08:00
namespace CodeWalker
{
public static class Xml
{
2023-11-12 01:59:17 +08:00
public static void ValidateReaderState ( XmlReader reader , string element )
{
if ( reader = = null )
{
throw new ArgumentNullException ( nameof ( reader ) ) ;
}
if ( ! reader . IsStartElement ( ) )
{
throw new InvalidOperationException ( $"Expected reader to be at a start element but was at \" { reader . NodeType } \ " with name \"{reader.Name}\"" ) ;
}
if ( reader . Name ! = element )
{
throw new InvalidOperationException ( $"Expected reader to be at start element of \" { element } \ " but was at \"{reader.NodeType}\" with name \"{reader.Name}\"" ) ;
}
}
2017-09-21 18:33:05 +08:00
public static string GetStringAttribute ( XmlNode node , string attribute )
{
if ( node = = null ) return null ;
return node . Attributes [ attribute ] ? . InnerText ;
}
public static bool GetBoolAttribute ( XmlNode node , string attribute )
{
if ( node = = null ) return false ;
string val = node . Attributes [ attribute ] ? . InnerText ;
bool b ;
bool . TryParse ( val , out b ) ;
return b ;
}
public static int GetIntAttribute ( XmlNode node , string attribute )
{
if ( node = = null ) return 0 ;
string val = node . Attributes [ attribute ] ? . InnerText ;
int i ;
int . TryParse ( val , out i ) ;
return i ;
}
2019-01-19 01:31:13 +08:00
public static uint GetUIntAttribute ( XmlNode node , string attribute )
{
if ( node = = null ) return 0 ;
string val = node . Attributes [ attribute ] ? . InnerText ;
uint i ;
uint . TryParse ( val , out i ) ;
return i ;
}
2020-01-21 00:12:36 +08:00
public static ulong GetULongAttribute ( XmlNode node , string attribute )
{
if ( node = = null ) return 0 ;
string val = node . Attributes [ attribute ] ? . InnerText ;
ulong i ;
ulong . TryParse ( val , out i ) ;
return i ;
}
2017-09-21 18:33:05 +08:00
public static float GetFloatAttribute ( XmlNode node , string attribute )
{
if ( node = = null ) return 0 ;
string val = node . Attributes [ attribute ] ? . InnerText ;
float f ;
FloatUtil . TryParse ( val , out f ) ;
return f ;
}
public static string GetChildInnerText ( XmlNode node , string name )
{
if ( node = = null ) return null ;
return node . SelectSingleNode ( name ) ? . InnerText ;
}
2023-11-12 01:59:17 +08:00
public static string GetChildInnerText ( XElement node , string name ) {
if ( node = = null ) return null ;
return node . Element ( name ) . Value ;
}
public static string GetChildInnerText ( XmlReader reader , string name )
{
ValidateReaderState ( reader , name ) ;
if ( reader . NodeType = = XmlNodeType . Element )
{
if ( reader . IsEmptyElement )
{
reader . ReadStartElement ( ) ;
return "" ;
}
return reader . ReadElementContentAsString ( ) ;
}
else
{
return reader . ReadContentAsString ( ) ;
}
}
public static bool GetChildBoolInnerText ( XElement node , string name )
{
if ( node = = null ) return false ;
string val = node . Element ( name ) . Value ;
bool b ;
bool . TryParse ( val , out b ) ;
return b ;
}
public static bool GetChildBoolInnerText ( XmlReader reader , string name )
{
ValidateReaderState ( reader , name ) ;
if ( reader . NodeType = = XmlNodeType . Element )
{
return reader . ReadElementContentAsBoolean ( ) ;
}
else
{
return reader . ReadContentAsBoolean ( ) ;
}
}
2017-09-25 01:45:30 +08:00
public static bool GetChildBoolInnerText ( XmlNode node , string name )
{
if ( node = = null ) return false ;
string val = node . SelectSingleNode ( name ) ? . InnerText ;
bool b ;
bool . TryParse ( val , out b ) ;
return b ;
}
public static int GetChildIntInnerText ( XmlNode node , string name )
{
if ( node = = null ) return 0 ;
string val = node . SelectSingleNode ( name ) ? . InnerText ;
int i ;
int . TryParse ( val , out i ) ;
return i ;
}
public static float GetChildFloatInnerText ( XmlNode node , string name )
{
if ( node = = null ) return 0 ;
string val = node . SelectSingleNode ( name ) ? . InnerText ;
float f ;
FloatUtil . TryParse ( val , out f ) ;
return f ;
}
2023-11-12 01:59:17 +08:00
public static float GetChildFloatInnerText ( XmlReader reader , string name )
{
ValidateReaderState ( reader , name ) ;
return reader . ReadElementContentAsFloat ( ) ;
}
2019-01-11 15:15:25 +08:00
public static T GetChildEnumInnerText < T > ( XmlNode node , string name ) where T : struct
{
if ( node = = null ) return new T ( ) ;
string val = node . SelectSingleNode ( name ) ? . InnerText ;
return GetEnumValue < T > ( val ) ;
}
public static T GetEnumValue < T > ( string val ) where T : struct
{
2019-12-28 13:01:09 +08:00
if ( string . IsNullOrEmpty ( val ) )
{
return default ( T ) ;
}
2023-11-12 01:59:17 +08:00
if ( val . StartsWith ( "hash_" , StringComparison . OrdinalIgnoreCase ) )
2019-01-11 15:15:25 +08:00
{
//convert hash_12ABC to Unk_12345
var substr = val . Substring ( 5 ) ;
var uval = Convert . ToUInt32 ( substr , 16 ) ;
val = "Unk_" + uval . ToString ( ) ;
}
T enumval ;
Enum . TryParse ( val , out enumval ) ;
return enumval ;
}
2023-11-12 01:59:17 +08:00
public static bool GetChildBoolAttribute ( XmlReader reader , string name , string attribute = "value" )
{
ValidateReaderState ( reader , name ) ;
string val = reader . GetAttribute ( attribute ) ;
bool . TryParse ( val , out bool boolval ) ;
reader . ReadStartElement ( ) ;
2017-09-21 18:33:05 +08:00
2023-11-12 01:59:17 +08:00
return boolval ;
}
2020-01-21 23:45:27 +08:00
public static bool GetChildBoolAttribute ( XmlNode node , string name , string attribute = "value" )
2017-09-21 18:33:05 +08:00
{
if ( node = = null ) return false ;
string val = node . SelectSingleNode ( name ) ? . Attributes [ attribute ] ? . InnerText ;
bool b ;
bool . TryParse ( val , out b ) ;
return b ;
}
2023-11-12 01:59:17 +08:00
public static int GetChildIntAttribute ( XmlReader reader , string name , string attribute = "value" )
{
ValidateReaderState ( reader , name ) ;
string val = reader . GetAttribute ( attribute ) ;
int . TryParse ( val , out var i ) ;
reader . ReadStartElement ( ) ;
return i ;
}
2020-01-21 23:45:27 +08:00
public static int GetChildIntAttribute ( XmlNode node , string name , string attribute = "value" )
2017-09-21 18:33:05 +08:00
{
if ( node = = null ) return 0 ;
string val = node . SelectSingleNode ( name ) ? . Attributes [ attribute ] ? . InnerText ;
int i ;
int . TryParse ( val , out i ) ;
return i ;
}
2023-11-12 01:59:17 +08:00
public static uint GetChildUIntAttribute ( XmlReader reader , string name , string attribute = "value" )
{
if ( reader = = null ) return 0 ;
uint i ;
string val = reader . GetAttribute ( attribute ) ;
if ( val ? . StartsWith ( "0x" , StringComparison . OrdinalIgnoreCase ) ? ? false )
{
var subs = val . Substring ( 2 ) ;
i = Convert . ToUInt32 ( subs , 16 ) ;
}
else
{
uint . TryParse ( val , out i ) ;
}
return i ;
}
2020-01-21 23:45:27 +08:00
public static uint GetChildUIntAttribute ( XmlNode node , string name , string attribute = "value" )
2019-01-11 15:15:25 +08:00
{
if ( node = = null ) return 0 ;
string val = node . SelectSingleNode ( name ) ? . Attributes [ attribute ] ? . InnerText ;
uint i ;
2023-11-12 01:59:17 +08:00
if ( val ? . StartsWith ( "0x" , StringComparison . OrdinalIgnoreCase ) ? ? false )
2019-01-11 15:15:25 +08:00
{
var subs = val . Substring ( 2 ) ;
i = Convert . ToUInt32 ( subs , 16 ) ;
}
else
{
uint . TryParse ( val , out i ) ;
}
return i ;
}
2022-03-29 20:32:29 +08:00
public static ulong GetChildULongAttribute ( XmlNode node , string name , string attribute = "value" )
{
if ( node = = null ) return 0 ;
string val = node . SelectSingleNode ( name ) ? . Attributes [ attribute ] ? . InnerText ;
ulong i ;
2023-11-12 01:59:17 +08:00
if ( val ? . StartsWith ( "0x" , StringComparison . OrdinalIgnoreCase ) ? ? false )
2022-03-29 20:32:29 +08:00
{
var subs = val . Substring ( 2 ) ;
i = Convert . ToUInt64 ( subs , 16 ) ;
}
else
{
ulong . TryParse ( val , out i ) ;
}
return i ;
}
2023-11-12 01:59:17 +08:00
public static float GetChildFloatAttribute ( XmlReader reader , string name , string attribute = "value" )
{
if ( ! string . IsNullOrEmpty ( name ) )
{
ValidateReaderState ( reader , name ) ;
}
string val = reader . GetAttribute ( attribute ) ;
FloatUtil . TryParse ( val , out float f ) ;
if ( ! string . IsNullOrEmpty ( name ) )
{
reader . ReadStartElement ( ) ;
}
return f ;
}
2020-01-21 23:45:27 +08:00
public static float GetChildFloatAttribute ( XmlNode node , string name , string attribute = "value" )
2017-09-21 18:33:05 +08:00
{
if ( node = = null ) return 0 ;
string val = node . SelectSingleNode ( name ) ? . Attributes [ attribute ] ? . InnerText ;
float f ;
FloatUtil . TryParse ( val , out f ) ;
return f ;
}
2020-01-21 23:45:27 +08:00
public static string GetChildStringAttribute ( XmlNode node , string name , string attribute = "value" )
2017-09-21 18:33:05 +08:00
{
if ( node = = null ) return string . Empty ;
string val = node . SelectSingleNode ( name ) ? . Attributes [ attribute ] ? . InnerText ;
return val ;
}
2023-11-12 01:59:17 +08:00
public static string GetChildStringAttribute ( XmlReader reader , string name , string attribute = "value" )
{
ValidateReaderState ( reader , name ) ;
var val = reader . GetAttribute ( attribute ) ;
reader . ReadStartElement ( ) ;
return val ;
}
2020-01-21 23:45:27 +08:00
public static Vector2 GetChildVector2Attributes ( XmlNode node , string name , string x = "x" , string y = "y" )
2019-01-11 15:15:25 +08:00
{
float fx = GetChildFloatAttribute ( node , name , x ) ;
float fy = GetChildFloatAttribute ( node , name , y ) ;
return new Vector2 ( fx , fy ) ;
}
2023-11-12 01:59:17 +08:00
public static Vector3 GetChildVector3Attributes ( XmlReader reader , string name , string x = "x" , string y = "y" , string z = "z" )
{
ValidateReaderState ( reader , name ) ;
float fx = GetChildFloatAttribute ( reader , null , x ) ;
float fy = GetChildFloatAttribute ( reader , null , y ) ;
float fz = GetChildFloatAttribute ( reader , null , z ) ;
reader . ReadStartElement ( ) ;
return new Vector3 ( fx , fy , fz ) ;
}
2020-01-21 23:45:27 +08:00
public static Vector3 GetChildVector3Attributes ( XmlNode node , string name , string x = "x" , string y = "y" , string z = "z" )
2017-09-21 18:33:05 +08:00
{
float fx = GetChildFloatAttribute ( node , name , x ) ;
float fy = GetChildFloatAttribute ( node , name , y ) ;
float fz = GetChildFloatAttribute ( node , name , z ) ;
return new Vector3 ( fx , fy , fz ) ;
}
2020-01-21 23:45:27 +08:00
public static Vector4 GetChildVector4Attributes ( XmlNode node , string name , string x = "x" , string y = "y" , string z = "z" , string w = "w" )
2019-01-11 15:15:25 +08:00
{
float fx = GetChildFloatAttribute ( node , name , x ) ;
float fy = GetChildFloatAttribute ( node , name , y ) ;
float fz = GetChildFloatAttribute ( node , name , z ) ;
float fw = GetChildFloatAttribute ( node , name , w ) ;
return new Vector4 ( fx , fy , fz , fw ) ;
}
2017-09-21 18:33:05 +08:00
public static XmlElement GetChild ( XmlElement element , string name )
{
return element . SelectSingleNode ( name ) as XmlElement ;
}
public static XmlElement AddChild ( XmlDocument doc , XmlNode node , string name )
{
XmlElement child = doc . CreateElement ( name ) ;
node . AppendChild ( child ) ;
return child ;
}
2023-11-12 01:59:17 +08:00
public static bool IsItemElement ( this XmlReader reader )
{
if ( reader . MoveToContent ( ) = = XmlNodeType . Element & & reader . Name = = "Item" )
{
return true ;
}
return false ;
}
public static IEnumerable < XElement > IterateItems ( XmlReader reader , string parentElementName )
{
ValidateReaderState ( reader , parentElementName ) ;
reader . MoveToContent ( ) ;
if ( reader . IsEmptyElement )
{
// Move past empty element
reader . ReadStartElement ( parentElementName ) ;
yield break ;
}
reader . ReadStartElement ( parentElementName ) ;
while ( reader . IsItemElement ( ) )
{
if ( XNode . ReadFrom ( reader ) is XElement el )
{
yield return el ;
}
}
reader . ReadEndElement ( ) ;
}
2017-09-21 18:33:05 +08:00
public static XmlElement AddChildWithInnerText ( XmlDocument doc , XmlNode node , string name , string innerText )
{
XmlElement child = AddChild ( doc , node , name ) ;
child . InnerText = innerText ;
return child ;
}
public static XmlElement AddChildWithAttribute ( XmlDocument doc , XmlNode node , string name , string attributeName , string attributeValue )
{
XmlElement child = AddChild ( doc , node , name ) ;
child . SetAttribute ( attributeName , attributeValue ) ;
return child ;
}
2019-01-19 01:31:13 +08:00
2020-01-21 00:12:36 +08:00
public static byte [ ] GetRawByteArray ( XmlNode node , int fromBase = 16 )
2019-01-19 01:31:13 +08:00
{
if ( node = = null ) return new byte [ 0 ] ;
var data = new List < byte > ( ) ;
var split = Regex . Split ( node . InnerText , @"[\s\r\n\t]" ) ;
for ( int i = 0 ; i < split . Length ; i + + )
{
if ( ! string . IsNullOrEmpty ( split [ i ] ) )
{
var str = split [ i ] ;
if ( string . IsNullOrEmpty ( str ) ) continue ;
2020-01-21 00:12:36 +08:00
var val = Convert . ToByte ( str , fromBase ) ;
2019-01-19 01:31:13 +08:00
data . Add ( val ) ;
}
}
return data . ToArray ( ) ;
}
2020-01-21 00:12:36 +08:00
public static byte [ ] GetChildRawByteArray ( XmlNode node , string name , int fromBase = 16 )
2019-01-19 01:31:13 +08:00
{
var cnode = node . SelectSingleNode ( name ) ;
2020-01-21 00:12:36 +08:00
return GetRawByteArray ( cnode , fromBase ) ;
2019-01-19 01:31:13 +08:00
}
2020-01-23 23:55:48 +08:00
public static byte [ ] GetChildRawByteArrayNullable ( XmlNode node , string name , int fromBase = 16 )
{
var cnode = node . SelectSingleNode ( name ) ;
var arr = GetRawByteArray ( cnode , fromBase ) ;
return ( ( arr ! = null ) & & ( arr . Length > 0 ) ) ? arr : null ;
}
2019-01-19 01:31:13 +08:00
2020-01-19 23:08:04 +08:00
public static ushort [ ] GetRawUshortArray ( XmlNode node )
{
if ( node = = null ) return new ushort [ 0 ] ;
var data = new List < ushort > ( ) ;
var split = Regex . Split ( node . InnerText , @"[\s\r\n\t]" ) ;
for ( int i = 0 ; i < split . Length ; i + + )
{
if ( ! string . IsNullOrEmpty ( split [ i ] ) )
{
var str = split [ i ] ;
if ( string . IsNullOrEmpty ( str ) ) continue ;
var val = ( ushort ) 0 ;
ushort . TryParse ( str , out val ) ;
data . Add ( val ) ;
}
}
return data . ToArray ( ) ;
}
public static ushort [ ] GetChildRawUshortArray ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetRawUshortArray ( cnode ) ;
}
2020-01-23 23:55:48 +08:00
public static ushort [ ] GetChildRawUshortArrayNullable ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
var arr = GetRawUshortArray ( cnode ) ;
return ( ( arr ! = null ) & & ( arr . Length > 0 ) ) ? arr : null ;
}
2020-01-19 23:08:04 +08:00
2019-11-14 15:58:20 +08:00
public static uint [ ] GetRawUintArray ( XmlNode node )
{
if ( node = = null ) return new uint [ 0 ] ;
var data = new List < uint > ( ) ;
var split = Regex . Split ( node . InnerText , @"[\s\r\n\t]" ) ;
for ( int i = 0 ; i < split . Length ; i + + )
{
if ( ! string . IsNullOrEmpty ( split [ i ] ) )
{
var str = split [ i ] ;
if ( string . IsNullOrEmpty ( str ) ) continue ;
var val = 0 u ;
uint . TryParse ( str , out val ) ;
data . Add ( val ) ;
}
}
return data . ToArray ( ) ;
}
public static uint [ ] GetChildRawUintArray ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetRawUintArray ( cnode ) ;
}
2020-01-23 23:55:48 +08:00
public static uint [ ] GetChildRawUintArrayNullable ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
var arr = GetRawUintArray ( cnode ) ;
return ( ( arr ! = null ) & & ( arr . Length > 0 ) ) ? arr : null ;
}
2019-11-14 15:58:20 +08:00
2019-11-23 00:57:00 +08:00
public static int [ ] GetRawIntArray ( XmlNode node )
{
if ( node = = null ) return new int [ 0 ] ;
var data = new List < int > ( ) ;
var split = Regex . Split ( node . InnerText , @"[\s\r\n\t]" ) ;
for ( int i = 0 ; i < split . Length ; i + + )
{
if ( ! string . IsNullOrEmpty ( split [ i ] ) )
{
var str = split [ i ] ;
if ( string . IsNullOrEmpty ( str ) ) continue ;
var val = 0 ;
int . TryParse ( str , out val ) ;
data . Add ( val ) ;
}
}
return data . ToArray ( ) ;
}
public static int [ ] GetChildRawIntArray ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetRawIntArray ( cnode ) ;
}
2020-01-23 23:55:48 +08:00
public static int [ ] GetChildRawIntArrayNullable ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
var arr = GetRawIntArray ( cnode ) ;
return ( ( arr ! = null ) & & ( arr . Length > 0 ) ) ? arr : null ;
}
2019-11-23 00:57:00 +08:00
2019-03-11 22:13:04 +08:00
public static float [ ] GetRawFloatArray ( XmlNode node )
{
if ( node = = null ) return new float [ 0 ] ;
var items = new List < float > ( ) ;
2019-11-14 15:58:20 +08:00
var split = Regex . Split ( node . InnerText , @"[\s\r\n\t]" ) ; //node.InnerText.Split('\n');//
2019-03-11 22:13:04 +08:00
for ( int i = 0 ; i < split . Length ; i + + )
{
var s = split [ i ] ? . Trim ( ) ;
if ( string . IsNullOrEmpty ( s ) ) continue ;
var f = FloatUtil . Parse ( s ) ;
items . Add ( f ) ;
}
return items . ToArray ( ) ;
}
public static float [ ] GetChildRawFloatArray ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetRawFloatArray ( cnode ) ;
}
2020-01-23 23:55:48 +08:00
public static float [ ] GetChildRawFloatArrayNullable ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
var arr = GetRawFloatArray ( cnode ) ;
return ( ( arr ! = null ) & & ( arr . Length > 0 ) ) ? arr : null ;
}
2019-03-11 22:13:04 +08:00
2019-01-19 01:31:13 +08:00
public static Vector2 [ ] GetRawVector2Array ( XmlNode node )
{
if ( node = = null ) return new Vector2 [ 0 ] ;
float x = 0f ;
float y = 0f ;
var items = new List < Vector2 > ( ) ;
var split = node . InnerText . Split ( '\n' ) ; // Regex.Split(node.InnerText, @"[\s\r\n\t]");
for ( int i = 0 ; i < split . Length ; i + + )
{
var s = split [ i ] ? . Trim ( ) ;
if ( string . IsNullOrEmpty ( s ) ) continue ;
var split2 = s . Split ( ',' ) ; // Regex.Split(s, @"[\s\t]");
int c = 0 ;
x = 0f ; y = 0f ;
for ( int n = 0 ; n < split2 . Length ; n + + )
{
var ts = split2 [ n ] ? . Trim ( ) ;
if ( string . IsNullOrEmpty ( ts ) ) continue ;
var f = FloatUtil . Parse ( ts ) ;
switch ( c )
{
case 0 : x = f ; break ;
case 1 : y = f ; break ;
//case 2: z = f; break;
}
c + + ;
}
if ( c > = 2 )
{
var val = new Vector2 ( x , y ) ;
items . Add ( val ) ;
}
}
return items . ToArray ( ) ;
}
public static Vector2 [ ] GetChildRawVector2Array ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetRawVector2Array ( cnode ) ;
}
public static Vector3 [ ] GetRawVector3Array ( XmlNode node )
{
if ( node = = null ) return new Vector3 [ 0 ] ;
float x = 0f ;
float y = 0f ;
float z = 0f ;
var items = new List < Vector3 > ( ) ;
var split = node . InnerText . Split ( '\n' ) ; // Regex.Split(node.InnerText, @"[\s\r\n\t]");
for ( int i = 0 ; i < split . Length ; i + + )
{
var s = split [ i ] ? . Trim ( ) ;
if ( string . IsNullOrEmpty ( s ) ) continue ;
var split2 = s . Split ( ',' ) ; // Regex.Split(s, @"[\s\t]");
int c = 0 ;
x = 0f ; y = 0f ;
for ( int n = 0 ; n < split2 . Length ; n + + )
{
var ts = split2 [ n ] ? . Trim ( ) ;
if ( string . IsNullOrEmpty ( ts ) ) continue ;
var f = FloatUtil . Parse ( ts ) ;
switch ( c )
{
case 0 : x = f ; break ;
case 1 : y = f ; break ;
case 2 : z = f ; break ;
}
c + + ;
}
if ( c > = 3 )
{
var val = new Vector3 ( x , y , z ) ;
items . Add ( val ) ;
}
}
return items . ToArray ( ) ;
}
public static Vector3 [ ] GetChildRawVector3Array ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetRawVector3Array ( cnode ) ;
}
2020-01-16 15:03:24 +08:00
public static Vector3 [ ] GetChildRawVector3ArrayNullable ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
var arr = GetRawVector3Array ( cnode ) ;
return ( ( arr ! = null ) & & ( arr . Length > 0 ) ) ? arr : null ;
}
2019-01-19 01:31:13 +08:00
2019-01-20 13:54:42 +08:00
public static Vector4 [ ] GetRawVector4Array ( XmlNode node )
{
if ( node = = null ) return new Vector4 [ 0 ] ;
float x = 0f ;
float y = 0f ;
float z = 0f ;
float w = 0f ;
var items = new List < Vector4 > ( ) ;
var split = node . InnerText . Split ( '\n' ) ; // Regex.Split(node.InnerText, @"[\s\r\n\t]");
for ( int i = 0 ; i < split . Length ; i + + )
{
var s = split [ i ] ? . Trim ( ) ;
if ( string . IsNullOrEmpty ( s ) ) continue ;
var split2 = s . Split ( ',' ) ; // Regex.Split(s, @"[\s\t]");
int c = 0 ;
x = 0f ; y = 0f ;
for ( int n = 0 ; n < split2 . Length ; n + + )
{
var ts = split2 [ n ] ? . Trim ( ) ;
if ( string . IsNullOrEmpty ( ts ) ) continue ;
var f = FloatUtil . Parse ( ts ) ;
switch ( c )
{
case 0 : x = f ; break ;
case 1 : y = f ; break ;
case 2 : z = f ; break ;
case 3 : w = f ; break ;
}
c + + ;
}
if ( c > = 4 )
{
var val = new Vector4 ( x , y , z , w ) ;
items . Add ( val ) ;
}
}
return items . ToArray ( ) ;
}
public static Vector4 [ ] GetChildRawVector4Array ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetRawVector4Array ( cnode ) ;
}
2020-01-23 23:55:48 +08:00
public static Vector4 [ ] GetChildRawVector4ArrayNullable ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
var arr = GetRawVector4Array ( cnode ) ;
return ( ( arr ! = null ) & & ( arr . Length > 0 ) ) ? arr : null ;
}
2019-01-20 13:54:42 +08:00
2020-01-21 00:12:36 +08:00
public static Matrix GetMatrix ( XmlNode node )
{
if ( node = = null ) return Matrix . Identity ;
var arr = GetRawFloatArray ( node ) ;
if ( ( arr = = null ) | | ( arr . Length ! = 16 ) ) return Matrix . Identity ;
return new Matrix ( arr ) ;
}
public static Matrix GetChildMatrix ( XmlNode node , string name )
{
var cnode = node . SelectSingleNode ( name ) ;
return GetMatrix ( cnode ) ;
}
2019-01-19 01:31:13 +08:00
2017-09-21 18:33:05 +08:00
}
}