Files
ttw_fmx_v10/uGeneral.pas
T

475 lines
13 KiB
ObjectPascal

unit uGeneral;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.TabControl,
FMX.Controls.Presentation, FMX.StdCtrls, System.ImageList, FMX.ImgList,
FMX.Styles, ShellAPI, StrUtils,
fSettings, fAI, fNotify, fAutoActions, FMX.ListBox, fLog, uRecords,
System.IOUtils, fCommands, uDataBase, FMX.Edit, FMX.Colors, FMX.SpinBox,
windows, System.Skia, FMX.Skia, uCreateChat, uCreateNotify, fOBS;
type
TTTW_Bot = class(TForm)
V: TTabControl;
TabItem1: TTabItem;
TabItem2: TTabItem;
TabItem3: TTabItem;
TabItem4: TTabItem;
frSettings1: TfrSettings;
ImageList1: TImageList;
TabItem5: TTabItem;
Panel1: TPanel;
btnConnecting: TButton;
Label2: TLabel;
Label3: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
aiConnecting: TAniIndicator;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
frAI1: TfrAI;
TabItem6: TTabItem;
TabItem7: TTabItem;
TabItem8: TTabItem;
TabItem9: TTabItem;
frNotify1: TfrNotify;
Label1: TLabel;
frAutoActions1: TfrAutoActions;
frOBS1: TfrOBS;
frLog1: TfrLog;
cbTheme: TComboBox;
Label15: TLabel;
frCommands1: TfrCommands;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
SpeedButton3: TSpeedButton;
btnCreateChat: TButton;
procedure cbThemeChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton3Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
procedure frChatOBS1btnCreateChatClick(Sender: TObject);
procedure frOBS1btnCreateOBSNotifyClick(Sender: TObject);
procedure frOBS1btnCreateOBSKandinskyClick(Sender: TObject);
procedure frSettings1btnDAStartClick(Sender: TObject);
procedure frCommands1btnRandAddClick(Sender: TObject);
private
{ Private declarations }
procedure ReadDB();
public
{ Public declarations }
end;
var
TTW_Bot: TTTW_Bot;
myConst: TConst;
db: TSettingsDatabase;
appconst: TBotAppCfg;
implementation
{$R *.fmx}
procedure TTTW_Bot.cbThemeChange(Sender: TObject);
begin
cbTheme.ItemIndex := cbTheme.Items.IndexOf(cbTheme.text);
if cbTheme.ItemIndex <> -1 then
TStyleManager.SetStyleFromFile(myConst.stlPath + cbTheme.text);
// db.WriteSetting('cbTheme', inttostr(cbTheme.ItemIndex));
end;
procedure TTTW_Bot.FormCreate(Sender: TObject);
var
Path: string;
SearchRec: TSearchRec;
function GetPathToTestExe: string; // âåðíåò ïàïêó romaming
begin
Result := GetEnvironmentVariable('APPDATA');
if Result <> '' then
Result := IncludeTrailingPathDelimiter(Result);
end;
begin
myConst.GeneralPath := ExtractFilePath(ParamStr(0));
myConst.AppDataPath := GetPathToTestExe + 'TTW_Bot\';
if not DirectoryExists(myConst.AppDataPath) then
CreateDir(myConst.AppDataPath);
myConst.DBPath := myConst.AppDataPath + 'settings.db';
if not DirectoryExists(myConst.AppDataPath + 'fonts') then
CreateDir(myConst.AppDataPath + 'fonts');
myConst.fontsPath := myConst.AppDataPath + 'fonts\';
if not DirectoryExists(myConst.AppDataPath + 'imgs') then
CreateDir(myConst.AppDataPath + 'imgs');
myConst.imgsPath := myConst.AppDataPath + 'imgs\';
if not DirectoryExists(myConst.AppDataPath + 'sounds') then
CreateDir(myConst.AppDataPath + 'sounds');
myConst.soundsPath := myConst.AppDataPath + 'sounds\';
if not DirectoryExists(myConst.AppDataPath + 'stl') then
CreateDir(myConst.AppDataPath + 'stl');
myConst.stlPath := myConst.AppDataPath + 'stl\';
if not DirectoryExists(myConst.AppDataPath + 'ytSongs') then
CreateDir(myConst.AppDataPath + 'ytSongs');
myConst.ytSongsPath := myConst.AppDataPath + 'ytSongs\';
myConst.PublicPlay := myConst.GeneralPath + 'PublicPlay.exe';
myConst.SilentPlay := myConst.GeneralPath + 'SilentPlayer.exe';
myConst.ytPlay := myConst.GeneralPath + 'Player.exe';
myConst.cfg1 := myConst.GeneralPath + 'botapp.cfg';
db := TSettingsDatabase.Create(myConst.DBPath);
ReadDB;
for Path in TDirectory.GetFiles(myConst.stlPath) do
cbTheme.Items.Add(ExtractFileName(Path));
cbTheme.ItemIndex := strtoint(db.ReadSetting('cbTheme', '-1'));
end;
procedure TTTW_Bot.frChatOBS1btnCreateChatClick(Sender: TObject);
begin
fCreateChat.Show;
end;
procedure TTTW_Bot.frCommands1btnRandAddClick(Sender: TObject);
begin
frCommands1.btnRandAddClick(Sender);
end;
procedure TTTW_Bot.frOBS1btnCreateOBSKandinskyClick(Sender: TObject);
var dport:integer;
I: Integer;
begin
dport:=8080;
for I := 0 to frOBS1.sgWebChats.RowCount-1 do
begin
if strtoint(frOBS1.sgWebChats.Cells[0,i]) >= dport then
dport:=strtoint(frOBS1.sgWebChats.Cells[0,i])+1;
end;
frOBS1.sgWebChats.RowCount:=frOBS1.sgWebChats.RowCount+1;
frOBS1.sgWebChats.Cells[0,frOBS1.sgWebChats.RowCount-1]:=inttostr(dport);
frOBS1.sgWebChats.Cells[1,frOBS1.sgWebChats.RowCount-1]:='Kandinsky';
frOBS1.sgWebChats.Cells[2,frOBS1.sgWebChats.RowCount-1]:='http://127.0.0.1:'+inttostr(dport);
end;
procedure TTTW_Bot.frOBS1btnCreateOBSNotifyClick(Sender: TObject);
begin
fCreateNotify.Show;
end;
procedure TTTW_Bot.frSettings1btnDAStartClick(Sender: TObject);
begin
frSettings1.btnDAStartClick(Sender);
end;
procedure TTTW_Bot.ReadDB;
var
I: Integer;
c: TComponent;
sl: TStringList;
SavedColor: TAlphaColor;
ColorStr: string;
function XorDecryptToStrings(const InputFile, Key: string): TStrings;
var
InStream: TFileStream;
MemStream: TMemoryStream;
KeyBytes: TBytes;
KeyLen, KeyIndex: Integer;
B: Byte;
begin
// Ïðåîáðàçóåì êëþ÷ â áàéòû ñ èñïîëüçîâàíèåì ANSI êîäèðîâêè
KeyBytes := TEncoding.ANSI.GetBytes(Key);
KeyLen := Length(KeyBytes);
if KeyLen = 0 then
raise Exception.Create('Êëþ÷ íå ìîæåò áûòü ïóñòûì');
InStream := TFileStream.Create(InputFile, fmOpenRead);
try
MemStream := TMemoryStream.Create;
try
KeyIndex := 0;
// Ðàñøèôðîâûâàåì äàííûå è çàïèñûâàåì â ïîòîê â ïàìÿòè
while InStream.Position < InStream.Size do
begin
InStream.ReadBuffer(B, 1);
B := B xor KeyBytes[KeyIndex];
MemStream.WriteBuffer(B, 1);
KeyIndex := (KeyIndex + 1) mod KeyLen;
end;
// Ïðåîáðàçóåì äàííûå èç ïîòîêà â TStrings
MemStream.Position := 0; // Ñáðàñûâàåì ïîçèöèþ äëÿ ÷òåíèÿ
Result := TStringList.Create;
try
// Èñïîëüçóåì ANSI êîäèðîâêó äëÿ ïðåîáðàçîâàíèÿ áàéòîâ â ñòðîêó
Result.LoadFromStream(MemStream, TEncoding.ANSI);
except
//  ñëó÷àå îøèáêè îñâîáîæäàåì ðåñóðñû è ïðîáðàñûâàåì èñêëþ÷åíèå
Result.Free;
raise;
end;
finally
MemStream.Free;
end;
finally
InStream.Free;
end;
end;
// Çàãðóçêà êîìïîíåíòîâ íàñòðîåê (TEdit, TCheckBox)
procedure LoadSettingsComponents;
var
I: Integer;
c: TComponent;
begin
for I := 0 to frSettings1.ComponentCount - 1 do
begin
c := frSettings1.Components[I];
if c is TEdit then
TEdit(c).text := db.ReadSetting(TEdit(c).Name)
else if c is TCheckBox then
TCheckBox(c).IsChecked := (db.ReadSetting(TCheckBox(c).Name) = 'True');
end;
db.FChannel := frSettings1.edtChannel.text;
end;
// Çàãðóçêà äàííûõ â ãðèäû êîìàíä
procedure LoadGridsData;
begin
DB.LoadRecordArray<TRandomCounters>('RandomCounters', frCommands1.RandomCounters);
{ db.LoadGridFromTable('sgRandomInt', frCommands1.sgRandomInt);
db.LoadGridFromTable('sgCommands', frCommands1.sgCommands);
db.LoadGridFromTable('sgSAFiles', frCommands1.sgSAFiles);
db.LoadGridFromTable('sgTFiles', frCommands1.sgTFiles);
db.LoadGridFromTable('sgAIGen', frCommands1.sgAIGen); }
end;
// Çàãðóçêà ñïèñêà ãðóïï
procedure LoadGroupNames;
begin
db.getGroupName(frCommands1.lbRandomGroup.Items);
end;
// Çàãðóçêà çàøèôðîâàííîãî êîíôèãà
procedure LoadEncryptedConfig;
var
sl: TStringList;
I: Integer;
begin
if not FileExists(myConst.cfg1) then
Exit;
sl := TStringList.Create;
try
sl.Assign(XorDecryptToStrings(myConst.cfg1, 'fgvasrgEFAXFAFAS'));
for I := 0 to sl.Count - 1 do
begin
var
eqPos := Pos('=', sl[I]);
if eqPos > 0 then
begin
var
Key := Trim(Copy(sl[I], 1, eqPos - 1));
var
Value := Trim(Copy(sl[I], eqPos + 1, MaxInt));
if Key = 'k1' then
appconst.TTV_ClientID := Value
else if Key = 'k2' then
appconst.AI_GigaChat_AC := Value
else if Key = 'k3' then
appconst.AI_GigaChat_ClientID := Value
else if Key = 'k4' then
appconst.AI_ChatGPT_Token := Value
else if Key = 'k5' then
appconst.AI_DeepSeec_Token := Value
else if Key = 'k6' then
appconst.DA_ClientID := Value
else if Key = 'k7' then
appconst.DA_Sicret := Value
else if Key = 'k8' then
appconst.DA_URL := Value;
end;
end;
frSettings1.btnGetClientID.Visible := (appconst.TTV_ClientID <> '');
frAI1.btnGetAIDef.Visible := ((appconst.AI_GigaChat_AC <> '') and
(appconst.AI_GigaChat_ClientID <> '')) or
(appconst.AI_ChatGPT_Token <> '') or (appconst.AI_DeepSeec_Token <> '');
frSettings1.btnGetDADef.Visible := (appconst.DA_ClientID <> '') and
(appconst.DA_Sicret <> '') and (appconst.DA_URL <> '');
finally
sl.Free;
end;
end;
// Çàãðóçêà íàñòðîåê óâåäîìëåíèé
procedure LoadNotifySettings;
var
I: Integer;
c: TComponent;
begin
for I := 0 to frNotify1.ComponentCount - 1 do
begin
c := frNotify1.Components[I];
if c is TEdit then
TEdit(c).text := db.ReadSetting(TEdit(c).Name)
else if c is TCheckBox then
TCheckBox(c).IsChecked := (db.ReadSetting(TCheckBox(c).Name) = 'True')
else if c is TSwitch then
TSwitch(c).IsChecked := (db.ReadSetting(TSwitch(c).Name) = 'True')
else if c is TTrackBar then
TTrackBar(c).Value :=
strtoint(db.ReadSetting(TTrackBar(c).Name, '100'));
end;
end;
// Çàãðóçêà íàñòðîåê ÈÈ
procedure LoadAISettings;
var
I: Integer;
c: TComponent;
ii: Integer;
// Íàñòðîéêè GigaChat
procedure SetupGigaChatSettings;
begin
frAI1.rbGC.IsChecked := True;
frAI1.Label45.text := 'ClientID';
frAI1.Label47.text := 'Autorization Code';
frAI1.Label1.Visible := False;
frAI1.edtAIP2.Visible := True;
frAI1.edtAIP2.Password := True;
frAI1.edtAIP3.Visible := False;
frAI1.cbOllama.Visible := False;
end;
// Íàñòðîéêè DeepSeek
procedure SetupDeepSeekSettings;
begin
frAI1.rbDS.IsChecked := True;
frAI1.Label45.text := 'API Token';
frAI1.Label47.text := '';
frAI1.Label1.Visible := False;
frAI1.edtAIP2.Visible := False;
frAI1.edtAIP3.Visible := False;
frAI1.cbOllama.Visible := False;
end;
// Íàñòðîéêè ChatGPT
procedure SetupChatGPTSettings;
begin
frAI1.rbCG.IsChecked := True;
frAI1.Label45.text := 'API Token';
frAI1.Label47.text := '';
frAI1.Label1.Visible := False;
frAI1.edtAIP2.Visible := False;
frAI1.edtAIP3.Visible := False;
frAI1.cbOllama.Visible := False;
end;
// Íàñòðîéêè êàñòîìíîãî ÈÈ
procedure SetupCustomAISettings;
begin
frAI1.RBCustom.IsChecked := True;
frAI1.Label45.text := 'API Token';
frAI1.Label47.text := 'URL';
frAI1.Label1.Visible := True;
frAI1.edtAIP2.Visible := True;
frAI1.edtAIP2.Password := False;
frAI1.edtAIP3.Visible := True;
frAI1.cbOllama.Visible := True;
frAI1.cbOllama.IsChecked := db.ReadSetting(frAI1.cbOllama.Name) = '1';
end;
begin
for I := 0 to frAI1.ComponentCount - 1 do
begin
c := frAI1.Components[I];
if c is TEdit then
TEdit(c).text := db.ReadSetting(TEdit(c).Name)
else if c is TCheckBox then
TCheckBox(c).IsChecked := db.ReadSetting(TCheckBox(c).Name) = '1';
end;
ii := strtoint(db.ReadSetting('aiIndex', '0'));
case ii of
0:
SetupGigaChatSettings;
1:
SetupDeepSeekSettings;
2:
SetupChatGPTSettings;
3:
SetupCustomAISettings;
end;
frSettings1.init;
end;
// Çàãðóçêà ãðèäîâ àâòîìàòè÷åñêèõ äåéñòâèé
procedure LoadAutoActionsGrids;
begin
db.LoadGridFromTable('sgTimers', frAutoActions1.sgTimers);
db.LoadGridFromTable('sgCounter', frAutoActions1.sgCounter);
db.LoadGridFromTable('sgBanWords', frAutoActions1.sgBanWords);
end;
begin
LoadSettingsComponents;
LoadGridsData;
LoadGroupNames;
LoadEncryptedConfig;
LoadNotifySettings;
LoadAISettings;
LoadAutoActionsGrids;
end;
procedure TTTW_Bot.SpeedButton1Click(Sender: TObject);
begin
ShellExecute(0, 'open', pwidechar('https://www.twitch.tv/incadence'),
nil, nil, 1);
end;
procedure TTTW_Bot.SpeedButton2Click(Sender: TObject);
begin
// https://www.twitch.tv/kuznecogr
ShellExecute(0, 'open', pwidechar('https://www.twitch.tv/kuznecogr'),
nil, nil, 1);
end;
procedure TTTW_Bot.SpeedButton3Click(Sender: TObject);
begin
// https://www.flaticon.com/ru/authors/karacis
ShellExecute(0, 'open',
pwidechar('https://www.flaticon.com/ru/authors/karacis'), nil, nil, 1);
end;
end.