Skip to content

Commit

Permalink
Actually use the cache instead of just storing it thousands of times (#…
Browse files Browse the repository at this point in the history
…1869)

Co-authored-by: amylizzle <[email protected]>
  • Loading branch information
amylizzle and amylizzle authored Jul 27, 2024
1 parent 2d88bcc commit d13705c
Showing 1 changed file with 11 additions and 9 deletions.
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}");
_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

0 comments on commit d13705c

Please sign in to comment.