Skip to content

Commit

Permalink
2024-08-17T0156Z
Browse files Browse the repository at this point in the history
  • Loading branch information
Windows81 committed Aug 16, 2024
1 parent b5c9a83 commit df74b06
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 65 deletions.
26 changes: 9 additions & 17 deletions GameConfig.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ icon_path = 'c:\Users\USERNAME\Pictures\Byfron Icon.png'
startup_script = 'game.workspace.FilteringEnabled = false'

[game_setup.place_file]
rbxl_uri = 'c:\Users\USERNAME\Documents\Roblox Files\Roblox Hacked Place Database 2 - MrManchot\Building game.rbxl'
rbxl_uri = 'c:\Users\USERNAME\Documents\Roblox Files\VideoTest.rbxl'

enable_saveplace = false

Expand All @@ -29,7 +29,7 @@ clear_on_start = false

[game_setup.asset_cache]
dir_path = './AssetCache'
clear_on_start = true
clear_on_start = false

[server_core]
retrieve_default_user_code = '''
Expand All @@ -39,16 +39,11 @@ end
'''

check_user_allowed = '''
def f(user_id_num, user_code):
return True
function(user_id_num, user_code) -- string -> bool
return true
end
'''

#check_user_allowed = '''
#function(user_id_num, user_code) -- string -> bool
# return true
#end
#'''

check_user_has_admin = '''
function(user_id_num, user_code) -- string -> bool
return true
Expand All @@ -57,7 +52,7 @@ end

retrieve_avatar_type = '''
function(user_id_num, user_code) -- str -> str
return 'R6'
return 'R15'
end
'''

Expand Down Expand Up @@ -173,17 +168,14 @@ id_num = 163230957
name = 'DoubleCash'
price = 1

[[remote_data.asset_redirects]]
id_val = 13
[remote_data.asset_redirects.13]
uri = 'https://archive.org/download/youtube-f3AD_dk7MgQ/f3AD_dk7MgQ.webm'
#cmd_line = '''sh -c 'ffmpeg -i $(yt-dlp -g -f b "ykS9ooPccq4") -c:v libvpx -b:v 3000k -speed 16 -c:a libvorbis -b:a 128k -ar 44100 -f webm -''''

[[remote_data.asset_redirects]]
id_val = 14
[remote_data.asset_redirects.14]
uri = 'https://archive.org/download/youtube-WmNfDXTnKMw/WmNfDXTnKMw.webm'

[[remote_data.asset_redirects]]
id_val = 15
[remote_data.asset_redirects.15]
cmd_line = 'curl https://archive.org/download/youtube-WmNfDXTnKMw/WmNfDXTnKMw.webm -L --output -'

[server_assignment]
Expand Down
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,34 +607,48 @@ price = 100

#### `remote_data.asset_redirects`

Resolves to a data dictionary.
Resolves to [function](#functions) type `(int | str) -> asset_redirect`.

When an RFD server receives a request to load an asset by id, it does so from Roblox.com by default.

However, entries in [`asset_redirects`](#remote_dataasset_redirects) override that default and
However, entries in [`asset_redirects`](#remote_dataasset_redirects) override that default.

Through the `uri` field, assets can load either from a local or remote resource.

The following examples notate the structure into the [dict mode](#dict-mode) syntax:

```toml
[[remote_data.asset_redirects]]
id_val = 13
[remote_data.asset_redirects.13] # asset id 13
uri = 'c:\Users\USERNAME\Pictures\Image.jpg'
```

You can include a `cmd_line` field if you want the loaded asset to literally come from the `stdout` of a program.

```toml
[[remote_data.asset_redirects]]
id_val = 14
[remote_data.asset_redirects.14] # asset id 14
uri = 'https://archive.org/download/youtube-WmNfDXTnKMw/WmNfDXTnKMw.webm'
```

You can include a `cmd_line` field if you want the loaded asset to literally come from the `stdout` of a program.

```toml
[[remote_data.asset_redirects]]
id_val = 15
[remote_data.asset_redirects.15] # asset id 15
cmd_line = 'curl https://archive.org/download/youtube-WmNfDXTnKMw/WmNfDXTnKMw.webm -L --output -'
```

This should also work. It redirects asset iden strings starting wtih `time_music_` to static files on the internet.

```toml
remote_data.asset_redirects = "python"
remote_data.asset_redirects = '''
def f(asset_iden):
PREFIX = "time_music_"
if asset_iden.startswith(PREFIX):
h = int(asset_iden[len(PREFIX):])
return {
"uri": "https://github.com/Windows81/Time-Is-Musical/blob/main/hour_%02d.wav" % (h % 24)
}
return None
```
#### `remote_data.badges`
Resolves to a data dictionary.
Expand Down
54 changes: 33 additions & 21 deletions Source/assets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from genericpath import isfile
from . import extract, returns, material, serialisers, queue
from config.types import structs, wrappers
from config.types import structs, wrappers, callable
import util.const
import functools
import shutil
Expand All @@ -10,11 +11,12 @@ class asseter:
def __init__(
self,
dir_path: str,
redirects: structs.asset_redirects,
redirect_func: callable.obj_type[[int | str], structs.asset_redirect | None],
clear_on_start: bool,
) -> None:
self.dir_path = dir_path
self.redirects = redirects
self.redirect_func = redirect_func
self.redirect_iden_flags = set[int | str]()
self.queuer = queue.queuer()

if os.path.isdir(dir_path):
Expand All @@ -24,13 +26,6 @@ def __init__(
else:
os.makedirs(dir_path)

if not clear_on_start:
# Deletes cache from assets which should redirect so that the config file remains correct.
for redir_id in redirects:
path = self.get_asset_path(redir_id)
if os.path.isfile(path):
os.remove(path)

@functools.cache
def get_asset_path(self, asset_id: int | str) -> str:
return os.path.normpath(
Expand Down Expand Up @@ -94,9 +89,9 @@ def resolve_asset_query(self, query: dict[str, str]) -> int | str:
raise Exception('Unable to extract asset id from URL query.')

def add_asset(self, asset_id: int | str, data: bytes) -> None:
redirect = self.redirects.get(asset_id)
if redirect is not None:
raise Exception('File does not exist.')
redirect_info = self.redirect_func(asset_id)
if redirect_info is not None:
raise Exception('Asset already has a redirect per config file.')
path = self.get_asset_path(asset_id)
self._save_file(path, data)

Expand All @@ -119,7 +114,21 @@ def _load_asset_str(self, asset_id: str) -> bytes | None:
return material.load_asset(asset_id)
return None

def _load_redir_asset(self, redirect: structs.asset_redirect) -> returns.base_type:
def _load_redir_asset(self, asset_id: int | str, redirect: structs.asset_redirect) -> returns.base_type:
asset_path = self.get_asset_path(asset_id)

# Checks if it's the first time for a redirect to be called.
# If it is, remove any file it might point to from the cache.
# This is done dynamically to keep redirects compliant with config.
if asset_id not in self.redirect_iden_flags:
if os.path.isfile(asset_path):
os.remove(asset_path)
self.redirect_iden_flags.add(asset_id)
else:
local_data = self._load_file(asset_path)
if local_data is not None:
return returns.construct(data=local_data)

if redirect.uri is not None:
if redirect.uri.is_online:
return returns.construct(
Expand All @@ -142,9 +151,15 @@ def _load_redir_asset(self, redirect: structs.asset_redirect) -> returns.base_ty
return returns.construct()

def _load_asset(self, asset_id: int | str) -> returns.base_type:
redirect = self.redirects.get(asset_id)
if redirect is not None:
return self._load_redir_asset(redirect)
redirect_info = self.redirect_func(asset_id)
if redirect_info is not None:
return self._load_redir_asset(asset_id, redirect_info)

asset_path = self.get_asset_path(asset_id)
local_data = self._load_file(asset_path)
if local_data is not None:
return returns.construct(data=local_data)

if isinstance(asset_id, str):
return returns.construct(data=self._load_asset_str(asset_id))
elif isinstance(asset_id, int):
Expand All @@ -155,11 +170,8 @@ def get_asset(self, asset_id: int | str, bypass_blacklist: bool = False) -> retu
return returns.construct()

asset_path = self.get_asset_path(asset_id)
local_data = self._load_file(asset_path)
if local_data:
return returns.construct(data=local_data)

result_data = self._load_asset(asset_id)

if isinstance(result_data, returns.ret_data):
self._save_file(asset_path, result_data.data)
return result_data
2 changes: 1 addition & 1 deletion Source/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, path: str = util.resource.DEFAULT_CONFIG_PATH) -> None:

self.asset_cache = assets.asseter(
dir_path=self.game_setup.asset_cache.dir_path,
redirects=self.remote_data.asset_redirects,
redirect_func=self.remote_data.asset_redirects,
clear_on_start=self.game_setup.asset_cache.clear_on_start,
)

Expand Down
3 changes: 2 additions & 1 deletion Source/config/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ class server_core(allocateable.obj_type):

class remote_data(allocateable.obj_type):
gamepasses: structs.gamepasses = [] # type: ignore
asset_redirects: structs.asset_redirects = [] # type: ignore
asset_redirects: callable[[int | str], structs.asset_redirect | None] = \
'function() return nil end' # type: ignore
badges: structs.badges = [] # type: ignore
2 changes: 1 addition & 1 deletion Source/config/types/callable.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def call(*args):
)
return next(
v
for v in local_vars.values()
for v in reversed(local_vars.values())
if callable(v)
)
case call_mode_enum.dicted:
Expand Down
7 changes: 1 addition & 6 deletions Source/config/types/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ def __post_init__(self):
]) > 1:
raise Exception(
'Entries for `asset_redirects` should not have '
'both a URI and a pipeable command-line.'
'both a `uri` and a pipeable `cmd_line`.'
)
id_val: int | str
uri: wrappers.uri_obj | None = None
cmd_line: str | None = None

Expand All @@ -42,10 +41,6 @@ class badges(wrappers.dicter[badge, int]):
key_name = 'id_num'


class asset_redirects(wrappers.dicter[asset_redirect, int | str]):
key_name = 'id_val'


@dataclasses.dataclass
class avatar_colors:
head: int
Expand Down
16 changes: 8 additions & 8 deletions Source/util/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,16 @@ def get_path(*paths: str) -> str:
class ssl_mutable:
CONTEXT_COUNTS = {}

def prepare_file_path(self, prefix: str, ext: str) -> str:
count_key = f'{prefix}.{ext}'
count = ssl_mutable.CONTEXT_COUNTS.setdefault(count_key, 0)
def prepare_file_path(self, name_prefix: str, ext: str) -> str:
count_key = f'{name_prefix}.{ext}'
context_count = ssl_mutable.CONTEXT_COUNTS.setdefault(count_key, 0)
ssl_mutable.CONTEXT_COUNTS[count_key] += 1
suffix = f'{count:03d}'
name_sufix = f'{context_count:03d}'

path = get_path(f'{prefix}{suffix}.{ext}')
if os.path.isfile(path):
os.remove(path)
return path
ssl_file_path = get_path(f'{name_prefix}{name_sufix}.{ext}')
if os.path.isfile(ssl_file_path):
os.remove(ssl_file_path)
return ssl_file_path

def __init__(self) -> None:
self.ca = trustme.CA()
Expand Down

0 comments on commit df74b06

Please sign in to comment.