Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,77 +46,25 @@ This can also be set from map, see <page>Sandbox Specific Mapping</page></item>

</structure>`;

export const apiDefinition =
`---@class Custom_Entity_Fields
local Custom_Entity_Fields = {}

---\`Serverside\`, Sandbox and Sandbox derived only. Called by the Drive property to override the default drive type, which is \`drive_sandbox\`.
---@type function
Custom_Entity_Fields.GetEntityDriveMode = nil

---Documented at ENTITY:OnEntityCopyTableFinish.
---@type function
Custom_Entity_Fields.OnEntityCopyTableFinish = nil

---Documented at ENTITY:PostEntityCopy.
---@type function
Custom_Entity_Fields.PostEntityCopy = nil

---Documented at ENTITY:PostEntityPaste.
---@type function
Custom_Entity_Fields.PostEntityPaste = nil

---Documented at ENTITY:PreEntityCopy.
---@type function
Custom_Entity_Fields.PreEntityCopy = nil

---Documented at ENTITY:OnDuplicated.
---@type function
Custom_Entity_Fields.OnDuplicated = nil

---\`Shared\`, Sandbox or Sandbox derived only. If set to \`true\`, physgun will not be able to pick this entity up. This can also be set from map, see Sandbox Specific Mapping
---@type boolean
Custom_Entity_Fields.PhysgunDisabled = nil

---\`Shared\`, Sandbox or Sandbox derived only. Called from GM:PhysgunPickup, overrides \`PhysgunDisabled\`
---@type function
Custom_Entity_Fields.PhysgunPickup = nil

---\`Shared\`, Sandbox or Sandbox derived only. Controls 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. This can also be set from map, see Sandbox Specific Mapping
---@type table
Custom_Entity_Fields.m_tblToolsAllowed = nil

---Documented at ENTITY:GravGunPickupAllowed.
---@type function
Custom_Entity_Fields.GravGunPickupAllowed = nil

---Documented at ENTITY:GravGunPunt.
---@type function
Custom_Entity_Fields.GravGunPunt = nil

---Documented at ENTITY:CanProperty.
---@type function
Custom_Entity_Fields.CanProperty = nil

---Documented at ENTITY:CanTool.
---@type function
Custom_Entity_Fields.CanTool = nil

---Documented at ENTITY:CalcAbsolutePosition.
---@type function
Custom_Entity_Fields.CalcAbsolutePosition = nil

---Documented at ENTITY:RenderOverride.
---@type function
Custom_Entity_Fields.RenderOverride = nil

---(Clientside) Do not use.
---@type Vector
Custom_Entity_Fields.m_RenderOrigin = nil

---(Clientside) Do not use.
---@type Angle
Custom_Entity_Fields.m_RenderAngles = nil\n\n`;
export const apiDefinition = `---@class Custom_Entity_Fields
---@field GetEntityDriveMode function \`Serverside\`, Sandbox and Sandbox derived only. Called by the Drive property to override the default drive type, which is \`drive_sandbox\`.
---@field OnEntityCopyTableFinish function Documented at ENTITY:OnEntityCopyTableFinish.
---@field PostEntityCopy function Documented at ENTITY:PostEntityCopy.
---@field PostEntityPaste function Documented at ENTITY:PostEntityPaste.
---@field PreEntityCopy function Documented at ENTITY:PreEntityCopy.
---@field OnDuplicated function Documented at ENTITY:OnDuplicated.
---@field PhysgunDisabled boolean \`Shared\`, Sandbox or Sandbox derived only. If set to \`true\`, physgun will not be able to pick this entity up. This can also be set from map, see Sandbox Specific Mapping
---@field PhysgunPickup function \`Shared\`, Sandbox or Sandbox derived only. Called from GM:PhysgunPickup, overrides \`PhysgunDisabled\`
---@field m_tblToolsAllowed table \`Shared\`, Sandbox or Sandbox derived only. Controls 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. This can also be set from map, see Sandbox Specific Mapping
---@field GravGunPickupAllowed function Documented at ENTITY:GravGunPickupAllowed.
---@field GravGunPunt function Documented at ENTITY:GravGunPunt.
---@field CanProperty function Documented at ENTITY:CanProperty.
---@field CanTool function Documented at ENTITY:CanTool.
---@field CalcAbsolutePosition function Documented at ENTITY:CalcAbsolutePosition.
---@field RenderOverride function Documented at ENTITY:RenderOverride.
---@field m_RenderOrigin Vector (Clientside) Do not use.
---@field m_RenderAngles Angle (Clientside) Do not use.
local Custom_Entity_Fields = {}\n\n`;

export const json = {
name: 'Custom_Entity_Fields',
Expand Down
3 changes: 1 addition & 2 deletions custom/class.Color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
---@field g number The green component of the color.
---@field b number The blue component of the color.
---@field a number The alpha component of the color.
---{{CLASS_FIELDS}}
local Color = {}

---{{CLASS_FIELDS}}
3 changes: 1 addition & 2 deletions custom/class.ENT.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---@class ENT : Entity
---{{CLASS_FIELDS}}
ENT = {}

---{{CLASS_FIELDS}}
3 changes: 1 addition & 2 deletions custom/class.NPC.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---@class NPC : Entity
---{{CLASS_FIELDS}}
local NPC = {}

---{{CLASS_FIELDS}}
3 changes: 1 addition & 2 deletions custom/class.Player.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---@class Player : Entity
---{{CLASS_FIELDS}}
local Player = {}

---{{CLASS_FIELDS}}
3 changes: 1 addition & 2 deletions custom/class.SWEP.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---@class SWEP : WEAPON
---{{CLASS_FIELDS}}
SWEP = {}

---{{CLASS_FIELDS}}
61 changes: 14 additions & 47 deletions src/api-writer/glua-api-writer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClassFunction, Enum, Function, HookFunction, LibraryFunction, Panel, PanelFunction, Realm, Struct, WikiPage, isPanel } from '../scrapers/wiki-page-markup-scraper.js';
import { escapeSingleQuotes, putCommentBeforeEachLine, removeNewlines, safeFileName, toLowerCamelCase } from '../utils/string.js';
import { putCommentBeforeEachLine, removeNewlines, safeFileName, toLowerCamelCase } from '../utils/string.js';
import {
isClassFunction,
isHookFunction,
Expand Down Expand Up @@ -69,12 +69,12 @@ export class GluaApiWriter {
const fileSafeAddress = safeFileName(page.address, '.');
if (this.pageOverrides.has(fileSafeAddress)) {
let api = '';

if (isClassFunction(page))
api += this.writeClass(page.parent, undefined, undefined, page.deprecated);
api += this.writeClass(page.parent);
else if (isLibraryFunction(page))
api += this.writeLibraryGlobal(page);

api += this.pageOverrides.get(fileSafeAddress);

return `${api}\n\n`;
Expand All @@ -94,26 +94,23 @@ export class GluaApiWriter {
return this.writeStruct(page);
}

private writeClass(className: string, parent?: string, classFields: string = '', deprecated?: string ) {
private writeClass(className: string, parent?: string, classFields: string = '') {
let api: string = '';

if (!this.writtenClasses.has(className)) {
const classOverride = `class.${className}`;
if (this.pageOverrides.has(classOverride)) {
api += this.pageOverrides.get(classOverride)!.replace(/\n$/g, '') + '\n\n';
api = api.replace('---{{CLASS_FIELDS}}', classFields);
api = api.replace('---{{CLASS_FIELDS}}\n', classFields);
} else {
if (deprecated)
api += `---@deprecated ${deprecated ?? ''}\n`

api += `---@class ${className}`;

if (parent)
api += ` : ${parent}`;

api += '\n';
api += `local ${className} = {}\n\n`;
api += classFields;
api += `local ${className} = {}\n\n`;
}

this.writtenClasses.add(className);
Expand All @@ -124,12 +121,7 @@ export class GluaApiWriter {

private writeLibraryGlobal(func: LibraryFunction) {
if (!func.dontDefineParent && !this.writtenLibraryGlobals.has(func.parent)) {
let global = '';

if(func.deprecated)
global += `---@deprecated ${func.deprecated ?? ''}\n`;

global += `${func.parent} = {}\n\n`;
const global = `${func.parent} = {}\n\n`;

this.writtenLibraryGlobals.add(func.parent);

Expand All @@ -140,7 +132,7 @@ export class GluaApiWriter {
}

private writeClassFunction(func: ClassFunction) {
let api: string = this.writeClass(func.parent, undefined, undefined, func.deprecated);
let api: string = this.writeClass(func.parent);

api += this.writeFunctionLuaDocComment(func, func.realm);
api += this.writeFunctionDeclaration(func, func.realm, ':');
Expand All @@ -162,7 +154,7 @@ export class GluaApiWriter {
}

private writePanel(panel: Panel) {
return this.writeClass(panel.name, panel.parent, undefined, panel.deprecated);
return this.writeClass(panel.name, panel.parent);
}

private writePanelFunction(func: PanelFunction) {
Expand All @@ -178,9 +170,6 @@ export class GluaApiWriter {
let api: string = '';
const isContainedInTable = _enum.items[0]?.key.includes('.') ?? false;

if (_enum.deprecated)
api += `---@deprecated ${_enum.deprecated ?? ''}\n`;

api += `---@enum ${_enum.name}\n`;

if (isContainedInTable)
Expand All @@ -191,10 +180,8 @@ export class GluaApiWriter {
key = key.split('.')[1];
api += ` ${key} = ${item.value}, ` + (item.description ? `--[[ ${item.description} ]]` : '') + '\n';
} else {
api += item.description ? `${putCommentBeforeEachLine(item.description, false)}\n` : ''
if (item.deprecated)
api += `---@deprecated ${item.deprecated ?? ''}\n`;
api += `${key} = ${item.value}\n`;
const comment = item.description ? `${putCommentBeforeEachLine(item.description, false)}\n` : '';
api += `${comment}${key} = ${item.value}\n`;
}
};

Expand All @@ -209,31 +196,14 @@ export class GluaApiWriter {
return api;
}

private writeType(type: string, value: any) {
if (type === 'string')
return `'${escapeSingleQuotes(value)}'`;

if (type === 'Vector')
return `Vector${value}`;

return value;
}

private writeStruct(struct: Struct) {
let fields: string = '';

for (const field of struct.fields) {
if (field.deprecated)
fields += `---@deprecated ${field.deprecated ?? ''}\n`;

fields += `---${removeNewlines(field.description).replace(/\s+/g, ' ')}\n`;

const type = this.transformType(field.type)
fields += `---@type ${type}\n`
fields += `${struct.name}.${GluaApiWriter.safeName(field.name)} = ${field.default ? this.writeType(type, field.default) : 'nil'}\n\n`;
fields += `---@field ${GluaApiWriter.safeName(field.name)} ${this.transformType(field.type)} ${removeNewlines(field.description!)}\n`;
}

return this.writeClass(struct.name, undefined, fields, struct.deprecated);
return this.writeClass(struct.name, undefined, fields);
}

public writePages(pages: WikiPage[]) {
Expand Down Expand Up @@ -284,9 +254,6 @@ export class GluaApiWriter {
});
}

if (func.deprecated)
luaDocComment += `---@deprecated ${func.deprecated ?? ''}\n`;

return luaDocComment;
}

Expand Down
Loading