Ejemplo n.º 1
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.º 2
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);
     }
 }
Ejemplo n.º 3
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;
            }
Ejemplo n.º 4
0
            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);
                    }
                }
            }