forked from FabricMC/fabricmc.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.ts
More file actions
137 lines (114 loc) · 4.01 KB
/
Copy pathtemplate.ts
File metadata and controls
137 lines (114 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { addGradle } from './gradle';
import { addGradleWrapper } from './gradlewrapper';
import { getApiVersionForMinecraft, getKotlinAdapterVersions, getLoaderVersions, getMinecraftYarnVersions, type GameVersion, getGameVersions } from '../Api';
import { addModJson } from './modjson';
import { addGitFiles } from './git';
import { minecraftIsUnobfuscated, minecraftSupportsSplitSources, minecraftSupportsDataGen } from './minecraft';
export const ICON_FONT = "Comic Relief";
export interface Options {
/**
* The configuration of the project we'd like to generate.
*/
config: Configuration;
/**
* An interface we can write the generated files to, depending on the platform we're running
* this might be directly to the filesystem or to a zip file.
*/
writer: TemplateWriter;
canvas: CanvasAdaptorFactory;
}
export interface Configuration {
modid: string,
minecraftVersion: string,
projectName: string,
packageName: string,
mojmap: boolean,
useKotlin: boolean,
dataGeneration: boolean,
splitSources: boolean,
uniqueModIcon: boolean,
gradleKotlin: boolean
}
export interface KotlinConfiguration {
kotlinVersion: string,
fabricKotlinAdapterVersion: string
}
// Computed options are not presented to the user.
export interface ComputedConfiguration extends Configuration {
modid: string,
loaderVersion: string,
fabricVersion: string,
yarnVersion: string | undefined,
kotlin: KotlinConfiguration | undefined
unobfuscated: boolean
}
export interface TemplateOptions {
minecraftVersion: string, loaderVersion: string, yarnVersion: string
}
export interface FileOptions {
executable: boolean;
}
export interface TemplateWriter {
write(path: string, content: string | ArrayBufferLike, options?: FileOptions): Promise<void>
}
export interface CanvasAdaptorFactory {
create(width: number, height: number): CanvasAdaptor | null
}
export interface CanvasAdaptor {
getContext(contextId: "2d"): unknown
getPng(): ArrayBufferLike
measureText(ctx: unknown, text: string): TextMetricsAdaptor
}
export interface TextMetricsAdaptor {
width: number
ascent: number
descent: number
}
export async function generateTemplate(options: Options) {
const computedConfig = await computeConfig(options.config);
await addGradleWrapper(options);
await addGradle(options.writer, computedConfig);
await addModJson(options.writer, options.canvas, computedConfig);
await addGitFiles(options.writer, computedConfig);
}
export async function getTemplateGameVersions(): Promise<GameVersion[]> {
const versions = await getGameVersions()
return versions.filter((v) => {
const version = v.version;
if (version.startsWith("1.14") && version != "1.14.4") {
// Hide pre 1.14.4 MC versions as they require using V1 yarn.
return false;
}
if (!v.stable) {
// Hide unstable versions, other than the latest snapshot.
const isLatest = versions[0].version == version;
return isLatest;
}
return true;
});
}
async function computeConfig(options: Configuration): Promise<ComputedConfiguration> {
const unobfuscated = minecraftIsUnobfuscated(options.minecraftVersion);
return {
...options,
splitSources: minecraftSupportsSplitSources(options.minecraftVersion) && options.splitSources,
dataGeneration: minecraftSupportsDataGen(options.minecraftVersion) && options.dataGeneration,
loaderVersion: (await getLoaderVersions()).find((v) => v.stable)!.version,
fabricVersion: await getApiVersionForMinecraft(options.minecraftVersion),
yarnVersion: unobfuscated ? undefined : (await getMinecraftYarnVersions(options.minecraftVersion))[0].version,
kotlin: await computeKotlinOptions(options),
unobfuscated
};
}
async function computeKotlinOptions(options: Configuration): Promise<KotlinConfiguration | undefined> {
if (!options.useKotlin) {
return undefined;
}
const kotlinVersions = await getKotlinAdapterVersions();
const fabricKotlinAdapterVersion = kotlinVersions.pop()!; // 1.8.2+kotlin.1.7.10
const kotlinVersion = fabricKotlinAdapterVersion.split("+kotlin.")[1];
return {
fabricKotlinAdapterVersion,
kotlinVersion
};
}