public void DownloadText() { var progress = new InternalNodeProgress("Download text main task"); progress.ProgressChanged += sender => { Console.WriteLine(progress.Percent); }; var content = DownloadUtils.GetFileContent(progress.CreateNewLeafSubProgress(100D, String.Format("Downloading Text")), "http://baidu.com"); // Console.WriteLine(content); }
public void DownloadFileWithProgress() { var progress = new InternalNodeProgress("Download File Main Task"); progress.ProgressChanged += sender => { Console.WriteLine(progress.Percent); }; var downloadFile = new FileInfo("Test.QQ.exe"); if (downloadFile.Exists) { downloadFile.Delete(); } DownloadUtils.DownloadFile(progress.CreateNewLeafSubProgress(100D, "Downloading test file"), "http://dldir1.qq.com/qqfile/qq/QQ7.4/15197/QQ7.4.exe", downloadFile.FullName); downloadFile.Refresh(); Assert.IsTrue(downloadFile.Exists); }
public Process LaunchInstance(InternalNodeProgress progress, String instanceName, PlayerEntity player) { Logger.GetLogger().Info(String.Format("Start to launch {0} by player {1}...", instanceName, player.PlayerName)); var instanceInfo = this.InstanceBank.InstancesInfoList.First(x => (x.Name.Equals(instanceName))); var instance = JsonConverter.Parse<InstanceEntity>( File.ReadAllText(instanceInfo.FilePath)); if (!(instanceInfo.InstanceState == InstanceState.Ok || instanceInfo.InstanceState == InstanceState.PerInitialized)) { throw new WrongStateException("Wrong instance state! Just instance which in OK or PerInitialized state could launch."); } this.CriticalInstanceFieldCheck(instance); var instanceRootFolder = this.GetInstanceRootFolder(instance.InstanceName); #region Buding environment if (instanceInfo.InstanceState == InstanceState.PerInitialized) { #region entire file. if (instance.FileSystem.EntirePackageFiles != null && instance.FileSystem.EntirePackageFiles.Count != 0) { var singlePackageDownloadNodeProgress = 30D / instance.FileSystem.EntirePackageFiles.Count; foreach (var entirePackageFile in instance.FileSystem.EntirePackageFiles) { this.ReceiveEntirePackage(progress.CreateNewInternalSubProgress(singlePackageDownloadNodeProgress, String.Format("Receiving entire package {0}", entirePackageFile.Name)), instance.InstanceName, entirePackageFile); } } #endregion progress.Percent = 30D; #region official files. if (instance.FileSystem.OfficialFiles != null && instance.FileSystem.OfficialFiles.Count != 0) { var singleOfficialDownloadNodeProgress = 30D / instance.FileSystem.OfficialFiles.Count; foreach (var officialFileEntity in instance.FileSystem.OfficialFiles) { this.ReceiveOfficialFile( progress.CreateNewLeafSubProgress(singleOfficialDownloadNodeProgress, String.Format("Downloading official file: {0}", officialFileEntity.Name)), instance.InstanceName, officialFileEntity, this.UsingFileRepository); } } #endregion progress.Percent = 60D; #region custom files if (instance.FileSystem.CustomFiles != null && instance.FileSystem.CustomFiles.Count != 0) { var singleCustomDownloadNodeProgress = 30D / instance.FileSystem.CustomFiles.Count; foreach (var customFileEntity in instance.FileSystem.CustomFiles) { this.ReceiveCustomFile( progress.CreateNewLeafSubProgress(singleCustomDownloadNodeProgress, String.Format("Downloading custom file: {0}", customFileEntity.Name)), instance.InstanceName, customFileEntity); } } #endregion } #endregion progress.Percent = 90D; instanceInfo.InstanceState = InstanceState.Ok; instanceInfo.UpdateDate = DateTime.Now.ToString("O"); this.SaveInstancesBankToFile(); //DONE:Build start argument. var startArgument = new StringBuilder();// placer.ReplaceArgument(instance.StartupArguments); foreach (var jvmArgument in instance.StartupArguments.JvmArguments) { startArgument.Append(jvmArgument + " "); } startArgument.Append(this.Config.GetConfig("extraJvmArguments") ?? String.Empty).Append(" "); startArgument.AppendFormat("-Xmx{0}M -Xms{1}M" + " ", Convert.ToInt64(this.Config.GetConfig("maxMemorySizeMega")), instance.StartupArguments.MiniumMemoryMegaSize); var nativeFolder = new DirectoryInfo(Path.Combine(instanceRootFolder.FullName, instance.StartupArguments.Nativespath)); if (nativeFolder.Exists) { startArgument.AppendFormat("-Djava.library.path=\"{0}\"" + " ", nativeFolder.FullName); } else { throw new DirectoryNotFoundException(String.Format("Native folder is not valid!")); } startArgument.Append("-cp" + " "); foreach (var libraryPath in instance.StartupArguments.LibraryPaths) { var libFile = new FileInfo(Path.Combine(instanceRootFolder.FullName, libraryPath)); if (libFile.Exists) { startArgument.Append("\"" + libFile.FullName + "\"" + ";"); } else { throw new FileNotFoundException(String.Format("Instance {0} is missing lib file {1}", instance.InstanceName, libraryPath)); } } var mainJarFile = new FileInfo(Path.Combine(instanceRootFolder.FullName, instance.StartupArguments.MainJarPath)); if (mainJarFile.Exists) { startArgument.Append("\"" + mainJarFile.FullName + "\"" + " "); } else { throw new FileNotFoundException(String.Format("Instance {0} is missing main jar file {1}", instance.InstanceName, mainJarFile.Name)); } startArgument.Append(instance.StartupArguments.MainClass + " "); startArgument.AppendFormat("--username {0} ", player.PlayerName); startArgument.AppendFormat("--version {0} ", instance.StartupArguments.Version); startArgument.AppendFormat("--gameDir \"{0}\" ", instanceRootFolder.FullName); var assetsDir = new DirectoryInfo(Path.Combine(instanceRootFolder.FullName, instance.StartupArguments.AssetsDir)); if (assetsDir.Exists) { startArgument.AppendFormat("--assetsDir \"{0}\" ", assetsDir.FullName); } else { throw new DirectoryNotFoundException("Assets folder not found!"); } startArgument.AppendFormat("--assetIndex {0} ", instance.StartupArguments.AssetIndex); startArgument.AppendFormat("--uuid {0} ", player.PlayerId); startArgument.AppendFormat("--accessToken {0} ", player.AccessToken); startArgument.AppendFormat("--userProperties {{{0}}} ", instance.StartupArguments.UserProperties); startArgument.AppendFormat("--userType {0} ", instance.StartupArguments.UserType); foreach (var tweakClass in instance.StartupArguments.TweakClasses) { startArgument.AppendFormat("--tweakClass {0} ", tweakClass); } //Launch minecraft var instanceStartInfo = new ProcessStartInfo { FileName = this.JavaRuntime.JavaWPath, Arguments = startArgument.ToString(), WorkingDirectory = instanceRootFolder.FullName, WindowStyle = ProcessWindowStyle.Normal, UseShellExecute = false, RedirectStandardOutput = true }; var instanceProcess = new Process { StartInfo = instanceStartInfo, EnableRaisingEvents = true }; instanceProcess.Start(); progress.Percent = 100D; this.CurrentInstanceProcess = instanceProcess; Logger.GetLogger().Info(String.Format("Instance {0} launched!", instanceName)); return this.CurrentInstanceProcess; }
public void MidLevel(InternalNodeProgress progress) { this.BottomLevelA(progress.CreateNewLeafSubProgress(50D, "MidLevel A")); this.BottomLevelB(progress.CreateNewLeafSubProgress(50, "MidLevel B")); }
public static void DownloadZippedFile(InternalNodeProgress progress, String url, String path, String md5) { var tempFileInfo = new FileInfo(Path.Combine(new[] { FolderUtils.SystemTempFolder.FullName, Guid.NewGuid().ToString("N") })); DownloadFile(progress.CreateNewLeafSubProgress(90D, String.Format("Downloading zip file {0}", url)), url, tempFileInfo.FullName, md5); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } new FastZip().ExtractZip(tempFileInfo.FullName, path, null); progress.Percent = 100D; }
public string UpdateInstance(InternalNodeProgress progress, string instanceName) { TerminologyLogger.GetLogger().Info($"Start to update {instanceName}"); var instanceInfo = this.InstanceBank.InstancesInfoList.First(x => (x.Name.Equals(instanceName))); var oldInstanceEntity = JsonConverter.Parse<InstanceEntity>( File.ReadAllText(this.GetInstnaceFile(instanceInfo.Name))); this.CriticalInstanceFieldCheck(oldInstanceEntity); var newInstanceContent = DownloadUtils.GetWebContent(progress.CreateNewLeafSubProgress( $"Receiving {instanceInfo.Name} instance file", 50D), instanceInfo.UpdateUrl); var newInstanceEntity = JsonConverter.Parse<InstanceEntity>(newInstanceContent); progress.Percent = 60D; //Check instance is available to update if (newInstanceEntity.Version.Equals(oldInstanceEntity.Version)) { progress.Percent = 100D; TerminologyLogger.GetLogger() .InfoFormat( $"Instacne {oldInstanceEntity.InstanceName} already at latest version {oldInstanceEntity.Version}"); return (string.Format(TranslationManager.GetManager.Localize("InsatnceAtLatestVersion", "Instance now in latest version:{0}! Ignore update.", 1), newInstanceEntity.Version)); } var updateSuccessfulInfo = TranslationManager.GetManager.Localize("InstanceUpdateToVersion", "Successful update instance file {0} from version {1} to {2}!", 3); switch (instanceInfo.InstanceState) { case InstanceState.PerInitialized: { this.RemoveInstance(instanceInfo.Name); this.AddInstance(instanceInfo.UpdateUrl); progress.Percent = 100D; TerminologyLogger.GetLogger() .InfoFormat( $"Successful updated instance file {newInstanceEntity.InstanceName} from {oldInstanceEntity.Version} to {newInstanceEntity.Version}!"); return string.Format(updateSuccessfulInfo, newInstanceEntity.InstanceName, oldInstanceEntity.Version, newInstanceEntity.Version); } case InstanceState.Ok: { //TODO:I'll support instance name change at future version. if (!newInstanceEntity.InstanceName.Equals(oldInstanceEntity.InstanceName)) { throw new Exception("Old instance name not equal with new instance name."); } var instanceRootFolder = this.GetInstanceRootFolder(oldInstanceEntity.InstanceName); #region Update files #region Entire package //Difference entire package will cause whole package target folder been delete var newEntirePackages = newInstanceEntity.FileSystem.EntirePackageFiles ?? new List<EntirePackageFileEntity>(); var oldEntirePackages = oldInstanceEntity.FileSystem.EntirePackageFiles ?? new List<EntirePackageFileEntity>(); //Delete old entire package foreach (var oldEntirePackageFileEntity in oldEntirePackages) { if (!newEntirePackages.Exists ( x => x.Name.Equals(oldEntirePackageFileEntity.Name) && x.Md5.Equals(oldEntirePackageFileEntity.Md5) ) ) { FolderUtils.DeleteDirectory(Path.Combine(instanceRootFolder.FullName, oldEntirePackageFileEntity.LocalPath)); } } //Download new entire package foreach (var newEntirePackageFileEntity in newEntirePackages) { if (!oldEntirePackages.Exists ( x => x.Name.Equals(newEntirePackageFileEntity.Name) && x.Md5.Equals(newEntirePackageFileEntity.Md5) ) ) { //TODO:Support progress this.ReceiveEntirePackage(new InternalNodeProgress("Ignore"), newInstanceEntity.InstanceName, newEntirePackageFileEntity); } } #endregion progress.Percent = 80D; #region Official files var newOfficialFiles = newInstanceEntity.FileSystem.OfficialFiles ?? new List<OfficialFileEntity>(); var oldOfficialFiles = newInstanceEntity.FileSystem.OfficialFiles ?? new List<OfficialFileEntity>(); //Delete old official files foreach (var oldOfficialFileEntity in oldOfficialFiles) { if (!newOfficialFiles.Exists(x => x.ProvideId.Equals(oldOfficialFileEntity.ProvideId))) { File.Delete(Path.Combine(instanceRootFolder.FullName, oldOfficialFileEntity.LocalPath)); } } //Receive new official files foreach (var newOfficialFileEntity in newOfficialFiles) { if (!oldOfficialFiles.Exists(x => x.ProvideId.Equals(newOfficialFileEntity.ProvideId))) { this.ReceiveOfficialFile(new LeafNodeProgress("Ignore"), newInstanceEntity.InstanceName, newOfficialFileEntity, this.UsingFileRepository); } } #endregion progress.Percent = 90D; #region Custom files var newCustomFiles = newInstanceEntity.FileSystem.CustomFiles ?? new List<CustomFileEntity>(); var oldCustomFiles = oldInstanceEntity.FileSystem.CustomFiles ?? new List<CustomFileEntity>(); foreach (var oldCustomFileEntity in oldCustomFiles) { if ( !newCustomFiles.Exists( x => x.Name.Equals(oldCustomFileEntity.Name) && x.Md5.Equals(oldCustomFileEntity.Md5))) { File.Delete(Path.Combine(instanceRootFolder.FullName, oldCustomFileEntity.LocalPath)); } } foreach (var newCustomFileEntity in newCustomFiles) { if (!oldCustomFiles.Exists(x => x.Name.Equals(newCustomFileEntity.Name) && x.Md5.Equals(newCustomFileEntity.Md5))) { this.ReceiveCustomFile(new LeafNodeProgress("Ignore"), newInstanceEntity.InstanceName, newCustomFileEntity); } } #endregion progress.Percent = 100D; #endregion instanceInfo.UpdateDate = DateTime.Now.ToString("O"); this.SaveInstancesBankToFile(); File.WriteAllText(this.GetInstnaceFile(instanceInfo.Name), JsonConverter.ConvertToJson(newInstanceEntity)); TerminologyLogger.GetLogger() .InfoFormat( $"Successful updated entire instance {newInstanceEntity.InstanceName} from {oldInstanceEntity.Version} to {newInstanceEntity.Version}!"); return string.Format(updateSuccessfulInfo, newInstanceEntity.InstanceName, oldInstanceEntity.Version, newInstanceEntity.Version); } default: { throw new WrongStateException("Wrong instance state! Just instance which in OK or perinitialized state could update."); break; } } }