Use guid of game.ini for directory name
This commit is contained in:
+32
-6
@@ -1,13 +1,43 @@
|
||||
"""
|
||||
Implementation of C# System.Guid
|
||||
|
||||
References:
|
||||
https://referencesource.microsoft.com/#mscorlib/system/guid.cs
|
||||
|
||||
# TODO Add License
|
||||
"""
|
||||
|
||||
from struct import Struct, pack
|
||||
from math import log
|
||||
|
||||
# 16 bytes, 11 arguments
|
||||
# Constants for masking bytes
|
||||
MAXINT64 = 0xFFFFFFFFFFFFFFFF
|
||||
MAXINT16 = 0xFFFF
|
||||
MAXINT8 = 0xFF
|
||||
|
||||
|
||||
class Guid:
|
||||
"""
|
||||
A Global Unique Identifier (Guid) based on the one provided in C#'s System package
|
||||
|
||||
The Guid is stored as a tuple of length 11 for easy processing and comparisons
|
||||
|
||||
Attributes:
|
||||
guid (tuple): A tuple containing the guid
|
||||
"""
|
||||
def __init__(self, *args):
|
||||
"""
|
||||
Initialize the Guid
|
||||
|
||||
Arguments:
|
||||
Union[int, str, bytes, short(int will be masked to short)]:
|
||||
The arguments can be in any of the following formats:
|
||||
(bytes(len 16))
|
||||
(int(len <= 4), short, short, bytes(len 8))
|
||||
(int(len <= 8), int(len <= 8))
|
||||
(int(len <= 16))
|
||||
(str) - str must be formatted 00000000-0000-0000-0000-000000000000
|
||||
"""
|
||||
# Guid will be stored as a tuple len 11
|
||||
if bool(args):
|
||||
self.get_guid(args)
|
||||
@@ -24,13 +54,9 @@ class Guid:
|
||||
*b[8:16]
|
||||
)
|
||||
self.guid = s.unpack(guid)
|
||||
elif len(args) == 11 and all(isinstance(arg, int) for arg in args[0:3]) \
|
||||
and all(isinstance(arg, bytes) for arg in args[3:]) and all(len(arg) == 1 for arg in args[3:]):
|
||||
guid = s.pack(*args)
|
||||
self.guid = s.unpack(guid)
|
||||
elif len(args) == 4 and all(isinstance(arg, int) for arg in args[0:3]) \
|
||||
and isinstance(args[3], bytes) and len(args[3]) == 8:
|
||||
guid = s.pack(args[0], args[1], args[2], *args[3])
|
||||
guid = s.pack(args[0], args[1] & MAXINT16, args[2] & MAXINT16, *args[3])
|
||||
self.guid = s.unpack(guid)
|
||||
elif len(args) == 2 and all(isinstance(arg, int) for arg in args):
|
||||
if all((self.bytes_needed(arg) <= 4) for arg in args):
|
||||
|
||||
@@ -4,6 +4,7 @@ import json
|
||||
from configparser import ConfigParser
|
||||
from .guid import Guid
|
||||
from shutil import rmtree
|
||||
from hashlib import md5
|
||||
|
||||
config_dir = 'config/'
|
||||
bot_config_file = 'bot_config.json'
|
||||
@@ -47,7 +48,9 @@ def rename_section(cfg, sec, sec_new):
|
||||
|
||||
def get_server_guid(server_file):
|
||||
server_file = server_file.encode()
|
||||
|
||||
m = md5(server_file).hexdigest().encode()
|
||||
guid = Guid(int(m.hex()))
|
||||
return guid
|
||||
|
||||
|
||||
def process_file(in_file, file_type) -> ConfigParser:
|
||||
@@ -87,9 +90,10 @@ def process_file(in_file, file_type) -> ConfigParser:
|
||||
return config
|
||||
|
||||
|
||||
def process_files(z) -> (ConfigParser, ConfigParser, list):
|
||||
def process_files(z) -> (ConfigParser, ConfigParser, list, Guid):
|
||||
dino_data = dict()
|
||||
game_config = ConfigParser()
|
||||
server_guid = Guid()
|
||||
mods = list()
|
||||
path = 'submissions_temp/tmp/'
|
||||
z.extractall(path=path)
|
||||
@@ -144,7 +148,7 @@ def process_files(z) -> (ConfigParser, ConfigParser, list):
|
||||
rmtree('submissions_temp/tmp')
|
||||
if not mods:
|
||||
mods = check_for_modded_dinos(dino_data, mods)
|
||||
return game_config, dino_data, mods
|
||||
return game_config, dino_data, mods, server_guid
|
||||
|
||||
|
||||
def generate_game_ini(game_config, mods, directory):
|
||||
@@ -164,12 +168,12 @@ def generate_dino_files(dino_data, directory):
|
||||
dino.write(f, space_around_delimiters=False)
|
||||
|
||||
|
||||
def generate_files(storage_dir, ctx, filename, game_ini, dinos_data, mods):
|
||||
def generate_files(storage_dir, ctx, dirname, game_ini, dinos_data, mods):
|
||||
if not os.path.isdir(f'{storage_dir}/{ctx.author.id}'):
|
||||
os.mkdir(f'{storage_dir}/{ctx.author.id}')
|
||||
directory = f'{storage_dir}/{ctx.author.id}/{filename.replace(".zip", "")}_' \
|
||||
f'{ctx.message.created_at.strftime("%Y%m%dT%H%M%S")}'
|
||||
os.mkdir(directory)
|
||||
directory = f'{storage_dir}/{ctx.author.id}/{dirname}'
|
||||
if not os.path.isdir(directory):
|
||||
os.mkdir(directory)
|
||||
generate_game_ini(game_ini, mods, directory)
|
||||
generate_dino_files(dinos_data, directory)
|
||||
return 1
|
||||
|
||||
Reference in New Issue
Block a user