factorio-api.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import https from "node:https";
  2. import fs from "fs";
  3. import handlebars from "handlebars";
  4. import type * as M from "./factorio-api.models.d.ts";
  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(
  91. "Unexpected struct: use properties on parent or pass a struct name to use"
  92. );
  93. return structName;
  94. case "tuple":
  95. return `[${type.values.map((v) => parseType(v, structName)).join(", ")}]`;
  96. case "type":
  97. return parseType(type.value, structName);
  98. case "union":
  99. return `(${type.options.map((o) => parseType(o, structName)).join(" | ")})`;
  100. }
  101. }
  102. function parseProperty(prop: M.Property): Property {
  103. return {
  104. name: prop.name,
  105. description: prop.description,
  106. optional: prop.optional,
  107. type: parseType(prop.type),
  108. };
  109. }
  110. function getTypePropertyValue(props: M.Property[]): string | undefined {
  111. const typeProp = props.find((p) => p.name === "type");
  112. if (
  113. typeProp &&
  114. typeof typeProp.type !== "string" &&
  115. typeProp.type.complex_type === "literal" &&
  116. typeof typeProp.type.value === "string"
  117. ) {
  118. return typeProp.type.value;
  119. }
  120. return undefined;
  121. }
  122. function parsePrototypeApi(api: M.PrototypeApi): Data {
  123. const data: Data = {
  124. app: api.application,
  125. appVersion: api.application_version,
  126. apiVersion: api.api_version,
  127. stage: api.stage,
  128. interfaces: [],
  129. types: [],
  130. };
  131. api.prototypes.forEach((p) => {
  132. const int: Interface = {
  133. name: p.name,
  134. description: p.description,
  135. export: p.parent == null,
  136. extends: p.parent,
  137. props: p.properties.map((p) => parseProperty(p)),
  138. };
  139. const typePropValue = getTypePropertyValue(p.properties);
  140. if (typePropValue != null) {
  141. int.isTypeFunction = typePropValue;
  142. } else if (p.typename) {
  143. int.typeName = p.typename;
  144. int.isTypeFunction = p.typename;
  145. }
  146. data.interfaces.push(int);
  147. });
  148. api.types.forEach((t) => {
  149. if (typeof t.type === "string") {
  150. if (t.type !== "builtin") {
  151. const dataType: DataType = {
  152. name: t.name,
  153. description: t.description,
  154. value: parseType(t.type),
  155. };
  156. data.types.push(dataType);
  157. }
  158. } else if (t.type.complex_type === "struct") {
  159. const int: Interface = {
  160. name: t.name,
  161. description: t.description,
  162. export: t.parent == null,
  163. extends: t.parent,
  164. props: [],
  165. };
  166. if (t.properties) {
  167. int.props = t.properties.map((p) => parseProperty(p));
  168. int.isTypeFunction = getTypePropertyValue(t.properties);
  169. }
  170. data.interfaces.push(int);
  171. } else {
  172. let structName: string | undefined;
  173. if (t.properties) {
  174. structName = `_${t.name}`;
  175. const int: Interface = {
  176. name: structName,
  177. description: t.description,
  178. export: false,
  179. extends: t.parent,
  180. props: t.properties.map((p) => parseProperty(p)),
  181. };
  182. data.interfaces.push(int);
  183. }
  184. const dataType: DataType = {
  185. name: t.name,
  186. description: t.description,
  187. value: parseType(t.type, structName),
  188. };
  189. data.types.push(dataType);
  190. }
  191. });
  192. return data;
  193. }
  194. const generate = async function (): Promise<void> {
  195. console.log(`Regenerating Factorio prototype models from ${API_PATH}...`);
  196. const api = await getPrototypeApi();
  197. const data = parsePrototypeApi(api);
  198. const modelsSource = `/** Generated file, do not edit. See scripts/factorio-api.ts for generator. */
  199. /**
  200. * Application: {{app}}
  201. * Version: {{appVersion}}
  202. * API Version: {{apiVersion}}
  203. */
  204. {{#interfaces}}
  205. {{#if description}}
  206. /** {{{description}}} */
  207. {{/if}}
  208. {{#unless props.length}}
  209. // eslint-disable-next-line @typescript-eslint/no-empty-interface
  210. {{/unless}}
  211. {{#if export}}export {{/if}}interface {{#if extends}}_{{/if}}{{name}} {
  212. {{#if typeName}}
  213. type: '{{typeName}}';
  214. {{/if}}
  215. {{#props}}
  216. {{#if description}}
  217. /** {{{description}}} */
  218. {{/if}}
  219. {{name}}{{#if optional}}?{{/if}}: {{{type}}};
  220. {{/props}}
  221. }
  222. {{#if extends}}
  223. export type {{name}} = _{{name}} & Omit<{{extends}}, keyof _{{name}}>;
  224. {{/if}}
  225. {{#if isTypeFunction}}
  226. export function is{{name}}(value: unknown): value is {{name}} {
  227. return (value as { type: string }).type === '{{isTypeFunction}}';
  228. }
  229. {{/if}}
  230. {{/interfaces}}
  231. {{#types}}
  232. {{#if description}}
  233. /** {{{description}}} */
  234. {{/if}}
  235. export type {{name}} = {{{value}}};
  236. {{/types}}`;
  237. const modelsTemplate = handlebars.compile(modelsSource);
  238. const result = modelsTemplate(data);
  239. fs.writeFileSync("scripts/factorio-dump.models.ts", result);
  240. console.log(`Generated ${api.prototypes.length.toString()} models`);
  241. };
  242. void generate();