首页 技术 正文
技术 2022年11月15日
0 收藏 318 点赞 4,985 浏览 3540 个字

仿LOL项目开发第二天

                                      by草帽

昨个我们已经实现了下载功能,但是发现没有,下载的包是压缩的,没有解压开,那么Unity是识别不了的。

所以今个我们来讲讲如何实现解压文件。

还记得吗,我们在DownloadTask里面添加了一个完成下载后的一个解压委托,我们还没有实现,那么,我们就去解决他。

回到VersionManager的DownloadPackageList方法里面,在OnDownloadFinished委托里面,添加解压缩的代码。

之前讲过类的单一职责,所以不可能在VersionManager里面完成解压缩功能吧。

所以呢,我们新建一个文件管理类:FileAccessManager.cs:

using UnityEngine;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using ICSharpCode.SharpZipLib.Zip;
/// <summary>
/// 文件管理器
/// </summary>
public static class FileAccessManager
{
/// <summary>
/// 解压文件到资源文件目录
/// </summary>
/// <param name="filePath"></param>
public static void DecompressFile(string filePath)
{
DecompressToDirectory(SystemConfig.ResourceFolder, filePath);
}
/// <summary>
/// 解压文件到指定文件路径
/// </summary>
/// <param name="targetPath"></param>
/// <param name="zipFilePath"></param>
public static void DecompressToDirectory(string targetPath,string zipFilePath)
{
if (File.Exists(zipFilePath))
{
Stream compressed = File.OpenRead(zipFilePath);
compressed.DecompressToDirectory(targetPath);
}
else
{
Debug.LogError("解压文件不存在");
}
}
private static void DecompressToDirectory(this Stream source, string targetPath)
{
targetPath = Path.GetFullPath(targetPath);
using (ZipInputStream decompressor = new ZipInputStream(source))
{
ZipEntry entry; while ((entry = decompressor.GetNextEntry()) != null)
{
string name = entry.Name;
if (entry.IsDirectory && entry.Name.StartsWith("\\"))
name = entry.Name.ReplaceFirst("\\", "");
//name = ReplaceFirst(entry.Name, "\\", "");
string filePath = Path.Combine(targetPath, name);
string directoryPath = Path.GetDirectoryName(filePath); if (!string.IsNullOrEmpty(directoryPath) && !Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath); if (entry.IsDirectory)
continue; byte[] data = new byte[2048];
using (FileStream streamWriter = File.Create(filePath))
{
int bytesRead;
while ((bytesRead = decompressor.Read(data, 0, data.Length)) > 0)
{
streamWriter.Write(data, 0, bytesRead);
}
}
}
}
}
public static string ReplaceFirst(this string source, string oldString, string newString)
{
Regex regEx = new Regex(oldString, RegexOptions.Multiline);
return regEx.Replace(source, newString == null ? "" : newString, 1);
}
}

不懂解压缩文件的童鞋可以看我另一篇博客:通用工具类解压缩文件篇 

这里我就不详细介绍了,我们继续回到VersionManager的DownloadPackageList:

Action OnDownloadFinished =()=>
{
//进行解压,以后再来
if (File.Exists(localFile))
{
//开始解压文件
FileAccessManager.DecompressFile(localFile);
}
if (File.Exists(localFile))
{
File.Delete(localFile);
}
//更新本地版本信息
LocalVersion.ResourceVersionCodeInfo = new VersionCodeInfo(kvp.Key);
//保存版本信息
SaveVersion(LocalVersion);
};

SaveVersion():更新本地版本信息xml

    /// <summary>
/// 保存版本信息到xml文件中
/// </summary>
/// <param name="version"></param>
private void SaveVersion(VersionManagerInfo version)
{
var props = typeof(VersionManagerInfo).GetProperties();
XmlDocument doc = new XmlDocument();
XmlDeclaration newChild = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
doc.AppendChild(newChild);
XmlElement root = doc.CreateElement("root");
doc.AppendChild(root);
foreach (var prop in props)
{
XmlElement e = doc.CreateElement(prop.Name);
string value = prop.GetGetMethod().Invoke(version,null) as string;
e.InnerText = value;
root.AppendChild(e);
}
UnityTools.SaveText(SystemConfig.VersionPath, doc.InnerXml);
}

Ok,我们来运行一下程序,哎,发现有错误,和之前一样的错误。

仿LOL项目开发第三天

也就是说获取Application.dataPath不能在其他线程获取,我们看看那些弄了线程。

找到了,也就是在DownloadMrg的CheckDownloadList的时候new 一个下载线程:

仿LOL项目开发第三天

所以解决方法还是,去DownloadTask类里面,找到OnFinished方法,修改下:

    public void OnFinished()
{
if (Finished != null)
LOLGameDriver.Invoke(Finished);
//Finished();
}

然后回到DownloadFinishedWithMd5()里面,找到task.Finished()改成task.OnFinished();

再次运行程序:

仿LOL项目开发第三天

可以看到原先的压缩文件已经自动解压开了(看不到的童鞋请刷新下Project的资源),因为我们已经修改了版本信息,所以再次运行看看:

仿LOL项目开发第三天

可以看到不需要更新了,说明版本已经迭代了。Ok,基本上版本更新已经搞一个段落,现在只差和界面交互,所以下一节,我们专门来讲讲界面UI的框架。

仿LOL项目开发第四天链接地址

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,401
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896