Skip to content

Instantly share code, notes, and snippets.

View michel-pi's full-sized avatar

Michel michel-pi

View GitHub Profile
@michel-pi
michel-pi / LazyType.cs
Created September 30, 2018 22:47
How Lazy<T> should have been implemented
using System;
using System.Threading;
namespace System
{
public class LazyType<T>
{
private static readonly Func<T> DefaultValueFactory = () => default(T);
private readonly object _lock;
@michel-pi
michel-pi / KeyboardState.cs
Created September 30, 2018 23:21
Retreives a buffer containing all keys state
using System;
using System.Runtime.InteropServices;
namespace System.Input
{
public class KeyboardState
{
[DllImport("user32.dll")]
private static extern int GetKeyboardState(byte[] buffer);
@michel-pi
michel-pi / DynamicImport.cs
Last active December 23, 2018 08:42
Import methods from .dll's at runtime
using System;
using System.ComponentModel;
using System.Runtime.Serialization;
namespace System.Runtime.InteropServices
{
public static class DynamicImport
{
[DllImport("kernel32.dll", EntryPoint = "GetProcAddress", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procname);
@michel-pi
michel-pi / AsyncKeyState.cs
Last active December 23, 2018 08:48
GetAsyncKeyState wrapper
using System;
using System.Runtime.InteropServices;
namespace System.Input
{
public static class AsyncKeyState
{
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int key);
@michel-pi
michel-pi / IntPtrExtensions.cs
Last active December 23, 2018 08:50
Extension methods to validate IntPtr's and count with them
using System;
namespace System
{
public static class IntPtrExtensions
{
private static readonly bool _x86 = IntPtr.Size == 4;
public static ulong GetValue(this IntPtr ptr)
{
@michel-pi
michel-pi / StaticRandom.cs
Created April 1, 2019 07:20
A static and thread safe pseudo-random number generator
using System;
using System.Threading;
namespace System
{
/// <summary>
/// Represents a static and thread safe pseudo-random number generator, which is a device that produces a sequence of numbers that meet certain statistical requirements for randomness.
/// </summary>
public static class StaticRandom
{
@michel-pi
michel-pi / HashProvider.cs
Created May 29, 2019 18:01
Provides static methods to use hash algorithms in a generic way.
using System;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace System.Security.Cryptography.Provider
{
/// <summary>
/// Provides static methods to use hash algorithms in a generic way.
/// </summary>
@michel-pi
michel-pi / WindowExtensions.cs
Last active May 29, 2019 18:10
Provides extension methods for WPF windows such as Invoke, enabling drag move, control sliders through the mouse wheel and enumerating controls.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Media3D;
namespace System.Windows.Extensions
{
function DiscordWidgetData() {
this.load = async function (guildId) {
return new Promise(function (resolve, reject) {
fetch("https://discordapp.com/api/v6/guilds/" + guildId + "/widget.json", {
method: "GET",
mode: "cors",
cache: "reload",
redirect: "follow"
}).then(response => {
if (!response.ok) {
@michel-pi
michel-pi / DateTimeFormat.php
Created October 12, 2020 22:34
Formats DateTime for usage in JSON and MySQL Databases
<?php
namespace MichelPi\Utils;
use DateTime;
class DateTimeFormat
{
private static $_jsonFormatString = 'Y-m-d\\TH:i:s.v\\Z';
private static $_mysqlFormatString = 'Y-m-d H:i:s';