-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transition server to builder architecture
- Loading branch information
Ableytner
committed
Mar 12, 2024
1 parent
4e55d72
commit 14ad342
Showing
13 changed files
with
243 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import re | ||
|
||
|
||
class McVersionType: | ||
UNKNOWN = 0 | ||
VANILLA = 1 | ||
SNAPSHOT = 2 | ||
FORGE = 3 | ||
PAPER = 4 | ||
SPIGOT = 5 | ||
BUKKIT = 6 | ||
|
||
@staticmethod | ||
def get_all() -> dict[str, int]: | ||
return { | ||
"UNKNOWN": 0, | ||
"VANILLA": 1, | ||
"SNAPSHOT": 2, | ||
"FORGE": 3, | ||
"PAPER": 4, | ||
"SPIGOT": 5, | ||
"BUKKIT": 6 | ||
} | ||
|
||
@staticmethod | ||
def get_name(type: int) -> str: | ||
for name, type_id in McVersionType.get_all().values(): | ||
if type_id == type: | ||
return name | ||
|
||
class McVersion: | ||
def __init__(self, name: str, type: int) -> None: | ||
if not isinstance(name, str): | ||
raise TypeError(f"Expected str, got {type(name)}") | ||
if not isinstance(type, int): | ||
raise TypeError(f"Expected int, got {type(type)}") | ||
|
||
if re.search(r"^1\.[1-2]{0,1}[0-9](\.[0-9]{1,2})?$", name) is None: | ||
raise ValueError(f"Expected version regex to match with {name}") | ||
|
||
self.name = name | ||
self.type = type | ||
|
||
split_name = [int(item) for item in name.split(".")] | ||
if len(split_name) == 2: | ||
self.id = split_name[0] * 10**4 + split_name[1] * 10**2 | ||
elif len(split_name) == 3: | ||
self.id = split_name[0] * 10**4 + split_name[1] * 10**2 + split_name[2] | ||
else: | ||
raise ValueError(f"Expected to version to have at most 3 subparts, but received {split_name}") | ||
|
||
name: str | ||
type: int | ||
id: int | ||
|
||
def __str__(self) -> str: | ||
if self.type == McVersionType.VANILLA: | ||
return f"{self.name}" | ||
else: | ||
return f"{McVersionType.get_name(self.type)} {self.name}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Export server classes""" | ||
|
||
from .server_builder import ServerBuilder | ||
from .base_server import BaseServer | ||
from .paper_server import PaperServer | ||
from .vanilla_server import VanillaServer | ||
|
||
__exports__ = [ | ||
ServerBuilder, | ||
BaseServer, | ||
PaperServer, | ||
VanillaServer | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from .base_server import BaseServer | ||
from ..mcversion import McVersion, McVersionType | ||
|
||
class PaperServer(BaseServer): | ||
VERSION_TYPE = McVersionType.PAPER | ||
|
||
def execute_command(self, command: str): | ||
if command.startswith("/"): | ||
command = command[1::] | ||
|
||
ret = super().execute_command(command) | ||
|
||
if command == "stop": | ||
self._stopping() | ||
|
||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import json | ||
import pathlib | ||
import os | ||
from zipfile import ZipFile | ||
|
||
from .base_server import BaseServer | ||
from .vanilla_server import VanillaServer | ||
from ..mcversion import McVersion, McVersionType | ||
from ..util import logger | ||
|
||
class ServerBuilder: | ||
"""Builder clas to create a new Server object""" | ||
|
||
__create_key = object() | ||
|
||
@classmethod | ||
def from_jar(cls, jar_file: str): | ||
if not isinstance(jar_file, str): | ||
raise TypeError(f"Expected str, got {type(jar_file)}") | ||
|
||
if not os.path.isfile(jar_file): | ||
raise FileNotFoundError(f"Jarfile {jar_file} not found") | ||
if not jar_file.endswith(".jar"): | ||
raise FileNotFoundError("Expected a .jar file") | ||
|
||
mcv = ServerBuilder._check_jar(jar_file) | ||
assert isinstance(mcv, McVersion) | ||
|
||
server_path = pathlib.Path(jar_file).parent.resolve() | ||
|
||
server = None | ||
match mcv.type: | ||
case McVersionType.VANILLA: | ||
server = VanillaServer(server_path, mcv) | ||
|
||
assert server is not None | ||
|
||
builder = ServerBuilder(cls.__create_key, server) | ||
return builder | ||
|
||
def build(self): | ||
assert isinstance(self._server, BaseServer) | ||
return self._server | ||
|
||
def __init__(self, create_key, server: BaseServer) -> None: | ||
if create_key != ServerBuilder.__create_key: | ||
raise NotImplementedError("Cannot instantiate builder class") | ||
|
||
self._server = server | ||
|
||
@staticmethod | ||
def _check_jar(jar_file: str) -> McVersion: | ||
mcv = None | ||
|
||
mcv = ServerBuilder._check_jar_fabric(jar_file) | ||
if mcv is not None: | ||
return mcv | ||
|
||
mcv = ServerBuilder._check_jar_vanilla(jar_file) | ||
if mcv is not None: | ||
return mcv | ||
|
||
raise ValueError(f"Minecraft version could not be identified from {jar_file}") | ||
|
||
@staticmethod | ||
def _check_jar_fabric(jar_file: str) -> McVersion | None: | ||
return None | ||
|
||
@staticmethod | ||
def _check_jar_vanilla(jar_file: str) -> McVersion | None: | ||
version_json = None | ||
with ZipFile(jar_file, "r") as zf: | ||
for zip_fileinfo in zf.filelist: | ||
if zip_fileinfo.filename == "version.json": | ||
version_json = json.loads(zf.read(zip_fileinfo)) | ||
if version_json is None: | ||
return None | ||
return McVersion(version_json["name"], McVersionType.VANILLA) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from .base_server import BaseServer | ||
from ..mcversion import McVersion, McVersionType | ||
|
||
class VanillaServer(BaseServer): | ||
VERSION_TYPE = McVersionType.VANILLA | ||
|
||
def execute_command(self, command: str): | ||
if not command.startswith("/"): | ||
command = "/" + command | ||
|
||
ret = super().execute_command(command) | ||
|
||
if command == "/stop": | ||
self._stopping() | ||
|
||
return ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"""Export util classes""" | ||
|
||
from . import info_getter, logger | ||
|
||
__exports__ = [ | ||
info_getter, | ||
logger | ||
] |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.