357 lines
12 KiB
C#
357 lines
12 KiB
C#
using Thousandto.Core.Asset;
|
||
using Thousandto.Core.Base;
|
||
using Thousandto.Launcher.ExternalLibs;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
using JSONNode = UnityEngine.Gonbest.MagicCube.JSONNode;
|
||
using JSON = UnityEngine.Gonbest.MagicCube.JSON;
|
||
using PathUtils = UnityEngine.Gonbest.MagicCube.PathUtils;
|
||
using CoroutinePool = UnityEngine.Gonbest.MagicCube.CoroutinePool;
|
||
using StringUtils = UnityEngine.Gonbest.MagicCube.StringUtils;
|
||
|
||
namespace Thousandto.Package
|
||
{
|
||
/// <summary>
|
||
/// 测试bundle读取,方便确认打出来的bundle是否有效
|
||
/// </summary>
|
||
public class TestReadBundle
|
||
{
|
||
static AssetBundleRequest _request = null;
|
||
static AssetBundle _bundle = null;
|
||
|
||
[MenuItem("Test/LoadBundle(Runtime)")]
|
||
static void TestLoad()
|
||
{
|
||
|
||
string streamingAssetPath = Application.streamingAssetsPath;
|
||
string path = AssetDatabase.GetAssetPath(Selection.objects[0]).Replace("\\", "/");
|
||
string requestPath = path.Replace("Assets/StreamingAssets/GameAssets/Resources/", "").Replace(".unity3d", "");
|
||
LoadAssetBundle(requestPath);
|
||
}
|
||
|
||
[MenuItem("Test/LoadBundle(Editor)")]
|
||
static void TestLoad2()
|
||
{
|
||
|
||
string streamingAssetPath = Application.streamingAssetsPath;
|
||
string path = AssetDatabase.GetAssetPath(Selection.objects[0]).Replace("\\", "/");
|
||
var bundle = AssetBundle.LoadFromFile(path);
|
||
var assetNames = bundle.GetAllAssetNames();
|
||
UnityEngine.Object obj = null;
|
||
for(int i = 0; i < assetNames.Length; ++i)
|
||
{
|
||
UnityEngine.Debug.Log(assetNames[i]);
|
||
//obj = bundle.LoadAsset(assetNames[i]);
|
||
}
|
||
|
||
var assetName = Path.GetFileNameWithoutExtension(path);
|
||
if (path.IndexOf("Prefab/") >= 0 || path.IndexOf("VFX/") >= 0)
|
||
{
|
||
assetName += "_ScriptableObject";
|
||
}
|
||
UnityEngine.Debug.Log("AssetName:" + assetName);
|
||
obj = bundle.LoadAsset(assetName);
|
||
bundle.Unload(false);
|
||
|
||
GameObject.Instantiate(obj);
|
||
GameObject.DestroyImmediate(obj, false);
|
||
}
|
||
|
||
[MenuItem("Test/ResetLoadBundle")]
|
||
static void ResetTestLoad()
|
||
{
|
||
PathUtils.Initialize(false, 0);
|
||
}
|
||
|
||
|
||
[MenuItem("Test/清空游戏内已经加载过的资源的缓存数据")]
|
||
static void ClearAllCache()
|
||
{
|
||
try
|
||
{
|
||
ResourcesEx.ClearActionQueue();
|
||
Code.Center.GameCenter.TextureManager.Sweep();
|
||
}
|
||
catch(System.Exception ex)
|
||
{
|
||
UnityEngine.Debug.LogException(ex);
|
||
}
|
||
}
|
||
|
||
|
||
public static void LoadAssetBundle(string requestPath)
|
||
{
|
||
PathUtils.Initialize(true, 0);
|
||
|
||
ResourcesEx.LoadAsync(requestPath, AssetTypeCode.None,
|
||
(retObject, retBundle, err) =>
|
||
{
|
||
if (retBundle != null)
|
||
{
|
||
var assetBundle = retBundle;
|
||
var names = assetBundle.GetAllAssetNames();
|
||
string pathInBundle = "";
|
||
for (int i = 0; i < names.Length; ++i)
|
||
{
|
||
pathInBundle = names[i].ToUpper();
|
||
UnityEngine.Debug.Log(pathInBundle);
|
||
}
|
||
|
||
_request = assetBundle.LoadAssetAsync(pathInBundle, typeof(UnityEngine.Object));
|
||
_bundle = assetBundle;
|
||
EditorApplication.update += Update;
|
||
}
|
||
else
|
||
{
|
||
UnityEngine.Debug.LogError("Load bundel fail: " + requestPath);
|
||
}
|
||
}, AsyncActionType.LoadObject);
|
||
}
|
||
|
||
public static void Update()
|
||
{
|
||
if (_request == null || !_request.isDone)
|
||
return;
|
||
EditorApplication.update -= Update;
|
||
|
||
var asset = _request.asset;
|
||
var name = asset.name;
|
||
UnityEngine.Debug.Log("LoadAsync asset name: " + name);
|
||
|
||
if(_bundle != null)
|
||
{
|
||
var names = _bundle.GetAllAssetNames();
|
||
string pathInBundle = "";
|
||
for (int i = 0; i < names.Length; ++i)
|
||
{
|
||
pathInBundle = names[i].ToUpper();
|
||
UnityEngine.Debug.Log(pathInBundle);
|
||
}
|
||
|
||
var obje = _bundle.LoadAsset(pathInBundle);
|
||
var tempGO = obje;
|
||
if (obje is Launcher.ExternalLibs.PrefabAssetData)
|
||
{
|
||
var _assetData = new PrefabAssetDataProxy(obje);
|
||
tempGO = _assetData.ToPrefabGo();
|
||
GameObject ins2 = GameObject.Instantiate(tempGO) as GameObject;
|
||
}
|
||
//_bundle.Unload(false);
|
||
}
|
||
}
|
||
|
||
[MenuItem("Test/TestLoadMapFile")]
|
||
public static void TestParseMapFile()
|
||
{
|
||
string mapFilePath = AssetDatabase.GetAssetPath(Selection.objects[0]);
|
||
MapFileManage manage = new MapFileManage();
|
||
manage.parseMapFile(mapFilePath, "", "");
|
||
}
|
||
|
||
[MenuItem("Test/TestJson")]
|
||
public static void TestJson()
|
||
{
|
||
JSON.Parse("[{\"headers\":{\"sign\":\"AccountLoginBefore\",\"area\":\"China\",\"gameID\":\"185\"},\"body\":\"11~@185~@China~@yumo~@1.3.0~@null~@92c55bcdda09fe2055fc89f78af269b6~@90168f82-85de-480b-a45f-942d2884b923~@{\\\"errorCode\\\":1,\\\"errorInfo\\\":\\\"登陆失败,验证不通过 code=1\\\"}~@1502288728731~@1502288731577\"}]");
|
||
}
|
||
|
||
class MapFileManage
|
||
{
|
||
string _TAG = "MapFileManage.cs ";
|
||
List<MapFileData> _mapFileDataList = new List<MapFileData>();
|
||
string _saveDir;
|
||
|
||
public void setSaveDir(string saveDir)
|
||
{
|
||
_saveDir = saveDir;
|
||
}
|
||
|
||
public List<MapFileData> GetMapFileDataList()
|
||
{
|
||
return _mapFileDataList;
|
||
}
|
||
|
||
public int parseMapFile(string mapFile, string resUrl, string saveDir)
|
||
{
|
||
_saveDir = saveDir;
|
||
UnityEngine.Debug.LogError(_TAG + "parseMapFile(string mapFile, string resUrl):" + mapFile + "," + resUrl);
|
||
|
||
int ret = 1;
|
||
|
||
List<MapFileData> mapFileDataList = _mapFileDataList;
|
||
|
||
FileStream mapFileStream = null;
|
||
try
|
||
{
|
||
mapFileStream = new FileStream(mapFile, FileMode.Open);
|
||
|
||
long filePosition = 0;
|
||
long mapFileSize = mapFileStream.Length;
|
||
while (mapFileSize > 0 && filePosition != mapFileSize)
|
||
{
|
||
if (filePosition >= mapFileSize)
|
||
{
|
||
UnityEngine.Debug.LogError("解析出错了");
|
||
return ret;
|
||
}
|
||
MapFileData mapFileData = new MapFileData();
|
||
mapFileData.Begin = parseInt(read(mapFileStream, 10, filePosition, out filePosition));
|
||
|
||
if (mapFileData.Begin == -1)
|
||
{
|
||
UnityEngine.Debug.LogError("解析map文件,出现异常,请检查: " + mapFile);
|
||
return ret;
|
||
}
|
||
|
||
mapFileData.End = parseInt(read(mapFileStream, 10, filePosition, out filePosition));
|
||
mapFileData.DirLen = parseInt(read(mapFileStream, 10, filePosition, out filePosition));
|
||
mapFileData.NameLen = parseInt(read(mapFileStream, 10, filePosition, out filePosition));
|
||
mapFileData.Md5Len = parseInt(read(mapFileStream, 10, filePosition, out filePosition));
|
||
mapFileData.FileSize = parseInt(read(mapFileStream, 10, filePosition, out filePosition));
|
||
mapFileData.Dir = read(mapFileStream, mapFileData.DirLen, filePosition, out filePosition);
|
||
mapFileData.Name = read(mapFileStream, mapFileData.NameLen, filePosition, out filePosition);
|
||
mapFileData.Md5 = read(mapFileStream, mapFileData.Md5Len, filePosition, out filePosition);
|
||
|
||
mapFileData.ResUrl = resUrl;
|
||
mapFileData.SaveDir = _saveDir;
|
||
|
||
mapFileDataList.Add(mapFileData);
|
||
}
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
UnityEngine.Debug.LogError(_TAG + ex.Message + "\n" + ex.StackTrace);
|
||
}
|
||
finally
|
||
{
|
||
if (mapFileStream != null)
|
||
{
|
||
mapFileStream.Close();
|
||
}
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
private int parseInt(string intStr)
|
||
{
|
||
int ret = 0;
|
||
if (!int.TryParse(intStr, out ret))
|
||
{
|
||
return -1;
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
private string read(FileStream mapFileStream, int readLen, long position, out long filePosition)
|
||
{
|
||
Byte[] beginBuf = new Byte[readLen];
|
||
int readNum = mapFileStream.Read(beginBuf, 0, readLen);
|
||
filePosition = position + readLen;
|
||
if (readNum <= 0)
|
||
{
|
||
return null;
|
||
}
|
||
string retStr = System.Text.Encoding.Default.GetString(beginBuf);
|
||
return retStr;
|
||
}
|
||
|
||
}
|
||
|
||
public class MapFileData
|
||
{
|
||
int _begin;
|
||
|
||
public int Begin
|
||
{
|
||
get { return _begin; }
|
||
set { _begin = value; }
|
||
}
|
||
int _end;
|
||
|
||
public int End
|
||
{
|
||
get { return _end; }
|
||
set { _end = value; }
|
||
}
|
||
int _dirLen;
|
||
|
||
public int DirLen
|
||
{
|
||
get { return _dirLen; }
|
||
set { _dirLen = value; }
|
||
}
|
||
int _nameLen;
|
||
|
||
public int NameLen
|
||
{
|
||
get { return _nameLen; }
|
||
set { _nameLen = value; }
|
||
}
|
||
int _md5Len;
|
||
|
||
public int Md5Len
|
||
{
|
||
get { return _md5Len; }
|
||
set { _md5Len = value; }
|
||
}
|
||
int _fileSize;
|
||
|
||
public int FileSize
|
||
{
|
||
get { return _fileSize; }
|
||
set { _fileSize = value; }
|
||
}
|
||
string _dir;
|
||
|
||
public string Dir
|
||
{
|
||
get { return _dir; }
|
||
set { _dir = value; }
|
||
}
|
||
string _name;
|
||
|
||
public string Name
|
||
{
|
||
get { return _name; }
|
||
set { _name = value; }
|
||
}
|
||
string _md5;
|
||
|
||
public string Md5
|
||
{
|
||
get { return _md5; }
|
||
set { _md5 = value; }
|
||
}
|
||
|
||
string _resUrl;
|
||
|
||
public string ResUrl
|
||
{
|
||
get { return _resUrl; }
|
||
set { _resUrl = value; }
|
||
}
|
||
|
||
string _saveDir;
|
||
|
||
public string SaveDir
|
||
{
|
||
get { return _saveDir; }
|
||
set { _saveDir = value; }
|
||
}
|
||
|
||
public string GetTempFullPath()
|
||
{
|
||
return FullPath + ".bak";
|
||
}
|
||
|
||
internal object ArgObj = null;
|
||
internal Thousandto.Update.Delegate.UpdateAction<string, int, object> DownloadCallBack = null;
|
||
internal bool Downloaded = false;
|
||
internal bool Downloading = false;
|
||
internal string FullPath = null;
|
||
}
|
||
}
|
||
}
|