Skip to content

Commit 321292e

Browse files
committed
Override Entity class inherit (ENT, Player, SWEP)
1 parent 8144022 commit 321292e

File tree

6 files changed

+141
-19
lines changed

6 files changed

+141
-19
lines changed

__tests__/api-writer/glua-api-writer.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { apiDefinition as hookApiDefinition, json as hookJson } from '../test-data/offline-sites/gmod-wiki/hook-player-initial-spawn';
22
// import { apiDefinition as classFunctionApiDefinition, json as classFunctionJson } from '../test-data/offline-sites/gmod-wiki/class-function-weapon-allowsautoswitchto';
33
// import { apiDefinition as libraryFunctionApiDefinition, json as libraryFunctionJson } from '../test-data/offline-sites/gmod-wiki/library-function-ai-getscheduleid';
4-
// import { apiDefinition as structApiDefinition, json as structJson } from '../test-data/offline-sites/gmod-wiki/struct-ang-pos';
5-
import { apiDefinition as structApiDefinition, markup as structMarkup } from '../test-data/offline-sites/gmod-wiki/struct-custom-entity-fields';
4+
import { apiDefinition as structApiDefinition, markup as structMarkup, json as structJson } from '../test-data/offline-sites/gmod-wiki/struct-custom-entity-fields';
65
// import { apiDefinition as enumApiDefinition, json as enumJson } from '../test-data/offline-sites/gmod-wiki/enums-use';
76
import { WikiPage, WikiPageMarkupScraper } from '../../src/scrapers/wiki-page-markup-scraper';
87
import { GluaApiWriter } from '../../src/api-writer/glua-api-writer';
@@ -36,7 +35,7 @@ describe('GLua API Writer', () => {
3635
expect(api).toEqual(structApiDefinition);
3736
});
3837

39-
it('should allow override specific page addresses', () => {
38+
it('should allow overriding specific page addresses', () => {
4039
const writer = new GluaApiWriter();
4140
const override = Math.random().toString(36).substring(7);
4241
writer.addOverride(hookJson.address, override);
@@ -45,6 +44,18 @@ describe('GLua API Writer', () => {
4544

4645
expect(api).toEqual(override);
4746
});
47+
48+
it('should allow overriding specific class declarations', () => {
49+
const writer = new GluaApiWriter();
50+
const overrideStart = `---@class Custom_Entity_Fields : Parent`;
51+
const override = `${overrideStart}\n---{{CLASS_FIELDS}}\nlocal Custom_Entity_Fields = {}`;
52+
writer.addOverride('class.Custom_Entity_Fields', override);
53+
54+
const api = writer.writePages([<WikiPage>structJson]);
55+
56+
expect(api).toMatch(new RegExp(`^${overrideStart}`));
57+
});
58+
4859

4960
// it('should be able to write Annotated API files directly from wiki pages', async () => {
5061
// const baseUrl = 'https://wiki.facepunch.com/gmod/GM:AcceptInput';

__tests__/test-data/offline-sites/gmod-wiki/struct-custom-entity-fields.ts

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,97 @@ export const apiDefinition = `---@class Custom_Entity_Fields
6464
---@field RenderOverride function Documented at ENTITY:RenderOverride.
6565
---@field m_RenderOrigin Vector (Clientside) Do not use.
6666
---@field m_RenderAngles Angle (Clientside) Do not use.
67-
local Custom_Entity_Fields = {}\n\n`;
67+
local Custom_Entity_Fields = {}\n\n`;
68+
69+
export const json = {
70+
name: 'Custom_Entity_Fields',
71+
address: 'Custom_Entity_Fields',
72+
type: 'struct',
73+
fields: [
74+
{
75+
name: 'GetEntityDriveMode',
76+
type: 'function',
77+
description: '`Serverside`, Sandbox and Sandbox derived only.\n\nCalled by the Drive property to override the default drive type, which is `drive_sandbox`.',
78+
},
79+
{
80+
name: 'OnEntityCopyTableFinish',
81+
type: 'function',
82+
description: 'Documented at ENTITY:OnEntityCopyTableFinish.',
83+
},
84+
{
85+
name: 'PostEntityCopy',
86+
type: 'function',
87+
description: 'Documented at ENTITY:PostEntityCopy.',
88+
},
89+
{
90+
name: 'PostEntityPaste',
91+
type: 'function',
92+
description: 'Documented at ENTITY:PostEntityPaste.',
93+
},
94+
{
95+
name: 'PreEntityCopy',
96+
type: 'function',
97+
description: 'Documented at ENTITY:PreEntityCopy.',
98+
},
99+
{
100+
name: 'OnDuplicated',
101+
type: 'function',
102+
description: 'Documented at ENTITY:OnDuplicated.',
103+
},
104+
{
105+
name: 'PhysgunDisabled',
106+
type: 'boolean',
107+
description: '`Shared`, Sandbox or Sandbox derived only.\n\nIf set to `true`, physgun will not be able to pick this entity up. This can also be set from map, see Sandbox Specific Mapping',
108+
},
109+
{
110+
name: 'PhysgunPickup',
111+
type: 'function',
112+
description: '`Shared`, Sandbox or Sandbox derived only.\n\nCalled from GM:PhysgunPickup, overrides `PhysgunDisabled`',
113+
},
114+
{
115+
name: 'm_tblToolsAllowed',
116+
type: 'table',
117+
description: '`Shared`, Sandbox or Sandbox derived only.\n\nControls which tools **and** properties can be used on this entity. Format is a list of strings where each string is the tool or property classname.\n\nThis can also be set from map, see Sandbox Specific Mapping',
118+
},
119+
{
120+
name: 'GravGunPickupAllowed',
121+
type: 'function',
122+
description: 'Documented at ENTITY:GravGunPickupAllowed.',
123+
},
124+
{
125+
name: 'GravGunPunt',
126+
type: 'function',
127+
description: 'Documented at ENTITY:GravGunPunt.',
128+
},
129+
{
130+
name: 'CanProperty',
131+
type: 'function',
132+
description: 'Documented at ENTITY:CanProperty.',
133+
},
134+
{
135+
name: 'CanTool',
136+
type: 'function',
137+
description: 'Documented at ENTITY:CanTool.',
138+
},
139+
{
140+
name: 'CalcAbsolutePosition',
141+
type: 'function',
142+
description: 'Documented at ENTITY:CalcAbsolutePosition.',
143+
},
144+
{
145+
name: 'RenderOverride',
146+
type: 'function',
147+
description: 'Documented at ENTITY:RenderOverride.',
148+
},
149+
{
150+
name: 'm_RenderOrigin',
151+
type: 'Vector',
152+
description: '(Clientside) Do not use.',
153+
},
154+
{
155+
name: 'm_RenderAngles',
156+
type: 'Angle',
157+
description: '(Clientside) Do not use.',
158+
},
159+
],
160+
};

custom/class.ENT.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---@class ENT : Entity
2+
---{{CLASS_FIELDS}}
3+
ENT = {}

custom/class.Player.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---@class Player : Entity
2+
---{{CLASS_FIELDS}}
3+
local Player = {}

custom/class.SWEP.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---@class SWEP : Entity
2+
---{{CLASS_FIELDS}}
3+
SWEP = {}

src/api-writer/glua-api-writer.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ export class GluaApiWriter {
6262
}
6363

6464
public addOverride(pageAddress: string, override: string) {
65-
this.pageOverrides.set(pageAddress.toLowerCase(), override);
65+
this.pageOverrides.set(pageAddress, override);
6666
}
6767

6868
public writePage(page: WikiPage) {
69-
if (this.pageOverrides.has(page.address.toLowerCase()))
70-
return this.pageOverrides.get(page.address.toLowerCase());
69+
if (this.pageOverrides.has(page.address))
70+
return this.pageOverrides.get(page.address);
7171
else if (isClassFunction(page))
7272
return this.writeClassFunction(page);
7373
else if (isLibraryFunction(page))
@@ -82,16 +82,29 @@ export class GluaApiWriter {
8282
return this.writeStruct(page);
8383
}
8484

85-
private writeClassFunction(func: ClassFunction) {
85+
private writeClass(className: string, classFields: string = '') {
8686
let api: string = '';
8787

88-
if (!this.writtenClasses.has(func.parent)) {
89-
api += `---@class ${func.parent}\n`;
90-
api += `local ${func.parent} = {}\n\n`;
88+
if (!this.writtenClasses.has(className)) {
89+
const classOverride = `class.${className}`;
90+
if (this.pageOverrides.has(classOverride)) {
91+
api += this.pageOverrides.get(classOverride)!.replace(/\n$/g, '') + '\n\n';
92+
api = api.replace('---{{CLASS_FIELDS}}\n', classFields);
93+
} else {
94+
api += `---@class ${className}\n`;
95+
api += classFields;
96+
api += `local ${className} = {}\n\n`;
97+
}
9198

92-
this.writtenClasses.add(func.parent);
99+
this.writtenClasses.add(className);
93100
}
94101

102+
return api;
103+
}
104+
105+
private writeClassFunction(func: ClassFunction) {
106+
let api: string = this.writeClass(func.parent);
107+
95108
api += this.writeFunctionLuaDocComment(func, func.realm);
96109
api += this.writeFunctionDeclaration(func, func.realm, ':');
97110

@@ -146,17 +159,13 @@ export class GluaApiWriter {
146159
}
147160

148161
private writeStruct(struct: Struct) {
149-
let api: string = '';
150-
151-
api += `---@class ${struct.name}\n`;
162+
let fields: string = '';
152163

153164
for (const field of struct.fields) {
154-
api += `---@field ${GluaApiWriter.safeName(field.name)} ${this.transformType(field.type)} ${removeNewlines(field.description!)}\n`;
165+
fields += `---@field ${GluaApiWriter.safeName(field.name)} ${this.transformType(field.type)} ${removeNewlines(field.description!)}\n`;
155166
}
156-
157-
api += `local ${struct.name} = {}\n\n`;
158167

159-
return api;
168+
return this.writeClass(struct.name, fields);
160169
}
161170

162171
public writePages(pages: WikiPage[]) {

0 commit comments

Comments
 (0)