154 lines
13 KiB
HTML
154 lines
13 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>关卡 JSON 转换</title>
|
||
<style>
|
||
*{box-sizing:border-box}body{font:14px system-ui,sans-serif;color:#243238;background:#f5f7f8;margin:0}main{max-width:900px;margin:32px auto;padding:24px}h1{font-size:24px}section{padding:20px 0;border-top:1px solid #d3dce0}label{display:block;margin:12px 0}select,input,button{font:inherit;max-width:100%}select,button{padding:8px 12px;border:1px solid #aabcc2;border-radius:4px;background:white}button{cursor:pointer}button:disabled{opacity:.4;cursor:default}pre{white-space:pre-wrap;overflow-wrap:anywhere;line-height:1.6}textarea{width:100%;height:140px;font:13px monospace;border:1px solid #aabcc2;border-radius:4px;padding:12px}#download{background:#087d83;color:white}small{color:#586971}footer{padding-top:16px}a{color:#087d83}
|
||
</style>
|
||
<style>
|
||
.mapping-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,230px),1fr));gap:12px;margin:12px 0}.mapping-row{border-bottom:1px solid #d3dce0;padding:8px 0}.mapping-row strong{display:block;overflow-wrap:anywhere}.mapping-row label{margin:8px 0}.mapping-row select{width:100%}.mapping-note{color:#586971;line-height:1.6}#failure{color:#a52a27;white-space:pre-wrap;overflow-wrap:anywhere;line-height:1.6}#failure:empty{display:none}
|
||
</style>
|
||
<main><h1>关卡 JSON 转换</h1><section>
|
||
<label>源关卡 JSON <input id="files" type="file" accept=".json,application/json" multiple></label>
|
||
<label>源关卡文件夹 <input id="folder" type="file" webkitdirectory multiple></label>
|
||
<p id="summary" role="status">未选择文件</p>
|
||
<label>待查看文件 <select id="selected"></select></label>
|
||
<label>源角度 0° 朝向 <select id="zero"><option value="north">上</option><option value="east">右</option><option value="south">下</option><option value="west">左</option></select></label>
|
||
<h2>源类型与目标对象</h2>
|
||
<form id="add-mapping">
|
||
<label>源分类 <select id="new-group"><option value="animalGroup">动物(animalGroup)</option><option value="obstacleGroup">机关(obstacleGroup)</option></select></label>
|
||
<label>源类型 <input id="new-type" type="text" maxlength="64" required aria-label="新增源类型"></label>
|
||
<label>目标对象 <select id="new-target"></select></label>
|
||
<button type="submit">新增映射</button>
|
||
</form>
|
||
<p id="mapping-status" role="status"></p>
|
||
<div id="mapping-list" class="mapping-grid"></div>
|
||
<p class="mapping-note">默认对应:1 → 箭、100 炸弹熊 → 爆破箭矢、101 计时狮子 → 计时炸弹箭矢、103 大象 → 张飞、106 沉睡河马 → 张角、107 魅力羊 → 黄忠、108 撞击牛 → 赵云、109 羚羊 → 攻城车、112 小鸡 → 滚石。爆破箭矢固定 3 次碰撞;大象对应张飞的 3×2 占格,战斗能力采用张飞配置。计时狮子的 boomTime 转为计时附加参数,但可能被目标模板和难度覆盖,须核对最终倒计时。其他特殊参数仍需单独适配;占格或参数不兼容时转换失败。</p>
|
||
<label>对象映射 JSON <textarea id="mapping" spellcheck="false" aria-label="对象映射 JSON"></textarea></label>
|
||
<button id="convert">转换</button> <button id="download" disabled>下载当前单关 JSON</button> <button id="download-all" disabled>下载批量结果 ZIP</button> <button id="report" disabled>下载转换报告</button>
|
||
</section><section><h2>转换结果</h2><p id="failure" role="status"></p><pre id="result" role="status">未选择文件</pre></section><footer><a href="使用说明.md" target="_blank">使用说明</a></footer></main>
|
||
<script src="fflate.js"></script><script src="converter.cjs"></script><script>
|
||
'use strict';
|
||
const $=id=>document.getElementById(id);let results=[],generation=0,sourceFiles=[];
|
||
$('mapping').value=JSON.stringify(PiggyConverter.defaults,null,2);
|
||
const discoveredTypes=new Map();
|
||
const groupNames={animalGroup:'动物',obstacleGroup:'机关'};
|
||
const animalNames={1:'普通猪',100:'炸弹熊',101:'计时狮子',103:'大象',106:'沉睡河马',107:'魅力羊',108:'撞击牛',109:'羚羊',112:'小鸡'};
|
||
const obstacleNames={200:'捕猎狼',204:'笼子',205:'推车',206:'地鼠',207:'泡泡',209:'云雾'};
|
||
function targetLabel(target){return target.name+' · '+target.id+'('+target.length+'×'+target.width+')';}
|
||
function sourceLabel(collection,type){
|
||
const names=collection==='animalGroup'?animalNames:obstacleNames;
|
||
const name=Object.hasOwn(names,type)?' · '+names[type]:'';
|
||
const footprint=PiggyConverter.defaults[collection]?.[type];
|
||
return groupNames[collection]+' · '+type+name+(footprint?'('+footprint.length+'×'+footprint.width+')':'(占格待确认)');
|
||
}
|
||
for(const target of PiggyConverter.targetTypes)$('new-target').add(new Option(targetLabel(target),String(target.id)));
|
||
$('add-mapping').onsubmit=event=>{
|
||
event.preventDefault();
|
||
try{
|
||
const group=$('new-group').value,type=$('new-type').value.trim(),target=PiggyConverter.targetTypes.find(item=>item.id===Number($('new-target').value));
|
||
if(!type||['__proto__','prototype','constructor'].includes(type))throw Error('源类型无效');
|
||
if(!/^(0|[1-9]\d*)$/.test(type)||!Number.isSafeInteger(Number(type)))throw Error('源类型必须是非负整数');
|
||
const mapping=JSON.parse($('mapping').value);if(!mapping||typeof mapping!=='object'||Array.isArray(mapping))throw Error('映射 JSON 必须是对象');
|
||
const current=mapping[group];if(current&&(!current||typeof current!=='object'||Array.isArray(current)))throw Error('源分类映射必须是对象');
|
||
if(Object.prototype.hasOwnProperty.call(current||{},type)&¤t[type])throw Error('该源类型已有映射,请在列表中修改');
|
||
mapping[group]||={};const {name,...rule}=target;
|
||
mapping[group][type]=rule;discoveredTypes.set(group+':'+type,{collection:group,type});
|
||
$('mapping').value=JSON.stringify(mapping,null,2);invalidate();renderMappings();$('new-type').value='';$('mapping-status').textContent='已新增 '+groupNames[group]+' · '+type+' → '+target.name+',请重新转换';
|
||
}catch(error){$('mapping-status').textContent='新增失败:'+error.message;}
|
||
};
|
||
function renderMappings(){
|
||
let mapping;try{mapping=JSON.parse($('mapping').value);}catch{return;}
|
||
const rows=new Map(discoveredTypes);
|
||
for(const group of Object.keys(groupNames))for(const type of Object.keys(mapping?.[group]||{}))rows.set(group+':'+type,{collection:group,type});
|
||
const elements=[];
|
||
for(const {collection,type} of rows.values()){
|
||
const row=document.createElement('div');row.className='mapping-row';
|
||
const heading=document.createElement('strong');heading.textContent=sourceLabel(collection,type);
|
||
const label=document.createElement('label');label.append('目标对象');
|
||
const select=document.createElement('select');select.setAttribute('aria-label',collection+' '+type+' 目标对象');select.dataset.group=collection;select.dataset.type=type;
|
||
select.add(new Option('未适配 / 不转换',''));
|
||
for(const target of PiggyConverter.targetTypes)select.add(new Option(targetLabel(target),String(target.id)));
|
||
const rule=mapping?.[collection]?.[type];
|
||
if(rule&&!PiggyConverter.targetTypes.some(target=>target.id===rule.id))select.add(new Option('自定义编号 '+rule.id,String(rule.id)));
|
||
select.value=rule?String(rule.id):'';
|
||
select.onchange=()=>{
|
||
try{
|
||
const current=JSON.parse($('mapping').value);if(!current||typeof current!=='object'||Array.isArray(current))throw Error('映射 JSON 必须是对象');
|
||
current[collection]||={};
|
||
if(!select.value)delete current[collection][type];
|
||
else{
|
||
const target=PiggyConverter.targetTypes.find(item=>item.id===Number(select.value)),previous=current[collection][type];
|
||
const next={...previous,id:target.id,collection:target.collection,length:target.length,width:target.width};
|
||
current[collection][type]=next;
|
||
}
|
||
discoveredTypes.set(collection+':'+type,{collection,type});$('mapping').value=JSON.stringify(current,null,2);invalidate();renderMappings();
|
||
}catch(error){$('result').textContent='修改映射失败:'+error.message;}
|
||
};
|
||
const remove=document.createElement('button');remove.type='button';remove.textContent='删除映射';remove.setAttribute('aria-label','删除 '+collection+' '+type+' 映射');
|
||
remove.onclick=()=>{
|
||
if(!confirm('删除 '+type+' 的映射?不会删除源对象,再次转换时该类型将提示未适配。'))return;
|
||
try{
|
||
const current=JSON.parse($('mapping').value);if(current?.[collection])delete current[collection][type];
|
||
discoveredTypes.delete(collection+':'+type);$('mapping').value=JSON.stringify(current,null,2);invalidate();renderMappings();$('mapping-status').textContent='已删除 '+groupNames[collection]+' · '+type+' 的映射,请重新转换';
|
||
}catch(error){$('mapping-status').textContent='删除失败:'+error.message;}
|
||
};
|
||
label.append(select);row.append(heading,label,remove);elements.push(row);
|
||
}
|
||
$('mapping-list').replaceChildren(...elements);
|
||
}
|
||
renderMappings();
|
||
function invalidate(){generation++;results=[];$('failure').textContent='';$('selected').replaceChildren();$('download-all').disabled=$('download').disabled=$('report').disabled=true;$('result').textContent='请点击转换';}
|
||
for(const id of ['zero','mapping'])$(id).addEventListener('change',invalidate);
|
||
function choose(id){
|
||
const chosen=Array.from($(id).files);
|
||
if(!chosen.length)return;
|
||
$(id==='files'?'folder':'files').value='';
|
||
sourceFiles=chosen.filter(file=>/\.json$/i.test(file.name)&&!/^LevelInfoConfig\.json$/i.test(file.name))
|
||
.sort((a,b)=>(a.webkitRelativePath||a.name).localeCompare(b.webkitRelativePath||b.name,'zh-CN',{numeric:true}));
|
||
invalidate();
|
||
$('summary').textContent='已选择 '+sourceFiles.length+' 个 JSON,跳过 '+(chosen.length-sourceFiles.length)+' 个其他文件';
|
||
}
|
||
$('files').addEventListener('change',()=>choose('files'));
|
||
$('folder').addEventListener('change',()=>choose('folder'));
|
||
$('mapping').addEventListener('input',invalidate);
|
||
$('mapping').addEventListener('change',renderMappings);
|
||
function show(){
|
||
const item=results[$('selected').value];$('download').disabled=!item?.ok;
|
||
const types=(item?.unsupportedTypes||[]).map(entry=>entry.collection+':'+entry.type+' 类型未适配');
|
||
$('failure').textContent=item&&!item.ok?(types.length?'此关未转换:\n'+types.join('\n')+'\n未导出该关,未跳过任何对象。':'此关未转换:\n'+(item.errors||[]).join('\n')):'';
|
||
$('result').textContent=item?JSON.stringify(item,null,2):'没有可查看结果';
|
||
}
|
||
$('selected').onchange=show;
|
||
$('convert').onclick=async()=>{
|
||
const current=++generation;$('failure').textContent='';$('convert').disabled=true;$('download-all').disabled=$('download').disabled=$('report').disabled=true;results=[];
|
||
try{
|
||
const mapping=JSON.parse($('mapping').value),zero=$('zero').value,files=sourceFiles.slice();
|
||
if(!files.length)throw Error('请选择关卡 JSON');
|
||
const converted=[];
|
||
for(const [index,file] of files.entries()){
|
||
if(current!==generation)return;
|
||
$('summary').textContent='正在转换 '+(index+1)+' / '+files.length;
|
||
let item;try{if(file.size>32*1024*1024)throw Error('文件不能超过 32MB');const text=await file.text();if(current!==generation)return;item=PiggyConverter.convert(JSON.parse(text.replace(/^\uFEFF/,'')),mapping,zero);}catch(error){item={ok:false,errors:['转换失败:'+error.message]};}
|
||
const folders=(file.webkitRelativePath||'').split('/').slice(1,-1).map(part=>PiggyConverter.outputName(part+'.json').slice(0,-5).replace(/^\.+$/,'_'));
|
||
converted.push({file:file.webkitRelativePath||file.name,output:[...folders,PiggyConverter.outputName(file.name)].join('/'),...item});for(const unknown of item.unsupportedTypes||[])discoveredTypes.set(unknown.collection+':'+unknown.type,unknown);
|
||
if(index%20===19)await new Promise(resolve=>setTimeout(resolve,0));
|
||
}
|
||
if(current!==generation)return;results=converted;renderMappings();$('selected').replaceChildren(...results.map((item,index)=>new Option((item.ok?'通过 · ':'未转换 · ')+item.file,index)));$('report').disabled=false;$('download-all').disabled=!results.some(item=>item.ok);$('summary').textContent='共 '+results.length+' 关,通过 '+results.filter(item=>item.ok).length+' 关,未转换 '+results.filter(item=>!item.ok).length+' 关';show();
|
||
}catch(error){$('result').textContent='转换失败:'+error.message;}finally{$('convert').disabled=false;}
|
||
};
|
||
function save(name,blob){const url=URL.createObjectURL(blob),a=document.createElement('a');a.href=url;a.download=name;a.click();setTimeout(()=>URL.revokeObjectURL(url),1000);}
|
||
function download(name,data){save(name,new Blob([JSON.stringify(data,null,2)],{type:'application/json'}));}
|
||
$('download').onclick=()=>{const item=results[$('selected').value];if(item?.ok)download(item.output.split('/').pop(),item.layout);};
|
||
$('download-all').onclick=()=>{
|
||
try{
|
||
const files=Object.create(null);
|
||
for(const item of results.filter(item=>item.ok)){
|
||
if(Object.hasOwn(files,item.output))throw Error('输出文件重名,请分别转换同名源文件');
|
||
files[item.output]=fflate.strToU8(JSON.stringify(item.layout,null,2));
|
||
}
|
||
files['转换报告.json']=fflate.strToU8(JSON.stringify(results.map(({layout,...report})=>report),null,2));
|
||
save('关卡转换结果.zip',new Blob([fflate.zipSync(files,{level:6})],{type:'application/zip'}));
|
||
}catch(error){$('result').textContent='批量下载失败:'+error.message;}
|
||
};
|
||
$('report').onclick=()=>download('转换报告.json',results.map(({layout,...report})=>report));
|
||
</script></html>
|