factorio-api.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import https from "node:https";
  2. import fs from "fs";
  3. import handlebars from "handlebars";
  4. import * as M from "./factorio-api.models.js";
  5. interface Data {
  6. app: string;
  7. appVersion: string;
  8. apiVersion: number;
  9. stage: string;
  10. interfaces: Interface[];
  11. types: DataType[];
  12. }
  13. interface Interface {
  14. name: string;
  15. description: string;
  16. export: boolean;
  17. extends?: string;
  18. typeName?: string;
  19. props: Property[];
  20. isTypeFunction?: string;
  21. }
  22. interface Property {
  23. name: string;
  24. description: string;
  25. optional: boolean;
  26. type: string;
  27. }
  28. interface DataType {
  29. name: string;
  30. description: string;
  31. value: string;
  32. }
  33. const API_PATH = "https://lua-api.factorio.com/latest/prototype-api.json";
  34. function getPrototypeApi(): Promise<M.PrototypeApi> {
  35. return new Promise((resolve, reject) => {
  36. https
  37. .get(API_PATH, (res) => {
  38. let data = "";
  39. res.on("data", (chunk: string) => {
  40. data += chunk;
  41. });
  42. res.on("end", () => {
  43. const api = JSON.parse(data) as M.PrototypeApi;
  44. resolve(api);
  45. });
  46. })
  47. .on("error", (err) => {
  48. reject(err);
  49. });
  50. });
  51. }
  52. function parseType(type: M.DataType, structName?: string): string {
  53. if (typeof type === "string") {
  54. // Handle known built-in types
  55. switch (type) {
  56. case "bool":
  57. return "boolean";
  58. case "double":
  59. case "float":
  60. case "int8":
  61. case "int16":
  62. case "int32":
  63. case "uint8":
  64. case "uint16":
  65. case "uint32":
  66. case "uint64":
  67. return "number";
  68. case "string":
  69. return "string";
  70. case "DataExtendMethod":
  71. return "() => void";
  72. default:
  73. return type;
  74. }
  75. }
  76. switch (type.complex_type) {
  77. case "array":
  78. return `${parseType(type.value, structName)}[]`;
  79. case "dictionary":
  80. return `Record<${parseType(type.key, structName)}, ${parseType(type.value, structName)}>`;
  81. case "literal": {
  82. if (typeof type.value === "string") {
  83. return `'${type.value}'`;
  84. } else {
  85. return type.value.toString();
  86. }
  87. }
  88. case "struct":
  89. if (structName == null)
  90. throw new Error("Unexpected struct: use properties on parent or pass a struct name to use");
  91. return structName;
  92. case "tuple":
  93. return `[${type.values.map((v) => parseType(v, structName)).join(", ")}]`;
  94. case "type":
  95. return parseType(type.value, structName);
  96. case "union":
  97. return `(${type.options.map((o) => parseType(o, structName)).join(" | ")})`;
  98. }
  99. }
  100. function parseProperty(prop: M.Property): Property {
  101. return {
  102. name: prop.name,
  103. description: prop.description,
  104. optional: prop.optional,
  105. type: parseType(prop.type),
  106. };
  107. }
  108. function getTypePropertyValue(props: M.Property[]): string | undefined {
  109. const typeProp = props.find((p) => p.name === "type");
  110. if (
  111. typeProp &&
  112. typeof typeProp.type !== "string" &&
  113. typeProp.type.complex_type === "literal" &&
  114. typeof typeProp.type.value === "string"
  115. ) {
  116. return typeProp.type.value;
  117. }
  118. return undefined;
  119. }
  120. function parsePrototypeApi(api: M.PrototypeApi): Data {
  121. const data: Data = {
  122. app: api.application,
  123. appVersion: api.application_version,
  124. apiVersion: api.api_version,
  125. stage: api.stage,
  126. interfaces: [],
  127. types: [],
  128. };
  129. api.prototypes.forEach((p) => {
  130. const int: Interface = {
  131. name: p.name,
  132. description: p.description,
  133. export: p.parent == null,
  134. extends: p.parent,
  135. props: p.properties.map((p) => parseProperty(p)),
  136. };
  137. const typePropValue = getTypePropertyValue(p.properties);
  138. if (typePropValue != null) {
  139. int.isTypeFunction = typePropValue;
  140. } else if (p.typename) {
  141. int.typeName = p.typename;
  142. int.isTypeFunction = p.typename;
  143. }
  144. data.interfaces.push(int);
  145. });
  146. api.types.forEach((t) => {
  147. if (typeof t.type === "string") {
  148. if (t.type !== "builtin") {
  149. const dataType: DataType = {
  150. name: t.name,
  151. description: t.description,
  152. value: parseType(t.type),
  153. };
  154. data.types.push(dataType);
  155. }
  156. } else if (t.type.complex_type === "struct") {
  157. const int: Interface = {
  158. name: t.name,
  159. description: t.description,
  160. export: t.parent == null,
  161. extends: t.parent,
  162. props: [],
  163. };
  164. if (t.properties) {
  165. int.props = t.properties.map((p) => parseProperty(p));
  166. int.isTypeFunction = getTypePropertyValue(t.properties);
  167. }
  168. data.interfaces.push(int);
  169. } else {
  170. let structName: string | undefined;
  171. if (t.properties) {
  172. structName = `_${t.name}`;
  173. const int: Interface = {
  174. name: structName,
  175. description: t.description,
  176. export: false,
  177. extends: t.parent,
  178. props: t.properties.map((p) => parseProperty(p)),
  179. };
  180. data.interfaces.push(int);
  181. }
  182. const dataType: DataType = {
  183. name: t.name,
  184. description: t.description,
  185. value: parseType(t.type, structName),
  186. };
  187. data.types.push(dataType);
  188. }
  189. });
  190. return data;
  191. }
  192. const generate = async function (): Promise<void> {
  193. console.log(`Regenerating Factorio prototype models from ${API_PATH}...`);
  194. const api = await getPrototypeApi();
  195. const data = parsePrototypeApi(api);
  196. const modelsSource = `/** Generated file, do not edit. See scripts/factorio-api.ts for generator. */
  197. /**
  198. * Application: {{app}}
  199. * Version: {{appVersion}}
  200. * API Version: {{apiVersion}}
  201. */
  202. {{#interfaces}}
  203. {{#if description}}
  204. /** {{{description}}} */
  205. {{/if}}
  206. {{#unless props.length}}
  207. // eslint-disable-next-line @typescript-eslint/no-empty-interface
  208. {{/unless}}
  209. {{#if export}}export {{/if}}interface {{#if extends}}_{{/if}}{{name}} {
  210. {{#if typeName}}
  211. type: '{{typeName}}';
  212. {{/if}}
  213. {{#props}}
  214. {{#if description}}
  215. /** {{{description}}} */
  216. {{/if}}
  217. {{name}}{{#if optional}}?{{/if}}: {{{type}}};
  218. {{/props}}
  219. }
  220. {{#if extends}}
  221. export type {{name}} = _{{name}} & Omit<{{extends}}, keyof _{{name}}>;
  222. {{/if}}
  223. {{#if isTypeFunction}}
  224. export function is{{name}}(value: unknown): value is {{name}} {
  225. return (value as { type: string }).type === '{{isTypeFunction}}';
  226. }
  227. {{/if}}
  228. {{/interfaces}}
  229. {{#types}}
  230. {{#if description}}
  231. /** {{{description}}} */
  232. {{/if}}
  233. export type {{name}} = {{{value}}};
  234. {{/types}}`;
  235. const modelsTemplate = handlebars.compile(modelsSource);
  236. const result = modelsTemplate(data);
  237. fs.writeFileSync("scripts/factorio-dump.models.ts", result);
  238. console.log(`Generated ${api.prototypes.length.toString()} models`);
  239. };
  240. void generate();