Skip to content

Commit

Permalink
arrange code and -bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Feb 13, 2020
1 parent d3a6c3b commit 38d3ca1
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 188 deletions.
11 changes: 7 additions & 4 deletions PCRC.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import traceback

import utils
from Logger import Logger
from Recorder import Recorder, Config
from logger import Logger
from recorder import Recorder
from config import Config
from pycraft.compat import input
from pycraft.exceptions import YggdrasilError

recorder = None
logger = Logger(name='PCRC', file_name=utils.LoggingFileName)
logger = Logger(name='PCRC')
ConfigFile = 'config.json'
TranslationFolder = 'lang/'

Expand Down Expand Up @@ -44,7 +45,7 @@ def isWorking():
def stop():
global recorder, logger
if isWorking():
recorder.stop()
recorder.stop(by_user=True)
while not recorder.is_stopped():
time.sleep(0.1)
logger.log('Recorder stopped')
Expand Down Expand Up @@ -124,6 +125,8 @@ def main():
except Exception:
logger.error(traceback.format_exc())

logger.log('Exited')


if __name__ == "__main__":
on_start_up()
Expand Down
37 changes: 0 additions & 37 deletions ReplayFile.py

This file was deleted.

1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"file_buffer_size_mb": 8,
"time_recorded_limit_hour": 12,
"delay_before_afk_second": 15,
"record_packets_when_afk": true,
"upload_file": false,
"auto_relogin": true,
"chat_spam_protect": true,
Expand Down
132 changes: 132 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import json


DefaultOption = json.loads('''
{
"__1__": "-------- Base --------",
"language": "en_us",
"debug_mode": false,
"__2__": "-------- Account and Server --------",
"online_mode": false,
"username": "bot_PCRC",
"password": "secret",
"address": "localhost",
"port": 20000,
"server_name": "SECRET SERVER",
"__3__": "-------- PCRC Control --------",
"file_size_limit_mb": 512,
"file_buffer_size_mb": 8,
"time_recorded_limit_hour": 12,
"delay_before_afk_second": 15,
"record_packets_when_afk": true,
"upload_file": false,
"auto_relogin": true,
"chat_spam_protect": true,
"__4__": "-------- PCRC Features --------",
"minimal_packets": true,
"daytime": 4000,
"weather": false,
"with_player_only": true,
"remove_items": false,
"remove_bats": true,
"remove_phantoms": true
}
''')

SettableOptions = [
'language',
'server_name',
'minimal_packets',
'daytime',
'weather',
'with_player_only',
'remove_items',
'remove_bats'
]


class Config:
def __init__(self, file_name):
self.file_name = file_name
try:
with open(file_name) as f:
self.data = json.load(f)
except FileNotFoundError:
self.data = {}
self.fill_missing_options()
self.write_to_file()

def fill_missing_options(self):
new_data = {}
for key in DefaultOption.keys():
new_data[key] = self.data.get(key, DefaultOption[key])
self.data = new_data

def get_option_type(self, option):
return type(self.data[option])

def convert_to_option_type(self, option, value):
t = self.get_option_type(option)
if t == bool:
value = value in ['True', 'true', 'TRUE', True] or (type(value) is int and value != 0)
else:
value = t(value)
return value

def set_value(self, option, value, forced=False):
if not forced:
value = self.convert_to_option_type(option, value)
self.data[option] = value
return

def write_to_file(self, file_name=None):
if file_name is None:
file_name = self.file_name
text = json.dumps(self.data, indent=4)
for key in self.data.keys():
if len(key) == 5 and key[0] == key[1] == key[3] == key[4] == '_' and key != '__1__':
p = text.find(' "{}"'.format(key))
text = text[:p] + '\n' + text[p:]
with open(file_name, 'w') as f:
f.write(text)

def get(self, option):
if option in self.data:
return self.data[option]
else:
return None

def display(self):
def secret(text):
return '******' if len(text) <= 4 else '{}***{}'.format(text[0:2], text[-1])
messages = []
messages.append('================ Config ================')
messages.append('-------- Base --------')
messages.append(f"Language = {self.get('language')}")
messages.append(f"Debug mode = {self.get('debug_mode')}")
messages.append('-------- Account and Server --------')
messages.append(f"Online mode = {self.get('online_mode')}")
messages.append(f"User name = {secret(self.get('username'))}")
messages.append(f"Password = ******")
messages.append(f"Server address = {self.get('address')}")
messages.append(f"Server port = {self.get('port')}")
messages.append(f"Server name = {self.get('server_name')}")
messages.append('-------- PCRC Control --------')
messages.append(f"File size limit = {self.get('file_size_limit_mb')}MB")
messages.append(f"File buffer size = {self.get('file_buffer_size_mb')}MB")
messages.append(f"Time recorded limit = {self.get('time_recorded_limit_hour')}h")
messages.append(f"Upload file to transfer.sh = {self.get('upload_file')}")
messages.append(f"Auto relogin = {self.get('auto_relogin')}")
messages.append(f"Chat spam protect = {self.get('chat_spam_protect')}")
messages.append('-------- PCRC Features --------')
messages.append(f"Minimal packets mode = {self.get('minimal_packets')}")
messages.append(f"Daytime set to = {self.get('daytime')}")
messages.append(f"Weather switch = {self.get('weather')}")
messages.append(f"Record with player only = {self.get('with_player_only')}")
messages.append(f"Remove items = {self.get('remove_items')}")
messages.append(f"Remove bats = {self.get('remove_bats')}")
messages.append('========================================')
return '\n'.join(messages)
8 changes: 7 additions & 1 deletion Logger.py → logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@


class Logger:
def __init__(self, name=None, thread=None, file_name=None, display_debug=False):
DefaultFileName = 'PCRC.log'

def __init__(self, name=None, thread=None, file_name=DefaultFileName, display_debug=False):
self.name = name
self.thread = thread
self.file_name = file_name
self.display_debug = display_debug

@staticmethod
def set_default_file_name(fn):
Logger.DefaultFileName = fn

def _log(self, msg, log_type, do_print):
if not isinstance(msg, str):
msg = str(msg)
Expand Down
2 changes: 1 addition & 1 deletion pycraft/networking/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from future.utils import raise_

import Logger
import logger
from .types import VarInt
from .packets import clientbound, serverbound
from . import packets
Expand Down
6 changes: 4 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The config file is `config.json`. All settings can be changed in it. Those which

`debug_mode`: Whether outputs debug info or not

## Account and Server
### Account and Server

`online_mode`: Use online mode to login or offline mode instead

Expand All @@ -69,7 +69,7 @@ The config file is `config.json`. All settings can be changed in it. Those which

`server_name`: The server name showed in replay viewer

## PCRC Control
### PCRC Control

`file_size_limit_mb`: The limit of size of the `.tmcpr` file. Every time it is reached, PCRC will restart. Default: `512`

Expand All @@ -79,6 +79,8 @@ The config file is `config.json`. All settings can be changed in it. Those which

`delay_before_afk_second`: The time delay between every player leaving and PCRC pausing recording. Default: `15`

`record_packets_when_afk`: If set to false, PCRC will ignore almost every incoming packets when PCRC pauses recording

`upload_file`: If set to true, .mcpr file will be sent to [transfer.sh](transfer.sh) after finishing recording

`auto_relogin`: If this option is enabled and the client gets disconnected, it will automatically try to reconnect
Expand Down
2 changes: 2 additions & 0 deletions readme_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ PCRC 目前支持连接 `1.12`、`1.12.2` 以及 `1.14.4` 的原版 Minecraft

`delay_before_afk_second`: 所有人都离开与暂停录制间的延迟,单位: 秒。默认值: `15`

`record_packets_when_afk`: 若设为 `false`,PCRC 将会在暂停录制时忽略几乎所有到来的数据包

`upload_file`: 是否将录制好的文件上传至 [transfer.sh](transfer.sh) 以便进行分享~~(国内用户还是关掉吧不然上传十年)~~

`auto_relogin`: 当客户端掉线时是否自动重连。若为 `true`,PCRC 会在掉线后尝试重连
Expand Down
Loading

0 comments on commit 38d3ca1

Please sign in to comment.