当前位置:网站首页>Luaframwrok handles resource updates
Luaframwrok handles resource updates
2022-07-03 07:57:00 【qq_ two billion three hundred and eighty-five million seven hun】
GlobalGenerator After initialization, give it to GameManager
Util class Get the storage directory of data
/// Get the data storage directory , Download resources to the local directory
public static string DataPath {
get
{
// Get the name of the game ,SimpleFramework
string game = AppConst.AppName.ToLower();
if (Application.isMobilePlatform) {
return Application.persistentDataPath + "/" + game + "/";
}
if (AppConst.DebugMode) {
return Application.dataPath + "/" + AppConst.AssetDirname + "/";
}
return "c:/" + game + "/";
}
}
GameManager class :
For the first time , Download resources from the server , then
public class GameManager : LuaBehaviour
{
// load lua The script
public LuaScriptMgr uluaMgr;
private List<string> downloadFiles = new List<string>();
/// <summary>
/// Initialize the game manager
/// </summary>
void Awake() {
Init();
}
/// <summary>
/// initialization
/// </summary>
void Init() {
DontDestroyOnLoad(gameObject); // Prevent destroying yourself
CheckExtractResource(); // Release resources
// The screen does not sleep
Screen.sleepTimeout = SleepTimeout.NeverSleep;
// Set frame rate
Application.targetFrameRate = AppConst.GameFrameRate;
}
/// <summary>
/// Release resources
/// </summary>
public void CheckExtractResource()
{
// Test directory , testing lua Folder , testing files.txt Basic configuration file
bool isExists = Directory.Exists(Util.DataPath) &&
Directory.Exists(Util.DataPath + "lua/") && File.Exists(Util.DataPath + "files.txt");
// There are these documents , Or internal testing
if (isExists || AppConst.DebugMode)
{
// Make a differential update
StartCoroutine(OnUpdateResource());
return; // The file has been unzipped , You can add check file list logic by yourself
}
// Enter the game for the first time
StartCoroutine(OnExtractResource());
}
IEnumerator OnExtractResource()
{
// Resource download storage directory
// Download the resources in the game package to the storage directory , Use this directory to compare with the resources on the server
//Application.persistentDataPath
string dataPath = Util.DataPath;
// Game pack resource directory , Application content path
string resPath = Util.AppContentPath();
if (Directory.Exists(dataPath)) Directory.Delete(dataPath, true);
Directory.CreateDirectory(dataPath);
string infile = resPath + "files.txt";
string outfile = dataPath + "files.txt";
if (File.Exists(outfile)) File.Delete(outfile);
string message = " Unpacking file :>files.txt";
Debug.Log(infile);
Debug.Log(outfile);
if (Application.platform == RuntimePlatform.Android)
{
WWW www = new WWW(infile);
yield return www;
if (www.isDone)
{
File.WriteAllBytes(outfile, www.bytes);
}
yield return 0;
}
else File.Copy(infile, outfile, true);
yield return new WaitForEndOfFrame();
// Release all files to the data directory
string[] files = File.ReadAllLines(outfile);
foreach (var file in files) {
string[] fs = file.Split('|');
infile = resPath + fs[0]; //
outfile = dataPath + fs[0];
message = " Unpacking file :>" + fs[0];
Debug.Log(" Unpacking file :>" + infile);
facade.SendNotification(NotiConst.UPDATE_MESSAGE, message);
string dir = Path.GetDirectoryName(outfile);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
if (Application.platform == RuntimePlatform.Android)
{
WWW www = new WWW(infile);
yield return www;
if (www.isDone)
{
File.WriteAllBytes(outfile, www.bytes);
}
yield return 0;
}
else
{
if (File.Exists(outfile)) {
File.Delete(outfile);
}
File.Copy(infile, outfile, true);
}
yield return new WaitForEndOfFrame();
}
message = " Unpacking complete !!!";
facade.SendNotification(NotiConst.UPDATE_MESSAGE, message);
yield return new WaitForSeconds(0.1f);
message = string.Empty;
// The release is complete , Start updating resources
StartCoroutine(OnUpdateResource());
}
// Start update download , Here is just a demonstration of ideas , Here you can start a thread to download updates
// Download... From the server fest Comparison with the client , Differentiated update
IEnumerator OnUpdateResource()
{
// Off by default , After opening, it will be compared with the server
if (!AppConst.UpdateMode)
{
// Initialize the game
OnResourceInited();
yield break;
}
string dataPath = Util.DataPath; // Data directory
string url = AppConst.WebUrl;
string message = string.Empty;
string random = DateTime.Now.ToString("yyyymmddhhmmss");
string listUrl = url + "files.txt?v=" + random;
Debug.LogWarning("LoadUpdate---->>>" + listUrl);
WWW www = new WWW(listUrl); yield return www;
if (www.error != null)
{
OnUpdateFailed(string.Empty);
yield break;
}
if (!Directory.Exists(dataPath))
{
Directory.CreateDirectory(dataPath);
}
File.WriteAllBytes(dataPath + "files.txt", www.bytes);
string filesText = www.text;
string[] files = filesText.Split('\n');
for (int i = 0; i < files.Length; i++) {
if (string.IsNullOrEmpty(files[i])) continue;
string[] keyValue = files[i].Split('|');
string f = keyValue[0];
string localfile = (dataPath + f).Trim();
string path = Path.GetDirectoryName(localfile);
if (!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
string fileUrl = url + f + "?v=" + random;
bool canUpdate = !File.Exists(localfile);
if (!canUpdate)
{
string remoteMd5 = keyValue[1].Trim();
string localMd5 = Util.md5file(localfile);
canUpdate = !remoteMd5.Equals(localMd5);
if (canUpdate) File.Delete(localfile);
}
if (canUpdate)
{ // Local missing file
Debug.Log(fileUrl);
message = "downloading>>" + fileUrl;
facade.SendNotification(NotiConst.UPDATE_MESSAGE, message);
BeginDownload(fileUrl, localfile);
while (!(IsDownOK(localfile))) { yield return new WaitForEndOfFrame(); }
}
}
yield return new WaitForEndOfFrame();
message = " Update complete !!";
facade.SendNotification(NotiConst.UPDATE_MESSAGE, message);
// Initialize the game
OnResourceInited();
}
void OnUpdateFailed(string file) {
string message = " Update failed !>" + file;
facade.SendNotification(NotiConst.UPDATE_MESSAGE, message);
}
/// <summary>
/// Whether the download is complete
/// </summary>
bool IsDownOK(string file) {
return downloadFiles.Contains(file);
}
/// <summary>
/// Thread Download
/// </summary>
void BeginDownload(string url, string file) { // Thread Download
object[] param = new object[2] { url, file };
ThreadEvent ev = new ThreadEvent();
ev.Key = NotiConst.UPDATE_DOWNLOAD;
ev.evParams.AddRange(param);
ThreadManager.AddEvent(ev, OnThreadCompleted); // Thread Download
}
/// <summary>
/// Thread complete
/// </summary>
/// <param name="data"></param>
void OnThreadCompleted(NotiData data)
{
switch (data.evName) {
case NotiConst.UPDATE_EXTRACT: // Unzip one to complete
//
break;
case NotiConst.UPDATE_DOWNLOAD: // Download a complete
downloadFiles.Add(data.evParam.ToString());
break;
}
}
/// <summary>
/// Initialize the game
/// </summary>
public void OnResourceInited()
{
LuaManager.Start();
// Load network
LuaManager.DoFile("Logic/Network");
// Load game
LuaManager.DoFile("Logic/GameManager");
initialize = true;
NetManager.OnInit(); // Initialize the network
object[] panels = CallMethod("LuaScriptPanel");
//---------------------Lua panel ---------------------------
foreach (object o in panels)
{
string name = o.ToString().Trim();
if (string.IsNullOrEmpty(name)) continue;
name += "Panel"; // add to
LuaManager.DoFile("View/" + name);
Debug.LogWarning("LoadLua---->>>>" + name + ".lua");
}
//------------------------------------------------------------
CallMethod("OnInitOK"); // Initialization complete
}
void Update() {
if (LuaManager != null && initialize) {
LuaManager.Update();
}
}
void LateUpdate() {
if (LuaManager != null && initialize) {
LuaManager.LateUpate();
}
}
void FixedUpdate() {
if (LuaManager != null && initialize) {
LuaManager.FixedUpdate();
}
}
/// <summary>
/// Destructor
/// </summary>
void OnDestroy()
{
if (NetManager != null) {
NetManager.Unload();
}
if (LuaManager != null) {
LuaManager.Destroy();
LuaManager = null;
}
Debug.Log("~GameManager was destroyed");
}
}
LuaManager.DoFile("Logic/Network");
LuaScriptMgr class
边栏推荐
- haproxy+keepalived搭建01
- vcs import src < ros2. Repos failed
- Demonstration of plug-in use of ventuz basic series
- Precautions for opensips and TLS SIP trunk docking
- Unity dotween sequence animation replay problem.
- Wechat applet taro learning record
- LwIP learning socket (application)
- Install cross compiler arm none liunx gnueabihf
- Go language foundation ------ 14 ------ gotest
- [end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
猜你喜欢
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
[cocos creator] Click the button to switch the interface
Technical dry goods Shengsi mindspire dynamic transformer with variable sequence length has been released!
多旅行商问题——公式和求解过程概述
Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once
Go language foundation ----- 16 ----- goroutine, GPM model
An article for you to understand - Manchester code
VMware virtual machine configuration static IP
【踩坑系列】mysql 修改root密码失败
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
随机推荐
华为S5700交换机初始化和配置SSH和TELNET远程登录方法
Client server model
go语言-循环语句
JS common basic case sorting (continuous update)
PHP common sorting algorithm
Huawei switches are configured with SSH login remote management switches
Redis批量启停脚本
Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once
PDO and SDO concepts
Redis查看客户端连接
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Professor Zhang Yang of the University of Michigan is employed as a visiting professor of Shanghai Jiaotong University, China (picture)
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
[untitled]
Quelle est la définition? Qu'est - ce qu'une déclaration? Quelle est la différence?
Screenshot tool snipaste
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
When did you find out that youth was over
MAE