Created
July 15, 2019 18:35
-
-
Save NimbusFox/5c83b6dd090b4f094977d6bf28202070 to your computer and use it in GitHub Desktop.
An example of the web library I found
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static bool loaded = false; | |
private static bool renderReady = false; | |
private static Texture2D _texture; | |
private static Page page; | |
private static DeviceContext _context; | |
private static SpriteBatch spriteBatch; | |
[HarmonyPatch(typeof(StaxelGame))] | |
[HarmonyPatch("Draw")] | |
private class DrawOverride { | |
static void Prefix(StaxelGame __instance, GameTime gameTime) { | |
if (!loaded) { | |
_context = __instance.GetPrivateFieldValue<DeviceContext>("_context"); | |
spriteBatch = new SpriteBatch(_context.Graphics.GraphicsDevice); | |
var task = new Task(async () => { | |
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); | |
var browser = await Puppeteer.LaunchAsync(new LaunchOptions { | |
Headless = true, | |
Args = new string[] {"--no-sandbox"} | |
}); | |
page = await browser.NewPageAsync(); | |
await page.SetViewportAsync(new ViewPortOptions { | |
Width = _context.Graphics.PreferredBackBufferWidth, | |
Height = _context.Graphics.PreferredBackBufferHeight | |
}); | |
await page.GoToAsync("https://google.com"); | |
renderReady = true; | |
}); | |
task.Start(); | |
loaded = true; | |
} | |
} | |
static void Postfix(StaxelGame __instance, GameTime gameTime) { | |
UIManager.Draw(_context); | |
if (!renderReady) { | |
return; | |
} | |
if (_texture == null || _texture.IsDisposed) { | |
_texture = Texture2D.FromStream(_context.Graphics.GraphicsDevice, page.ScreenshotStreamAsync().Result); | |
} | |
spriteBatch.Begin(); | |
spriteBatch.Draw(_texture, Vector2.Zero, Color.White); | |
spriteBatch.End(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment