Config refactor part 3

main
Bram van den Heuvel 2023-10-01 16:20:22 +02:00
parent a1a7bf69ff
commit 94155b34a1
4 changed files with 56 additions and 53 deletions

View File

@ -1,48 +1,25 @@
import os """
This module prepares the necessary files for running the server in the
correct configuration.
"""
import config import config
def rewriter(file_name): def write_eula():
"""Create a decorator that rewrites a file based on given settings.""" """
Write whether the user accepts to Minecraft's EULA.
def exec(func): The server refuses to run unless explicitly accepted.
"""Rewrite a file""" """
new_lines = [] with open("eula.txt", 'w') as fp:
if config.EULA == True:
info_to_remember = {} fp.write("eula=true")
line_no = 0
with open(file_name, 'r', encoding='utf-8') as open_file:
for line in open_file:
line_no += 1
new_line = func(line, line_no, data=info_to_remember)
new_lines.append(new_line)
with open(file_name, 'w', encoding='utf-8') as write_file:
for line in new_lines:
write_file.write(line)
return exec
@rewriter('eula.txt')
def confirm_eula(line : str, line_no : int, data):
"""Confirm the Minecraft EULA"""
if not config.EULA:
return line
else: else:
return line.replace('eula=false', 'eula=true') fp.write("eula=false")
@rewriter('server.properties') def write_server_properties():
def fill_in_server_settings(line : str, line_no : int, data): """
"""Set up the server based on our chosen properties""" Write the configuration for the Minecraft world.
if line.strip().startswith('#'): """
return line with open("server.properties", 'w') as fp:
for key, value in config.at(['minecraft']).items():
key = line.split('=')[0] fp.write(f"{key}={value}\n")
server_settings = config.at(['minecraft']) or {}
try:
value = server_settings[key]
except IndexError:
return line
else:
return key + '=' + str(value) + '\n'

View File

@ -1,6 +1,9 @@
import os """
import yaml This module loads and parses the config.yaml file.
"""
from typing import Any, List, Optional from typing import Any, List, Optional
import yaml
with open('config.yaml', 'r') as open_file: with open('config.yaml', 'r') as open_file:
SETTINGS = yaml.load(open_file) SETTINGS = yaml.load(open_file)
@ -27,7 +30,7 @@ def at_value(keys : List[str], value : Any) -> Optional[Any]:
return at_value(tail, new_value) return at_value(tail, new_value)
# EULA # EULA
EULA = at(['minecraft', 'eula']) or False EULA = at(['config', 'eula']) or False
# Minecraft bridge credentials # Minecraft bridge credentials
MATRIX_HOMESERVER = at(['matrix', 'homeserver']) or "https://matrix.example.org/" MATRIX_HOMESERVER = at(['matrix', 'homeserver']) or "https://matrix.example.org/"
@ -37,6 +40,23 @@ MATRIX_PASSWORD = at(['matrix', 'password']) or "bridge_password"
# Matrix bridge room # Matrix bridge room
MATRIX_ROOM = at(['matrix', 'room_id']) or "!channel_id:example.org" MATRIX_ROOM = at(['matrix', 'room_id']) or "!channel_id:example.org"
SERVER_IP = os.getenv('SERVER_ADDRESS') or 'unknown ip' SERVER_IP = at(['matrix', 'server_address']) or 'unknown ip'
MATRIX_ADMINS = at(['matrix', 'mc-admins']) or [] MATRIX_ADMINS = at(['matrix', 'mc-admins']) or []
try:
RAM_SIZE = int(at(['config', 'ram']))
except TypeError:
RAM_SIZE = 1024
except ValueError:
RAM_SIZE = 1024
SERVER_JAR_LOCATION = at(['config', 'server_jar']) or 'server.jar'
RUN_COMMAND = [
'java',
f'-Xmx{RAM_SIZE}M',
f'-Xms{RAM_SIZE}M',
'-jar', SERVER_JAR_LOCATION,
'nogui'
]

View File

@ -7,6 +7,10 @@ config:
# To be downloaded at: https://www.minecraft.net/en-us/download/server # To be downloaded at: https://www.minecraft.net/en-us/download/server
server_jar: server.jar server_jar: server.jar
# Confirm the Minecraft EULA. https://account.mojang.com/documents/minecraft_eula
# Defaults to false, but is required to run the server.
# eula: true
# Matrix bridge configurations # Matrix bridge configurations
matrix: matrix:
# Homeserver URL # Homeserver URL
@ -45,8 +49,6 @@ matrix:
# Settings that directly affect running the Minecraft server. # Settings that directly affect running the Minecraft server.
minecraft: minecraft:
# Confirm the Minecraft EULA. Defaults to false.
# eula: true
# -------------------- # --------------------
# MINECRAFT SERVER # MINECRAFT SERVER

View File

@ -2,13 +2,17 @@ from subprocess import Popen, PIPE
from typing import Union from typing import Union
import asyncio import asyncio
import json import json
import sys
import re import re
from nbsr import NonBlockingStreamReader as NBSR from nbsr import NonBlockingStreamReader as NBSR
import config import config
import build_server as build
# Write the appropriate files
build.write_eula()
build.write_server_properties()
# run the shell as a subprocess: # run the shell as a subprocess:
p = Popen(sys.argv[1:], p = Popen(config.RUN_COMMAND,
stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False) stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False)
# wrap p.stdout with a NonBlockingStreamReader object: # wrap p.stdout with a NonBlockingStreamReader object:
nbsr = NBSR(p.stdout) nbsr = NBSR(p.stdout)