当前位置:网站首页>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
边栏推荐
- Worldview satellite remote sensing image data / meter resolution remote sensing image
- Demonstration of plug-in use of ventuz basic series
- [cocos creator] Click the button to switch the interface
- 华为交换机:配置telnet和ssh、web访问
- 华为S5700交换机初始化和配置telnet,ssh用户方法
- [MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
- 【cocos creator】点击按钮切换界面
- 【LeetCode】2. Valid parentheses · valid parentheses
- Unity XR realizes interaction (grasping, moving, rotating, transmitting, shooting) -pico
- JS to implement publish and subscribe
猜你喜欢

STM32F103 SPI (pit Diary)

Unity XR实现交互(抓取,移动旋转,传送,射击)-Pico

Oracle queries grouped by time

Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“

在浏览器输入url后执行什么

Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once

An intern's journey to cnosdb

Go language foundation ------ 14 ------ gotest

C language learning notes (mind map)

haproxy+keepalived搭建01
随机推荐
什么是定义?什么是声明?它们有何区别?
Redis profile
【cocos creator】点击按钮切换界面
oracle中的 (+)是什么意思
P2622 关灯问题II(状态压缩 搜索)
Research shows that breast cancer cells are more likely to enter the blood when patients sleep
RM delete file
Technical dry goods | Bert model for the migration of mindspore NLP model - text matching task (2): training and evaluation
LwIP learning socket (application)
Huawei switches are configured with SSH login remote management switches
oracle 插入单引号
PHP常用排序算法
JS common basic case sorting (continuous update)
什么是数据类型?数据类型有什么用?
C2-关于VCF文件合并的几种方法
Viz artist advanced script video tutorial -- stringmap use and vertex operation
IP production stream is so close to me
s7700设备如何清除console密码
华为S5700交换机初始化和配置telnet,ssh用户方法
Screenshot tool snipaste