code/change/change.py

99 lines
2.8 KiB
Python
Raw Normal View History

2024-09-26 18:01:24 +08:00
import ctypes
import sys
import os
from time import sleep
import shelve
2024-09-27 22:26:31 +08:00
# 检查并创建cmd文件的函数
def check_and_create_cmd_files(path):
csso_cmd_path = os.path.join(path, 'csso.cmd')
css_cmd_path = os.path.join(path, 'css.cmd')
2024-09-28 11:32:19 +08:00
hl2_exe_path = os.path.join(path, "hl2.exe")
if not os.path.isfile(hl2_exe_path):
print('请把文件放置在游戏根目录下!')
sleep(1)
sys.exit()
2024-09-27 22:26:31 +08:00
if not os.path.isfile(csso_cmd_path):
with open(csso_cmd_path, 'w') as csso_file:
csso_file.write('@echo off\n')
csso_file.write('ren bin bin1\n')
csso_file.write('ren bin2 bin\n')
2024-09-26 18:01:24 +08:00
2024-09-27 22:26:31 +08:00
if not os.path.isfile(css_cmd_path):
with open(css_cmd_path, 'w') as css_file:
css_file.write('@echo off\n')
css_file.write('ren bin bin2\n')
css_file.write('ren bin1 bin\n')
2024-09-26 18:01:24 +08:00
with shelve.open('main.db') as db:
# 如果数据库中没有 'bin_name',返回 '1'(默认值)
bin_name = db.get('bin_name', '1')
if bin_name is None:
2024-09-27 22:26:31 +08:00
bin_name = '1' # 这里设为字符串以保持一致
db['bin_name'] = bin_name
2024-09-26 18:01:24 +08:00
def is_admin():
"""检查当前是否有管理员权限"""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
# 如果已经是管理员,执行主要的逻辑
print("已获得管理员权限")
2024-09-28 11:32:19 +08:00
path = os.getcwd() # 获取当前工作目录的实际路径
# 检查并创建cmd文件
check_and_create_cmd_files(path)
2024-09-26 18:01:24 +08:00
print(f"当前模式(1为CS原版, 2为CS:SO): {bin_name}")
print("请选择模式")
print("1. 改为CS:SO\n2. 改回原版\n3. 退出")
choice = input('>>> ')
if choice == '1':
if bin_name == '2':
print('错误你已是CS:SO!')
sleep(1.5)
sys.exit()
2024-09-27 22:26:31 +08:00
# 启动 csso.cmd
os.startfile(os.path.join(path, 'csso.cmd'))
2024-09-26 18:01:24 +08:00
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()
2024-09-27 22:26:31 +08:00
# 启动 css.cmd
os.startfile(os.path.join(path, 'css.cmd'))
2024-09-26 18:01:24 +08:00
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` 模块提升权限,重新运行脚本
2024-09-27 22:26:31 +08:00
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)