예제 #1
0
        public void Load(string folderPath, bool savesOnly = false)
        {
            Loaded         = false;
            GameDataSource = GameDataSource.LegacyFiles;

            if (!Directory.Exists(folderPath))
            {
                if (stopAtFirstError)
                {
                    throw new FileNotFoundException("Given data folder does not exist.");
                }

                return;
            }

            var fileReader = new FileReader();

            string GetPath(string name) => Path.Combine(folderPath, name.Replace('/', Path.DirectorySeparatorChar));

            Func <string, IFileContainer>             fileLoader = name => fileReader.ReadFile(name, File.OpenRead(GetPath(name)));
            Func <char, Dictionary <string, byte[]> > diskLoader = disk =>
            {
                string diskFile = FindDiskFile(folderPath, disk);

                if (diskFile == null)
                {
                    return(null);
                }

                return(ADFReader.ReadADF(File.OpenRead(diskFile)));
            };
            Func <string, bool> fileExistChecker = name => File.Exists(GetPath(name));

            Load(fileLoader, diskLoader, fileExistChecker, savesOnly);
        }
예제 #2
0
        // TODO: preference settings?
        public static string GetVersionInfo(string folderPath, out string language)
        {
            language = null;

            if (!Directory.Exists(folderPath))
            {
                return(null);
            }

            var possibleAssemblies = new string[2] {
                "AM2_CPU", "AM2_BLIT"
            };

            foreach (var assembly in possibleAssemblies)
            {
                var assemblyPath = Path.Combine(folderPath, assembly);

                if (File.Exists(assemblyPath))
                {
                    // check last 128 bytes
                    using var stream = File.OpenRead(assemblyPath);

                    if (stream.Length < 128)
                    {
                        return(null);
                    }

                    stream.Position = stream.Length - 128;
                    Span <byte> buffer = new byte[128];
                    stream.Read(buffer);
                    return(GetVersionFromAssembly(buffer, out language));
                }
            }

            var diskFile = FindDiskFile(folderPath, 'A');

            if (diskFile != null)
            {
                var adf = ADFReader.ReadADF(File.OpenRead(diskFile));

                foreach (var assembly in possibleAssemblies)
                {
                    if (adf.ContainsKey(assembly))
                    {
                        var data = adf[assembly];

                        if (data.Length < 128)
                        {
                            continue;
                        }

                        return(GetVersionFromAssembly(data.TakeLast(128).ToArray(), out language));
                    }
                }
            }

            return(null);
        }
예제 #3
0
        public void Load(string folderPath)
        {
            var  ambermoonFiles    = Legacy.Files.AmigaFiles;
            var  fileReader        = new FileReader();
            bool foundNoDictionary = true;

            void HandleFileLoaded(string file)
            {
                if (_log != null)
                {
                    _log.AppendLine("succeeded");
                }

                if (IsDictionary(file))
                {
                    foundNoDictionary = false;
                }
            }

            void HandleFileNotFound(string file)
            {
                if (_log != null)
                {
                    _log.AppendLine("failed");
                    _log.AppendLine($" -> Unable to find file '{file}'.");
                }

                if (IsDictionary(file))
                {
                    return;
                }

                if (_stopAtFirstError)
                {
                    throw new FileNotFoundException($"Unable to find file '{file}'.");
                }
            }

            foreach (var ambermoonFile in ambermoonFiles)
            {
                var name = ambermoonFile.Key;
                var path = Path.Combine(folderPath, name);

                if (_log != null)
                {
                    _log.Append($"Trying to load file '{name}' ... ");
                }

                // prefer direct files but also allow loading ADF disks
                if (_loadPreference == LoadPreference.PreferExtracted && File.Exists(path))
                {
                    Files.Add(name, fileReader.ReadFile(name, File.OpenRead(path)));
                    HandleFileLoaded(name);
                }
                else if (_loadPreference == LoadPreference.ForceExtracted)
                {
                    if (File.Exists(path))
                    {
                        Files.Add(name, fileReader.ReadFile(name, File.OpenRead(path)));
                        HandleFileLoaded(name);
                    }
                    else
                    {
                        HandleFileNotFound(name);
                    }
                }
                else
                {
                    // load from disk
                    var disk = ambermoonFile.Value;

                    if (!_loadedDisks.ContainsKey(disk))
                    {
                        string diskFile = FindDiskFile(folderPath, disk);

                        if (diskFile == null)
                        {
                            // file not found
                            if (_loadPreference == LoadPreference.ForceAdf)
                            {
                                if (_log != null)
                                {
                                    _log.AppendLine("failed");
                                    _log.AppendLine($" -> Unabled to find ADF disk file with letter '{disk}'. Try to rename your ADF file to 'ambermoon_{disk}.adf'.");
                                }

                                if (_stopAtFirstError)
                                {
                                    throw new FileNotFoundException($"Unabled to find ADF disk file with letter '{disk}'. Try to rename your ADF file to 'ambermoon_{disk}.adf'.");
                                }
                            }

                            if (_loadPreference == LoadPreference.PreferAdf)
                            {
                                if (!File.Exists(path))
                                {
                                    HandleFileNotFound(name);
                                }
                                else
                                {
                                    Files.Add(name, fileReader.ReadFile(name, File.OpenRead(path)));
                                }
                            }

                            HandleFileLoaded(name);

                            continue;
                        }

                        _loadedDisks.Add(disk, ADFReader.ReadADF(File.OpenRead(diskFile)));
                    }

                    if (!_loadedDisks[disk].ContainsKey(name))
                    {
                        HandleFileNotFound(name);
                    }
                    else
                    {
                        Files.Add(name, fileReader.ReadFile(name, _loadedDisks[disk][name]));
                        HandleFileLoaded(name);
                    }
                }
            }

            if (foundNoDictionary)
            {
                throw new FileNotFoundException($"Unable to find any dictionary file.");
            }
        }