Skip to content

Instantly share code, notes, and snippets.

@xxshady
xxshady / safe_baseobjects.rs
Last active June 21, 2024 18:38
safe baseobject api concept
fn main(main_context: &MainContext) {
let _players: &[Player] = main_context.all_players();
every_tick(|ctx: &EveryTickContext| {
// cannot be called here
// main_context.all_players();
let _players: &[Player] = ctx.all_players();
// TODO: allow holding reference for unknown duration to player via Option?
@xxshady
xxshady / wait.rs
Last active June 17, 2024 22:41
Simple WASM wait/sleep implementation
use crate::timers::set_timeout;
use std::{
future::{poll_fn, Future},
task::Poll,
};
use web_time::{Duration, SystemTime};
pub fn wait(duration: Duration) -> impl Future {
let dest = SystemTime::now() + duration;
let mut timer_was_set = false;
@xxshady
xxshady / timers.rs
Last active June 12, 2024 11:09
Simple WASM (in JS environment) Rust timers implementation
// depends on web_time + wasm_bindgen
use std::{
cell::{RefCell, RefMut},
fmt::Debug,
};
use web_time::{Duration, SystemTime};
pub type TimerId = u64;
@xxshady
xxshady / altv_esbuild_rust_wasm_example.js
Last active June 8, 2024 22:05
Simple WASM Rust async executor + spawn_future helper
// js usage example for https://github.com/xxshady/altv-esbuild-rust-wasm
import alt from "alt-shared"
import loadWasm from "./rust-wasm/pkg/rust_wasm_bg.wasm"
const { on_every_tick } = loadWasm({})
alt.everyTick(on_every_tick);
@xxshady
xxshady / lib.rs
Created April 30, 2024 18:18
working tokio multi threading on alt:V
use std::{
cell::RefCell,
sync::{Arc, Mutex},
};
#[altv::main]
fn main() -> impl altv::IntoVoidResult {
let rt = Arc::new(RefCell::new(Some(tokio::runtime::Runtime::new().unwrap())));
let data = Arc::new(Mutex::new(1));
@xxshady
xxshady / client.js
Last active May 28, 2024 20:02
Saving clientside JS heap snaphot as file on alt:V
import alt from "alt-client"
const STRING_LIMIT = 2 ** 12
const sendSnapshot = async (string) => {
const len = string.length
const parts = Math.ceil(len / STRING_LIMIT)
alt.log("sendSnapshot", "parts:", parts, "len:", len)
@xxshady
xxshady / client.js
Last active December 31, 2023 13:39
alt:V player look sync
import alt from "alt-client"
import native from "natives"
const localPlayer = alt.Player.local
const SYNC_MS_DELAY = 500
const SYNC_MS_DELAY_REMOTE = 100
const MAX_HEADING_DIST = 70.0
const BACKWARDS_HEADING_DIST = 145.0
const TASK_LOOK_AT_COORD_DURATION = 300
const LOOKING_AT_POS_OFFSET = 5.0
@xxshady
xxshady / external-ip.ts
Last active December 7, 2021 21:32
external ip nodejs
import https from "https"
const options = {
hostname: "ipinfo.io",
path: "/ip",
method: "GET",
}
export const getExternalIp = (): Promise<string> =>
new Promise<string>((resolve, reject) => {