diff --git a/Source/Common/Preferences.cpp b/Source/Common/Preferences.cpp index 743617a46..732530509 100644 --- a/Source/Common/Preferences.cpp +++ b/Source/Common/Preferences.cpp @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include using namespace ZenLib; #ifdef MEDIAINFO_DLL_RUNTIME #include "MediaInfoDLL/MediaInfoDLL.h" @@ -39,6 +42,33 @@ const Ztring Empty_Ztring_Ref=__T(""); int ExplorerShell_Edit (const AnsiString &Name, bool ShellExtension, bool &IsChanged); //--------------------------------------------------------------------------- +//*************************************************************************** +// Helper +//*************************************************************************** +size_t WeightedRandomIndex(size_t Size, const std::vector& Weights) +{ + if (!Size) + return 0; + + size_t Total=std::accumulate(Weights.begin(), Weights.end(), 0ULL); + + if (Total==0) + return 0; + + static std::mt19937 Generation(std::chrono::high_resolution_clock::now().time_since_epoch().count()); + std::uniform_int_distribution Distribution(0, Total-1); + + size_t Result=Distribution(Generation), Cumulative=0; + for (size_t Pos=0; PosConfig(__T("Sponsored"))=__T("0"); Prefs->Config(__T("SponsorMessage"))=__T(""); Prefs->Config(__T("SponsorUrl"))=__T(""); + Prefs->Config(__T("SponsorMessageWeight"))=__T("";) Prefs->Config(__T("SponsorBanner"))=__T(""); Prefs->Config(__T("SponsorBannerClickUrl"))=__T(""); + Prefs->Config(__T("SponsorBannerWeight"))=__T(""); ZtringListList Sponsor=Download.SubSheet(__T("ShowSponsor")); unsigned int En=Sponsor.Find(__T("en"), 1); if (En!=(unsigned int)-1) @@ -542,26 +586,90 @@ void __fastcall ThreadInternetCheck::Execute() Prefs->Config(__T("Sponsored"))=__T("1"); Ztring Message; Ztring Url; + Ztring MessageWeight; Ztring Banner; Ztring BannerClickUrl; + Ztring BannerWeight; for (size_t Pos=0; Pos1 && Sponsor[Pos][1]!=__T("")) { - if (Sponsor[Pos](2)!=__T("")) - Message+=(Message.empty()?__T(""):__T("\\r\\n"))+Sponsor[Pos](1)+__T(";")+Sponsor[Pos](2); - if (Sponsor[Pos](3)!=__T("")) - Url+=(Url.empty()?__T(""):__T("\\r\\n"))+Sponsor[Pos](1)+__T(";")+Sponsor[Pos](3); - if (Sponsor[Pos](4)!=__T("")) - Banner+=(Banner.empty()?__T(""):__T("\\r\\n"))+Sponsor[Pos](1)+__T(";")+Sponsor[Pos](4); - if (Sponsor[Pos](5)!=__T("")) - BannerClickUrl+=(BannerClickUrl.empty()?__T(""):__T("\\r\\n"))+Sponsor[Pos](1)+__T(";")+Sponsor[Pos](5); + for (size_t Pos2=1; Pos2Pos2+1 && Sponsor[Pos][Pos2+1]!=__T("")) + { + if (!Message.empty() && Pos2==1) + Message+=__T("\\r\\n"); + if (Pos2==1) + Message+=Sponsor[Pos][1]; + + Message+=__T(";")+Sponsor[Pos][Pos2+1]; + } + if (Sponsor[Pos].size()>Pos2+2 && Sponsor[Pos][Pos2+2]!=__T("")) + { + if (!Url.empty() && Pos2==1) + Url+=__T("\\r\\n"); + if (Pos2==1) + Url+=Sponsor[Pos][1]; + + Url+=__T(";")+Sponsor[Pos][Pos2+2]; + } + if (Sponsor[Pos].size()>Pos2+3 && Sponsor[Pos][Pos2+3]!=__T("")) + { + if (!Banner.empty() && Pos2==1) + Banner+=__T("\\r\\n"); + if (Pos2==1) + Banner+=Sponsor[Pos][1]; + + Banner+=__T(";")+Sponsor[Pos][Pos2+3]; + } + if (Sponsor[Pos].size()>Pos2+4 && Sponsor[Pos][Pos2+4]!=__T("")) + { + if (!BannerClickUrl.empty() && Pos2==1) + BannerClickUrl+=__T("\\r\\n"); + if (Pos2==1) + BannerClickUrl+=Sponsor[Pos][1]; + BannerClickUrl+=__T(";")+Sponsor[Pos][Pos2+4]; + } + + ZtringList Weights; + Weights.Separator_Set(0, __T(",")); + if (Sponsor[Pos].size()>Pos2+5) + Weights.Write(Sponsor[Pos][Pos2+5]); + + if (Sponsor[Pos].size()>Pos2+1 && Sponsor[Pos][Pos2+1]!=__T("")) + { + if (!MessageWeight.empty() && Pos2==1) + MessageWeight+=__T("\\r\\n"); + if (Pos2==1) + MessageWeight+=Sponsor[Pos][1]; + if (Weights.size()>0 && Weights[0]!=__T("")) + MessageWeight+=__T(";")+Weights[0]; + else + MessageWeight+=__T(";50"); + } + + if (Sponsor[Pos].size()>Pos2+3 && Sponsor[Pos][Pos2+3]!=__T("")) + { + if (!BannerWeight.empty() && Pos2==1) + BannerWeight+=__T("\\r\\n"); + if (Pos2==1) + BannerWeight+=Sponsor[Pos][1]; + if (Weights.size()>1 && Weights[1]!=__T("")) + + BannerWeight+=__T(";")+Weights[1]; + else + BannerWeight+=__T(";50"); + } + } } } Prefs->Config(__T("SponsorMessage"))=Message.Quote(); Prefs->Config(__T("SponsorUrl"))=Url.Quote(); + Prefs->Config(__T("SponsorMessageWeight"))=MessageWeight.Quote(); Prefs->Config(__T("SponsorBanner"))=Banner.Quote(); Prefs->Config(__T("SponsorBannerClickUrl"))=BannerClickUrl.Quote(); + Prefs->Config(__T("SponsorBannerWeight"))=BannerWeight.Quote(); } Prefs->Config_Save(); @@ -594,6 +702,7 @@ void __fastcall ThreadInternetCheck::Execute() Prefs->Config(__T("GraphPluginURL"))=GraphPluginURL; Prefs->Config_Save(); } + Ztring GraphPluginVersion=Download(__T("GraphPluginVersion")); if (!GraphPluginVersion.empty()) { @@ -1658,13 +1767,24 @@ const ZenLib::Ztring &Preferences::Translate(ZenLib::Ztring Name) if (Name==__T("SponsorMessage")) { int Index=SponsorMessage.Find(Language, 0); - if (Index==-1 || SponsorMessage(Index)(1)==__T("")) + if (Index==-1) Index=SponsorMessage.Find(__T("en"), 0); - if (Index==-1 || SponsorMessage(Index).empty()) + if (Index==-1) return Empty_Ztring_Ref; - return SponsorMessage(Index)(1); + if (Index2) + { + std::vector Weights; + for (size_t Pos=1; Pos2) + { + std::vector Weights; + for (size_t Pos=1; Pos #include -//--------------------------------------------------------------------------- +#include +#include +#include //--------------------------------------------------------------------------- #include "GUI_Sponsor.h" @@ -114,6 +116,36 @@ void __fastcall TSponsorFrame::Init() SourceURL = Prefs->Translate(__T("SponsorBanner")); BannerClickUrl = Prefs->Translate(__T("SponsorBannerClickUrl")); + // Clean olds banners + ZtringList FileNames=Dir::GetAllFileNames(Prefs->BaseFolder + __T("\\Sponsor\\"), Dir::Include_Files); + for (size_t Pos=0; PosSponsorBanner.size(); Pos2++) + { + for (size_t Pos3=0; Pos3SponsorBanner[Pos2].size(); Pos3++) + { + Ztring Hash=Ztring().From_Unicode(THashMD5::GetHashString(Prefs->SponsorBanner[Pos2][Pos3].c_str()).c_str()); + if (Name.Name_Get()==Hash) + { + std::string name=Name.Name_Get().To_UTF8(); + std::string name2=Ztring().From_Unicode(THashMD5::GetHashString(Prefs->SponsorBanner[Pos2][Pos3].c_str()).c_str()).To_UTF8(); + Found=true; + break; + } + } + if (Found) + break; + } + + if (!Found) + File::Delete(FileNames[Pos]); + } + } + if (!Prefs->Sponsored || Prefs->Donated || SourceURL.empty() || @@ -129,15 +161,12 @@ void __fastcall TSponsorFrame::Init() Ztring NewBanner = Ztring().From_Unicode(THashMD5::GetHashString(SourceURL.c_str()).c_str()) + __T(".jpg"); BannerPath = Prefs->BaseFolder + __T("\\Sponsor\\") + NewBanner; - if (CurBanner == NewBanner) + if (CurBanner == NewBanner && File::Exists(BannerPath)) { Finalize(); return; } - if (!CurBanner.empty()) - File::Delete(Prefs->BaseFolder + __T("\\Sponsor\\") + CurBanner); - DownloadThread = new DownloadBannerThread(this, SourceURL, BannerPath); }