Giter Club home page Giter Club logo

supdolphinws.js's Introduction

supDolphinWS.js

A WebSocket client API library to access memory of emulated games in Dolphin.

This library is meant to be used with supDolphinWS-server

Usage

Installation (for Browser)

Add the following line to your HTML file

<script src="https://cdn.sup39.dev/js/[email protected]/index.min.js"></script>

API

async function main() {
  /**
   * Connect to supDolphinWS server
   * You can also specify the URL to the supDolphinWS server
   * The default URL is 'ws://localhost:35353'
   * Example:
   * > const dolphin = await Dolphin('ws://localhost:35354');
   */
  const dolphin = await Dolphin();
  // expose it as global variable for debug purpose
  window.dolphin = dolphin;

  // symbol map // TODO check version + load from csv
  const S = {
    gpMarioOriginal: 0x8040A378,
    gpMarDirector: 0x8040A2A8,
  };
  // expose it as global variable for debug purpose
  window.S = S;

  /**
   * Read memory
   * > readRAM(addr: number|number[], size: number): Promise<DataView>
   * The `addr` argument can be a number array to represent pointer+offsets
   * The value of the returned Promise is a DataView object of the content in RAM
   * The DataView will have size 0 if the address is not valid
   */
  // read the pointer to Mario's instance
  const dvMario = await dolphin.readRAM(S.gpMarioOriginal, 4);
  console.log('gpMarioOriginal:', dvMario.getUint32(0).toString(16).toUpperCase());
  // read Mario's position (x, y, z)
  const dvMPos = await dolphin.readRAM([S.gpMarioOriginal, 0x10], 12);
  if (dvMPos.byteLength > 0) {
    const x = dvMPos.getFloat32(0);
    const y = dvMPos.getFloat32(4);
    const z = dvMPos.getFloat32(8);
    console.log(`Mario's position: ${x} ${y} ${z}`);
  } else {
    console.log(`Cannot get Mario's position`);
  }

  /**
   * Write memory
   * > writeRAM(addr: number|number[], value: string|ArrayBufferLike): Promise<DataView>
   * The `addr` argument can be a number array to represent pointer+offsets
   * The `value` argument can be a hex string or an ArrayBufferLike (including Uint8Array etc.)
   * The value of the returned Promise is a DataView object of the written 32-bit address
   * The DataView will have size 0 if the address is not valid
   */
  // read current global QF
  const dvGlobalQF = await dolphin.readRAM([S.gpMarDirector, 0x5C], 4);
  // write current global QF to QFT freeze time
  await dolphin.writeRAM(0x817F00B8, dvGlobalQF.buffer);
  // set QFT freeze duration to 45 frame
  const dvFreezeDur = new DataView(new ArrayBuffer(4));
  dvFreezeDur.setUint32(0, 45); // setUint32(offset, value)
  await dolphin.writeRAM(0x817F00BC, dvFreezeDur.buffer);
  // You can also pass hex string as `value` argument
  // await dolphin.write(0x817F00BC, '0000002D');

  /**
   * Hook Dolphin
   * > hook()
   * If you restart Dolphin, you will need to call this function to reinitialize the shared memory of Dolphin
   * You can also simply just ask your user to refresh the page though
   */
  await dolphin.hook();
}
main();

supdolphinws.js's People

Contributors

sup39 avatar

Stargazers

Micah Russell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.