Song_14

by Administrator 29. Februar 2012 10:24

New song added.

Enjoy :P

Tags:

Music | My Stuff

Song_13

by Administrator 27. Februar 2012 10:12

New song added.

Enjoy :P

Tags:

Music | My Stuff

Song_12

by Administrator 22. Februar 2012 09:44

New song added.

Enjoy :P

Tags:

Music | My Stuff

HTML5 Canvas and Unvoluntary Zooming

by Administrator 20. Februar 2012 04:55

This one almost drove me nuts. I wanted to play around with HTML canvas and got everything drawn massively zoomed. I added a canvas element to my page and used jQuery to blow it up to fill the browser canvas:


<canvas id="GameCanvas"></canvas>

$("#GameCanvas")
    .width($(window).width())
    .height($(window).height());

I did canvas before and never had problem but here everything was displayed massively zoomed. After loosing some hair and lots of cursing and swearing, i found out that setting the widht via css really enlarges the element, however it leaves the original coordinate system of the canvas (the default width/height) intact causing massive scaling to happen. The trick is to use the width and height attributes on the canvas element instead:

$("#GameCanvas")
    .attr("width", $(window).width())
    .attr("height", $(window).height());

Tags: ,

Html

Agent Smith with Resharper 6.1

by Administrator 13. Februar 2012 10:08

Agent Smith is a really great plugin for Resharper. Especially the spell check, commenting and naming convention capabilities are just great. Unfortunately JetBrains decided to do a big API overhaul for Resharper 6.1 and many plugins (including Agent Smith) stopped working. Though there is work going on to get Agent Smith working with Resharper 6.1, some people report that they cannot get the current version running. I'm having the same problem here. I'm not really sure, why - but recompiling on my box and replacing the DLL did the trick. However it does not bring the options back into the application. Perhaps if everything else suggested did not work for you and you can live with the default settings, try the dll.

AgentSmith.zip (70,12 kb)

Tags: ,

Analyzing method usage with Mono Cecil

by Administrator 4. Februar 2012 05:37

In a current project we wanted to have a way to prevent the usage of some standard overloads in our code and perform a check in the build process. We already have some infrastructure in place to check for things like missing comments and improper dependency injection attributes, so I put some thoughts into how this can be pulled off with a minimal amount of work.

I worked with Mono Cecil in a previous project and decided to give it a shot for static analyis.

When it comes to overloads, just scanning the source code for the name of method calls is not enough and some in depth knowledge is required. Mono Cecil offers an easy way to reflect on assemblies without actually loading them. This comes very convenient since you do not have to load all your dependencies to get ready to go. Furthermore it allows even working on Silverlight aseemblies which would be very hard to use for such a test in a build environment.

I did a little class called AssemblyScanner which I will use in this article. The source is attached. I first define a dictionary of method overloads that should not be used:

protected Dictionary<string, string> deniedOverloads = new Dictionary<string, string>()
{
    {"System.String System.String::Format(System.String,System.Object[])", "string.Format without specifiying CultureInfo"},
    {"System.String System.Int32::ToString()", "int.ToString() without specifying a CultureInfo"}
};

The value in the dictionary is just some nicer display for the user. The key is the part that matters. As you see, I want to prevent the usage of string.Format without specifying a culture and the use of int.ToString without a culture. A real goody since we're not working with reflection is, that we do not have to handle assembly references. I do not care where these methods actually live, so this approach is somehow version resilient (e.g. .NET framework updates).

The next step is to iterate over all methods in the assembly to scan. This is quite easy to do:

// Iterate over all types in the given assembly.
AssemblyDefinition assembly = AssemblyFactory.GetAssembly(assemblyName);
foreach (ModuleDefinition module in assembly.Modules)
{
    // The reader is used to obtain debugging information to locate positions
    // in the source code.
    using (ISymbolReader symbolReader = new PdbFactory().CreateReader(module, assemblyName))
    {
        foreach (TypeDefinition type in module.Types)
        {
            foreach (MethodDefinition method in type.Methods)
            {

              // Analyis goes here
            }
        }
    }
}

Probably the only interesting part here is the symbol reader. I did not notice this the last time working with Cecil, but that library has support for working with PDB files (debug information). This allows to find the location of the source code for an IL instruction in the assembly. Pretty neat Cool

When analyzing a method, we iterate over it's IL instructions. IL is a little like assembly, you've got an OpCode which represents the command you're doing and optional Operands that define the arguments to use. When using the debug information you may have a SequencePoint attached to your IL instruction. The sequence point is a link to the source code (file/line/character). Since this point is not given for each instruction I just track the last one available and update it when a new one is defined.

The core is then to search for CALL operations that reference the overloads you are scanning for:

// Scan for CALL operations with an operand matching our denied
// signatures.
if (instruction.OpCode.Equals(OpCodes.Call) && deniedOverloads.ContainsKey(instruction.Operand.ToString()))
{
    // Emit a result.
}

As shown in the example this works for static methods like string.Format as well as for instance methods line int.ToString. The reason is, that both calls use the same opcode but differ in their operand. The operand has a property indicating whether a this reference (object instance) is available.

Here's the source code for the project. Feel free to play with it.

SigCheck.zip (166,51 kb)

Tags: , , ,

C# | My Stuff | Projects

About the author

for comments and suggestions contact:

 

Month List