2020-09-08 15:45:26 -04:00
|
|
|
package d2loader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-10 02:44:42 -05:00
|
|
|
"io"
|
2020-09-23 13:30:54 -04:00
|
|
|
"log"
|
2020-09-08 15:45:26 -04:00
|
|
|
"testing"
|
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2loader/asset/types"
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-02 21:23:07 -05:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-09-08 15:45:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-09-09 14:35:52 -04:00
|
|
|
sourcePathA = "testdata/A"
|
|
|
|
sourcePathB = "testdata/B"
|
|
|
|
sourcePathC = "testdata/C"
|
|
|
|
sourcePathD = "testdata/D.mpq"
|
|
|
|
commonFile = "common.txt"
|
|
|
|
exclusiveA = "exclusive_a.txt"
|
|
|
|
exclusiveB = "exclusive_b.txt"
|
|
|
|
exclusiveC = "exclusive_c.txt"
|
|
|
|
exclusiveD = "exclusive_d.txt"
|
2020-09-14 14:47:11 -04:00
|
|
|
subdirCommonD = "dir\\common.txt"
|
2020-09-09 14:35:52 -04:00
|
|
|
badSourcePath = "/x/y/z.mpq"
|
|
|
|
badFilePath = "a/bad/file/path.txt"
|
2020-09-08 15:45:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLoader_NewLoader(t *testing.T) {
|
2020-11-03 07:54:15 -05:00
|
|
|
loader, _ := NewLoader(d2util.LogLevelDefault)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
|
|
|
if loader.Cache == nil {
|
|
|
|
t.Error("loader should not be nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoader_AddSource(t *testing.T) {
|
2020-11-03 07:54:15 -05:00
|
|
|
loader, _ := NewLoader(d2util.LogLevelDefault)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
errA := loader.AddSource(sourcePathA, types.AssetSourceFileSystem)
|
|
|
|
errB := loader.AddSource(sourcePathB, types.AssetSourceFileSystem)
|
|
|
|
errC := loader.AddSource(sourcePathC, types.AssetSourceFileSystem)
|
|
|
|
errD := loader.AddSource(sourcePathD, types.AssetSourceFileSystem)
|
|
|
|
errE := loader.AddSource(badSourcePath, types.AssetSourceMPQ)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
if errA != nil {
|
|
|
|
t.Error(errA)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errB != nil {
|
|
|
|
t.Error(errB)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errC != nil {
|
|
|
|
t.Error(errC)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errD != nil {
|
|
|
|
t.Error(errD)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errE == nil {
|
|
|
|
t.Error("expecting error on bad file path")
|
|
|
|
}
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:12:06 -04:00
|
|
|
// nolint:gocyclo // this is just a test, not a big deal if we ignore linter here
|
2020-09-08 15:45:26 -04:00
|
|
|
func TestLoader_Load(t *testing.T) {
|
2020-11-03 07:54:15 -05:00
|
|
|
loader, _ := NewLoader(d2util.LogLevelDefault)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
2020-11-03 07:54:15 -05:00
|
|
|
// we expect files common to any source to come from here
|
2021-01-10 02:44:42 -05:00
|
|
|
err := loader.AddSource(sourcePathB, types.AssetSourceFileSystem)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
err = loader.AddSource(sourcePathD, types.AssetSourceMPQ)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
err = loader.AddSource(sourcePathA, types.AssetSourceFileSystem)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2021-01-10 02:44:42 -05:00
|
|
|
err = loader.AddSource(sourcePathC, types.AssetSourceFileSystem)
|
2020-09-23 13:30:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-09 14:35:52 -04:00
|
|
|
entryCommon, errCommon := loader.Load(commonFile) // common file exists in all three Sources
|
2020-09-08 15:45:26 -04:00
|
|
|
|
|
|
|
entryA, errA := loader.Load(exclusiveA) // each source has a file exclusive to itself
|
|
|
|
entryB, errB := loader.Load(exclusiveB)
|
|
|
|
entryC, errC := loader.Load(exclusiveC)
|
|
|
|
entryD, errD := loader.Load(exclusiveD)
|
2020-09-14 14:47:11 -04:00
|
|
|
entryDsubdir, errDsubdir := loader.Load(subdirCommonD)
|
2020-09-08 15:45:26 -04:00
|
|
|
|
|
|
|
_, expectedError := loader.Load(badFilePath) // we expect an Error for this bad file path
|
|
|
|
|
|
|
|
if entryCommon == nil || errCommon != nil {
|
|
|
|
t.Error("common entry should exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if errA != nil || errB != nil || errC != nil || errD != nil {
|
|
|
|
t.Error("files exclusive to each source don't exist")
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:47:11 -04:00
|
|
|
if errDsubdir != nil {
|
|
|
|
t.Error("mpq subdir entry not found")
|
|
|
|
}
|
|
|
|
|
2020-09-08 15:45:26 -04:00
|
|
|
if expectedError == nil {
|
|
|
|
t.Error("expected Error for nonexistant file path")
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []byte
|
|
|
|
|
|
|
|
buffer := make([]byte, 1)
|
|
|
|
|
|
|
|
tests := []struct {
|
2021-01-10 02:44:42 -05:00
|
|
|
entry io.ReadSeeker
|
2020-09-08 15:45:26 -04:00
|
|
|
data string
|
|
|
|
}{
|
2020-09-09 14:35:52 -04:00
|
|
|
{entryCommon, "b"}, // sourcePathB is loaded first, we expect a "b"
|
2020-09-08 15:45:26 -04:00
|
|
|
{entryA, "a"},
|
|
|
|
{entryB, "b"},
|
|
|
|
{entryC, "c"},
|
|
|
|
{entryD, "d"},
|
2020-09-14 14:47:11 -04:00
|
|
|
{entryDsubdir, "d"},
|
2020-09-08 15:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range tests {
|
|
|
|
entry, expected := tests[idx].entry, tests[idx].data
|
|
|
|
|
|
|
|
result = make([]byte, 0)
|
|
|
|
|
|
|
|
for {
|
|
|
|
if bytesRead, err := entry.Read(buffer); err != nil || bytesRead == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, buffer...)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := string(result[0])
|
|
|
|
|
|
|
|
if got != expected {
|
2021-01-10 02:44:42 -05:00
|
|
|
fmtStr := "unexpected data in file, expected %q, got %q"
|
|
|
|
msg := fmt.Sprintf(fmtStr, expected, got)
|
2020-09-08 15:45:26 -04:00
|
|
|
t.Error(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|