Skip to content

Commit

Permalink
Merge pull request #87 from KotRikD/fix/memory_leak
Browse files Browse the repository at this point in the history
fix: memory leak on bindings states
  • Loading branch information
KotRikD authored Mar 4, 2024
2 parents d4f02ef + e5bc281 commit 654f28f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 52 deletions.
100 changes: 56 additions & 44 deletions packages/tosu/src/entities/AllTimesData/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class AllTimesData extends AbstractEntity {
ShowInterface: boolean = false;
IsWatchingReplay: number = 0;

bindingNames = new Map();

private configList: Record<string, IConfigBindable> = {
VolumeUniversal: {
type: 'int',
Expand Down Expand Up @@ -320,77 +322,87 @@ export class AllTimesData extends AbstractEntity {
super(services);
}

async updateConfigState(
updateConfigState(
process: Process,
settings: Settings,
configurationAddr: number
) {
try {
process.readSharpDictionary(configurationAddr, (current) => {
const key = process.readSharpString(process.readInt(current));
const rawSharpDictionary =
process.readSharpDictionary(configurationAddr);
for (let i = 0; i < rawSharpDictionary.length; i++) {
const current = rawSharpDictionary[i];
const keyAddress = process.readInt(current);
let key;
if (this.bindingNames.has(keyAddress)) {
key = this.bindingNames.get(keyAddress);
} else {
key = process.readSharpString(keyAddress);
this.bindingNames.set(keyAddress, key);
}
const bindable = process.readInt(current + 0x4);

const configBindable = this.configList[key];

if (configBindable !== undefined) {
let value: any;
if (!(key in this.configList)) {
continue;
}
let value: any;

switch (configBindable.type) {
case 'byte':
value = process.readByte(bindable + 0xc);
break;
case 'bool':
value = process.readByte(bindable + 0xc) == 1;
break;
case 'int':
case 'double':
value = process.readDouble(bindable + 0x4);
break;
case 'string':
value = process.readSharpString(
process.readInt(current + 0x4)
);
break;
case 'bstring':
value = process.readSharpString(
process.readInt(bindable + 0x4)
);
break;
case 'enum':
value = process.readInt(bindable + 0xc);
break;
default:
return false;
}
switch (this.configList[key].type) {
case 'byte':
value = process.readByte(bindable + 0xc);
break;
case 'bool':
value = Boolean(process.readByte(bindable + 0xc));
break;
case 'int':
case 'double':
value = process.readDouble(bindable + 0x4);
break;
case 'string':
value = process.readSharpString(
process.readInt(current + 0x4)
);
break;
case 'bstring':
value = process.readSharpString(
process.readInt(bindable + 0x4)
);
break;
case 'enum':
value = process.readInt(bindable + 0xc);
break;
default:
break;
}

configBindable.setValue(settings, value);
if (value) {
this.configList[key].setValue(settings, value);
}
return true;
});
}
} catch (exc) {
wLogger.error("can't update config state");
console.error(exc);
}
}

async updateBindingState(
updateBindingState(
process: Process,
settings: Settings,
bindingConfigAddr: number
) {
try {
process.readSharpDictionary(bindingConfigAddr, (current) => {
const rawSharpDictionary =
process.readSharpDictionary(bindingConfigAddr);
for (let i = 0; i < rawSharpDictionary.length; i++) {
const current = rawSharpDictionary[i];
const key = process.readInt(current);
const value = process.readInt(current + 0xc);

const bindable = this.bindingList[key];

if (bindable !== undefined) {
if (bindable) {
bindable.setValue(settings, value);
}

return true;
});
}
} catch (exc) {
wLogger.error("can't update binding state");
console.error(exc);
Expand Down
14 changes: 6 additions & 8 deletions packages/tsprocess/src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,18 @@ export class Process {
return endString;
}

readSharpDictionary(
address: number,
onLoop: (current: number) => boolean
): void {
readSharpDictionary(address: number): number[] {
const result = [];
const items = this.readInt(address + 0x8);
const size = this.readInt(address + 0x1c);

for (let i = 0; i < size; i++) {
const current = items + 0x8 + 0x10 * i;
const address = items + 0x8 + 0x10 * i;

if (!onLoop(current)) {
return;
}
result.push(address);
}

return result;
}

readBuffer(address: number, size: number): Buffer {
Expand Down

0 comments on commit 654f28f

Please sign in to comment.