code/change/change.py
2024-09-27 21:57:32 +08:00

83 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import ctypes
import sys
import os
from time import sleep
import shelve
def get_path(relative_path):
try:
path = sys._MEIPASS # pyinstaller 打包后的路径
except AttributeError:
path = os.path.abspath(".") # 当前工作目录的路径
return os.path.normpath(os.path.join(path, relative_path)) # 返回实际路径
with shelve.open('main.db') as db:
# 如果数据库中没有 'bin_name',返回 '1'(默认值)
bin_name = db.get('bin_name', '1')
if bin_name is None:
os.startfile(get_path('load.exe'))
def is_admin():
"""检查当前是否有管理员权限"""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
# 如果已经是管理员,执行主要的逻辑
print("已获得管理员权限")
print(f"当前模式(1为CS原版, 2为CS:SO): {bin_name}")
print("请选择模式")
print("1. 改为CS:SO\n2. 改回原版\n3. 退出")
choice = input('>>> ')
path = get_path('') # 获取当前工作目录的实际路径
if choice == '1':
if bin_name == '2':
print('错误你已是CS:SO!')
sleep(1.5)
sys.exit()
# 切换到 CS:SO
os.system(f"ren {path}/bin {path}/bin1")
os.system(f"ren {path}/bin2 {path}/bin")
with shelve.open('main.db') as db:
db['bin_name'] = '2' # 切换至 CS:SO
print("完成!")
sleep(1.85)
sys.exit()
elif choice == '2':
if bin_name == '1':
print("错误,你已是原版!")
sleep(1.5)
sys.exit()
# 切换到原版 CS
os.system(f"ren {path}/bin {path}/bin2")
os.system(f"ren {path}/bin1 {path}/bin")
with shelve.open('main.db') as db:
db['bin_name'] = '1' # 切换至 CS 原版
print("完成!")
sleep(1.55)
sys.exit()
elif choice == '3':
sys.exit()
else:
print("输入错误!")
else:
# 如果没有管理员权限,重新以管理员权限运行脚本
print("请求管理员权限...")
# 使用 `ctypes` 模块提升权限,重新运行脚本
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)