Ejemplo n.º 1
0
        private System.Exception UpdateFilePath(string filePath, bool forceUpdate)
        {
#if REPORT_PERFORMANCE
            System.Diagnostics.Debug.WriteLine(">>>> ENTER Fork.UpdateFilePath(" + filePath + "," + forceUpdate + ") + T:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
            try
            {
#endif // REPORT_PERFORMANCE
            System.Exception error = null;
            if ((FileSystem != null) && FileSystem.Frozen)
            {
                // During save, do not attempt to regenerate contents.
                return(error);
            }
            try
            {
                // Do some special work if this is a ROM fork.
                if ((FileSystem != null) && (FileSystem.GetForkKind(this) == ForkKind.Program))
                {
                    if (forceUpdate)
                    {
#if REPORT_PERFORMANCE
                        System.Diagnostics.Debug.WriteLine(">>>>>> ENTER Fork.UpdateFilePath(forceUpdate): GetRom");
                        var stopwatch2 = System.Diagnostics.Stopwatch.StartNew();
                        try
                        {
#endif // REPORT_PERFORMANCE
                        var filesUsingFork = FileSystem.GetFilesUsingForks(new[] { this }).Values;
                        foreach (var program in filesUsingFork.OfType <Program>())
                        {
                            // Since "Program" entries using the same Fork will all point to this one,
                            // we don't need to re-do this work for every file that might point at the fork.
                            var rom = program.Description.GetRom();
                            if ((rom != null) && !string.IsNullOrEmpty(rom.RomPath))
                            {
                                var path = string.Empty;
#if REPORT_PERFORMANCE
                                var stopwatch3 = System.Diagnostics.Stopwatch.StartNew();
#endif // REPORT_PERFORMANCE
                                path = rom.PrepareForDeployment(LuigiGenerationMode.Standard);
#if REPORT_PERFORMANCE
                                stopwatch3.Stop();
                                AccumulatedUpdateFilePathForcedPrepareTime += stopwatch3.Elapsed;
#endif // REPORT_PERFORMANCE
                                UpdateFilePath(path);
                            }
                        }
#if REPORT_PERFORMANCE
                    }
                    finally
                    {
                        stopwatch2.Stop();
                        System.Diagnostics.Debug.WriteLine(">>>>>> EXIT  Fork.UpdateFilePath(forceUpdate): GetRom");     // took: + " + stopwatch2.Elapsed.ToString());
                        AccumulatedUpdateFilePathForcedTime += stopwatch2.Elapsed;
                    }
#endif // REPORT_PERFORMANCE
                    }
                    else
                    {
                        var rom = Rom;
                        var prepareForDeployment = string.IsNullOrEmpty(filePath) && (rom != null);
                        if (!prepareForDeployment && (rom != null))
                        {
                            // We think we don't have to prepare for deployment, but do have ROM set.
                            // Let's just make sure that we have that LUIGI file, shall we?
                            prepareForDeployment = string.IsNullOrEmpty(_filePath) || !System.IO.File.Exists(_filePath);
                            if (!prepareForDeployment && System.IO.File.Exists(_filePath))
                            {
                                // Check to see if it looks like a valid LUIGI file.
                                prepareForDeployment = LuigiFileHeader.GetHeader(_filePath) == null;
                            }
                        }
                        if (prepareForDeployment)
                        {
                            // No file path, but somehow we have a ROM. Force recreation of the LUIGI file. This can
                            // occur during sync from device.
                            UpdateFilePath(Rom.PrepareForDeployment(LuigiGenerationMode.Standard));
                        }
                        else if (rom == null)
                        {
                            if (!System.IO.File.Exists(filePath))
                            {
                                // No ROM, and the file we think we have does not exist.
                                ILfsFileInfo file;
                                rom = null;
                                if (FileSystem.GetFilesUsingForks(new[] { this }).TryGetValue(this, out file))
                                {
                                    switch (FileSystem.Origin)
                                    {
                                    case FileSystemOrigin.HostComputer:
                                        var description = ((Program)file).Description;
                                        rom = description == null ? null : description.GetRom();
                                        break;

                                    case FileSystemOrigin.LtoFlash:
                                        break;
                                    }
                                }
                                prepareForDeployment = (rom != null) && !System.IO.File.Exists(filePath) && !string.IsNullOrEmpty(rom.RomPath) && System.IO.File.Exists(rom.RomPath);
                                if (prepareForDeployment)
                                {
                                    _filePath = null;
                                    UpdateFilePath(rom.PrepareForDeployment(LuigiGenerationMode.Standard));
                                }
                            }

                            // We should *always* be a LUIGI file. If not, fix it!
                            if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath))
                            {
                                if (!LuigiFileHeader.PotentialLuigiFile(filePath))
                                {
                                    rom = INTV.Core.Model.Rom.Create(filePath, null);
                                    prepareForDeployment = rom != null;
                                    if (prepareForDeployment)
                                    {
                                        _filePath = null;
                                        UpdateFilePath(rom.PrepareForDeployment(LuigiGenerationMode.Standard));
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (forceUpdate)
                    {
                        _filePath = null;
                    }
                    UpdateFilePath(filePath);
                    if (forceUpdate && !string.IsNullOrEmpty(_filePath) && !System.IO.File.Exists(_filePath))
                    {
                        error = new System.IO.FileNotFoundException("File for fork " + GlobalForkNumber + " not found.", filePath, null);
                    }
                }
            }
            catch (System.IO.IOException e)
            {
                error = e;
                if (!forceUpdate)
                {
                    throw;
                }
            }
            return(error);

#if REPORT_PERFORMANCE
        }

        finally
        {
            stopwatch.Stop();
            System.Diagnostics.Debug.WriteLine(">>>> EXIT  Fork.UpdateFilePath(" + filePath + ",force)");     // took: + " + stopwatch.Elapsed.ToString());
            AccumulatedUpdateFilePathTime += stopwatch.Elapsed;
        }
#endif // REPORT_PERFORMANCE
        }