Dump a SVN repository from a URL

November 7th, 2008

Dumping a repository is sometime necessary, for example when you want to transfer it from one place to the other, say from Google Code to Sourceforge. The dump is normally created using svnadmin. However if you ever try to dump a remote repository, you’ll find out that it can’t be done:

svnadmin dump http://projectname.googlecode.com/svn > repodump

svnadmin: 'http://projectname.googlecode.com/svn' is an URL when it should be a path

One way around that is to use svnsync. This utility allows you to synchronize a local depository with a remote one. So the technique here is to create an empty repository, synchronize it with the remote one, and, finally, dump the local repository.

Here are some notes on how to do that:

1. Create a local repository

svnadmin create c:/repository

2. Add a “pre-revprop-change” hook to the local repository.

echo > c:\repository\hooks\pre-revprop-change.cmd

This is done by opening the “hooks” folder of the repository and adding a blank “pre-revprop-change.cmd” file into it. This will enable revision property change, which is necessary for synchronization.

On Linux and Mac, please follow the instructions in this comment.

3. Synchronize your new repository with the remote one:

svnsync init file:///c:/repository https://example.googlecode.com/svn

svnsync sync file:///c:/repository

Note: if you get this error: “Cannot initialize a repository with content in it“, it means that you’ve made some changes to the repository. In which case, the best thing to do is to just delete it and start over. The repository must be completely empty for the synchronization to work.

4. Finally, once the synchronization is done, simply dump your local repository:

svnadmin dump c:/repository > repodump

If everything went well, repodump should now contain the exact content of the remote repository.

Everything in one batch script

The batch script below allows to do all that in just one script. Simply replace REPO_PATH and REPO_PATH_NUX by the path to your repository and REPO_URL by the repository URL.

SET REPO_PATH=c:\your\local\repository
SET REPO_PATH_NUX=file:///c:/your/local/repository
SET REPO_URL=https://example.com/remote/repository

REM Uncomment the line below if the repository folder already exists
REM rmdir %REPO_PATH% /s/q
mkdir %REPO_PATH%
svnadmin create %REPO_PATH%
echo > %REPO_PATH%\hooks\pre-revprop-change.cmd
svnsync init %REPO_PATH_NUX% %REPO_URL%
svnsync sync %REPO_PATH_NUX%
Bookmark and Share

Get the URL of a YouTube FLV in Flash

October 31st, 2008

YouTube recently changed the way it serves FLV videos and previous methods to get the URL of the FLV files no longer work. As a workaround, it is still possible to retrieve it by:

1. Downloading the YouTube webpage where the video is embedded
2. Parsing it to extract the “t” and “video_id” parameters
3. Building the FLV URL using the old method.

Obviously, having to download and parse the YouTube web page is a lot slower than the previous method, which was just about reading the HTTP headers, but at least it works.

The code snippet below allows to do just that:

public function getFLVURL(iYouTubeVideoURL) {
  // iYouTubeVideoURL is the "watch" URL such as:
  // http://www.youtube.com/watch?v=OVVvAnU3m6E

  var loader:URLLoader = new URLLoader();
  var req:URLRequest = new URLRequest(iYouTubeVideoURL);
  loader.addEventListener(Event.COMPLETE, this.getFLVURL_complete);
  loader.addEventListener(IOErrorEvent.IO_ERROR, this.getFLVURL_ioError);
  loader.load(req);
}

private function getFLVURL_complete(iEvent) {
  var pageData:String = iEvent.currentTarget.data;

  // Get the "t" parameter
  var regex:RegExp = /"t": "(.*?)"/;
  var result:Array = pageData.match(regex);
  if (result.length < 2) {
    trace("Error parsing YouTube page: Couldn't get 't' parameter");
    return;
  }
  var param_t = result[1];

  // Get the "video_id" parameter
  regex = /"video_id": "(.*?)"/;
  result = pageData.match(regex);
  if (result.length < 2) {
    trace("Error parsing YouTube page: Couldn't get 'video_id' parameter");
    return;
  }
  var param_video_id = result[1];

  var flvURL:String = constructFLVURL(param_video_id, param_t);
  trace("Here's the FLV URL: " + flvURL);

  flvTextBox.text = flvURL;
}

private function getFLVURL_ioError(iEvent) {
  var loader = iEvent.target;
  trace("Couldn't download YouTube web page: " + iEvent);
}

private function constructFLVURL (video_id:String, t:String):String {
  var str:String = "http://www.youtube.com/get_video.php?";
  str += "video_id=" + video_id;
  str += "&t=" + t;
  return str;
}

Update (01/12/2008):

As it turns out, this technique only works from Adobe AIR and not from a web page. This is because YouTube doesn’t allow crossdomain calls, which means that if a Flash application tries to fetch one of their pages, it will fail with a security error.

To go around this issue, it is possible to use a simple PHP stub file, which fetches the YouTube page and send it back to Flash. The PHP file would be as follow:

<?php

$url = urldecode($_GET["url"]);
header("Content-Type: text/html");
readfile($url);

?>

Then, in Flash, instead of calling the YouTube URL directly, you call the PHP URL:

var youTubeURL = "http://uk.youtube.com/watch?v=bxfpMGLMZ7Y"
var stubURL = "http://example.com/stub.php?url=" + escape(youTubeURL)
getFLVURL(stubURL);

This small demo illustrates all that. The source code with PHP stub is available here.

Update (19/03/2009):

I’ve posted a follow up to this post with some information on how to get the HD quality version of the videos:
Get the URL of an HD-quality Youtube video.

Bookmark and Share

Create a short unique ID in PHP

October 21st, 2008

Mads Kristensen describes a nice way of generating shorter UUIDs in C#. The idea is to take the 16 bytes of a UUID and convert them to a readable string via a Base64 encoder. The result is a string of 22 characters instead of 32, which is particularly convenient for web services since it makes the URLs 10 characters shorter.

I’ve implemented a version of it in PHP. Just copy and paste the code below and call createBase64UUID()

function createBase64UUID() {
  $uuid com_create_guid();
  $byteString "";

  // Remove the dashes from the string
  $uuid str_replace("-"""$uuid);

  // Remove the opening and closing brackets
  $uuid substr($uuid1strlen($uuid) - 2); 

  // Read the UUID string byte by byte
  for($i 0$i strlen($uuid); $i += 2) {
    // Get two hexadecimal characters
    $s substr($uuid$i2);
    // Convert them to a byte
    $d hexdec($s);
    // Convert it to a single character
    $c chr($d);
    // Append it to the byte string
    $byteString $byteString.$c;
  } 

  // Convert the byte string to a base64 string
  $b64uuid base64_encode($byteString);
  // Replace the "/" and "+" since they are reserved characters
  $b64uuid str_replace("/""_"$b64uuid);
  $b64uuid str_replace("+""-"$b64uuid);
  // Remove the trailing "=="
  $b64uuid substr($b64uuid0strlen($b64uuid) - 2); 

  return $b64uuid;
}
Bookmark and Share

Lost your widget?

June 30th, 2008

Now and then it might happen that one of your Yahoo! widgets disappears, and no matter what you try - uninstalling the widget, the engine, restarting the computer, etc. - it won’t come back.

When this happens, there are a number of things you can try to get it back:

» Use Widget Cleaner

This is the simplest solution. Just close the widget then clear the widget preferences using Widget Cleaner. This will reset all the widget preferences to their initial values, including the dimensions and positions of the windows, so the widget should be visible again.

However, it might not always work since Widget Cleaner currently can’t “detect” all the widgets. In that case, try the second solution.

» Check the widget Preferences

Some widget authors add an option in the Preferences to reset the window positions. You can access the Preferences by clicking on the Gear button in the Dock icon, then look for any option that might allow you to reset the windows. If such an option is available, use it and the widget should be visible again.

Widget preferences button

» Delete the registry settings

The last option is more complicated and only works on Windows, however it will definitely fix the issue in all cases. In order to reset the widget preferences (and therefore its visibility), you can open the registry editor and manually delete the appropriate settings. To do so, follow these instructions:

  1. Press the Windows Start button and click on Run
  2. Type “regedit” (without the quotes)
  3. Navigate to this folder: “HKEY_CURRENT_USER\Software\Yahoo\WidgetEngine\Widgets”
  4. Here you will find a number of subfolders. Each of them contains the Preferences of a widget.
  5. Find the name of the widget you are looking for, then right-click on its subfolder and select “Delete”.

For example, this is what you should see if you wish to delete the preferences of the “Day Planner Calendar” widget:

Delete Yahoo widget preferences

After doing so, restart your widget and normally it should be visible again.

Bookmark and Share

2D Polygon Collision Detection

June 27th, 2008

This article describes how to detect the collision between two moving (2D) polygons. It’s not the first tutorial on the topic, however, the tutorials on the net tend to be a bit too complex for a relatively simple problem. The source codes I could find also had too many abbreviations that I don’t get, or were crippled with C optimizations. So here, I’ll try to keep it as simple as possible. In any case, it should be possible to include the functions presented here to your C# projects quite straightforwardly. The technique can be used to detect collisions between sprites as an alternative to pixel-perfect collisions which are usually too slow.

Background

To detect if two polygons are intersecting, we use the Separating Axis Theorem. The idea is to find a line that separates both polygons - if such a line exists, the polygons are not intersecting (Fig. 1). The implementation of this theorem is relatively simple, and could be summed up in this pseudo code:

  • For each edge of both polygons:
    • Find the axis perpendicular to the current edge.
    • Project both polygons on that axis.
    • If these projections don't overlap, the polygons don't intersect (exit the loop).

This can be easily extended to deal with moving polygons by adding one additional step. After having checked that the current projections do not overlap, project the relative velocity of the polygons on the axis. Extend the projection of the first polygon by adding to it the velocity projection (Fig. 2). This will give you the interval spanned by the polygon over the duration of the motion. From there, you can use the technique used for static polygons: if the projections of polygons A and B don’t overlap, the polygons won’t collide. (NB: However, remember that if the intervals do overlap, it doesn’t necessarily mean that the polygons will collide. We need to do the test for all the edges before knowing that.)

Once we have found that the polygons are going to collide, we calculate the translation needed to push the polygons apart. The axis on which the projection overlapping is minimum will be the one on which the collision will take place. We will push the first polygon along this axis. Then, the amount of translation will simply be the amount of overlapping on this axis (Fig. 3).

That is it! Now, each time a collision occurs, the first polygon will nicely slide along the surface of the other polygon.

Projections of polygons onto an axis

Figure 1: Projections of the polygons onto an axis.

Projections of moving polygons onto an axis

Figure 2: Projections for moving polygons.

Polygon Collision

Figure 3: Find the minimum interval overlapping, then calculate the translation required to push the polygons apart.

Using the code

The PolygonCollision() function does all of the above, and returns a PolygonCollisionResult structure containing all the necessary information to handle the collision:

// Structure that stores the results of the PolygonCollision function

public struct PolygonCollisionResult {
    // Are the polygons going to intersect forward in time?

    public bool WillIntersect;
    // Are the polygons currently intersecting?

    public bool Intersect;
    // The translation to apply to the first polygon to push the polygons apart.

    public Vector MinimumTranslationVector;
}

Two helper functions are used by the PolygonCollision function. The first one is used to project a polygon onto an axis:

// Calculate the projection of a polygon on an axis

// and returns it as a [min, max] interval

public void ProjectPolygon(Vector axis, Polygon polygon,
                           ref float min, ref float max) {
    // To project a point on an axis use the dot product

    float dotProduct = axis.DotProduct(polygon.Points[0]);
    min = dotProduct;
    max = dotProduct;
    for (int i = 0; i < polygon.Points.Count; i++) {
        dotProduct = polygon.Points[i].DotProduct(axis);
        if (d < min) {
            min = dotProduct;
        } else {
            if (dotProduct> max) {
                max = dotProduct;
            }
        }
    }
}

The second one returns the signed distance between two given projections:

// Calculate the distance between [minA, maxA] and [minB, maxB]

// The distance will be negative if the intervals overlap

public float IntervalDistance(float minA, float maxA, float minB, float maxB) {
    if (minA < minB) {
        return minB - maxA;
    } else {
        return minA - maxB;
    }
}

Finally, here is the main function:

// Check if polygon A is going to collide with polygon B.

// The last parameter is the *relative* velocity 

// of the polygons (i.e. velocityA - velocityB)

public PolygonCollisionResult PolygonCollision(Polygon polygonA,
                              Polygon polygonB, Vector velocity) {
    PolygonCollisionResult result = new PolygonCollisionResult();
    result.Intersect = true;
    result.WillIntersect = true;

    int edgeCountA = polygonA.Edges.Count;
    int edgeCountB = polygonB.Edges.Count;
    float minIntervalDistance = float.PositiveInfinity;
    Vector translationAxis = new Vector();
    Vector edge;

    // Loop through all the edges of both polygons

    for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) {
        if (edgeIndex < edgeCountA) {
            edge = polygonA.Edges[edgeIndex];
        } else {
            edge = polygonB.Edges[edgeIndex - edgeCountA];
        }

        // ===== 1. Find if the polygons are currently intersecting =====

        // Find the axis perpendicular to the current edge

        Vector axis = new Vector(-edge.Y, edge.X);
        axis.Normalize();

        // Find the projection of the polygon on the current axis

        float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
        ProjectPolygon(axis, polygonA, ref minA, ref maxA);
        ProjectPolygon(axis, polygonB, ref minB, ref maxB);

        // Check if the polygon projections are currentlty intersecting

        if (IntervalDistance(minA, maxA, minB, maxB) > 0)\
            result.Intersect = false;

        // ===== 2. Now find if the polygons *will* intersect =====

        // Project the velocity on the current axis

        float velocityProjection = axis.DotProduct(velocity);

        // Get the projection of polygon A during the movement

        if (velocityProjection < 0) {
            minA += velocityProjection;
        } else {
            maxA += velocityProjection;
        }

        // Do the same test as above for the new projection

        float intervalDistance = IntervalDistance(minA, maxA, minB, maxB);
        if (intervalDistance > 0) result.WillIntersect = false;

        // If the polygons are not intersecting and won't intersect, exit the loop

        if (!result.Intersect && !result.WillIntersect) break;

        // Check if the current interval distance is the minimum one. If so store

        // the interval distance and the current distance.

        // This will be used to calculate the minimum translation vector

        intervalDistance = Math.Abs(intervalDistance);
        if (intervalDistance < minIntervalDistance) {
            minIntervalDistance = intervalDistance;
            translationAxis = axis;

            Vector d = polygonA.Center - polygonB.Center;
            if (d.DotProduct(translationAxis) < 0)
                translationAxis = -translationAxis;
        }
    }

    // The minimum translation vector

    // can be used to push the polygons appart.

    if (result.WillIntersect)
        result.MinimumTranslationVector =
               translationAxis * minIntervalDistance;

    return result;
}

The function can be used this way:

Vector polygonATranslation = new Vector();

PolygonCollisionResult r = PolygonCollision(polygonA, polygonB, velocity);

if (r.WillIntersect) {
  // Move the polygon by its velocity, then move

  // the polygons appart using the Minimum Translation Vector

  polygonATranslation = velocity + r.MinimumTranslationVector;
} else {
  // Just move the polygon by its velocity

  polygonATranslation = velocity;
}

polygonA.Offset(polygonATranslation);

Reference

Bookmark and Share

Copyright © Pogopixels Ltd, 2008-2010