Manager modów/pluginów pobiera zależności
This commit is contained in:
parent
694134ccec
commit
8ea8fd3567
2 changed files with 71 additions and 38 deletions
49
app/api.py
49
app/api.py
|
@ -330,31 +330,64 @@ def download_mod():
|
||||||
server_version = data['version']
|
server_version = data['version']
|
||||||
|
|
||||||
base_path = f"./servers/mc-{username}/{'plugins' if server_type == 'paper' else 'mods'}"
|
base_path = f"./servers/mc-{username}/{'plugins' if server_type == 'paper' else 'mods'}"
|
||||||
|
os.makedirs(base_path, exist_ok=True)
|
||||||
|
|
||||||
downloaded = []
|
downloaded_files = []
|
||||||
|
visited_versions = set()
|
||||||
|
|
||||||
def download_project(pid, version_id=None):
|
def download_project(pid, version_id=None):
|
||||||
if version_id:
|
if version_id:
|
||||||
version_data = requests.get(f"https://api.modrinth.com/v2/version/{version_id}").json()
|
version_data = requests.get(f"https://api.modrinth.com/v2/version/{version_id}").json()
|
||||||
else:
|
else:
|
||||||
all_versions = requests.get(f"https://api.modrinth.com/v2/project/{pid}/version").json()
|
all_versions = requests.get(f"https://api.modrinth.com/v2/project/{pid}/version").json()
|
||||||
version_data = next((v for v in all_versions if server_version in v['game_versions'] and server_type in v['loaders']), None)
|
version_data = next(
|
||||||
|
(v for v in all_versions
|
||||||
|
if server_version in v['game_versions']
|
||||||
|
and server_type in v['loaders']
|
||||||
|
and v.get('server_side') != 'unsupported'),
|
||||||
|
None)
|
||||||
if not version_data:
|
if not version_data:
|
||||||
|
print(f"[SKIP] No compatible version found for {pid}")
|
||||||
return
|
return
|
||||||
|
|
||||||
file = version_data['files'][0]
|
if version_data["id"] in visited_versions:
|
||||||
file_path = f"{base_path}/{file['filename']}"
|
return
|
||||||
if file['filename'] not in downloaded:
|
visited_versions.add(version_data["id"])
|
||||||
|
|
||||||
|
if version_data.get('server_side') == 'unsupported':
|
||||||
|
print(f"[SKIP] {version_data.get('name', 'Nieznana nazwa')} is client-only")
|
||||||
|
return
|
||||||
|
|
||||||
|
file = next((f for f in version_data['files'] if f.get('primary', True)), version_data['files'][0])
|
||||||
|
file_path = os.path.join(base_path, file['filename'])
|
||||||
|
|
||||||
|
if not os.path.exists(file_path):
|
||||||
|
print(f"[DOWNLOAD] {file['filename']}")
|
||||||
with requests.get(file['url'], stream=True) as r:
|
with requests.get(file['url'], stream=True) as r:
|
||||||
with open(file_path, 'wb') as f:
|
with open(file_path, 'wb') as f:
|
||||||
for chunk in r.iter_content(chunk_size=8192):
|
for chunk in r.iter_content(chunk_size=8192):
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
downloaded.append(file['filename'])
|
|
||||||
|
downloaded_files.append({
|
||||||
|
"filename": file['filename'],
|
||||||
|
"name": version_data.get("name") or version_data.get("version_number") or "Nieznana wersja",
|
||||||
|
"project_id": version_data["project_id"],
|
||||||
|
"version_id": version_data["id"]
|
||||||
|
})
|
||||||
|
|
||||||
for dep in version_data.get('dependencies', []):
|
for dep in version_data.get('dependencies', []):
|
||||||
if dep.get('version_id'):
|
if dep.get('version_id'):
|
||||||
download_project(dep['project_id'], dep['version_id'])
|
try:
|
||||||
|
download_project(dep['project_id'], dep['version_id'])
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[ERROR] Failed to fetch dep version {dep['version_id']}: {e}")
|
||||||
|
elif dep.get('project_id'):
|
||||||
|
try:
|
||||||
|
download_project(dep['project_id'])
|
||||||
|
except Exception as e:
|
||||||
|
print(f"[ERROR] Failed to fetch dep project {dep['project_id']}: {e}")
|
||||||
|
|
||||||
download_project(project_id)
|
download_project(project_id)
|
||||||
|
|
||||||
return jsonify({"success": True, "downloaded": downloaded})
|
return jsonify({"success": True, "downloaded": downloaded_files})
|
||||||
|
|
||||||
|
|
|
@ -33,36 +33,36 @@ function searchMods() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function installMod(projectId) {
|
function installMod(projectId) {
|
||||||
fetch('/api/config')
|
fetch('/api/config')
|
||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(configData => {
|
.then(configData => {
|
||||||
console.log(configData);
|
if (configData.success) {
|
||||||
if (configData.success) {
|
const serverType = configData.type;
|
||||||
const serverType = configData.type;
|
const serverVersion = configData.version;
|
||||||
const serverVersion = configData.version;
|
if (serverType && serverVersion) {
|
||||||
if (serverType && serverVersion) {
|
fetch('/api/modrinth/download', {
|
||||||
fetch('/api/modrinth/download', {
|
method: 'POST',
|
||||||
method: 'POST',
|
headers: { 'Content-Type': 'application/json' },
|
||||||
headers: { 'Content-Type': 'application/json' },
|
body: JSON.stringify({ project_id: projectId, username, type: serverType, version: serverVersion }),
|
||||||
body: JSON.stringify({ project_id: projectId, username, type: serverType, version: serverVersion }),
|
})
|
||||||
})
|
.then(r => r.json())
|
||||||
.then(r => r.json())
|
.then(data => {
|
||||||
.then(data => {
|
if (data.success) {
|
||||||
if (data.success) {
|
const modFiles = data.downloaded.map(mod => "- " + mod.filename).join("\n");
|
||||||
alert(`Zainstalowano: ${data.file}`);
|
alert(`Zainstalowano:\n${modFiles}`);
|
||||||
loadFileList();
|
loadFileList();
|
||||||
} else {
|
} else {
|
||||||
alert("Nie udało się zainstalować moda.");
|
alert("Nie udało się zainstalować moda.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
console.error('Server type or version not available');
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
console.error('Server config not found');
|
console.error('Server type or version not available');
|
||||||
}
|
}
|
||||||
})
|
} else {
|
||||||
.catch(err => {
|
console.error('Server config not found');
|
||||||
console.error('Error fetching server config:', err);
|
}
|
||||||
});
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error('Error fetching server config:', err);
|
||||||
|
});
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue