Skip to content

Commit

Permalink
v1.3 根目录模式
Browse files Browse the repository at this point in the history
  • Loading branch information
Hikari16665 committed Jul 1, 2024
1 parent 13271ea commit f09733e
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ dist/
*.spec
#venv
.venv/
test.py
test*.py
.gitignore
.git/
26 changes: 16 additions & 10 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

class Config:
def __init__(self):
self.first_run = True
self.use_mirror = False
self.workspace_dir = 'workspace'
self.config_dir = 'hsl-config'
self.config_file = 'config.json'
self.workspace_file = 'workspace.json'
self.config_path = os.path.join(self.config_dir, self.config_file)
self.workspace_path = os.path.join(self.workspace_dir, self.workspace_file)
self.autorun = ''
self.first_run: bool = True
self.use_mirror: bool = False
self.workspace_dir: str = 'workspace'
self.config_dir: str = 'hsl-config'
self.config_file: str = 'config.json'
self.workspace_file: str = 'workspace.json'
self.config_path: str = os.path.join(self.config_dir, self.config_file)
self.workspace_path: str = os.path.join(self.workspace_dir, self.workspace_file)
self.autorun: str = ''
self.debug: bool = False
self.direct_mode: bool = False
self.initialize()

def initialize(self):
Expand All @@ -33,11 +35,15 @@ def save_config(self):
'first_run': self.first_run,
'use_mirror': self.use_mirror,
'autorun': self.autorun,
'debug': self.debug,
'direct_mode': self.direct_mode
}, f)
self.load_config()
def load_config(self):
with open(self.config_path, 'r') as f:
config = json.load(f)
self.first_run = config['first_run']
self.use_mirror = config['use_mirror']
self.autorun = config['autorun']
self.autorun = config['autorun']
self.debug = config['debug']
self.direct_mode = config['direct_mode']
70 changes: 39 additions & 31 deletions hsl.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,62 @@
import requests
from config import Config
import httpx
import asyncio

from rich.console import Console

from config import Config


console = Console()

HSL_VERSION = 12
DOWNLOAD_SOURCE = r'http://hsl.hikari.bond/source.json'
CONFIGS_SOURCE = r'http://hsl.hikari.bond/configs.json'
VERSION = r'http://hsl.hikari.bond/hsl.json'
HSL_VERSION = 13
DOWNLOAD_SOURCE = r'https://hsl.hikari.bond/source.json'
CONFIGS_SOURCE = r'https://hsl.hikari.bond/configs.json'
VERSION_SOURCE = r'https://hsl.hikari.bond/hsl.json'

def make_request(url: str, error_message: str, timeout: int = 3) -> dict:
try:
response = requests.get(url, timeout=timeout)
response.raise_for_status() # Raise an HTTPError for bad responses
return response.json()
except requests.RequestException:
console.print(error_message)
return {}

def check_update(version: int) -> tuple:
data = make_request(VERSION, '检查更新失败')
async def make_request(url: str, error_message: str, timeout: int = 3) -> dict:
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, timeout=timeout)
response.raise_for_status() # Will raise an HTTPError for bad status codes
return response.json()
except httpx.RequestError:
console.print(error_message)
return {}

async def check_update(version: int) -> tuple[bool, int]:
data = await make_request(VERSION_SOURCE, '检查更新失败')
if data:
latest = data['version']
latest: int = data['version']
if version < latest:
return True, latest
else:
return False, version
return False, version

def get_source() -> dict:
data = make_request(DOWNLOAD_SOURCE, '无法获取下载源信息')
async def get_source() -> dict:
data = await make_request(DOWNLOAD_SOURCE, '无法获取下载源信息')
if data:
return data
raise Exception('无法连接到服务器,请检查网络连接,软件将关闭...')

def get_configs() -> list:
data = list(make_request(CONFIGS_SOURCE, '无法获取特定配置信息'))
async def get_configs() -> list:
data = list(await make_request(CONFIGS_SOURCE, '无法获取特定配置信息'))
if data:
return data
return []

try:
SOURCE = get_source()
except Exception as e:
console.print(e)
exit()

NEWVERSION_INFO = check_update(HSL_VERSION)
async def init() -> tuple:
tasks = [
get_source(),
check_update(HSL_VERSION)
]
try:
SOURCE, NEWVERSION_INFO = await asyncio.gather(*tasks, return_exceptions=True)
except Exception as e:
console.print(e)
exit()
return SOURCE, NEWVERSION_INFO
CONFIG = Config()

SOURCE, NEWVERSION_INFO = asyncio.run(init())
class HSL:
def __init__(self):
self.source = SOURCE
Expand Down
51 changes: 42 additions & 9 deletions java.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import zipfile

from utils.download import downloadFile

from hsl import HSL


Expand All @@ -9,9 +11,15 @@
elif os.name == 'posix':
JAVA_EXEC = 'java'
class Java(HSL):

def __init__(self):
super().__init__()
async def getJavaVersion(self,mcVersion) -> str:
async def getJavaVersion(self,mcVersion: str) -> str:
'''
get java version
:param mcVersion: minecraft version
:return: java version
'''
parts = mcVersion.split('.')
version = int(parts[1])
if version <= 6:
Expand All @@ -27,12 +35,24 @@ async def getJavaVersion(self,mcVersion) -> str:
else:
return '0'
async def checkJavaExist(self,javaVersion,path) -> bool:
'''
check java exist
:param javaVersion: java version
:param path: path
:return: exist? bool
'''
if not os.path.exists(os.path.join(path,'java',javaVersion,'bin')):
return False
return True
async def downloadJava(self,javaVersion,path):
async def downloadJava(self,javaVersion,path) -> bool:
'''
download java
:param javaVersion: java version
:param path: path
:return: done? bool
'''
sources = self.source['java']['list']
if self.config['use_mirror']:
if self.config.use_mirror:
sources = sources[::-1]
path = os.path.join(path,'java',javaVersion)
filename = os.path.join(path,'java.zip')
Expand All @@ -46,12 +66,19 @@ async def downloadJava(self,javaVersion,path):
if os.name == 'posix':
url = i['linux'][javaVersion]
if downloadFile(url,filename):
break
#unzip java.zip
with zipfile.ZipFile(filename,'r') as file:
file.extractall(path)
os.remove(filename)
async def getJavaByGameVersion(self, mcVersion: str, path: str):
with zipfile.ZipFile(filename,'r') as file:
file.extractall(path)
os.remove(filename)
return True
return False

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
"""
javaVersion = await self.getJavaVersion(mcVersion)
javaPath = os.path.join(path,'java',javaVersion)
if not await self.checkJavaExist(javaVersion, path):
Expand All @@ -60,6 +87,12 @@ async def getJavaByGameVersion(self, mcVersion: str, path: str):
return os.path.join(javaPath, 'bin', JAVA_EXEC)

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
"""
javaPath = os.path.join(path,'java',javaVersion)
if not await self.checkJavaExist(javaVersion, path):
print(f'Java版本 {javaVersion} 不存在。正在下载Java...')
Expand Down
Loading

0 comments on commit f09733e

Please sign in to comment.