| 1 | /* fre:ac - free audio converter |
| 2 | * Copyright (C) 2001-2022 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include "driveoffsets.h" |
| 14 | #include "config.h" |
| 15 | |
| 16 | using namespace smooth::IO; |
| 17 | using namespace smooth::Net; |
| 18 | using namespace smooth::Threads; |
| 19 | |
| 20 | BoCA::DriveOffsets *BoCA::DriveOffsets::instance = NIL; |
| 21 | |
| 22 | BoCA::DriveOffsets::DriveOffsets() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | BoCA::DriveOffsets::~DriveOffsets() |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | BoCA::DriveOffsets *BoCA::DriveOffsets::Get() |
| 31 | { |
| 32 | if (instance == NIL) instance = new DriveOffsets(); |
| 33 | |
| 34 | return instance; |
| 35 | } |
| 36 | |
| 37 | Void BoCA::DriveOffsets::Free() |
| 38 | { |
| 39 | if (instance != NIL) delete instance; |
| 40 | |
| 41 | instance = NIL; |
| 42 | } |
| 43 | |
| 44 | Bool BoCA::DriveOffsets::LoadDriveOffsets() |
| 45 | { |
| 46 | static Mutex mutex; |
| 47 | |
| 48 | Lock lock(mutex); |
| 49 | |
| 50 | Config *config = Config::Get(); |
| 51 | String cacheDir = config->cacheDir; |
| 52 | |
| 53 | if (cacheDir == GUI::Application::GetApplicationDirectory()) cacheDir.Append("boca/"); |
| 54 | else cacheDir.Append("freac/"); |
| 55 | |
| 56 | String offsetsFile = String(cacheDir).Append(ConfigureAccurateRip::ResourcesFolder).Append("/DriveOffsets.bin"); |
| 57 | |
| 58 | Utilities::CreateDirectoryForFile(offsetsFile); |
| 59 | |
| 60 | /* Download offsets file if necessary. |
| 61 | */ |
| 62 | if (!File(offsetsFile).Exists()) |
| 63 | { |
| 64 | String base = config->GetStringValue(ConfigureAccurateRip::ConfigID, ConfigureAccurateRip::ConfigPathID, ConfigureAccurateRip::ConfigPathDefault); |
| 65 | |
| 66 | if (!base.EndsWith("/")) base.Append("/"); |
| 67 | |
| 68 | String url = String(base).Append("DriveOffsets.bin"); |
| 69 | Protocols::HTTP protocol(url); |
| 70 | |
| 71 | protocol.DownloadToFile(offsetsFile); |
| 72 | |
| 73 | if (!protocol.GetResponseHeaderField("Status-Code").StartsWith("200")) File(offsetsFile).Delete(); |
| 74 | } |
| 75 | |
| 76 | if (!File(offsetsFile).Exists()) return False; |
| 77 | |
| 78 | /* Parse offsets file. |
| 79 | */ |
| 80 | InStream in(STREAM_FILE, offsetsFile); |
| 81 | |
| 82 | while (in.GetPos() < in.Size()) |
| 83 | { |
| 84 | DriveOffset offset; |
| 85 | |
| 86 | /* Read drive's sample offset. |
| 87 | */ |
| 88 | offset.offset = (Int16) in.InputNumber(2); |
| 89 | |
| 90 | /* Read drive ID string. |
| 91 | */ |
| 92 | String id = in.InputString(32); |
| 93 | |
| 94 | offset.vendor = id.Head(id.Find("- ")).Trim(); |
| 95 | offset.model = id.Tail(id.Length() - id.Find("- ") - 2).Trim(); |
| 96 | |
| 97 | /* Skip rest of chunk. |
| 98 | */ |
| 99 | in.RelSeek(1); // Drive ID terminator |
| 100 | in.RelSeek(1); // Number of submissions |
| 101 | in.RelSeek(32); // Unknown 32 byte string |
| 102 | in.RelSeek(1); // Unknown string terminator |
| 103 | |
| 104 | /* Add drive to list of offsets. |
| 105 | */ |
| 106 | driveOffsets.Add(offset); |
| 107 | } |
| 108 | |
| 109 | return True; |
| 110 | } |
| 111 | |
| 112 | const BoCA::DriveOffset &BoCA::DriveOffsets::FindDriveOffset(const Device &device) |
| 113 | { |
| 114 | static DriveOffset invalidOffset; |
| 115 | |
| 116 | if (driveOffsets.Length() == 0) LoadDriveOffsets(); |
| 117 | |
| 118 | foreach (const DriveOffset &offset, driveOffsets) |
| 119 | { |
| 120 | if (offset.vendor == device.vendor && |
| 121 | offset.model == device.model) return offset; |
| 122 | } |
| 123 | |
| 124 | return invalidOffset; |
| 125 | } |