/// <summary> /// Downloads a ROM to an Intellicart. /// </summary> /// <param name="intellicart">The Intellicart to load the ROM onto.</param> /// <param name="programName">Name of the program being downloaded.</param> /// <param name="rom">The ROM to load.</param> /// <param name="errorHandler">Error handler function.</param> public static void DownloadRom(this IntellicartModel intellicart, string programName, IRom rom, Action <string, Exception> errorHandler) { var title = string.Format(CultureInfo.CurrentCulture, Resources.Strings.DownloadRom_Title_Format, programName); var task = new AsyncTaskWithProgress(title, true, true, 0); var taskData = new DownloadTaskData(task, intellicart, programName, rom); taskData.ErrorHandler = errorHandler; task.RunTask(taskData, DownloadRom, DownloadRomComplete); }
private static string PrepareRom(DownloadTaskData data) { var rom = data.Rom; data.UpdateTaskProgress(0, string.Format(CultureInfo.CurrentCulture, Resources.Strings.DownloadRom_PrepareUpdateFormat, rom.RomPath)); var jzIntvConfiguration = SingleInstanceApplication.Instance.GetConfiguration <INTV.JzIntv.Model.Configuration>(); var converterApps = jzIntvConfiguration.GetConverterApps(rom, RomFormat.Rom); if (!converterApps.Any()) { converterApps = new[] { new Tuple <string, RomFormat>(JustCopy, RomFormat.Rom) }; } var converterApp = converterApps.First(); // rom.GetConverterApp(jzIntvConfiguration); if ((converterApp.Item1 != JustCopy) && (string.IsNullOrEmpty(converterApp.Item1) || !System.IO.File.Exists(converterApp.Item1)) && (rom.Format != RomFormat.Rom)) { var message = string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Strings.DownloadRom_PrepareFailedErrorFormat, converterApp); throw new InvalidOperationException(message); } var romStagingArea = Configuration.Instance.RomsStagingAreaPath; var stagingAreaPath = rom.GetStagingAreaPath(romStagingArea); var cachedRomPath = rom.GetCachedRomFilePath(stagingAreaPath); var cachedConfigPath = rom.GetCachedConfigFilePath(stagingAreaPath); var romFile = rom.GetOutputFilePath(stagingAreaPath, INTV.Core.Model.Program.ProgramFileKind.Rom, false); bool createRomFile = true; bool changed; bool isSourceFileInCache = rom.IsInCache(stagingAreaPath, out changed); if (isSourceFileInCache) { createRomFile = !System.IO.File.Exists(romFile); } else { cachedRomPath.ClearReadOnlyAttribute(); cachedConfigPath.ClearReadOnlyAttribute(); System.IO.File.Copy(rom.RomPath, cachedRomPath, true); if (!string.IsNullOrWhiteSpace(cachedConfigPath) && !string.IsNullOrEmpty(rom.ConfigPath) && System.IO.File.Exists(rom.ConfigPath) && (rom.ConfigPath != rom.RomPath)) { System.IO.File.Copy(rom.ConfigPath, cachedConfigPath, true); } cachedRomPath.ClearReadOnlyAttribute(); cachedConfigPath.ClearReadOnlyAttribute(); } if (createRomFile) { foreach (var converter in converterApps) { var argument = "\"" + cachedRomPath + "\""; var result = -1; if (JustCopy == converterApp.Item1) { System.IO.File.Copy(rom.RomPath, cachedRomPath, true); result = 0; } else { result = INTV.Shared.Utility.RunExternalProgram.Call(converter.Item1, argument, stagingAreaPath); } if (result == 0) { cachedRomPath = System.IO.Path.ChangeExtension(cachedRomPath, converter.Item2.FileExtension()); } else { var message = string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Strings.DownloadRom_RomConversionToolFailedErrorFormat, System.IO.Path.GetFileName(converterApp.Item1), result); throw new InvalidOperationException(message); } } if (!System.IO.File.Exists(romFile)) { var message = string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Strings.DownloadRom_RomConversionOutputFileNotFoundErrorFormat, rom.RomPath, System.IO.Path.GetFileNameWithoutExtension(romFile)); throw new InvalidOperationException(message); } else if ((new System.IO.FileInfo(romFile)).Length > IntellicartModel.MaxROMSize) { var message = string.Format(System.Globalization.CultureInfo.CurrentCulture, Resources.Strings.DownloadRom_RomTooLargeErrorFormat, rom.RomPath, romFile); throw new InvalidOperationException(message); } } // UNDONE Not sure if we should handle any of these explicitly. Haven't encountered them during testing. ////catch (System.IO.PathTooLongException e) ////catch (System.IO.IOException e) ////catch (UnauthorizedAccessException e) ////catch (InvalidOperationException e) return(romFile); }