Example #1
0
        public async Task DownloadFile(string remotePath, string localPath = null, FileHandler handler = null)
        {
            try
            {
                var pathKey = localPath ?? remotePath;
                using (var s3Client = AWSS3Client)
                {
                    if (handler != null)
                    {
                        _fileHandlers[pathKey] = handler;
                    }
                    var request = new GetObjectRequest
                    {
                        BucketName = ZibaobaoBucket,
                        Key        = ConvertToRemotePath(remotePath)
                    };

                    try
                    {
                        using (var response = await s3Client.GetObjectAsync(request))
                        {
                            response.WriteObjectProgressEvent += Request_WriteObjectProgressEvent;
                            string content = null;
                            using (var stream = response.ResponseStream)
                            {
                                if (!string.IsNullOrEmpty(localPath))
                                {
                                    ZibaobaoLibContext.Instance.PersistentStorage.SaveContent(localPath, stream);
                                }
                                else
                                {
                                    using (var reader = new StreamReader(stream))
                                    {
                                        content = reader.ReadToEnd();
                                    }
                                }
                            }

                            if (_fileHandlers.ContainsKey(pathKey))
                            {
                                _fileHandlers[pathKey].OnFileAvailable(pathKey, content);
                                _fileHandlers.Remove(pathKey);
                            }
                            else
                            {
                                OnFileAvailable?.Invoke(this, new StringEventArgs(pathKey));
                            }
                        }
                    }
                    catch (AmazonS3Exception s3Exception)
                    {
                        X1LogHelper.Exception(s3Exception);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("=====ERROR ========");
            }
        }
 public void InitDB()
 {
     lock (_dbLock)
     {
         int reTry = 3;
         X1LogHelper.Log($"InitDB [{_dbName}] [{typeof(T).FullName}]");
         Exception ex = null;
         while (reTry-- > 0)
         {
             try
             {
                 Connection = PersistentDataHelper.Instance.GetConnection(_isGlobal, _dbName);
                 Connection?.CreateTable <T>();
                 return;
             }
             catch (Exception e)
             {
                 ex = e;
             }
         }
         X1LogHelper.Error("InitDB failed");
         if (ex != null)
         {
             X1LogHelper.Exception(ex);
         }
     }
 }
Example #3
0
        public async Task UploadString(string remotePath, string content)
        {
            try
            {
                using (var s3Client = AWSS3Client)
                {
                    remotePath = ConvertToRemotePath(remotePath);
                    var request = new PutObjectRequest
                    {
                        BucketName  = ZibaobaoBucket,
                        Key         = remotePath,
                        ContentBody = content
                    };

                    try
                    {
                        await s3Client.PutObjectAsync(request);
                    }
                    catch (Exception e)
                    {
                        X1LogHelper.Exception(e);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("=====ERROR ========");
            }
        }
Example #4
0
        public async Task AddBucket(string remotePath)
        {
            remotePath = ConvertToRemotePath(remotePath);
            using (var s3Client = AWSS3Client)
            {
                var listOfBuckets = await s3Client.ListBucketsAsync();

                bool found = false;
                foreach (S3Bucket bucket in listOfBuckets.Buckets)
                {
                    if (bucket.BucketName == remotePath)
                    {
                        found = true;
                        break;
                    }
                }
                if (found == false)
                {
                    try
                    {
                        var response = await s3Client.PutBucketAsync(new PutBucketRequest { BucketName = remotePath, BucketRegion = S3Region.APS1 });
                    }
                    catch (Exception e)
                    {
                        X1LogHelper.Exception(e);
                    }
                }
            }
        }
Example #5
0
        public App()
        {
            AppDomain.CurrentDomain.UnhandledException += (s, e) =>
            {
                X1LogHelper.HandlerGlobalException(e.ExceptionObject as Exception, s);
            };

            PlatformContext.Init("Desktop");
        }
Example #6
0
        void OnAuthError(object sender, AuthenticatorErrorEventArgs e)
        {
            var authenticator = sender as OAuth2Authenticator;

            if (authenticator != null)
            {
                authenticator.Completed -= OnAuthCompleted;
                authenticator.Error     -= OnAuthError;
            }
            X1LogHelper.Error(e.Message);
        }
 public void SaveText(string path, string text)
 {
     try
     {
         File.WriteAllText(path, text);
     }
     catch (Exception ex)
     {
         X1LogHelper.Error(ex.Message);
     }
 }
        public int Count(string sql = "")
        {
            if (string.IsNullOrEmpty(sql))
            {
                sql = TableName;
            }
            int count = QueryStatistics(sql, "count(1)");

            X1LogHelper.Verbose($"Count {sql} [{count}]");
            return(count);
        }
 static ShootingGame()
 {
     UnhandledException += (s, e) =>
     {
         X1LogHelper.Exception(e.Exception);
         if (Debugger.IsAttached)
         {
             Debugger.Break();
         }
         e.Handled = true;
     };
 }
Example #10
0
 public Stream LoadEntiry(string id)
 {
     try
     {
         return(LoadContent(GetEntityPath(id)));
     }
     catch (Exception ex)
     {
         X1LogHelper.Error(ex.Message);
     }
     return(null);
 }
 public void Dispose()
 {
     try
     {
         Connection.Close();
     }
     catch (Exception e)
     {
         X1LogHelper.Exception(e);
     }
     PersistentDataHelper.Instance.RemoveConnection(_isGlobal, _dbName);
 }
Example #12
0
 public void CopyFile(string source, string dest)
 {
     Delete(dest);
     try
     {
         File.Copy(source, dest);
     }
     catch (Exception ex)
     {
         X1LogHelper.Exception(ex);
     }
 }
Example #13
0
 public static T ParseJSON <T>(string content)
 {
     try
     {
         return(!string.IsNullOrEmpty(content)?JsonConvert.DeserializeObject <T>(content, _settings):default(T));
     }
     catch (Exception ex)
     {
         X1LogHelper.Exception(ex);
     }
     return(default(T));
 }
Example #14
0
        public long GetFileLength(string path)
        {
            try
            {
                return(new FileInfo(path).Length);
            }
            catch (Exception ex)
            {
                X1LogHelper.Error(ex.Message);
            }

            return(0);
        }
Example #15
0
 /// <summary>
 /// Initializes the singleton application object.  This is the first line of authored code
 /// executed, and as such is the logical equivalent of main() or WinMain().
 /// </summary>
 public App()
 {
     UnhandledException += (s, e) =>
     {
         if (X1LogHelper.HandlerGlobalException(e.Exception, s))
         {
             e.Handled = true;
         }
     };
     this.InitializeComponent();
     this.Suspending += OnSuspending;
     Windows.System.MemoryManager.AppMemoryUsageLimitChanging += MemoryManager_AppMemoryUsageLimitChanging;
 }
 public virtual bool Add(T entity)
 {
     try
     {
         CheckDateTime(entity);
         Connection.Insert(entity);
         return(true);
     }
     catch (Exception ex)
     {
         X1LogHelper.Exception(ex);
         return(false);
     }
 }
Example #17
0
 public string LoadText(string path)
 {
     try
     {
         if (File.Exists(path))
         {
             return(File.ReadAllText(path));
         }
     }
     catch (Exception ex)
     {
         X1LogHelper.Error(ex.Message);
     }
     return(string.Empty);
 }
Example #18
0
 static string _CreateDirectory(string path)
 {
     try
     {
         if (!Directory.Exists(path))
         {
             Directory.CreateDirectory(path);
         }
     }
     catch (Exception ex)
     {
         X1LogHelper.Exception(ex);
     }
     return(path);
 }
Example #19
0
 public Stream LoadContent(string path)
 {
     try
     {
         if (File.Exists(path))
         {
             return(new FileStream(path, FileMode.Open, FileAccess.Read));
         }
     }
     catch (Exception ex)
     {
         X1LogHelper.Error(ex.Message);
     }
     return(null);
 }
Example #20
0
        public async Task <List <string> > ListFiles(string remotePath, bool includingFolder = false, bool includingFiles = true)
        {
            var list = new List <string>();

            try
            {
                using (var s3Client = AWSS3Client)
                {
                    remotePath = ConvertToRemotePath(remotePath);
                    try
                    {
                        var response = await s3Client.ListObjectsAsync(ZibaobaoBucket, remotePath);

                        {
                            foreach (var s3Object in response.S3Objects)
                            {
                                var name = s3Object.Key;
                                if (!includingFolder && name.EndsWith("/"))
                                {
                                    continue;
                                }
                                if (!includingFiles && !name.EndsWith("/"))
                                {
                                    continue;
                                }
                                if (name.StartsWith(remotePath))
                                {
                                    var localPath = name.Substring(remotePath.Length).Trim('/');
                                    if (!string.IsNullOrEmpty(localPath))
                                    {
                                        list.Add(localPath);
                                    }
                                }
                            }
                        }
                    }
                    catch (AmazonS3Exception e)
                    {
                        X1LogHelper.Exception(e);
                    }
                }
            }
            catch (Exception ex)
            {
                X1LogHelper.Exception(ex);
            }
            return(list);
        }
Example #21
0
 public bool DeleteEntiry(string id)
 {
     try
     {
         var path = GetEntityPath(id);
         if (File.Exists(path))
         {
             File.Delete(path);
         }
     }
     catch (Exception ex)
     {
         X1LogHelper.Error(ex.Message);
     }
     return(false);
 }
Example #22
0
 public static string GetImageUrl(string id, string baseUrl = HttpResourceBase + "images")
 {
     try
     {
         if (String.IsNullOrEmpty(id))
         {
             return(null);
         }
         return(baseUrl + "/" + id);
     }
     catch (Exception e)
     {
         X1LogHelper.Exception(e);
     }
     return(null);
 }
Example #23
0
 public bool ResetData()
 {
     try
     {
         _Delete(Path.Combine(_dataPath, "cache"));
         _Delete(Path.Combine(_dataPath, "temp"));
         _Delete(Path.Combine(_dataPath, "entity"));
         _Delete(PersistentDataHelper.Instance.GetDataBasePath(true, PersistentDataHelper.DataDB));
         return(true);
     }
     catch (Exception ex)
     {
         X1LogHelper.Exception(ex);
         return(false);
     }
 }
Example #24
0
 public bool SaveEntiry(string id, Stream data)
 {
     try
     {
         using (var stream = new FileStream(GetEntityPath(id), FileMode.Create))
         {
             data.Position = 0;
             data.CopyTo(stream);
             return(true);
         }
     }
     catch (Exception ex)
     {
         X1LogHelper.Error(ex.Message);
     }
     return(false);
 }
        void File_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("[Property changed] " + e.PropertyName + " -> " + sender?.GetType().GetProperty(e.PropertyName)?.GetValue(sender, null));

            // Update UI text-fields
            var downloadFile = ((IDownloadFile)sender);

            if (downloadFile == null)
            {
                return;
            }
            // Update UI text-fields
            switch (e.PropertyName)
            {
            case nameof(IDownloadFile.Status):
                X1LogHelper.Log($"{downloadFile.Url} [status]: {downloadFile.Status}");
                break;

            case nameof(IDownloadFile.TotalBytesExpected):
                X1LogHelper.Verbose($"{downloadFile.Url} [Total length]: {downloadFile.TotalBytesExpected}");
                break;

            case nameof(IDownloadFile.TotalBytesWritten):
                X1LogHelper.Verbose($"{downloadFile.Url} [downloaded]: {downloadFile.TotalBytesWritten}");
                break;
            }

            // Update UI if download-status changed.
            if (e.PropertyName == "Status")
            {
                switch (((IDownloadFile)sender).Status)
                {
                case DownloadFileStatus.COMPLETED:
                {
                    X1LogHelper.Log($"{downloadFile.Url} [downloaded as ]: [{downloadFile.DestinationPathName}]");
                    var path = downloadFile.DestinationPathName;
                    if (path.StartsWith(BaobaoModel.LocalPathPrefix))
                    {
                        path = path.Substring(BaobaoModel.LocalPathPrefix.Length);
                    }
                    OnFileAvailable?.Invoke(this, new StringEventArgs(path));
                }
                break;
                }
            }
        }
 public void RemoveConnection(bool isGlobal, string dbName)
 {
     lock (_connectionLock)
     {
         try
         {
             string connectionKey = GetDataBasePath(isGlobal, dbName);
             if (_connections.ContainsKey(connectionKey))
             {
                 _connections.Remove(connectionKey);
             }
         }
         catch (Exception e)
         {
             X1LogHelper.Exception(e);
             throw;
         }
     }
 }
Example #27
0
 public bool Create(string path)
 {
     try
     {
         CreateDirectory(Path.GetDirectoryName(path));
     }
     catch (Exception)
     {
     }
     try
     {
         using (File.Create(path)) { }
         return(true);
     }
     catch (Exception ex)
     {
         X1LogHelper.Exception(ex);
     }
     return(false);
 }
Example #28
0
 public bool SaveContent(string path, Stream data)
 {
     try
     {
         using (var stream = new FileStream(path, FileMode.Create))
         {
             try
             {
                 data.Position = 0;
             }
             catch (Exception ex)
             {
                 X1LogHelper.Error(ex.Message);
             }
             data.CopyTo(stream);
             return(true);
         }
     }
     catch (Exception ex)
     {
         X1LogHelper.Exception(ex);
     }
     return(false);
 }
 public SQLiteConnectionWithLock GetConnection(bool isGlobal, string dbName)
 {
     lock (_connectionLock)
     {
         try
         {
             string connectionKey = GetDataBasePath(isGlobal, dbName);
             SQLiteConnectionWithLock connection;
             if (!_connections.TryGetValue(connectionKey, out connection))
             {
                 connection = new SecureSqLiteConnectionWithLock(
                     new SQLiteConnectionString(connectionKey, true, connectionKey), SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.Create);
                 _connections[connectionKey] = connection;
             }
             return(connection);
         }
         catch (TypeInitializationException e)
         {
             X1LogHelper.Exception(e);
             X1LogHelper.Error("Please try to rebuild entire service package when this happens");
             throw;
         }
     }
 }
 public ShootingGame(ApplicationOptions opts)
     : base(opts)
 {
     X1LogHelper.Info($"{ZibaobaoLibContext.Instance.AppName} starting {opts.Width}, {opts.Height} from {ZibaobaoLibContext.Instance.PersistentStorage.DocumentPath}");
 }