CompilerIf #PB_Editor_CreateExecutable Declare FatalError() G_FatalErrorStatus = 0 OnErrorCall(@FatalError()) #Debug_Mode = #False CompilerElse #Debug_Mode = #True CompilerEndIf CompilerIf #PB_Compiler_Version>=510 UseBriefLZPacker() CompilerEndIf Declare.b Torrent_GetArray_MapPiece(Array MapPiece.a(1), NumberPiece) Declare Core_Times_Thread(*x) Declare SetIcon_TorrentListIcon(Gadget, Item, State) Declare Tracker_Stopped_All_Tracker() Declare Window_LabelTorrent() Declare.a Test_LockMutex(Mutex, TimeOut_ms.u) Declare.b SaveTorrentData() Declare End_Program() Declare.b TestNumber(String.s) Declare Core_NetworkEvent_Thread(*x) Declare.a Peer_Download(*Local_TorrentList) Declare Peer_Download_InPeers(*Local_TorrentList) Declare Core_Network_ConnectPeer_Thread(*x) Declare Peer_Download_Calc_MaxRequest(*Local_TorrentList) Declare Peer_Download_FindSleepPiece(*Local_TorrentList) Declare Peer_Send_Cancel(*Local_TorrentList, Piece.l, Begin.l, Length.l) Declare Peer_Create_Request(Piece.l, Begin.l, Length.l) Declare.a Peer_AddList_OutData(*Local_TorrentList, *Mem, Size) Declare Peer_Upload_New(*Local_TorrentList) Declare Peer_Upload_Refresh(*Local_TorrentList) Declare Peer_Upload_RandomConnect(*Local_TorrentList) Prototype DrawThemeParentBackground(hWnd, hDC, *Rect) Prototype WSACreateEvent() Prototype WSAEventSelect(Socket.i, hEvent.i, lNetworkEvents.l) Prototype WSACloseEvent(hEvent.i) Prototype WSAWaitForMultipleEvents(EventNum.i, *lphEvents, fWaitAll.b, dwTimeout.l, fAlertable) Prototype WSAEnumNetworkEvents(Socket.i, hEventObject, *lpNetworkEvents) XIncludeFile "Constant.pbi" XIncludeFile "TorrentFiles.pbi" XIncludeFile "Structures.pbi" XIncludeFile "FlagIcon.pbi" XIncludeFile "ClientName.pbi" XIncludeFile "Controls_CB.pbi" XIncludeFile "misc.pbi" XIncludeFile "UDP_Tracker.pbi" XIncludeFile "Tracker.pbi" XIncludeFile "Out_GUI.pbi" XIncludeFile "Torrent.pbi" XIncludeFile "EndgameMode.pbi" XIncludeFile "Peer.pbi" XIncludeFile "Core.pbi" XIncludeFile "Window.pbi" XIncludeFile "Peer_Download.pbi" XIncludeFile "Peer_Upload.pbi" G_FatalErrorStatus=0 CompilerIf #PB_Editor_CreateExecutable Procedure FatalError() Shared G_FatalErrorStatus Protected Result$, Line G_FatalErrorStatus + 1 Result$="Во время работы программы произошла ошибка" Line = ErrorLine() If Line>=0 Result$+" в строке "+Str(Line)+", файла: "+Chr(10)+ErrorFile() EndIf Result$+Chr(10)+Chr(10)+"Тип ошибки: "+Chr(34)+ErrorMessage()+Chr(34)+"." Result$+Chr(10)+Chr(10)+"Просьба сообщить разработчику о ней для устранения данной ошибки." MessageRequester(#MessageName+" Build "+Str(#PB_Editor_BuildCount)+" — ошибка программы!", Result$, #MB_OK|#MB_ICONERROR) End_Program() End EndProcedure Procedure.s Handle_ToFileName(hWnd, *PID.Integer) Protected PID, hProcess, Lib, Name.s, Len Name = "" PID=0 GetWindowThreadProcessId_(hWnd, @PID) If PID *PID\i = PID hProcess = OpenProcess_(#PROCESS_ALL_ACCESS, 0, PID) If hProcess *mem = AllocateMemory(#MAX_PATH*2+10) If *mem Lib = OpenLibrary(#PB_Any,"PSAPI.DLL") If Lib CompilerIf #PB_Compiler_Unicode *F=GetFunction(Lib,"GetModuleFileNameExW") CompilerElse *F=GetFunction(Lib,"GetModuleFileNameExA") CompilerEndIf If *F FillMemory(*mem, #MAX_PATH*2+8, 0) Len = CallFunctionFast(*F,hProcess,0,*mem,#MAX_PATH) If Len>0 Name = PeekS(*mem, Len) EndIf EndIf CloseLibrary(Lib) EndIf FreeMemory(*mem) EndIf EndIf EndIf ProcedureReturn Name EndProcedure Procedure GetMyWin_h(*PID) Protected hWnd, *WinMem, Len, String.s, Result hWnd = FindWindow_(0, 0) If hWnd *WinMem = AllocateMemory(#MAX_PATH*2+10) If *WinMem Repeat FillMemory(*WinMem, #MAX_PATH*2+8, 0) Len = GetWindowText_(hWnd, *WinMem, #MAX_PATH) If Len>0 String=PeekS(*WinMem, Len) If String<>"" If FindString(String, #ProgName, 1) = 1 FillMemory(*WinMem, #MAX_PATH*2+8, 0) Len = GetClassName_(hWnd, *WinMem, #MAX_PATH) If Len>0 String=PeekS(*WinMem, Len) If String<>"" If FindString(String, "WindowClass_", 1)=1 If Handle_ToFileName(hWnd, *PID) = G_ProgramMiscInfo\ProgFullFileName Len = Random(900)+20 If SendMessage_(hWnd, #MY_MESSAGE, $22F24028, Len) = $22F24028 + Len-10 Result = hWnd Break EndIf EndIf EndIf EndIf EndIf EndIf EndIf EndIf hWnd = GetWindow_(hWnd, #GW_HWNDNEXT) Until hWnd=0 FreeMemory(*WinMem) EndIf EndIf ProcedureReturn hWnd EndProcedure Procedure RunTest() Protected Result.b, a, Count, i Protected String.s, Temp.s, hWnd, PID Protected proc, *ProcMem, Sha1.s Result = 0 Temp=ProgramFilename() Sha1=LCase(SHA1Fingerprint(@Temp, StringByteLength(Temp, #PB_Unicode))) Temp="" a = CreateSemaphore_(#Null,0,1,Sha1) If a<>0 And GetLastError_()=#ERROR_ALREADY_EXISTS CloseHandle_(a) Count=CountProgramParameters() If Count>0 Count-1 String="" For i=0 To Count Temp=ProgramParameter(i) If Temp<>"" String + Temp + Chr(10) EndIf Next EndIf String = #ProgName + Chr(9) + String PID = 0 hWnd = GetMyWin_h(@PID) If hWnd And PID proc = OpenProcess_(#PROCESS_VM_OPERATION|#PROCESS_VM_WRITE, #False, PID) If proc *ProcMem = VirtualAllocEx_(proc, 0, StringByteLength(String, #PB_Unicode)+10, #MEM_COMMIT, #PAGE_READWRITE) If *ProcMem WriteProcessMemory_(proc, *ProcMem, @String, StringByteLength(String, #PB_Unicode), #Null) SendMessage_(hWnd, #MY_MESSAGE, $A8F24028, *ProcMem) Delay(400) VirtualFreeEx_(proc, *ProcMem, 0, #MEM_RELEASE) EndIf CloseHandle_(proc) EndIf EndIf Result = 1 EndIf ProcedureReturn Result EndProcedure If RunTest() = #True End EndIf CompilerEndIf Procedure LoadTorrent(Count) ; Загрудка торрент файлов из командной строки. Protected String.s, *File.String, i, x G_ProgramMiscInfo\Setting\MainWinVisible = #True HideWindow(#MainWin, 0) Count-1 String.s="" For i=0 To Count *File = AllocateMemory(SizeOf(String)) If *File InitializeStructure(*File, String) *File\s=ProgramParameter(i) If *File\s<>"" If FileSize(*File\s)>0 x=CreateThread(@Open_LoadTorrent_Win(), *File) If x ThreadPriority(x, 8) Else ClearStructure(*File, String) FreeMemory(*File) EndIf EndIf EndIf EndIf Next EndProcedure If InitNetwork() = 0 MessageRequester(#MessageName, "Ошибка при инициализации сетевой библиотеки!", #MB_OK|#MB_ICONERROR) End EndIf SetCurrentDirectory(G_ProgramMiscInfo\ProgFilePath) If CreatePopupMenu(#TrayMenu) MenuItem(#TrayMenu_Show_HideWindow,"Показать/скрыть окно") MenuBar() MenuItem(#TrayMenu_PauseAllTorrent,"Пауза для всех торрентов") MenuBar() MenuItem(#MainMenu_Exit,"Выход") EndIf If CreatePopupMenu(#PeerListMemu) MenuItem(#PeerListMemu_AddPeer, "Добавить пира") MenuBar() MenuItem(#PeerListMemu_ShowDomainsName, "Доменное имя") EndIf Init_Program() SetMenuItemState(#PeerListMemu, #PeerListMemu_ShowDomainsName, G_ProgramMiscInfo\Setting\ShowDomainsName) MainWindow_Create() Torrent_ProgStartAddTorrent() Count=CountProgramParameters() If Count>0 LoadTorrent(Count) EndIf Repeat Event = WaitWindowEvent() E_Window = EventWindow() If E_Window = #MainWin If MainWindow_Event(Event) = #True Break EndIf ElseIf E_Window = #Setting_Win SettingWindow_Event(Event) ElseIf E_Window = #Window_LabelTorrent Window_LabelTorrent_Event(Event) ElseIf E_Window = #Window_AddPeer Window_AddPeer_Event(Event) EndIf ForEver HideWindow(#MainWin, 1) End_Program() If Test_TrayIcon(#SysTrayID, WindowID(#MainWin))<>0 ; Обнаружена иконка в трее RemoveSysTrayIcon(#SysTrayID) EndIf CloseNetworkServer(#TorrentServer) Delay(400) If IsThread(G_ProgramMiscInfo\Thread\OutText_ThreadID) KillThread(G_ProgramMiscInfo\Thread\OutText_ThreadID) EndIf SignalSemaphore(G_ProgramMiscInfo\Network\TrackerTask_Semaphore) SignalSemaphore(G_ProgramMiscInfo\Network\TrackerTask_Semaphore) WaitThread(G_ProgramMiscInfo\Thread\UpdateTracker_ThreadID, 60000) ; Ожидаем завершения работы потока заданий, который при выполнении этой строки, уже должен был обрабатывать задания информирования трекеров об завершении работы торрент-клиента. End DataSection IconPac: CompilerIf #PB_Compiler_Version>=510 IncludeBinary "Files\IconPac_510.pac" IconPac_End: CompilerElse CompilerIf #PB_Compiler_Processor=#PB_Processor_x86 IncludeBinary "Files\IconPac.pac" CompilerElse IncludeBinary "Files\IconPac_x64.pac" CompilerEndIf CompilerEndIf FlagPac: CompilerIf #PB_Compiler_Version>=510 IncludeBinary "Files\FlagsPac_510.pac" FlagPac_End: CompilerElse CompilerIf #PB_Compiler_Processor=#PB_Processor_x86 IncludeBinary "Files\FlagsPac.pac" CompilerElse IncludeBinary "Files\FlagsPac_x64.pac" CompilerEndIf CompilerEndIf EndDataSection ; IDE Options = PureBasic 5.20 beta 15 LTS (Windows - x86) ; Folding = - ; EnableXP