Skip to content

Instantly share code, notes, and snippets.

@emoose
Last active March 28, 2017 14:39
Show Gist options
  • Save emoose/309fd3a870bd6af60258284eaa316f3d to your computer and use it in GitHub Desktop.
Save emoose/309fd3a870bd6af60258284eaa316f3d to your computer and use it in GitHub Desktop.
BSPver 0.1
using System;
using System.Collections.Generic;
using System.IO;
namespace BSPver
{
class Program
{
struct BSPVersion
{
public uint formatVersion;
public uint mapVersion;
}
static string startDir = "";
static void CheckFolder(string folder, ref Dictionary<string, BSPVersion> versions)
{
foreach(var file in Directory.GetFiles(folder, "*.bsp"))
{
if (!versions.ContainsKey(file))
versions.Add(file, GetBSPVer(file));
}
foreach (var dir in Directory.GetDirectories(folder))
CheckFolder(dir, ref versions);
}
static void Main(string[] args)
{
startDir = Directory.GetCurrentDirectory();
var dict = new Dictionary<string, BSPVersion>();
// if we have an arg, check if it's a path to a bsp file, if it is we'll just grab the version of it
// but if it's not a bsp it must be a folder path, so set our startDir to that
if (args.Length > 1)
{
if (args[1].ToLower().EndsWith(".bsp"))
{
dict.Add(args[1], GetBSPVer(args[1]));
startDir = "";
}
else
startDir = args[1];
}
if (!string.IsNullOrEmpty(startDir))
{
startDir = Path.GetFullPath(startDir) + Path.DirectorySeparatorChar; // fix up the path incase the user used wrong slashes or something
CheckFolder(startDir, ref dict);
}
foreach(var kvp in dict)
{
var name = kvp.Key.Replace(startDir, ""); // remove startDir from the filename (not using Path.GetFileName so that it'll still have the subdirectory)
if (string.IsNullOrEmpty(startDir))
name = Path.GetFileName(kvp.Key); // startDir is empty so a bsp file must have been passed, use Path.GFN instead
Console.WriteLine($"{name} = {kvp.Value.mapVersion}"); // there was a kvp.Value.formatVersion output here, it's gone now
}
}
static uint SwapBytes(uint x)
{
return ((x & 0x000000ff) << 24) +
((x & 0x0000ff00) << 8) +
((x & 0x00ff0000) >> 8) +
((x & 0xff000000) >> 24);
}
static BSPVersion GetBSPVer(string filePath)
{
BSPVersion retVal = new BSPVersion();
using (var reader = new BinaryReader(File.OpenRead(filePath)))
{
reader.BaseStream.Position = 0;
var magic = reader.ReadUInt32();
bool bigEndian = magic != 0x50534256;
if (bigEndian)
magic = SwapBytes(magic);
if (magic != 0x50534256)
return retVal; // not a VBSP
reader.BaseStream.Position = 0x4;
retVal.formatVersion = reader.ReadUInt32();
reader.BaseStream.Position = 0x408;
retVal.mapVersion = reader.ReadUInt32();
if (bigEndian)
{
retVal.formatVersion = SwapBytes(retVal.formatVersion);
retVal.mapVersion = SwapBytes(retVal.mapVersion);
}
}
return retVal;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment