Exemple #1
0
        static IsolatedPrefs()
        {
            string json = null;
            string file = GetIsolatedPath() + "/iprefs.txt";

            if (PlatDependant.IsFileExist(file))
            {
                try
                {
                    using (var sr = PlatDependant.OpenReadText(file))
                    {
                        json = sr.ReadToEnd();
                    }
                    if (!string.IsNullOrEmpty(json))
                    {
                        try
                        {
                            _Dict = new JSONObject(json);
                        }
                        catch (Exception e)
                        {
                            LogError(e);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogError(e);
                }
            }
        }
        public static void Init()
        {
#if UNITY_EDITOR
            try
            {
#endif
            if (object.ReferenceEquals(L, null))
            {
                L = new LuaState();
                for (int i = 0; i < LuaExLibs.InitFuncs.Count; ++i)
                {
                    var func = LuaExLibs.InitFuncs[i].InitFunc;
                    if (func != null)
                    {
                        try
                        {
                            func(L);
                        }
                        catch (Exception e)
                        {
                            PlatDependant.LogError(e);
                        }
                    }
                }
            }
#if UNITY_EDITOR
        }

        catch (DllNotFoundException e)
        {
            Debug.LogException(e);
            LuaDllPostprocessor.LuaDllMissing = true;
        }
#endif
        }
        public static Dictionary <string, int> ParseResVersion(string verfile)
        {
            Dictionary <string, int> versions = new Dictionary <string, int>();

            if (PlatDependant.IsFileExist(verfile))
            {
                try
                {
                    using (var sr = PlatDependant.OpenReadText(verfile))
                    {
                        while (true)
                        {
                            var line = sr.ReadLine();
                            if (line == null)
                            {
                                break;
                            }
                            if (line != "")
                            {
                                var parts = line.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                                if (parts != null && parts.Length >= 2)
                                {
                                    var reskey = parts[0];
                                    if (!string.IsNullOrEmpty(reskey))
                                    {
                                        int partver = 0;
                                        if (int.TryParse(parts[1], out partver))
                                        {
                                            if (versions.ContainsKey(reskey))
                                            {
                                                PlatDependant.LogWarning("Res version record duplicated key: " + reskey);
                                            }
                                            versions[reskey] = partver;
                                        }
                                        else
                                        {
                                            PlatDependant.LogWarning("Res version num is invalid in: " + line);
                                        }
                                    }
                                    else
                                    {
                                        PlatDependant.LogWarning("Res version key is invalid in: " + line);
                                    }
                                }
                                else
                                {
                                    PlatDependant.LogWarning("Res version record line is invalid: " + line);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    PlatDependant.LogError(e);
                }
            }
            return(versions);
        }
Exemple #4
0
                public override IEnumerator LoadAsync(CoroutineTasks.CoroutineWork req, Type type)
                {
                    var holdhandle = Hold();

                    try
                    {
                        if (!Prefab)
                        {
                            if (ManiItem != null && DepBundles.Count > 0)
                            {
                                var bi = DepBundles[DepBundles.Count - 1];
                                if (bi != null && bi.Bundle != null)
                                {
                                    var path = ConcatAssetPath();
                                    while (AsyncWorkTimer.Check())
                                    {
                                        yield return(null);
                                    }

                                    AssetBundleRequest raw = null;
                                    try
                                    {
                                        raw = bi.Bundle.LoadAssetAsync <GameObject>(path);
                                    }
                                    catch (Exception e)
                                    {
                                        PlatDependant.LogError(e);
                                    }
                                    if (raw != null)
                                    {
                                        yield return(raw);

                                        var asset = raw.asset as GameObject;

                                        if (!Prefab)
                                        {
                                            Holder = null;
                                            Prefab = asset;
                                        }
                                    }
                                }
                            }
                        }
                        if (Prefab)
                        {
                            if (!Holder)
                            {
                                Holder           = Prefab.AddComponent <CapsPrefabHolder>();
                                Holder.AssetInfo = this;
                            }
                        }
                        req.Result = Prefab;
                    }
                    finally
                    {
                        GC.KeepAlive(holdhandle);
                    }
                }
Exemple #5
0
 public string Format(string format, object arg, IFormatProvider provider)
 {
     if (format == null)
     {
         if (arg is IFormattable)
         {
             return(((IFormattable)arg).ToString(format, provider));
         }
         return(arg.ToString());
     }
     else
     {
         if (format.StartsWith("`cnt`"))
         {
             // Examples:
             // string.Format(Instance, "Look! {1} {0:`cnt`is/are} {0:`cnt`a} {0:`cnt`child/children}", 1, "There")
             // string.Format(Instance, "Look! {1} {0:`cnt`is/are} {0:`cnt`a} {0:`cnt`child/children}", 10, "There")
             string sub   = format.Substring("`cnt`".Length);
             var    words = sub.Split(_WordSplitChars, StringSplitOptions.RemoveEmptyEntries);
             if (words.Length > 0)
             {
                 double cnt = 0;
                 try
                 {
                     cnt = Convert.ToDouble(arg);
                     if (Math.Abs(cnt) > 1)
                     {
                         if (words.Length > 1)
                         {
                             return(words[1]);
                         }
                     }
                     else
                     {
                         return(words[0]);
                     }
                 }
                 catch (Exception e)
                 {
                     PlatDependant.LogError(e);
                 }
             }
             if (arg is IFormattable)
             {
                 return(((IFormattable)arg).ToString("", provider)); // invalid format. so we set format to "" and call default formatter.
             }
             return(arg.ToString());
         }
         else
         {
             if (arg is IFormattable)
             {
                 return(((IFormattable)arg).ToString(format, provider));
             }
             return(arg.ToString());
         }
     }
 }
Exemple #6
0
        private static string LoadInstallID()
        {
#if UNITY_EDITOR || !UNITY_ENGINE && !UNITY_5_3_OR_NEWER
            string capid = null;
#if UNITY_ENGINE || UNITY_5_3_OR_NEWER
            string capidfile = "EditorOutput/Runtime/capid.txt";
#else
            string capidfile = "./runtime/capid.txt";
#endif
            if (PlatDependant.IsFileExist(capidfile))
            {
                try
                {
                    using (var sr = PlatDependant.OpenReadText(capidfile))
                    {
                        capid = sr.ReadLine().Trim();
                    }
                }
                catch (Exception e)
                {
                    LogError(e);
                }
            }
            if (string.IsNullOrEmpty(capid))
            {
                capid = Guid.NewGuid().ToString("N");
                try
                {
                    using (var sw = PlatDependant.OpenWriteText(capidfile))
                    {
                        sw.WriteLine(capid);
                    }
                }
                catch (Exception e)
                {
                    LogError(e);
                }
            }
            return(capid);
#else
            string capid = null;
            if (PlayerPrefs.HasKey("___Pref__CapID"))
            {
                capid = PlayerPrefs.GetString("___Pref__CapID");
            }
            if (string.IsNullOrEmpty(capid))
            {
                capid = Guid.NewGuid().ToString("N");
                PlayerPrefs.SetString("___Pref__CapID", capid);
                PlayerPrefs.Save();
            }
            return(capid);
#endif
        }
Exemple #7
0
        public static Dictionary <string, string> LoadConfig(string file)
        {
            Dictionary <string, string> config;
            var error = LoadConfig(file, out config);

            if (error != null)
            {
                PlatDependant.LogError(error);
            }
            return(config);
        }
                private IEnumerator LoadMainAssetAsync(CoroutineTasks.CoroutineWork req)
                {
                    if (ManiItem != null && DepBundles.Count > 0)
                    {
                        var bi = DepBundles[DepBundles.Count - 1];
                        if (bi != null && bi.Bundle != null)
                        {
                            var path = ConcatAssetPath();

                            while (AsyncWorkTimer.Check())
                            {
                                yield return(null);
                            }

                            AssetBundleRequest rawreq = null;
                            try
                            {
                                rawreq = bi.Bundle.LoadAssetAsync(path);
                            }
                            catch (Exception e)
                            {
                                PlatDependant.LogError(e);
                            }
                            if (rawreq != null)
                            {
                                yield return(rawreq);

                                var asset = rawreq.asset;
                                if (asset is Texture2D)
                                {
                                    rawreq = null;
                                    try
                                    {
                                        rawreq = bi.Bundle.LoadAssetAsync(path, typeof(Sprite));
                                    }
                                    catch (Exception e)
                                    {
                                        PlatDependant.LogError(e);
                                    }
                                    if (rawreq != null)
                                    {
                                        yield return(rawreq);

                                        if (rawreq.asset)
                                        {
                                            asset = rawreq.asset;
                                        }
                                    }
                                }
                                req.Result = asset;
                            }
                        }
                    }
                }
Exemple #9
0
        public static object GetExValType(object val, out int vtype)
        {
            if (val == null || val is UnityEngine.Object)
            {
                vtype = 4;
                return(val);
            }
            if (val is string)
            {
                vtype = 3;
                return(val);
            }
            if (val is bool)
            {
                vtype = 0;
                return(val);
            }
            if (val is double)
            {
                vtype = 2;
                return(val);
            }
            if (val is int)
            {
                vtype = 1;
                return(val);
            }
            if (val.IsObjIConvertible())
            {
                try
                {
                    double d = Convert.ToDouble(val);
                    int    i = (int)d;
                    if (((double)i) == d)
                    {
                        vtype = 1;
                        return(i);
                    }
                    else
                    {
                        vtype = 2;
                        return(d);
                    }
                }
                catch (Exception e)
                {
                    PlatDependant.LogError(e);
                }
            }

            vtype = -1;
            return(val);
        }
        public static void LoadLib(string lib)
        {
            if (!string.IsNullOrEmpty(lib))
            {
                _LoadedLibs.GetOrAdd(lib, lib =>
                {
                    var info = new LibEntryPointInfo();

                    var assemblyName   = new AssemblyName();
                    assemblyName.Name  = "CodeEmit_LibEntry_" + lib;
                    var asmbuilder     = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
                    var codeEmitModule = asmbuilder.DefineDynamicModule(assemblyName.Name);
                    var typebuilder    = codeEmitModule.DefineType(assemblyName.Name, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Abstract | TypeAttributes.Sealed, typeof(object));

                    var dllname = lib;
                    if (Environment.OSVersion.Platform == PlatformID.Unix)
                    {
                        // TODO: on higher version .NET Core, we should consider MacOS X.
                        dllname = AppDomain.CurrentDomain.BaseDirectory + "/lib" + lib + ".so";
                    }
//#if UNITY_EDITOR_OSX
//                    var epLoad = "CapsPluginLoad";
//                    var epUnload = "CapsPluginUnload";
//#else
                    var epLoad   = "UnityPluginLoad";
                    var epUnload = "UnityPluginUnload";
//#endif
                    var mbuilder_load = typebuilder.DefineMethod(epLoad, MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl, typeof(void), new[] { typeof(IUnityInterfaces).MakeByRefType() });
                    var dllimport     = new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new[] { typeof(string) }), new[] { dllname });
                    mbuilder_load.SetCustomAttribute(dllimport);

                    var mbuilder_unload = typebuilder.DefineMethod(epUnload, MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl, typeof(void), new Type[0]);
                    //var dllimport = new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new[] { typeof(string) }), new[] { dllname });
                    mbuilder_unload.SetCustomAttribute(dllimport);

                    var createdtype = typebuilder.CreateType();
                    info.OnLoad     = (UnityPluginLoadDel)Delegate.CreateDelegate(typeof(UnityPluginLoadDel), createdtype.GetMethod(epLoad));
                    info.OnUnload   = (UnityPluginUnloadDel)Delegate.CreateDelegate(typeof(UnityPluginUnloadDel), createdtype.GetMethod(epUnload));
                    try
                    {
                        info.OnLoad(ref UnityInterfaces);
                    }
                    catch (EntryPointNotFoundException) { }
                    catch (Exception e)
                    {
                        PlatDependant.LogError(e);
                    }
                    return(info);
                });
                _LoadedLibsSeq.Push(lib);
            }
        }
Exemple #11
0
            private void Close()
            {
                _IsolatedIDMutex.WaitOne();
                try
                {
                    if (_IsolatedIDHolder != null)
                    {
                        _IsolatedIDHolder.Dispose();
                        _IsolatedIDHolder = null;
                    }
#if UNITY_ENGINE || UNITY_5_3_OR_NEWER
                    var file = Application.persistentDataPath + "/iid.txt";
#else
                    var file = "./runtime/iid.txt";
#endif
                    int instanceid = 0;
                    if (PlatDependant.IsFileExist(file))
                    {
                        try
                        {
                            using (var sr = PlatDependant.OpenReadText(file))
                            {
                                var index = sr.ReadLine();
                                int.TryParse(index, out instanceid);
                            }
                        }
                        catch (Exception e)
                        {
                            LogError(e);
                        }
                    }
                    if (instanceid <= 1)
                    {
                        PlatDependant.DeleteFile(file);
                    }
                    else
                    {
                        using (var sw = PlatDependant.OpenWriteText(file))
                        {
                            sw.Write(instanceid - 1);
                        }
                    }
                }
                catch (Exception e)
                {
                    LogError(e);
                }
                finally
                {
                    _IsolatedIDMutex.ReleaseMutex();
                }
            }
Exemple #12
0
        public static string GetLangValue(string key, params object[] args)
        {
            string format = null, result = null;

            if (key == null)
            {
                PlatDependant.LogError("Language Converter - cannot convert null key.");
            }
            else
            {
                if (!LangDict.TryGetValue(key, out format))
                {
                    PlatDependant.LogError("Language Converter - cannot find key: " + key);
                }
                else
                {
                    if (format == null)
                    {
                        PlatDependant.LogError("Language Converter - null record for key: " + key);
                    }
                    else
                    {
                        if (args != null && args.Length > 0)
                        {
                            try
                            {
                                result = string.Format(CapsLangFormatter.Instance, format, args);
                            }
                            catch (Exception e)
                            {
                                PlatDependant.LogError(e);
                                System.Text.StringBuilder sbmess = new System.Text.StringBuilder();
                                sbmess.AppendLine("Language Converter - format failed.");
                                sbmess.Append("key: ");
                                sbmess.AppendLine(key);
                                sbmess.Append("format: ");
                                sbmess.AppendLine(format);
                                sbmess.Append("args: cnt ");
                                sbmess.AppendLine(args.Length.ToString());
                                for (int i = 0; i < args.Length; ++i)
                                {
                                    sbmess.AppendLine((args[i] ?? "null").ToString());
                                }
                                PlatDependant.LogError(sbmess);
                            }
                        }
                    }
                }
            }
            return(result ?? format ?? key);
        }
Exemple #13
0
        public static Dictionary <string, object> LoadFullConfig(string file)
        {
            if (string.IsNullOrEmpty(file))
            {
                PlatDependant.LogError("LoadConfig - filename is empty");
            }
            else
            {
                try
                {
#if UNITY_ENGINE || UNITY_5_3_OR_NEWER
                    TextAsset txt = ResManager.LoadResDeep(file, typeof(TextAsset)) as TextAsset;
                    if (txt == null)
                    {
                        PlatDependant.LogError("LoadConfig - cannot load file: " + file);
                    }
                    else
                    {
                        JSONObject json = new JSONObject(txt.text);
                        if (json.IsObject)
                        {
                            return(json.ToObject() as Dictionary <string, object>);
                        }
                    }
#else
                    var text = LoadText(file);
                    if (string.IsNullOrEmpty(text))
                    {
                        PlatDependant.LogError("LoadConfig - cannot load file: " + file);
                    }
                    else
                    {
                        JSONObject json = new JSONObject(text);
                        if (json.IsObject)
                        {
                            return(json.ToObject() as Dictionary <string, object>);
                        }
                    }
#endif
                }
                catch (Exception e)
                {
                    PlatDependant.LogError(e);
                }
            }
            return(null);
        }
Exemple #14
0
 public static string LoadText(string path)
 {
     using (var stream = LoadFile(path))
     {
         if (stream != null)
         {
             try
             {
                 var sr = new System.IO.StreamReader(stream);
                 return(sr.ReadToEnd());
             }
             catch (Exception e)
             {
                 PlatDependant.LogError(e);
             }
         }
     }
     return(null);
 }
Exemple #15
0
        internal static void HandleEvents()
        {
            Action act = null;

            while (ActionQueue.TryDequeue(out act))
            {
                if (act != null)
                {
                    try
                    {
                        act();
                    }
                    catch (Exception e)
                    {
                        PlatDependant.LogError(e);
                    }
                }
            }
        }
        public static void UnloadLib(string lib)
        {
            LibEntryPointInfo info;

            if (_LoadedLibs.TryRemove(lib, out info))
            {
                if (info != null && info.OnUnload != null)
                {
                    try
                    {
                        info.OnUnload();
                    }
                    catch (EntryPointNotFoundException) { }
                    catch (Exception e)
                    {
                        PlatDependant.LogError(e);
                    }
                }
            }
        }
Exemple #17
0
        public static string FindFileRelative(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            if (path[0] != '\\' && path[0] != '/')
            {
                path = "/" + path;
            }
            var spath = ThreadSafeValues.UpdatePath + path;

            if (PlatDependant.IsFileExist(spath))
            {
                return(spath);
            }
            spath = ThreadSafeValues.AppStreamingAssetsPath + path;
            if (PlatDependant.IsFileExist(spath))
            {
                return(spath);
            }
            return(null);
        }
Exemple #18
0
 public static void Save()
 {
     try
     {
         string json = _Dict.ToString(true);
         string file = GetIsolatedPath() + "/iprefs.txt";
         if (string.IsNullOrEmpty(json))
         {
             PlatDependant.DeleteFile(file);
         }
         else
         {
             using (var sw = PlatDependant.OpenWriteText(file))
             {
                 sw.Write(json);
             }
         }
     }
     catch (Exception e)
     {
         LogError(e);
     }
 }
Exemple #19
0
        public static System.IO.Stream LoadFileRelative(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(null);
            }
            if (path[0] != '\\' && path[0] != '/')
            {
                path = "/" + path;
            }
            var spath = ThreadSafeValues.UpdatePath + path;

            try
            {
                if (PlatDependant.IsFileExist(spath))
                {
                    return(PlatDependant.OpenRead(spath));
                }
            }
            catch (Exception e)
            {
                PlatDependant.LogError(e);
            }
            spath = ThreadSafeValues.AppStreamingAssetsPath + path;
            try
            {
                if (PlatDependant.IsFileExist(spath))
                {
                    return(PlatDependant.OpenRead(spath));
                }
            }
            catch (Exception e)
            {
                PlatDependant.LogError(e);
            }
            return(null);
        }
Exemple #20
0
        static IsolatedPrefs()
        {
            string json = null;
            string file = GetIsolatedPath() + "/iprefs.txt";

            if (PlatDependant.IsFileExist(file))
            {
                try
                {
                    using (var sr = PlatDependant.OpenReadText(file))
                    {
                        json = sr.ReadToEnd();
                    }
                    if (!string.IsNullOrEmpty(json))
                    {
                        JsonUtility.FromJsonOverwrite(json, _Dict);
                    }
                }
                catch (Exception e)
                {
                    LogError(e);
                }
            }
        }
Exemple #21
0
            public static string CheckModPathSafe(string path)
            {
                string found = null;
                Func <string, bool> checkFile = file =>
                {
                    bool exist = PlatDependant.IsFileExist(file);
                    if (exist)
                    {
                        found = file;
                        return(true);
                    }
                    return(false);
                };

                var dflags = _RuntimeCache.DFlags;

                for (int i = dflags.Count - 1; i >= 0; --i)
                {
                    var    dflag = dflags[i];
                    string package;
                    _RuntimeCache.ModToPackage.TryGetValue(dflag, out package);
                    if (!string.IsNullOrEmpty(package))
                    {
#if EDITOR_LOAD_RAW_RES
                        {
                            var realpath = "Packages/" + package + "/Raw/" + path;
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
                        }
#endif
                        {
                            var realpath = "Packages/" + package + "/" + path;
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
                        }
                    }
#if EDITOR_LOAD_RAW_RES
                    {
                        var realpath = "Assets/Mods/" + dflag + "/Raw/" + path;
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
                    }
#endif
                    {
                        var realpath = "Assets/Mods/" + dflag + "/" + path;
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
                    }
                }
                var cflags = _RuntimeCache.CriticalMods;
                for (int i = cflags.Count - 1; i >= 0; --i)
                {
                    var    dflag = cflags[i];
                    string package;
                    _RuntimeCache.ModToPackage.TryGetValue(dflag, out package);
                    if (!string.IsNullOrEmpty(package))
                    {
#if EDITOR_LOAD_RAW_RES
                        {
                            var realpath = "Packages/" + package + "/Raw/" + path;
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
                        }
#endif
                        {
                            var realpath = "Packages/" + package + "/" + path;
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
                        }
                    }
#if EDITOR_LOAD_RAW_RES
                    {
                        var realpath = "Assets/Mods/" + dflag + "/Raw/" + path;
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
                    }
#endif
                    {
                        var realpath = "Assets/Mods/" + dflag + "/" + path;
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
                    }
                }
#if EDITOR_LOAD_RAW_RES
                {
                    var realpath = "Assets/Raw/" + path;
                    if (checkFile(realpath))
                    {
                        return(found);
                    }
                }
#endif
                {
                    var realpath = "Assets/" + path;
                    if (checkFile(realpath))
                    {
                        return(found);
                    }
                }
                return(found);
            }
            public int CountWorkStep()
            {
                _PackageVer     = 0;
                _ObbVer         = 0;
                _PackageResKeys = null;
                _ObbResKeys     = null;
                _RunningVer     = null;
                _OldRunningKeys = null;
                _PendingFiles   = null;
                _UpdateFiles    = null;

                // Parse the ver num in the app package.
                if (Application.streamingAssetsPath.Contains("://"))
                {
                    if (Application.platform == RuntimePlatform.Android)
                    {
                        { // Apk ver.
                            var arch = ResManager.AndroidApkZipArchive;
                            if (arch != null)
                            {
                                try
                                {
                                    var entry = arch.GetEntry("assets/res/version.txt");
                                    if (entry != null)
                                    {
                                        using (var stream = entry.Open())
                                        {
                                            using (var sr = new System.IO.StreamReader(stream))
                                            {
                                                var strver = sr.ReadLine();
                                                int.TryParse(strver, out _PackageVer);
                                            }
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    PlatDependant.LogError(e);
                                }
                            }
                        }
                        { // Obb ver.
                            var arch = ResManager.ObbZipArchive;
                            if (arch != null)
                            {
                                try
                                {
                                    var entry = arch.GetEntry("res/version.txt");
                                    if (entry != null)
                                    {
                                        using (var stream = entry.Open())
                                        {
                                            using (var sr = new System.IO.StreamReader(stream))
                                            {
                                                var strver = sr.ReadLine();
                                                int.TryParse(strver, out _ObbVer);
                                            }
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    PlatDependant.LogError(e);
                                }
                            }
                        }
                    }
                    else
                    {
                        var vertxt = Resources.Load <TextAsset>("version");
                        if (vertxt != null)
                        {
                            try
                            {
                                var strver = vertxt.text;
                                int.TryParse(strver, out _PackageVer);
                            }
                            catch (Exception e)
                            {
                                PlatDependant.LogError(e);
                            }
                        }
                    }
                }
                else
                {
                    var path = Application.streamingAssetsPath + "/res/version.txt";
                    if (PlatDependant.IsFileExist(path))
                    {
                        using (var sr = PlatDependant.OpenReadText(path))
                        {
                            var strver = sr.ReadLine();
                            int.TryParse(strver, out _PackageVer);
                        }
                    }
                }

                // Parse the ver num running now.
                if (_PackageVer > 0 || _ObbVer > 0)
                {
                    var uverpath = ThreadSafeValues.UpdatePath + "/res/ver.txt";
                    if (PlatDependant.IsFileExist(uverpath))
                    {
                        _RunningVer = ParseRunningResVersion();
                        if (_RunningVer.Count == 0)
                        {
                            _RunningVer = null;
                        }
                    }
                    else
                    {
                        // _RunningVer = null;
                        // this means: should delete all.
                    }
                }

                // Are all running versions newer than the app ver?
                bool IsAllRunningVersionNew = true;

                if (_PackageVer > 0 || _ObbVer > 0)
                {
                    if (_RunningVer == null)
                    {
                        IsAllRunningVersionNew = false;
                    }
                    else
                    {
                        var MaxAppVer = _PackageVer > _ObbVer ? _PackageVer : _ObbVer;
                        foreach (var kvpver in _RunningVer)
                        {
                            if (kvpver.Value < MaxAppVer)
                            {
                                IsAllRunningVersionNew = false;
                            }
                        }
                    }
                }

                int workcnt = 0;

                if (!IsAllRunningVersionNew)
                {
                    // Parse res keys in the app package.
                    if (Application.streamingAssetsPath.Contains("://"))
                    {
                        if (Application.platform == RuntimePlatform.Android)
                        {
                            if (_PackageVer > 0)
                            {
                                ResManager.SkipPending = true;
                                ResManager.SkipUpdate  = true;
                                ResManager.SkipObb     = true;
                                ResManager.SkipPackage = false;

                                _PackageResKeys = ParseRunningResKeys();
                                if (_PackageResKeys.Count == 0)
                                {
                                    _PackageResKeys = null;
                                }
                            }
                            if (_ObbVer > 0)
                            {
                                ResManager.SkipPending = true;
                                ResManager.SkipUpdate  = true;
                                ResManager.SkipPackage = true;
                                ResManager.SkipObb     = false;

                                _ObbResKeys = ParseRunningResKeys();
                                if (_ObbResKeys.Count == 0)
                                {
                                    _ObbResKeys = null;
                                }
                            }
                        }
                        else
                        {
                            _PackageResKeys = new List <string>(_RunningVer.Keys);
                        }
                    }
                    else
                    {
                        ResManager.SkipPending = true;
                        ResManager.SkipUpdate  = true;
                        ResManager.SkipObb     = true;
                        ResManager.SkipPackage = false;

                        _PackageResKeys = ParseRunningResKeys();
                        if (_PackageResKeys.Count == 0)
                        {
                            _PackageResKeys = null;
                        }
                    }

                    if (_RunningVer != null)
                    {
                        // Check ver
                        IsAllRunningVersionNew = true;
                        if (_PackageResKeys != null)
                        {
                            for (int i = 0; i < _PackageResKeys.Count; ++i)
                            {
                                var key = _PackageResKeys[i];
                                if (_RunningVer.ContainsKey(key) && _RunningVer[key] < _PackageVer)
                                {
                                    IsAllRunningVersionNew = false;
                                    _OldRunningKeys        = _OldRunningKeys ?? new HashSet <string>();
                                    _OldRunningKeys.Add(key);
                                }
                            }
                            if (Application.platform == RuntimePlatform.Android && !IsAllRunningVersionNew && !ResManager.LoadAssetsFromApk)
                            {
                                try
                                {
                                    workcnt += ResManager.AndroidApkZipArchive.Entries.Count;
                                }
                                catch (Exception e)
                                {
                                    PlatDependant.LogError(e);
                                }
                            }
                        }
                        if (_ObbResKeys != null)
                        {
                            bool _ObbIsNew = false;
                            for (int i = 0; i < _ObbResKeys.Count; ++i)
                            {
                                var key = _ObbResKeys[i];
                                if (_RunningVer.ContainsKey(key) && _RunningVer[key] < _ObbVer)
                                {
                                    _ObbIsNew = true;
                                    IsAllRunningVersionNew = false;
                                    _OldRunningKeys        = _OldRunningKeys ?? new HashSet <string>();
                                    _OldRunningKeys.Add(key);
                                }
                            }
                            if (_ObbIsNew)
                            {
                                try
                                {
                                    workcnt += ResManager.ObbZipArchive.Entries.Count;
                                }
                                catch (Exception e)
                                {
                                    PlatDependant.LogError(e);
                                }
                            }
                        }
                    }
                }

                if (_RunningVer == null)
                {
                    if (Application.platform == RuntimePlatform.Android && !ResManager.LoadAssetsFromApk)
                    {
                        try
                        {
                            workcnt += ResManager.AndroidApkZipArchive.Entries.Count;
                        }
                        catch (Exception e)
                        {
                            PlatDependant.LogError(e);
                        }
                    }
                    if (_ObbVer > 0)
                    {
                        try
                        {
                            workcnt += ResManager.ObbZipArchive.Entries.Count;
                        }
                        catch (Exception e)
                        {
                            PlatDependant.LogError(e);
                        }
                    }
                }

                if (IsAllRunningVersionNew)
                {
                    // Check whether the EntrySceneBg is pending to be updated.
                    ResManager.SkipPackage = false;
                    ResManager.SkipObb     = false;
                    ResManager.SkipUpdate  = false;
                    ResManager.SkipPending = false;

                    List <string> entryPendingAbs = new List <string>();
                    if (PlatDependant.IsFileExist(ThreadSafeValues.UpdatePath + "/pending/res/ver.txt"))
                    {
                        CapsUnityMainBehav.LoadEntrySceneBg();
                        foreach (var kvploaded in ResManager.LoadedAssetBundles)
                        {
                            var abname = kvploaded.Key;
                            if (kvploaded.Value.RealName != null)
                            {
                                abname = kvploaded.Value.RealName;
                            }
                            string path = ThreadSafeValues.UpdatePath + "/pending/res/" + abname;
                            if (PlatDependant.IsFileExist(path))
                            {
                                entryPendingAbs.Add(abname);
                            }
                        }
                        CapsUnityMainBehav.UnloadEntrySceneBg();
                    }
                    ResManager.SkipPending = true;

                    if (entryPendingAbs.Count > 0)
                    {
                        for (int i = 0; i < entryPendingAbs.Count; ++i)
                        {
                            var abname = entryPendingAbs[i];
                            var src    = ThreadSafeValues.UpdatePath + "/pending/res/" + abname;
                            var dst    = ThreadSafeValues.UpdatePath + "/res/" + abname;
                            PlatDependant.MoveFile(src, dst);
                        }
                    }

                    _PendingFiles = PlatDependant.GetAllFiles(ThreadSafeValues.UpdatePath + "/pending/res/");
                    return(_PendingFiles.Length);
                }
                else
                {
                    ResManager.SkipPackage = false;
                    ResManager.SkipObb     = false;
                    ResManager.SkipUpdate  = true;
                    ResManager.SkipPending = true;

                    _PendingFiles = PlatDependant.GetAllFiles(ThreadSafeValues.UpdatePath + "/pending/res/");
                    _UpdateFiles  = PlatDependant.GetAllFiles(ThreadSafeValues.UpdatePath + "/res/");
                    return(workcnt + _PendingFiles.Length + _UpdateFiles.Length);
                }
            }
        public static TaskProgress UnzipAsync(string zip, string destdir)
        {
            var prog = new TaskProgress();

            try
            {
                if (IsFileExist(zip))
                {
                    var taskcnt    = Environment.ProcessorCount;
                    int entryIndex = 0;
                    int finishCnt  = 0;
                    Action <TaskProgress> UnzipWork = p =>
                    {
                        try
                        {
                            using (var stream = PlatDependant.OpenRead(zip))
                            {
                                using (var zipa = new ZipArchive(stream, ZipArchiveMode.Read))
                                {
                                    var entries = zipa.Entries;
                                    if (entries != null)
                                    {
                                        var index = System.Threading.Interlocked.Increment(ref entryIndex) - 1;
                                        if (index == 0)
                                        {
                                            prog.Total = entries.Count;
                                        }
                                        while (index < entries.Count)
                                        {
                                            System.Threading.Interlocked.Increment(ref prog.Length);

                                            try
                                            {
                                                var entry = entries[index];
                                                var name  = entry.FullName;
                                                var dest  = System.IO.Path.Combine(destdir, name);
                                                using (var srcs = entry.Open())
                                                {
                                                    using (var dsts = PlatDependant.OpenWrite(dest))
                                                    {
                                                        srcs.CopyTo(dsts);
                                                    }
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                LogError(e);
                                            }

                                            index = System.Threading.Interlocked.Increment(ref entryIndex) - 1;
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            LogError(e);
                            prog.Error = e.Message;
                        }
                        finally
                        {
                            if (System.Threading.Interlocked.Increment(ref finishCnt) >= taskcnt)
                            {
                                prog.Done = true;
                            }
                        }
                    };
                    for (int i = 0; i < taskcnt; ++i)
                    {
                        RunBackground(UnzipWork);
                    }
                    return(prog);
                }
            }
            catch (Exception e)
            {
                LogError(e);
                prog.Error = e.Message;
            }
            prog.Done = true;
            return(prog);
        }
                public override IEnumerator LoadAsync(CoroutineTasks.CoroutineWork req, Type type)
                {
                    var holdhandle = Hold();

                    try
                    {
                        while (AsyncWorkTimer.Check())
                        {
                            yield return(null);
                        }

                        if (MainType == null)
                        {
                            var mainwork = new CoroutineTasks.CoroutineWorkSingle();
                            mainwork.SetWork(LoadMainAssetAsync(mainwork));
                            mainwork.StartCoroutine();

                            while (true)
                            {
                                if (MainType != null)
                                {
                                    mainwork.Dispose();
                                    break;
                                }
                                if (mainwork.Done)
                                {
                                    var asset = mainwork.Result as Object;
                                    if (!asset)
                                    {
                                        MainType = typeof(object);
                                    }
                                    else
                                    {
                                        MainType = asset.GetType();
                                        TypedAssets[MainType] = new AssetRef()
                                        {
                                            Asset = new WeakReference(asset)
                                        };
                                    }
                                    if (MainType == typeof(object) || type == null || type.IsAssignableFrom(MainType))
                                    {
                                        req.Result = asset;
                                        yield break;
                                    }
                                }
                            }
                        }
                        if (MainType == typeof(object))
                        {
                            yield break;
                        }
                        else if (type == null || type.IsAssignableFrom(MainType))
                        {
                            type = MainType;
                        }

                        AssetRef rAsset;
                        if (TypedAssets.TryGetValue(type, out rAsset))
                        {
                            if (rAsset.Asset != null)
                            {
                                var asset = rAsset.Asset.GetWeakReference <Object>();
                                if (asset)
                                {
                                    req.Result = asset;
                                    yield break;
                                }
                            }
                        }

                        while (AsyncWorkTimer.Check())
                        {
                            yield return(null);
                        }
                        if (rAsset == null)
                        {
                            rAsset            = new AssetRef();
                            TypedAssets[type] = rAsset;
                        }
                        rAsset.Asset = null;

                        if (ManiItem != null && DepBundles.Count > 0)
                        {
                            var bi = DepBundles[DepBundles.Count - 1];
                            if (bi != null && bi.Bundle != null)
                            {
                                var path = ConcatAssetPath();

                                AssetBundleRequest reqraw = null;
                                try
                                {
                                    reqraw = bi.Bundle.LoadAssetAsync(path, type);
                                }
                                catch (Exception e)
                                {
                                    PlatDependant.LogError(e);
                                }
                                if (reqraw != null)
                                {
                                    yield return(reqraw);

                                    var asset = reqraw.asset;

                                    if (!asset)
                                    {
                                        yield break;
                                    }

                                    rAsset.Asset = new WeakReference(asset);
                                    req.Result   = asset;
                                }
                            }
                        }
                    }
                    finally
                    {
                        GC.KeepAlive(holdhandle);
                    }
                }
Exemple #25
0
            public static string CheckModPath(string path)
            {
                string found = null;
                Func <string, bool> checkFile = file =>
                {
                    bool exist = PlatDependant.IsFileExist(file);
                    if (exist)
                    {
#if EDITOR_LOADER_NO_CHECK
                        found = file;
#endif
                        if (found == null)
                        {
                            found = file;
                        }
                        else
                        {
                            Debug.LogWarning("Duplicated item: " + found + "\nReplaces: " + file);
                        }
                        return(true);
                    }
                    return(false);
                };

                var dflags = _RuntimeCache.DFlags;

                for (int i = dflags.Count - 1; i >= 0; --i)
                {
                    var    dflag = dflags[i];
                    string package;
                    _RuntimeCache.ModToPackage.TryGetValue(dflag, out package);
                    if (!string.IsNullOrEmpty(package))
                    {
#if EDITOR_LOAD_RAW_RES
                        {
                            var realpath = "Packages/" + package + "/Raw/" + path;
#if EDITOR_LOADER_NO_CHECK
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
#else
                            checkFile(realpath);
#endif
                        }
#endif
                        {
                            var realpath = "Packages/" + package + "/" + path;
#if EDITOR_LOADER_NO_CHECK
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
#else
                            checkFile(realpath);
#endif
                        }
                    }
#if EDITOR_LOAD_RAW_RES
                    {
                        var realpath = "Assets/Mods/" + dflag + "/Raw/" + path;
#if EDITOR_LOADER_NO_CHECK
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
#else
                        checkFile(realpath);
#endif
                    }
#endif
                    {
                        var realpath = "Assets/Mods/" + dflag + "/" + path;
#if EDITOR_LOADER_NO_CHECK
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
#else
                        checkFile(realpath);
#endif
                    }
                }
                var cflags = _RuntimeCache.CriticalMods;
                for (int i = cflags.Count - 1; i >= 0; --i)
                {
                    var    dflag = cflags[i];
                    string package;
                    _RuntimeCache.ModToPackage.TryGetValue(dflag, out package);
                    if (!string.IsNullOrEmpty(package))
                    {
#if EDITOR_LOAD_RAW_RES
                        {
                            var realpath = "Packages/" + package + "/Raw/" + path;
#if EDITOR_LOADER_NO_CHECK
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
#else
                            checkFile(realpath);
#endif
                        }
#endif
                        {
                            var realpath = "Packages/" + package + "/" + path;
#if EDITOR_LOADER_NO_CHECK
                            if (checkFile(realpath))
                            {
                                return(found);
                            }
#else
                            checkFile(realpath);
#endif
                        }
                    }
#if EDITOR_LOAD_RAW_RES
                    {
                        var realpath = "Assets/Mods/" + dflag + "/Raw/" + path;
#if EDITOR_LOADER_NO_CHECK
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
#else
                        checkFile(realpath);
#endif
                    }
#endif
                    {
                        var realpath = "Assets/Mods/" + dflag + "/" + path;
#if EDITOR_LOADER_NO_CHECK
                        if (checkFile(realpath))
                        {
                            return(found);
                        }
#else
                        checkFile(realpath);
#endif
                    }
                }
#if EDITOR_LOAD_RAW_RES
                {
                    var realpath = "Assets/Raw/" + path;
#if EDITOR_LOADER_NO_CHECK
                    if (checkFile(realpath))
                    {
                        return(found);
                    }
#else
                    checkFile(realpath);
#endif
                }
#endif
                {
                    var realpath = "Assets/" + path;
#if EDITOR_LOADER_NO_CHECK
                    if (checkFile(realpath))
                    {
                        return(found);
                    }
#else
                    checkFile(realpath);
#endif
                }
                return(found);
            }
Exemple #26
0
            public IsolatedIDFileHolder()
            {
                _IsolatedIDMutex.WaitOne();
                try
                {
#if UNITY_ENGINE || UNITY_5_3_OR_NEWER
                    var file  = Application.persistentDataPath + "/iid.txt";
                    var fileh = Application.persistentDataPath + "/iidh.txt";
#else
                    var file  = "./runtime/iid.txt";
                    var fileh = "./runtime/iidh.txt";
#endif
                    if (PlatDependant.IsFileExist(fileh))
                    {
                        bool shouldDeleteFile = true;
                        try
                        {
                            var hstream = System.IO.File.Open(fileh, System.IO.FileMode.Open, System.IO.FileAccess.Write, System.IO.FileShare.Read);
                            if (hstream == null)
                            {
                                shouldDeleteFile = false;
                            }
                            else
                            {
                                hstream.Dispose();
                            }
                        }
                        catch (Exception)
                        {
                            shouldDeleteFile = false;
                        }
                        if (shouldDeleteFile)
                        {
                            PlatDependant.DeleteFile(fileh);
                            PlatDependant.DeleteFile(file);
                        }
                    }
                    if (!PlatDependant.IsFileExist(fileh))
                    {
                        using (var sw = PlatDependant.OpenWriteText(fileh))
                        {
                            sw.Write(" ");
                        }
                    }
                    _IsolatedIDHolder = System.IO.File.Open(fileh, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
                    if (PlatDependant.IsFileExist(file))
                    {
                        try
                        {
                            using (var sr = PlatDependant.OpenReadText(file))
                            {
                                var index = sr.ReadLine();
                                int.TryParse(index, out _InstanceID);
                            }
                        }
                        catch (Exception e)
                        {
                            LogError(e);
                        }
                    }
                    using (var sw = PlatDependant.OpenWriteText(file))
                    {
                        sw.Write(_InstanceID + 1);
                    }
                }
                catch (Exception e)
                {
                    LogError(e);
                }
                finally
                {
                    _IsolatedIDMutex.ReleaseMutex();
                }

                PlatDependant.PreQuitting += Close;
            }
            public IEnumerator InitAsync(bool async)
            {
                if (_PackageVer > 0 || _ObbVer > 0)
                {
                    if (_RunningVer == null)
                    {
                        // delete all existing.
                        if (_PendingFiles != null)
                        {
                            for (int i = 0; i < _PendingFiles.Length; ++i)
                            {
                                if (async && AsyncWorkTimer.Check())
                                {
                                    yield return(null);
                                }
                                var file = _PendingFiles[i];
                                PlatDependant.DeleteFile(file);
                                ReportProgress("WorkingStepAdvance", null, 0);
                            }
                        }
                        if (_UpdateFiles != null)
                        {
                            for (int i = 0; i < _UpdateFiles.Length; ++i)
                            {
                                if (async && AsyncWorkTimer.Check())
                                {
                                    yield return(null);
                                }
                                var file = _UpdateFiles[i];
                                PlatDependant.DeleteFile(file);
                                ReportProgress("WorkingStepAdvance", null, 0);
                            }
                        }
                        if (Application.platform == RuntimePlatform.Android)
                        {
                            if (!ResManager.LoadAssetsFromApk)
                            {
                                var arch = ResManager.AndroidApkZipArchive;
                                if (arch != null)
                                {
                                    var entries = arch.Entries;
                                    for (int i = 0; i < entries.Count; ++i)
                                    {
                                        if (async && AsyncWorkTimer.Check())
                                        {
                                            yield return(null);
                                        }
                                        try
                                        {
                                            var entry = entries[i];
                                            var name  = entry.FullName;
                                            if (name.StartsWith("assets/res/") && name != "assets/res/version.txt")
                                            {
                                                // copy
                                                using (var src = entry.Open())
                                                {
                                                    using (var dst = PlatDependant.OpenWrite(ThreadSafeValues.UpdatePath + "/" + name.Substring("assets/".Length)))
                                                    {
                                                        src.CopyTo(dst);
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            PlatDependant.LogError(e);
                                        }
                                        ReportProgress("WorkingStepAdvance", null, 0);
                                    }
                                }
                            }
                            {
                                var arch = ResManager.ObbZipArchive;
                                if (arch != null)
                                {
                                    var entries = arch.Entries;
                                    for (int i = 0; i < entries.Count; ++i)
                                    {
                                        if (async && AsyncWorkTimer.Check())
                                        {
                                            yield return(null);
                                        }
                                        try
                                        {
                                            var entry = entries[i];
                                            var name  = entry.FullName;
                                            if (name.StartsWith("res/") && name != "res/version.txt")
                                            {
                                                if (!ResManager.LoadAssetsFromObb || entry.CompressedLength != entry.Length)
                                                {
                                                    // copy
                                                    using (var src = entry.Open())
                                                    {
                                                        using (var dst = PlatDependant.OpenWrite(ThreadSafeValues.UpdatePath + "/" + name))
                                                        {
                                                            src.CopyTo(dst);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            PlatDependant.LogError(e);
                                        }
                                        ReportProgress("WorkingStepAdvance", null, 0);
                                    }
                                }
                            }
                        }
                        // write version
                        var finalVersions = new Dictionary <string, int>();
                        if (_PackageResKeys != null)
                        {
                            for (int i = 0; i < _PackageResKeys.Count; ++i)
                            {
                                finalVersions[_PackageResKeys[i]] = _PackageVer;
                            }
                        }
                        if (_ObbResKeys != null)
                        {
                            for (int i = 0; i < _ObbResKeys.Count; ++i)
                            {
                                finalVersions[_ObbResKeys[i]] = _ObbVer;
                            }
                        }
                        var versionfile    = ThreadSafeValues.UpdatePath + "/res/ver.txt";
                        var versionfiletmp = versionfile + ".tmp";
                        using (var sw = PlatDependant.OpenWriteText(versionfiletmp))
                        {
                            foreach (var kvpver in finalVersions)
                            {
                                sw.Write(kvpver.Key);
                                sw.Write("|");
                                sw.Write(kvpver.Value);
                                sw.WriteLine();
                            }
                            sw.Flush();
                        }
                        PlatDependant.MoveFile(versionfiletmp, versionfile);
                        yield break;
                    }
                    else if (_OldRunningKeys != null && _OldRunningKeys.Count > 0)
                    {
                        // delete old existing.
                        if (_PendingFiles != null)
                        {
                            string pverfile     = ThreadSafeValues.UpdatePath + "/pending/res/ver.txt";
                            bool   pendingready = PlatDependant.IsFileExist(pverfile);
                            if (pendingready)
                            {
                                for (int i = 0; i < _PendingFiles.Length; ++i)
                                {
                                    if (async && AsyncWorkTimer.Check())
                                    {
                                        yield return(null);
                                    }
                                    var file = _PendingFiles[i];
                                    var part = file.Substring(ThreadSafeValues.UpdatePath.Length + "/pending/res/".Length);
                                    if (IsResFileOld(part))
                                    {
                                        PlatDependant.DeleteFile(file);
                                    }
                                    else
                                    {
                                        if (part != "ver.txt")
                                        {
                                            PlatDependant.MoveFile(file, ThreadSafeValues.UpdatePath + "/res/" + part);
                                        }
                                    }
                                    ReportProgress("WorkingStepAdvance", null, 0);
                                }
                                PlatDependant.DeleteFile(pverfile);
                            }
                            else
                            {
                                for (int i = 0; i < _PendingFiles.Length; ++i)
                                {
                                    if (async && AsyncWorkTimer.Check())
                                    {
                                        yield return(null);
                                    }
                                    var file = _PendingFiles[i];
                                    PlatDependant.DeleteFile(file);
                                    ReportProgress("WorkingStepAdvance", null, 0);
                                }
                            }
                        }
                        if (_UpdateFiles != null)
                        {
                            for (int i = 0; i < _UpdateFiles.Length; ++i)
                            {
                                if (async && AsyncWorkTimer.Check())
                                {
                                    yield return(null);
                                }
                                var file = _UpdateFiles[i];
                                var part = file.Substring(ThreadSafeValues.UpdatePath.Length + "/res/".Length);
                                if (IsResFileOld(part))
                                {
                                    PlatDependant.DeleteFile(file);
                                }
                                ReportProgress("WorkingStepAdvance", null, 0);
                            }
                        }
                        if (Application.platform == RuntimePlatform.Android)
                        {
                            if (!ResManager.LoadAssetsFromApk)
                            {
                                var arch = ResManager.AndroidApkZipArchive;
                                if (arch != null)
                                {
                                    var entries = arch.Entries;
                                    for (int i = 0; i < entries.Count; ++i)
                                    {
                                        if (async && AsyncWorkTimer.Check())
                                        {
                                            yield return(null);
                                        }
                                        try
                                        {
                                            var entry = entries[i];
                                            var name  = entry.FullName;
                                            if (name.StartsWith("assets/res/") && name != "assets/res/version.txt")
                                            {
                                                var part = name.Substring("assets/res/".Length);
                                                if (IsResFileOld(part))
                                                {
                                                    // copy
                                                    using (var src = entry.Open())
                                                    {
                                                        using (var dst = PlatDependant.OpenWrite(ThreadSafeValues.UpdatePath + "/res/" + part))
                                                        {
                                                            src.CopyTo(dst);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            PlatDependant.LogError(e);
                                        }
                                        ReportProgress("WorkingStepAdvance", null, 0);
                                    }
                                }
                            }
                            {
                                var arch = ResManager.ObbZipArchive;
                                if (arch != null)
                                {
                                    var entries = arch.Entries;
                                    for (int i = 0; i < entries.Count; ++i)
                                    {
                                        if (async && AsyncWorkTimer.Check())
                                        {
                                            yield return(null);
                                        }
                                        try
                                        {
                                            var entry = entries[i];
                                            var name  = entry.FullName;
                                            if (name.StartsWith("res/") && name != "res/version.txt")
                                            {
                                                if (!ResManager.LoadAssetsFromObb || entry.CompressedLength != entry.Length)
                                                {
                                                    var part = name.Substring("res/".Length);
                                                    if (IsResFileOld(part))
                                                    {
                                                        // copy
                                                        using (var src = entry.Open())
                                                        {
                                                            using (var dst = PlatDependant.OpenWrite(ThreadSafeValues.UpdatePath + "/" + name))
                                                            {
                                                                src.CopyTo(dst);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            PlatDependant.LogError(e);
                                        }
                                        ReportProgress("WorkingStepAdvance", null, 0);
                                    }
                                }
                            }
                        }
                        // write version
                        var finalVersions = new Dictionary <string, int>(_RunningVer);
                        if (_PackageResKeys != null)
                        {
                            for (int i = 0; i < _PackageResKeys.Count; ++i)
                            {
                                var key = _PackageResKeys[i];
                                if (_OldRunningKeys.Contains(key))
                                {
                                    finalVersions[key] = _PackageVer;
                                }
                            }
                        }
                        if (_ObbResKeys != null)
                        {
                            for (int i = 0; i < _ObbResKeys.Count; ++i)
                            {
                                var key = _ObbResKeys[i];
                                if (_OldRunningKeys.Contains(key))
                                {
                                    finalVersions[key] = _ObbVer;
                                }
                            }
                        }
                        var versionfile    = ThreadSafeValues.UpdatePath + "/res/ver.txt";
                        var versionfiletmp = versionfile + ".tmp";
                        using (var sw = PlatDependant.OpenWriteText(versionfiletmp))
                        {
                            foreach (var kvpver in finalVersions)
                            {
                                sw.Write(kvpver.Key);
                                sw.Write("|");
                                sw.Write(kvpver.Value);
                                sw.WriteLine();
                            }
                            sw.Flush();
                        }
                        PlatDependant.MoveFile(versionfiletmp, versionfile);
                        yield break;
                    }
                }

                // All running version is new
                // move pending update
                if (_PendingFiles != null)
                {
                    string pverfile     = ThreadSafeValues.UpdatePath + "/pending/res/ver.txt";
                    bool   pendingready = PlatDependant.IsFileExist(pverfile);
                    for (int i = 0; i < _PendingFiles.Length; ++i)
                    {
                        if (async && AsyncWorkTimer.Check())
                        {
                            yield return(null);
                        }
                        var file = _PendingFiles[i];
                        if (pendingready)
                        {
                            var part = file.Substring(ThreadSafeValues.UpdatePath.Length + "/pending/res/".Length);
                            if (part != "ver.txt")
                            {
                                PlatDependant.MoveFile(file, ThreadSafeValues.UpdatePath + "/res/" + part);
                            }
                        }
                        else
                        {
                            PlatDependant.DeleteFile(file);
                        }
                        ReportProgress("WorkingStepAdvance", null, 0);
                    }
                    PlatDependant.DeleteFile(pverfile);
                    if (_RunningVer != null && pendingready)
                    {
                        // write version
                        var versionfile    = ThreadSafeValues.UpdatePath + "/spt/ver.txt";
                        var versionfiletmp = versionfile + ".tmp";
                        using (var sw = PlatDependant.OpenWriteText(versionfiletmp))
                        {
                            foreach (var kvpver in _RunningVer)
                            {
                                sw.Write(kvpver.Key);
                                sw.Write("|");
                                sw.Write(kvpver.Value);
                                sw.WriteLine();
                            }
                            sw.Flush();
                        }
                        PlatDependant.MoveFile(versionfiletmp, versionfile);
                    }
                }
            }
        private static IEnumerator LoadFromResourceWork(CoroutineWork req, string name, Type type)
        {
            if (req.Result != null)
            {
                yield break;
            }
            while (AsyncWorkTimer.Check())
            {
                yield return(null);
            }
            if (!string.IsNullOrEmpty(name))
            {
                var ext = System.IO.Path.GetExtension(name);
                name = name.Substring(0, name.Length - ext.Length);

                string[] distributeFlags = GetValidDistributeFlags();
                if (distributeFlags != null)
                {
                    for (int i = distributeFlags.Length - 1; i >= 0; --i)
                    {
                        while (AsyncWorkTimer.Check())
                        {
                            yield return(null);
                        }

                        var             flag   = distributeFlags[i];
                        ResourceRequest rawreq = null;
                        if (type == null)
                        {
                            try
                            {
                                rawreq = Resources.LoadAsync("dist/" + flag + "/" + name);
                            }
                            catch (Exception e)
                            {
                                PlatDependant.LogError(e);
                            }
                        }
                        else
                        {
                            try
                            {
                                rawreq = Resources.LoadAsync("dist/" + flag + "/" + name, type);
                            }
                            catch (Exception e)
                            {
                                PlatDependant.LogError(e);
                            }
                        }
                        if (rawreq != null)
                        {
                            yield return(rawreq);

                            if (rawreq.asset != null)
                            {
                                req.Result = rawreq.asset;
                                if (type == null && rawreq.asset is Texture2D)
                                {
                                    rawreq = null;
                                    try
                                    {
                                        rawreq = Resources.LoadAsync("dist/" + flag + "/" + name, typeof(Sprite));
                                    }
                                    catch (Exception e)
                                    {
                                        PlatDependant.LogError(e);
                                    }
                                    if (rawreq != null)
                                    {
                                        yield return(rawreq);

                                        if (rawreq.asset != null)
                                        {
                                            req.Result = rawreq.asset;
                                        }
                                    }
                                }
                                yield break;
                            }
                        }
                    }
                }
                {
                    while (AsyncWorkTimer.Check())
                    {
                        yield return(null);
                    }
                    ResourceRequest rawreq = null;
                    if (type == null)
                    {
                        try
                        {
                            rawreq = Resources.LoadAsync(name);
                        }
                        catch (Exception e)
                        {
                            PlatDependant.LogError(e);
                        }
                    }
                    else
                    {
                        try
                        {
                            rawreq = Resources.LoadAsync(name, type);
                        }
                        catch (Exception e)
                        {
                            PlatDependant.LogError(e);
                        }
                    }
                    if (rawreq != null)
                    {
                        yield return(rawreq);

                        if (rawreq.asset != null)
                        {
                            req.Result = rawreq.asset;
                            if (type == null && rawreq.asset is Texture2D)
                            {
                                rawreq = null;
                                try
                                {
                                    rawreq = Resources.LoadAsync(name, typeof(Sprite));
                                }
                                catch (Exception e)
                                {
                                    PlatDependant.LogError(e);
                                }
                                if (rawreq != null)
                                {
                                    yield return(rawreq);

                                    if (rawreq.asset != null)
                                    {
                                        req.Result = rawreq.asset;
                                    }
                                }
                            }
                            yield break;
                        }
                    }
                }
            }
        }
        public static CapsModDesc GetDistributeDesc(string flag)
        {
            if (!string.IsNullOrEmpty(flag))
            {
#if !UNITY_EDITOR
                CapsModDesc cacheddesc;
                if (_LoadedDistributeDescs.TryGetValue(flag, out cacheddesc) && (cacheddesc != null || ReferenceEquals(cacheddesc, null)))
                {
                    return(cacheddesc);
                }
                try
                {
                    var udescdir  = ThreadSafeValues.UpdatePath + "/res/moddesc/";
                    var udescfile = udescdir + flag + ".md.txt";
                    if (PlatDependant.IsFileExist(udescfile))
                    {
                        using (var sr = PlatDependant.OpenReadText(udescfile))
                        {
                            var json = sr.ReadToEnd();
                            var desc = ScriptableObject.CreateInstance <CapsModDesc>();
                            JsonUtility.FromJsonOverwrite(json, desc);
                            if (desc.Mod != null)
                            {
                                _LoadedDistributeDescs[flag] = desc;
                                return(desc);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    PlatDependant.LogError(e);
                }
#endif
                var descs = Resources.LoadAll <CapsModDesc>("resdesc");
                if (descs != null)
                {
#if UNITY_EDITOR
                    try
                    {
#endif
                    for (int i = 0; i < descs.Length; ++i)
                    {
                        var desc = descs[i];
                        if (desc != null && desc.Mod == flag)
                        {
#if UNITY_EDITOR
                            return(uobj.Instantiate <CapsModDesc>(desc));
#else
                            _LoadedDistributeDescs[flag] = desc;
                            return(desc);
#endif
                        }
                    }
#if UNITY_EDITOR
                }
                finally
                {
                    for (int i = 0; i < descs.Length; ++i)
                    {
                        var desc = descs[i];
                        if (desc != null)
                        {
                            Resources.UnloadAsset(desc);
                        }
                    }
                }
#endif
                }
            }
#if !UNITY_EDITOR
            _LoadedDistributeDescs[flag] = null;
#endif
            return(null);
        }