Skip to content

Commit

Permalink
Moving files around to make more sense (OpenDiablo2#279)
Browse files Browse the repository at this point in the history
* Prevent input fighting between terminal and other input listeners.

* Moving files around, removing exports

* Add missing line
  • Loading branch information
FooSoft authored Feb 1, 2020
1 parent e878ebc commit 8a547eb
Show file tree
Hide file tree
Showing 49 changed files with 599 additions and 704 deletions.
3 changes: 2 additions & 1 deletion d2common/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type Cache struct {
func CreateCache(budget int) *Cache {
return &Cache{lookup: make(map[string]*cacheNode), budget: budget}
}
func (c *Cache) SetCacheVerbose(verbose bool) {

func (c *Cache) SetVerbose(verbose bool) {
c.verbose = verbose
}

Expand Down
25 changes: 0 additions & 25 deletions d2common/d2config/configuration.go → d2common/d2config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,6 @@ import (
"runtime"
)

// Configuration defines the configuration for the engine, loaded from config.json
type Configuration struct {
Language string
FullScreen bool
Scale float64
RunInBackground bool
TicksPerSecond int
FpsCap int
VsyncEnabled bool
MpqPath string
MpqLoadOrder []string
SfxVolume float64
BgmVolume float64
}

/*
func getConfigurationPath() string {
configDir, err := os.UserConfigDir()
if err != nil {
return "config.json"
}
return path.Join(configDir, "OpenDiablo2/config.json")
}
*/
func getDefaultConfiguration() *Configuration {
config := &Configuration{
Language: "ENG",
Expand Down
15 changes: 15 additions & 0 deletions d2common/d2config/d2config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ var (
ErrHasInit = errors.New("configuration has already been initialized")
)

// Configuration defines the configuration for the engine, loaded from config.json
type Configuration struct {
Language string
FullScreen bool
Scale float64
RunInBackground bool
TicksPerSecond int
FpsCap int
VsyncEnabled bool
MpqPath string
MpqLoadOrder []string
SfxVolume float64
BgmVolume float64
}

var singleton *Configuration

func Initialize() error {
Expand Down
13 changes: 7 additions & 6 deletions d2core/d2render/animation.go → d2core/d2asset/animation.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package d2render
package d2asset

import (
"errors"
"image/color"
"math"

"github.com/OpenDiablo2/OpenDiablo2/d2common"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dc6"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2helper"

"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)

type playMode int
Expand Down Expand Up @@ -49,7 +50,7 @@ type Animation struct {
playLoop bool
}

func CreateAnimationFromDCC(dcc *d2dcc.DCC, palette *d2datadict.PaletteRec, transparency int) (*Animation, error) {
func createAnimationFromDCC(dcc *d2dcc.DCC, palette *d2datadict.PaletteRec, transparency int) (*Animation, error) {
animation := &Animation{
playLength: 1.0,
playLoop: true,
Expand Down Expand Up @@ -83,7 +84,7 @@ func CreateAnimationFromDCC(dcc *d2dcc.DCC, palette *d2datadict.PaletteRec, tran
}
}

err, image := NewSurface(frameWidth, frameHeight, d2common.FilterNearest)
err, image := d2render.NewSurface(frameWidth, frameHeight, d2common.FilterNearest)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -111,14 +112,14 @@ func CreateAnimationFromDCC(dcc *d2dcc.DCC, palette *d2datadict.PaletteRec, tran
return animation, nil
}

func CreateAnimationFromDC6(dc6 *d2dc6.DC6File) (*Animation, error) {
func createAnimationFromDC6(dc6 *d2dc6.DC6File) (*Animation, error) {
animation := &Animation{
playLength: 1.0,
playLoop: true,
}

for frameIndex, dc6Frame := range dc6.Frames {
err, image := NewSurface(int(dc6Frame.Width), int(dc6Frame.Height), d2common.FilterNearest)
err, image := d2render.NewSurface(int(dc6Frame.Width), int(dc6Frame.Height), d2common.FilterNearest)
if err != nil {
return nil, err
}
Expand Down
67 changes: 67 additions & 0 deletions d2core/d2asset/animation_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package d2asset

import (
"errors"
"fmt"
"path/filepath"
"strings"

"github.com/OpenDiablo2/OpenDiablo2/d2common"
)

const (
animationBudget = 64
)

type animationManager struct {
cache *d2common.Cache
}

func createAnimationManager() *animationManager {
return &animationManager{d2common.CreateCache(animationBudget)}
}

func (am *animationManager) loadAnimation(animationPath, palettePath string, transparency int) (*Animation, error) {
cachePath := fmt.Sprintf("%s;%s;%d", animationPath, palettePath, transparency)
if animation, found := am.cache.Retrieve(cachePath); found {
return animation.(*Animation).Clone(), nil
}

var animation *Animation
switch strings.ToLower(filepath.Ext(animationPath)) {
case ".dc6":
dc6, err := loadDC6(animationPath, palettePath)
if err != nil {
return nil, err
}

animation, err = createAnimationFromDC6(dc6)
if err != nil {
return nil, err
}
case ".dcc":
dcc, err := loadDCC(animationPath)
if err != nil {
return nil, err
}

palette, err := loadPalette(palettePath)
if err != nil {
return nil, err
}

animation, err = createAnimationFromDCC(dcc, palette, transparency)
if err != nil {
return nil, err
}

default:
return nil, errors.New("unknown animation format")
}

if err := am.cache.Insert(cachePath, animation.Clone(), 1); err != nil {
return nil, err
}

return animation, nil
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package d2archivemanager
package d2asset

import (
"errors"
"path"
"sync"

"github.com/OpenDiablo2/OpenDiablo2/d2common"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2config"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2mpq"
)

Expand All @@ -17,59 +15,43 @@ type archiveEntry struct {
hashEntryMap d2mpq.HashEntryMap
}

type ArchiveManager struct {
type archiveManager struct {
cache *d2common.Cache
config *d2config.Configuration
entries []archiveEntry
mutex sync.Mutex
}

const (
ArchiveBudget = 1024 * 1024 * 512
archiveBudget = 1024 * 1024 * 512
)

func CreateArchiveManager(config *d2config.Configuration) *ArchiveManager {
return &ArchiveManager{cache: d2common.CreateCache(ArchiveBudget), config: config}
}

func (am *ArchiveManager) SetCacheVerbose(verbose bool) {
am.cache.SetCacheVerbose(verbose)
}

func (am *ArchiveManager) GetCacheWeight() int {
return am.cache.GetWeight()
}

func (am *ArchiveManager) GetCacheBudget() int {
return am.cache.GetBudget()
}

func (am *ArchiveManager) ClearCache() {
am.cache.Clear()
func createArchiveManager(config *d2config.Configuration) *archiveManager {
return &archiveManager{cache: d2common.CreateCache(archiveBudget), config: config}
}

func (am *ArchiveManager) LoadArchiveForFile(filePath string) (*d2mpq.MPQ, error) {
func (am *archiveManager) loadArchiveForFile(filePath string) (*d2mpq.MPQ, error) {
am.mutex.Lock()
defer am.mutex.Unlock()

if err := am.CacheArchiveEntries(); err != nil {
if err := am.cacheArchiveEntries(); err != nil {
return nil, err
}

for _, archiveEntry := range am.entries {
if archiveEntry.hashEntryMap.Contains(filePath) {
return am.LoadArchive(archiveEntry.archivePath)
return am.loadArchive(archiveEntry.archivePath)
}
}

return nil, errors.New("file not found")
}

func (am *ArchiveManager) FileExistsInArchive(filePath string) (bool, error) {
func (am *archiveManager) fileExistsInArchive(filePath string) (bool, error) {
am.mutex.Lock()
defer am.mutex.Unlock()

if err := am.CacheArchiveEntries(); err != nil {
if err := am.cacheArchiveEntries(); err != nil {
return false, err
}

Expand All @@ -82,7 +64,7 @@ func (am *ArchiveManager) FileExistsInArchive(filePath string) (bool, error) {
return false, nil
}

func (am *ArchiveManager) LoadArchive(archivePath string) (*d2mpq.MPQ, error) {
func (am *archiveManager) loadArchive(archivePath string) (*d2mpq.MPQ, error) {
if archive, found := am.cache.Retrieve(archivePath); found {
return archive.(*d2mpq.MPQ), nil
}
Expand All @@ -99,7 +81,7 @@ func (am *ArchiveManager) LoadArchive(archivePath string) (*d2mpq.MPQ, error) {
return archive, nil
}

func (am *ArchiveManager) CacheArchiveEntries() error {
func (am *archiveManager) cacheArchiveEntries() error {
if len(am.entries) == len(am.config.MpqLoadOrder) {
return nil
}
Expand All @@ -108,7 +90,7 @@ func (am *ArchiveManager) CacheArchiveEntries() error {

for _, archiveName := range am.config.MpqLoadOrder {
archivePath := path.Join(am.config.MpqPath, archiveName)
archive, err := am.LoadArchive(archivePath)
archive, err := am.loadArchive(archivePath)
if err != nil {
return err
}
Expand Down
67 changes: 67 additions & 0 deletions d2core/d2asset/asset_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package d2asset

import (
"errors"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2data/d2datadict"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2cof"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dc6"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
)

var (
ErrHasInit error = errors.New("asset system is already initialized")
ErrNoInit error = errors.New("asset system is not initialized")
)

type assetManager struct {
archiveManager *archiveManager
fileManager *fileManager
paletteManager *paletteManager
animationManager *animationManager
}

func loadPalette(palettePath string) (*d2datadict.PaletteRec, error) {
if singleton == nil {
return nil, ErrNoInit
}

return singleton.paletteManager.loadPalette(palettePath)
}

func loadDC6(dc6Path, palettePath string) (*d2dc6.DC6File, error) {
dc6Data, err := LoadFile(dc6Path)
if err != nil {
return nil, err
}

paletteData, err := loadPalette(palettePath)
if err != nil {
return nil, err
}

dc6, err := d2dc6.LoadDC6(dc6Data, *paletteData)
if err != nil {
return nil, err
}

return &dc6, nil
}

func loadDCC(dccPath string) (*d2dcc.DCC, error) {
dccData, err := LoadFile(dccPath)
if err != nil {
return nil, err
}

return d2dcc.LoadDCC(dccData)
}

func loadCOF(cofPath string) (*d2cof.COF, error) {
cofData, err := LoadFile(cofPath)
if err != nil {
return nil, err
}

return d2cof.LoadCOF(cofData)
}
Loading

0 comments on commit 8a547eb

Please sign in to comment.