refactor(il2cpp): share indexed row projection schemas (#16616)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-04 14:27:53 +08:00
1 parent c150d7ec66
commit 61939775ca
1 file changed
+39 -59
+39 -59
View File
@@ -112,38 +112,29 @@ export const il2cppMetadataSummarySchema = il2cppMetadataObjectSchema
});
const indexSchema = z.number().int().nonnegative();
const sectionRowSchema = il2cppMetadataSectionSchema.extend({
index: indexSchema,
});
const imageRowSchema = il2cppMetadataImageSchema.extend({ index: indexSchema });
const typeRowSchema = il2cppMetadataTypeSchema.extend({ index: indexSchema });
const methodRowSchema = il2cppMetadataMethodSchema.extend({
index: indexSchema,
});
const memberRowSchema = il2cppMetadataMemberSchema.extend({
index: indexSchema,
});
const parameterRowSchema = il2cppMetadataParameterSchema.extend({
index: indexSchema,
});
const stringLiteralRowSchema = il2cppMetadataStringLiteralSchema.extend({
index: indexSchema,
});
const methodAddressRowSchema = il2cppMethodAddressSchema.extend({
index: indexSchema,
});
const typeReferenceRowSchema = il2cppTypeReferenceSchema.extend({
index: indexSchema,
});
const genericInstanceRowSchema = il2cppGenericMethodInstanceSchema.extend({
index: indexSchema,
});
const codeGenModuleRowSchema = il2cppCodeGenModuleSchema.extend({
index: indexSchema,
});
const genericIssueRowSchema = il2cppGenericMethodIssueSchema.extend({
index: indexSchema,
});
const indexedProjection = <Shape extends z.ZodRawShape>(
source: z.ZodObject<Shape, z.core.$strict>,
) => {
const row = source.extend({ index: indexSchema });
return {
fields: () => z.array(row.keyof()).optional(),
items: z.array(row.partial().required({ index: true })),
};
};
const indexedRows = {
section: indexedProjection(il2cppMetadataSectionSchema),
image: indexedProjection(il2cppMetadataImageSchema),
type: indexedProjection(il2cppMetadataTypeSchema),
method: indexedProjection(il2cppMetadataMethodSchema),
member: indexedProjection(il2cppMetadataMemberSchema),
parameter: indexedProjection(il2cppMetadataParameterSchema),
stringLiteral: indexedProjection(il2cppMetadataStringLiteralSchema),
methodAddress: indexedProjection(il2cppMethodAddressSchema),
typeReference: indexedProjection(il2cppTypeReferenceSchema),
codeGenModule: indexedProjection(il2cppCodeGenModuleSchema),
genericInstance: indexedProjection(il2cppGenericMethodInstanceSchema),
genericIssue: indexedProjection(il2cppGenericMethodIssueSchema),
};
const indexPage = {
kind: z.literal("il2cpp-metadata"),
indices: z.array(indexSchema).optional(),
@@ -160,12 +151,12 @@ export const il2cppMetadataViewRequestSchema = z.discriminatedUnion(
z.strictObject({
...page,
collection: z.literal("sections"),
fields: z.array(sectionRowSchema.keyof()).optional(),
fields: indexedRows.section.fields(),
}),
z.strictObject({
...page,
collection: z.literal("images"),
fields: z.array(imageRowSchema.keyof()).optional(),
fields: indexedRows.image.fields(),
}),
z.strictObject({
...page,
@@ -173,39 +164,39 @@ export const il2cppMetadataViewRequestSchema = z.discriminatedUnion(
image_indices: z.array(indexSchema).optional(),
namespaces: z.array(z.string()).optional(),
declaring_types: declaringTypes,
fields: z.array(typeRowSchema.keyof()).optional(),
fields: indexedRows.type.fields(),
}),
z.strictObject({
...page,
collection: z.literal("methods"),
declaring_types: declaringTypes,
fields: z.array(methodRowSchema.keyof()).optional(),
fields: indexedRows.method.fields(),
}),
...(["fields", "properties", "events"] as const).map((collection) =>
z.strictObject({
...page,
collection: z.literal(collection),
declaring_types: declaringTypes,
fields: z.array(memberRowSchema.keyof()).optional(),
fields: indexedRows.member.fields(),
}),
),
z.strictObject({
...page,
collection: z.literal("parameters"),
method_indices: z.array(indexSchema).optional(),
fields: z.array(parameterRowSchema.keyof()).optional(),
fields: indexedRows.parameter.fields(),
}),
z.strictObject({
...indexPage,
collection: z.literal("string_literals"),
fields: z.array(stringLiteralRowSchema.keyof()).optional(),
fields: indexedRows.stringLiteral.fields(),
}),
z.strictObject({
...indexPage,
collection: z.literal("method_addresses"),
addresses: z.array(z.string().regex(/^0x[0-9a-fA-F]{1,16}$/u)).optional(),
states: z.array(il2cppMethodAddressSchema.shape.state).optional(),
fields: z.array(methodAddressRowSchema.keyof()).optional(),
fields: indexedRows.methodAddress.fields(),
}),
z.strictObject({
...page,
@@ -214,26 +205,26 @@ export const il2cppMetadataViewRequestSchema = z.discriminatedUnion(
method_indices: z.array(indexSchema).optional(),
addresses: z.array(z.string().regex(/^0x[0-9a-fA-F]{1,16}$/u)).optional(),
states: z.array(il2cppGenericMethodInstanceSchema.shape.state).optional(),
fields: z.array(genericInstanceRowSchema.keyof()).optional(),
fields: indexedRows.genericInstance.fields(),
}),
z.strictObject({
...indexPage,
collection: z.literal("generic_method_issues"),
/** genericMethodTable rows the issues leave unknown. */
rows: z.array(indexSchema).optional(),
fields: z.array(genericIssueRowSchema.keyof()).optional(),
fields: indexedRows.genericIssue.fields(),
}),
z.strictObject({
...indexPage,
collection: z.literal("code_gen_modules"),
image_indices: z.array(indexSchema).optional(),
fields: z.array(codeGenModuleRowSchema.keyof()).optional(),
fields: indexedRows.codeGenModule.fields(),
}),
...(["return_types", "field_types"] as const).map((collection) =>
z.strictObject({
...page,
collection: z.literal(collection),
fields: z.array(typeReferenceRowSchema.keyof()).optional(),
fields: indexedRows.typeReference.fields(),
}),
),
z.strictObject({
@@ -241,25 +232,14 @@ export const il2cppMetadataViewRequestSchema = z.discriminatedUnion(
collection: z.literal("parameter_types"),
// Owners come from the retained parameters collection.
method_indices: z.array(indexSchema).optional(),
fields: z.array(typeReferenceRowSchema.keyof()).optional(),
fields: indexedRows.typeReference.fields(),
}),
],
);
export const il2cppMetadataViewItemsSchema = z.union([
z.array(sectionRowSchema.partial().required({ index: true })),
z.array(imageRowSchema.partial().required({ index: true })),
z.array(typeRowSchema.partial().required({ index: true })),
z.array(methodRowSchema.partial().required({ index: true })),
z.array(memberRowSchema.partial().required({ index: true })),
z.array(parameterRowSchema.partial().required({ index: true })),
z.array(stringLiteralRowSchema.partial().required({ index: true })),
z.array(methodAddressRowSchema.partial().required({ index: true })),
z.array(typeReferenceRowSchema.partial().required({ index: true })),
z.array(codeGenModuleRowSchema.partial().required({ index: true })),
z.array(genericInstanceRowSchema.partial().required({ index: true })),
z.array(genericIssueRowSchema.partial().required({ index: true })),
]);
export const il2cppMetadataViewItemsSchema = z.union(
Object.values(indexedRows).map((row) => row.items),
);
export const il2cppMetadataViewContextSchema = z.strictObject({
metadata_version: il2cppMetadataObjectSchema.shape.metadata_version,