|
1 | | -const global = globalThis as typeof globalThis & { reloadCache: Record<string, any> }; |
2 | | -if (global.reloadCache === undefined) { |
3 | | - global.reloadCache = {}; |
| 1 | +export interface BaseAbility extends CDOTA_Ability_Lua {} |
| 2 | +export class BaseAbility {} |
| 3 | + |
| 4 | +export interface BaseItem extends CDOTA_Item_Lua {} |
| 5 | +export class BaseItem {} |
| 6 | + |
| 7 | +export interface BaseModifier extends CDOTA_Modifier_Lua {} |
| 8 | +export class BaseModifier { |
| 9 | + public static apply<T extends typeof BaseModifier>( |
| 10 | + this: T, |
| 11 | + target: CDOTA_BaseNPC, |
| 12 | + caster?: CDOTA_BaseNPC, |
| 13 | + ability?: CDOTABaseAbility, |
| 14 | + modifierTable?: object, |
| 15 | + ): InstanceType<T> { |
| 16 | + return target.AddNewModifier(caster, ability, this.name, modifierTable) as any; |
| 17 | + } |
4 | 18 | } |
5 | 19 |
|
6 | | -export function reloadable<T extends { new (...args: any[]): {} }>(constructor: T): T { |
7 | | - const className = constructor.name; |
8 | | - if (global.reloadCache[className] === undefined) { |
9 | | - global.reloadCache[className] = constructor; |
| 20 | +export interface BaseModifierMotionHorizontal extends CDOTA_Modifier_Lua_Horizontal_Motion {} |
| 21 | +export class BaseModifierMotionHorizontal extends BaseModifier {} |
| 22 | + |
| 23 | +export interface BaseModifierMotionVertical extends CDOTA_Modifier_Lua_Vertical_Motion {} |
| 24 | +export class BaseModifierMotionVertical extends BaseModifier {} |
| 25 | + |
| 26 | +export interface BaseModifierMotionBoth extends CDOTA_Modifier_Lua_Motion_Both {} |
| 27 | +export class BaseModifierMotionBoth extends BaseModifier {} |
| 28 | + |
| 29 | +export const registerAbility = (name?: string) => (ability: new () => CDOTA_Ability_Lua | CDOTA_Item_Lua) => { |
| 30 | + if (name !== undefined) { |
| 31 | + // @ts-ignore |
| 32 | + ability.name = name; |
| 33 | + } else { |
| 34 | + name = ability.name; |
| 35 | + } |
| 36 | + |
| 37 | + const [env] = getFileScope(); |
| 38 | + |
| 39 | + if (env[name]) { |
| 40 | + clearTable(env[name]); |
| 41 | + } else { |
| 42 | + env[name] = {}; |
| 43 | + } |
| 44 | + |
| 45 | + toDotaClassInstance(env[name], ability); |
| 46 | + |
| 47 | + const originalSpawn = env[name].Spawn; |
| 48 | + env[name].Spawn = function () { |
| 49 | + this.____constructor(); |
| 50 | + if (originalSpawn) { |
| 51 | + originalSpawn.call(this); |
| 52 | + } |
| 53 | + }; |
| 54 | +}; |
| 55 | + |
| 56 | +export const registerModifier = (name?: string) => (modifier: new () => CDOTA_Modifier_Lua) => { |
| 57 | + if (name !== undefined) { |
| 58 | + // @ts-ignore |
| 59 | + modifier.name = name; |
| 60 | + } else { |
| 61 | + name = modifier.name; |
| 62 | + } |
| 63 | + |
| 64 | + const [env, source] = getFileScope(); |
| 65 | + const [fileName] = string.gsub(source, ".*scripts[\\/]vscripts[\\/]", ""); |
| 66 | + |
| 67 | + if (env[name]) { |
| 68 | + clearTable(env[name]); |
| 69 | + } else { |
| 70 | + env[name] = {}; |
10 | 71 | } |
11 | 72 |
|
12 | | - Object.assign(global.reloadCache[className].prototype, constructor.prototype); |
13 | | - return global.reloadCache[className]; |
| 73 | + toDotaClassInstance(env[name], modifier); |
| 74 | + |
| 75 | + const originalOnCreated = env[name].OnCreated; |
| 76 | + env[name].OnCreated = function (parameters: any) { |
| 77 | + this.____constructor(); |
| 78 | + if (originalOnCreated) { |
| 79 | + originalOnCreated.call(this, parameters); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + let type = LuaModifierType.LUA_MODIFIER_MOTION_NONE; |
| 84 | + let base = (modifier as any).____super; |
| 85 | + while (base && type === undefined) { |
| 86 | + if (base === BaseModifierMotionBoth) { |
| 87 | + type = LuaModifierType.LUA_MODIFIER_MOTION_BOTH; |
| 88 | + } else if (base === BaseModifierMotionHorizontal) { |
| 89 | + type = LuaModifierType.LUA_MODIFIER_MOTION_HORIZONTAL; |
| 90 | + } else if (base === BaseModifierMotionVertical) { |
| 91 | + type = LuaModifierType.LUA_MODIFIER_MOTION_VERTICAL; |
| 92 | + } |
| 93 | + |
| 94 | + base = base.____super; |
| 95 | + } |
| 96 | + |
| 97 | + LinkLuaModifier(name, fileName, type); |
| 98 | +}; |
| 99 | + |
| 100 | +function clearTable(table: object) { |
| 101 | + for (const key in table) { |
| 102 | + delete (table as any)[key]; |
| 103 | + } |
14 | 104 | } |
15 | 105 |
|
16 | | -export function luaModifier<T extends typeof CDOTA_Modifier_Lua>(modifier: T): T { |
17 | | - const instance: any = {}; |
18 | | - let prototype = modifier.prototype as any; |
19 | | - while (prototype) { |
20 | | - for (const key in prototype) { |
21 | | - if (instance[key] === undefined) { |
22 | | - instance[key] = prototype[key]; |
23 | | - } |
| 106 | +function getFileScope(): [any, string] { |
| 107 | + let level = 1; |
| 108 | + while (true) { |
| 109 | + const info = debug.getinfo(level, "S"); |
| 110 | + if (info && info.what === "main") { |
| 111 | + return [getfenv(level), info.source!]; |
24 | 112 | } |
25 | | - prototype = getmetatable(prototype); |
| 113 | + |
| 114 | + level += 1; |
26 | 115 | } |
27 | | - getfenv(1)[modifier.name] = instance; |
28 | | - return instance; |
29 | 116 | } |
30 | 117 |
|
31 | | -export function luaAbility<T extends typeof CDOTA_Ability_Lua>(ability: T): T { |
32 | | - const instance: any = {}; |
33 | | - let prototype = ability.prototype as any; |
| 118 | +function toDotaClassInstance(instance: any, table: new () => any) { |
| 119 | + let { prototype } = table; |
34 | 120 | while (prototype) { |
35 | 121 | for (const key in prototype) { |
36 | | - if (instance[key] === undefined) { |
| 122 | + if (instance[key] == null) { |
37 | 123 | instance[key] = prototype[key]; |
38 | 124 | } |
39 | 125 | } |
| 126 | + |
40 | 127 | prototype = getmetatable(prototype); |
41 | 128 | } |
42 | | - getfenv(1)[ability.name] = instance; |
43 | | - return instance; |
44 | 129 | } |
0 commit comments