#if !COMBAT_SERVER
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using LitJson;
using UnityEngine;
namespace Utility
{
///
/// 用来做JsonUtility.FromJson的容器类
///
[Serializable]
public class AllPackedAssetsInfo
{
public List assetsList;
// public List bundlsList;
}
[Serializable]
public class AssetInfo
{
public string name;
public string bundle;
public string localPath;
public bool isConfig;
public LitJson.JsonData ToJson()
{
JsonData jsonData = new JsonData();
jsonData["name"] = name;
jsonData["bundle"] = bundle;
jsonData["isConfig"] = isConfig;
if (!string.IsNullOrEmpty(localPath))
{
jsonData["localPath"] = localPath;
}
return jsonData;
}
}
[Serializable]
public struct BundleInfo
{
// public string name;
// public string MD5;
// public long size;//byte
//
// public JsonData ToJson()
// {
// JsonData jsonData = new JsonData();
// jsonData["name"] = name;
// jsonData["MD5"] = MD5;
// jsonData["size"] = size;
// return jsonData;
// }
}
public class AssetsPacker
{
private static char[] splits = new char[] { '/', '.' };
public static string SpliteName(string name)
{
string[] splitName = name.Split(splits);
if (splitName.Length > 2)
{
return splitName[splitName.Length - 2];
}
else
{
return "";
}
}
// public static BundleInfo CreateBundleInfo(string name, List dependence, string MD5Value)
// {
// BundleInfo bundle = new BundleInfo();
// bundle.name = SpliteName(name);
//
// if (dependence != null)
// {
// dependence.ForEach(str => SpliteName(str));
// }
//
// bundle.MD5 = MD5Value;
// return bundle;
// }
#region Write Files
/*
private static XmlAttribute addXmlAttribute(XmlDocument doc, string name, string val)
{
XmlAttribute attr = doc.CreateAttribute(name);
attr.Value = val;
return attr;
}
private static XmlNode CreateNode(AssetInfo asset, XmlDocument doc)
{
XmlNode node = doc.CreateElement("AssetConfig");
node.Attributes.Append(addXmlAttribute(doc, "AssetName", asset.name));
node.Attributes.Append(addXmlAttribute(doc, "BundleName", asset.bundle));
if (!string.IsNullOrEmpty(asset.localPath))
{
node.Attributes.Append(addXmlAttribute(doc, "AssetPath", asset.localPath));
}
return node;
}
private static XmlNode CreateNode(BundleInfo bundle, XmlDocument doc)
{
XmlNode node = doc.CreateElement("BundleConfig");
node.Attributes.Append(addXmlAttribute(doc, "BundleName", bundle.name));
node.Attributes.Append(addXmlAttribute(doc, "MD5", bundle.MD5));
node.Attributes.Append(addXmlAttribute(doc, "Size", bundle.size.SplitToString()));
// if (bundle.depedenceBundles != null)
// {
// node.Attributes.Append(addXmlAttribute(doc, "DependenceCount", bundle.depedenceBundles.Count.SplitToString()));
// for (int i = 0; i < bundle.depedenceBundles.Count; i++)
// {
// node.Attributes.Append(addXmlAttribute(doc, "DependenceBundle" + i, bundle.depedenceBundles [i]));
// }
// }
return node;
}
[ExecuteInEditMode]
public static void WriteInfoToXMLFile(Dictionary assetList, Dictionary bundleList, string savePath)
{
if (null == assetList ||
null == bundleList)
{
return;
}
XmlDocument doc = new XmlDocument();
XmlDeclaration desc = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(desc);
XmlElement root = doc.CreateElement("PackConfig");
doc.AppendChild(root);
XmlElement assetsRoot = doc.CreateElement("Assets");
root.AppendChild(assetsRoot);
foreach (var kvp in assetList)
{
XmlNode node = CreateNode(kvp.Value, doc);
assetsRoot.AppendChild(node);
}
XmlElement bundlesRoot = doc.CreateElement("Bundles");
root.AppendChild(bundlesRoot);
foreach (var kvp in bundleList)
{
XmlNode node = CreateNode(kvp.Value, doc);
bundlesRoot.AppendChild(node);
}
if (File.Exists(savePath))
{
File.Delete(savePath);
}
doc.Save(savePath);
}
*/
[ExecuteInEditMode]
public static AllPackedAssetsInfo WriteInfoToJsonFile(Dictionary assetDict,
string savePath)
{
AllPackedAssetsInfo allPackedInfo = new AllPackedAssetsInfo();
allPackedInfo.assetsList = new List();
allPackedInfo.assetsList.AddRange(assetDict.Values);
// allPackedInfo.bundlsList = new List();
// allPackedInfo.bundlsList.AddRange(bundleDict.Values);
string content = JsonUtility.ToJson(allPackedInfo);
if (File.Exists(savePath))
{
File.Delete(savePath);
}
Encoding encoding = new UTF8Encoding(false);
File.WriteAllText(savePath, content, encoding);
return allPackedInfo;
// JsonData jsonRoot = new JsonData();
// JsonData assetsRoot = new JsonData();
// JsonData bundleRoot = new JsonData();
// jsonRoot["Assets"] = assetsRoot;
// jsonRoot["Bundles"] = bundleRoot;
//
// foreach (var kvp in assetDict)
// {
// assetsRoot[kvp.Value.name] = kvp.Value.ToJson();
// }
//
// foreach (var kvp in bundleDict)
// {
// bundleRoot[kvp.Value.name] = kvp.Value.ToJson();
// }
//
// if (File.Exists(savePath))
// {
// File.Delete(savePath);
// }
// Encoding encoding = new UTF8Encoding(false);
// File.WriteAllText(savePath, jsonRoot.ToJson(), encoding);
}
#endregion
}//AssetsPackInfo
}
#endif