Skip to content

Commit

Permalink
Merge pull request #9 from Con-JS-Development/development
Browse files Browse the repository at this point in the history
Error
  • Loading branch information
conmaster2112 authored Jan 8, 2024
2 parents 52dd6b3 + 31bdfef commit 484fef4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 41 deletions.
2 changes: 1 addition & 1 deletion con-database.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Serializer {
function registrySerializer<K extends string>(kind: K, serializer: (object: any)=>Generator<string>, deserializer: (source: Deserializer<string>)=>void): K;
function getSerializer(kind: string): null | ((object: any)=>Generator<string>)
function getDeserializer(kind: string): ((source: Deserializer<string>)=>void) | null
function setSerializableClass(construct: new (...any: any[])=>any, kind: string, serializer: (object: any)=>Generator<string>, deserializer: (source: Deserializer<string>)=>void): void
function setSerializableClass<T>(construct: new (...any: any[])=>T, kind: string, serializer: (object: T)=>Generator<string>, deserializer: (source: Deserializer<string>)=>T): void
function getKindFromClass(construct: new (...any: any[])=>any): string | null;
function getSerializerKinds(): IterableIterator<string>
function getSerializers(kind: string): {serializer: (object: any)=>Generator<string>, deserializer: (source: Deserializer<string>)=>void}
Expand Down
27 changes: 20 additions & 7 deletions con-database.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const DYNAMIC_DB_PREFIX = "\u1221\u2112";
const ROOT_CONTENT_TABLE_UUID = "c0211201-0001-4001-8001-4f90af596647";
const STRING_LIMIT = 32e3;
const TABLE_STRING_LENGTH = 31e3;
const GENERATOR_DESERIALIZER_SYMBOL = Symbol("DESERIALIZER");
const eP = {
gDP: eGDP,
sDP: eSDP,
Expand Down Expand Up @@ -209,7 +210,6 @@ class DynamicProxy extends JsonDatabase{
return false;
},
deleteProperty(t,p){
console.warn("DELETE: ",p);
if(typeof p === "string") return t.delete(p);
return false;
},
Expand Down Expand Up @@ -308,7 +308,14 @@ const Serializer = {
return {...data};
},
setSerializableClass(construct, kind, serializer, deserializer){
Serializer.registrySerializer(kind, serializer, deserializer);
if(typeof serializer !== "function" || typeof deserializer !== "function") throw new TypeError("Serializer or deserializer is not a function");
Serializer.registrySerializer(kind, function(obj){
if(obj == null) throw new TypeError("Null or Undefined is not possible to serialize.");
return serializer(obj);
}, function(obj){
if(obj[GENERATOR_DESERIALIZER_SYMBOL] !== true) throw new TypeError("Null or Undefined is not possible to serialize.");
return deserializer(obj);
});
Serializer.setSerializableKind(construct.prototype,kind);
},
getKindFromClass(construct){
Expand Down Expand Up @@ -365,10 +372,17 @@ const DATABASE_MANAGER = {
}
source.set(rootRef, JSONWritable({length:newLength.toString(36),kind},headerData));
}
} finally {
for (let i = newLength; i < oldLength; i++) source.delete(prefix + i);
return newLength;
}
catch(er){
Object.setPrototypeOf(er, DataCoruptionError.prototype);
er.source = source;
er.rootKey = rootRef;
throw er;
}
finally {
for (let i = newLength; i < oldLength; i++) source.delete(prefix + i);
}
},
deserialize(rootRef, source, header = undefined){
try {
Expand Down Expand Up @@ -418,7 +432,9 @@ const DATABASE_MANAGER = {
return true;
}
}

Object.defineProperties(DATABASE_MANAGER.deserializer.prototype,Object.getOwnPropertyDescriptors({
[GENERATOR_DESERIALIZER_SYMBOL]: true,
return(){
return {done:true};
},
Expand All @@ -442,8 +458,6 @@ Object.defineProperties(DATABASE_MANAGER.deserializer.prototype,Object.getOwnPro
return DESERIALIZER_INFO.get(this).kind;
}
}));


class DynamicTable extends Map{
/**@readonly */
static get KIND(){return "c0211201-0001-4002-8101-4f90af596647";}
Expand Down Expand Up @@ -709,7 +723,6 @@ export const registryAPISerializers = ()=>{
for (const key in ItemStackSupportLevel) {
if (Object.hasOwnProperty.call(ItemStackSupportLevel, key)) {
const element = ItemStackSupportLevel[key];
console.warn(key, !!element);
}
}
const ItemStackComponentManager = {
Expand Down
2 changes: 1 addition & 1 deletion packs/BP/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"dependencies": [
{"module_name": "@minecraft/server","version": "1.7.0"}
{"module_name": "@minecraft/server","version": "1.9.0-beta"}
],
"capabilities": ["script_eval"]
}
89 changes: 57 additions & 32 deletions packs/BP/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,69 @@
import { system, world } from "@minecraft/server";
import { SerializableKinds, JsonDatabase } from "./con-database";
import { system, world, Container } from "@minecraft/server";
import { SerializableKinds } from "./con-database";
import { DynamicTable, Serializer } from "./con-database";
import { APISerializableKinds, registryAPISerializers } from "./con-database";


class MyClassWithMethods {
constructor(id, message){
this.id = id;
this.message = message;
registryAPISerializers();
class ContainerMetadata{
/**@param {Container} container */
static FromContainer(container){
const meta = new ContainerMetadata();
for (let i = 0; i < container.size; i++) {
const item = container.getItem(i);
if(item){
meta.items.push(item);
meta.slots.push(i);
}
}
return meta;
}
warn(){
console.warn(this.id,this.message);
static SaveToContainer(container, meta){
for (const slot of meta.slots) {
container.setItem(slot, meta.items.shift());
}
}
constructor(){
this.items = [];
this.slots = [];
}
}
Serializer.setSerializableClass(
MyClassWithMethods,
"custom-kind-id",
function({id,message}){ // serialization function, called when DynamicTable.set()
ContainerMetadata,
"container-metedata-serialization-id", //do not change this id its unique ti your serializer
function* ({items, slots}){
const objectSerializer = Serializer.getSerializer(SerializableKinds.Object);
return objectSerializer({id,message}); //basic serialization as poor object
yield * objectSerializer(slots); //saving manifest info
const itemSerialzer = Serializer.getSerializer(APISerializableKinds.ItemStack);
for (const item of items) {
throw new Error("Throw test");
yield * itemSerialzer(item);
}
},
function(n){ //deserialization function, called when DynamicTable.get();
const object = Serializer.getDeserializer(SerializableKinds.Object)(n); //loaded poor object
return Object.setPrototypeOf(object, MyClassWithMethods.prototype); //add a MyClassWithMethods prototype
function(n){
const slots = Serializer.getDeserializer(SerializableKinds.Object)(n); //loading info
const itemDeserializer = Serializer.getDeserializer(APISerializableKinds.ItemStack);
const obj = new ContainerMetadata();
obj.slots = slots;
for (const s of slots) {
obj.items.push(itemDeserializer(n));
}
return obj;
}
)



);

const table = DynamicTable.OpenCreate("id-of-the-table");
table.set("key-the-test", new MyClassWithMethods("warn-id", "My custom message"));


table.get("key-the-test").warn(); //warn method from MyClasswithMethods
const jsDB = new JsonDatabase("the id");
jsDB.set("Lmao","adfasdfsa".repeat(600));
jsDB.set("Some test", {asd:"asdfasdf",asds:{asdfasdfa:"asdfasdf"}});
for (const [k,v] of jsDB) {
console.warn(k,v);
}
jsDB.clear();
DynamicTable.ClearAll();
system.runTimeout(()=>console.warn(world.getDynamicPropertyTotalByteCount()), 3);
world.clearDynamicProperties();
const table = DynamicTable.OpenCreate("Testing so far");
let saved = false;
world.afterEvents.chatSend.subscribe(({sender, message})=>{
if(message === "m"){
}else{
if(!saved){
table.set("inv", ContainerMetadata.FromContainer(sender.getComponent("inventory").container));
}else{
ContainerMetadata.SaveToContainer(sender.getComponent("inventory").container, table.get("inv"));
}
saved = !saved;
}
});

0 comments on commit 484fef4

Please sign in to comment.