Skip to content

Commit

Permalink
v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Feb 12, 2025
1 parent 7406034 commit 724a118
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ Fount 助手会在以下这些情况下自动出现:
你可以使用 `f` 命令来轻松地禁用或启用自动助手功能:

```powershell
f 0 # 禁用自动助手 (也可以用 f false / f no / f n / f disable)
f 1 # 启用自动助手 (也可以用 f true / f yes / f y / f enable)
f 0 # 禁用自动助手 (也可以用 f false / f no / f n / f disable / f unset / f off 等)
f 1 # 启用自动助手 (也可以用 f true / f yes / f y / f enable / f set / f on 等)
```
32 changes: 13 additions & 19 deletions assists/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,24 @@

$script:FountAssistInstalled = $false

function Set-FountAssist {
param (
$FountUsername,
$AssistCharname
)
function Set-FountAssist(
$FountUsername,
$AssistCharname
) {
if ($AssistCharname) { $Global:FountAssist.AssistCharname = $AssistCharname }
if ($FountUsername) { $Global:FountAssist.FountUsername = $FountUsername }
Start-Fount background keepalive runshell $Global:FountAssist.FountUsername preload chars $Global:FountAssist.AssistCharname
if ($script:FountAssistInstalled) {
return
}
if ($EshellUI) {
. $PSScriptRoot/esh-assist.ps1
}
else {
. $PSScriptRoot/pwsh-assist.ps1
if (-not (Test-FountRunning)) {
Start-Fount background keepalive runshell $Global:FountAssist.FountUsername preload chars $Global:FountAssist.AssistCharname
}
if ($script:FountAssistInstalled) { return }
if ($EshellUI) { . $PSScriptRoot/esh-assist.ps1 }
else { . $PSScriptRoot/pwsh-assist.ps1 }
}

function Install-FountAssist {
param (
$FountUsername,
$AssistCharname
)
function Install-FountAssist(
$FountUsername,
$AssistCharname
) {
if ($AssistCharname) { $Global:FountAssist.AssistCharname = $AssistCharname }
if ($FountUsername) { $Global:FountAssist.FountUsername = $FountUsername }
if ($EshellUI) {
Expand Down
79 changes: 48 additions & 31 deletions base.ps1
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
$script:FountEncoding = [System.Text.Encoding]::UTF8

function Get-FountClient($ComputerName = "localhost", $Port = 16698) {
if ($script:FountClient -and $script:FountClient.Connected) {
return $script:FountClient
$script:FountClient = $null
$script:FountStream = $null
function Set-FountClient($ComputerName = "localhost", $Port = 16698) {
if ($script:FountClient) {
if ($script:FountClient.Connected) {
return $script:FountClient
}
else {
Close-FountClient
}
}

try {
Write-Verbose "正在尝试连接到 Fount 服务器..."
# 创建新的 TCP 客户端
$Client = New-Object System.Net.Sockets.TcpClient
$Client.Connect($ComputerName, $Port)
Write-Verbose "正在尝试连接到 Fount 服务器..."
# 创建新的 TCP 客户端
$Client = New-Object System.Net.Sockets.TcpClient
if ($Client.ConnectAsync($ComputerName, $Port).Wait(100)) {
Write-Verbose "成功连接到 Fount 服务器。"
return $Client
$script:FountClient = $Client
}
catch {
Write-Error "与 Fount 服务器建立连接失败: $($_.Exception.Message)"
return $null
else {
Write-Error "无法连接到 Fount 服务器" -ErrorAction Stop
}
}
function Close-FountClient {
if ($script:FountStream) {
$script:FountStream.Dispose()
$script:FountStream = $null
}
if ($script:FountClient) {
if ($script:FountClient.Connected) {
$script:FountClient.Close()
}
$script:FountClient.Dispose()
$script:FountClient = $null
}
}

$script:FountClient = $null
$script:FountStream = $null
function Invoke-FountIPC(
[string]$Type,
[hashtable]$Data,
[string]$ComputerName = "localhost",
[int]$Port = 16698
[string]$Type,
[hashtable]$Data,
[string]$ComputerName = "localhost",
[int]$Port = 16698
) {
# 构建要发送的完整 JSON 对象
$CommandObject = @{
Expand All @@ -35,15 +51,12 @@ function Invoke-FountIPC(
$JsonCommand = $CommandObject | ConvertTo-Json -Compress -Depth 100
$JsonCommand += "`n" # 添加换行符作为消息结束符

#Write-Host "Sending command:" -ForegroundColor Green
#$CommandObject | ConvertTo-Json -Depth 100 | Write-Host

try {
# 获取或建立连接
if (-not $script:FountClient) {
$script:FountClient = Get-FountClient -ComputerName $ComputerName -Port $Port
Set-FountClient -ComputerName $ComputerName -Port $Port
if (-not $script:FountClient) {
throw "无法连接到 Fount 服务器"
Write-Error "无法连接到 Fount 服务器" -ErrorAction Stop
}
}
if (-not $script:FountClient.Connected) {
Expand Down Expand Up @@ -90,23 +103,17 @@ function Invoke-FountIPC(
# 解析 JSON 响应
if (-not [string]::IsNullOrEmpty($ResponseString)) {
$result = ConvertFrom-Json -InputObject $ResponseString
#Write-Host "Received response:" -ForegroundColor Green
#$result | ConvertTo-Json -Depth 100 | Write-Host
if ($result.status -ne 'ok') {
Write-Error "与 Fount 服务器通信失败: $($result.message)"
Write-Error "与 Fount 服务器通信失败: $($result.message)" -ErrorAction Stop
}
else {
return $result.data
}
}
else {
Write-Error "收到空响应"
Write-Error "收到空响应" -ErrorAction Stop
}
}
catch {
Write-Error "与 Fount 服务器通信失败: $($_.Exception.Message)"
Close-FountClient # 连接出错时关闭连接,以便下次重新连接
}
finally {
if ($MemoryStream) {
$MemoryStream.Dispose()
Expand Down Expand Up @@ -151,6 +158,16 @@ function Invoke-FountShell {
}
}

function Test-FountRunning {
try {
Invoke-FountIPC -Type "ping" -ErrorAction Stop | Out-Null
$true
}
catch {
$false
}
}

function Install-Fount {
$scriptContent = Invoke-RestMethod https://raw.githubusercontent.com/steve02081504/fount/refs/heads/master/src/runner/main.ps1
Invoke-Expression "function fountInstaller { $scriptContent }"
Expand Down
6 changes: 4 additions & 2 deletions fount-pwsh.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'fount-pwsh.psm1'

# Version number of this module.
ModuleVersion = '0.0.1'
ModuleVersion = '0.0.2'

# ID used to uniquely identify this module
GUID = '2a16dae2-91d8-4743-952c-94acd1ed7e5a'
Expand Down Expand Up @@ -73,7 +73,9 @@ Allows you:
FunctionsToExport = @(
"Set-FountAssist",
"Install-FountAssist",
"Get-FountClient",
"Set-FountClient",
"Close-FountClient",
"Test-FountRunning",
"Invoke-FountIPC",
"Start-FountShell",
"Invoke-FountShell",
Expand Down
4 changes: 3 additions & 1 deletion fount-pwsh.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
Export-ModuleMember -Function @(
"Set-FountAssist",
"Install-FountAssist",
"Get-FountClient",
"Set-FountClient",
"Close-FountClient",
"Test-FountRunning",
"Invoke-FountIPC",
"Start-FountShell",
"Invoke-FountShell",
Expand Down

0 comments on commit 724a118

Please sign in to comment.