Skip to content

Commit

Permalink
v1.4-4 修改结构
Browse files Browse the repository at this point in the history
  • Loading branch information
Hikari16665 committed Jul 13, 2024
1 parent a4bcc6b commit 57f2c35
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 65 deletions.
1 change: 1 addition & 0 deletions gametypes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import vanilla, forge, fabric, paper
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
86 changes: 57 additions & 29 deletions java.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import os
import zipfile

from utils.download import downloadFile

from hsl import HSL
from utils.download import downloadFile


if os.name == 'nt':
JAVA_EXEC = 'java.exe'
# Windows
elif os.name == 'posix':
JAVA_EXEC = 'java'
# Linux
async def recursive_chmod(directory, mode):
"""
Recursively change the permissions of a directory and its contents.
for Linux.
Args:
directory(str): directory path
mode(int): permission mode
"""

for root, dirs, files in os.walk(directory):
for d in dirs:
os.chmod(os.path.join(root, d), mode)
Expand All @@ -23,11 +31,15 @@ def __init__(self):
super().__init__()

async def getJavaVersion(self,mcVersion: str) -> str:
'''
get java version
:param mcVersion: minecraft version
:return: java version
'''
"""
Get java version by game version.
Args:
mcVersion(int): mc version
Returns:
str: java version should be used
"""
parts = mcVersion.split('.')
version = int(parts[1])
if version <= 6:
Expand All @@ -43,22 +55,30 @@ async def getJavaVersion(self,mcVersion: str) -> str:
else:
return '0'
async def checkJavaExist(self,javaVersion,path) -> bool:
'''
check java exist
:param javaVersion: java version
:param path: path
:return: exist? bool
'''
"""
Check if Java exists.
Args:
javaVersion(int): Java version
path(str): workspace path
Returns:
bool: True if the java exists, False otherwise.
"""

if not os.path.exists(os.path.join(path,'java',javaVersion,'bin')):
return False
return True
async def downloadJava(self,javaVersion,path) -> bool:
'''
download java
:param javaVersion: java version
:param path: path
:return: done? bool
'''
async def downloadJava(self, javaVersion, path) -> bool:
"""
Download Java and return the status.
Args:
javaVersion(int): Java version
path(str): workspace path
Returns:
bool: True if the java is downloaded and extracted successfully, False otherwise.
"""
sources = self.source['java']['list']
if self.config.use_mirror:
sources = sources[::-1]
Expand All @@ -83,10 +103,14 @@ async def downloadJava(self,javaVersion,path) -> bool:

async def getJavaByGameVersion(self, mcVersion: str, path: str) -> str:
"""
get java path by game version
:param mcVersion: minecraft version
:param path: path
:return: java exec path
get java path by game version(if not exist, will call downloadJava)
Args:
mcVersion(int): mc version
path(str): workspace path
Returns:
str: java executeable file path
"""
javaVersion = await self.getJavaVersion(mcVersion)
javaPath = os.path.join(path,'java',javaVersion)
Expand All @@ -97,10 +121,14 @@ async def getJavaByGameVersion(self, mcVersion: str, path: str) -> str:

async def getJavaByJavaVersion(self, javaVersion:str, path:str) -> str:
"""
get java path by java version
:param javaVersion: java version
:param path: path
:return: java exec path
get java path by java version(if not exist, will call downloadJava)
Args:
javaVersion(int): java version
path(str): workspace path
Returns:
str: java executeable file path
"""
javaPath = os.path.join(path,'java',javaVersion)
if not await self.checkJavaExist(javaVersion, path):
Expand Down
56 changes: 43 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
from typing import Callable
from rich.console import Console

from prompt import promptSelect, promptInput, promptConfirm
from utils import vanilla, paper, forge, fabric, osfunc
from gametypes import fabric, forge, paper, vanilla
from utils.prompt import promptSelect, promptInput, promptConfirm
from utils import osfunc
from hsl import HSL, get_configs
from server import Server
from workspace import Workspace
from java import Java
import gui
import utils.gui as gui


OPTIONS_YN = ['是', '否']
Expand All @@ -35,6 +36,9 @@ def __init__(self):
self.Java = Java()

async def welcome(self):
"""
Welcome
"""
console.rule('配置设置')
console.print('如果你的服务器环境在国内, 推荐使用镜像源源以获得更好的速度。\n是否使用镜像源优先? (默认: 否)\n')
self.config.use_mirror = await promptConfirm('是否使用镜像源优先?')
Expand All @@ -46,9 +50,13 @@ async def welcome(self):
await self.create()

async def exit(self):
exit()
#exit the program
os._exit(0)

async def create(self):
"""
Create server
"""
serverName = await self.get_valid_server_name()
if not serverName:
console.print('[bold magenta]服务器已存在。')
Expand All @@ -63,11 +71,16 @@ async def create(self):

serverName, serverType, serverPath, javaPath, data = server_setting
maxRam = await self.get_valid_max_ram()

if maxRam is None:
maxRam = str(OS_MAXRAM) + 'M'
server = Server(name=serverName, type=serverType, path=serverPath, javaPath=javaPath, maxRam=maxRam, data=data)
await self.Workspace.add(server)

async def get_valid_server_name(self):
async def get_valid_server_name(self) -> str | None:
"""
Get valid server name
Return: str | None
"""
serverName = await promptInput('请输入服务器名称:')
while (not serverName.strip()) or (serverName in ['con','aux','nul','prn'] and os.name == 'nt'):
serverName = await promptInput('名称非法,请重新输入:')
Expand All @@ -77,13 +90,30 @@ async def get_valid_server_name(self):
return None
return serverName

async def get_valid_max_ram(self):
async def get_valid_max_ram(self) -> str | None:
"""
Get valid max ram
Return: str | None
"""
maxRam = await promptInput(f'你的主机最大内存为:{OS_MAXRAM}MB 请输入服务器最大内存(示例:1024M 或 1G):')
while not MAXRAM_PATTERN.match(maxRam):
maxRam = await promptInput('输入错误,请重新输入:')
return maxRam

async def install(self, *, serverName: str, serverPath: str):
"""
Install the server
Args:
serverName: str
serverPath: str
Returns:
serverName,
serverPath,
serverJarPath,
data
"""

if self.config.direct_mode:
serverPath = ''
serverJarPath = os.path.join(serverPath, 'server.jar')
Expand All @@ -100,7 +130,7 @@ async def install(self, *, serverName: str, serverPath: str):
}
return await install_methods[gameType](serverName, serverPath, serverJarPath, data)

async def install_vanilla(self, serverName, serverPath, serverJarPath, data):
async def install_vanilla(self, serverName: str, serverPath: str, serverJarPath: str, data: dict):
serverType = 'vanilla'
mcVersions = [x['id'] for x in await vanilla.get_versions(self.source) if x['type'] == 'release']
mcVersion = mcVersions[await promptSelect(mcVersions, '请选择Minecraft服务器版本:')]
Expand All @@ -112,7 +142,7 @@ async def install_vanilla(self, serverName, serverPath, serverJarPath, data):
console.print('Vanilla 服务端下载完成。')
return serverName, serverType, serverPath, javaPath, data

async def install_paper(self, serverName, serverPath, serverJarPath, data):
async def install_paper(self, serverName: str, serverPath: str, serverJarPath: str, data: dict):
serverType = 'paper'
mcVersion = await paper.getLatestVersionName(self.source)
javaPath = await self.Java.getJavaByGameVersion(mcVersion, path=self.config.workspace_dir)
Expand All @@ -122,7 +152,7 @@ async def install_paper(self, serverName, serverPath, serverJarPath, data):
console.print('Paper 服务端下载完成。')
return serverName, serverType, serverPath, javaPath, data

async def install_forge(self, serverName, serverPath, serverJarPath, data):
async def install_forge(self, serverName: str, serverPath: str, serverJarPath: str, data: dict):
serverType = 'forge'
mcVersions = await vanilla.get_versions(self.source)
_mcVersions = await forge.get_mcversions(self.source, self.config.use_mirror)
Expand Down Expand Up @@ -154,7 +184,7 @@ async def install_forge(self, serverName, serverPath, serverJarPath, data):

return serverName, serverType, serverPath, javaPath, data

async def install_fabric(self, serverName, serverPath, serverJarPath, data):
async def install_fabric(self, serverName: str, serverPath: str, serverJarPath: str, data: str):
serverType = 'fabric'
fabVersion = await fabric.getMcVersions(self.source)
mcVersion = fabVersion[await promptSelect(fabVersion, '请选择 Fabric 服务器版本:')]
Expand Down Expand Up @@ -373,7 +403,7 @@ async def mainMenu(self):
console.print(f'[bold gold]欢迎使用 {HSL_NAME}.')
index = await promptSelect(OPTIONS_MENU, '菜单:')

menu_methods = {
menu_methods: dict[int, Callable] = {
0: lambda: self.create(),
1: lambda: self.manage(),
2: lambda: self.delete(),
Expand All @@ -387,7 +417,7 @@ async def autorun(self):
server = await self.Workspace.getFromName(self.config.autorun)
console.print(f'[bold blue]将于三秒后启动 {server.name}。,键入Ctrl+C(^C)可取消.')
await asyncio.sleep(3)
server.run()
await server.run()
exit()

mainProgram = Main()
Expand Down
Loading

0 comments on commit 57f2c35

Please sign in to comment.