Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions OpenDreamClient/OpenDreamClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ProjectReference Include="..\RobustToolbox\Robust.Shared.Maths\Robust.Shared.Maths.csproj" />
<ProjectReference Include="..\RobustToolbox\Robust.Shared\Robust.Shared.csproj" />
<ProjectReference Include="..\RobustToolbox\Robust.Client\Robust.Client.csproj" />
<PackageReference Include="SpaceWizards.Sodium" />
</ItemGroup>
<Import Project="..\RobustToolbox\MSBuild\Robust.Properties.targets" />
<Import Project="..\RobustToolbox\MSBuild\XamlIL.targets" />
Expand Down
23 changes: 18 additions & 5 deletions OpenDreamClient/Resources/DreamResourceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Robust.Shared.ContentPack;
using Robust.Shared.Network;
using Robust.Shared.Utility;
using System.Linq;
using SpaceWizards.Sodium;

namespace OpenDreamClient.Resources;

Expand Down Expand Up @@ -69,21 +71,32 @@ private void EnsureCacheDirectory() {
}

private void RxBrowseResource(MsgBrowseResource message) {
_sawmill.Debug($"Received cache check for {message.Filename}");
_sawmill.Debug($"Received cache check for {message.Filename} hash: {BitConverter.ToString(message.DataHash)}");
EnsureCacheDirectory();
if(_resourceManager.UserData.Exists(GetCacheFilePath(message.Filename))){ //TODO CHECK HASH
if(_resourceManager.UserData.Exists(GetCacheFilePath(message.Filename)) && GetFileHash(GetCacheFilePath(message.Filename)).SequenceEqual(message.DataHash)){
_sawmill.Debug($"Cache hit for {message.Filename}");
} else {
if(_activeBrowseRscRequests.Contains(message.Filename)) //we've already requested it, don't need to do it again
return;
_sawmill.Debug($"Cache miss for {message.Filename}, requesting from server.");
if (_resourceManager.UserData.Exists(GetCacheFilePath(message.Filename))) {
_sawmill.Debug($"Cache hit for {message.Filename} but hashes did not match (hash: {BitConverter.ToString(GetFileHash(GetCacheFilePath(message.Filename)))}). Re-requesting!");
_resourceManager.UserData.Delete(GetCacheFilePath(message.Filename));
} else
_sawmill.Debug($"Cache miss for {message.Filename}, requesting from server.");
_activeBrowseRscRequests.Add(message.Filename);
_netManager.ServerChannel?.SendMessage(new MsgBrowseResourceRequest(){ Filename = message.Filename});
}
}

private byte[] GetFileHash(ResPath path) {
var stream = _resourceManager.UserData.OpenRead(path);
Span<byte> filebytes = new(new byte[stream.Length]);
stream.ReadToEnd(filebytes);
return CryptoGenericHashBlake2B.Hash(32, filebytes, ReadOnlySpan<byte>.Empty);
}

private void RxBrowseResourceResponse(MsgBrowseResourceResponse message) {
if(_activeBrowseRscRequests.Contains(message.Filename)) {
if (_activeBrowseRscRequests.Contains(message.Filename)) {
_activeBrowseRscRequests.Remove(message.Filename);
EnsureCacheDirectory();
CreateCacheFile(message.Filename, message.Data);
Expand Down Expand Up @@ -227,7 +240,7 @@ public bool EnsureCacheFile(string filename, int timeoutSeconds = 5) {

return _resourceManager.UserData.Exists(actualPath);
} else {
_sawmill.Error("Cache was ensured for a file that does not exist in cache and is not requested. Probably somebody called browse() without browse_rsc() first.");
_sawmill.Error($"Cache was ensured for a file ({filename}) that does not exist in cache and is not requested. Probably somebody called browse() without browse_rsc() first.");
return false;
}
}
Expand Down
5 changes: 3 additions & 2 deletions OpenDreamRuntime/DreamConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using OpenDreamShared.Network.Messages;
using Robust.Shared.Enums;
using Robust.Shared.Player;
using SpaceWizards.Sodium;

namespace OpenDreamRuntime;

Expand Down Expand Up @@ -373,7 +374,7 @@ public void BrowseResource(DreamResource resource, string filename) {

var msg = new MsgBrowseResource() {
Filename = filename,
DataHash = resource.ResourceData.Length //TODO: make a quick hash that can work clientside too
DataHash = CryptoGenericHashBlake2B.Hash(32, resource.ResourceData!, ReadOnlySpan<byte>.Empty)
};
_permittedBrowseRscFiles[filename] = resource;

Expand All @@ -384,7 +385,7 @@ public void HandleBrowseResourceRequest(string filename) {
if(_permittedBrowseRscFiles.TryGetValue(filename, out var dreamResource)) {
var msg = new MsgBrowseResourceResponse() {
Filename = filename,
Data = dreamResource.ResourceData! //honestly if this is null, something mega fucked up has happened and we should error hard
Data = dreamResource.ResourceData!, //honestly if this is null, something mega fucked up has happened and we should error hard
};
_permittedBrowseRscFiles.Remove(filename);
Session?.Channel.SendMessage(msg);
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/OpenDreamRuntime.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Byond.TopicSender" />
<PackageReference Include="Tracy-CSharp" />
<PackageReference Include="OpenDreamByondApi" />
<PackageReference Include="SpaceWizards.Sodium" />
</ItemGroup>

<Import Project="..\RobustToolbox\MSBuild\Robust.Properties.targets" />
Expand Down
6 changes: 3 additions & 3 deletions OpenDreamShared/Network/Messages/MsgBrowseResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public sealed class MsgBrowseResource : NetMessage {
public override MsgGroups MsgGroup => MsgGroups.EntityEvent;

public string Filename = string.Empty;
public int DataHash;
public byte[] DataHash = [];

public override void ReadFromBuffer(NetIncomingMessage buffer, IRobustSerializer serializer) {
Filename = buffer.ReadString();
DataHash = buffer.ReadVariableInt32();
DataHash = buffer.ReadBytes(32);
}

public override void WriteToBuffer(NetOutgoingMessage buffer, IRobustSerializer serializer) {
buffer.Write(Filename);
buffer.WriteVariableInt32(DataHash);
buffer.Write(DataHash);
}
}
Loading