mirror of
https://github.com/Sunnyyoung/WeChatTweak-macOS.git
synced 2025-12-17 09:51:56 +08:00
refactor: use async/await instead of promises
support both local and network config
This commit is contained in:
parent
540f13b35b
commit
4b1bd7c2ab
|
|
@ -1,25 +1,15 @@
|
||||||
{
|
{
|
||||||
"object": {
|
"originHash" : "fc7f739a75c4c771c736e9ff1f765bbd53fb1f3c5beaa621fbc867f0d71964c4",
|
||||||
"pins": [
|
"pins" : [
|
||||||
{
|
{
|
||||||
"package": "PromiseKit",
|
"identity" : "swift-argument-parser",
|
||||||
"repositoryURL": "https://github.com/mxcl/PromiseKit",
|
"kind" : "remoteSourceControl",
|
||||||
"state": {
|
"location" : "https://github.com/apple/swift-argument-parser",
|
||||||
"branch": null,
|
"state" : {
|
||||||
"revision": "2bc44395edb4f8391902a9ff7c220471882a4d07",
|
"revision" : "cdd0ef3755280949551dc26dee5de9ddeda89f54",
|
||||||
"version": "8.2.0"
|
"version" : "1.6.2"
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"package": "swift-argument-parser",
|
|
||||||
"repositoryURL": "https://github.com/apple/swift-argument-parser",
|
|
||||||
"state": {
|
|
||||||
"branch": null,
|
|
||||||
"revision": "cdd0ef3755280949551dc26dee5de9ddeda89f54",
|
|
||||||
"version": "1.6.2"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
}
|
||||||
},
|
],
|
||||||
"version": 1
|
"version" : 3
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,12 @@ let package = Package(
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/mxcl/PromiseKit", from: "8.0.0"),
|
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.6.0")
|
||||||
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.0")
|
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.executableTarget(
|
.executableTarget(
|
||||||
name: "WeChatTweak",
|
name: "WeChatTweak",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
"PromiseKit",
|
|
||||||
.product(name: "ArgumentParser", package: "swift-argument-parser")
|
.product(name: "ArgumentParser", package: "swift-argument-parser")
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
11
README.md
11
README.md
|
|
@ -5,8 +5,15 @@
|
||||||
|
|
||||||
## 功能
|
## 功能
|
||||||
|
|
||||||
- [x] 阻止消息撤回
|
- 阻止消息撤回
|
||||||
- [ ] 客户端无限多开
|
- 阻止自动更新
|
||||||
|
- 客户端多开
|
||||||
|
|
||||||
|
## 安装
|
||||||
|
|
||||||
|
```bash
|
||||||
|
brew install sunnyyoung/tap/wechattweak
|
||||||
|
```
|
||||||
|
|
||||||
## 使用
|
## 使用
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import PromiseKit
|
|
||||||
import ArgumentParser
|
import ArgumentParser
|
||||||
|
|
||||||
struct Command {
|
struct Command {
|
||||||
|
|
@ -15,46 +14,44 @@ struct Command {
|
||||||
var errorDescription: String? {
|
var errorDescription: String? {
|
||||||
switch self {
|
switch self {
|
||||||
case let .executing(command, error):
|
case let .executing(command, error):
|
||||||
return "Execute command: \(command) failed: \(error)"
|
return "executing: \(command) error: \(error)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func patch(app: URL, config: Config) -> Promise<Void> {
|
static func version(app: URL) async throws -> String? {
|
||||||
print("------ Path ------")
|
try await Command.execute(command: "defaults read \(app.appendingPathComponent("Contents/Info.plist").path) CFBundleVersion")
|
||||||
return Promise { seal in
|
|
||||||
do {
|
|
||||||
seal.fulfill(try Patcher.patch(binary: app.appendingPathComponent("Contents/MacOS/WeChat"), config: config))
|
|
||||||
} catch {
|
|
||||||
seal.reject(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static func resign(app: URL) -> Promise<Void> {
|
static func patch(app: URL, config: Config) async throws {
|
||||||
print("------ Resign ------")
|
try Patcher.patch(binary: app.appendingPathComponent("Contents/MacOS/WeChat"), config: config)
|
||||||
return firstly {
|
|
||||||
Command.execute(command: "codesign --remove-sign \(app.path)")
|
|
||||||
}.then {
|
|
||||||
Command.execute(command: "codesign --force --deep --sign - \(app.path)")
|
|
||||||
}.then {
|
|
||||||
Command.execute(command: "xattr -cr \(app.path)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func execute(command: String) -> Promise<Void> {
|
static func resign(app: URL) async throws {
|
||||||
return Promise { seal in
|
try await Command.execute(command: "codesign --remove-sign \(app.path)")
|
||||||
print("Execute command: \(command)")
|
try await Command.execute(command: "codesign --force --deep --sign - \(app.path)")
|
||||||
var error: NSDictionary?
|
try await Command.execute(command: "xattr -cr \(app.path)")
|
||||||
guard let script = NSAppleScript(source: "do shell script \"\(command)\"") else {
|
}
|
||||||
return seal.reject(Error.executing(command: command, error: ["error": "Create script failed."]))
|
|
||||||
}
|
@discardableResult
|
||||||
script.executeAndReturnError(&error)
|
private static func execute(command: String) async throws -> String? {
|
||||||
if let error = error {
|
guard let script = NSAppleScript(source: "do shell script \"\(command)\"") else {
|
||||||
seal.reject(Error.executing(command: command, error: error))
|
throw Error.executing(
|
||||||
} else {
|
command: command,
|
||||||
seal.fulfill(())
|
error: ["error": "Create script failed."]
|
||||||
}
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
var error: NSDictionary?
|
||||||
|
let descriptor = script.executeAndReturnError(&error)
|
||||||
|
|
||||||
|
if let error = error {
|
||||||
|
throw Error.executing(
|
||||||
|
command: command,
|
||||||
|
error: error
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return descriptor.stringValue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -80,6 +80,20 @@ struct Config: Decodable {
|
||||||
|
|
||||||
let version: String
|
let version: String
|
||||||
let targets: [Target]
|
let targets: [Target]
|
||||||
|
|
||||||
|
static func load(from url: URL) async throws -> [Config] {
|
||||||
|
if url.isFileURL {
|
||||||
|
return try JSONDecoder().decode(
|
||||||
|
[Config].self,
|
||||||
|
from: Data(contentsOf: url)
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return try JSONDecoder().decode(
|
||||||
|
[Config].self,
|
||||||
|
from: try await URLSession.shared.data(from: url).0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension Data {
|
private extension Data {
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,14 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
import PromiseKit
|
import Dispatch
|
||||||
import ArgumentParser
|
import ArgumentParser
|
||||||
|
|
||||||
struct Patch: ParsableCommand {
|
struct Patch: AsyncParsableCommand {
|
||||||
enum Error: LocalizedError {
|
enum Error: LocalizedError {
|
||||||
case invalidApp
|
case invalidApp
|
||||||
case invalidConfig
|
case invalidConfig
|
||||||
|
case invalidVersion
|
||||||
case unsupportedVersion
|
case unsupportedVersion
|
||||||
|
|
||||||
var errorDescription: String? {
|
var errorDescription: String? {
|
||||||
|
|
@ -20,6 +21,8 @@ struct Patch: ParsableCommand {
|
||||||
return "Invalid app path"
|
return "Invalid app path"
|
||||||
case .invalidConfig:
|
case .invalidConfig:
|
||||||
return "Invalid patch config"
|
return "Invalid patch config"
|
||||||
|
case .invalidVersion:
|
||||||
|
return "Invalid app version"
|
||||||
case .unsupportedVersion:
|
case .unsupportedVersion:
|
||||||
return "Unsupported WeChat version"
|
return "Unsupported WeChat version"
|
||||||
}
|
}
|
||||||
|
|
@ -30,7 +33,7 @@ struct Patch: ParsableCommand {
|
||||||
|
|
||||||
@Option(
|
@Option(
|
||||||
name: .shortAndLong,
|
name: .shortAndLong,
|
||||||
help: "Default: /Applications/WeChat.app",
|
help: "Path of WeChat.app",
|
||||||
transform: {
|
transform: {
|
||||||
guard FileManager.default.fileExists(atPath: $0) else {
|
guard FileManager.default.fileExists(atPath: $0) else {
|
||||||
throw Error.invalidApp
|
throw Error.invalidApp
|
||||||
|
|
@ -42,55 +45,58 @@ struct Patch: ParsableCommand {
|
||||||
|
|
||||||
@Option(
|
@Option(
|
||||||
name: .shortAndLong,
|
name: .shortAndLong,
|
||||||
help: "Default: ./config.json",
|
help: "Local path or Remote URL of config.json",
|
||||||
transform: {
|
transform: {
|
||||||
guard FileManager.default.fileExists(atPath: $0) else {
|
if FileManager.default.fileExists(atPath: $0) {
|
||||||
throw Error.invalidConfig
|
return URL(fileURLWithPath: $0)
|
||||||
|
} else {
|
||||||
|
guard let url = URL(string: $0) else {
|
||||||
|
throw Error.invalidConfig
|
||||||
|
}
|
||||||
|
return url
|
||||||
}
|
}
|
||||||
return URL(fileURLWithPath: $0)
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
var config: URL = {
|
var config: URL = URL(string: "https://raw.githubusercontent.com/sunnyyoung/WeChatTweak/refs/heads/feature/2.0/config.json")!
|
||||||
var size: UInt32 = 0
|
|
||||||
_NSGetExecutablePath(nil, &size)
|
|
||||||
var buffer = [CChar](repeating: 0, count: Int(size))
|
|
||||||
guard _NSGetExecutablePath(&buffer, &size) == 0, let path = String(utf8String: buffer) else {
|
|
||||||
return URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
|
|
||||||
}
|
|
||||||
return URL(fileURLWithPath: path).resolvingSymlinksInPath().deletingLastPathComponent().appendingPathComponent("config.json")
|
|
||||||
}()
|
|
||||||
|
|
||||||
func run() throws {
|
mutating func run() async throws {
|
||||||
let configs = try JSONDecoder().decode([Config].self, from: Data(contentsOf: self.config))
|
do {
|
||||||
|
print("------ Version ------")
|
||||||
|
guard let version = try await Command.version(app: self.app) else {
|
||||||
|
throw Error.invalidVersion
|
||||||
|
}
|
||||||
|
print("\(version)")
|
||||||
|
|
||||||
guard
|
print("------ Config ------")
|
||||||
let info = NSDictionary(contentsOf: self.app.appendingPathComponent("Contents/Info.plist")),
|
guard let config = (try await Config.load(from: self.config)).first(where: { $0.version == version }) else {
|
||||||
let version = info["CFBundleVersion"] as? String,
|
throw Error.unsupportedVersion
|
||||||
let config = configs.first(where: { $0.version == version })
|
}
|
||||||
else {
|
print("\(config)")
|
||||||
throw Error.unsupportedVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
firstly {
|
print("------ Patch ------")
|
||||||
Command.patch(
|
try await Command.patch(
|
||||||
app: self.app,
|
app: self.app,
|
||||||
config: config
|
config: config
|
||||||
)
|
)
|
||||||
}.then {
|
print("Done!")
|
||||||
Command.resign(app: self.app)
|
|
||||||
}.ensure {
|
print("------ Resign ------")
|
||||||
print("")
|
try await Command.resign(
|
||||||
}.done {
|
app: self.app
|
||||||
print("🎉 Done!")
|
)
|
||||||
|
print("Done!")
|
||||||
|
|
||||||
|
print("------🎉 Done!------")
|
||||||
Darwin.exit(EXIT_SUCCESS)
|
Darwin.exit(EXIT_SUCCESS)
|
||||||
}.catch { error in
|
} catch {
|
||||||
print("🚨 \(error.localizedDescription)", stderr)
|
print("------🚨 Error------")
|
||||||
|
print("\(error.localizedDescription)")
|
||||||
Darwin.exit(EXIT_FAILURE)
|
Darwin.exit(EXIT_FAILURE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Tweak: ParsableCommand {
|
struct Tweak: AsyncParsableCommand {
|
||||||
static let configuration = CommandConfiguration(
|
static let configuration = CommandConfiguration(
|
||||||
commandName: "wechattweak",
|
commandName: "wechattweak",
|
||||||
abstract: "A command-line tool for tweaking WeChat.",
|
abstract: "A command-line tool for tweaking WeChat.",
|
||||||
|
|
@ -101,5 +107,8 @@ struct Tweak: ParsableCommand {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
Tweak.main()
|
Task {
|
||||||
CFRunLoopRun()
|
await Tweak.main()
|
||||||
|
}
|
||||||
|
|
||||||
|
Dispatch.dispatchMain()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user