Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of checksum calculation in javascript #5

Closed
sdwvit opened this issue Apr 4, 2019 · 3 comments
Closed

Implementation of checksum calculation in javascript #5

sdwvit opened this issue Apr 4, 2019 · 3 comments

Comments

@sdwvit
Copy link

sdwvit commented Apr 4, 2019

const fs = require("fs");
const path = require("path");
const file = path.join(process.cwd(), "path_to_save.d2s");

function calculateSum(data) {
  let sum = 0;
  for (let i = 0; i < data.length; i++) {
    let ch = data[i];
    if (i >= 12 && i < 16) {
      ch = 0;
    }
    ch += sum < 0;
    sum = (sum << 1) + ch;
  }

  return sum;
}

function littleToBigEndian(number) {
  return new DataView(
    Int32Array.of(
      new DataView(Int32Array.of(number).buffer).getUint32(0, true)
    ).buffer
  );
}

function ashex(buffer) {
  return buffer.getUint32(0, false).toString(16);
}

async function readSafeFile() {
  return await new Promise((resolve, reject) => {
    fs.readFile(file, (err, data) => {
      if (err) return reject(err);
      return resolve(data);
    });
  });
}

async function writeCheckSumToSafeFile(data) {
  return await new Promise((resolve, reject) => {
    fs.writeFile(file, data, err => {
      if (err) reject(err);
      resolve();
    });
  });
}

readSafeFile().then(data => {
  const sum = calculateSum(data);
  const bufferSum = littleToBigEndian(sum);
  const hex = ashex(bufferSum);
  const newData = data;
  for (let i = 0; i < 4; i++) {
    newData[12 + i] = bufferSum.getInt8(i);
  }
  writeCheckSumToSafeFile(newData).then(() => console.log(hex));
});
@sdwvit
Copy link
Author

sdwvit commented Apr 4, 2019

tested with 1.12

@krisives
Copy link
Owner

krisives commented Apr 4, 2019

Thanks I'll add it somewhere so it's easy to see.

@sdwvit
Copy link
Author

sdwvit commented Apr 4, 2019

btw you can check https://www.neoseeker.com/diablo-ii/faqs/26409-hex-edit.html for very old hex guide for d2s files

gucio321 added a commit to gucio321/d2s-format that referenced this issue Sep 12, 2021
gucio321 added a commit to gucio321/d2s-format that referenced this issue Sep 12, 2021
gucio321 added a commit to gucio321/d2s-format that referenced this issue Sep 12, 2021
gucio321 added a commit to gucio321/d2s-format that referenced this issue Sep 12, 2021
gucio321 added a commit to gucio321/d2s-format that referenced this issue Sep 12, 2021
krisives added a commit that referenced this issue Sep 12, 2021
readme/checksum: add programming language iterpretation (fix #5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants