Skip to content

Instantly share code, notes, and snippets.

View Yoplitein's full-sized avatar
🔈
[screaming]

Steven Dwy Yoplitein

🔈
[screaming]
View GitHub Profile
@Yoplitein
Yoplitein / png.rs
Last active October 25, 2024 09:17
Low budget PNG encoder
#![allow(non_snake_case)]
use std::io::{Cursor, Error, Result, Write};
macro_rules! write_val {
($writer:expr, $val:expr, $bigEndian:expr) => {{
let bytes = if $bigEndian {
$val.to_be_bytes()
} else {
$val.to_le_bytes()
@Yoplitein
Yoplitein / rustfmt.toml
Created September 1, 2024 06:40
rustfmt configuration
edition = "2021"
binop_separator = "Back"
condense_wildcard_suffixes = true
format_strings = true
group_imports = "StdExternalCrate"
hard_tabs = true
hex_literal_case = "Upper"
imports_granularity = "Module"
imports_layout = "HorizontalVertical"
@Yoplitein
Yoplitein / cfpackdl.py
Last active June 16, 2024 07:13
Script to download CurseForge modpacks
from urllib import request
import io
import json
import os
import sys
import time
import urllib
import zipfile
def main():
#![feature(return_position_impl_trait_in_trait)]
trait ComposableOnce<From, To>: FnOnce(From) -> To {
fn compose_once<Final>(self, rhs: impl FnOnce(To) -> Final) -> impl FnOnce(From) -> Final;
}
impl<From, To, Func: FnOnce(From) -> To> ComposableOnce<From, To> for Func {
fn compose_once<Final>(self, rhs: impl FnOnce(To) -> Final) -> impl FnOnce(From) -> Final {
move |v| rhs(self(v))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct VarInt(u128);
macro_rules! conversions {
($($ty:ty => $unsigned:ty, $signed:expr);*;) => {$(
impl From<$ty> for VarInt {
fn from(v: $ty) -> Self {
let raw = if $signed {
// rotate sign bit to front, and flip value bits if negative
// this encodes small absolute values in the fewest bytes possible
#[derive(Clone, Debug)]
pub struct SurrogateString(String);
impl SurrogateString {
unsafe fn inner(&self) -> &String {
&self.0
}
unsafe fn inner_mut(&mut self) -> &mut String {
&mut self.0
@Yoplitein
Yoplitein / builder.d
Created September 5, 2022 22:32
Generic implementation of the builder pattern for any struct/class (though only for public fields)
auto builder(T)(T inst = T.init)
{
enum isClass = is(T == class);
static assert(is(T == struct) || isClass, "builder!T can only build structs and classes");
static if(isClass)
if(inst is null) inst = new T;
static struct Builder
{
@Yoplitein
Yoplitein / ringbuffer.d
Last active August 11, 2022 04:51
Fixed-capacity ring buffer type
struct RingBuffer(T, size_t _capacity)
{
import core.lifetime: move;
private
{
T[capacity] buffer;
size_t read;
size_t write;
size_t len; // keep it simple :^)
@Yoplitein
Yoplitein / counting_alloc.rs
Last active October 10, 2022 06:55
GlobalAllocator that tracks number of allocated bytes
use std::sync::atomic::{AtomicUsize, Ordering};
use std::alloc;
#[global_allocator]
static COUNTING_ALLOC: CountingAlloc = CountingAlloc::new();
struct CountingAlloc {
used: AtomicUsize,
}
@Yoplitein
Yoplitein / mappingutil.as
Last active March 25, 2022 22:14
Commands to aid in developing Sven Co-Op maps
void PluginInit()
{
g_Module.ScriptInfo.SetAuthor("Yoplitein");
g_Module.ScriptInfo.SetContactInfo("[email protected]");
}
void dbgDrawLine(const Vector&in pt1, const Vector&in pt2, uint16 lifetimeDs = 25, uint8 r = 255, uint8 g = 127, uint8 b = 0)
{
NetworkMessage msg(MSG_BROADCAST, NetworkMessages::SVC_TEMPENTITY, null);
msg.WriteByte(TE_LINE);