Skip to content

Instantly share code, notes, and snippets.

@NimbusFox
Created July 15, 2019 18:35
Show Gist options
  • Save NimbusFox/5c83b6dd090b4f094977d6bf28202070 to your computer and use it in GitHub Desktop.
Save NimbusFox/5c83b6dd090b4f094977d6bf28202070 to your computer and use it in GitHub Desktop.
An example of the web library I found
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