Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually use the cache instead of just storing it thousands of times #1869

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions OpenDreamClient/Resources/DreamResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ internal sealed class DreamResourceManager : IDreamResourceManager {

public void Initialize() {
_sawmill = Logger.GetSawmill("opendream.res");
InitCacheDirectory();

_netManager.RegisterNetMessage<MsgBrowseResource>(RxBrowseResource);
_netManager.RegisterNetMessage<MsgBrowseResourceResponse>(RxBrowseResourceResponse);
Expand All @@ -55,20 +54,22 @@ public void Shutdown() {
_resourceManager.UserData.Delete(_cacheDirectory);
}

private void InitCacheDirectory() {
var random = new Random();
while (true) {
_cacheDirectory = new ResPath($"/OpenDream/Cache/{random.Next()}");
if (!_resourceManager.UserData.Exists(_cacheDirectory))
break;
}
private void EnsureCacheDirectory() {
if(_cacheDirectory != default)
return;
if(_netManager.ServerChannel is null)
throw new Exception("Server doesn't appear to be connected, can't use cache right now!");
_cacheDirectory = new ResPath($"/OpenDream/Cache/{_netManager.ServerChannel.RemoteEndPoint}");
Fixed Show fixed Hide fixed
_resourceManager.UserData.CreateDir(_cacheDirectory);
if (!_resourceManager.UserData.Exists(_cacheDirectory))
throw new Exception($"Could not create cache directory at {_cacheDirectory}");

_sawmill.Debug($"Cache directory is {_cacheDirectory}");
_resourceManager.UserData.CreateDir(_cacheDirectory);
}

private void RxBrowseResource(MsgBrowseResource message) {
_sawmill.Debug($"Received cache check for {message.Filename}");
EnsureCacheDirectory();
if(_resourceManager.UserData.Exists(GetCacheFilePath(message.Filename))){ //TODO CHECK HASH
_sawmill.Debug($"Cache hit for {message.Filename}");
} else {
Expand All @@ -83,6 +84,7 @@ private void RxBrowseResource(MsgBrowseResource message) {
private void RxBrowseResourceResponse(MsgBrowseResourceResponse message) {
if(_activeBrowseRscRequests.Contains(message.Filename)) {
_activeBrowseRscRequests.Remove(message.Filename);
EnsureCacheDirectory();
CreateCacheFile(message.Filename, message.Data);
} else {
_sawmill.Error($"Recieved a browse_rsc response for a file we didn't ask for: {message.Filename}");
Expand Down
Loading