-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
suggest methods to hook inside of modify class (#5)
somewhat wip
- Loading branch information
Showing
4 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { existsSync, readFileSync } from "node:fs"; | ||
import { None, Option } from "../utils/monads"; | ||
import { getActiveProject } from "./project"; | ||
|
||
export interface CodegenData { | ||
classes: CodegenClass[]; | ||
} | ||
|
||
export interface CodegenClass { | ||
name: string; | ||
functions: CodegenFunction[]; | ||
} | ||
|
||
type CodegenBindingType = number | "link" | "inline" | null; | ||
|
||
export interface CodegenFunction { | ||
name: string; | ||
args: CodegenArg[]; | ||
static: boolean; | ||
const: boolean; | ||
virtual: boolean; | ||
bindings: { | ||
win: CodegenBindingType; | ||
imac: CodegenBindingType; | ||
m1: CodegenBindingType; | ||
ios: CodegenBindingType; | ||
android32: CodegenBindingType; | ||
android64: CodegenBindingType; | ||
}; | ||
return: string; | ||
docs?: string; | ||
kind: string; | ||
} | ||
|
||
export interface CodegenArg { | ||
type: string; | ||
name: string; | ||
} | ||
|
||
let CACHED_CODEGEN_DATA: Option<CodegenData> = None; | ||
|
||
export function getActiveCodegenData(): Option<CodegenData> { | ||
if (CACHED_CODEGEN_DATA) { | ||
return CACHED_CODEGEN_DATA; | ||
} | ||
|
||
const project = getActiveProject(); | ||
if (!project) { | ||
return None; | ||
} | ||
|
||
const codegenDataPath = `${project.path}/build/bindings/bindings/Geode/CodegenData.json`; | ||
if (!existsSync(codegenDataPath)) { | ||
return None; | ||
} | ||
|
||
return (CACHED_CODEGEN_DATA = JSON.parse( | ||
readFileSync(codegenDataPath).toString(), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { | ||
CancellationToken, | ||
CompletionContext, | ||
CompletionItem, | ||
CompletionItemKind, | ||
CompletionItemProvider, | ||
CompletionItemTag, | ||
Position, | ||
SnippetString, | ||
TextDocument, | ||
} from "vscode"; | ||
import { getActiveCodegenData } from "./CodegenData"; | ||
import { getExtConfig } from "../config"; | ||
|
||
const MODIFY_CLASS_REGEX = | ||
/^(?:class|struct)(?: \$modify\((?:\w+,)?\s?(\w+)\)|.+Modify\<(\w+)\>)/; | ||
|
||
export class ModifyClassMethodCompletion implements CompletionItemProvider { | ||
provideCompletionItems( | ||
document: TextDocument, | ||
position: Position, | ||
_token: CancellationToken, | ||
_context: CompletionContext, | ||
) { | ||
if (!getExtConfig().get<boolean>("modifyClassSuggestions.enable")) { | ||
return; | ||
} | ||
|
||
// try to find what modify class we're on | ||
// not a great system but works for now | ||
let currentClass = null; | ||
let lineN = position.line; | ||
while (lineN >= 0) { | ||
let line = document.lineAt(lineN).text; | ||
if (line.startsWith("};")) { | ||
break; | ||
} | ||
let match = line.match(MODIFY_CLASS_REGEX); | ||
if (match) { | ||
currentClass = match[1] ?? match[2]; | ||
break; | ||
} | ||
--lineN; | ||
} | ||
if (!currentClass) { | ||
return; | ||
} | ||
|
||
const codegenData = getActiveCodegenData(); | ||
if (!codegenData) { | ||
return; | ||
} | ||
|
||
let classInfo = null; | ||
for (let c of codegenData.classes) { | ||
if (c.name === currentClass) { | ||
classInfo = c; | ||
break; | ||
} | ||
} | ||
if (!classInfo) { | ||
return; | ||
} | ||
|
||
const stripCocos = getExtConfig().get<boolean>( | ||
"modifyClassSuggestions.stripCocosNamespace", | ||
); | ||
|
||
let suggestions = []; | ||
for (let func of classInfo.functions) { | ||
const shortFuncDecl = `${func.name}(${func.args.map((a) => `${a.type} ${a.name}`).join(", ")})`; | ||
const fullFuncDecl = `${func.static ? "static " : ""}${func.return} ${shortFuncDecl}`; | ||
const origCall = `${currentClass}::${func.name}(${func.args.map((a) => a.name).join(", ")})`; | ||
|
||
let origStatement; | ||
if (func.return === "void") { | ||
origStatement = `\${1}\n\t${origCall};`; | ||
} else if (func.return === "bool") { | ||
origStatement = `if (!${origCall}) return false;\n\t\${1}\n\treturn true;`; | ||
} else { | ||
origStatement = `${func.return} ret = ${origCall};\n\t\${1}\n\treturn ret;`; | ||
} | ||
|
||
const item = new CompletionItem( | ||
shortFuncDecl, | ||
CompletionItemKind.Method, | ||
); | ||
|
||
item.insertText = `${fullFuncDecl} {\n\t${origStatement}\n}`; | ||
if (stripCocos) { | ||
item.insertText = item.insertText.replace(/cocos2d::/g, ""); | ||
} | ||
item.insertText = new SnippetString(item.insertText); | ||
|
||
let rank = 0; | ||
item.detail = fullFuncDecl; | ||
item.documentation = ""; | ||
|
||
if (func.docs) { | ||
item.documentation = func.docs.trim(); | ||
} | ||
if (func.return === "TodoReturn") { | ||
item.tags = [CompletionItemTag.Deprecated]; | ||
item.documentation = "Missing return type, do not use"; | ||
item.insertText = `// TODO: fix TodoReturn\n// ${fullFuncDecl}`; | ||
rank = 1; | ||
item.sortText = "1" + shortFuncDecl; | ||
} | ||
if (Object.values(func.bindings).includes("inline")) { | ||
item.tags = [CompletionItemTag.Deprecated]; | ||
if (Object.values(func.bindings).every((b) => b === "inline")) { | ||
item.documentation += "\nInline method, cannot hook"; | ||
item.insertText = `// ${shortFuncDecl} is inlined on all platforms, cannot hook`; | ||
rank = 1; | ||
} else { | ||
item.documentation += "\nInline method on some platforms"; | ||
} | ||
} | ||
|
||
item.sortText = rank.toString() + shortFuncDecl; | ||
suggestions.push(item); | ||
} | ||
return suggestions; | ||
} | ||
} |