-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
563 additions
and
366 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
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 |
---|---|---|
@@ -1,4 +1,113 @@ | ||
package profile | ||
|
||
type ModStore map[string]IMod | ||
type ProfileStore = map[string]Profile | ||
import ( | ||
"encoding/json" | ||
"modm8/backend" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// type ModStore map[string]IMod | ||
// type ProfileStore = map[string]Profile | ||
|
||
type ProfileManager struct{} | ||
|
||
func NewManager() *ProfileManager { | ||
return &ProfileManager{} | ||
} | ||
|
||
type ProfileMods struct { | ||
Thunderstore []string `json:"thunderstore"` | ||
Nexus []string `json:"nexus"` | ||
} | ||
|
||
type ProfileManifest struct { | ||
Mods ProfileMods `json:"mods"` | ||
} | ||
|
||
func (pm *ProfileManager) GetProfiles(gameTitle string) (map[string]ProfileManifest, error) { | ||
return GetProfiles(gameTitle) | ||
} | ||
|
||
func (pm *ProfileManager) GetProfile(gameTitle, profileName string) (*ProfileManifest, error) { | ||
return GetManifest(gameTitle, profileName) | ||
} | ||
|
||
func (pm *ProfileManager) SaveProfile(gameTitle, profileName string, prof ProfileManifest) error { | ||
return SaveManifest(gameTitle, profileName, prof) | ||
} | ||
|
||
func PathToProfilesDir(gameTitle string) string { | ||
cacheDir, _ := os.UserConfigDir() | ||
path := filepath.Join(cacheDir, "modm8", "Games", gameTitle, "Profiles") | ||
|
||
return path | ||
} | ||
|
||
func PathToProfile(gameTitle, profileName string) string { | ||
return filepath.Join(PathToProfilesDir(gameTitle), profileName+".prof") | ||
} | ||
|
||
func GetProfileNames(gameTitle string) ([]string, error) { | ||
profDir := PathToProfilesDir(gameTitle) | ||
|
||
// The user probably hasn't created a profiles yet. | ||
exists, _ := backend.ExistsAtPath(profDir) | ||
if !exists { | ||
return []string{}, nil | ||
} | ||
|
||
paths, err := backend.WalkDirExt(profDir, []string{"prof"}) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
|
||
var names []string | ||
for _, path := range paths { | ||
name := strings.Replace(filepath.Base(path), ".prof", "", -1) | ||
names = append(names, name) | ||
} | ||
|
||
return names, nil | ||
} | ||
|
||
func SaveManifest(gameTitle, profileName string, prof ProfileManifest) error { | ||
data, err := json.Marshal(prof) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return backend.SaveFile(PathToProfile(gameTitle, profileName), data) | ||
} | ||
|
||
func GetManifest(gameTitle, profileName string) (*ProfileManifest, error) { | ||
contents, err := backend.ReadFile(PathToProfile(gameTitle, profileName)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var manifest ProfileManifest | ||
if err := json.Unmarshal(contents, &manifest); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &manifest, nil | ||
} | ||
|
||
func GetProfiles(gameTitle string) (map[string]ProfileManifest, error) { | ||
profNames, err := GetProfileNames(gameTitle) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
profiles := make(map[string]ProfileManifest) | ||
for _, name := range profNames { | ||
manifest, _ := GetManifest(gameTitle, name) | ||
if manifest != nil { | ||
profiles[name] = *manifest | ||
} | ||
} | ||
|
||
return profiles, err | ||
} |
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 |
---|---|---|
@@ -1,23 +1,27 @@ | ||
package profile | ||
|
||
type Profile struct { | ||
Name string `json:"name" mapstructure:"name"` | ||
Favourited bool `json:"favourited" mapstructure:"favourited"` | ||
Mods ModStore `json:"mods" mapstructure:"mods"` | ||
} | ||
// type Profile struct { | ||
// Name string `json:"name" mapstructure:"name"` | ||
// //Favourited bool `json:"favourited" mapstructure:"favourited"` | ||
// Mods ModStore `json:"mods" mapstructure:"mods"` | ||
// } | ||
|
||
func NewProfile(name string) Profile { | ||
return Profile{ | ||
Name: name, | ||
Favourited: false, | ||
Mods: ModStore{}, | ||
} | ||
} | ||
// func NewProfile(name string) Profile { | ||
// return Profile{ | ||
// Name: name, | ||
// //Favourited: false, | ||
// Mods: ModStore{}, | ||
// } | ||
// } | ||
|
||
func (p *Profile) Favourite() { | ||
p.Favourited = true | ||
} | ||
// func (p *Profile) AddMod(name string, mod IMod) { | ||
// p.Mods[name] = mod | ||
// } | ||
|
||
func (p *Profile) AddMod(name string, mod IMod) { | ||
p.Mods[name] = mod | ||
} | ||
// func (p *Profile) Rename(name string) { | ||
// p.Name = name | ||
// } | ||
|
||
// // func (p *Profile) Favourite() { | ||
// // p.Favourited = true | ||
// // } |
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
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
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,27 @@ | ||
package backend | ||
|
||
import ( | ||
"modm8/backend/common/profile" | ||
"testing" | ||
) | ||
|
||
func TestWriteProfile(t *testing.T) { | ||
manifest := profile.ProfileManifest{ | ||
Mods: profile.ProfileMods{ | ||
Thunderstore: []string{"example-ts-mod-4.2.0"}, | ||
Nexus: []string{"example-nexus-mod-6.9.0"}, | ||
}, | ||
} | ||
|
||
err := profile.SaveManifest("Lethal Company", "test", manifest) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestReadProfile(t *testing.T) { | ||
_, err := profile.GetManifest("Lethal Company", "test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.