public static void Refresh() { for (int i = 0; i < loadedlist.Count; i++) { ResourceInfoEx info = loadedlist[i]; if (info.length > 0) { if (abm.abhs.ContainsKey(info.bundlename)) { AssetBundleEx ab = abm.abhs[info.bundlename]; if (ab.afs.ContainsKey(info.assetfilename)) { AssetFileEx af = ab.afs[info.assetfilename]; Stream fs = af.stream; fs.Position = info.offset; byte[] data = new byte[info.length]; int ret = fs.Read(data, (int)0, (int)info.length); if (ret == info.length) { UpdateByType(info, data); } } } } } }
public static void SaveResourceTable(string path) { StreamWriter sw = new StreamWriter(path, false); sw.WriteLine("classname,objectname,bundlename,assetfilename,typename,path,offset,length"); foreach (KeyValuePair <string, Dictionary <string, ResourceInfoEx> > pair1 in resourcetable) { string classname = pair1.Key; Dictionary <string, ResourceInfoEx> resourcelist = pair1.Value; foreach (KeyValuePair <string, ResourceInfoEx> pair2 in resourcelist) { string objectname = pair2.Key; ResourceInfoEx info = pair2.Value; sw.WriteLine(classname + "," + objectname + "," + info.bundlename + "," + info.assetfilename + "," + info.typename + "," + info.path + "," + info.offset + "," + info.length); } } sw.Close(); sw.Dispose(); sw = null; }
public static void LoadResourceTable(string path) { resourcetable.Clear(); ScpReader reader = new ScpReader(path, true); for (int i = 0; i < reader.GetRecordCount(); i++) { ResourceInfoEx currentinfo = new ResourceInfoEx(); currentinfo.bundlename = reader.GetString(i, 2, ""); currentinfo.assetfilename = reader.GetString(i, 3, ""); currentinfo.objectname = reader.GetString(i, 1, ""); currentinfo.classname = reader.GetString(i, 0, ""); currentinfo.typename = reader.GetString(i, 4, ""); currentinfo.path = reader.GetString(i, 5, ""); currentinfo.offset = reader.GetLong(i, 6, -1); currentinfo.length = reader.GetLong(i, 7, -1); if (resourcetable.ContainsKey(currentinfo.classname) == false) { resourcetable[currentinfo.classname] = new Dictionary <string, ResourceInfoEx>(); } Dictionary <string, ResourceInfoEx> resourcelist = resourcetable[currentinfo.classname]; resourcelist[currentinfo.objectname] = currentinfo; } reader.Dispose(); Trace.Log("ResourceEx::LoadResourceTable:" + path + " finish"); }
private static void UpdateByType(ResourceInfoEx info, byte[] data) { if (info.obj == null) { return; } if (info.obj is Texture2D) { int tfval = (int)info.propertyfield["m_TextureFormat"]; TextureFormat tf = (TextureFormat)tfval; bool minmip = (bool)info.propertyfield["m_MipMap"]; int width = (int)info.propertyfield["m_Width"]; int height = (int)info.propertyfield["m_Height"]; int lightmapformat = (int)info.propertyfield["m_LightmapFormat"]; Texture2D tex = (Texture2D)info.obj; int d3dformat = -1; if (tf == TextureFormat.ARGB32) { d3dformat = 21; } else if (tf == TextureFormat.DXT5) { d3dformat = 894720068; } else if (tf == TextureFormat.DXT1) { d3dformat = 827611204; } else if (tf == TextureFormat.Alpha8) { d3dformat = 28; } if (d3dformat != -1) { int usage = 0; // 0x00000200; int lockflag = 0; // 0x00002000; int levels = minmip ? 0 : 1; int pool = 0; IntPtr oldp = tex.GetNativeTexturePtr(); RenderingPlugin.ReleaseTexture(oldp); IntPtr pData = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, pData, data.Length); IntPtr p = RenderingPlugin.CreateTexture(width, height, levels, usage, d3dformat, pool, lightmapformat, pData, data.Length, lockflag); Marshal.FreeHGlobal(pData); tex.UpdateExternalTexture(p); tex.name = info.objectname; } } }
private static UnityEngine.Object LoadByType(ResourceInfoEx info, byte[] data, Type t) { UnityEngine.Object retobj = null; if (t == typeof(Texture2D)) { int tfval = (int)info.propertyfield["m_TextureFormat"]; TextureFormat tf = (TextureFormat)tfval; bool minmip = (bool)info.propertyfield["m_MipMap"]; int width = (int)info.propertyfield["m_Width"]; int height = (int)info.propertyfield["m_Height"]; int lightmapformat = (int)info.propertyfield["m_LightmapFormat"]; Texture2D tex = null; int d3dformat = -1; if (tf == TextureFormat.ARGB32) { d3dformat = 21; } else if (tf == TextureFormat.DXT5) { d3dformat = 894720068; } else if (tf == TextureFormat.DXT1) { d3dformat = 827611204; } else if (tf == TextureFormat.Alpha8) { d3dformat = 28; } if (d3dformat != -1) { int usage = 0; // 0x00000200; int lockflag = 0; // 0x00002000; int levels = minmip ? 0 : 1; int pool = 0; IntPtr pData = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, pData, data.Length); IntPtr p = RenderingPlugin.CreateTexture(width, height, levels, usage, d3dformat, pool, lightmapformat, pData, data.Length, lockflag); Marshal.FreeHGlobal(pData); tex = Texture2D.CreateExternalTexture(width, height, tf, true, false, p); tex.name = info.objectname; } retobj = tex; } info.refcount = 1; info.obj = retobj; return(retobj); }
private static void LoadResourceInfoFromAssetFile(AssetBundleEx ab, AssetFileEx af, string classname) { List <ObjectData> odlist = af.extractlist[classname]; Dictionary <string, ResourceInfoEx> resourcetableoftype = resourcetable[classname]; foreach (ObjectData od in odlist) { ResourceInfoEx currentinfo = new ResourceInfoEx(); currentinfo.bundlename = ab.name; currentinfo.assetfilename = af.sourceFile; currentinfo.objectname = od.objectname; currentinfo.classname = od.classname; currentinfo.typename = od.typename; currentinfo.propertyfield = od.propertyfield; currentinfo.path = od.path; object data = od.data; if (data is StreamBlock) { StreamBlock sdi = (StreamBlock)od.data; if (sdi != null) { currentinfo.offset = sdi.offset; currentinfo.length = sdi.length; } } if (currentinfo.objectname == null || currentinfo.objectname == "") { continue; } //if (currentinfo.path == null || currentinfo.path == "") //{ // continue; //} if (resourcetableoftype.ContainsKey(currentinfo.objectname)) { ResourceInfoEx otherinfo = resourcetableoftype[od.objectname]; //Logger.Log("current=" + currentinfo.objectname + "<" + currentinfo.typename + ">,other=" + otherinfo.objectname + "<" + otherinfo.typename + ">"); } else { resourcetableoftype.Add(currentinfo.objectname, currentinfo); } } }
public static void Collect(bool force = false) { foreach (KeyValuePair <string, Dictionary <string, ResourceInfoEx> > pair in resourcetable) { string typename = pair.Key; Dictionary <string, ResourceInfoEx> infolist = pair.Value; foreach (KeyValuePair <string, ResourceInfoEx> pair1 in infolist) { ResourceInfoEx info = pair1.Value; if (force || info.refcount <= 0) { UnloadByType(info); loadedlist.Remove(info); } } } }
private static void UnloadByType(ResourceInfoEx info) { if (info.obj == null) { return; } if (info.obj is Texture2D) { Texture2D tex = (Texture2D)info.obj; IntPtr oldp = tex.GetNativeTexturePtr(); RenderingPlugin.ReleaseTexture(oldp); tex.UpdateExternalTexture(IntPtr.Zero); Texture2D.Destroy(tex); } info.obj = null; info.refcount = 0; }
public static void Unload(string relpath, Type t) { if (!bEnable) { return; } else { string classname = t.Name; if (resourcetable.ContainsKey(classname) == false) { return; } Dictionary <string, ResourceInfoEx> infolist = resourcetable[classname]; if (infolist == null) { return; } string resname = Path.GetFileNameWithoutExtension(relpath); if (infolist.ContainsKey(resname) == false) { return; } ResourceInfoEx info = infolist[resname]; if (info.refcount <= 1) { UnloadByType(info); loadedlist.Remove(info); } else { info.refcount--; } } }
public static void LoadAsyn(string relpath, Type t, ResourceAsynHandlerEx handler) { if (relpath == null || relpath.Length <= 0) { return; } if (!bEnable) //editor case { //return Resources.Load(relpath, t); return; } else { string classname = t.Name; if (resourcetable.ContainsKey(classname) == false) { return; } Dictionary <string, ResourceInfoEx> infolist = resourcetable[classname]; if (infolist == null) { return; } string resname = Path.GetFileNameWithoutExtension(relpath); if (infolist.ContainsKey(resname) == false) { return; } ResourceInfoEx info = infolist[resname]; //if (info.path == null || info.path.Length <= 0) return; //UnityEngine.Object retobj = Resources.Load(info.path, t); //if (retobj == null) //{ // retobj = Resources.Load(info.path); //} //if (!t.IsInstanceOfType(retobj)) //{ // UnityEngine.Object extractobj = GetObjectFromDependent(retobj, resname, t); // if (extractobj != null) retobj = extractobj; //} //if (t == null || !t.IsInstanceOfType(retobj)) //{ // Debug.Log("resname=" + resname + ",respath=" + info.path); // Debug.Log(retobj); //} //if (handler != null) //{ // handler(relpath, retobj); //} if (info.obj != null)//已经加载过的直接回调 { info.refcount++; if (handler != null) { handler(relpath, info.obj); } return; } if (info.length > 0) { if (abm.abhs.ContainsKey(info.bundlename)) { AssetBundleEx ab = abm.abhs[info.bundlename]; if (ab.afs.ContainsKey(info.assetfilename)) { AssetFileEx af = ab.afs[info.assetfilename]; Stream fs = af.stream; fs.Position = info.offset; byte[] data = new byte[info.length]; AsyncResourceState asyncState = new AsyncResourceState { FS = fs, Buffer = data, Info = info, T = t, Handler = handler, ResourcePath = relpath }; IAsyncResult asyncResult = fs.BeginRead(data, (int)0, (int)info.length, new AsyncCallback(AsyncReadCallback), asyncState); } } } return; } }
public static UnityEngine.Object Load(string relpath, Type t) { if (relpath == null || relpath.Length <= 0) { return(null); } if (!bEnable) { UnityEngine.Object retobj = null; try { if (LoadResourceEvent != null) { retobj = LoadResourceEvent(relpath, t); } } catch { } return(retobj); } else { string classname = t.Name; if (resourcetable.ContainsKey(classname) == false) { return(null); } Dictionary <string, ResourceInfoEx> infolist = resourcetable[classname]; if (infolist == null) { return(null); } string resname = Path.GetFileNameWithoutExtension(relpath); if (infolist.ContainsKey(resname) == false) { return(null); } ResourceInfoEx info = infolist[resname]; //if (info.path == null || info.path.Length <= 0) return null; //UnityEngine.Object retobj = Resources.Load(info.path, t); //if (retobj == null) //{ // retobj = Resources.Load(info.path); //} //if (!t.IsInstanceOfType(retobj)) //{ // UnityEngine.Object extractobj = GetObjectFromDependent(retobj, resname, t); // if (extractobj != null) retobj = extractobj; //} //if (t == null || !t.IsInstanceOfType(retobj)) //{ //} //Debug.Log("resname=" + resname + ",respath=" + info.path); //Debug.Log(retobj); //return retobj; if (info.obj != null) { info.refcount++; return(info.obj); //已经有直接返回 } UnityEngine.Object retobj = null; if (info.length > 0) { if (abm.abhs.ContainsKey(info.bundlename)) { AssetBundleEx ab = abm.abhs[info.bundlename]; if (ab.afs.ContainsKey(info.assetfilename)) { AssetFileEx af = ab.afs[info.assetfilename]; Stream fs = af.stream; fs.Position = info.offset; byte[] data = new byte[info.length]; int ret = fs.Read(data, (int)0, (int)info.length); if (ret == info.length) { retobj = LoadByType(info, data, t); loadedlist.Add(info); } } } } return(retobj); } }