Skip to content

Commit

Permalink
Returning the first directory folder always.
Browse files Browse the repository at this point in the history
Changed the archive.FindFile to always return the first directory found
in the pacakge.

Fixes #3
  • Loading branch information
ronoaldo committed Oct 1, 2021
1 parent c4f1af8 commit a080338
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion api/contentdb/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ func (p *PackageArchive) Contents() []string {
func (p *PackageArchive) FindFile(name string, max int) (count int, dir string) {
for _, f := range p.Contents() {
if strings.HasSuffix(f, "/"+name) || f == name {
dir, _ = path.Split(f)
if dir == "" {
dir, _ = path.Split(f)
}
count++
}
if max > 0 && count >= max {
Expand Down
12 changes: 11 additions & 1 deletion api/contentdb/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ var (

bytesInvalidModpack []byte = mustReadFile("testdata/invalidmodpack.zip")
zipInvalidModpack *zip.Reader = mustZip(bytesInvalidModpack)

bytesMoreblocks []byte = mustReadFile("testdata/moreblocks.zip")
zipMoreblocks *zip.Reader = mustZip(bytesMoreblocks)
)

func mustReadFile(f string) []byte {
Expand Down Expand Up @@ -76,7 +79,7 @@ func TestPackageArchive_FindFile(t *testing.T) {
fields: fields{z: zip3darmor},
args: args{"init.lua", 0},
wantCount: 7,
wantDir: "3d_armor/wieldview/",
wantDir: "3d_armor/3d_armor/",
},
{
name: "should find only first init.lua when max is 1",
Expand All @@ -85,6 +88,13 @@ func TestPackageArchive_FindFile(t *testing.T) {
wantCount: 1,
wantDir: "3d_armor/3d_armor/",
},
{
name: "should find init.lua in root directory if available",
fields: fields{z: zipMoreblocks},
args: args{"init.lua", 0},
wantCount: 2,
wantDir: "moreblocks/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Binary file added api/contentdb/testdata/moreblocks.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions cmd/contentdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ func installMod(mods []string) error {
var modconf *ini.File

modconfFilename := "mod.conf"
// mod root dir is where init.lua is
_, stripPrefix := archive.FindFile("init.lua", 0)
// mod root dir is where the first init.lua is
_, stripPrefix := archive.FindFile("init.lua", 1)
if pkgType == contentdb.Modpack {
var found = 0
// For modpack, load a diferent config name and adjust the stripPrefix
Expand Down

0 comments on commit a080338

Please sign in to comment.