示例#1
0
        public static void MakeObb(string dest, params string[] subzips)
        {
            if (!string.IsNullOrEmpty(dest) && subzips != null && subzips.Length > 0)
            {
                HashSet <string> reskeys = new HashSet <string>();
                HashSet <string> sptkeys = new HashSet <string>();
                using (var sdest = PlatDependant.OpenWrite(dest))
                {
                    using (var zdest = new ZipArchive(sdest, ZipArchiveMode.Create))
                    {
                        for (int i = 0; i < subzips.Length; ++i)
                        {
                            try
                            {
                                var sfile = subzips[i];
                                if (PlatDependant.IsFileExist(sfile))
                                {
                                    var              key        = System.IO.Path.GetFileNameWithoutExtension(sfile).ToLower();
                                    bool             isres      = false;
                                    bool             isspt      = false;
                                    HashSet <string> entrynames = new HashSet <string>();
                                    using (var ssrc = PlatDependant.OpenRead(sfile))
                                    {
                                        using (var zsrc = new ZipArchive(ssrc, ZipArchiveMode.Read))
                                        {
                                            foreach (var sentry in zsrc.Entries)
                                            {
                                                var fullname = sentry.FullName;
                                                if (fullname.StartsWith("res/"))
                                                {
                                                    isres = true;
                                                }
                                                else if (fullname.StartsWith("spt/"))
                                                {
                                                    isspt = true;
                                                }
                                                if (entrynames.Add(fullname))
                                                {
                                                    var dentry = zdest.CreateEntry(fullname, isres ? CompressionLevel.NoCompression : CompressionLevel.Optimal);
                                                    using (var ses = sentry.Open())
                                                    {
                                                        using (var des = dentry.Open())
                                                        {
                                                            ses.CopyTo(des);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    if (isres)
                                    {
                                        reskeys.Add(key);
                                    }
                                    if (isspt)
                                    {
                                        sptkeys.Add(key);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                PlatDependant.LogError(e);
                            }
                        }

                        if (reskeys.Count > 0)
                        {
                            var resindex = zdest.CreateEntry("res/index.txt", CompressionLevel.Optimal);
                            using (var sindex = resindex.Open())
                            {
                                using (var swindex = new System.IO.StreamWriter(sindex, System.Text.Encoding.UTF8))
                                {
                                    foreach (var key in reskeys)
                                    {
                                        swindex.WriteLine(key);
                                    }
                                }
                            }
                        }
                        if (sptkeys.Count > 0)
                        {
                            var sptindex = zdest.CreateEntry("spt/index.txt", CompressionLevel.Optimal);
                            using (var sindex = sptindex.Open())
                            {
                                using (var swindex = new System.IO.StreamWriter(sindex, System.Text.Encoding.UTF8))
                                {
                                    foreach (var key in sptkeys)
                                    {
                                        swindex.WriteLine(key);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
示例#2
0
        public static System.IO.Stream GetLuaStream(string name, out string location)
        {
#if UNITY_EDITOR
            try
            {
                if (!string.IsNullOrEmpty(name))
                {
                    if (name.Length > 0 && name[0] == '?')
                    {
                        var    real = name.Substring("?raw.".Length);
                        string mod  = null;
                        string norm = real;
                        if (real.StartsWith("mod."))
                        {
                            if (real.StartsWith("mod.\""))
                            {
                                var mindex = real.IndexOf('\"', "mod.\"".Length);
                                if (mindex > 0)
                                {
                                    mod  = real.Substring("mod.\"".Length, mindex - "mod.\"".Length);
                                    norm = real.Substring(mindex + 2);
                                }
                            }
                            else
                            {
                                var mindex = real.IndexOf('.', "mod.".Length);
                                if (mindex > 0)
                                {
                                    mod  = real.Substring("mod.".Length, mindex - "mod.".Length);
                                    norm = real.Substring(mindex + 1);
                                }
                            }
                        }
                        norm = norm.Replace('.', '/');
                        bool isFileExist = false;
                        if (mod == null)
                        {
                            real = "Assets/CapsSpt/" + norm + ".lua";
                        }
                        else
                        {
                            string package;
                            ResManager.EditorResLoader.ResRuntimeCache.ModToPackage.TryGetValue(mod, out package);
                            if (!string.IsNullOrEmpty(package))
                            {
                                real = "Packages/" + package + "/CapsSpt/" + norm + ".lua";
                                //real = EditorToClientUtils.GetPathFromAssetName(real);
                                isFileExist = !string.IsNullOrEmpty(real) && PlatDependant.IsFileExist(real);
                            }
                            if (!isFileExist)
                            {
                                real = "Assets/Mods/" + mod + "/CapsSpt/" + norm + ".lua";
                            }
                        }
                        if (isFileExist || PlatDependant.IsFileExist(real))
                        {
                            location = real;
                            return(PlatDependant.OpenRead(real));
                        }
                    }
                    else
                    {
                        var    file = "CapsSpt/" + name.Replace('.', '/') + ".lua";
                        string found;
                        if (ThreadLocalObj.GetThreadId() == ThreadSafeValues.UnityThreadID)
                        {
                            found = ResManager.EditorResLoader.CheckDistributePath(file, true);
                        }
                        else
                        {
                            found = ResManager.EditorResLoader.CheckDistributePathSafe(file);
                        }
                        //if (found != null)
                        //{
                        //    if (found.StartsWith("Packages/"))
                        //    {
                        //        found = EditorToClientUtils.GetPathFromAssetName(found);
                        //    }
                        //}
                        if (found != null)
                        {
                            location = found;
                            return(PlatDependant.OpenRead(found));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                PlatDependant.LogError(e);
            }
#elif UNITY_ENGINE || UNITY_5_3_OR_NEWER
            try
            {
                if (name.Length > 0 && name[0] == '?')
                {
                    if (name.StartsWith("?raw."))
                    {
                        if (_RuntimeRawManifest != null)
                        {
                            var real = name.Substring("?raw.".Length);
                            real = real.Replace("\"", "");
                            string archreal = Environment.Is64BitProcess ? "@64." + real : "@32." + real;
                            CapsResManifestNode node;
                            if (_RuntimeRawManifest.TryGetItemIgnoreExt(archreal, out node, _LuaRequireSeperateChars) || _RuntimeRawManifest.TryGetItemIgnoreExt(real, out node, _LuaRequireSeperateChars))
                            {
                                if (node != null && node.Item != null)
                                {
                                    var item = node.Item;
                                    while (item.Ref != null)
                                    {
                                        item = item.Ref;
                                    }
                                    return(GetLuaStream(item, out location));
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (_RuntimeManifest != null)
                    {
                        var node = _RuntimeManifest.GetItem(name, _LuaRequireSeperateChars);
                        if (node != null && node.Item != null)
                        {
                            var item = node.Item;
                            while (item.Ref != null)
                            {
                                item = item.Ref;
                            }
                            return(GetLuaStream(item, out location));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                PlatDependant.LogError(e);
            }
#else
            try
            {
                if (!string.IsNullOrEmpty(name))
                {
                    if (name.Length > 0 && name[0] == '?')
                    {
                        var    real = name.Substring("?raw.".Length);
                        string mod  = null;
                        string norm = real;
                        if (real.StartsWith("mod."))
                        {
                            if (real.StartsWith("mod.\""))
                            {
                                var mindex = real.IndexOf('\"', "mod.\"".Length);
                                if (mindex > 0)
                                {
                                    mod  = real.Substring("mod.\"".Length, mindex - "mod.\"".Length);
                                    norm = real.Substring(mindex + 2);
                                }
                            }
                            else
                            {
                                var mindex = real.IndexOf('.', "mod.".Length);
                                if (mindex > 0)
                                {
                                    mod  = real.Substring("mod.".Length, mindex - "mod.".Length);
                                    norm = real.Substring(mindex + 1);
                                }
                            }
                        }
                        norm = norm.Replace('.', '/');
                        if (mod == null)
                        {
                            real = "/spt/" + norm + ".lua";
                        }
                        else
                        {
                            real = "/mod/" + mod + "/spt/" + norm + ".lua";
                        }
                        var stream = ResManager.LoadFileRelative(real);
                        if (stream != null)
                        {
                            location = real;
                            return(stream);
                        }
                    }
                    else
                    {
                        var    file = "/spt/" + name.Replace('.', '/') + ".lua";
                        string real, mod, dist;
                        real = ResManager.FindFile(file, out mod, out dist);
                        if (real != null)
                        {
                            try
                            {
                                var stream = PlatDependant.OpenRead(real);
                                if (stream != null)
                                {
                                    location = file;
                                    if (!string.IsNullOrEmpty(dist))
                                    {
                                        location = "/dist/" + dist + location;
                                    }
                                    if (!string.IsNullOrEmpty(mod))
                                    {
                                        location = "/mod/" + mod + location;
                                    }
                                    return(stream);
                                }
                            }
                            catch (Exception e)
                            {
                                PlatDependant.LogError(e);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                PlatDependant.LogError(e);
            }
#endif
            location = "";
            return(null);
        }
示例#3
0
        private static System.IO.Stream GetLuaStream(CapsResManifestItem item, out string location)
        {
            try
            {
                var rnode = item.Node;
                System.Text.StringBuilder sbpath = new System.Text.StringBuilder();
                while (rnode.Parent != null)
                {
                    if (sbpath.Length > 0)
                    {
                        sbpath.Insert(0, '/');
                    }
                    sbpath.Insert(0, rnode.PPath);
                    rnode = rnode.Parent;
                }
                var path = sbpath.ToString();

                // load from update path
                var sptpath = ThreadSafeValues.UpdatePath + "/spt/" + path;
                if (PlatDependant.IsFileExist(sptpath))
                {
                    location = sptpath;
                    return(PlatDependant.OpenRead(sptpath));
                }
                // load from package
                if (ThreadSafeValues.AppStreamingAssetsPath.Contains("://"))
                {
                    if (ThreadSafeValues.AppPlatform == RuntimePlatform.Android.ToString() && ResManager.LoadAssetsFromApk)
                    {
                        // Obb
                        if (ResManager.LoadAssetsFromObb && ResManager.ObbZipArchive != null)
                        {
                            sptpath = "spt/" + path;
                            int retryTimes = 10;
                            for (int i = 0; i < retryTimes; ++i)
                            {
                                Exception error = null;
                                do
                                {
                                    ZipArchive za = ResManager.ObbZipArchive;
                                    if (za == null)
                                    {
                                        PlatDependant.LogError("Obb Archive Cannot be read.");
                                        break;
                                    }
                                    try
                                    {
                                        var entry = za.GetEntry(sptpath);
                                        if (entry != null)
                                        {
                                            location = sptpath;
                                            return(entry.Open());
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        error = e;
                                        break;
                                    }
                                } while (false);
                                if (error != null)
                                {
                                    if (i == retryTimes - 1)
                                    {
                                        PlatDependant.LogError(error);
                                    }
                                    else
                                    {
                                        PlatDependant.LogError(error);
                                        PlatDependant.LogInfo("Need Retry " + i);
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        // Apk
                        //if (true)
                        {
                            sptpath = "assets/spt/" + path;
                            int retryTimes = 10;
                            for (int i = 0; i < retryTimes; ++i)
                            {
                                Exception error = null;
                                do
                                {
                                    ZipArchive za = ResManager.AndroidApkZipArchive;
                                    if (za == null)
                                    {
                                        PlatDependant.LogError("Apk Archive Cannot be read.");
                                        break;
                                    }
                                    try
                                    {
                                        var entry = za.GetEntry(sptpath);
                                        if (entry != null)
                                        {
                                            location = sptpath;
                                            return(entry.Open());
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        error = e;
                                        break;
                                    }
                                } while (false);
                                if (error != null)
                                {
                                    if (i == retryTimes - 1)
                                    {
                                        PlatDependant.LogError(error);
                                    }
                                    else
                                    {
                                        PlatDependant.LogError(error);
                                        PlatDependant.LogInfo("Need Retry " + i);
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {
                    sptpath = ThreadSafeValues.AppStreamingAssetsPath + "/spt/" + path;
                    if (PlatDependant.IsFileExist(sptpath))
                    {
                        location = sptpath;
                        return(PlatDependant.OpenRead(sptpath));
                    }
                }
            }
            catch (Exception e)
            {
                PlatDependant.LogError(e);
            }
            location = "";
            return(null);
        }
示例#4
0
        private static void LoadRuntimeManifest(TaskProgress progress)
        {
            try
            {
                var maniPath = ThreadSafeValues.UpdatePath + "/spt/manifest.m.txt";
                if (PlatDependant.IsFileExist(maniPath))
                {
                    _RuntimeRawManifest = LoadManifest(maniPath);
                }
                else
                {
                    CapsResManifest mani = new CapsResManifest();
                    // load from update path
                    var sptfolder = ThreadSafeValues.UpdatePath + "/spt/";
                    try
                    {
                        var files = PlatDependant.GetAllFiles(sptfolder);
                        if (files != null && files.Length > 0)
                        {
                            for (int i = 0; i < files.Length; ++i)
                            {
                                var file = files[i];
                                var part = file.Substring(sptfolder.Length).Replace('\\', '/');
                                var node = mani.AddOrGetItem(part);
                                if (node.Item == null)
                                {
                                    CapsResManifestItem item;
                                    item      = new CapsResManifestItem(node);
                                    node.Item = item;
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        PlatDependant.LogError(e);
                    }
                    // load from package
                    if (ThreadSafeValues.AppStreamingAssetsPath.Contains("://"))
                    {
                        if (ThreadSafeValues.AppPlatform == RuntimePlatform.Android.ToString() && ResManager.LoadAssetsFromApk)
                        {
                            // Obb
                            if (ResManager.LoadAssetsFromObb && ResManager.ObbZipArchive != null)
                            {
                                sptfolder = "spt/";
                                int retryTimes = 10;
                                int entryindex = 0;
                                for (int i = 0; i < retryTimes; ++i)
                                {
                                    Exception error = null;
                                    do
                                    {
                                        ZipArchive za = ResManager.ObbZipArchive;
                                        if (za == null)
                                        {
                                            PlatDependant.LogError("Obb Archive Cannot be read.");
                                            break;
                                        }
                                        try
                                        {
                                            var entries = za.Entries;
                                            while (entryindex < entries.Count)
                                            {
                                                var entry    = entries[entryindex];
                                                var fullname = entry.FullName;
                                                if (fullname.StartsWith(sptfolder))
                                                {
                                                    var part = fullname.Substring(sptfolder.Length);
                                                    var node = mani.AddOrGetItem(part);
                                                    if (node.Item == null)
                                                    {
                                                        CapsResManifestItem item;
                                                        item      = new CapsResManifestItem(node);
                                                        node.Item = item;
                                                    }
                                                }
                                                ++entryindex;
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            error = e;
                                            break;
                                        }
                                    } while (false);
                                    if (error != null)
                                    {
                                        if (i == retryTimes - 1)
                                        {
                                            PlatDependant.LogError(error);
                                        }
                                        else
                                        {
                                            PlatDependant.LogError(error);
                                            PlatDependant.LogInfo("Need Retry " + i);
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            // Apk
                            //if (true)
                            {
                                sptfolder = "assets/spt/";
                                int retryTimes = 10;
                                int entryindex = 0;
                                for (int i = 0; i < retryTimes; ++i)
                                {
                                    Exception error = null;
                                    do
                                    {
                                        ZipArchive za = ResManager.AndroidApkZipArchive;
                                        if (za == null)
                                        {
                                            PlatDependant.LogError("Apk Archive Cannot be read.");
                                            break;
                                        }
                                        try
                                        {
                                            var entries = za.Entries;
                                            while (entryindex < entries.Count)
                                            {
                                                var entry    = entries[entryindex];
                                                var fullname = entry.FullName;
                                                if (fullname.StartsWith(sptfolder))
                                                {
                                                    var part = fullname.Substring(sptfolder.Length);
                                                    var node = mani.AddOrGetItem(part);
                                                    if (node.Item == null)
                                                    {
                                                        CapsResManifestItem item;
                                                        item      = new CapsResManifestItem(node);
                                                        node.Item = item;
                                                    }
                                                }
                                                ++entryindex;
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            error = e;
                                            break;
                                        }
                                    } while (false);
                                    if (error != null)
                                    {
                                        if (i == retryTimes - 1)
                                        {
                                            PlatDependant.LogError(error);
                                        }
                                        else
                                        {
                                            PlatDependant.LogError(error);
                                            PlatDependant.LogInfo("Need Retry " + i);
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        sptfolder = ThreadSafeValues.AppStreamingAssetsPath + "/spt/";
                        try
                        {
                            var files = PlatDependant.GetAllFiles(sptfolder);
                            if (files != null && files.Length > 0)
                            {
                                for (int i = 0; i < files.Length; ++i)
                                {
                                    var file = files[i];
                                    var part = file.Substring(sptfolder.Length).Replace('\\', '/');
                                    var node = mani.AddOrGetItem(part);
                                    if (node.Item == null)
                                    {
                                        CapsResManifestItem item;
                                        item      = new CapsResManifestItem(node);
                                        node.Item = item;
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            PlatDependant.LogError(e);
                        }
                    }

                    mani.TrimExcess();
                    _RuntimeRawManifest = mani;
                    _RuntimeManifestReady.Set();
                    SaveManifest(mani, maniPath);
                }
            }
            finally
            {
                _RuntimeManifestReady.Set();
                _RuntimeManifestTaskIdle.Set();
            }
        }
示例#5
0
        public static CapsResManifest LoadManifest(string file)
        {
            CapsResManifest mani = new CapsResManifest();

            if (PlatDependant.IsFileExist(file))
            {
                using (var sr = PlatDependant.OpenReadText(file))
                {
                    if (sr != null)
                    {
                        List <CapsResManifestNode> nodeStack = new List <CapsResManifestNode>();
                        var root = new CapsResManifestNode(mani);
                        mani.Root = root;
                        nodeStack.Add(root);

                        int nxtChar = -1;
                        while ((nxtChar = sr.Peek()) > 0)
                        {
                            int lvl = 0;
                            while (nxtChar == '*')
                            {
                                sr.Read();
                                ++lvl;
                                nxtChar = sr.Peek();
                            }
                            string ppath = sr.ReadLine();
                            if (string.IsNullOrEmpty(ppath))
                            {
                                continue;
                            }

                            if (nodeStack.Count > lvl)
                            {
                                var last = nodeStack[nodeStack.Count - 1];
                                if (last.Children == null || last.Children.Count <= 0)
                                {
                                    CapsResManifestItem item;
                                    item      = new CapsResManifestItem(last);
                                    last.Item = item;
                                }

                                nodeStack.RemoveRange(lvl, nodeStack.Count - lvl);
                            }

                            {
                                var last = nodeStack[nodeStack.Count - 1];
                                if (last.Children == null)
                                {
                                    last.Children = new SortedList <string, CapsResManifestNode>();
                                }
                                var child = new CapsResManifestNode(last, ppath);
                                last.Children[ppath] = child;
                                nodeStack.Add(child);
                            }
                        }

                        if (nodeStack.Count > 1)
                        {
                            var last = nodeStack[nodeStack.Count - 1];
                            CapsResManifestItem item;
                            item      = new CapsResManifestItem(last);
                            last.Item = item;
                        }

                        mani.TrimExcess();
                    }
                }
            }
            return(mani);
        }
        public void Prepare(string output)
        {
            _Output = output;
            _OldMap.Clear();
            _NewMap.Clear();
            _FullSet.Clear();

            if (!string.IsNullOrEmpty(output))
            {
                var cachefile = output + "/res/inatlas.txt";
                if (PlatDependant.IsFileExist(cachefile))
                {
                    try
                    {
                        string json = "";
                        using (var sr = PlatDependant.OpenReadText(cachefile))
                        {
                            json = sr.ReadToEnd();
                        }
                        var jo  = new JSONObject(json);
                        var joc = jo["tex"] as JSONObject;
                        if (joc != null && joc.type == JSONObject.Type.OBJECT)
                        {
                            for (int i = 0; i < joc.list.Count; ++i)
                            {
                                var key = joc.keys[i];
                                var val = joc.list[i].str;
                                _OldMap[key] = val;
                            }
                        }
                    }
                    catch { }
                }
            }

            var assets = AssetDatabase.GetAllAssetPaths();

            if (assets != null)
            {
                for (int i = 0; i < assets.Length; ++i)
                {
                    var asset = assets[i];
                    if (asset.EndsWith(".spriteatlas"))
                    {
                        var atlas = AssetDatabase.LoadAssetAtPath <UnityEngine.U2D.SpriteAtlas>(asset);
                        if (atlas)
                        {
                            var name   = atlas.tag;
                            var packed = UnityEditor.U2D.SpriteAtlasExtensions.GetPackables(atlas);
                            if (packed != null)
                            {
                                for (int j = 0; j < packed.Length; ++j)
                                {
                                    var item = packed[j];
                                    var path = AssetDatabase.GetAssetPath(item);
                                    if (!string.IsNullOrEmpty(path))
                                    {
                                        _NewMap[path] = name;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            foreach (var kvp in _OldMap)
            {
                var key = kvp.Key;
                var val = kvp.Value;
                if (!_NewMap.ContainsKey(key) || _NewMap[key] != val)
                {
                    _FullSet.Add(key);
                }
            }
            foreach (var kvp in _NewMap)
            {
                var key = kvp.Key;
                var val = kvp.Value;
                if (!_OldMap.ContainsKey(key) || _OldMap[key] != val)
                {
                    _FullSet.Add(key);
                }
            }
        }