Ejemplo n.º 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);
                }
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
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
        }
Ejemplo n.º 4
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();
                }
            }
Ejemplo n.º 5
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);
                }
            }
        }
Ejemplo n.º 6
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 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);
        }
Ejemplo n.º 8
0
            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);
                }
            }