Skip to content

Instantly share code, notes, and snippets.

@def-
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save def-/20ad9a792e16a192b8a6 to your computer and use it in GitHub Desktop.

Select an option

Save def-/20ad9a792e16a192b8a6 to your computer and use it in GitHub Desktop.
map download updater script
import unsigned, strutils
type TCrc32* = uint32
const InitCrc32* = TCrc32(-1)
proc createCrcTable(): array[0..255, TCrc32] =
for i in 0..255:
var rem = TCrc32(i)
for j in 0..7:
if (rem and 1) > 0: rem = (rem shr 1) xor TCrc32(0xedb88320)
else: rem = rem shr 1
result[i] = rem
# Table created at compile time
const crc32table = createCrcTable()
proc updateCrc32(c: char, crc: var TCrc32) =
crc = (crc shr 8) xor crc32table[(crc and 0xff) xor ord(c)]
proc crc32*(s: string): TCrc32 =
result = InitCrc32
for c in s:
updateCrc32(c, result)
result = not result
proc crc32FromFile*(filename: string): TCrc32 =
const bufSize = 8192
var bin: File
result = InitCrc32
if not open(bin, filename):
return
var buf {.noinit.}: array[bufSize, char]
while true:
var readBytes = bin.readChars(buf, 0, bufSize)
for i in countup(0, readBytes - 1):
updateCrc32(buf[i], result)
if readBytes != bufSize:
break
close(bin)
result = not result
import os, crc32, strutils
const
baseDir = "/home/teeworlds/servers"
mapdlDir = "/var/www-maps"
for kind, path in walkDir baseDir/"maps":
if kind != pcFile:
continue
let (dir, name, ext) = splitFile(path)
if ext != ".map":
continue
let
sum = crc32FromFile(path).int64.toHex(8).toLower
newName = name & "_" & sum & ext
newPath = mapdlDir / newName
tmpPath = newPath & ".tmp"
if existsFile newPath:
continue
copyFile path, tmpPath
moveFile tmpPath, newPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment