Skip to content

Commit

Permalink
fix: #22
Browse files Browse the repository at this point in the history
  • Loading branch information
livelycode36 committed Oct 10, 2024
1 parent fd13d3e commit 2dd422d
Show file tree
Hide file tree
Showing 17 changed files with 971 additions and 599 deletions.
150 changes: 150 additions & 0 deletions lib/CharsetDetect.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
class TextEncodingDetect {
UTF8Bom := [0xEF, 0xBB, 0xBF]
UTF16LeBom := [0xFF, 0xFE]
UTF16BeBom := [0xFE, 0xFF]
UTF32LeBom := [0xFF, 0xFE, 0x00, 0x00]

DetectEncoding(fileName) {
if !FileExist(fileName) {
MsgBox("File not found")
return ""
}

buffer := FileRead(fileName, "RAW")
size := buffer.Size

encodingType := this.DetectWithBom(buffer)
if (encodingType == "Utf8Bom") {
encodingType := "UTF-8"
} else if (encodingType == "UnicodeBom") {
encodingType := "UTF-16"
}

if encodingType != "None" {
return encodingType
}

encodingType := this.DetectWithoutBom(buffer, size)
if (encodingType == "GBK" ||
encodingType == "Ansi") {
; CP0 是 系统默认编码
encodingType := "CP0"
}

return encodingType != "None" ? encodingType : "None"
}

DetectWithBom(buffer) {
if (buffer.Size >= 3 && this.ByteAt(buffer, 0) = this.UTF8Bom[1] && this.ByteAt(buffer, 1) = this.UTF8Bom[2] && this.ByteAt(buffer, 2) = this.UTF8Bom[3]) {
return "Utf8Bom"
}

if (buffer.Size >= 2 && this.ByteAt(buffer, 0) = this.UTF16LeBom[1] && this.ByteAt(buffer, 1) = this.UTF16LeBom[2]) {
return "UnicodeBom"
}

if (buffer.Size >= 2 && this.ByteAt(buffer, 0) = this.UTF16BeBom[1] && this.ByteAt(buffer, 1) = this.UTF16BeBom[2]) {
if (buffer.Size >= 4 && this.ByteAt(buffer, 2) = this.UTF32LeBom[3] && this.ByteAt(buffer, 3) = this.UTF32LeBom[4]) {
return "Utf32Bom"
}
return "BigEndianUnicodeBom"
}

return "None"
}

DetectWithoutBom(buffer, size) {
; Check for UTF-8 encoding
encoding := this.CheckUtf8(buffer, size)
if encoding != "None" {
return encoding
}

; Check for ANSI encoding
if !this.ContainsZero(buffer, size) {
return this.CheckChinese(buffer, size) ? "GBK" : "Ansi"
}

return "None"
}

CheckUtf8(buffer, size) {
pos := 0
while pos < size {
ch := this.ByteAt(buffer, pos)
pos++

if ch < 0x80 {
continue
}

if ch >= 0xC2 && ch <= 0xDF {
if !this.IsContinuationByte(this.ByteAt(buffer, pos)) {
return "None"
}
pos++
continue
}

if ch >= 0xE0 && ch <= 0xEF {
if !this.IsContinuationByte(this.ByteAt(buffer, pos)) || !this.IsContinuationByte(this.ByteAt(buffer, pos + 1)) {
return "None"
}
pos += 2
continue
}

if ch >= 0xF0 && ch <= 0xF4 {
if !this.IsContinuationByte(this.ByteAt(buffer, pos)) || !this.IsContinuationByte(this.ByteAt(buffer, pos + 1)) || !this.IsContinuationByte(this.ByteAt(buffer, pos + 2)) {
return "None"
}
pos += 3
continue
}

return "None"
}
; return "Utf8Nobom"
return "UTF-8"
}

IsContinuationByte(byte) {
return byte >= 0x80 && byte <= 0xBF
}

ContainsZero(buffer, size) {
pos := 0
while pos < size {
if this.ByteAt(buffer, pos) = 0 {
return true
}
pos++
}
return false
}

CheckChinese(buffer, size) {
pos := 0
while pos < size - 1 {
ch1 := this.ByteAt(buffer, pos)
ch2 := this.ByteAt(buffer, pos + 1)
pos += 2

if (ch1 >= 176 && ch1 <= 247 && ch2 >= 160 && ch2 <= 254) ; GB2312
|| (ch1 >= 129 && ch1 <= 254 && ch2 >= 64 && ch2 <= 254) ; GBK
|| (ch1 >= 129 && ((ch2 >= 64 && ch2 <= 126) || (ch2 >= 161 && ch2 <= 254))) { ; Big5
return true
}
}
return false
}

ByteAt(buffer, index) {
return NumGet(buffer, index, "UChar")
}
}

; 使用示例:
; detect := TextEncodingDetect()
; encoding := detect.DetectEncoding("C:\Users\Thunder\Downloads\Compressed\02 音名与钢琴键盘.srt")
; MsgBox "Detected Encoding: " encoding
76 changes: 42 additions & 34 deletions lib/MyTool.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
log_toggle := false

; 在路径中,获取程序的名称
GetNameForPath(program_path){
SplitPath program_path, &name
GetNameForPath(program_path) {
SplitPath program_path, &name, &dir, &ext, &name_no_ext, &drive
return name
}

SearchProgram(target_app_path) {
; 程序正在运行
if (WinExist("ahk_exe " GetNameForPath(target_app_path))) {
return true
return true
} else {
return false
return false
}
}

SelectedNoteProgram(note_app_names){
Loop Parse note_app_names, "`n"{
SelectedNoteProgram(note_app_names) {
Loop Parse note_app_names, "`n" {
note_program := A_LoopField
if (WinExist("ahk_exe " note_program)) {
return note_program
Expand All @@ -28,49 +28,57 @@ SelectedNoteProgram(note_app_names){
Exit
}

ActivateProgram(process_name){
if WinActive("ahk_exe " process_name){
return
ActivateProgram(process_name) {
if WinActive("ahk_exe " process_name) {
return
}

if (WinExist("ahk_exe " process_name)) {
WinActivate ("ahk_exe " process_name)
Sleep 300 ; 给程序切换窗口的时间
WinActivate ("ahk_exe " process_name)
Sleep 300 ; 给程序切换窗口的时间
} else {
MsgBox process_name " is not running"
Exit
MsgBox process_name " is not running"
Exit
}
}

IsPotplayerRunning(media_player_path){
IsPotplayerRunning(media_player_path) {
if SearchProgram(media_player_path)
return true
return true
else
return false
return false
}

; 获取Potplayer的标题
; 获取失败的原因:因为Potplayer的exe可能存在多个线程,而WinGetTitle其中一个线程的标题,结果可能为空字符串
; 参考:https://stackoverflow.com/questions/54570212/why-is-my-call-to-wingettitle-returning-an-empty-string
GetPotplayerTitle(potplayer_process_name){
GetPotplayerTitle(potplayer_process_name) {
ids := WinGetList("ahk_exe " potplayer_process_name)
for id in ids{

title := ""
for id in ids {
try {
title := WinGetTitle("ahk_id " id)
if title == ""
continue
else
return title
} catch TargetError as err {
; 目标窗口未找到
continue
}
if title == ""
continue
else
return title
}
Assert(title == "", "Error: Get Potplayer title failure!")
}

MyLog(message){
MyLog(message) {
if log_toggle
MsgBox message
MsgBox message
}

; 安全的递归
global running_count := 0
SafeRecursion(){
SafeRecursion() {
global running_count
running_count++
ToolTip("正在重试,第" running_count "次尝试...")
Expand All @@ -83,17 +91,17 @@ SafeRecursion(){
}

; 等待释放指定按键
ReleaseKeyboard(keyName){
if GetKeyState(keyName){
if KeyWait(keyName,"T2") == 0{
SafeRecursion()
ReleaseKeyboard(keyName)
}
ReleaseKeyboard(keyName) {
if GetKeyState(keyName) {
if KeyWait(keyName, "T2") == 0 {
SafeRecursion()
ReleaseKeyboard(keyName)
}
}
running_count := 0
}

ReleaseCommonUseKeyboard(){
ReleaseCommonUseKeyboard() {
ReleaseKeyboard("Control")
ReleaseKeyboard("Shift")
ReleaseKeyboard("Alt")
Expand Down Expand Up @@ -137,8 +145,8 @@ UrlDecode(Url, Enc := "UTF-8") {
Return Url
}

Assert(condition, exception_message){
if (condition){
Assert(condition, exception_message) {
if (condition) {
MsgBox exception_message
Exit
}
Expand Down
Loading

0 comments on commit 2dd422d

Please sign in to comment.