Config refactor part 3
parent
a1a7bf69ff
commit
94155b34a1
|
@ -1,48 +1,25 @@
|
|||
import os
|
||||
"""
|
||||
This module prepares the necessary files for running the server in the
|
||||
correct configuration.
|
||||
"""
|
||||
|
||||
import config
|
||||
|
||||
def rewriter(file_name):
|
||||
"""Create a decorator that rewrites a file based on given settings."""
|
||||
|
||||
def exec(func):
|
||||
"""Rewrite a file"""
|
||||
new_lines = []
|
||||
|
||||
info_to_remember = {}
|
||||
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
|
||||
def write_eula():
|
||||
"""
|
||||
Write whether the user accepts to Minecraft's EULA.
|
||||
The server refuses to run unless explicitly accepted.
|
||||
"""
|
||||
with open("eula.txt", 'w') as fp:
|
||||
if config.EULA == True:
|
||||
fp.write("eula=true")
|
||||
else:
|
||||
return line.replace('eula=false', 'eula=true')
|
||||
fp.write("eula=false")
|
||||
|
||||
@rewriter('server.properties')
|
||||
def fill_in_server_settings(line : str, line_no : int, data):
|
||||
"""Set up the server based on our chosen properties"""
|
||||
if line.strip().startswith('#'):
|
||||
return line
|
||||
|
||||
key = line.split('=')[0]
|
||||
|
||||
server_settings = config.at(['minecraft']) or {}
|
||||
|
||||
try:
|
||||
value = server_settings[key]
|
||||
except IndexError:
|
||||
return line
|
||||
else:
|
||||
return key + '=' + str(value) + '\n'
|
||||
def write_server_properties():
|
||||
"""
|
||||
Write the configuration for the Minecraft world.
|
||||
"""
|
||||
with open("server.properties", 'w') as fp:
|
||||
for key, value in config.at(['minecraft']).items():
|
||||
fp.write(f"{key}={value}\n")
|
||||
|
|
28
config.py
28
config.py
|
@ -1,6 +1,9 @@
|
|||
import os
|
||||
import yaml
|
||||
"""
|
||||
This module loads and parses the config.yaml file.
|
||||
"""
|
||||
|
||||
from typing import Any, List, Optional
|
||||
import yaml
|
||||
|
||||
with open('config.yaml', 'r') as 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)
|
||||
|
||||
# EULA
|
||||
EULA = at(['minecraft', 'eula']) or False
|
||||
EULA = at(['config', 'eula']) or False
|
||||
|
||||
# Minecraft bridge credentials
|
||||
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_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 []
|
||||
|
||||
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'
|
||||
]
|
||||
|
|
|
@ -7,6 +7,10 @@ config:
|
|||
# To be downloaded at: https://www.minecraft.net/en-us/download/server
|
||||
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:
|
||||
# Homeserver URL
|
||||
|
@ -45,8 +49,6 @@ matrix:
|
|||
|
||||
# Settings that directly affect running the Minecraft server.
|
||||
minecraft:
|
||||
# Confirm the Minecraft EULA. Defaults to false.
|
||||
# eula: true
|
||||
|
||||
# --------------------
|
||||
# MINECRAFT SERVER
|
||||
|
|
|
@ -2,13 +2,17 @@ from subprocess import Popen, PIPE
|
|||
from typing import Union
|
||||
import asyncio
|
||||
import json
|
||||
import sys
|
||||
import re
|
||||
from nbsr import NonBlockingStreamReader as NBSR
|
||||
import config
|
||||
import build_server as build
|
||||
|
||||
# Write the appropriate files
|
||||
build.write_eula()
|
||||
build.write_server_properties()
|
||||
|
||||
# run the shell as a subprocess:
|
||||
p = Popen(sys.argv[1:],
|
||||
p = Popen(config.RUN_COMMAND,
|
||||
stdin = PIPE, stdout = PIPE, stderr = PIPE, shell = False)
|
||||
# wrap p.stdout with a NonBlockingStreamReader object:
|
||||
nbsr = NBSR(p.stdout)
|
||||
|
|
Loading…
Reference in New Issue