-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from saiko-tech/crc32
implement Bsp.CRC32() - aka CRC_MapFile()
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.bsp.gz filter=lfs diff=lfs merge=lfs -text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package bsp | ||
|
||
import ( | ||
"hash/crc32" | ||
"sort" | ||
) | ||
|
||
func (bsp *Bsp) CRC32() (uint32, error) { | ||
const lumpCount = 64 | ||
|
||
crc := crc32.NewIEEE() | ||
|
||
lumpList := make([]LumpId, lumpCount) | ||
for i := 0; i < lumpCount; i++ { | ||
lumpList[i] = LumpId(i) | ||
} | ||
|
||
sort.Slice(lumpList, func(i, j int) bool { | ||
return bsp.header.Lumps[lumpList[i]].Offset < bsp.header.Lumps[lumpList[j]].Offset | ||
}) | ||
|
||
for i := 0; i < lumpCount; i++ { | ||
l := lumpList[i] | ||
if l == LumpEntities { | ||
continue | ||
} | ||
|
||
_, err := crc.Write(bsp.RawLump(l).raw) | ||
if err != nil { | ||
return 0, err | ||
} | ||
} | ||
|
||
// see CRC32_Final | ||
res := crc.Sum32() ^ 0xFFFFFFFF | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package bsp_test | ||
|
||
import ( | ||
"compress/gzip" | ||
"os" | ||
"testing" | ||
|
||
"github.com/galaco/bsp" | ||
) | ||
|
||
func TestBsp_Crc(t *testing.T) { | ||
f, err := os.Open("ar_baggage.bsp.gz") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
gzR, err := gzip.NewReader(f) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
bspF, err := bsp.ReadFromStream(gzR) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
res, err := bspF.CRC32() | ||
if err != nil { | ||
t.Error("unexpected error:", err) | ||
} | ||
|
||
const expected = 2836609078 | ||
|
||
if res != expected { | ||
t.Errorf("CRC incorrect, expected %d got %d", expected, res) | ||
} | ||
} |