Skip to content

Commit

Permalink
Instance api: Ability
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-782 committed Jan 7, 2024
1 parent 8951029 commit 2f954bc
Show file tree
Hide file tree
Showing 17 changed files with 31,197 additions and 393 deletions.
561 changes: 561 additions & 0 deletions lib/index.d.ts

Large diffs are not rendered by default.

28,622 changes: 28,622 additions & 0 deletions lib/index.js

Large diffs are not rendered by default.

1,102 changes: 998 additions & 104 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"text-decoding": "^1.0.0",
"ts-morph": "^20.0.0",
"ts-node": "^10.9.1",
"tsup": "^8.0.1",
"typed-emitter": "^2.1.0",
"typedoc": "^0.25.3",
"typescript": "^5.2.2"
Expand Down
22 changes: 22 additions & 0 deletions src/handles/Ability.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { CreateAbilityNe } from "../utils/common.js";
import { Handle, HandleEventMap } from "./Handle.js";

export interface AbilityEventMap extends HandleEventMap {}

export interface Ability {
get handle(): HandleHolder<"ability">;
}

export class Ability<T extends HandleEventMap = HandleEventMap> extends Handle<T> {
constructor(itemHandle: HandleHolder<"ability">);
constructor(itemHandle: Ability);
constructor(abilCode: number);
constructor(arg: number | Ability | HandleHolder<"ability">) {
if (arg instanceof Ability || arg instanceof HandleHolder) super(arg);
else if (typeof arg === "number") {
super(CreateAbilityNe(arg));
} else {
throw new TypeError("Unknown first arg");
}
}
}
66 changes: 66 additions & 0 deletions src/handles/instanceApi/AbilityData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-disable matching-export-value-to-file-name */
import { ABILITY_IF_LEVELS, GetAbilityBaseIntegerFieldById, GetAbilityIntegerField } from "../../utils/common.js";
import { Ability } from "../Ability.js";

export interface AbilityLevelAccessorNatives<T> {
get: (ability: HandleHolder<"ability">, field: HandleHolder<"_enum">, level: number) => T;
set: (ability: HandleHolder<"ability">, field: HandleHolder<"_enum">, level: number, value: T) => boolean;

baseGet: (abilId: number, field: HandleHolder<"_enum">, level: number) => T;
baseSet: (ability: number, field: HandleHolder<"_enum">, level: number, value: T) => boolean;
}

export interface AbilityData {
instance: number | Ability;
}

export class AbilityLevelAccessor<T> implements Iterable<T> {
#abilityData: AbilityData;
#field: HandleHolder<"_enum">;

#accessorNatives: AbilityLevelAccessorNatives<T>;

constructor(abilityData: AbilityData, field: HandleHolder<"_enum">, accessNatives: AbilityLevelAccessorNatives<T>) {
this.#abilityData = abilityData;
this.#field = field;

this.#accessorNatives = accessNatives;
}

*[Symbol.iterator](): Iterator<T> {
for (let i = 0; i < this.getLength(); ++i) {
yield this.getIndexValue(i);
}
}

public getLength() {
return typeof this.#abilityData.instance === "number"
? GetAbilityBaseIntegerFieldById(this.#abilityData.instance, ABILITY_IF_LEVELS)
: GetAbilityIntegerField(this.#abilityData.instance.handle, ABILITY_IF_LEVELS);
}

public getIndexValue(index: number) {
return typeof this.#abilityData.instance === "number"
? this.#accessorNatives.baseGet(this.#abilityData.instance, this.#field, index)
: this.#accessorNatives.get(this.#abilityData.instance.handle, this.#field, index);
}

public setIndexValue(index: number, value: T) {
return typeof this.#abilityData.instance === "number"
? this.#accessorNatives.baseSet(this.#abilityData.instance, this.#field, index, value)
: this.#accessorNatives.set(this.#abilityData.instance.handle, this.#field, index, value);
}
}

export class AbilityData {
#_instance: number | Ability;

constructor(instance: number | Ability) {
this.#_instance = instance;

Object.defineProperty(this, "instance", {
value: instance,
writable: false,
});
}
}
33 changes: 21 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { setToHandleHolder, getNativeByName } from "@war3js/unsafe";

import { Console } from "./console/Console.js";
import { toHandleHolderSoft } from "./utils/ToHandleHolder.js";
import { HandleBuilder } from "./services/HandleBuilder.js";
import { HandleBuilder, InstanceApiBuilder } from "./services/ClassBuilder.js";
import { Widget } from "./handles/Widget.js";
import { Unit } from "./handles/Unit.js";
import { Player } from "./handles/Player.js";
Expand All @@ -24,6 +24,7 @@ export const stream: any = {
Object.freeze(stream);

import * as _Buffer from "buffer";
import { AbilityData } from "./handles/instanceApi/AbilityData.js";

export const buffer: any = {
..._Buffer,
Expand All @@ -39,19 +40,27 @@ const wrapper = new HandleWrapper();

setToHandleHolder(toHandleHolderSoft);

const builder = new HandleBuilder();
const handleBuilder = new HandleBuilder();

builder.addChainProperties(Widget, "widget");
builder.addChainProperties(Unit, "unit");
builder.addChainProperties(Player, "player");
builder.addChainProperties(Destructable, "destructable");
builder.addChainProperties(Item, "item");
handleBuilder.addChainProperties(Widget, "widget");
handleBuilder.addChainProperties(Unit, "unit");
handleBuilder.addChainProperties(Player, "player");
handleBuilder.addChainProperties(Destructable, "destructable");
handleBuilder.addChainProperties(Item, "item");

builder.addMethods(Widget, "widget");
builder.addMethods(Unit, "unit");
builder.addMethods(Player, "player");
builder.addMethods(Destructable, "destructable");
builder.addMethods(Item, "item");
handleBuilder.addMethods(Widget, "widget");
handleBuilder.addMethods(Unit, "unit");
handleBuilder.addMethods(Player, "player");
handleBuilder.addMethods(Destructable, "destructable");
handleBuilder.addMethods(Item, "item");

const instanceApiBuilder = new InstanceApiBuilder();

instanceApiBuilder.appendAbilityData(AbilityData);

export * from "./handles/instanceApi/AbilityData.js";

export { IndexAccessArray } from "./services/ClassBuilder.js";

export { TextDecoder, TextEncoder } from "text-decoding";
export { Headers } from "@war3js/headers-polyfill";
Expand Down
Loading

0 comments on commit 2f954bc

Please sign in to comment.