Skip to content

Instantly share code, notes, and snippets.

@karlgluck
karlgluck / ECS.cs
Last active May 17, 2018 00:59
My quick and incomplete version of Overwatch's Entity-Component System from the GDC17 presentation. Mostly written to think about how this would be implemented in a language with reflection.
public class ECSEntity
{
public ArrayList Components;
public static ECSEntity Acquire ()
{
throw new System.NotImplementedException();
ECSEntity retval = null;
return retval;
}
@karlgluck
karlgluck / GvimOmniSharpSetup.md
Last active September 21, 2017 20:13
Sadly, Gvim + OmniSharp-vim is just not worth the hassle on Windows. Locks up for 1s+ every cursor movement.

How to set up Gvim + OmniSharp-vim on Windows 10

Install Gvim

Right-click the C:\Program Files (x86)\Vim folder and change its Security permissions to allow full control & editing -> You shouldn't have to give admin permissions to do anything else in this guide

Install pathogen.vim

Clone OmniSharp-vim to C:\Program Files (x86)\Vim\vimfiles\bundle\omnisharp-vim

@karlgluck
karlgluck / VectorMagnitude.md
Created October 13, 2017 05:11
Approximate magnitude of 2d and 3d vectors

Vec3 Magnitude

These C# implementations of approximate magnitude with no square roots are actually slower than calling a native implementation in Unity, but if this were implemented intelligently in C there's a good chance they would be faster. In any case, they're here because it was a pain to get the magic numbers.

Accurate to within 4.5%.


public static float MagnitudeFast (this Vector2 self)
    {
private static Vector3 ViewportToWorldPointUnity (Vector3 point, float fieldOfView, float aspect, Vector3 from, Vector3 to, Vector3 up)
{
// The zNear / zFar magic numbers are what Unity's viewport always uses regardless of camera settings
var matP = Matrix4x4.Perspective (fieldOfView, aspect, 0.6f /* zNear */, 1000f /* zFar */);
// For some reason we have to reverse these coordinates
from = -from;
to = -to;
@karlgluck
karlgluck / PerfectPixelCamera.cs
Created September 13, 2018 22:35
Main file from the Perfect Pixel Camera asset on the Unity asset store, licensed with MIT https://assetstore.unity.com/packages/tools/camera/perfect-pixel-camera-by-gg-ez-100000
// Copyright 2018 Karl Gluck
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@karlgluck
karlgluck / RoombaVictory.ps1
Created September 18, 2019 00:55
Play the Roomba's victory jingle using the Powershell beep. Because why not?
[console]::beep(260,200)
[console]::beep(320,200)
[console]::beep(380,150)
[console]::beep(520,200)
Start-Sleep -Milliseconds 200
[console]::beep(520,150)
[console]::beep(700,800)
@karlgluck
karlgluck / DownloadFromPrivateGitHubRepoExample.ps1
Created May 14, 2022 17:32
One-liner for downloading from a private GitHub repository
# Fill these in with your own values
$Username = "your_username"
$Branch = "main"
$PathToFile = "README.md"
# Go to [https://github.com/settings/tokens] and add a Personal Access Token with two scopes: "repo" and "read:org"
$GitHubAuthToken = "ghp_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Invoke-WebRequest -Method Get -Uri "https://raw.githubusercontent.com/$Username/$Repository/$Branch/$PathToFile" -Headers @{Authorization="token $GitHubAuthToken"; 'Cache-Control'='no-cache'} | ForEach-Object { $_.Content } | Write-Host
@karlgluck
karlgluck / ExtractFileFromZipExample.ps1
Created May 14, 2022 17:46
How to extract a single file from a ZIP archive using Powershell
$ZipFilePath = ""
$FileNameInZip = ""
Add-Type -AssemblyName System.IO.Compression.FileSystem
$Zip = [System.IO.Compression.ZipFile]::OpenRead($ZipFilePath)
$Zip.Entries |
Where-Object { $_.Name -eq $FileNameInZip } |
ForEach-Object {
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($_, $_.Name, $true)
@karlgluck
karlgluck / CreatePathIfDoesntExistExample.ps1
Created May 14, 2022 19:57
One-liner to create a path if it doesn't exist
if (-not (Test-Path $Path)) { New-Item $Path -ItemType Directory | Out-Null }
@karlgluck
karlgluck / RunAtLoginExample.ps1
Created May 15, 2022 19:02
How to register a script block to run with elevated privileges when the current user logs in
$JobName = "RunAtLoginExample"
$Enabled = $True # set to $False and run to remove the job
# https://stackoverflow.com/questions/40569045/register-scheduledjob-as-the-system-account-without-having-to-pass-in-credentia
# Output: %LOCALAPPDATA%\Microsoft\Windows\PowerShell\ScheduledJobs\$JobName
$accountId = ((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username))
$Trigger = New-JobTrigger -AtLogOn
$JobOptions = New-ScheduledJobOption -StartIfOnBattery -RunElevated