Skip to content

Commit

Permalink
Use WebResourceRequested to load resources (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
hbl917070 committed Dec 17, 2024
1 parent 00fcc0c commit 02f8fe7
Showing 1 changed file with 53 additions and 2 deletions.
55 changes: 53 additions & 2 deletions Tiefsee/WebWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,11 @@ public async Task Init() {
try {
var ext = Path.GetExtension(assetsFilePath).ToLower();
var headers = "Content-Type: " + WebServerController.GetMimeTypeMapping(ext);
var fs = File.OpenRead(assetsFilePath);

FileStream fs = File.OpenRead(assetsFilePath);
ManagedStream ms = new ManagedStream(fs);
args.Response = _wv2.CoreWebView2.Environment.CreateWebResourceResponse(
fs, 200, "OK", headers);
ms, 200, "OK", headers);
}
catch {
args.Response = _wv2.CoreWebView2.Environment.CreateWebResourceResponse(
Expand Down Expand Up @@ -771,6 +773,55 @@ public void WindowRoundedCorners(bool enable) {

}

class ManagedStream : Stream {
public ManagedStream(Stream s) {
s_ = s;
}

public override bool CanRead => s_.CanRead;

public override bool CanSeek => s_.CanSeek;

public override bool CanWrite => s_.CanWrite;

public override long Length => s_.Length;

public override long Position { get => s_.Position; set => s_.Position = value; }

public override void Flush() {
throw new NotImplementedException();
}

public override long Seek(long offset, SeekOrigin origin) {
return s_.Seek(offset, origin);
}

public override void SetLength(long value) {
throw new NotImplementedException();
}

public override int Read(byte[] buffer, int offset, int count) {
int read = 0;
try {
read = s_.Read(buffer, offset, count);
if (read == 0) {
s_.Dispose();
}
}
catch {
s_.Dispose();
throw;
}
return read;
}

public override void Write(byte[] buffer, int offset, int count) {
throw new NotImplementedException();
}

private Stream s_;
}

/// <summary>
/// 不會顯示出來的窗體
/// </summary>
Expand Down

0 comments on commit 02f8fe7

Please sign in to comment.