配置表解包(二)

public static void UnBuild()
{
    byte[] bytes = Qingame.Utils.FileUtil.ReadAllBytes(PersistentAssetPath.ConfigZipFile);
    string url = HotVersionURL.GetServerVersionUrl();
    string versionXml = File.ReadAllText(StreamingAssetPath.VersionXmlFile);
    XmlReader xr = XmlUtil.CreateXmlReader(versionXml);
    xr.Read();
    string clientVersion = xr.GetAttribute("version");
    int[] clientVersionArray = StringExtend.ToIntArray(clientVersion, CharConstants.Dot);
    int clientRepairVersion;
    int clientNoneRepairVersion;
    int.TryParse(xr.GetAttribute("rv"), out clientRepairVersion);
    int.TryParse(xr.GetAttribute("nrv"), out clientNoneRepairVersion);

    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "GET";
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    using (Stream stream = response.GetResponseStream())
    {
        using (BinaryReader reader = new BinaryReader(stream, Encoding.UTF8))
        {
            byte[] text = reader.ReadBytes((int)response.ContentLength);
            string json = ZipUtil.UnGZipBytesToUTFString(text);
            Hashtable table = JsonUtil.jsonDecode(json) as Hashtable;
            Hashtable rootNode = table["root"] as Hashtable;
            string serverVersion = Convert.ToString(rootNode["version"]);
            int serverRepairVersion;
            int serverNoneRepairVersion;
            int.TryParse(Convert.ToString(rootNode["rv"]), out serverRepairVersion);
            int.TryParse(Convert.ToString(rootNode["nrv"]), out serverNoneRepairVersion);
            int[] serverVersionArray = StringExtend.ToIntArray(serverVersion, CharConstants.Dot);
            string configVersion = Convert.ToString(rootNode["cv"]);
            string configTestVersion = Convert.ToString(rootNode["ctv"]);
            string configNewVersion = Convert.ToString(rootNode["ctv_new"]);
            bool isNewClient = VersionUtil.CompareForce(clientVersionArray, serverVersionArray);
            if (isNewClient == false)
            {
                isNewClient = clientRepairVersion > serverRepairVersion;
                if (isNewClient == false)
                {
                    isNewClient = clientNoneRepairVersion > serverNoneRepairVersion;
                }
            }
            //是否是测试版
            bool isClientTest = StringExtend.ToBool(xr.GetAttribute("debug")) ? true : isNewClient;
            string version = isNewClient ? configNewVersion : (isClientTest ? configTestVersion : configVersion);
            bytes = EncryptUtil.Decrypt(bytes, version);
        }
    }

    Dictionary files = ZipUtil.UnZipBytesToFiles(bytes);
    XmlDocument xmlDoc = new XmlDocument();
    foreach (var pair in files)
    {
        if (pair.Key.EndsWith(AssetSuffixName.BS))
        {
            string file = GameAssetPath.Config + "/" + pair.Key.Replace(AssetSuffixName.BS, AssetSuffixName.XML);
            string content = ConfigUtil.ByteStreamToXml(pair.Value, "bs", Path.GetFileNameWithoutExtension(pair.Key));
            content = content.Replace("&", "&");
            xmlDoc.LoadXml(content);

            using (XmlTextWriter writer = new XmlTextWriter(file, Encoding.UTF8))
            {
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;
                xmlDoc.WriteTo(writer);
            }
        }
        else if (pair.Key.EndsWith(AssetSuffixName.XML))
        {
            string file = GameAssetPath.Config + "/" + pair.Key;
            string content = Encoding.UTF8.GetString(pair.Value);
            content = content.Replace("&", "&");
            xmlDoc.LoadXml(content);
        
            using (XmlTextWriter writer = new XmlTextWriter(file, Encoding.UTF8))
            {
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;
                xmlDoc.WriteTo(writer);
            }
        }
        else
        {
            //c2s.txt, s2c.txt, *.*
            string file = GameAssetPath.Config + "/" + pair.Key;
            File.WriteAllBytes(file, pair.Value);
        }
    }

    AssetDatabase.SaveAssets();
    AssetDatabase.Refresh();
}

格式化XML文件,下面的例子可能不太恰当,但是能说明问题。sb能存储XML格式化之后的字符。

public static void FormatConfig()
{
    string[] paths = Directory.GetFiles(GameAssetPath.Config, "*.xml", SearchOption.AllDirectories);
    XmlDocument doc = new XmlDocument();
    foreach (string file in paths)
    {
        doc.Load(file);
        StringBuilder sb = new StringBuilder();
        using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb)))
        {
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;
            doc.WriteTo(writer);
        }

        using (XmlTextWriter writer = new XmlTextWriter(file, Encoding.UTF8))
        {
            writer.WriteRaw(sb.ToString());
        }
    }

    AssetDatabase.SaveAssets();
    AssetDatabase.Refresh();
}

只是存储的话可以下面这么写。

public static void FormatConfig()
{
    string[] paths = Directory.GetFiles(GameAssetPath.Config, "*.xml", SearchOption.AllDirectories);
    XmlDocument doc = new XmlDocument();
    foreach (string file in paths)
    {
        doc.Load(file);
        StringBuilder sb = new StringBuilder();
        using (XmlTextWriter writer = new XmlTextWriter(file, Encoding.UTF8))
        {
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;
            doc.WriteTo(writer);
        }

    AssetDatabase.SaveAssets();
    AssetDatabase.Refresh();
}

你可能感兴趣的:(配置表解包(二))