make kxml output sorted xml result

This commit is contained in:
GEEKiDoS
2024-03-08 16:57:11 +08:00
parent 8d64cad959
commit 6fc9a884f9
3 changed files with 64 additions and 17 deletions
+11 -7
View File
@@ -58,19 +58,22 @@ function serializeObject(obj: Object, name: string, linePrefix: string = '') {
.filter(v => v[0].startsWith('$'))
.map(v => [v[0].substring(1), v[1]]);
for (const attr of attrs) {
output += ` ${attr[0]}="${_.escape(attr[1])}"`;
}
// handle kxml value shit
const value = entries.find(v => v[0] == '__value');
if (value && value[1] instanceof Array) {
output += ` __count="${value[1].length}"`;
attrs.push(['__count', value[1].length]);
}
if (value && value[1] instanceof Buffer) {
output += ` __size="${value[1].length}"`;
attrs.push(['__size', value[1].length]);
}
// the kbinxml library had issue when serializing non-ordered stuff
attrs.sort((a, b) => a[0] < b[0] ? 1 : (a[0] > b[0] ? -1 : 0));
for (const attr of attrs) {
output += ` ${attr[0]}="${_.escape(attr[1])}"`;
}
output += '>';
@@ -88,7 +91,8 @@ function serializeObject(obj: Object, name: string, linePrefix: string = '') {
.join(' ');
} else {
const elements = entries
.filter(v => !v[0].startsWith('$'));
.filter(v => !v[0].startsWith('$'))
.sort((a, b) => a[0] < b[0] ? 1 : (a[0] > b[0] ? -1 : 0));
if (elements.length) {
output += '\n';
+38 -5
View File
@@ -1,4 +1,5 @@
import { serviceKxml } from "../../decorators/to-kxml.js";
// import { v } from "../../utils/kxml-value.js";
const resp = serviceKxml('IIDX31music');
@@ -8,12 +9,44 @@ export default class {
return {
$expire: 1800,
c: [
// {
// $__type: 's32',
// $mid: 11451,
// __value: new Array(20).fill(-1),
// }
// v.s32('114514', { mid: 11451 }),
],
};
}
@resp('reg')
async reg() {
return {
$clid: 0,
$crate: 0,
$frate: 0,
$mid: 0,
$rankside: 0,
$status: 0,
shopdata: {
$rank: 252,
},
ranklist: {
$total_user_num: 1,
data: [{
$clflg: 4,
$dgrade: -1,
$sgrade: -1,
$iidx_id: '114514',
$myFlg: '0',
$name: 'MK_',
$opname: '雷窝',
$pid: 0,
$rnum: 0,
$score: 0,
$update: 0,
$body: 0,
$face: 0,
$hair: 0,
$hand: 0,
$head: 0,
}]
}
}
}
}
+15 -5
View File
@@ -15,13 +15,23 @@ export type ValueTypes = 's8' |
'time';
function vg(type: ValueTypes) {
return (value: unknown) => ({
$__type: type,
__value: value,
});
return (value: unknown, attrs?: object) => {
const result = {
$__type: type,
__value: value,
};
if (attrs) {
for (const key in attrs) {
result['$' + key] = attrs[key];
}
}
return attrs;
};
}
export const v: Record<ValueTypes, (value: unknown) => object> = {
export const v: Record<ValueTypes, (value: unknown, attrs?: object) => object> = {
s8: vg('s8'),
u8: vg('u8'),
s16: vg('s16'),