1 #include "stdafx.h" 2 #include "LastFMDataArchive.h" 3 #include "MusicPlayer2.h" 4 #include <time.h> 5 #include "AudioTag.h" 6 7 void LastFMTrack::Clear() { 8 artist = L""; 9 track = L""; 10 timestamp = 0; 11 album = L""; 12 streamId = L""; 13 chosenByUser = true; 14 trackNumber = 0; 15 mbid = L""; 16 albumArtist = L""; 17 duration.fromInt(0); 18 } 19 20 void LastFMTrack::SaveDataTo(CArchive& ar) { 21 ar << CString(artist.c_str()); 22 ar << CString(track.c_str()); 23 ar << timestamp; 24 ar << CString(album.c_str()); 25 ar << CString(streamId.c_str()); 26 ar << chosenByUser; 27 ar << trackNumber; 28 ar << CString(mbid.c_str()); 29 ar << CString(albumArtist.c_str()); 30 ar << (int32_t)duration.toInt(); 31 } 32 33 void LastFMTrack::ReadDataFrom(CArchive& ar) { 34 CString temp; 35 ar >> temp; 36 artist = temp; 37 ar >> temp; 38 track = temp; 39 ar >> timestamp; 40 ar >> temp; 41 album = temp; 42 ar >> temp; 43 streamId = temp; 44 ar >> chosenByUser; 45 ar >> trackNumber; 46 ar >> temp; 47 mbid = temp; 48 ar >> temp; 49 albumArtist = temp; 50 int32_t d; 51 ar >> d; 52 duration.fromInt((int)d); 53 } 54 55 void LastFMTrack::ReadDataFrom(SongInfo info) { 56 Clear(); 57 if (!info.artist.empty()) { 58 artist = info.artist; 59 } 60 if (!info.title.empty()) { 61 track = info.title; 62 } 63 __time64_t tm; 64 _time64(&tm); 65 timestamp = tm; 66 if (!info.album.empty()) { 67 album = info.album; 68 } 69 trackNumber = info.track; 70 duration = info.length(); 71 CAudioTag tag(info); 72 std::map<wstring, wstring> property_map; 73 tag.GetAudioTagPropertyMap(property_map); 74 for (const auto& prop : property_map) { 75 if (prop.first == L"ALBUMARTIST") { 76 albumArtist = prop.second; 77 } 78 } 79 } 80 81 bool LastFMTrack::operator==(const LastFMTrack& track) { 82 return artist == track.artist && 83 this->track == track.track && 84 album == track.album && 85 trackNumber == track.trackNumber && 86 albumArtist == track.albumArtist && 87 duration == track.duration; 88 } 89 90 bool LastFMTrack::operator==(const SongInfo& info) { 91 return artist == info.artist && 92 track == info.title && 93 album == info.album && 94 trackNumber == info.track && 95 duration == info.length(); 96 } 97 98 void LastFMDataArchive::SaveData(wstring path) { 99 CFile file; 100 BOOL bRet = file.Open(path.c_str(), CFile::modeCreate | CFile::modeWrite); 101 if (!bRet) { 102 return; 103 } 104 CArchive ar(&file, CArchive::store); 105 /// �汾�� 106 ar << (uint16_t)1; 107 ar << CString(session_key.c_str()); 108 ar << CString(user_name.c_str()); 109 ar << current_played_time; 110 ar << is_pushed; 111 current_track.SaveDataTo(ar); 112 corrected_current_track.SaveDataTo(ar); 113 ar << (uint64_t)cached_tracks.size(); 114 for (auto i = cached_tracks.begin(); i != cached_tracks.end(); i++) { 115 auto& track = *i; 116 track.SaveDataTo(ar); 117 } 118 ar.Close(); 119 file.Close(); 120 } 121 122 void LastFMDataArchive::LoadData(wstring path) { 123 CFile file; 124 BOOL bRet = file.Open(path.c_str(), CFile::modeRead); 125 if (!bRet) { 126 return; 127 } 128 CArchive ar(&file, CArchive::load); 129 try { 130 uint16_t version; 131 ar >> version; 132 if (version > 1) { 133 return; 134 } 135 CString temp; 136 ar >> temp; 137 session_key = temp; 138 ar >> temp; 139 user_name = temp; 140 if (version > 0) { 141 ar >> current_played_time; 142 } else { 143 current_played_time = 0; 144 } 145 if (version > 0) { 146 ar >> is_pushed; 147 } else { 148 is_pushed = false; 149 } 150 current_track.ReadDataFrom(ar); 151 corrected_current_track.ReadDataFrom(ar); 152 uint64_t size; 153 ar >> size; 154 cached_tracks.clear(); 155 for (uint64_t i = 0; i < size; i++) { 156 LastFMTrack track; 157 track.ReadDataFrom(ar); 158 cached_tracks.push_back(track); 159 } 160 } catch (CArchiveException* exception) { 161 wstring info = theApp.m_str_table.LoadTextFormat(L"MSG_SERIALIZE_ERROR", { path, exception->m_cause }); 162 theApp.WriteLog(info); 163 } 164 ar.Close(); 165 file.Close(); 166 } 167