-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmapBreaker.js
337 lines (285 loc) · 11.3 KB
/
SmapBreaker.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
var FS = require('fs')
var Path = require('path')
var InvalidCharConverter = require('./InvalidCharConverter')
var projectPath
var smapJson
//var exportTexts
var localResourceDataDict = {}
function SerializeGuid(guid) {
let s = guid[0] + ',' + guid[1] + ',' + guid[2] + ',' + guid[3]
return s
}
function GuidIsZero(guid) {
for (let i = 0; i < 4; i++) {
if (guid[i] != 0) {
return false
}
}
return true
}
function GuidEqual(guid0, guid1) {
for (let i = 0; i < 4; i++) {
if (guid0[i] != guid1[i]) {
return false
}
}
return true
}
function MakeResourceFileOrFolder(resourceNode, nodeDict, folderNodeToPathDict) {
let parentNodeGuid = resourceNode.parentGuid
let dir
if (!GuidIsZero(parentNodeGuid)) {
//我不是根目录的儿子,所以要检查父节点对应的文件夹有没有准备好
let parentId = SerializeGuid(parentNodeGuid)
let parentNode = nodeDict[parentId]
dir = folderNodeToPathDict[parentId]
if (!dir) {
//父节点对应的文件夹还没创建出来,所以先让父节点创建文件夹
dir = MakeResourceFileOrFolder(parentNode, nodeDict, folderNodeToPathDict)
}
}
//父节点对应的文件夹应该已经准备好了,可以创建我自己了
if (GuidIsZero(parentNodeGuid)) {
dir = Path.join(projectPath, 'Resource')
}
let id = SerializeGuid(resourceNode.guid)
let convertedFilename = InvalidCharConverter.ConvertInvalidCharInString(resourceNode.name)
if (convertedFilename) console.log(`原资源名"${resourceNode.name}"不能直接用于文件名,已转换成"${convertedFilename}"`)
let filenameWithoutExt = convertedFilename || resourceNode.name
if (resourceNode.type == "kGeneric") { //是文件夹
let path = Path.join(dir, filenameWithoutExt)
if (FS.existsSync(path)) {
console.error(`资源文件夹重名,不影响打散,聚合时将只保留一个。Path:`, path)
} else {
FS.mkdirSync(path)
}
folderNodeToPathDict[id] = path
return path //返回路径
}
//是文件
let path = Path.join(dir, filenameWithoutExt + '.' + resourceNode.type)
let localResourceData = localResourceDataDict[id]
if (localResourceData) {
//有本地资源数据,先写入
dataPath = path + '.data'
if (FS.existsSync(dataPath)) {
console.error(`资源二进制数据文件重名,该文件无法导出。Path:`, dataPath)
} else {
FS.writeFileSync(dataPath, JSON.stringify(localResourceData))
}
}
//清除parentGuid,防止每次导出都变化
resourceNode.parentGuid = [-1, -1, -1, -1]
//写入res文件
if (FS.existsSync(path)) {
let filenameWithGuid = filenameWithoutExt + JSON.stringify(resourceNode.guid) + '.' + resourceNode.type
console.error(`资源文件重名,已重命名为${filenameWithGuid},引用关系不会受影响,但资源路径会改变。Path:`, path)
path = Path.join(dir, filenameWithGuid)
}
FS.writeFileSync(path, JSON.stringify(resourceNode))
//不是文件夹就不返回路径了
return null
}
function ExportAllResources() {
//拆解本地Resource,记录入Dict
let localResourceDatas = smapJson.resources[0].default
localResourceDatas.forEach(resData => {
localResourceDataDict[SerializeGuid(resData.guid)] = resData
});
//生成Resource里的文件夹结构
let resourceCart = smapJson.resourceCart //数组,元素是Resource空间里的节点
let nodeDict = {} //nodeid→node的Json
let folderNodeToPathDict = {} //文件夹nodeid→文件夹路径
resourceCart.forEach(resourceNode => {
let id = SerializeGuid(resourceNode.guid)
nodeDict[id] = resourceNode
});
resourceCart.forEach(resourceNode => {
MakeResourceFileOrFolder(resourceNode, nodeDict, folderNodeToPathDict)
});
//导出autogenerated Resource
let autogenResourceDatas = smapJson.resources[1]?.autogenerated
if (autogenResourceDatas && autogenResourceDatas.length > 0) {
//创建文件夹
let autogenResourcePath = Path.join(projectPath, 'AutoGeneratedResource')
autogenResourceDatas.forEach(resData => {
let resFilename = `[${resData.guid}].${resData.revisions[0].name}.${resData.revisions[0].type}.data` //形如[25751272,3868476979,2704663161,877037577].LowPolyMesh.kMesh.data
FS.writeFileSync(Path.join(autogenResourcePath, resFilename), JSON.stringify(resData))
})
}
}
var archetypeTreeRoot //里面是树形结构的"极简node"
var archetypeIdToSimpleNode = {} //id→SimpleNode
var archetypeNodeDict = {} //id→ArchetypeNode
function CollectNodesInRealArchetype(list, curSimpleNode) {
let curNode = archetypeNodeDict[curSimpleNode.id]
list.push(curNode)
curSimpleNode.children.forEach(childSimpleNode => {
CollectNodesInRealArchetype(list, childSimpleNode)
})
}
function TraverseTreeAndFindArchetypeAndExport(simpleNode, curPath) {
let node = archetypeNodeDict[simpleNode.id]
let convertedFilename = InvalidCharConverter.ConvertInvalidCharInString(node.name)
if (convertedFilename) console.log(`原Archetype名"${node.name}"不能直接用于文件名,已转换成"${convertedFilename}"`)
let filenameWithoutExt = convertedFilename || node.name
if (node.class == "cFolderObject") {
//是文件夹,创建文件夹
let myPath = Path.join(curPath, filenameWithoutExt)
FS.mkdirSync(myPath)
//继续深度优先遍历
simpleNode.children.forEach(simnode => {
TraverseTreeAndFindArchetypeAndExport(simnode, myPath)
})
} else {
//不是文件夹,说明是真Archetype,整体导出
let list = [] //该真Archetype包含的所有节点
CollectNodesInRealArchetype(list, simpleNode)//把树形结构转换成包含节点Data的list
//清除parentGuid,防止每次导出都变化
list[0].parentGuid = [-1, -1, -1, -1]
//输出list
let myPath = Path.join(curPath, filenameWithoutExt + '.arch')
FS.writeFileSync(myPath, JSON.stringify(list))
}
}
function ExportAllArchetypes() {
let archetypeSpace = smapJson.mapdata[0]
let archetypeNodeDatas = archetypeSpace.ObjectsData
//还原archetypeTree树形结构
archetypeNodeDatas.forEach(node => {
let id = SerializeGuid(node.guid)
archetypeNodeDict[id] = node
let simpleNode = archetypeIdToSimpleNode[id]
if (!simpleNode) {
simpleNode = { //用来还原树形结构的简单node类
id: id,
children: []
}
archetypeIdToSimpleNode[id] = simpleNode
}
if (GuidIsZero(node.parentGuid)) {
//这是workspace根节点
archetypeTreeRoot = simpleNode //把根节点记下来
}
else {
//如果不是根节点,则把自己塞到父节点simnode的children中
let parentId = SerializeGuid(node.parentGuid)
let parentSimpleNode = archetypeIdToSimpleNode[parentId]
if (!parentSimpleNode) {
//如果父节点simnode还没有创建,则帮它创建
parentSimpleNode = {
id: parentId,
children: []
}
archetypeIdToSimpleNode[parentId] = parentSimpleNode
}
parentSimpleNode.children.push(simpleNode) //把自己塞入父节点simnode的children里
}
})
//树形结构还原完毕
//从根节点开始,寻找所有的真正Archetype
archetypeTreeRoot.children.forEach(simnode => {
TraverseTreeAndFindArchetypeAndExport(simnode, Path.join(projectPath, 'Archetype'))
})
}
function ExportAllWorlds() {
//目前只有1个World
let worldSpace = smapJson.mapdata[1]
FS.writeFileSync(Path.join(projectPath, 'World/world'), JSON.stringify(worldSpace))
}
function DeletePath(path) {
var files = [];
if (FS.existsSync(path)) {
files = FS.readdirSync(path);
files.forEach(function (file, index) {
var curPath = path + "/" + file;
if (FS.statSync(curPath).isDirectory()) { // recurse
DeletePath(curPath);
} else { // delete file
FS.unlinkSync(curPath);
}
});
FS.rmdirSync(path);
}
}
const ScriptClasses = ["cScriptObject", "cModuleScriptObject"]
function IsNodeScriptClass(node) {
for (let i = 0; i < ScriptClasses.length; i++) {
const c = ScriptClasses[i];
if (c == node.class) return true
}
return false
}
const luaPlaceholder = FS.readFileSync('./lua_placeholder.txt').toString()
function ReplaceAllLuaData() {
let n = 0
for (let space = 0; space < 2; space++) {
smapJson.mapdata[space].ObjectsData.forEach(node => {
if (IsNodeScriptClass(node)) {
node.components.forEach(comp => {
if (comp.class == 'sLuaComponent') {
comp.data.m_luaContent = luaPlaceholder.replace('<class>', '"' + node.class + '"').replace('<guid>', '[' + node.guid + ']')
n++
}
})
}
})
}
console.log('检测到Script, ModuleScript节点', n, '个')
}
function ExportCompatibilityInfo() {
let compatibilityInfo = {}
compatibilityInfo.sandbox_archive_version = smapJson.sandbox_archive_version
let len = smapJson.mapdata[0].ObjectsData.length
for (let i = 0; i < len; i++) {
const node = smapJson.mapdata[0].ObjectsData[i];
if (node.class == "cWorkspace") {
compatibilityInfo.archetypeWorldspace = node
break
}
}
FS.writeFileSync(Path.join(projectPath, 'compatibility_info.json'), JSON.stringify(compatibilityInfo))
}
function Disassemble(projectPath) {
if (!FS.existsSync(projectPath)) {
FS.mkdirSync(projectPath)
}
//1.删除Resource, Archetype, World文件夹
let arr = ['Resource', 'AutoGeneratedResource', 'Archetype', 'World']
for (let i = 0; i < arr.length; i++) {
try {
let path = Path.join(projectPath, arr[i])
if (FS.existsSync(path)) {
DeletePath(path)
}
} catch (error) {
console.error(error)
}
}
// arr.push('Lua', 'Csv') //无需创建这俩文件夹
for (let i = 0; i < arr.length; i++) {
try {
let path = Path.join(projectPath, arr[i])
if (!FS.existsSync(path)) FS.mkdirSync(path)
} catch (error) {
console.error(error)
}
}
//2.将lua、csv内容替换成注释,但不单独导出它们
ReplaceAllLuaData()
//3.1.导出 Resource
ExportAllResources(projectPath)
//3.2.导出 Archetype
ExportAllArchetypes()
//3.3.导出 World
ExportAllWorlds()
//4.导出兼容性信息
ExportCompatibilityInfo()
}
exports.BreakSmap = function (smapPath, inputProjectPath) {
projectPath = inputProjectPath
let rawdata = FS.readFileSync(smapPath)
//smapJson = JSON.parse(rawdata)
smapJson = eval('(' + rawdata + ')')
Disassemble(projectPath)
}