sapdon 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/README.md +3 -2
  2. package/dist/cli/build.d.ts +7 -0
  3. package/dist/cli/build.js +236 -0
  4. package/dist/cli/dev-server/client.d.ts +2 -0
  5. package/dist/cli/dev-server/client.js +21 -0
  6. package/dist/cli/dev-server/config.d.ts +3 -0
  7. package/dist/cli/dev-server/config.js +3 -0
  8. package/dist/cli/dev-server/handles.d.ts +4 -0
  9. package/dist/cli/dev-server/handles.js +5 -0
  10. package/dist/cli/dev-server/hmr.d.ts +1 -0
  11. package/dist/cli/dev-server/hmr.js +13 -0
  12. package/dist/cli/dev-server/index.d.ts +4 -0
  13. package/dist/cli/dev-server/index.js +17 -0
  14. package/dist/cli/dev-server/server.d.ts +14 -0
  15. package/dist/cli/dev-server/server.js +59 -0
  16. package/dist/cli/dev-server/syncFiles.d.ts +3 -0
  17. package/dist/cli/dev-server/syncFiles.js +45 -0
  18. package/dist/cli/index.d.ts +20 -0
  19. package/dist/cli/index.js +90 -0
  20. package/dist/cli/init.d.ts +9 -0
  21. package/dist/cli/init.js +80 -0
  22. package/dist/cli/load.d.ts +3 -0
  23. package/dist/cli/load.js +94 -0
  24. package/dist/cli/start.d.ts +2 -0
  25. package/dist/cli/start.js +1398 -0
  26. package/dist/cli/tools/textureSet.d.ts +13 -0
  27. package/dist/cli/tools/textureSet.js +103 -0
  28. package/dist/cli/utils.d.ts +13 -0
  29. package/dist/cli/utils.js +89 -0
  30. package/dist/core/addon/biome.d.ts +22 -0
  31. package/dist/core/addon/biome.js +27 -0
  32. package/dist/core/addon/block/block.d.ts +41 -0
  33. package/dist/core/addon/block/block.js +46 -0
  34. package/dist/core/addon/controllers/animationController.d.ts +43 -0
  35. package/dist/core/addon/controllers/animationController.js +73 -0
  36. package/dist/core/addon/controllers/render_controllers.js +162 -0
  37. package/dist/core/addon/entity/clientEntity.d.ts +107 -0
  38. package/dist/core/addon/entity/clientEntity.js +149 -0
  39. package/dist/core/addon/entity/entity.d.ts +42 -0
  40. package/dist/core/addon/entity/entity.js +50 -0
  41. package/dist/core/addon/feature/oreFeature.d.ts +19 -0
  42. package/dist/core/addon/feature/oreFeature.js +24 -0
  43. package/dist/core/addon/featureRule.d.ts +20 -0
  44. package/dist/core/addon/featureRule.js +25 -0
  45. package/dist/core/addon/item/attachable.d.ts +94 -0
  46. package/dist/core/addon/item/attachable.js +134 -0
  47. package/dist/core/addon/item/item.d.ts +19 -0
  48. package/dist/core/addon/item/item.js +26 -0
  49. package/dist/core/addon/manifest.d.ts +49 -0
  50. package/dist/core/addon/manifest.js +65 -0
  51. package/dist/core/addon/menuCategory.d.ts +6 -0
  52. package/dist/core/addon/menuCategory.js +7 -0
  53. package/dist/core/addon/recipe/baseRecipe.d.ts +12 -0
  54. package/dist/core/addon/recipe/baseRecipe.js +23 -0
  55. package/dist/core/addon/recipe/data.d.ts +66 -0
  56. package/dist/core/addon/recipe/data.js +74 -0
  57. package/dist/core/addon/recipe/furnace.d.ts +17 -0
  58. package/dist/core/addon/recipe/furnace.js +43 -0
  59. package/dist/core/addon/recipe/shaped.d.ts +26 -0
  60. package/dist/core/addon/recipe/shaped.js +53 -0
  61. package/dist/core/addon/recipe/shapeless.d.ts +18 -0
  62. package/dist/core/addon/recipe/shapeless.js +36 -0
  63. package/dist/core/biome/biome.d.ts +14 -0
  64. package/dist/core/biome/biome.js +23 -0
  65. package/dist/core/biome/biomeComponent.d.ts +46 -0
  66. package/dist/core/biome/biomeComponent.js +77 -0
  67. package/dist/core/block/basicBlock.d.ts +48 -0
  68. package/dist/core/block/basicBlock.js +105 -0
  69. package/dist/core/block/block.d.ts +32 -0
  70. package/dist/core/block/block.js +92 -0
  71. package/dist/core/block/blockComponent.d.ts +187 -0
  72. package/dist/core/block/blockComponent.js +500 -0
  73. package/dist/core/block/cropBlock.d.ts +4 -0
  74. package/dist/core/block/cropBlock.js +27 -0
  75. package/dist/core/block/geometryBlock.d.ts +12 -0
  76. package/dist/core/block/geometryBlock.js +16 -0
  77. package/dist/core/block/oreBlock.d.ts +9 -0
  78. package/dist/core/block/oreBlock.js +25 -0
  79. package/dist/core/block/rotatableBlock.d.ts +27 -0
  80. package/dist/core/block/rotatableBlock.js +107 -0
  81. package/dist/core/block/tileBlock.d.ts +17 -0
  82. package/dist/core/block/tileBlock.js +125 -0
  83. package/dist/core/entity/basicEntity.d.ts +111 -0
  84. package/dist/core/entity/basicEntity.js +170 -0
  85. package/dist/core/entity/behavior/followMob.d.ts +12 -0
  86. package/dist/core/entity/behavior/followMob.js +35 -0
  87. package/dist/core/entity/behavior/followParent.d.ts +7 -0
  88. package/dist/core/entity/behavior/followParent.js +15 -0
  89. package/dist/core/entity/behavior/randomStroll.d.ts +31 -0
  90. package/dist/core/entity/behavior/randomStroll.js +63 -0
  91. package/dist/core/entity/behavior/tempt.d.ts +73 -0
  92. package/dist/core/entity/behavior/tempt.js +134 -0
  93. package/dist/core/entity/bundles/BasicMoveBundle.js +8 -0
  94. package/dist/core/entity/bundles/basicBundle.d.ts +13 -0
  95. package/dist/core/entity/bundles/basicBundle.js +38 -0
  96. package/dist/core/entity/clientEntity.d.ts +20 -0
  97. package/dist/core/entity/clientEntity.js +37 -0
  98. package/dist/core/entity/componets/entityComponet.d.ts +202 -0
  99. package/dist/core/entity/componets/entityComponet.js +354 -0
  100. package/dist/core/entity/data/nativeEntityData.d.ts +9 -0
  101. package/dist/core/entity/data/nativeEntityData.js +38 -0
  102. package/dist/core/entity/dummyEntity.js +14 -0
  103. package/dist/core/entity/entity.d.ts +15 -0
  104. package/dist/core/entity/entity.js +11 -0
  105. package/dist/core/entity/nativeEntity.d.ts +8 -0
  106. package/dist/core/entity/nativeEntity.js +28 -0
  107. package/dist/core/entity/navigation/walk.d.ts +23 -0
  108. package/dist/core/entity/navigation/walk.js +56 -0
  109. package/dist/core/entity/projectile.d.ts +3 -0
  110. package/dist/core/entity/projectile.js +7 -0
  111. package/dist/core/factory/biomeFactory.d.ts +4 -0
  112. package/dist/core/factory/biomeFactory.js +13 -0
  113. package/dist/core/factory/blockFactory.d.ts +34 -0
  114. package/dist/core/factory/blockFactory.js +143 -0
  115. package/dist/core/factory/entityFactory.d.ts +17 -0
  116. package/dist/core/factory/entityFactory.js +75 -0
  117. package/dist/core/factory/featureFactory.d.ts +6 -0
  118. package/dist/core/factory/featureFactory.js +23 -0
  119. package/dist/core/factory/itemExtra.d.ts +8 -0
  120. package/dist/core/factory/itemExtra.js +9 -0
  121. package/dist/core/factory/itemFactory.d.ts +27 -0
  122. package/dist/core/factory/itemFactory.js +112 -0
  123. package/dist/core/factory/recipeFactory.d.ts +18 -0
  124. package/dist/core/factory/recipeFactory.js +39 -0
  125. package/dist/core/factory/uiFactory.d.ts +12 -0
  126. package/dist/core/factory/uiFactory.js +32 -0
  127. package/dist/core/feature/oreFeature.d.ts +10 -0
  128. package/dist/core/feature/oreFeature.js +11 -0
  129. package/dist/core/feature-rule/condition/biomeFilter.d.ts +26 -0
  130. package/dist/core/feature-rule/condition/biomeFilter.js +36 -0
  131. package/dist/core/feature-rule/condition/featureConditions.d.ts +27 -0
  132. package/dist/core/feature-rule/condition/featureConditions.js +37 -0
  133. package/dist/core/feature-rule/distribution/coordinateDistribution.d.ts +17 -0
  134. package/dist/core/feature-rule/distribution/coordinateDistribution.js +23 -0
  135. package/dist/core/feature-rule/distribution/featureDistribution.d.ts +31 -0
  136. package/dist/core/feature-rule/distribution/featureDistribution.js +41 -0
  137. package/dist/core/feature-rule/featureRule.d.ts +17 -0
  138. package/dist/core/feature-rule/featureRule.js +26 -0
  139. package/dist/core/index.d.ts +2241 -0
  140. package/dist/core/index.js +1 -0
  141. package/dist/core/item/armor.d.ts +21 -0
  142. package/dist/core/item/armor.js +90 -0
  143. package/dist/core/item/attachable.d.ts +6 -0
  144. package/dist/core/item/attachable.js +26 -0
  145. package/dist/core/item/food.d.ts +24 -0
  146. package/dist/core/item/food.js +38 -0
  147. package/dist/core/item/item.d.ts +36 -0
  148. package/dist/core/item/item.js +69 -0
  149. package/dist/core/item/itemComponents.d.ts +113 -0
  150. package/dist/core/item/itemComponents.js +225 -0
  151. package/dist/core/registry.d.ts +12 -0
  152. package/dist/core/registry.js +17 -0
  153. package/dist/core/texture.d.ts +18 -0
  154. package/dist/core/texture.js +38 -0
  155. package/dist/core/ui/buttonMapping.d.ts +97 -0
  156. package/dist/core/ui/buttonMapping.js +132 -0
  157. package/dist/core/ui/dataBindingObject.d.ts +97 -0
  158. package/dist/core/ui/dataBindingObject.js +134 -0
  159. package/dist/core/ui/elements/button.d.ts +21 -0
  160. package/dist/core/ui/elements/button.js +101 -0
  161. package/dist/core/ui/elements/collectionPanel.d.ts +12 -0
  162. package/dist/core/ui/elements/collectionPanel.js +53 -0
  163. package/dist/core/ui/elements/grid.d.ts +8 -0
  164. package/dist/core/ui/elements/grid.js +33 -0
  165. package/dist/core/ui/elements/image.d.ts +13 -0
  166. package/dist/core/ui/elements/image.js +55 -0
  167. package/dist/core/ui/elements/label.d.ts +14 -0
  168. package/dist/core/ui/elements/label.js +61 -0
  169. package/dist/core/ui/elements/panel.d.ts +11 -0
  170. package/dist/core/ui/elements/panel.js +49 -0
  171. package/dist/core/ui/elements/scrollingPanel.d.ts +14 -0
  172. package/dist/core/ui/elements/scrollingPanel.js +59 -0
  173. package/dist/core/ui/elements/stackPanel.d.ts +14 -0
  174. package/dist/core/ui/elements/stackPanel.js +37 -0
  175. package/dist/core/ui/elements/uiElement.d.ts +53 -0
  176. package/dist/core/ui/elements/uiElement.js +129 -0
  177. package/dist/core/ui/properties/control.d.ts +214 -0
  178. package/dist/core/ui/properties/control.js +286 -0
  179. package/dist/core/ui/properties/dataBinding.d.ts +6 -0
  180. package/dist/core/ui/properties/dataBinding.js +16 -0
  181. package/dist/core/ui/properties/factory.d.ts +37 -0
  182. package/dist/core/ui/properties/factory.js +62 -0
  183. package/dist/core/ui/properties/gridProp.d.ts +65 -0
  184. package/dist/core/ui/properties/gridProp.js +90 -0
  185. package/dist/core/ui/properties/input.d.ts +97 -0
  186. package/dist/core/ui/properties/input.js +134 -0
  187. package/dist/core/ui/properties/layout.d.ts +105 -0
  188. package/dist/core/ui/properties/layout.js +145 -0
  189. package/dist/core/ui/properties/scrollView.d.ts +105 -0
  190. package/dist/core/ui/properties/scrollView.js +145 -0
  191. package/dist/core/ui/properties/sound.d.ts +47 -0
  192. package/dist/core/ui/properties/sound.js +66 -0
  193. package/dist/core/ui/properties/sprite.d.ts +161 -0
  194. package/dist/core/ui/properties/sprite.js +222 -0
  195. package/dist/core/ui/properties/text.d.ts +135 -0
  196. package/dist/core/ui/properties/text.js +187 -0
  197. package/dist/core/ui/registry/uiSystemRegistry.d.ts +8 -0
  198. package/dist/core/ui/registry/uiSystemRegistry.js +26 -0
  199. package/dist/core/ui/systems/chest.d.ts +5 -0
  200. package/dist/core/ui/systems/chest.js +21 -0
  201. package/dist/core/ui/systems/containerUISystem.d.ts +67 -0
  202. package/dist/core/ui/systems/containerUISystem.js +214 -0
  203. package/dist/core/ui/systems/guidebook.d.ts +9 -0
  204. package/dist/core/ui/systems/guidebook.js +206 -0
  205. package/dist/core/ui/systems/serverForm.d.ts +13 -0
  206. package/dist/core/ui/systems/serverForm.js +116 -0
  207. package/dist/core/ui/systems/system.d.ts +16 -0
  208. package/dist/core/ui/systems/system.js +37 -0
  209. package/dist/oc/actor.js +44 -0
  210. package/dist/oc/core.js +201 -0
  211. package/dist/oc/index.d.ts +142 -0
  212. package/dist/oc/index.js +1 -0
  213. package/dist/oc/input.js +67 -0
  214. package/dist/oc/optional.js +31 -0
  215. package/doc/oc/index.md +0 -0
  216. package/doc/sapdon-ts.md +41 -5
  217. package/package.json +6 -8
  218. package/doc/cli.md +0 -24
  219. /package/doc/hello_sapdon/{sapdon/344/275/277/347/224/250/346/225/231/347/250/213.md" → hello_sapdon.md} +0 -0
package/README.md CHANGED
@@ -81,8 +81,9 @@ hello_sapdon/
81
81
 
82
82
  ## 📚 学习资源
83
83
 
84
- - [官方文档](https://docs.sapdon.org)
85
- - [示例模组仓库](https://github.com/Meteage/sapdon/examples)
84
+ - [官方文档](./doc/)
85
+ - [入门教程](./doc/hello_sapdon)
86
+ - [示例模组仓库](./examples/)
86
87
 
87
88
 
88
89
  ## ❓ 常见问题
@@ -0,0 +1,7 @@
1
+ export function projectCanBuild(projectPath: any, projectName: any): boolean;
2
+ export namespace scriptBundler {
3
+ let __projectPath: string;
4
+ function js(source: any, target: any): void;
5
+ function ts(source: any, target: any, tname?: string, sname?: string): Promise<void>;
6
+ }
7
+ export function buildProject(projectPath: string, projectName: string): Promise<void>;
@@ -0,0 +1,236 @@
1
+ import path from 'path';
2
+ import { pathNotExist, readFile, generateUUID, saveFile, copyFileSync, copyFolder, asyncImport } from "./utils.js";
3
+ import { generateAddonClient } from './load.js';
4
+ import { AddonManifestHeader, AddonManifestModule, AddonManifest, AddonManifestMetadata } from '../core/addon/manifest.js';
5
+ import { fileURLToPath } from 'url';
6
+ import fs from 'fs';
7
+ import { rollup } from 'rollup';
8
+ import commonjs from '@rollup/plugin-commonjs';
9
+ import { nodeResolve } from '@rollup/plugin-node-resolve';
10
+ import ts from '@rollup/plugin-typescript';
11
+ import { typescriptPaths as paths } from 'rollup-plugin-typescript-paths';
12
+ import json from '@rollup/plugin-json';
13
+ import { visualizer } from 'rollup-plugin-visualizer';
14
+ import { syncDevFilesClient } from './dev-server/syncFiles.js';
15
+ import { hmr } from './dev-server/hmr.js';
16
+ import cp from 'child_process';
17
+ // 获取当前文件的目录路径
18
+ const __filename = fileURLToPath(import.meta.url);
19
+ const __dirname = path.dirname(__filename);
20
+ //读取配置文件
21
+ // const pathConfig = JSON.parse(readFile(path.join(__dirname, "./build.config")))
22
+ const rollupIgnores = [
23
+ 'rollup',
24
+ 'typescript',
25
+ '@sapdon/core',
26
+ '@sapdon/cli',
27
+ '@minecraft',
28
+ ];
29
+ //脚本打包器
30
+ export const scriptBundler = {
31
+ __projectPath: path.join(__dirname, '../../'),
32
+ js: (source, target) => {
33
+ copyFolder(source, target);
34
+ },
35
+ //ts先通过rollup处理后再复制
36
+ ts: async (source, target, tname = 'index.js', sname = 'index.ts') => {
37
+ // console.log(source, target, tname)
38
+ const tmpFile = path.join(scriptBundler.__projectPath, '.tmp', `sapdon-${crypto.randomUUID()}.js`);
39
+ try {
40
+ const bundle = await rollup({
41
+ input: path.join(source, sname),
42
+ plugins: [
43
+ paths(),
44
+ nodeResolve({
45
+ preferBuiltins: true
46
+ }),
47
+ //@ts-ignore
48
+ ts({
49
+ tsconfig: path.join(scriptBundler.__projectPath, 'tsconfig.json'),
50
+ }),
51
+ //@ts-ignore
52
+ commonjs(),
53
+ //@ts-ignore
54
+ json(),
55
+ // visualizer({ open: true }), //可视化分析, 打包出问题取消注释这一行
56
+ ],
57
+ external(name) {
58
+ for (const candidate of rollupIgnores) {
59
+ if (name.includes(candidate)) {
60
+ return true;
61
+ }
62
+ }
63
+ return false;
64
+ }
65
+ });
66
+ await bundle.write({
67
+ file: tmpFile,
68
+ format: 'esm',
69
+ });
70
+ bundle.close();
71
+ fs.cpSync(tmpFile, path.join(target, tname));
72
+ }
73
+ catch (e) {
74
+ console.error(e);
75
+ }
76
+ finally {
77
+ fs.rmSync(tmpFile, {
78
+ recursive: true,
79
+ force: true
80
+ });
81
+ }
82
+ }
83
+ };
84
+ async function bundleScripts(projectPath, scriptPath, element) {
85
+ const sourcePath = path.join(projectPath, element.path);
86
+ const elementType = element.type || 'js';
87
+ scriptBundler.__projectPath = projectPath;
88
+ scriptBundler[elementType](sourcePath, scriptPath);
89
+ }
90
+ function preload(filePath) {
91
+ fs.appendFileSync(filePath, `
92
+ ;(async () => {
93
+ const { startDevServer } = await import('@sapdon/cli')
94
+ const { GRegistry, UISystemRegistry } = await import('@sapdon/core')
95
+ startDevServer(GRegistry, UISystemRegistry)
96
+ })();
97
+ `);
98
+ }
99
+ async function runOnChild(targetFilePath) {
100
+ const { promise, resolve } = Promise.withResolvers();
101
+ const child = cp.fork(targetFilePath, { stdio: 'inherit' });
102
+ child.once('message', message => {
103
+ if (message !== 'initialized') {
104
+ child.kill();
105
+ }
106
+ resolve();
107
+ });
108
+ await promise;
109
+ }
110
+ async function runScript(src) {
111
+ if (src.endsWith('.ts')) {
112
+ const randomName = crypto.randomUUID() + '.js';
113
+ const sourceDir = path.dirname(src);
114
+ const sourceFileName = src.replace(sourceDir, '');
115
+ const targetFilePath = path.join(sourceDir, randomName);
116
+ await scriptBundler.ts(sourceDir, sourceDir, randomName, sourceFileName);
117
+ preload(targetFilePath);
118
+ try {
119
+ await runOnChild(targetFilePath);
120
+ }
121
+ finally {
122
+ fs.rmSync(targetFilePath, { force: true });
123
+ }
124
+ return;
125
+ }
126
+ await runOnChild(src);
127
+ }
128
+ export function projectCanBuild(projectPath, projectName) {
129
+ console.log("开始构建项目");
130
+ console.log("项目路径:" + projectPath);
131
+ //检查项目是否存在
132
+ if (pathNotExist(projectPath)) {
133
+ console.log("项目不存在");
134
+ return false;
135
+ }
136
+ //检查项目是否有build.config文件
137
+ if (pathNotExist(path.join(projectPath, "build.config"))) {
138
+ console.log("项目没有build.config文件");
139
+ return false;
140
+ }
141
+ return true;
142
+ }
143
+ //构建项目
144
+ /**
145
+ * `client`
146
+ *
147
+ * 在使用前使用 `projectCanBuild` 确保项目可以构建
148
+ * @param {string} projectPath
149
+ * @param {string} projectName
150
+ */
151
+ export const buildProject = async (projectPath, projectName) => {
152
+ //读取build.config文件
153
+ const buildConfigPath = path.join(projectPath, "build.config");
154
+ const buildConfig = JSON.parse(readFile(buildConfigPath));
155
+ //console.log(buildConfig)
156
+ //读取mod.info文件
157
+ const modInfoPath = path.join(projectPath, "mod.info");
158
+ const modInfo = JSON.parse(readFile(modInfoPath));
159
+ const min_engine_version = versionStringToArray(modInfo.min_engine_version);
160
+ const buildDirPath = path.join(projectPath, buildConfig.defaultConfig.buildDir);
161
+ const buildBehDirPath = path.join(buildDirPath, `${projectName}_BP/`);
162
+ const buildResDirPath = path.join(buildDirPath, `${projectName}_RP/`);
163
+ //生成manifest.json文件
164
+ //判断manifest.json是否已经生成过了,生成过了就不用生成
165
+ const manifestPath = path.join(buildBehDirPath, "manifest.json");
166
+ if (pathNotExist(manifestPath)) {
167
+ const behManifest = generateBehManifest(modInfo.name, modInfo.description, modInfo.version, {
168
+ min_engine_version: min_engine_version,
169
+ }, buildConfig.defaultConfig.dependencies, buildConfig.defaultConfig.scriptEntry);
170
+ const resManifest = generateResManifest(modInfo.name, modInfo.description, modInfo.version, {
171
+ min_engine_version: min_engine_version,
172
+ }, []);
173
+ //BP
174
+ saveFile(path.join(buildBehDirPath, "manifest.json"), behManifest);
175
+ //RP
176
+ saveFile(path.join(buildResDirPath, "manifest.json"), resManifest);
177
+ }
178
+ //复制pack_icon.png
179
+ const packIconPath = path.join(projectPath, "pack_icon.png");
180
+ copyFileSync(packIconPath, path.join(buildBehDirPath, "pack_icon.png"));
181
+ copyFileSync(packIconPath, path.join(buildResDirPath, "pack_icon.png"));
182
+ const resources = buildConfig.resources;
183
+ resources.forEach(element => {
184
+ const sourcePath = path.join(projectPath, element.path);
185
+ copyFolder(sourcePath, buildResDirPath);
186
+ });
187
+ const scripts = buildConfig.scripts;
188
+ const scriptPath = path.join(buildBehDirPath, "scripts/");
189
+ for (const element of scripts) {
190
+ await bundleScripts(projectPath, scriptPath, element);
191
+ }
192
+ const absoluteModPath = path.join(projectPath, buildConfig.defaultConfig.buildEntry);
193
+ await runScript(absoluteModPath);
194
+ //只有当buildMode为development时才加载用户modjs文件
195
+ if (buildConfig.defaultConfig.buildMode === "development") {
196
+ //动态加载用户modjs文件
197
+ await generateAddonClient(absoluteModPath, buildDirPath, projectName);
198
+ }
199
+ syncDevFilesClient(projectPath, projectName);
200
+ if (buildConfig.useHMR === true) {
201
+ hmr(projectPath, projectName);
202
+ }
203
+ };
204
+ function versionStringToArray(versionString) {
205
+ // 使用 split 方法将字符串按 '.' 分割成数组
206
+ const parts = versionString.split('.');
207
+ // 使用 map 方法将每个部分转换为数字
208
+ const versionArray = parts.map(part => Number(part));
209
+ return versionArray;
210
+ }
211
+ const generateBehManifest = (name, description, version, options = {}, dependencies = [], entry) => {
212
+ console.log("开始生成behavior_packs/manifest.json");
213
+ console.log("Entry:", entry);
214
+ const header = new AddonManifestHeader((name + "_BP"), description, versionStringToArray(version), generateUUID(), options);
215
+ const module = new AddonManifestModule("行为模块", "data", generateUUID(), versionStringToArray(version));
216
+ const script_module = new AddonManifestModule("脚本模块", "script", generateUUID(), versionStringToArray(version));
217
+ if (entry) {
218
+ script_module.entry = entry;
219
+ }
220
+ const metadata = new AddonManifestMetadata(["@sapdon"], "MIT", {
221
+ "sapdon": ["1.0.0"]
222
+ }, "addon", "https://github.com/junjun260/sapdon");
223
+ const manifest = new AddonManifest(2, //格式版本
224
+ header, [module, script_module], dependencies, null, metadata);
225
+ return JSON.stringify(manifest, null, 2);
226
+ };
227
+ const generateResManifest = (name, description, version, options = {}, dependencies = []) => {
228
+ const header = new AddonManifestHeader((name + "_RP"), description, versionStringToArray(version), generateUUID(), options);
229
+ const module = new AddonManifestModule("资源模块", "resources", generateUUID(), versionStringToArray(version));
230
+ const metadata = new AddonManifestMetadata(["@sapdon"], "MIT", {
231
+ "sapdon": ["1.0.0"]
232
+ }, "addon", "https://github.com/junjun260/sapdon");
233
+ const manifest = new AddonManifest(2, //格式版本
234
+ header, [module], [], null, metadata);
235
+ return JSON.stringify(manifest, null, 2);
236
+ };
@@ -0,0 +1,2 @@
1
+ export function cliRequest(path: any, ...params: any[]): Promise<void>;
2
+ export function writeAddon(modPath: any, buildPath: any, projectName: any): Promise<void>;
@@ -0,0 +1,21 @@
1
+ import { devServerConfig } from './config.js';
2
+ import { ServerHandles } from './handles.js';
3
+ const { port } = devServerConfig;
4
+ export async function cliRequest(path, ...params) {
5
+ try {
6
+ await fetch(`http://localhost:${port}/${path}`, {
7
+ method: 'POST',
8
+ headers: {
9
+ 'Content-Type': 'application/json'
10
+ },
11
+ body: JSON.stringify(params)
12
+ });
13
+ }
14
+ catch (error) {
15
+ console.error(error);
16
+ console.error('尝试在构建脚本中使用 server.startDevServer() 启动开发服务器');
17
+ }
18
+ }
19
+ export async function writeAddon(modPath, buildPath, projectName) {
20
+ await cliRequest(ServerHandles.WRITE_ADDON, modPath, buildPath, projectName);
21
+ }
@@ -0,0 +1,3 @@
1
+ export namespace devServerConfig {
2
+ let port: number;
3
+ }
@@ -0,0 +1,3 @@
1
+ export const devServerConfig = {
2
+ port: 49037
3
+ };
@@ -0,0 +1,4 @@
1
+ export declare enum ServerHandles {
2
+ WRITE_ADDON = "write-addon",
3
+ WRITE_GAME_DEV = "write-dev"
4
+ }
@@ -0,0 +1,5 @@
1
+ export var ServerHandles;
2
+ (function (ServerHandles) {
3
+ ServerHandles["WRITE_ADDON"] = "write-addon";
4
+ ServerHandles["WRITE_GAME_DEV"] = "write-dev";
5
+ })(ServerHandles || (ServerHandles = {}));
@@ -0,0 +1 @@
1
+ export function hmr(projectPath: any, projectName: any): void;
@@ -0,0 +1,13 @@
1
+ import fs from 'fs';
2
+ import lodash from 'lodash';
3
+ import { buildProject } from '../build.js';
4
+ const debounce = lodash.debounce;
5
+ export function hmr(projectPath, projectName) {
6
+ // fs.watch(projectPath, { recursive: true }, debounce(async (eventType, filename) => {
7
+ // if (filename.endsWith('.js') || filename.endsWith('.ts')) {
8
+ // process.stdout.write(`File ${filename} changed, reloading...\r`)
9
+ // await buildProject(projectPath, projectName)
10
+ // console.log(`Reloaded ${filename}`)
11
+ // }
12
+ // }), 1000)
13
+ }
@@ -0,0 +1,4 @@
1
+ import { ServerHandles } from "./handles.js";
2
+ import { server } from "./server.js";
3
+ export declare function startDevServer(GRegistry: any, UIRegistry: any): void;
4
+ export { server, ServerHandles, };
@@ -0,0 +1,17 @@
1
+ import { generateAddon } from "../load.js";
2
+ import { ServerHandles } from "./handles.js";
3
+ import { server } from "./server.js";
4
+ import { syncDevFilesServer } from "./syncFiles.js";
5
+ function startup(GRegistry, UIRegistry) {
6
+ server.bootstrap();
7
+ server.handle(ServerHandles.WRITE_ADDON, generateAddon(GRegistry, UIRegistry));
8
+ server.handle(ServerHandles.WRITE_GAME_DEV, syncDevFilesServer);
9
+ }
10
+ export function startDevServer(GRegistry, UIRegistry) {
11
+ if (server.isListening()) {
12
+ return;
13
+ }
14
+ startup(GRegistry, UIRegistry);
15
+ process.send('initialized');
16
+ }
17
+ export { server, ServerHandles, };
@@ -0,0 +1,14 @@
1
+ import http from 'http';
2
+ type ServerHandler = (...args: Transferable[]) => void;
3
+ type HandlerInterceptor = (handler: ServerHandler) => ServerHandler;
4
+ declare class DevelopmentServer {
5
+ readonly cliServerHandlers: Map<string, ServerHandler>;
6
+ listening: boolean;
7
+ isListening(): boolean;
8
+ bootstrap(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
9
+ handle(url: string, handler: ServerHandler): void;
10
+ getHandler(url: string): ServerHandler | undefined;
11
+ interceptHandler(url: string, interceptor: HandlerInterceptor): ServerHandler;
12
+ }
13
+ export declare const server: DevelopmentServer;
14
+ export {};
@@ -0,0 +1,59 @@
1
+ import http from 'http';
2
+ import { devServerConfig } from './config.js';
3
+ const { port } = devServerConfig;
4
+ class DevelopmentServer {
5
+ cliServerHandlers = new Map();
6
+ listening = false;
7
+ isListening() {
8
+ return this.listening;
9
+ }
10
+ bootstrap() {
11
+ this.listening = true;
12
+ const svr = http.createServer(async (req, res) => {
13
+ const handler = this.cliServerHandlers.get((req.url ?? '/').slice(1));
14
+ if (handler) {
15
+ try {
16
+ const { promise, resolve, reject } = Promise.withResolvers();
17
+ let buf = Buffer.alloc(0);
18
+ req.on('data', chunk => buf = Buffer.concat([buf, chunk]));
19
+ req.on('end', () => {
20
+ try {
21
+ resolve(JSON.parse(buf));
22
+ }
23
+ catch (error) {
24
+ reject(error);
25
+ }
26
+ });
27
+ await handler(...await promise);
28
+ }
29
+ catch (error) {
30
+ console.error(error);
31
+ res.writeHead(500);
32
+ res.end();
33
+ return;
34
+ }
35
+ res.writeHead(200);
36
+ res.end();
37
+ }
38
+ else {
39
+ res.writeHead(404);
40
+ res.end();
41
+ }
42
+ }).listen(port, () => console.log(`Dev Server listening on port ${port}`));
43
+ svr.on('error', () => this.listening = false);
44
+ return svr;
45
+ }
46
+ handle(url, handler) {
47
+ this.cliServerHandlers.set(url, handler);
48
+ }
49
+ getHandler(url) {
50
+ return this.cliServerHandlers.get(url);
51
+ }
52
+ interceptHandler(url, interceptor) {
53
+ const currentHandler = this.getHandler(url) ?? Function.prototype;
54
+ const newHandler = interceptor(currentHandler);
55
+ this.cliServerHandlers.set(url, newHandler);
56
+ return newHandler;
57
+ }
58
+ }
59
+ export const server = new DevelopmentServer();
@@ -0,0 +1,3 @@
1
+ export function syncDevFilesClient(projectPath: any, projectName: any): Promise<void>;
2
+ export function syncDevFilesServer(projectPath: any, projectName: any): Promise<void>;
3
+ export function writeLib(projectPath: any): Promise<void>;
@@ -0,0 +1,45 @@
1
+ import { readFileSync } from "fs";
2
+ import { cliRequest } from "./client.js";
3
+ import { copyFolder, dirname } from "../utils.js";
4
+ import path from "path";
5
+ import fs from 'fs';
6
+ import { ServerHandles } from "./handles.js";
7
+ export async function syncDevFilesClient(projectPath, projectName) {
8
+ await cliRequest(ServerHandles.WRITE_GAME_DEV, projectPath, projectName);
9
+ }
10
+ export async function syncDevFilesServer(projectPath, projectName) {
11
+ const buildConfig = JSON.parse(readFileSync(path.join(projectPath, "build.config")));
12
+ const buildDir = path.join(projectPath, buildConfig.defaultConfig.buildDir);
13
+ const buildBehDirPath = path.join(buildDir, `${projectName}_BP`);
14
+ const buildResDirPath = path.join(buildDir, `${projectName}_RP`);
15
+ copyFolder(buildBehDirPath, path.join(buildConfig.mojangPath, "development_behavior_packs/", `${projectName}_BP/`));
16
+ copyFolder(buildResDirPath, path.join(buildConfig.mojangPath, "development_resource_packs/", `${projectName}_RP/`));
17
+ }
18
+ export async function writeLib(projectPath) {
19
+ const projectModules = path.join(projectPath, "node_modules");
20
+ const rootDir = path.join(dirname(import.meta), '../');
21
+ const corePath = path.join(rootDir, 'core');
22
+ const cliPath = path.join(rootDir, 'cli');
23
+ const ocPath = path.join(rootDir, 'oc');
24
+ const targetCorePath = path.join(projectModules, '@sapdon/core');
25
+ const targetCliPath = path.join(projectModules, '@sapdon/cli');
26
+ const targetOcPath = path.join(projectModules, '@sapdon/oc');
27
+ fs.cpSync(corePath, targetCorePath, { recursive: true, force: true });
28
+ fs.cpSync(cliPath, targetCliPath, { recursive: true, force: true });
29
+ fs.cpSync(ocPath, targetOcPath, { recursive: true, force: true });
30
+ fs.writeFileSync(path.join(targetCorePath, 'package.json'), JSON.stringify({
31
+ name: '@sapdon/core',
32
+ main: 'index.js',
33
+ version: '1.0.0',
34
+ }));
35
+ fs.writeFileSync(path.join(targetCliPath, 'package.json'), JSON.stringify({
36
+ name: '@sapdon/cli',
37
+ main: 'index.js',
38
+ version: '1.0.0',
39
+ }));
40
+ fs.writeFileSync(path.join(targetOcPath, 'package.json'), JSON.stringify({
41
+ name: '@sapdon/oc',
42
+ main: 'index.js',
43
+ version: '1.0.0',
44
+ }));
45
+ }
@@ -0,0 +1,20 @@
1
+ import http from 'http';
2
+
3
+ type ServerHandler = (...args: (Uint8Array & string)[]) => void | Promise<void>;
4
+ type HandlerInterceptor = (handler: ServerHandler) => ServerHandler;
5
+ declare class DevelopmentServer {
6
+ readonly cliServerHandlers: Map<string, ServerHandler>;
7
+ listening: boolean;
8
+ isListening(): boolean;
9
+ bootstrap(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
10
+ handle(url: string, handler: ServerHandler): void;
11
+ getHandler(url: string): ServerHandler | undefined;
12
+ interceptHandler(url: string, interceptor: HandlerInterceptor): ServerHandler;
13
+ }
14
+ declare const server: DevelopmentServer;
15
+
16
+ declare namespace client {
17
+ function call(name: any, ...args: any[]): Promise<void>;
18
+ }
19
+
20
+ export { client, server as devServer };
@@ -0,0 +1,90 @@
1
+ import http from 'http';
2
+
3
+ const devServerConfig = {
4
+ port: 49037
5
+ };
6
+
7
+ const {
8
+ port: port$1
9
+ } = devServerConfig;
10
+
11
+ async function cliRequest(path, ...params) {
12
+ try {
13
+ await fetch(`http://localhost:${port$1}/${path}`, {
14
+ method: 'POST',
15
+ headers: {
16
+ 'Content-Type': 'application/json'
17
+ },
18
+ body: JSON.stringify(params)
19
+ });
20
+ } catch (error) {
21
+ console.error(error);
22
+ console.error('尝试在构建脚本中使用 server.startDevServer() 启动开发服务器');
23
+ }
24
+ }
25
+
26
+ const { port } = devServerConfig;
27
+ class DevelopmentServer {
28
+ cliServerHandlers = new Map();
29
+ listening = false;
30
+ isListening() {
31
+ return this.listening;
32
+ }
33
+ bootstrap() {
34
+ this.listening = true;
35
+ const svr = http.createServer(async (req, res) => {
36
+ const handler = this.cliServerHandlers.get((req.url ?? '/').slice(1));
37
+ if (handler) {
38
+ try {
39
+ const { promise, resolve, reject } = Promise.withResolvers();
40
+ let buf = Buffer.alloc(0);
41
+ req.on('data', chunk => buf = Buffer.concat([buf, chunk]));
42
+ req.on('end', () => {
43
+ try {
44
+ resolve(JSON.parse(buf));
45
+ }
46
+ catch (error) {
47
+ reject(error);
48
+ }
49
+ });
50
+ await handler(...await promise);
51
+ }
52
+ catch (error) {
53
+ console.error(error);
54
+ res.writeHead(500);
55
+ res.end();
56
+ return;
57
+ }
58
+ res.writeHead(200);
59
+ res.end();
60
+ }
61
+ else {
62
+ res.writeHead(404);
63
+ res.end();
64
+ }
65
+ }).listen(port, () => console.log(`Dev Server listening on port ${port}`));
66
+ svr.on('error', () => this.listening = false);
67
+ return svr;
68
+ }
69
+ handle(url, handler) {
70
+ this.cliServerHandlers.set(url, handler);
71
+ }
72
+ getHandler(url) {
73
+ return this.cliServerHandlers.get(url);
74
+ }
75
+ interceptHandler(url, interceptor) {
76
+ const currentHandler = this.getHandler(url) ?? Function.prototype;
77
+ const newHandler = interceptor(currentHandler);
78
+ this.cliServerHandlers.set(url, newHandler);
79
+ return newHandler;
80
+ }
81
+ }
82
+ const server = new DevelopmentServer();
83
+
84
+ const client = {
85
+ call: async (name, ...args) => {
86
+ return await cliRequest(name, ...args)
87
+ }
88
+ };
89
+
90
+ export { client, server as devServer };
@@ -0,0 +1,9 @@
1
+ export function initMojangPath(projectPath: any): void;
2
+ export function initProject(projectPath: any, data: any): void;
3
+ export function initNPMProject(projectPath: any, data: any): void;
4
+ export function readPackageJson(dir: any): {
5
+ name: string;
6
+ description: any;
7
+ author: any;
8
+ version: any;
9
+ } | null;