Skip to content

Commit

Permalink
Beta 5 - Optimization
Browse files Browse the repository at this point in the history
Beta 5 - Run game
Beta 5 - Select username
  • Loading branch information
EyuphanMandiraci committed Nov 28, 2021
1 parent df2b82c commit 4973dfc
Show file tree
Hide file tree
Showing 12 changed files with 812 additions and 45 deletions.
25 changes: 25 additions & 0 deletions forge_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from wget import download
from zipfile import ZipFile
from json import loads
import minecraft_launcher_lib as mll
import threading


def download_forge_installer(version):
global installer
installer = f"forge-{version}-installer.jar"
url = f"https://files.minecraftforge.net/maven/net/minecraftforge/forge/{version}/forge-{version}-installer.jar"
download(url)



def download_forge(version):
download_forge_installer(version)
zf = ZipFile(installer, "r")
with zf.open("install_profile.json", "r") as f:
version_content = f.read()
version_data = loads(version_content)
forge_ver = version_data["version"]
minecraft_ver = version_data["minecraft"]


56 changes: 47 additions & 9 deletions funcs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from PyQt5.QtWidgets import QPushButton, QLabel, QDesktopWidget
from PyQt5.QtCore import Qt
import web_request
Expand All @@ -9,7 +11,7 @@
from shutil import move, rmtree
from pathlib import Path
from random import choice
from threads import download_thread
from threads import download_thread, run_thread
from requests import get


Expand All @@ -26,9 +28,12 @@ def center(cls):

def mp_names_loop(cls, mp_names, clicked_connect=none_test):
pos = 130
width = len(max(mp_names, key=len)) * 8
names = []
for i in mp_names:
names.append(i["mp_name"])
width = len(max(names, key=len)) * 8
for i in mp_names:
cls.mp_label = QLabel(i, cls)
cls.mp_label = QLabel(i["mp_name"], cls)
cls.mp_label.move(0, pos)
cls.mp_label.resize(width, cls.mp_label.height())
cls.mp_label.setAlignment(Qt.AlignLeft)
Expand All @@ -39,7 +44,7 @@ def mp_names_loop(cls, mp_names, clicked_connect=none_test):
cls.mp_button.move(width, pos)
cls.mp_button.setStyleSheet("background:rgba(185,187,190,255)")
cls.mp_button.clicked.connect(clicked_connect)
cls.mp_button.setObjectName(i)
cls.mp_button.setObjectName(i["mp_name"])
pos += 30
link.init(cls, "https://mpdb.xyz", pos + 30)

Expand All @@ -58,8 +63,8 @@ def change_button_text(cls, value):
cls.run_button.setText(f"Run {value}")
cls.selected_mp = value
cls.forge_selector.clear()
info = loads(get("https://mpdb.xyz/get_data.php?data=true&from=name&name=" + value).text)
cls.forge_selector.addItems(get_forges(info["version"]))
info = loads(get(f"https://mpdb.xyz/api/modpack.php?name={value}").text)
cls.forge_selector.addItems(get_forges(info["mp_version"]))
cls.run_button.resize(len("Run " + value) * 8, cls.run_button.height())
cls.username_selector.move(cls.width() - cls.run_button.width() - cls.username_selector.width(), 0)
cls.run_button.move(cls.width() - cls.run_button.width(), 0)
Expand All @@ -69,7 +74,8 @@ def change_button_text(cls, value):
def download_modpack(name, author, data):
global display_name, download_url
print(f"Downloading {name} by {author}!")
download(f"https://mpdb.xyz/modpacks/{author}_{name}.zip")
download(f"http://185.255.94.159/modpacks/{author}/{name}.zip")
os.rename(f"{name}.zip", f"{author}_{name}.zip")
zip_path = f"{author}_{name}.zip"
if name not in data["downloaded"]:
data["downloaded"].append(name)
Expand Down Expand Up @@ -125,7 +131,7 @@ def start_download_worker(cls, mp_name, data):
cls.run_minecraft_info.setText(f"Downloading {mp_name}")
cls.run_minecraft_info.resize(len(f"Downloading {mp_name}") * 8, cls.run_minecraft_info.height())
for i in cls.mp_names:
b = cls.findChild(QPushButton, i)
b = cls.findChild(QPushButton, i["mp_name"])
b.setEnabled(False)
cls.run_button.setEnabled(False)
cls.modpack_selector.setEnabled(False)
Expand All @@ -137,8 +143,9 @@ def start_download_worker(cls, mp_name, data):
def stop_download_worker():
thread.stop()
cl.run_minecraft_info.setText("")
print(cl.mp_names)
for i in cl.mp_names:
b = cl.findChild(QPushButton, i)
b = cl.findChild(QPushButton, i["mp_name"])
b.setEnabled(True)
b = cl.findChild(QPushButton, modpack_name)
b.hide()
Expand All @@ -156,3 +163,34 @@ def stop_download_worker():
def get_forges(version=None):
if version is not None:
return forges[version]


def start_run_worker(mc_version, forge_version, mp_name, username, ram, cls):
global run
if username == "":
run = run_thread.RunThread(mc_version=mc_version, forge_version=forge_version, mp_name=mp_name, ram=ram)
else:
run = run_thread.RunThread(mc_version=mc_version, forge_version=forge_version, mp_name=mp_name,
username=username, ram=ram)
run.start()
run.any_signal.connect(lambda cls=cls: stop_run_worker(cls))

def stop_run_worker(cls):
cls.run_button.setEnabled(True)
run.stop()


def download_file(url, path=""):
with open(path, "wb") as f:
r = get(url, stream=True)
total_length = r.headers.get("content-length")
if total_length is None:
print(r.content)
else:
total_length = int(total_length)
for data in r.iter_content(chunk_size=4096):
f.write(data)




6 changes: 6 additions & 0 deletions mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from requests import get



def send_email(e):
get("https://mpdb.xyz/mail.php?e="+e)
79 changes: 73 additions & 6 deletions mpdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtGui import QCursor
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from time import perf_counter
import data
import web_request
from get_ram import get_ram
import funcs
from ui import modpack_selector, ram_selector, username_selector, discord_image, run_button, run_minecraft_info, \
forge_selector
forge_selector, progress_bar
from webbrowser import open as wb_open
from mail import send_email
from requests import get
from json import loads

run_timer = perf_counter()

Expand All @@ -26,11 +30,13 @@ class MPDB(QMainWindow):
try:
def __init__(self):
super().__init__()
# self.resize(1000, 600)
# progress_bar.init(self)


def initUI(self):

self.mp_names = web_request.get_data("https://mpdb.xyz/get_data.php")
self.mp_names = web_request.get_data("https://mpdb.xyz/api/modpack.php")
self.rams = []
self.data = {}
self.selected_mp = ""
Expand All @@ -40,14 +46,14 @@ def initUI(self):
else:
self.data = data.get_data("data.txt")
if len(self.data["downloaded"]) != 0:
# TODO: Buraya run butonunu disable etmeyi ekle
pass

for ram in range(get_ram() - 1, 0, -1):
self.rams.append(str(ram) + " GB")

self.resize(1000, 600)

self.setWindowTitle("TheModpackDatabase")
self.resize(1000, 600)
self.setStyleSheet("background:rgba(54,57,63,255)")
self.setMinimumSize(839, 438)
self.setMouseTracking(True)
Expand All @@ -56,10 +62,13 @@ def initUI(self):
ram_selector.init(self, self.rams)
username_selector.init(self)
discord_image.init(self, "https://mpdb.xyz/discord.png")
run_button.init(self, f"RUN {modpack_selector.currentText(self)}")
run_button.init(self, f"RUN {modpack_selector.currentText(self)}", clicked_connect=self.start_runner)
self.run_button.resize(len(self.run_button.text()) * 8, self.run_button.height())
run_minecraft_info.init(self)
forge_selector.init(self, 0)
# progress_bar.init(self)
# print(self.progress_bar)

if len(self.data["downloaded"]) != 0:
self.run_button.setEnabled(True)

Expand All @@ -85,13 +94,33 @@ def mouseMoveEvent(self, event):
def mousePressEvent(self, event):
if event.buttons() == Qt.LeftButton:
if event.x() <= 64 and event.y() <= 64:
wb_open("https://discord.gg/frWGTqhFbn", new=0, autoraise=True)
wb_open("https://discord.gg/frWGTqhFbn")

def resizeEvent(self, event):
self.run_button.move(self.width() - self.run_button.width(), 0)
self.run_minecraft_info.move(self.width() - self.run_minecraft_info.width(), 30)
self.username_selector.move(self.width() - self.run_button.width() - self.username_selector.width(), 0)

def start_runner(self):
self.run_button.setEnabled(False)
info = loads(get("https://mpdb.xyz/api/modpack.php?name=" + self.modpack_selector.currentText()).text)
funcs.start_run_worker(info["mp_version"], info["mp_forge"], info["mp_name"], self.username_selector.text(),
self.ram_selector.currentText(), self)

def start_run_worker(self, mc_version, forge_version, mp_name):
self.run = RunThread(mc_version=mc_version, forge_version=forge_version, mp_name=mp_name)
self.run.start()
self.run.any_signal.connect(self.stop_run_worker)


def stop_run_worker(self):
self.run_button.setEnabled(True)
self.run.stop()

def changeValue(self, value):
progress_bar.init(self)
self.progress_bar.setValue(value)




Expand All @@ -100,6 +129,44 @@ def resizeEvent(self, event):
print(e)


class RunThread(QtCore.QThread):
any_signal = QtCore.pyqtSignal()

def __init__(self, parent=None, mc_version="", forge_version="", mp_name=""):
super(RunThread, self).__init__(parent)
self.mc_version = mc_version
self.forge_version = forge_version
self.mp_name = mp_name


def run(self):
mpdb = MPDB()
print(f"Running {self.forge_version} for {self.mc_version}")
link = f"https://mpdb.xyz/forge/{self.mc_version.replace('.','-')}/{self.forge_version}.zip"
file_name = f"{self.forge_version}.zip"
with open(file_name, "wb") as f:
print(f"Downloading {file_name}")
r = get(link, stream=True)
total_length = r.headers.get("content-length")
if total_length is None:
print(r.content)
else:
dl = 0
total_length = int(total_length)
for data in r.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(100 * dl / total_length)
mpdb.changeValue(done)
print("\r" + str(done))

self.any_signal.emit()

def stop(self):
print("Stopping")
self.wait()


if __name__ == "__main__":
app = QApplication(argv)
mpdb = MPDB()
Expand Down
Loading

0 comments on commit 4973dfc

Please sign in to comment.