Skip to content

Instantly share code, notes, and snippets.

@einarwh
einarwh / invaders.html
Created November 6, 2011 20:59
HTML hosting page for space invaders!
<html>
<head>
<title>Invaders!</title>
<style type="text/css">
.invader { visibility: hidden }
</style>
</head>
<body>
<div class="invader">#990000</div>
<div class="invader">#009900</div>
@einarwh
einarwh / pixit.js
Created November 6, 2011 21:02
JavaScript to load a base64-encoded space invader.
var PixIt = {
load : function () {
var j = {
"pixelsWide": 13,
"pixelsHigh": 10,
"pixelSize": 8,
"payload":
[
{
"color": '#000000',
@einarwh
einarwh / WriteResponse.cs
Created November 6, 2011 21:04
Write base64-encoded response.
private static void WriteResponse(
HttpResponse response,
byte[] buffer)
{
response.ContentType = "plain/text";
response.Write(Convert.ToBase64String(buffer));
response.Flush();
}
@einarwh
einarwh / Voles.cs
Created December 4, 2011 20:52
Calculating the number of offspring for field voles.
public class Voles
{
private static int _daysBeforeFirst = 25;
private static int _daysBetween = 20;
private static Dictionary<int, long> _cache =
new Dictionary<int, long>();
public static long F(int days) {
if (!_cache.ContainsKey(days)) {
@einarwh
einarwh / ViewableAttribute.cs
Created March 24, 2012 07:31
Attribute to mark properties that should do automatic notification when they change value.
public class ViewableAttribute: Attribute {}
@einarwh
einarwh / NotificationTamperer.cs
Created April 8, 2012 21:44
Iterate over types in an assembly, looking for types to tamper with.
public class NotificationTamperer : ITamperer
{
private readonly string _assemblyOutputFileName;
public NotificationTamperer() : this("default_tampered.dll") {}
public NotificationTamperer(string assemblyOutputFileName)
{
_assemblyOutputFileName = assemblyOutputFileName;
}
@einarwh
einarwh / MaybeTamperWith.cs
Created April 8, 2012 22:02
Looking for properties to tamper with.
public bool MaybeTamperWith()
{
return _typeDef.IsClass
&& HasPropertiesToTamperWith()
&& ReallyTamperWith();
}
private bool HasPropertiesToTamperWith()
{
FindPropertiesToTamperWith();
@einarwh
einarwh / EnsureTypeImplementsInterface.cs
Created April 8, 2012 22:13
Ensure that the type implements the INotifyPropertyChanged interface.
private bool ReallyTamperWith()
{
EnsureTypeImplementsInterface();
TamperWithPropertySetters();
return true;
}
private void EnsureTypeImplementsInterface()
{
if (!TypeAlreadyImplementsInterface())
@einarwh
einarwh / TypeResolver.cs
Created April 28, 2012 19:07
Helper class to facilitate looking up TypeReference objects for types.
public class TypeResolver
{
private readonly TypeDefinition _typeDef;
private readonly IDictionary<Type, TypeReference> _typeRefs =
new Dictionary<Type, TypeReference>();
private readonly TypeSystem _ts;
private readonly ModuleDefinition _systemModule;
private readonly ModuleDefinition _mscorlibModule;
public TypeResolver(TypeDefinition typeDef)
@einarwh
einarwh / InjectEventHandler.cs
Created April 28, 2012 19:11
Code to inject event handler, without hookup methods.
private void InjectEventHandler()
{
InjectPropertyChangedField();
InjectEventDeclaration();
}
private void InjectPropertyChangedField()
{
//.field private class [System]System.ComponentModel.PropertyChangedEventHandler PropertyChanged
var field = new FieldDefinition(PropertyChangedFieldName,