#NoTrayIcon
#SingleInstance Off
#Include *i %A_ScriptDir%\Extensions\AllIncludes.ahk
If FileExist(A_ScriptDir "\Extensions\Allincludes.ahk"){
	FileRead, Allincludes, %A_ScriptDir%\Extensions\Allincludes.ahk
	tvar:=""
} Else
	tvar:=Allincludes:=""
Loop, Files, %A_ScriptDir%\Extensions\*.ahk
{
	If (A_LoopFileFullPath=A_ScriptDir "\Extensions\Allincludes.ahk")
		Continue
	tvar .= "#Include %A_ScriptDir%\Extensions\" A_LoopFileName "`n"
}
If (tvar!=StrReplace(Allincludes, "`r")){
	FileDelete, %A_ScriptDir%\Extensions\Allincludes.ahk
	FileAppend, %tvar%, %A_ScriptDir%\Extensions\Allincludes.ahk, UTF-8
	Run, % "autohotkey.exe Extensions.ahk " A_Args[1]
	ExitApp
}
Allincludes:=tvar:=""
If !(A_Args[1]~="^\d+$")
	ExitApp
OnMessage(0x4a, "Receive_WM_COPYDATA")  ; 0x4a 为 WM_COPYDATA
Send_WM_COPYDATA("<<ExtensionsPID>>" DllCall("GetCurrentProcessId"),"ahk_pid" A_Args[1])
SetTimer, scriptexit, 5000
Return

scriptexit:
	Process, Exist, % A_Args[1]
	If !ErrorLevel
		ExitApp
Return

Receive_WM_COPYDATA(wParam, lParam){
	StringAddress := NumGet(lParam + 2*A_PtrSize)  ; 获取 CopyDataStruct 的 lpData 成员.
	CopyOfData := StrGet(StringAddress)  ; 从结构中复制字符串.
	arr:=StrSplit(CopyOfData, " "), funcname:=arr[1], Result:=""
	If IsFunc(funcname){
		Switch arr.Length()
		{
			Case 1:
				Result:=%funcname%()
			Case 2:
				Result:=%funcname%(arr[2])
			Case 3:
				Result:=%funcname%(arr[2],arr[3])
			Case 4:
				Result:=%funcname%(arr[2],arr[3],arr[4])
			Case 5:
				Result:=%funcname%(arr[2],arr[3],arr[4],arr[5])
			Case 6:
				Result:=%funcname%(arr[2],arr[3],arr[4],arr[5],arr[6])
			Default:
				arr.RemoveAt(1)
				Result:=%funcname%(arr)
		}
	} Else {
		ToolTip, 函数不存在
		SetTimer, ToolTipDestroy, -2000
	}
	If (Result="")
		Return 1
	Else {
		Send_WM_COPYDATA(Result,"ahk_pid" A_Args[1])
		Return 0
	}
}

Send_WM_COPYDATA(ByRef StringToSend, ByRef TargetScriptTitle){
	; 在这种情况中使用 ByRef 能节约一些内存.
	; 此函数发送指定的字符串到指定的窗口然后返回收到的回复.
	; 如果目标窗口处理了消息则回复为 1, 而消息被忽略了则为 0.
	VarSetCapacity(CopyDataStruct, 3*A_PtrSize, 0)  ; 分配结构的内存区域.
	; 首先设置结构的 cbData 成员为字符串的大小, 包括它的零终止符:
	SizeInBytes := (StrLen(StringToSend) + 1) * (A_IsUnicode ? 2 : 1)
	NumPut(SizeInBytes, CopyDataStruct, A_PtrSize)  ; 操作系统要求这个需要完成.
	NumPut(&StringToSend, CopyDataStruct, 2*A_PtrSize)  ; 设置 lpData 为到字符串自身的指针.
	Prev_DetectHiddenWindows := A_DetectHiddenWindows
	Prev_TitleMatchMode := A_TitleMatchMode
	DetectHiddenWindows On
	SetTitleMatchMode 2
	TimeOutTime := 4000  ; Optional. Milliseconds to wait for response from receiver.ahk. Default is 5000
	; 必须使用发送 SendMessage 而不是投递 PostMessage.
	SendMessage, 0x4a, 0, &CopyDataStruct,, %TargetScriptTitle%  ; 0x4a 为 WM_COPYDAT
	DetectHiddenWindows %Prev_DetectHiddenWindows%  ; 恢复调用者原来的设置.
	SetTitleMatchMode %Prev_TitleMatchMode%         ; 同样.
	Return ErrorLevel  ; 返回 SendMessage 的回复给我们的调用者.
}

ToolTipDestroy:
	ToolTip
Return