1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-19 21:55:24 +00:00

Add static checks to help ensure implementations conform to interfaces (#582)

This commit is contained in:
Sunny Klair 2020-07-13 09:05:04 -04:00 committed by GitHub
parent 7a32e7bbac
commit 41e7a5b28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 62 additions and 12 deletions

View File

@ -2,6 +2,8 @@ package d2common
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
var _ d2interface.BitMuncher = &BitMuncher{} // Static check to confirm struct conforms to interface
// BitMuncher is used for parsing files that are not byte-aligned such as the DCC files. // BitMuncher is used for parsing files that are not byte-aligned such as the DCC files.
type BitMuncher struct { type BitMuncher struct {
data []byte data []byte

View File

@ -1,6 +1,12 @@
package d2common package d2common
import "log" import (
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
)
var _ d2interface.BitStream = &BitStream{} // Static check to confirm struct conforms to interface
// BitStream is a utility class for reading groups of bits from a stream // BitStream is a utility class for reading groups of bits from a stream
type BitStream struct { type BitStream struct {

View File

@ -8,6 +8,8 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
var _ d2interface.Cache = &Cache{} // Static check to confirm struct conforms to interface
type cacheNode struct { type cacheNode struct {
next *cacheNode next *cacheNode
prev *cacheNode prev *cacheNode

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"encoding/binary" "encoding/binary"
"errors" "errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -12,8 +11,12 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
var _ d2interface.Archive = &MPQ{} // Static check to confirm struct conforms to interface
// MPQ represents an MPQ archive // MPQ represents an MPQ archive
type MPQ struct { type MPQ struct {
filePath string filePath string

View File

@ -1,5 +1,9 @@
package d2mpq package d2mpq
import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
var _ d2interface.ArchiveDataStream = &MpqDataStream{} // Static check to confirm struct conforms to interface
// MpqDataStream represents a stream for MPQ data. // MpqDataStream represents a stream for MPQ data.
type MpqDataStream struct { type MpqDataStream struct {
stream *Stream stream *Stream

View File

@ -5,16 +5,19 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common"
) )
const ( const (
animationBudget = 64 animationBudget = 64
) )
// Static checks to confirm struct conforms to interface
var _ d2interface.ArchivedAnimationManager = &animationManager{}
var _ d2interface.Cacher = &animationManager{}
type animationManager struct { type animationManager struct {
cache d2interface.Cache cache d2interface.Cache
renderer d2interface.Renderer renderer d2interface.Renderer
@ -37,7 +40,7 @@ func createAnimationManager(renderer d2interface.Renderer) *animationManager {
func (am *animationManager) LoadAnimation( func (am *animationManager) LoadAnimation(
animationPath, palettePath string, animationPath, palettePath string,
effect d2enum.DrawEffect ) (d2interface.Animation, error) { effect d2enum.DrawEffect) (d2interface.Animation, error) {
cachePath := fmt.Sprintf("%s;%s;%d", animationPath, palettePath, effect) cachePath := fmt.Sprintf("%s;%s;%d", animationPath, palettePath, effect)
if animation, found := am.cache.Retrieve(cachePath); found { if animation, found := am.cache.Retrieve(cachePath); found {
return animation.(d2interface.Animation).Clone(), nil return animation.(d2interface.Animation).Clone(), nil

View File

@ -11,6 +11,10 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
) )
// Static checks to confirm struct conforms to interface
var _ d2interface.ArchiveManager = &archiveManager{}
var _ d2interface.Cacher = &archiveManager{}
type archiveManager struct { type archiveManager struct {
cache d2interface.Cache cache d2interface.Cache
config *d2config.Configuration config *d2config.Configuration

View File

@ -13,6 +13,10 @@ const (
fileBudget = 1024 * 1024 * 32 fileBudget = 1024 * 1024 * 32
) )
// Static checks to confirm struct conforms to interface
var _ d2interface.ArchivedFileManager = &fileManager{}
var _ d2interface.Cacher = &fileManager{}
type fileManager struct { type fileManager struct {
cache d2interface.Cache cache d2interface.Cache
archiveManager d2interface.ArchiveManager archiveManager d2interface.ArchiveManager

View File

@ -2,12 +2,14 @@ package d2asset
import ( import (
"errors" "errors"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
d2iface "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" d2iface "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
var _ d2iface.Animation = &DC6Animation{} // Static check to confirm struct conforms to interface
// DC6Animation is an animation made from a DC6 file // DC6Animation is an animation made from a DC6 file
type DC6Animation struct { type DC6Animation struct {
animation animation

View File

@ -11,7 +11,9 @@ import (
d2iface "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" d2iface "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
// DCCAnimation represens an animation decoded from DCC var _ d2iface.Animation = &DCCAnimation{} // Static check to confirm struct conforms to interface
// DCCAnimation represents an animation decoded from DCC
type DCCAnimation struct { type DCCAnimation struct {
animation animation
dccPath string dccPath string

View File

@ -6,11 +6,12 @@ import (
"image/color" "image/color"
"strings" "strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common" "github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
var _ d2interface.Font = &Font{} // Static check to confirm struct conforms to interface
type fontGlyph struct { type fontGlyph struct {
frame int frame int
width int width int

View File

@ -2,15 +2,19 @@ package d2asset
import ( import (
"fmt" "fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common" "github.com/OpenDiablo2/OpenDiablo2/d2common"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
const ( const (
fontBudget = 64 fontBudget = 64
) )
// Static checks to confirm struct conforms to interface
var _ d2interface.ArchivedFontManager = &fontManager{}
var _ d2interface.Cacher = &fontManager{}
type fontManager struct { type fontManager struct {
cache d2interface.Cache cache d2interface.Cache
} }

View File

@ -6,6 +6,10 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
// Static checks to confirm struct conforms to interface
var _ d2interface.ArchivedPaletteManager = &paletteManager{}
var _ d2interface.Cacher = &paletteManager{}
type paletteManager struct { type paletteManager struct {
cache d2interface.Cache cache d2interface.Cache
} }

View File

@ -5,15 +5,16 @@ import (
"log" "log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
"github.com/hajimehoshi/ebiten/audio/wav"
"github.com/hajimehoshi/ebiten/audio" "github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/audio/wav"
) )
const sampleRate = 44100 const sampleRate = 44100
var _ d2interface.AudioProvider = &AudioProvider{} // Static check to confirm struct conforms to interface
// AudioProvider represents a provider capable of playing audio // AudioProvider represents a provider capable of playing audio
type AudioProvider struct { type AudioProvider struct {
audioContext *audio.Context // The Audio context audioContext *audio.Context // The Audio context

View File

@ -2,8 +2,16 @@ package d2input
import ( import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
) )
// Static checks to confirm struct conforms to interface
var _ d2interface.InputEventHandler = &HandlerEvent{}
var _ d2interface.KeyEvent = &KeyEvent{}
var _ d2interface.KeyCharsEvent = &KeyCharsEvent{}
var _ d2interface.MouseEvent = &MouseEvent{}
var _ d2interface.MouseMoveEvent = &MouseMoveEvent{}
// HandlerEvent is an event that EventHandlers will process and respond to // HandlerEvent is an event that EventHandlers will process and respond to
type HandlerEvent struct { type HandlerEvent struct {
keyMod d2enum.KeyMod keyMod d2enum.KeyMod