Skip to content

Commit

Permalink
fix: hash algo
Browse files Browse the repository at this point in the history
  • Loading branch information
dsrkafuu committed Jun 5, 2023
1 parent a81adf2 commit 46a4b19
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"private": true,
"name": "skyline-overlay",
"version": "3.8.0",
"version": "3.8.1",
"description": "A modern customizable horizon FFXIV miniparse overlay.",
"license": "Apache-2.0",
"author": "DSRKafuU <dsrkafuu@outlook.com> (https://dsrkafuu.net)",
Expand Down
11 changes: 11 additions & 0 deletions src/utils/lodash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,14 @@ export function xssEscape(str: string) {
}
});
}

export async function sha1(message: string) {
const stable = message;
const msgUint8 = new TextEncoder().encode(stable);
const hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, '0'))
.join('');
return hashHex;
}
11 changes: 9 additions & 2 deletions src/utils/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { OverlayAPI, ExtendData } from 'ffxiv-overlay-api';
import stablehash from 'stable-hash';
import { store } from '../store';
import { pushHistory, updateCombat } from '../store/slices/api';
import { cloneDeep, sha1 } from './lodash';

const overlay = new OverlayAPI();

Expand Down Expand Up @@ -31,10 +32,16 @@ function tryPushHistory(newData: ExtendData) {

let lastDataHash = '';

function tryUpdateCombat(newData: ExtendData) {
async function tryUpdateCombat(newData: ExtendData) {
try {
// prevent hash constantly changing leads to unnecessary re-render/history reset
const newDataHash = stablehash(newData);
const newDataHash = await sha1(
stablehash(
cloneDeep(newData).combatant.sort((a, b) =>
a.name.localeCompare(b.name)
)
)
);
if (lastDataHash !== newDataHash) {
store.dispatch(updateCombat(newData));
lastDataHash = newDataHash;
Expand Down

1 comment on commit 46a4b19

@vercel
Copy link

@vercel vercel bot commented on 46a4b19 Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

skyline-overlay – ./

skyline-overlay-git-main-dsrkafuu.vercel.app
skyline-overlay-dsrkafuu.vercel.app
skyline.dsrkafuu.net

Please sign in to comment.