using Mono.Xml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Security;
using UnityEngine;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;
using CodeStage.AntiCheat.ObscuredTypes;
public class LoadDatas : SingleMono
{
static string encryptKey = “Oyea”; //定义密钥
///
/// xml读取
///
///
///
///
///
///
public static List LoadData(string xmlName, string tag)
{
FieldInfo[] fields = typeof(T).GetFields(); //获取类型字段
//SecurityParser SP = new SecurityParser();
//SP.LoadXml(Resources.Load(xmlName).ToString());
//ReadData
// SecurityElement SE = SP.ToXml();
SecurityElement SE = ReadData(xmlName.Replace("ConfigData/", ""));
List data = new List();
foreach (SecurityElement child in SE.Children)
{
T newData = Activator.CreateInstance();
if (child.Attributes != null && child.Tag == tag)
{
for (int j = 0; j < fields.Length; j++)
{
if (child.Attribute(fields[j].Name) != null)
{
if (fields[j].FieldType.IsArray)
fields[j].SetValue(newData, StringToArray(child.Attribute(fields[j].Name), fields[j].FieldType.GetElementType()));
else if (fields[j].FieldType.IsGenericType)
fields[j].SetValue(newData, StringToList(child.Attribute(fields[j].Name)));
else if (fields[j].FieldType == typeof(bool))
if (child.Attribute(fields[j].Name) == "true" || child.Attribute(fields[j].Name) == "false")
fields[j].SetValue(newData, bool.Parse(child.Attribute(fields[j].Name)));
else
fields[j].SetValue(newData, IntToBool(int.Parse(child.Attribute(fields[j].Name))));
else if (fields[j].FieldType == typeof(ObscuredFloat))
{
ObscuredFloat temp = (float)Convert.ChangeType(child.Attribute(fields[j].Name), typeof(float));
fields[j].SetValue(newData, temp);
}
else if (fields[j].FieldType == typeof(ObscuredInt))
{
ObscuredInt temp = (int)Convert.ChangeType(child.Attribute(fields[j].Name), typeof(int));
fields[j].SetValue(newData, temp);
}
else
{
//if (newData.GetType()==typeof(EquipmentData))
//{
// Debug.Log(fields[j].Name+""+fields[j].FieldType);
//}
fields[j].SetValue(newData, Convert.ChangeType(child.Attribute(fields[j].Name), fields[j].FieldType));
}
}
else
{
//测试使用,预留
//MDebug.Log(fields[j].Name);
}
}
data.Add(newData);
}
else
{
MDebug.Log("Null -- " + child.Tag);
}
}
return data;
}
public void YieldLoadData(string xmlName, string tag, List data)
{
//获取类型字段
//SecurityParser SP = new SecurityParser();
//SP.LoadXml(Resources.Load(xmlName).ToString());
//ReadData
// SecurityElement SE = SP.ToXml();
YieldReadData(xmlName.Replace("ConfigData/", ""), tag, data);
// List data = new List();
//return data;
}
#region 解密字符串
///
/// 解密字符串
///
/// 要解密的字符串
/// 解密后的字符串
public static string Decrypt(string str)
{
DESCryptoServiceProvider descsp = new DESCryptoServiceProvider(); //实例化加/解密类对象
byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定义字节数组,用来存储密钥
byte[] data = Convert.FromBase64String(str);//定义字节数组,用来存储要解密的字符串
MemoryStream MStream = new MemoryStream(); //实例化内存流对象
//使用内存流实例化解密流对象
CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
CStream.Write(data, 0, data.Length); //向解密流中写入数据
CStream.FlushFinalBlock(); //释放解密流
return Encoding.Unicode.GetString(MStream.ToArray()); //返回解密后的字符串
}
#endregion
///
/// 解密bin文件
///
public static SecurityElement ReadData(string xmlName)
{
//star
//try
//{
//}
//catch (System.IO.IOException e)
//{
// MDebug.Log(e);
//}
#if UNITY_ANDROID&&!UNITY_EDITOR
string path = Application.streamingAssetsPath + “/” + xmlName + “.bin”;
//Debug.Log(path);
#elif UNITY_IPHONE
string path = Application.streamingAssetsPath + “/” + xmlName + “.bin”;
#elif UNITY_EDITOR
string path = Application.dataPath + “/StreamingAssets” + “/” + xmlName + “.bin”;
#endif
//string path = Application.dataPath + “/StreamingAssets” + “/” + xmlName + “.bin”;
// string path= Resources.Load(xmlName)
//Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
//BinaryFormatter formatter = new BinaryFormatter();
//string test = Decrypt(formatter.Deserialize(stream).ToString());
////Debug.Log(test);
//SecurityElement testxml = SecurityElement.FromString(test);
//stream.Close();
//return testxml;
// streamxml.Close();
return Downloader(xmlName);
}
public static string ReadBinData(string xmlName)
{
#if UNITY_ANDROID&&!UNITY_EDITOR
string path = Application.streamingAssetsPath + “/” + xmlName + “.bin”;
//Debug.Log(path);
#elif UNITY_IPHONE
string path = Application.streamingAssetsPath + “/” + xmlName + “.bin”;
#elif UNITY_EDITOR
string path = Application.dataPath + “/StreamingAssets” + “/” + xmlName + “.bin”;
#endif
return DownloadBin(xmlName);
}
public void YieldReadData(string xmlName, string tag, List data)
{
//star
//try
//{
//}
//catch (System.IO.IOException e)
//{
// MDebug.Log(e);
//}
#if UNITY_ANDROID&&!UNITY_EDITOR
string path = Application.streamingAssetsPath + “/” + xmlName + “.bin”;
Debug.Log(path);
#elif UNITY_IPHONE
string path = Application.streamingAssetsPath + “/” + xmlName + “.bin”;
#elif UNITY_EDITOR
// string path = Application.dataPath + “/StreamingAssets” + “/” + xmlName + “.bin”;
#endif
//string path = Application.dataPath + “/StreamingAssets” + “/” + xmlName + “.bin”;
// string path= Resources.Load(xmlName)
//Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
//BinaryFormatter formatter = new BinaryFormatter();
//string test = Decrypt(formatter.Deserialize(stream).ToString());
////Debug.Log(test);
//SecurityElement testxml = SecurityElement.FromString(test);
//stream.Close();
//return testxml;
// streamxml.Close();
StartCoroutine(YieldDownloader(xmlName, tag, data));
}
///
/// xml读取
///
///
///
///
///
///
///
static WWW www;
static string DownloadBin(string path)
{
#if UNITY_EDITOR // For running in Unity
www = new WWW(“file://” + Application.streamingAssetsPath + “/” + path + “.bin”);
#elif UNITY_ANDROID
www = new WWW (“jar:file://” + Application.dataPath + “!/assets/”+path + “.bin”);
#endif
#if UNITY_IOS && !UNITY_EDITOR
string iospath=Application.dataPath +"/Raw/"+path + “.bin”;
FileStream fs = new FileStream(iospath, FileMode.Open,FileAccess.Read);
//获取文件大小
long size = fs.Length;
byte[] array = new byte[size];
//将文件读到byte数组中
fs.Read(array, 0, array.Length);
File.WriteAllBytes(Application.persistentDataPath + "/" + path + ".bin", array);
Stream stream = new FileStream(Application.persistentDataPath + "/" + path + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter formatter = new BinaryFormatter();
string test = Decrypt(formatter.Deserialize(stream).ToString());
// Debug.Log("jiemi" + test);
//SecurityElement testxml = SecurityElement.FromString(test);
//// Debug.Log(testxml.ToString());
stream.Close();
// www.
return test;
#else
while (!www.isDone)
{
}
// yield return www; //will wait until the download finishes
// if (www.isDone == true)
// {
// FileStream
//Debug.Log(path + "" + );
/******原先的读取*********/
// File.WriteAllBytes(Application.persistentDataPath + "/" + path + ".bin", www.bytes);
// Stream stream = new FileStream(Application.persistentDataPath + "/" + path + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read);
Stream stream = new MemoryStream(www.bytes);
BinaryFormatter formatter = new BinaryFormatter();
string test = Decrypt(formatter.Deserialize(stream).ToString());
//Debug.Log("jiemi" + test);
//SecurityElement testxml = SecurityElement.FromString(test);
// Debug.Log(testxml.ToString());
stream.Close();
//File.Delete(Application.persistentDataPath + "/" + path + ".bin");
// www.
return test;
#endif
}
static SecurityElement Downloader(string path)
{
#if UNITY_EDITOR // For running in Unity
www = new WWW(“file://” + Application.streamingAssetsPath + “/” + path + “.bin”);
#elif UNITY_ANDROID
www = new WWW (“jar:file://” + Application.dataPath + “!/assets/”+path + “.bin”);
#endif
#if UNITY_IOS && !UNITY_EDITOR
string iospath=Application.dataPath +"/Raw/"+path + “.bin”;
FileStream fs = new FileStream(iospath, FileMode.Open,FileAccess.Read);
//获取文件大小
long size = fs.Length;
byte[] array = new byte[size];
//将文件读到byte数组中
fs.Read(array, 0, array.Length);
File.WriteAllBytes(Application.persistentDataPath + "/" + path + ".bin", array);
Stream stream = new FileStream(Application.persistentDataPath + "/" + path + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter formatter = new BinaryFormatter();
string test = Decrypt(formatter.Deserialize(stream).ToString());
// Debug.Log("jiemi" + test);
SecurityElement testxml = SecurityElement.FromString(test);
// Debug.Log(testxml.ToString());
stream.Close();
// www.
return testxml;
#else
while (!www.isDone)
{
}
// yield return www; //will wait until the download finishes
// if (www.isDone == true)
// {
// FileStream
//Debug.Log(path + "" + );
/******原先的读取*********/
// File.WriteAllBytes(Application.persistentDataPath + "/" + path + ".bin", www.bytes);
// Stream stream = new FileStream(Application.persistentDataPath + "/" + path + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read);
Stream stream = new MemoryStream(www.bytes);
BinaryFormatter formatter = new BinaryFormatter();
string test = Decrypt(formatter.Deserialize(stream).ToString());
//Debug.Log("jiemi" + test);
SecurityElement testxml = SecurityElement.FromString(test);
// Debug.Log(testxml.ToString());
stream.Close();
//File.Delete(Application.persistentDataPath + "/" + path + ".bin");
// www.
return testxml;
#endif
}
static IEnumerator YieldDownloader(string path, string tag, List data)
{
WWW www;
Debug.Log(path);
#if UNITY_ANDROID && !UNITY_EDITOR//For running in Android
www = new WWW (“jar:file://” + Application.dataPath + “!/assets/”+path + “.bin”);
#elif UNITY_IOS && !UNITY_EDITOR
www = new WWW (Application.dataPath +"/Raw/"+path + “.bin”);
#endif
#if UNITY_EDITOR // For running in Unity
www = new WWW(“file://” + Application.streamingAssetsPath + “/” + path + “.bin”);
#endif
//while (!www.isDone)
//{
//}
yield return www; //will wait until the download finishes
// if (www.isDone == true)
// {
File.WriteAllBytes(Application.persistentDataPath + "/" + path + ".bin", www.bytes);
Stream stream = new FileStream(Application.persistentDataPath + "/" + path + ".bin", FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter formatter = new BinaryFormatter();
string test = Decrypt(formatter.Deserialize(stream).ToString());
// Debug.Log("jiemi" + test);
SecurityElement testxml = SecurityElement.FromString(test);
// Debug.Log(testxml.ToString()+"testxml");
FieldInfo[] fields = typeof(T).GetFields();
foreach (SecurityElement child in testxml.Children)
{
T newData = Activator.CreateInstance();
if (child.Attributes != null && child.Tag == tag)
{
for (int j = 0; j < fields.Length; j++)
{
if (child.Attribute(fields[j].Name) != null)
{
if (fields[j].FieldType.IsArray)
fields[j].SetValue(newData, StringToArray(child.Attribute(fields[j].Name), typeof(T)));
else if (fields[j].FieldType.IsGenericType)
fields[j].SetValue(newData, StringToList(child.Attribute(fields[j].Name)));
else if (fields[j].FieldType == typeof(bool))
if (child.Attribute(fields[j].Name) == "true" || child.Attribute(fields[j].Name) == "false")
fields[j].SetValue(newData, bool.Parse(child.Attribute(fields[j].Name)));
else
fields[j].SetValue(newData, IntToBool(int.Parse(child.Attribute(fields[j].Name))));
else
{
fields[j].SetValue(newData, Convert.ChangeType(child.Attribute(fields[j].Name), fields[j].FieldType));
}
}
else
{
//测试使用,预留
//MDebug.Log(fields[j].Name);
}
}
data.Add(newData);
}
else
{
Debug.Log("Null -- " + child.Tag);
}
Debug.Log(data.Count);
}
// Debug.Log(testxml.ToString());
stream.Close();
FileInfo i = new FileInfo(Application.persistentDataPath + "/" + path + ".bin");
i.Delete();
// File.Delete(Application.persistentDataPath + "/" + path + ".bin");
// www.
// return testxml;
// }
}
public static Dictionary LoadDataToDic(string xmlName, string tag)
{
FieldInfo[] fields = typeof(T).GetFields(); //获取类型字段
//SecurityParser SP = new SecurityParser();
//SP.LoadXml(Resources.Load(xmlName).ToString());
//SecurityElement SE = SP.ToXml();
SecurityElement SE = ReadData(xmlName.Replace("ConfigData/", ""));
// List data = new List();
// Debug.Log(SE.ToString());
Dictionary data = new Dictionary();
int key = 0;
foreach (SecurityElement child in SE.Children)
{
T newData = Activator.CreateInstance();
if (child.Tag == tag)
{
if (fields[0].Name == "ID")
{
key = int.Parse(child.Attribute(fields[0].Name));
}
else if (fields[0].Name == "RoleID") //英雄表
{
key = int.Parse(child.Attribute(fields[0].Name));
}
else if (fields[0].Name == "MapID") //英雄表
{
key = int.Parse(child.Attribute(fields[0].Name));
}
else if (fields[0].Name == "GiftId") //英雄表
{
key = int.Parse(child.Attribute(fields[0].Name));
}
else if (fields[0].Name == "ItemID") //英雄表
{
key = int.Parse(child.Attribute(fields[0].Name));
}
else if (fields[0].Name == "MapId") //英雄表
{
key = int.Parse(child.Attribute(fields[0].Name));
}
else if (fields[0].Name == "Id") //英雄表
{
key = int.Parse(child.Attribute(fields[0].Name));
}
else
{
key++;
}
for (int j = 0; j < fields.Length; j++)
{
if (child.Attribute(fields[j].Name) != null)
{
if (fields[j].FieldType.IsArray)
fields[j].SetValue(newData, StringToArray(child.Attribute(fields[j].Name), typeof(T)));
else if (fields[j].FieldType.IsGenericType)
fields[j].SetValue(newData, StringToList(child.Attribute(fields[j].Name)));
else if (fields[j].FieldType == typeof(bool))
if (child.Attribute(fields[j].Name) == "true" || child.Attribute(fields[j].Name) == "false")
fields[j].SetValue(newData, bool.Parse(child.Attribute(fields[j].Name)));
else
fields[j].SetValue(newData, IntToBool(int.Parse(child.Attribute(fields[j].Name))));
else if (fields[j].FieldType == typeof(ObscuredFloat))
{
ObscuredFloat temp = (float)Convert.ChangeType(child.Attribute(fields[j].Name), typeof(float));
fields[j].SetValue(newData, temp);
}
else if (fields[j].FieldType == typeof(ObscuredInt))
{
ObscuredInt temp = (int)Convert.ChangeType(child.Attribute(fields[j].Name), typeof(int));
fields[j].SetValue(newData, temp);
}
else
{
fields[j].SetValue(newData, Convert.ChangeType(child.Attribute(fields[j].Name), fields[j].FieldType));
}
}
}
data[key] = newData;
}
}
return data;
}
///
/// 读取xml,转换成dictionary,格式为dictionary
///
///
///
///
///
///
public static Dictionary LoadSingleData(string xml, string tag, string str1, string str2)
{
// Debug.Log(xml);
//SecurityParser SP = new SecurityParser();
//SP.LoadXml(Resources.Load(xml).ToString());
//SecurityElement SE = SP.ToXml();
SecurityElement SE = ReadData(xml.Replace("ConfigData/", ""));
// List data = new List();
Dictionary data = new Dictionary();
foreach (SecurityElement child in SE.Children)
if (child.Tag == tag)
data.Add(int.Parse(child.Attribute(str1)), int.Parse(child.Attribute(str2)));
return data;
}
//public static Dictionary ListToDic(List list,string str)
//{
// Dictionary dic = new Dictionary();
// FieldInfo[] fields = typeof(T).GetFields();
// for (int i = 0; i < list.Count;++i)
// {
// for (int j = 0; j < fields.Length; ++j)
// {
// if (fields[j].Name == str)
// {
// }
// }
// }
// return dic;
//}
///
/// 字符串转int数组
///
///
///
static object StringToArray(string str,Type type = null)
{
if (str.Contains(".") || ((type != null)&&(type == typeof( float))))
{
string[] s = str.Split(',');
float[] newArray = new float[s.Length];
//MDebug.Log("判断数组长度是:::::",newArray.Length);
for (int i = 0; i < s.Length; ++i)
{
//Debug.Log(s[i]);
newArray[i] = float.Parse(s[i]);
}
return newArray;
}
else
{
string[] s = str.Split(',');
int[] newArray = new int[s.Length];
//MDebug.Log("判断数组长度是:::::",newArray.Length);
for (int i = 0; i < s.Length; ++i)
{
//Debug.Log("这个数组是"+s[i]);
newArray[i] = int.Parse(s[i]);
}
return newArray;
}
}
///
/// 字符串转List
///
///
///
static List StringToList(string str)
{
string[] s = str.Split(',');
List newList = new List();
for (int i = 0; i < s.Length; ++i)
{
try
{
newList.Add(int.Parse(s[i]));
}
catch
{
}
}
return newList;
}
///
/// int转bool
///
///
///
static bool IntToBool(int i)
{
if (i == 0)
return false;
else
return true;
}
}