tyhuynh-laya-cmd 1.0.9 → 1.0.11
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const path=require("path"),fs=require("fs-extra"),{getMtime:getMtime,ensureDir:ensureDir,collectFiles:collectFiles,writeIfChanged:writeIfChanged}=require("../utils/FileUtils"),{resolveStyle:resolveStyle}=require("../parsers/StylesParser"),{MaxRectsPacker:MaxRectsPacker}=require("maxrects-packer");function packSpritesMultiPage(e,
|
|
1
|
+
"use strict";const path=require("path"),fs=require("fs-extra"),{getMtime:getMtime,ensureDir:ensureDir,collectFiles:collectFiles,writeIfChanged:writeIfChanged}=require("../utils/FileUtils"),{resolveStyle:resolveStyle}=require("../parsers/StylesParser"),{MaxRectsPacker:MaxRectsPacker}=require("maxrects-packer");function packSpritesMultiPage(t,e,a){const n=new MaxRectsPacker(e,a,1,{smart:!0,pot:!1,square:!1}),s=t.map(t=>({width:t.w,height:t.h,data:t}));n.addArray(s);return n.bins.map(t=>t.rects.map(t=>{const e=t.data;return{name:e.name,path:e.path,w:e.w,h:e.h,x:t.x,y:t.y,sourceW:e.sourceW,sourceH:e.sourceH,offX:e.offX,offY:e.offY,buffer:e.buffer}}))}function buildAtlasJson(t,e,a){const n={},s=[];return t.forEach((t,e)=>{const r=0===e?`${a}.png`:`${a}${e}.png`;s.push(r);for(const a of t){const t=path.basename(a.name);n[t]={frame:{x:a.x,y:a.y,w:a.w,h:a.h,idx:e},sourceSize:{w:a.sourceW,h:a.sourceH},spriteSourceSize:{x:a.offX,y:a.offY}}}}),{frames:n,meta:{image:s.join(","),prefix:e+"/"}}}async function generateAtlas(t){const{styleMap:e,projectConfig:a,assetDir:n,outputDir:s,cachePath:r,clear:i=!1,changedDirs:o=null,log:c=console.log}=t;let h;try{h=require("sharp")}catch{return void c("[AtlasGen] ⚠ sharp not installed — skipping atlas generation")}ensureDir(s);const l=a.textureWidth||1024,f=a.textureHeight||1024,p=a.picWidth||512,u=a.picHeight||512;let m={};if(!i)try{m=fs.readJsonSync(r)}catch{}const g=collectFiles(n,[".png",".jpg"]);if(i){let A=0;function j(t){if(!fs.existsSync(t))return;let e;try{e=fs.readdirSync(t,{withFileTypes:!0})}catch{return}for(const a of e){const e=path.join(t,a.name);if(a.isDirectory()){j(e);try{fs.rmdirSync(e)}catch{}}else if(".json"!==path.extname(a.name).toLowerCase())try{fs.unlinkSync(e),A++}catch{}}}j(path.join(s,"res","image")),A>0&&c(`[AtlasGen] 🧹 Cleaned ${A} non-JSON file(s) from bin/res/image/`)}const d=new Map,w=[],y=await Promise.all(g.map(async t=>{const a=path.relative(n,t).replace(/\\/g,"/"),s=path.dirname(a);let r=resolveStyle(e,a),i=!1,o=!1;if(r&&void 0!==r.pack&&0!=r.pack)2==r.pack?(o=!0,i=!1):i=!0;else try{const e=await h(t).metadata();i=e.width<=p&&e.height<=u}catch{}return{relName:a,parentDir:s,absolutePath:t,shouldPack:i,isUnpack:o}}));for(const{relName:I,parentDir:P,absolutePath:D,shouldPack:F,isUnpack:W}of y){d.has(P)||d.set(P,{packItems:[],rawItems:[]});const v=d.get(P);F?v.packItems.push({name:I,path:D}):v.rawItems.push({name:I,path:D}),W&&w.push(I)}c(`[AtlasGen] Found ${d.size} directories containing images`);async function x(t,e){for(const t of e.rawItems){const e=path.join(s,t.name),a=getMtime(t.path),n=`raw_${t.name}`;(i||!m[n]||m[n]!==a||!fs.existsSync(e))&&(fs.ensureDirSync(path.dirname(e)),fs.copyFileSync(t.path,e),m[n]=a)}if(0===e.packItems.length)return;const a=path.basename(t),n=path.join(s,t),r=`${n}.atlas`;let p=i||!m[t];const u={};if(o&&o.has(t)&&(p=!0),p)for(const t of e.packItems)u[t.name]=getMtime(t.path);else{const a=m[t].mtimes||{};for(const t of e.packItems){const e=getMtime(t.path);u[t.name]=e,e!==a[t.name]&&(p=!0)}fs.existsSync(r)||(p=!0)}if(!p)return;c(`[AtlasGen] Building atlas for ${t} (${e.packItems.length} sprites)`);const g=e.packItems.map(async t=>{try{const e=h(t.path),a=await e.metadata();if(a.width>l||a.height>f)return c(`[AtlasGen] ⚠ ${t.name} is too large for atlas (${a.width}x${a.height}), skipping`),null;const n=0,s=await e.clone().ensureAlpha().raw().toBuffer({resolveWithObject:!0}),r=s.data,i=s.info.width,o=s.info.height;let p=i,u=o,m=-1,g=-1;for(let t=0;t<o;t++)for(let e=0;e<i;e++){r[4*(t*i+e)+3]>=1&&(e<p&&(p=e),t<u&&(u=t),e>m&&(m=e),t>g&&(g=t))}if(-1===m)return null;const d=Math.max(0,p-n),w=Math.max(0,u-n),y=Math.min(i,m+1+n)-d,x=Math.min(o,g+1+n)-w;let k,S;if(0===d&&0===w&&y===i&&x===o){const t=await h(r,{raw:{width:i,height:o,channels:4}}).png().toBuffer({resolveWithObject:!0});k=t.data,S=t.info}else{const t=await h(r,{raw:{width:i,height:o,channels:4}}).extract({left:d,top:w,width:y,height:x}).png().toBuffer({resolveWithObject:!0});k=t.data,S=t.info}return{name:t.name,path:t.path,w:S.width,h:S.height,sourceW:a.width,sourceH:a.height,offX:d,offY:w,buffer:k}}catch(e){return c(`[AtlasGen] ✗ Cannot process ${t.name} at ${t.path}: ${e.message}`),null}}),d=await Promise.all(g),w=[];for(const t of d)t&&w.push(t);if(0===w.length)return;const y=packSpritesMultiPage(w,l,f);fs.ensureDirSync(path.dirname(n));for(let t=0;t<y.length;t++){const e=0===t?`${a}.png`:`${a}${t}.png`,s=`${n}${0===t?"":t}.png`,r=y[t],i=Math.min(l,Math.max(...r.map(t=>t.x+t.w))),o=Math.min(f,Math.max(...r.map(t=>t.y+t.h))),p=r.map(t=>({input:t.buffer,top:t.y,left:t.x}));try{await h({create:{width:i,height:o,channels:4,background:{r:0,g:0,b:0,alpha:0}}}).composite(p).png().toFile(s)}catch(t){c(`[AtlasGen] ✗ Failed to render ${e}: ${t.message}`)}}const x=buildAtlasJson(y,t,a);fs.writeFileSync(r,JSON.stringify(x,null,2),"utf-8"),m[t]={mtimes:u}}const k=[...d.entries()];let S=0;const $=Array.from({length:Math.min(8,k.length)},async function(){for(;S<k.length;){const[t,e]=k[S++];await x(t,e)}});await Promise.all($),fs.ensureDirSync(path.dirname(r)),writeIfChanged(r,JSON.stringify(m,null,2));const M=path.join(s,"unpack.json"),b=JSON.stringify(w,null,4);writeIfChanged(M,b)&&c(`[AtlasGen] Written unpack.json with ${w.length} items`),c("[AtlasGen] Done processing all atlases and raw images.")}module.exports={generateAtlas:generateAtlas};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const path=require("path"),fs=require("fs-extra"),{writeIfChanged:writeIfChanged}=require("../utils/FileUtils"),{resolveStyle:resolveStyle}=require("../parsers/StylesParser"),EDITOR_KEYS=["searchKey","nodeParent","maxID","label","isOpen","isDirectory","isAniNode","hasChild","selectedBox","switchAble","removeAble","$HIDDEN","$LOCKED","selecteID","x","y"],ASSET_REF_RE=/\"([^\"]+\.(?:png|jpg|jpeg|sk|ani))\"/gi;function stripEditorKeys(e,s){if(e&&"object"==typeof e){for(const s of EDITOR_KEYS)delete e[s];if("Script"===e.type&&e.source){e.props||(e.props={});let s=e.source;s.startsWith("src/")&&(s=s.substring(4)),e.props.runtime=s,delete e.source}if(s&&e.props&&e.props.skin){const t=resolveStyle(s,e.props.skin);t&&t.propsMap&&(e.props=Object.assign({},t.propsMap,e.props))}if("Button"===e.type&&e.props&&void 0===e.props.stateNum&&(e.props.stateNum=1),"UIView"===e.type&&e.source){const s=path.basename(e.source,".scene");if(e.type=s,e.props||(e.props={}),e.props.runtime)e.props.runtime.startsWith("src/")&&(e.props.runtime=e.props.runtime.substring(4));else{const t="ui."+e.source.substring(0,e.source.lastIndexOf("/")).replace(/\\/g,"/").replace(/\//g,".");e.props.runtime=`${t}.${s}UI`}delete e.source}if(Array.isArray(e.child))if(0===e.child.length)delete e.child;else for(const t of e.child)stripEditorKeys(t,s)}}function
|
|
1
|
+
"use strict";const path=require("path"),fs=require("fs-extra"),{writeIfChanged:writeIfChanged}=require("../utils/FileUtils"),{resolveStyle:resolveStyle}=require("../parsers/StylesParser"),EDITOR_KEYS=["searchKey","nodeParent","maxID","label","isOpen","isDirectory","isAniNode","hasChild","selectedBox","switchAble","removeAble","$HIDDEN","$LOCKED","selecteID","x","y"],ASSET_REF_RE=/\"([^\"]+\.(?:png|jpg|jpeg|sk|ani|scene))\"/gi;function stripEditorKeys(e,s){if(e&&"object"==typeof e){for(const s of EDITOR_KEYS)delete e[s];if("Script"===e.type&&e.source){e.props||(e.props={});let s=e.source;s.startsWith("src/")&&(s=s.substring(4)),e.props.runtime=s,delete e.source}if(s&&e.props&&e.props.skin){const t=resolveStyle(s,e.props.skin);t&&t.propsMap&&(e.props=Object.assign({},t.propsMap,e.props))}if("Button"===e.type&&e.props&&void 0===e.props.stateNum&&(e.props.stateNum=1),"UIView"===e.type&&e.source){const s=path.basename(e.source,".scene");if(e.type=s,e.props||(e.props={}),e.props.runtime)e.props.runtime.startsWith("src/")&&(e.props.runtime=e.props.runtime.substring(4));else{const t="ui."+e.source.substring(0,e.source.lastIndexOf("/")).replace(/\\/g,"/").replace(/\//g,".");e.props.runtime=`${t}.${s}UI`}delete e.source}if(Array.isArray(e.child))if(0===e.child.length)delete e.child;else for(const t of e.child)stripEditorKeys(t,s)}}function collectSceneRefs(e,s={}){if(!e||"object"!=typeof e)return Object.keys(s);if("UIView"===e.type&&"string"==typeof e.source&&(s[e.source]=!0),Array.isArray(e.child))for(const t of e.child)collectSceneRefs(t,s);return Object.keys(s)}function buildLoadList(e,s=[]){const t=JSON.stringify(e),n={};let r;for(ASSET_REF_RE.lastIndex=0;null!==(r=ASSET_REF_RE.exec(t));)n[r[1]]=!0;for(const e of s)n[e]=!0;return Object.keys(n)}function buildSceneJson(e,s){const t=collectSceneRefs(e),n=JSON.parse(JSON.stringify(e));return stripEditorKeys(n,s),n.loadList=buildLoadList(n,t),n.loadList3D=[],n}async function exportSceneJsonFiles(e){const{resExportPath:s,scenes:t,separatedScenes:n=new Set,styleMap:r,log:o=console.log}=e,i={};let c=0;for(const{relPath:e,parsed:l}of t){const t=buildSceneJson(l.rawJson,r),p=e.replace(/\.scene$/i,".json"),a=path.join(s,p);if(n.has(e))i[p]=t,fs.existsSync(a)&&(fs.unlinkSync(a),o(`[SceneJsonGen] ✗ Removed stale standalone: ${p}`));else{fs.ensureDirSync(path.dirname(a));writeIfChanged(a,JSON.stringify(t))&&o(`[SceneJsonGen] ✓ Written: ${p}`),c++}}const l=path.join(s,"ui.json");return Object.keys(i).length>0?(fs.ensureDirSync(path.dirname(l)),writeIfChanged(l,JSON.stringify(i)),o(`[SceneJsonGen] ✓ Bundled ${Object.keys(i).length} scenes → ui.json`)):fs.existsSync(l)&&(fs.unlinkSync(l),o("[SceneJsonGen] ✓ Deleted empty ui.json")),{bundled:Object.keys(i).length,standalone:c}}module.exports={stripEditorKeys:stripEditorKeys,collectSceneRefs:collectSceneRefs,buildLoadList:buildLoadList,buildSceneJson:buildSceneJson,exportSceneJsonFiles:exportSceneJsonFiles};
|