get rid of s3

This commit is contained in:
Soph :3 2024-04-28 15:32:04 +03:00
parent 8cd07e8521
commit ef586df9a6
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
8 changed files with 86 additions and 86 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@
.vscode .vscode
plugins/* plugins/*
!plugins/commands.ts !plugins/commands.ts
!plugins/world.ts !plugins/world.ts
worlds/*

View file

@ -6,7 +6,7 @@ import {
World, World,
} from "./classes.ts"; } 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; type PlayerFunction = (a: Player) => void;
@ -36,30 +36,15 @@ export class Server {
this.server = Deno.listen({ port: port }); this.server = Deno.listen({ port: port });
try { try {
await s3.headBucket({ await Deno.stat("worlds/");
Bucket: "cla66ic", for await (const dirEntry of Deno.readDir("worlds/")) {
}); const world = new World({ x: 0, y: 0, z: 0 }, dirEntry.name.replace(".buf", ""));
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]);
this.worlds.push(world); this.worlds.push(world);
} }
}); } catch {
await Deno.mkdir("worlds")
}
if (config.onlineMode) { if (config.onlineMode) {
setInterval(async () => { setInterval(async () => {

View file

@ -1,14 +1,13 @@
import { gzip, ungzip } from "https://cdn.skypack.dev/pako"; import { gzip, ungzip } from "https://cdn.skypack.dev/pako";
import { s3 } from "../deps.ts";
import { Position } from "./classes.ts"; import { Position } from "./classes.ts";
import { cbor } from "../deps.ts";
export class World { export class World {
size: Position; size: Position;
data: Uint8Array; data: Uint8Array;
private dataView: DataView; private dataView: DataView;
name: string; name: string;
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
optionalJson: any = {}; metadata: any = {};
constructor(size: Position, name: string) { constructor(size: Position, name: string) {
this.size = size; this.size = size;
@ -72,39 +71,33 @@ export class World {
async delete() { async delete() {
try { try {
await s3.deleteObject({ await Deno.remove(`worlds/${this.name}.buf`)
Bucket: "cla66ic",
Key: this.name + ".buf",
});
} catch { } catch {
// doesn't exist, probably.. // gang
} }
} }
private async load() { private async load() {
try { try {
const head = await s3.headObject({
Bucket: "cla66ic",
Key: this.name + ".buf",
});
const ungziped = ungzip( const ungziped = ungzip(
(await s3.getObject({ await Deno.readFile(`worlds/${this.name}.buf`)
Bucket: "cla66ic",
Key: this.name + ".buf",
})).Body,
); );
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 = { this.size = {
x: +head.Metadata.x!, x: this.metadata.x!,
y: +head.Metadata.y!, y: this.metadata.y!,
z: +head.Metadata.z!, z: this.metadata.z!,
}; };
this.data = ungziped; this.data = ungziped.slice(cborSize+4);
this.dataView = new DataView(this.data.buffer); this.dataView = new DataView(this.data.buffer);
this.optionalJson = JSON.parse(head.Metadata.json || "{}"); } catch(e) {
} catch {
const layers = Math.floor(this.size.y / 2); const layers = Math.floor(this.size.y / 2);
for (let i = 0; i < layers; i += 1) { for (let i = 0; i < layers; i += 1) {
@ -118,16 +111,18 @@ export class World {
} }
async save() { async save() {
await s3.putObject({ const metadata = {
Bucket: "cla66ic", x: this.size.x!,
Key: this.name + ".buf", y: this.size.y!,
Body: gzip(this.data), z: this.size.z!,
Metadata: { ...this.metadata
"x": this.size.x + "", }
"y": this.size.y + "", const cborData = cbor.encode(metadata);
"z": this.size.z + "", const buffer = new Uint8Array(4 + cborData.byteLength + this.data.byteLength);
"json": JSON.stringify(this.optionalJson), 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)!);
} }
} }

5
deno.json Normal file
View file

@ -0,0 +1,5 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
}
}

30
deno.lock Normal file
View file

@ -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"
}
}

13
deps.ts
View file

@ -1,8 +1,6 @@
import "https://deno.land/x/dotenv@v3.2.0/load.ts"; 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"; export * as cbor from "https://deno.land/x/cbor@v1.5.9/index.js"
import { S3 } from "https://aws-api.deno.dev/v0.3/services/s3.ts";
export * as log from "https://deno.land/std@0.136.0/log/mod.ts"; 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 { crypto } from "https://deno.land/std@0.136.0/crypto/mod.ts";
export { EventEmitter } from "./events.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) => export const toHexString = (bytes: Uint8Array) =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), ""); 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 = { export const config = {
ops: Deno.env.get("OPS") ? JSON.parse(Deno.env.get("OPS")!) : [], ops: Deno.env.get("OPS") ? JSON.parse(Deno.env.get("OPS")!) : [],
port: +Deno.env.get("PORT")!, port: +Deno.env.get("PORT")!,

View file

@ -16,8 +16,8 @@ export default class CommandPlugin extends Plugin {
this.server = server; this.server = server;
this.on("setblock", (player, _mode, _id) => { this.on("setblock", (player, _mode, _id) => {
const world = server.worlds.find((e) => e.name == player.world)!; const world = server.worlds.find((e) => e.name == player.world)!;
if (!world.optionalJson?.builders?.includes("*")) { if (!world.metadata?.builders?.includes("*")) {
if (!world.optionalJson?.builders?.includes(player.username)) { if (!world.metadata?.builders?.includes(player.username)) {
player.message("You are %cnot allowed &fto build in this world!"); player.message("You are %cnot allowed &fto build in this world!");
return true; return true;
} }
@ -59,8 +59,8 @@ export default class CommandPlugin extends Plugin {
{ x: 64, y: 64, z: 64 }, { x: 64, y: 64, z: 64 },
player.username, player.username,
); );
world.optionalJson.builders = []; world.metadata.builders = [];
world.optionalJson.builders.push(player.username); world.metadata.builders.push(player.username);
server.worlds.push(world); server.worlds.push(world);
player.message(`&aWorld created!&f Use /g ${player.username}!`); player.message(`&aWorld created!&f Use /g ${player.username}!`);
@ -105,11 +105,11 @@ export default class CommandPlugin extends Plugin {
return; return;
} }
if (!world.optionalJson?.builders) world.optionalJson.builders = []; if (!world.metadata?.builders) world.metadata.builders = [];
if (subcategory == "add") { if (subcategory == "add") {
const username = args[2]; const username = args[2];
world.optionalJson.builders.push(username); world.metadata.builders.push(username);
player.message( player.message(
`&a${username}&f sucesfully added as a builder to world &a${world.name}!`, `&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") { } else if (subcategory == "remove") {
const username = args[2]; 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: string,
) => e !== username); ) => e !== username);
const after = world.optionalJson.builders.length; const after = world.metadata.builders.length;
player.message( player.message(
`Removed &a${ `Removed &a${
@ -134,7 +134,7 @@ export default class CommandPlugin extends Plugin {
} else if (subcategory == "list") { } else if (subcategory == "list") {
player.message( player.message(
`&a${world.name}&f's builders: &a${ `&a${world.name}&f's builders: &a${
world.optionalJson.builders.join(", ") world.metadata.builders.join(", ")
}`, }`,
); );
} else { } else {

View file

@ -12,8 +12,7 @@
### setup tutorial (be warned it's not the easiest) ### 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 1. configure .env file to look something like
2. configure .env file to look something like
``` ```
PORT=6969 PORT=6969
@ -21,10 +20,6 @@ HASH=RandomHashIlIke
OPS=["Me"] OPS=["Me"]
ONLINEMODE=true ONLINEMODE=true
MAIN=main 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 NOTE: if you are running inside of a cloud provider, just set these as your