make more generic, start using in real project

This commit is contained in:
zongor 2024-05-27 18:26:44 -04:00
parent 6e2350a872
commit f26ead177a
5 changed files with 398 additions and 29 deletions

34
client/types.h Normal file
View File

@ -0,0 +1,34 @@
typedef struct Entity {
char Name[24];
int Strength;
int Endurance;
int Intelligence;
int Wisdom;
int Charisma;
int Faith;
int Artisan;
int Metalworking;
int Alchemy;
int Engineering;
int Culinary;
int Ranged;
int Melee;
int Acrobatics;
int Prestidigitation;
int Language;
int Medicine;
int Thaumitology;
int Theology;
int Fishing;
int Mining;
int Survival;
int Gardening;
int History;
int Perception;
int Luck;
int Stealth;
int Attractiveness;
int Affluence;
int Notoriety;
} Entity;

203
server/components/Entity.js Normal file
View File

@ -0,0 +1,203 @@
class Entity {
get Name() {
return this._decoder.decode(new Uint8Array(this._ptr.slice(0, 24)));
}
set Name(v) {
this._data.set(this._encoder.encode(v), 0);
}
get Strength() {
return this._data.getInt32(24, true);
}
set Strength(v) {
return this._data.setInt32(24, v, true);
}
get Endurance() {
return this._data.getInt32(28, true);
}
set Endurance(v) {
return this._data.setInt32(28, v, true);
}
get Intelligence() {
return this._data.getInt32(32, true);
}
set Intelligence(v) {
return this._data.setInt32(32, v, true);
}
get Wisdom() {
return this._data.getInt32(36, true);
}
set Wisdom(v) {
return this._data.setInt32(36, v, true);
}
get Charisma() {
return this._data.getInt32(40, true);
}
set Charisma(v) {
return this._data.setInt32(40, v, true);
}
get Faith() {
return this._data.getInt32(44, true);
}
set Faith(v) {
return this._data.setInt32(44, v, true);
}
get Artisan() {
return this._data.getInt32(48, true);
}
set Artisan(v) {
return this._data.setInt32(48, v, true);
}
get Metalworking() {
return this._data.getInt32(52, true);
}
set Metalworking(v) {
return this._data.setInt32(52, v, true);
}
get Alchemy() {
return this._data.getInt32(56, true);
}
set Alchemy(v) {
return this._data.setInt32(56, v, true);
}
get Engineering() {
return this._data.getInt32(60, true);
}
set Engineering(v) {
return this._data.setInt32(60, v, true);
}
get Culinary() {
return this._data.getInt32(64, true);
}
set Culinary(v) {
return this._data.setInt32(64, v, true);
}
get Ranged() {
return this._data.getInt32(68, true);
}
set Ranged(v) {
return this._data.setInt32(68, v, true);
}
get Melee() {
return this._data.getInt32(72, true);
}
set Melee(v) {
return this._data.setInt32(72, v, true);
}
get Acrobatics() {
return this._data.getInt32(76, true);
}
set Acrobatics(v) {
return this._data.setInt32(76, v, true);
}
get Prestidigitation() {
return this._data.getInt32(80, true);
}
set Prestidigitation(v) {
return this._data.setInt32(80, v, true);
}
get Language() {
return this._data.getInt32(84, true);
}
set Language(v) {
return this._data.setInt32(84, v, true);
}
get Medicine() {
return this._data.getInt32(88, true);
}
set Medicine(v) {
return this._data.setInt32(88, v, true);
}
get Thaumitology() {
return this._data.getInt32(92, true);
}
set Thaumitology(v) {
return this._data.setInt32(92, v, true);
}
get Theology() {
return this._data.getInt32(96, true);
}
set Theology(v) {
return this._data.setInt32(96, v, true);
}
get Fishing() {
return this._data.getInt32(100, true);
}
set Fishing(v) {
return this._data.setInt32(100, v, true);
}
get Mining() {
return this._data.getInt32(104, true);
}
set Mining(v) {
return this._data.setInt32(104, v, true);
}
get Survival() {
return this._data.getInt32(108, true);
}
set Survival(v) {
return this._data.setInt32(108, v, true);
}
get Gardening() {
return this._data.getInt32(112, true);
}
set Gardening(v) {
return this._data.setInt32(112, v, true);
}
get History() {
return this._data.getInt32(116, true);
}
set History(v) {
return this._data.setInt32(116, v, true);
}
get Perception() {
return this._data.getInt32(120, true);
}
set Perception(v) {
return this._data.setInt32(120, v, true);
}
get Luck() {
return this._data.getInt32(124, true);
}
set Luck(v) {
return this._data.setInt32(124, v, true);
}
get Stealth() {
return this._data.getInt32(128, true);
}
set Stealth(v) {
return this._data.setInt32(128, v, true);
}
get Attractiveness() {
return this._data.getInt32(132, true);
}
set Attractiveness(v) {
return this._data.setInt32(132, v, true);
}
get Affluence() {
return this._data.getInt32(136, true);
}
set Affluence(v) {
return this._data.setInt32(136, v, true);
}
get Notoriety() {
return this._data.getInt32(140, true);
}
set Notoriety(v) {
return this._data.setInt32(140, v, true);
}
get bytes() {
return new Uint8Array(this._ptr);
}
constructor(init = {}, ptr = undefined) {
this._size = 144;
this._ptr = ptr.buffer || new ArrayBuffer(this._size);
this._data = new DataView(this._ptr);
this._encoder = new TextEncoder();
this._decoder = new TextDecoder();
for (const key of Object.keys(init)) {
this[key] = init[key];
}
}
}

2
server/generate.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
bun run ../tools/js-class-c-struct-transpiler/transpile.js -S schema.json -j components/ -c ../client/

129
server/schema.json Normal file
View File

@ -0,0 +1,129 @@
{
"Entity": {
"Name": {
"type": "string",
"kind": "string",
"size": "24"
},
"Strength": {
"type": "i32",
"kind": "scalar"
},
"Endurance": {
"type": "i32",
"kind": "scalar"
},
"Intelligence": {
"type": "i32",
"kind": "scalar"
},
"Wisdom": {
"type": "i32",
"kind": "scalar"
},
"Charisma": {
"type": "i32",
"kind": "scalar"
},
"Faith": {
"type": "i32",
"kind": "scalar"
},
"Artisan": {
"type": "i32",
"kind": "scalar"
},
"Metalworking": {
"type": "i32",
"kind": "scalar"
},
"Alchemy": {
"type": "i32",
"kind": "scalar"
},
"Engineering": {
"type": "i32",
"kind": "scalar"
},
"Culinary": {
"type": "i32",
"kind": "scalar"
},
"Ranged": {
"type": "i32",
"kind": "scalar"
},
"Melee": {
"type": "i32",
"kind": "scalar"
},
"Acrobatics": {
"type": "i32",
"kind": "scalar"
},
"Prestidigitation": {
"type": "i32",
"kind": "scalar"
},
"Language": {
"type": "i32",
"kind": "scalar"
},
"Medicine": {
"type": "i32",
"kind": "scalar"
},
"Thaumitology": {
"type": "i32",
"kind": "scalar"
},
"Theology": {
"type": "i32",
"kind": "scalar"
},
"Fishing": {
"type": "i32",
"kind": "scalar"
},
"Mining": {
"type": "i32",
"kind": "scalar"
},
"Survival": {
"type": "i32",
"kind": "scalar"
},
"Gardening": {
"type": "i32",
"kind": "scalar"
},
"History": {
"type": "i32",
"kind": "scalar"
},
"Perception": {
"type": "i32",
"kind": "scalar"
},
"Luck": {
"type": "i32",
"kind": "scalar"
},
"Stealth": {
"type": "i32",
"kind": "scalar"
},
"Attractiveness": {
"type": "i32",
"kind": "scalar"
},
"Affluence": {
"type": "i32",
"kind": "scalar"
},
"Notoriety": {
"type": "i32",
"kind": "scalar"
}
}
}

View File

@ -2,32 +2,32 @@ import { parseArgs } from "util";
let types = { let types = {
u8: { u8: {
c: "uint8_t", c: "unsigned char",
js: "Uint8", js: "Uint8",
size: 1, size: 1,
}, },
i8: { i8: {
c: "int8_t", c: "char",
js: "Int8", js: "Int8",
size: 1, size: 1,
}, },
u16: { u16: {
c: "uint16_t", c: "unsigned short",
js: "Uint16", js: "Uint16",
size: 2, size: 2,
}, },
i16: { i16: {
c: "int16_t", c: "short",
js: "Int16", js: "Int16",
size: 2, size: 2,
}, },
u32: { u32: {
c: "uint32_t", c: "unisgned int",
js: "Uint32", js: "Uint32",
size: 4, size: 4,
}, },
i32: { i32: {
c: "int32_t", c: "int",
js: "Int32", js: "Int32",
size: 4, size: 4,
}, },
@ -50,10 +50,14 @@ const { values } = parseArgs({
type: "string", type: "string",
short: "S", short: "S",
}, },
out: { jsout: {
type: "string", type: "string",
short: "o" short: "j"
} },
cout: {
type: "string",
short: "c"
},
}, },
strict: true, strict: true,
allowPositionals: true, allowPositionals: true,
@ -64,7 +68,6 @@ function jsStructConstructor(size, containsString) {
get bytes() { get bytes() {
return new Uint8Array(this._ptr); return new Uint8Array(this._ptr);
} }
constructor(init = {}, ptr = undefined) { constructor(init = {}, ptr = undefined) {
this._size = ${size}; this._size = ${size};
this._ptr = ptr.buffer || new ArrayBuffer(this._size); this._ptr = ptr.buffer || new ArrayBuffer(this._size);
@ -128,8 +131,7 @@ for (const type of Object.keys(schema)) {
return this._data.set${types[propType].js}(${parseInt( return this._data.set${types[propType].js}(${parseInt(
offset offset
)}, v, true); )}, v, true);
} }`;
`;
cData += ` cData += `
${types[propType].c} ${prop};`; ${types[propType].c} ${prop};`;
@ -143,8 +145,7 @@ for (const type of Object.keys(schema)) {
} }
set ${prop}(v) { set ${prop}(v) {
this._data.set(v.bytes(), ${offset}); this._data.set(v.bytes(), ${offset});
} }`;
`;
cData += ` cData += `
${types[propType].c} ${prop};`; ${types[propType].c} ${prop};`;
@ -176,6 +177,6 @@ for (const type of Object.keys(schema)) {
`; `;
await Bun.write(Bun.file(values.out + type + ".js"), jsData); await Bun.write(Bun.file(values.jsout + type + ".js"), jsData);
} }
await Bun.write(Bun.file(values.out + "types.h"), cData); await Bun.write(Bun.file(values.cout + "types.h"), cData);