配置表解包(一)

2020-03-16  本文已影响0人  泱千澈
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<string, byte[]> 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("&", "&amp;");
            StringReader Reader = new StringReader(content);
            xmlDoc.Load(Reader);
            xmlDoc.Save(file);
        }
        else if(pair.Key.EndsWith(AssetSuffixName.XML))
        {
            string file = GameAssetPath.Config + "/" + pair.Key;
            string content = Encoding.UTF8.GetString(pair.Value);
            content = content.Replace("&", "&amp;");
            StringReader Reader = new StringReader(content);
            xmlDoc.Load(Reader);
            xmlDoc.Save(file);
        }
        else
        {
            //c2s.txt, s2c.txt, *.*
            string file = GameAssetPath.Config + "/" + pair.Key;
            File.WriteAllBytes(file, pair.Value);
        }
    }

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

content = content.Replace("&", "&amp;"); 因为&写入到xml文件中需要用&amp;去替代&
参考网址 格式化XML:输出有缩进效果的XML字符串

上一篇下一篇

猜你喜欢

热点阅读