From ef586df9a688e81810a549f59131ef3be2b117a8 Mon Sep 17 00:00:00 2001 From: sophie Date: Sun, 28 Apr 2024 15:32:04 +0300 Subject: [PATCH] get rid of s3 --- .gitignore | 3 ++- classes/Server.ts | 29 +++++---------------- classes/World.ts | 65 ++++++++++++++++++++++------------------------- deno.json | 5 ++++ deno.lock | 30 ++++++++++++++++++++++ deps.ts | 13 +--------- plugins/world.ts | 20 +++++++-------- readme.md | 7 +---- 8 files changed, 86 insertions(+), 86 deletions(-) create mode 100644 deno.json create mode 100644 deno.lock diff --git a/.gitignore b/.gitignore index db54048..d7d5982 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .vscode plugins/* !plugins/commands.ts -!plugins/world.ts \ No newline at end of file +!plugins/world.ts +worlds/* \ No newline at end of file diff --git a/classes/Server.ts b/classes/Server.ts index 8ee992c..ec73dd4 100644 --- a/classes/Server.ts +++ b/classes/Server.ts @@ -6,7 +6,7 @@ import { World, } from "./classes.ts"; -import { config, crypto, log, s3, toHexString } from "../deps.ts"; +import { config, crypto, log, toHexString } from "../deps.ts"; type PlayerFunction = (a: Player) => void; @@ -36,30 +36,15 @@ export class Server { this.server = Deno.listen({ port: port }); try { - await s3.headBucket({ - Bucket: "cla66ic", - }); - - log.info("s3 bucket exists!"); - } catch { - log.warning("s3 bucket does not exist.. Creating!"); - - await s3.createBucket({ - Bucket: "cla66ic", - }); - } - - (await s3.listObjects({ - Bucket: "cla66ic", - })).Contents.forEach((e) => { - if (e.Key !== config.main + ".buf") { - log.info(`Autoloaded a world from s3! ${e.Key}`); - - const world = new World({ x: 0, y: 0, z: 0 }, e.Key!.split(".buf")[0]); + await Deno.stat("worlds/"); + for await (const dirEntry of Deno.readDir("worlds/")) { + const world = new World({ x: 0, y: 0, z: 0 }, dirEntry.name.replace(".buf", "")); this.worlds.push(world); } - }); + } catch { + await Deno.mkdir("worlds") + } if (config.onlineMode) { setInterval(async () => { diff --git a/classes/World.ts b/classes/World.ts index 78e4e01..f7d9030 100644 --- a/classes/World.ts +++ b/classes/World.ts @@ -1,14 +1,13 @@ import { gzip, ungzip } from "https://cdn.skypack.dev/pako"; -import { s3 } from "../deps.ts"; import { Position } from "./classes.ts"; - +import { cbor } from "../deps.ts"; export class World { size: Position; data: Uint8Array; private dataView: DataView; name: string; // deno-lint-ignore no-explicit-any - optionalJson: any = {}; + metadata: any = {}; constructor(size: Position, name: string) { this.size = size; @@ -72,39 +71,33 @@ export class World { async delete() { try { - await s3.deleteObject({ - Bucket: "cla66ic", - Key: this.name + ".buf", - }); + await Deno.remove(`worlds/${this.name}.buf`) } catch { - // doesn't exist, probably.. + // gang } } + private async load() { try { - const head = await s3.headObject({ - Bucket: "cla66ic", - Key: this.name + ".buf", - }); - const ungziped = ungzip( - (await s3.getObject({ - Bucket: "cla66ic", - Key: this.name + ".buf", - })).Body, + await Deno.readFile(`worlds/${this.name}.buf`) ); - if (!(ungziped instanceof Uint8Array)) return; + if (!(ungziped instanceof Uint8Array)) return; + const dv = new DataView(ungziped.buffer); + + const cborSize = dv.getUint32(0); + this.metadata = cbor.decode(new Uint8Array(ungziped.buffer.slice(4, cborSize+4))); + this.size = { - x: +head.Metadata.x!, - y: +head.Metadata.y!, - z: +head.Metadata.z!, + x: this.metadata.x!, + y: this.metadata.y!, + z: this.metadata.z!, }; - this.data = ungziped; + this.data = ungziped.slice(cborSize+4); this.dataView = new DataView(this.data.buffer); - this.optionalJson = JSON.parse(head.Metadata.json || "{}"); - } catch { + } catch(e) { const layers = Math.floor(this.size.y / 2); for (let i = 0; i < layers; i += 1) { @@ -118,16 +111,18 @@ export class World { } async save() { - await s3.putObject({ - Bucket: "cla66ic", - Key: this.name + ".buf", - Body: gzip(this.data), - Metadata: { - "x": this.size.x + "", - "y": this.size.y + "", - "z": this.size.z + "", - "json": JSON.stringify(this.optionalJson), - }, - }); + const metadata = { + x: this.size.x!, + y: this.size.y!, + z: this.size.z!, + ...this.metadata + } + const cborData = cbor.encode(metadata); + const buffer = new Uint8Array(4 + cborData.byteLength + this.data.byteLength); + const dv = new DataView(buffer.buffer); + dv.setUint32(0, cborData.byteLength); + buffer.set(cborData, 4); + buffer.set(this.data, 4 + cborData.byteLength); + await Deno.writeFile(`worlds/${this.name}.buf`, gzip(buffer)!); } } diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..3c5130f --- /dev/null +++ b/deno.json @@ -0,0 +1,5 @@ +{ + "tasks": { + "dev": "deno run --watch main.ts" + } +} diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..ae06d9b --- /dev/null +++ b/deno.lock @@ -0,0 +1,30 @@ +{ + "version": "3", + "remote": { + "https://cdn.skypack.dev/-/pako@v2.1.0-45rMaYN3B7tIGOhONLdV/dist=es2019,mode=imports/optimized/pako.js": "0e70f8f81aaa277e359308e2f07d560fd6c9f98e2bb8968301f237bf7f34c1a8", + "https://cdn.skypack.dev/pako": "cc52bc53fdee35b4854461706ea3655b8bda1a3b48ed170f16d0050f6b8534d5", + "https://deno.land/std@0.136.0/_util/assert.ts": "e94f2eb37cebd7f199952e242c77654e43333c1ac4c5c700e929ea3aa5489f74", + "https://deno.land/std@0.136.0/_wasm_crypto/crypto.mjs": "3b383eb715e8bfe61b4450ef0644b2653429c88d494807c86c5235979f62e56b", + "https://deno.land/std@0.136.0/_wasm_crypto/crypto.wasm.mjs": "0ad9ecc0d03ca8a083d9109db22e7507f019f63cf55b82ea618ab58855617577", + "https://deno.land/std@0.136.0/_wasm_crypto/mod.ts": "30a93c8b6b6c5b269e96a3e95d2c045d86a496814a8737443b77cad941d6a0b5", + "https://deno.land/std@0.136.0/bytes/bytes_list.ts": "67eb118e0b7891d2f389dad4add35856f4ad5faab46318ff99653456c23b025d", + "https://deno.land/std@0.136.0/bytes/equals.ts": "a60ef9f01fb6e06a4e0343fc44d53d39d12dd66bc3f09ac8e6eb9cc1a6335e48", + "https://deno.land/std@0.136.0/bytes/mod.ts": "4cef6fe8f0de217b9babbcbb0a566402b667f18a8e6d094a45e5fb3fc1afff70", + "https://deno.land/std@0.136.0/crypto/mod.ts": "a10fee951ddf2c91ae5938ba2a1d123580f773e533008988ba6089ddd2b65d67", + "https://deno.land/std@0.136.0/encoding/base64.ts": "c8c16b4adaa60d7a8eee047c73ece26844435e8f7f1328d74593dbb2dd58ea4f", + "https://deno.land/std@0.136.0/fmt/colors.ts": "30455035d6d728394781c10755351742dd731e3db6771b1843f9b9e490104d37", + "https://deno.land/std@0.136.0/fs/exists.ts": "cb734d872f8554ea40b8bff77ad33d4143c1187eac621a55bf37781a43c56f6d", + "https://deno.land/std@0.136.0/io/buffer.ts": "bd0c4bf53db4b4be916ca5963e454bddfd3fcd45039041ea161dbf826817822b", + "https://deno.land/std@0.136.0/log/handlers.ts": "b88c24df61eaeee8581dbef3622f21aebfd061cd2fda49affc1711c0e54d57da", + "https://deno.land/std@0.136.0/log/levels.ts": "82c965b90f763b5313e7595d4ba78d5095a13646d18430ebaf547526131604d1", + "https://deno.land/std@0.136.0/log/logger.ts": "30770bf29053d1c8d1054ec52ab385da8e5b8a43973da32c84dea819e5a11b52", + "https://deno.land/std@0.136.0/log/mod.ts": "e200a8600a9e9ac2b10668fda0e6537a1444c5050ea6a04c826df52519bf35fd", + "https://deno.land/x/cbor@v1.5.9/decode.js": "631f504e9e811609cb388a7f226d2054fd4f432eb188423e46be10443746b305", + "https://deno.land/x/cbor@v1.5.9/encode.js": "c28da2b6291e271e1f55bc3add9119fa23ddee5274940c09ee3c4c67f7ba45d3", + "https://deno.land/x/cbor@v1.5.9/index.js": "cc8678819d77aa34b6fa9293658d85d5e53e53eaf555f85b0f98a8a18dbfaa12", + "https://deno.land/x/cbor@v1.5.9/iterators.js": "744e0469fe37c33bab3787608ced2f2cda014cb9352b3adbd949a2701f043aea", + "https://deno.land/x/dotenv@v3.2.0/load.ts": "cbd76a0aee01aad8d09222afaa1dd04b84d9d3e44637503b01bf77a91df9e041", + "https://deno.land/x/dotenv@v3.2.0/mod.ts": "077b48773de9205266a0b44c3c3a3c3083449ed64bb0b6cc461b95720678d38e", + "https://deno.land/x/dotenv@v3.2.0/util.ts": "693730877b13f8ead2b79b2aa31e2a0652862f7dc0c5f6d2f313f4d39c7b7670" + } +} diff --git a/deps.ts b/deps.ts index cd87316..ca2cd75 100644 --- a/deps.ts +++ b/deps.ts @@ -1,8 +1,6 @@ import "https://deno.land/x/dotenv@v3.2.0/load.ts"; -import { ApiFactory } from "https://deno.land/x/aws_api@v0.7.0/client/mod.ts"; -import { S3 } from "https://aws-api.deno.dev/v0.3/services/s3.ts"; - +export * as cbor from "https://deno.land/x/cbor@v1.5.9/index.js" export * as log from "https://deno.land/std@0.136.0/log/mod.ts"; export { crypto } from "https://deno.land/std@0.136.0/crypto/mod.ts"; export { EventEmitter } from "./events.ts"; @@ -10,15 +8,6 @@ import "https://deno.land/x/dotenv@v3.2.0/load.ts"; export const toHexString = (bytes: Uint8Array) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), ""); -export const s3 = new ApiFactory({ - credentials: { - awsAccessKeyId: Deno.env.get("S3_ACCESS_KEY_ID")!, - awsSecretKey: Deno.env.get("S3_SECRET_KEY")!, - }, - fixedEndpoint: Deno.env.get("S3_ENDPOINT_URL"), - region: "us-west-004", -}).makeNew(S3); - export const config = { ops: Deno.env.get("OPS") ? JSON.parse(Deno.env.get("OPS")!) : [], port: +Deno.env.get("PORT")!, diff --git a/plugins/world.ts b/plugins/world.ts index 03f403c..8432b80 100644 --- a/plugins/world.ts +++ b/plugins/world.ts @@ -16,8 +16,8 @@ export default class CommandPlugin extends Plugin { this.server = server; this.on("setblock", (player, _mode, _id) => { const world = server.worlds.find((e) => e.name == player.world)!; - if (!world.optionalJson?.builders?.includes("*")) { - if (!world.optionalJson?.builders?.includes(player.username)) { + if (!world.metadata?.builders?.includes("*")) { + if (!world.metadata?.builders?.includes(player.username)) { player.message("You are %cnot allowed &fto build in this world!"); return true; } @@ -59,8 +59,8 @@ export default class CommandPlugin extends Plugin { { x: 64, y: 64, z: 64 }, player.username, ); - world.optionalJson.builders = []; - world.optionalJson.builders.push(player.username); + world.metadata.builders = []; + world.metadata.builders.push(player.username); server.worlds.push(world); player.message(`&aWorld created!&f Use /g ${player.username}!`); @@ -105,11 +105,11 @@ export default class CommandPlugin extends Plugin { return; } - if (!world.optionalJson?.builders) world.optionalJson.builders = []; + if (!world.metadata?.builders) world.metadata.builders = []; if (subcategory == "add") { const username = args[2]; - world.optionalJson.builders.push(username); + world.metadata.builders.push(username); player.message( `&a${username}&f sucesfully added as a builder to world &a${world.name}!`, ); @@ -117,13 +117,13 @@ export default class CommandPlugin extends Plugin { } else if (subcategory == "remove") { const username = args[2]; - const before = world.optionalJson.builders.length; + const before = world.metadata.builders.length; - world.optionalJson.builders = world.optionalJson.builders.filter(( + world.metadata.builders = world.metadata.builders.filter(( e: string, ) => e !== username); - const after = world.optionalJson.builders.length; + const after = world.metadata.builders.length; player.message( `Removed &a${ @@ -134,7 +134,7 @@ export default class CommandPlugin extends Plugin { } else if (subcategory == "list") { player.message( `&a${world.name}&f's builders: &a${ - world.optionalJson.builders.join(", ") + world.metadata.builders.join(", ") }`, ); } else { diff --git a/readme.md b/readme.md index 39595f0..17036a5 100644 --- a/readme.md +++ b/readme.md @@ -12,8 +12,7 @@ ### setup tutorial (be warned it's not the easiest) -1. make a backblaze b2 account, make a bucket, and get your keys from the bucket -2. configure .env file to look something like +1. configure .env file to look something like ``` PORT=6969 @@ -21,10 +20,6 @@ HASH=RandomHashIlIke OPS=["Me"] ONLINEMODE=true MAIN=main - -S3_ACCESS_KEY_ID="MyAccessKey" -S3_SECRET_KEY="SecretKey" -S3_ENDPOINT_URL="https://s3.us-west-004.backblazeb2.com" ``` NOTE: if you are running inside of a cloud provider, just set these as your