/// <summary> /// 初始化. /// </summary> /// <param name="iPath">路径.</param> /// <param name="iGameName">游戏名.</param> public bool Init(string iPath, string iGameName) { try { this.Load(iPath); // 创建strings.xml对象 this._stringsXml = this.CreateStringsXml(); // if(null == this._stringsXml) { // return false; // } // 初始化Appliction this.InitApplicationInfo(); // 初始化SDK版本信息. if (null == this.InitSDKVersions()) { return(false); } // 应用用户自定义数据 this.ApplyUserData(iGameName); } catch (Exception e) { UtilsLog.Exception("Init", "Failed!!! Exeption:{0}", e.Message); return(false); } return(true); }
/// <summary> /// 初始化. /// </summary> /// <param name="iPath">I path.</param> protected virtual bool Init(string iPath) { try { // 保存路径 this.SavePath = iPath; // 加载 this.Load(iPath); } catch (Exception e) { UtilsLog.Exception("StringsXMLBase", "Init():Failed!!! Exeption:{0}", e.Message); return(false); } return(true); }
/// <summary> /// 在Ftp服务器上创建文件夹. /// </summary> /// <param name="iUrl">URL.</param> /// <param name="iAccountId">账户.</param> /// <param name="iPwd">密码.</param> private TDirState CreateDirOnFtpServer( string iUrl, string iAccountId, string iPwd) { FtpWebRequest ftpRequest = null; FtpWebResponse response = null; TDirState state = TDirState.None; try { Uri targetURI = new Uri(iUrl); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(targetURI); ftpRequest.Credentials = new NetworkCredential(iAccountId, iPwd); ftpRequest.KeepAlive = false; ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory; ftpRequest.UseBinary = true; response = ftpRequest.GetResponse() as FtpWebResponse; state = TDirState.Created; UtilsLog.Info("CreateDirOnFtpServer", "Successed Url:{0}", iUrl); } catch (WebException exp) { UtilsLog.Exception("CreateDirOnFtpServer", "Failed!!! Url:{0} \n WebException: \n {1}", iUrl, exp.Message); state = TDirState.Exception; } catch (IOException exp) { UtilsLog.Exception("CreateDirOnFtpServer", "Failed!!! Url:{0} \n IOException: \n {1}", iUrl, exp.Message); state = TDirState.Exception; } catch (Exception exp) { UtilsLog.Exception("CreateDirOnFtpServer", "Failed!!! Url:{0} \n Exception: \n {1}", iUrl, exp.Message); state = TDirState.Exception; } finally { if (response != null) { response.Close(); } } return(state); }
private void Log(string message, TLogType iType = TLogType.kInfo) { System.Console.WriteLine(message); if (BuildParameters.IsBuildInCI == false) { switch (iType) { case TLogType.kInfo: { UtilsLog.Info("ConsoleBuildLogger", message); } break; case TLogType.kWarning: { UtilsLog.Warning("ConsoleBuildLogger", message); } break; case TLogType.kError: { UtilsLog.Error("ConsoleBuildLogger", message); } break; case TLogType.kException: { UtilsLog.Exception("ConsoleBuildLogger", message); } break; default: break; } } }
/// <summary> /// 往Ftp服务器上传文件. /// </summary> /// <param name="iUrl">URL.</param> /// <param name="iAccountId">账户.</param> /// <param name="iPwd">密码.</param> private TRunState UpLoadFileToFtpServer( string iUploadUrl, string iInputPath, string iAccountId, string iPwd) { FtpWebRequest ftpRequest = null; FtpWebResponse response = null; FileStream fileStream = null; Stream ftpStream = null; TRunState state = TRunState.OK; try { Uri targetURI = new Uri(iUploadUrl); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(targetURI); ftpRequest.Credentials = new NetworkCredential(iAccountId, iPwd); ftpRequest.KeepAlive = false; ftpRequest.Method = WebRequestMethods.Ftp.UploadFile; ftpRequest.UseBinary = true; // 读取文件 fileStream = File.OpenRead(iInputPath); byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); // 写入请求 ftpStream = ftpRequest.GetRequestStream(); ftpStream.Write(buffer, 0, buffer.Length); // 发出请求 response = ftpRequest.GetResponse() as FtpWebResponse; UtilsLog.Info("UpLoadFileToFtpServer", "Successed. UploadUrl:{0} \n InputPath:{1}", iUploadUrl, iInputPath); } catch (WebException exp) { UtilsLog.Exception("UpLoadFileToFtpServer", "Failed!!! Retries:{0} \n UploadUrl:{1} \n InputPath:{2} \n WebException: \n {3}", this.Retries, iUploadUrl, iInputPath, exp.Message); state = TRunState.Exception; } catch (IOException exp) { UtilsLog.Exception("UpLoadFileToFtpServer", "Failed!!! Retries:{0} \n UploadUrl:{1} \n InputPath:{2} \n WebException: \n {3}", this.Retries, iUploadUrl, iInputPath, exp.Message); state = TRunState.Exception; } catch (Exception exp) { UtilsLog.Exception("UpLoadFileToFtpServer", "Failed!!! Retries:{0} \n UploadUrl:{1} \n InputPath:{2} \n WebException: \n {3}", this.Retries, iUploadUrl, iInputPath, exp.Message); state = TRunState.Exception; } finally { if (fileStream != null) { fileStream.Close(); } if (ftpStream != null) { ftpStream.Close(); } if (response != null) { response.Close(); } } return(state); }
/// <summary> /// 获取下载文件的总大小 /// </summary> /// <returns>下载文件的总大小.</returns> /// <param name="iDownloadUrl">下载地址.</param> /// <param name="iFileSize">文件大小.</param> protected TRunState GetFileDataSize(string iDownloadUrl, ref long iFileSize) { TRunState ret = TRunState.OK; if (null != this._target) { iFileSize = Convert.ToInt64(this._target.DataSize); } else { HttpWebResponse response = null; HttpWebRequest request = null; try { Uri _url = new Uri(iDownloadUrl); request = WebRequest.Create(_url) as HttpWebRequest; if (null == request) { UtilsLog.Error("DownloaderBase", "GetFileDataSize:HttpWebRequest Create Failed!!!"); } if (TRunState.OK == ret) { request.Timeout = this.TimeOut; request.ReadWriteTimeout = this.TimeOut; request.Method = "HEAD"; request.ContentType = "application/octet-stream"; request.KeepAlive = false; response = request.GetResponse() as HttpWebResponse; if (response.StatusCode != HttpStatusCode.OK) { ErrorDetail error = new ErrorDetail(); error.Type = TErrorType.HttpError; error.State = TRunState.GetFileSizeFromServerFailed; error.Detail = string.Format("GetFileDataSize Failed!!(File:{0} StatusCode:{1})", iDownloadUrl, response.StatusCode.ToString()); error.Retries = this.Retries; iFileSize = -1; response.Close(); response = null; return(TRunState.GetFileSizeFromServerFailed); } else { iFileSize = response.ContentLength; } } } catch (Exception exp) { UtilsLog.Exception("DownloaderBase", "GetFileDataSize Type:{0} Message:{1} StackTrace:{2}", exp.GetType().ToString(), exp.Message, exp.StackTrace); ErrorDetail error = new ErrorDetail(); error.Type = TErrorType.HttpException; error.State = TRunState.Exception; error.Detail = string.Format("GetFileDataSize Failed!!(File:{0} exp:{1})", iDownloadUrl, exp.Message); error.Retries = this.Retries; iFileSize = -1; ret = TRunState.Exception; if (null != request) { request.Abort(); } } finally { if (null != response) { try { response.Close(); response = null; } catch { request.Abort(); } } } if (0 < iFileSize) { UtilsLog.Info("DownloaderBase", "GetFileDataSize Size:{0} ({1})", iFileSize.ToString(), iDownloadUrl); } } return(ret); }
/// <summary> /// 取得指定目录在Ftp服务器上得状态. /// </summary> /// <returns>目录在服务器上得状态.</returns> /// <param name="iParentUrl">上一层URL.</param> /// <param name="iTargetDir">目标目录.</param> /// <param name="iAccountId">账户.</param> /// <param name="iPwd">密码.</param> private TDirState GetDirStateOnFtpServer( string iParentUrl, string iTargetDir, string iAccountId, string iPwd) { FtpWebRequest ftpRequest = null; FtpWebResponse response = null; StreamReader reader = null; TDirState state = TDirState.None; try { Uri targetURI = new Uri(iParentUrl); ftpRequest = (FtpWebRequest)FtpWebRequest.Create(targetURI); ftpRequest.Credentials = new NetworkCredential(iAccountId, iPwd); ftpRequest.KeepAlive = false; ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory; ftpRequest.UseBinary = true; response = ftpRequest.GetResponse() as FtpWebResponse; reader = new StreamReader( response.GetResponseStream(), System.Text.Encoding.Default); string line = reader.ReadLine(); state = TDirState.NotExist; // 循环遍历内容 while (line != null) { if (line.Equals(iTargetDir) == true) { state = TDirState.Exist; break; } line = reader.ReadLine(); } if (TDirState.NotExist == state) { UtilsLog.Info("GetDirStateOnFtpServer", "State:{0} ParentUrl:{1} TargetDir:{2}", state.ToString(), iParentUrl, iTargetDir); } } catch (WebException exp) { UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n WebException: \n {2}", iParentUrl, iTargetDir, exp.Message); state = TDirState.Exception; } catch (IOException exp) { UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n IOException: \n {2}", iParentUrl, iTargetDir, exp.Message); state = TDirState.Exception; } catch (Exception exp) { UtilsLog.Exception("GetDirStateOnFtpServer", "ParentUrl:{0} TargetDir:{1} \n Exception: \n {2}", iParentUrl, iTargetDir, exp.Message); state = TDirState.Exception; } finally { if (reader != null) { reader.Close(); } if (response != null) { response.Close(); } } return(state); }