海淀天下城电子沙盘单机版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

196 lines
7.2 KiB

//using AX.Network.Protocols;
//using AX.NetworkSystem;
//using AX.Serialization;
//using System;
//using System.Collections;
//using System.Collections.Generic;
//using System.IO;
//using UnityEngine;
//using UnityEngine.UI;
//public class SaveDisaster : MonoBehaviour
//{
// public Button saveBtn;
// public string directoryPath;
// public string preLocalFileXml; //原有xml
// public string curLocalFileXml; //新建的xml
// public string localFileXml;
// public string serverFileXml;
// public string id;
// private List<string> delList;
// private List<string> updateList;
// // Use this for initialization
// void Start()
// {
// saveBtn = GetComponent<Button>();
// saveBtn.onClick.AddListener(Save);
// }
// void Save()
// {
// id = DisasterManager.disaster.Id.ToString();
// directoryPath = Application.dataPath + "/Data/" + id + "/File/"; //保存的是File文件夹下的内容
// curLocalFileXml = Application.dataPath + "/Data/" + id + "/" + id + ".xml"; //本地配置文件路径
// //根据当前文件夹内容创建新的xml到curLocalFileXml (保存在Data文件夹)
// CreateMD5Tool.Instance.CreateManifestXml(directoryPath, curLocalFileXml);
// DisasterInfosManager.Instance.SaveDisasterInfo(id);
// ResourceLoadWindow.Instance.LoadTextHintWindow("保存完成!", 2);
// }
//}
using AX.Network.Protocols;
using AX.NetworkSystem;
using AX.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class SaveDisaster : MonoBehaviour
{
public Button saveBtn;
public string directoryPath;
public string preLocalFileXml; //原有xml
public string curLocalFileXml; //新建的xml
public string localFileXml;
public string serverFileXml;
public string id;
private List<string> delList;
private List<string> updateList;
// Use this for initialization
void Start()
{
saveBtn = GetComponent<Button>();
saveBtn.onClick.AddListener(Save);
NetworkMessageDispatcher.AddListener("DELETE_FILE_REPLY", OnDeleteFileCompelete);
NetworkMessageDispatcher.AddListener("ERROR", OnDeleteFileError);
}
private void OnDestroy()
{
NetworkMessageDispatcher.RemoveListener("DELETE_FILE_REPLY", OnDeleteFileCompelete);
NetworkMessageDispatcher.RemoveListener("ERROR", OnDeleteFileError);
}
void Save()
{
id = DisasterManager.disaster.Id.ToString();
directoryPath = Application.dataPath + "/Data/" + id + "/File/"; //保存的是File文件夹下的内容
serverFileXml = id + "/" + id + ".xml"; //服务端配置文件路径
curLocalFileXml = Application.dataPath + "/Data/" + id + "/" + id + ".xml"; //本地配置文件路径
preLocalFileXml = Application.dataPath + "/Temp/" + id + ".xml"; //服务端配置文件下载到本地的路径
//1.根据当前文件夹内容创建新的xml到curLocalFileXml (保存在Data文件夹)
CreateMD5Tool.Instance.CreateManifestXml(directoryPath, curLocalFileXml);
ResourceLoadWindow.Instance.LoadTextHintWindow("保存成功", 0.5f);
//2.从服务端下载xml到preLocalFileXml (保存在Temp文件夹),单机注释
//NetworkManager.Default.DownloadFileAsync(preLocalFileXml, serverFileXml, null, null, OnFileVersionCompleted);
}
private void OnFileVersionCompleted(string preLocalFileXml, string serverFileXml)
{
//3.对比本地xml与服务端xml
SaveData data = CreateMD5Tool.Instance.CheckUpdate(preLocalFileXml, curLocalFileXml);
updateList = data.updateList;
delList = data.delList;
//4.删除从服务端下载的临时xml
File.Delete(preLocalFileXml);
//5.上传更新文件至服务端
foreach (string fileName in updateList)
{
string localFilePath = directoryPath + fileName;
string serverFilePath = id + "/File/" + fileName;
NetworkManager.Default.UploadFileAsync(localFilePath, serverFilePath, null, OnUploading, OnUpdateFileCompelete);
}
//6.从服务端删除多余文件
foreach (string fileName in delList)
{
string serverFilePath = id + "/File/" + fileName;
NetworkManager.Default.SendAsync("DELETE_FILE_REQUEST", serverFilePath);
}
}
/// <summary>
/// 正在上传
/// </summary>
/// <param name="fullName"></param>
/// <param name="fileName"></param>
/// <param name="progress"></param>
private void OnUploading(string fullName, string fileName, float progress)
{
ResourceLoadWindow.Instance.LoadTextHintWindow("正在上传文件...", 0.5f);
}
/// <summary>
/// 成功更新文件
/// </summary>
/// <param name="localFilePath"></param>
/// <param name="serverFilePath"></param>
private void OnUpdateFileCompelete(string localFilePath, string serverFilePath)
{
string fileName = serverFilePath.Replace(id + "/File/", string.Empty);
if (updateList.Contains(fileName))
{
updateList.Remove(fileName);
}
Debug.Log("********已将文件" + localFilePath + "更新至服务器" + serverFilePath + "********");
if (updateList.Count <= 0)
{
checkComplete();
}
}
/// <summary>
/// 成功删除文件
/// </summary>
/// <param name="message"></param>
private void OnDeleteFileCompelete(BinaryMessage message)
{
string serverFilePath = message.Body.Deserialize<string>();
string fileName = serverFilePath.Replace(id + "/File/", string.Empty);
if (delList.Contains(fileName))
{
delList.Remove(fileName);
}
Debug.Log("********已从服务端删除文件" + serverFilePath + "********");
if (delList.Count <= 0)
{
checkComplete();
}
}
/// <summary>
/// 删除文件失败
/// </summary>
/// <param name="obj"></param>
private void OnDeleteFileError(BinaryMessage message)
{
var msg = message.Body.Deserialize<ErrorInfo>();
if (msg.ErrorCode == 605)
{
Debug.LogError("******删除文件失败******");
}
}
private void checkComplete()
{
if (updateList.Count <= 0 && delList.Count <= 0)
{
//8.所有文件上传完毕,上传本地最新xml到服务端
NetworkManager.Default.UploadFileAsync(curLocalFileXml, serverFileXml, null, OnUploading, OnUpdateConfigCompelete);
}
}
/// <summary>
/// 成功上传xml到服务端
/// </summary>
/// <param name="localFileXml"></param>
/// <param name="serverFileXml"></param>
private void OnUpdateConfigCompelete(string localFileXml, string serverFileXml)
{
Debug.Log("********已将配置文件" + localFileXml + "更新至服务器" + serverFileXml + "********");
ResourceLoadWindow.Instance.LoadTextHintWindow("提交完成!", 2);
//9.通知其他客户端更新
NetworkManager.Default.SendAsync("DISASTER_FILES_UPDATE_SYNC", DisasterManager.disaster.Id);
}
}