You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RnQ/RnQ/ICQ/Stickers.pas

189 lines
4.7 KiB
Plaintext

unit Stickers;
{$I forRnQConfig.inc}
interface
uses
Windows, Classes, SysUtils, Generics.Collections, JSON;
{$I PubRTTI.inc}
type
PMemoryStream = ^TMemoryStream;
TStickerPack = record
Id: Integer;
StoreId: String;
Name: String;
Desc: String;
Purchased: Boolean;
UserSticker: Boolean;
IsEnabled: Boolean;
Priority: Integer;
Count: Integer;
class function FromJSON(const JSON: TJSONObject): TStickerPack; static;
end;
TStickerPacks = array of TStickerPack;
function GetSticker(const Ext, Sticker: String; pifs: TMemoryStream = nil; const ForceSize: String = ''): TBytes;
// function CheckStickerCache(Ext, Sticker: Integer; const ForceSize: String = ''): Boolean;
// function GetCachedStickers: String;
function GetCachedPickers: String;
procedure RemoveStickerPackCache(const PackId: String);
var
HiddenStickerPacks, DupStickerPacks: TList;
implementation
uses
Base64, RDGlobal, RnQGlobal, RnQNet, SQLiteDB, globalLib, utilLib;
function GetSticker(const Ext, Sticker: String; pifs: TMemoryStream = nil; const ForceSize: String = ''): TBytes;
var
URL, Size: String;
fn: String;
fs: TMemoryStream;
begin
if pifs = nil then
fs := TMemoryStream.Create
else
fs := pifs;
if not (ForceSize = '') then
Size := ForceSize
else
case StickerResolution of
0:
Size := 'small';
1:
Size := 'medium';
2:
Size := 'large';
end;
if Sticker = '' then
begin
URL := 'https://c.icq.com/store/stickers/' + Ext + '/sticker-picker/small.png';
fn := StickerPath + Ext + '_picker_small.png';
end
else
begin
URL := 'https://www.icq.com/store/stickers/' + Ext + '/' + Sticker + '/' + Size;
fn := StickerPath + Ext + '_' + Sticker + '_' + Size + '.png';
end;
if not FileExists(fn) then
begin
if not DirectoryExists(StickerPath) then
ForceDirectories(StickerPath);
LoadFromURL(URL, fn);
end;
if FileExists(fn) then
fs.LoadFromFile(fn);
fs.Seek(0, 0);
SetLength(Result, fs.size);
fs.ReadBuffer(Result, fs.size);
if pifs = nil then
fs.Free
else
fs.Seek(0, 0);
end;
procedure RemoveStickerPackCache(const PackId: String);
var
sr: TSearchRec;
begin
if FindFirst(StickerPath + PackId + '_*_*.png', faAnyFile, sr) = 0 then
repeat
if not (sr.name = '.') and not (sr.name = '..') then
DeleteFile(StickerPath + sr.name);
until FindNext(sr) <> 0;
FindClose(sr);
end;
{
function CheckStickerCache(Ext, Sticker: Integer; const ForceSize: String = ''): Boolean;
var
Size: String;
fn: String;
begin
if not (ForceSize = '') then
Size := ForceSize
else
case StickerResolution of
0:
Size := 'small';
1:
Size := 'medium';
2:
Size := 'large';
end;
fn := StickerPath + IntToStr(Ext) + '_' + IntToStr(Sticker) + '_' + Size + '.png';
Result := FileExists(fn);
end;
function GetCachedStickers: String;
var
i, j: Integer;
begin
Result := '';
for i := 0 to Length(StickerCats) - 1 do
for j := 1 to StickerCatCounts[i] do
if CheckStickerCache(StickerCats[i], j, 'small') then
Result := Result + 'sticker:n' + IntToStr(StickerCats[i]) + '_' + IntToStr(j) + #10;
Result := Trim(Result);
end;
}
function CheckPickerCache(Ext: Integer): Boolean;
begin
Result := FileExists(StickerPath + IntToStr(Ext) + '_picker_small.png');
end;
function GetCachedPickers: String;
var
i: Integer;
s: TStickerPacks;
begin
Result := '';
s := SQLDB.GetStickerPacks(True);
for i := 0 to Length(s) - 1 do
if CheckPickerCache(s[i].Id) then
Result := Result + 'picker:n' + IntToStr(s[i].Id) + #10;
Result := Trim(Result);
end;
class function TStickerPack.FromJSON(const JSON: TJSONObject): TStickerPack;
begin
Result := Default(TStickerPack);
with JSON do
begin
GetValueSafe('id', Result.Id);
GetValueSafe('store_id', Result.StoreId);
GetValueSafe('name', Result.Name);
GetValueSafe('description', Result.Desc);
GetValueSafe('count', Result.Count);
GetValueSafe('purchased', Result.Purchased);
GetValueSafe('usersticker', Result.UserSticker);
GetValueSafe('priority', Result.Priority);
GetValueSafe('is_enabled', Result.IsEnabled);
end;
end;
initialization
HiddenStickerPacks := TList.Create;
HiddenStickerPacks.AddRange([87, 108, 205, 209]);
DupStickerPacks := TList.Create;
DupStickerPacks.AddRange([116288, 194855, 234247 {Cute Pigs - Incomplete set}]);
finalization
HiddenStickerPacks.Free;
DupStickerPacks.Free;
end.