# -*- coding:utf-8 -*-
import ast
import configparser
import re
import psutil
import time
from subprocess import Popen
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
from configparser import ExtendedInterpolation
# 界面文件为 mainui.py
from mainui import *
import requests
import os
configFile = 'config.ini'
# 创建配置文件对象
config = configparser.ConfigParser(interpolation=ExtendedInterpolation(), inline_comment_prefixes=['#', ';'], allow_no_value=True)
# 读取配置文件
config.read(configFile, encoding='utf-8')
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
# 初始赋值
self.nowIpLabel.setText(config['GAME_SET']['now_ip'])
self.newIpInput.setText(config['GAME_SET']['new_ip'])
self.nowGameLabel.setText(config['GAME_SET']['game_name'])
self.newGameInput.setText(config['GAME_SET']['game_name'])
if isinstance(checkprocess("nginx.exe"), int):
# self.启动Nginx.Label = '关闭Nginx'
self.nginxButton.setText("关闭Nginx")
else:
# self.启动Nginx.Label = '启动Nginx'
self.nginxButton.setText("启动Nginx")
def saveSet(self, event):
print('保存设置,按钮被单击')
# print(config['GAME_PATH']['game'])
# tinydict = ast.literal_eval(config['GAME_PATH']['game'])
# print("字典值 : %s" %tinydict.items())
# # 遍历字典列表
# for key, values in tinydict.items():
# print(key, values)
# for key, value in config['GAME_PATH'].items():
# print(key, value)
# print(config.options('GAME_PATH'))
# return
message = QMessageBox.information(self, "提示信息", "确定要保存设置吗?", QMessageBox.Ok | QMessageBox.No, QMessageBox.No)
if message == QMessageBox.No:
return
now_ip = config['GAME_SET']['now_ip']
new_ip = self.newIpInput.text()
now_game = config['GAME_SET']['game_name']
new_game = self.newGameInput.text()
compile_ip = re.compile('^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$')
# 验证IP地址格式
if (new_ip == "") or new_game == "" or not (compile_ip.match(new_ip)):
QMessageBox.information(self, "提示信息", "IP地址、游戏名称为空或有误,请正确填写IPv4地址或游戏名称!")
return
dirname = config['MODFILE']['dirname']
sum = 0
for filedir in dirname.split("|"):
# 执行文本替换
if len(filedir.split(",")) < 2:
isencoding = config['DEFAULT']['file_encoding']
else:
isencoding = filedir.split(",")[1]
filename = filedir.split(",")[0]
try:
with open(filename, 'rt+', encoding=isencoding) as f:
t = f.read()
t = t.replace(now_ip, new_ip)
t = t.replace(now_game, new_game)
# 读写偏移位置移到最开始处
f.seek(0, 0)
f.write(t)
# 设置文件结尾 EOF
f.truncate()
sum = sum + 1
except:
QMessageBox.information(self, "提示信息", "替换“" + filename + "”出错,可能文件不存在或编码不对,请检查需要替换的文件编码与配置文件编码是否相符!")
return
# 写入配置文件
config.set('GAME_SET', 'game_name', new_game)
config.set('GAME_SET', 'now_ip', new_ip)
config.set('GAME_SET', 'new_ip', new_ip)
saveconfig = open(configFile, 'wt', encoding=config['DEFAULT']['file_encoding'])
config.write(saveconfig) # 把要修改的节点的内容写到文件中
saveconfig.close()
# 设置界面显示
self.nowGameLabel.setText(new_game)
self.nowIpLabel.setText(new_ip)
# 成功提示
QMessageBox.information(self, "提示信息", "配置保存成功!重启游戏后生效!
游戏名称:" + new_game + "----游戏IP:" + new_ip + "
共配置了" + str(sum) + "个文件")
print('修改完成:' + new_game + ':' + new_ip)
return
def nginx(self, event):
if self.nginxButton.text() == '启动Nginx':
nginx_process = Popen('start .\\nginx.exe',
shell=True,
cwd=config['SERVER_PATH']['nginx_path'],
# encoding='utf-8'
)
php_process = Popen(config['SERVER_PATH']['php_spawner_path'] + 'php-cgi-spawner.exe "' + config['SERVER_PATH']['php_path'] + 'php-cgi.exe -c ' + config['SERVER_PATH']['php_path'] + 'php.ini" ' + config['SERVER_PATH']['php_cgi_port'] + ' 1+16',
shell=True,
# cwd=config['DEFAULT']['dir'] + 'phpstudy_pro',
# encoding='utf-8'
)
self.nginxButton.setText("关闭Nginx")
else:
nginx_process = Popen('.\\nginx.exe -s stop',
shell=True,
cwd=config['SERVER_PATH']['nginx_path'],
# encoding='utf-8'
)
spawner_process = Popen('taskkill /f /t /im php-cgi-spawner.exe',
shell=True,
# encoding='utf-8'
)
PHP_process = Popen('taskkill /f /t /im php-cgi.exe',
shell=True,
# encoding='utf-8'
)
self.nginxButton.setText("启动Nginx")
# print(process)
# os.chdir("D:\\LYserver\\phpstudy_pro\\Extensions\\Nginx1.15.11")
# nginxtype = os.system('start .\\nginx.exe') # 不弹出界面
# if nginxtype
# os.system('start .\\nginx.exe') #弹出界面
print('启动Nginx,按钮被单击')
def mysql(self, event):
if self.mysqlButton.text() == '启动MySql':
php_process = Popen('.\\mysqld.exe',
shell=True,
cwd=config['SERVER_PATH']['mysql_path'] + 'bin',
# encoding='utf-8'
)
self.mysqlButton.setText("关闭MySql")
else:
kill_process = Popen('taskkill /f /t /im mysqld.exe',
shell=True,
# encoding='utf-8'
)
self.mysqlButton.setText("启动MySql")
print('启动MySql,按钮被单击')
def game(self, event):
if self.gameButton.text() == '启动游戏':
# tinydict = ast.literal_eval(config['GAME_PATH']['game_path'])
game_path = config['GAME_PATH']['game_path'].split("|")
# print("字典值 : %s" %game_path.items())
# 遍历字典列表
for f in game_path:
print(f)
# 执行文本替换
if len(f.split(",")) < 2:
sleeptime = 0
else:
sleeptime = float(f.split(",")[1])
game_file = f.split(",")[0]
try:
Popen('start ' + game_file,
shell=True,
# cwd=values,
# encoding='utf-8'
)
time.sleep(float(sleeptime)) # 延迟时间
except:
QMessageBox.information(self, "提示信息", "启动“" + game_file + "”出错,可能文件不存在或配置不对!")
return
self.gameButton.setText("关闭游戏")
else:
Popen(
'taskkill /f /t /im LocalLogServer64_R.exe & taskkill /f /t /im LoggerServer64_R.exe & taskkill /f /t /im SessionServer64_R.exe & taskkill /f /t /im NameServer64_R.exe & taskkill /f /t /im LogicServerCQ64_R.exe & taskkill /f /t /im GateServer64_R.exe & taskkill /f /t /im DBCenterServer64_R.exe & taskkill /f /t /im DBServer64_R.exe & taskkill /f /t /im BackStageServer64_R.exe & taskkill /f /t /im AMServer64_R.exe',
shell=True,
# encoding='utf-8'
)
self.gameButton.setText("启动游戏")
print('启动游戏,按钮被单击')
def fanbianyi(self, event):
apktool = Popen('apktool d game.apk',
shell=True,
cwd=config['DEFAULT']['dir'] + 'apk',
# encoding='utf-8'
)
print('反编译APK,按钮被单击')
def shengcheng(self, event):
apktool = Popen('apktool b game',
shell=True,
cwd=config['DEFAULT']['dir'] + 'apk',
# encoding='utf-8'
)
print('生成APK,按钮被单击')
def qianming(self, event):
apktool = Popen('jarsigner -verbose -keystore key.keystore -storepass 123456 -signedjar game-signed.apk game/dist/game.apk key.keystore',
shell=True,
cwd=config['DEFAULT']['dir'] + 'apk',
# encoding='utf-8'
)
print('APK签名,按钮被单击')
# 进程判断,返回进程ID
def checkprocess(processname):
pl = psutil.pids()
for pid in pl:
if psutil.Process(pid).name() == processname:
return pid
if __name__ == '__main__':
app = QApplication(sys.argv)
myWin = MainWindow()
myWin.show()
sys.exit(app.exec_())