diff --git a/OpenDreamClient/Resources/DreamResourceManager.cs b/OpenDreamClient/Resources/DreamResourceManager.cs index a7837fc4e9..c040923972 100644 --- a/OpenDreamClient/Resources/DreamResourceManager.cs +++ b/OpenDreamClient/Resources/DreamResourceManager.cs @@ -42,7 +42,6 @@ internal sealed class DreamResourceManager : IDreamResourceManager { public void Initialize() { _sawmill = Logger.GetSawmill("opendream.res"); - InitCacheDirectory(); _netManager.RegisterNetMessage(RxBrowseResource); _netManager.RegisterNetMessage(RxBrowseResourceResponse); @@ -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 { @@ -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}");