XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEEDToolBox

【WinUI】XFE精英危险工具箱

公开
关注 0 Fork 0 Star 0
UTF-8
// Offline adapter to the pinned EDSY headless Build/Slot API.
function xfeSlotName(build, group, index) {
    const ship = eddb.ship[build.getShipID()], sizes = ship.slots[group];
    if (ship.slotnames && ship.slotnames[group]) return ship.slotnames[group][index];
    if (group === 'component') return ['Armour','PowerPlant','MainEngines','FrameShiftDrive','LifeSupport','PowerDistributor','Radar','FuelTank'][index];
    if (group === 'hardpoint') return ['Tiny','Small','Medium','Large','Huge'][sizes[index]] + 'Hardpoint' + sizes.slice(0,index+1).filter(s=>s===sizes[index]).length;
    if (group === 'utility') return 'TinyHardpoint' + (index+1);
    if (group === 'military') return 'Military' + String(index+1).padStart(2,'0');
    return 'Slot' + String(index+1).padStart(2,'0') + '_Size' + sizes[index];
}
function xfeDescribe(build, errors) {
    const ship = eddb.ship[build.getShipID()], slots=[];
    for (const group of ['hardpoint','utility','component','military','internal']) {
        for(let index=0; index<ship.slots[group].length; index++) {
            const slot=build.getSlot(group,index), module=slot.getModule();
            slots.push({slot:xfeSlotName(build,group,index), category:group==='component'?'standard':group==='military'?'internal':group,
                capacity:ship.slots[group][index], module:slot.exportJournal(),
                miningOnly:!!((ship.slotlimit||{})[group]||[])[index] && group==='hardpoint',
                name:module ? module.name||module.fdname||'' : ''});
        }
    }
    return {hash:build.getHash(), name:build.getName(), ident:build.getNameTag(), ship:ship.fdname.toLowerCase(), slots:slots, errors:errors};
}
function xfeImport(hash) {
    const errors=[], build=window.edsy.Build.fromHash(hash,errors);
    if(!build) throw new Error('Invalid EDSY build: '+errors.join('; '));
    xfeRestoreDistributor(build,hash);
    return JSON.stringify(xfeDescribe(build,errors));
}
function xfeRestoreDistributor(build,hash) {
    // Upstream currently ignores these saved settings on import; retain them in our editor.
    const digits='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-',version=digits.indexOf(hash[0]);
    if(version<12)return;
    let offset=2+(version>=14?1:0);
    if(version>=15){const bits=digits.indexOf(hash[offset++]);offset+=((bits>>4)&3)+2;}
    const crew=digits.indexOf(hash[offset++]),distribution=digits.indexOf(hash[offset++])*64+digits.indexOf(hash[offset]);
    build.setCrewDist((crew>>4)&3,(crew>>2)&3,crew&3);
    build.setPowerDist((distribution>>8)&15,(distribution>>4)&15,distribution&15);
}
function xfeExport(json) {
    const input=JSON.parse(json), errors=[];
    if(input.baselineHash && input.changed.length===0) {
        const unchanged=window.edsy.Build.fromHash(input.baselineHash,errors);
        if(!unchanged)throw new Error('Invalid EDSY baseline');
        xfeRestoreDistributor(unchanged,input.baselineHash);
        unchanged.setName(input.loadout.ShipName||'');unchanged.setNameTag(input.loadout.ShipIdent||'');
        return JSON.stringify(input.statistics ? xfeDescribeStatistics(unchanged,errors) : xfeDescribe(unchanged,errors));
    }
    // Our recipe catalogue also contains English effect names without Journal aliases.
    // Resolve those against the selected module's own allowed effects/blueprints.
    for(const entry of input.loadout.Modules) {
        if(!entry.Engineering)continue;
        const module=Object.values(eddb.module).find(m=>(m.fdname||'').toLowerCase()===entry.Item.toLowerCase());
        const type=module&&eddb.mtype[module.mtype];
        if(!type)continue;
        const normalize=s=>(s||'').replace(/[^a-z0-9]/gi,'').toLowerCase();
        const bp=(type.blueprints||[]).map(id=>eddb.blueprint[id]).find(b=>normalize(b.name)===normalize(entry.Engineering.BlueprintName)||normalize(b.fdname)===normalize(entry.Engineering.BlueprintName));
        const exp=(type.expeffects||[]).map(id=>eddb.expeffect[id]).find(e=>normalize(e.name)===normalize(entry.Engineering.ExperimentalEffect)||normalize(e.fdname)===normalize(entry.Engineering.ExperimentalEffect));
        if(bp?.fdname)entry.Engineering.BlueprintName=bp.fdname;
        if(exp?.fdname)entry.Engineering.ExperimentalEffect=exp.fdname;
    }
    const converted=window.edsy.Build.fromJournal(input.loadout,errors);
    if(!converted) throw new Error('EDSY cannot encode this ship: '+errors.join('; '));
    const empty=input.loadout.Modules.filter(m=>!m.Item).map(m=>m.Slot);
    for(const group of ['hardpoint','utility','component','military','internal'])
        for(let index=0;index<eddb.ship[converted.getShipID()].slots[group].length;index++)
            if(empty.indexOf(xfeSlotName(converted,group,index))>=0)converted.getSlot(group,index).setModuleID(0,true);
    let build=converted;
    if(input.baselineHash) {
        build=window.edsy.Build.fromHash(input.baselineHash,errors);
        if(!build || Number(build.getShipID())!==Number(converted.getShipID())) throw new Error('EDSY baseline ship mismatch');
        xfeRestoreDistributor(build,input.baselineHash);
        for(const group of ['hardpoint','utility','component','military','internal']) {
            for(let index=0; index<eddb.ship[build.getShipID()].slots[group].length; index++) {
                if(input.changed.indexOf(xfeSlotName(build,group,index))>=0 &&
                    !build.getSlot(group,index).setHash(converted.getSlot(group,index).getHash(),20,errors,true))
                    errors.push('Cannot replace '+xfeSlotName(build,group,index));
            }
        }
        build.setName(input.loadout.ShipName||''); build.setNameTag(input.loadout.ShipIdent||'');
    }
    // Unchanged modules are copied from the original hash, not from the Journal conversion.
    const relevant=input.baselineHash ? errors.filter(e=>input.changed.some(slot=>e.indexOf(slot+':')===0)||e.indexOf(':')<0) : errors;
    return JSON.stringify(input.statistics ? xfeDescribeStatistics(build,relevant) : xfeDescribe(build,relevant));
}
// Read the effective values before encoding the hash, preserving Journal precision.
function xfeDescribeStatistics(build, errors) {
    const modules={}, attributes=['mass','integ','pwrdraw','pwrcap','heateff','dps','distdraw','pierce','maximumrng','shotspd','rof','ammoclip','ammomax','dmgfall','kinwgt','thmwgt','expwgt','abswgt','cauwgt','axewgt','fsdoptmass','maxfuel','fuelmul','fuelpower','fuelcap','cargocap','scooprate','engminmass','engoptmass','engmaxmass','minmulspd','optmulspd','maxmulspd','minmulrot','optmulrot','maxmulrot','syscap','engcap','wepcap','syschg','engchg','wepchg'];
    const weaponAttributes=['dps','distdraw','pierce','maximumrng','shotspd','rof','dmgfall','kinwgt','thmwgt','expwgt','abswgt','cauwgt','axewgt'];
    for (const group of ['hardpoint','utility','component','military','internal']) {
        for(let index=0;index<eddb.ship[build.getShipID()].slots[group].length;index++) {
            const slot=build.getSlot(group,index);
            if(!slot.getModule())continue;
            const values={};
            for(const attr of attributes) {
                if(group!=='hardpoint' && weaponAttributes.includes(attr))continue;
                const value=slot.getEffectiveAttrValue(attr);
                if(Number.isFinite(value) && (value!==0 || ['mass','integ','pwrdraw'].includes(attr)))values[attr]=value;
            }
            modules[xfeSlotName(build,group,index)]=values;
        }
    }
    const ship={};
    for(const key of ['pwrcap','mass','_speed','_boost','_jump_laden','_jump_unladen','_jump_max','_shields','_armour','dps','fuelcap','cargocap','jumpbst',
        '_skinres','_sthmres','_sexpres','_akinres','_athmres','_aexpres','integ_imrp','dmgprot','shieldrnfps_ammomax',
        'dps_abs','dps_thm','dps_kin','dps_exp','dps_axe','dps_cau','dps_nodistdraw','dps_distdraw','wepchg_sustain_cur','wepchg_sustain_max','wepcap_burst_cur',
        'cabincap','cost','cost_rearm']) {
        const value=build.getStat(key);
        if(Number.isFinite(value))ship[key]=value;
    }
    ship.pwrdraw_ret=build.getStat('pwrdraw_ret')[0];
    ship.pwrdraw_dep=build.getStat('pwrdraw_dep')[0];
    const hull=build.getSlot('ship','hull');
    for(const key of ['topspd','bstspd','minthrust','pitch','roll','yaw','minpitch','minroll','minyaw','boostcost','hardness','heatcap']) {
        const value=hull.getEffectiveAttrValue(key);
        if(Number.isFinite(value))ship['_hull_'+key]=value;
    }
    ship._engine_pips=build.getEffectivePowerDist('eng');
    ship._weapon_pips=build.getEffectivePowerDist('wep');
    const independent=build.getStat('dps_nodistdraw'), dependent=build.getStat('dps_distdraw');
    for(const name of ['cur','max']) {
        const sustained=independent+(dependent?dependent*build.getStat('wepchg_sustain_'+name):0);
        if(Number.isFinite(sustained))ship['_sustained_dps_'+name]=sustained;
    }
    ship._wepcap_unlimited=build.getStat('wepcap_burst_cur')===Infinity?1:0;
    return {errors,ship,modules};
}