Skip to content

Commit

Permalink
Refectoring to reduce code warnings (OpenDiablo2#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
essial authored Feb 2, 2020
1 parent ea4450f commit 6606774
Show file tree
Hide file tree
Showing 42 changed files with 346 additions and 375 deletions.
1 change: 1 addition & 0 deletions d2common/bitstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (v *BitStream) EnsureBits(bitCount int) bool {

// WasteBits dry-reads the specified number of bits
func (v *BitStream) WasteBits(bitCount int) {
//noinspection GoRedundantConversion
v.current >>= uint(bitCount)
v.bitCount -= bitCount
}
2 changes: 1 addition & 1 deletion d2common/d2data/d2datadict/object_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func init() {
func indexObjects(objects []ObjectLookupRecord) [][][]*ObjectLookupRecord {
// Allocating 6 to allow Acts 1-5 without requiring a -1 at every read.
indexedObjects = make([][][]*ObjectLookupRecord, 6)
for i, _ := range objects {
for i := range objects {
record := &objects[i]
if indexedObjects[record.Act] == nil {
// Likewise allocating 3 so a -1 isn't necessary.
Expand Down
6 changes: 3 additions & 3 deletions d2common/d2enum/hero.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const (
HeroDruid Hero = 7 // Druid
)

func (v Hero) GetToken() string {
switch v {
func (h Hero) GetToken() string {
switch h {
case HeroBarbarian:
return "BA"
case HeroNecromancer:
Expand All @@ -32,7 +32,7 @@ func (v Hero) GetToken() string {
case HeroDruid:
return "DZ"
default:
log.Fatalf("Unknown hero token: %d", v)
log.Fatalf("Unknown hero token: %d", h)
}
return ""
}
Expand Down
8 changes: 4 additions & 4 deletions d2common/d2enum/hero_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions d2common/d2fileformats/d2dcc/dcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func LoadDCC(fileData []byte) (*DCC, error) {
var bm = d2common.CreateBitMuncher(fileData, 0)
result.Signature = int(bm.GetByte())
if result.Signature != 0x74 {
return nil, errors.New("Signature expected to be 0x74 but it is not.")
return nil, errors.New("signature expected to be 0x74 but it is not")
}
result.Version = int(bm.GetByte())
result.NumberOfDirections = int(bm.GetByte())
result.FramesPerDirection = int(bm.GetInt32())
if bm.GetInt32() != 1 {
return nil, errors.New("This value isn't 1. It has to be 1.")
return nil, errors.New("this value isn't 1. It has to be 1")
}
bm.GetInt32() // TotalSizeCoded
directionOffsets := make([]int, result.NumberOfDirections)
Expand Down
2 changes: 1 addition & 1 deletion d2common/d2fileformats/d2dcc/dcc_direction.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func CreateDCCDirection(bm *d2common.BitMuncher, file DCC) DCCDirection {
maxx = int(d2common.MaxInt32(int32(result.Frames[frameIdx].Box.Right()), int32(maxx)))
maxy = int(d2common.MaxInt32(int32(result.Frames[frameIdx].Box.Bottom()), int32(maxy)))
}
result.Box = d2common.Rectangle{minx, miny, (maxx - minx), (maxy - miny)}
result.Box = d2common.Rectangle{Left: minx, Top: miny, Width: maxx - minx, Height: maxy - miny}
if result.OptionalDataBits > 0 {
log.Panic("Optional bits in DCC data is not currently supported.")
}
Expand Down
8 changes: 4 additions & 4 deletions d2common/d2fileformats/d2dcc/dcc_direction_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ func CreateDCCDirectionFrame(bits *d2common.BitMuncher, direction DCCDirection)
log.Panic("Bottom up frames are not implemented.")
} else {
result.Box = d2common.Rectangle{
result.XOffset,
result.YOffset - result.Height + 1,
result.Width,
result.Height,
Left: result.XOffset,
Top: result.YOffset - result.Height + 1,
Width: result.Width,
Height: result.Height,
}
}
result.valid = true
Expand Down
2 changes: 1 addition & 1 deletion d2common/d2fileformats/d2mpq/crypto_buff.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func InitializeCryptoBuffer() {
seed = (seed*125 + 3) % 0x2AAAAB
temp1 := (seed & 0xFFFF) << 0x10
seed = (seed*125 + 3) % 0x2AAAAB
temp2 := (seed & 0xFFFF)
temp2 := seed & 0xFFFF
CryptoBuffer[index2] = temp1 | temp2
index2 += 0x100
}
Expand Down
12 changes: 6 additions & 6 deletions d2common/d2fileformats/d2mpq/mpq_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ func (v *Stream) loadBlockOffsets() error {
v.BlockPositions = make([]uint32, blockPositionCount)
v.MPQData.File.Seek(int64(v.BlockTableEntry.FilePosition), 0)
reader := bufio.NewReader(v.MPQData.File)
bytes := make([]byte, blockPositionCount*4)
reader.Read(bytes)
mpqBytes := make([]byte, blockPositionCount*4)
reader.Read(mpqBytes)
for i := range v.BlockPositions {
idx := i * 4
v.BlockPositions[i] = binary.LittleEndian.Uint32(bytes[idx : idx+4])
v.BlockPositions[i] = binary.LittleEndian.Uint32(mpqBytes[idx : idx+4])
}
//binary.Read(v.MPQData.File, binary.LittleEndian, &v.BlockPositions)
blockPosSize := blockPositionCount << 2
if v.BlockTableEntry.HasFlag(FileEncrypted) {
decrypt(v.BlockPositions, v.EncryptionSeed-1)
if v.BlockPositions[0] != blockPosSize {
log.Println("Decryption of MPQ failed!")
return errors.New("Decryption of MPQ failed!")
return errors.New("decryption of MPQ failed")
}
if v.BlockPositions[1] > v.BlockSize+blockPosSize {
log.Println("Decryption of MPQ failed!")
return errors.New("Decryption of MPQ failed!")
return errors.New("decryption of MPQ failed")
}
}
return nil
Expand Down Expand Up @@ -123,7 +123,7 @@ func (v *Stream) readInternal(buffer []byte, offset, count uint32) uint32 {
}

func (v *Stream) bufferData() {
requiredBlock := uint32(v.CurrentPosition / v.BlockSize)
requiredBlock := v.CurrentPosition / v.BlockSize
if requiredBlock == v.CurrentBlockIndex {
return
}
Expand Down
22 changes: 0 additions & 22 deletions d2common/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,6 @@ func MaxInt32(a, b int32) int32 {
return b
}

func NextPow2(x int32) int32 {
result := int32(1)
for result < x {
result *= 2
}
return result
}

func AbsInt32(a int32) int32 {
if a < 0 {
return -a
Expand All @@ -64,24 +56,10 @@ func MinInt32(a, b int32) int32 {
}

// BytesToInt32 converts 4 bytes to int32
func BytesToInt32(b []byte) int32 {
// equivalnt of return int32(binary.LittleEndian.Uint32(b))
return int32(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
}

// IsoToScreen converts isometric coordinates to screenspace coordinates
func IsoToScreen(isoX, isoY, modX, modY float64) (float64, float64) {
screenX := (isoX - isoY) * 80
screenY := (isoX + isoY) * 40
return screenX + modX, screenY + modY
}

// ScreenToIso converts screenspace coordinates to isometric coordinates
func ScreenToIso(sx, sy float64) (float64, float64) {
x := (sx/80 + sy/40) / 2
y := (sy/40 - (sx / 80)) / 2
return x, y
}

// GetAngleBetween returns the angle between two points. 0deg is facing to the right.
func GetAngleBetween(p1X, p1Y, p2X, p2Y float64) int {
Expand Down
16 changes: 1 addition & 15 deletions d2common/stringutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ func StringToInt(text string) int {
}

// SafeStringToInt converts a string to an integer, or returns -1 on falure
func SafeStringToInt(text string) int {
result, err := strconv.Atoi(text)
if err != nil {
return -1
}
return result
}

// StringToUint converts a string to a uint32
func StringToUint(text string) uint {
Expand Down Expand Up @@ -78,18 +71,11 @@ func StringToInt8(text string) int8 {
}

// StringToFloat64 converts a string to a float64
func StringToFloat64(text string) float64 {
result, err := strconv.ParseFloat(text, 64)
if err != nil {
panic(err)
}
return float64(result)
}

func Utf16BytesToString(b []byte) (string, error) {

if len(b)%2 != 0 {
return "", fmt.Errorf("Must have even length byte slice")
return "", fmt.Errorf("must have even length byte slice")
}

u16s := make([]uint16, 1)
Expand Down
14 changes: 7 additions & 7 deletions d2core/d2asset/animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ func createAnimationFromDCC(dcc *d2dcc.DCC, palette *d2datadict.PaletteRec, tran
for y := 0; y < frameHeight; y++ {
for x := 0; x < frameWidth; x++ {
if paletteIndex := dccFrame.PixelData[y*frameWidth+x]; paletteIndex != 0 {
color := palette.Colors[paletteIndex]
palColor := palette.Colors[paletteIndex]
offset := (x + y*frameWidth) * 4
pixels[offset] = color.R
pixels[offset+1] = color.G
pixels[offset+2] = color.B
pixels[offset] = palColor.R
pixels[offset+1] = palColor.G
pixels[offset+2] = palColor.B
pixels[offset+3] = byte(transparency)
}
}
Expand All @@ -98,8 +98,8 @@ func createAnimationFromDCC(dcc *d2dcc.DCC, palette *d2datadict.PaletteRec, tran

direction := animation.directions[directionIndex]
direction.frames = append(direction.frames, &animationFrame{
width: int(dccFrame.Width),
height: int(dccFrame.Height),
width: dccFrame.Width,
height: dccFrame.Height,
offsetX: minX,
offsetY: minY,
image: image,
Expand Down Expand Up @@ -294,7 +294,7 @@ func (a *Animation) Pause() {
}

func (a *Animation) SetPlayLoop(loop bool) {
a.playLoop = true
a.playLoop = loop
}

func (a *Animation) SetPlaySpeed(playSpeed float64) {
Expand Down
4 changes: 2 additions & 2 deletions d2core/d2asset/asset_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
)

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

type assetManager struct {
Expand Down
4 changes: 2 additions & 2 deletions d2core/d2audio/d2audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
var singleton AudioProvider

var (
ErrHasInit error = errors.New("audio system is already initialized")
ErrNotInit error = errors.New("audio system has not been initialized")
ErrHasInit = errors.New("audio system is already initialized")
ErrNotInit = errors.New("audio system has not been initialized")
)

type SoundEffect interface {
Expand Down
12 changes: 6 additions & 6 deletions d2core/d2audio/ebiten/ebiten_audio_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"github.com/hajimehoshi/ebiten/audio"
)

type EbitenAudioProvider struct {
type AudioProvider struct {
audioContext *audio.Context // The Audio context
bgmAudio *audio.Player // The audio player
lastBgm string
sfxVolume float64
bgmVolume float64
}

func CreateAudio() (*EbitenAudioProvider, error) {
result := &EbitenAudioProvider{}
func CreateAudio() (*AudioProvider, error) {
result := &AudioProvider{}
var err error
result.audioContext, err = audio.NewContext(44100)
if err != nil {
Expand All @@ -29,7 +29,7 @@ func CreateAudio() (*EbitenAudioProvider, error) {
return result, nil
}

func (eap *EbitenAudioProvider) PlayBGM(song string) {
func (eap *AudioProvider) PlayBGM(song string) {
if eap.lastBgm == song {
return
}
Expand Down Expand Up @@ -71,12 +71,12 @@ func (eap *EbitenAudioProvider) PlayBGM(song string) {
}()
}

func (eap *EbitenAudioProvider) LoadSoundEffect(sfx string) (d2audio.SoundEffect, error) {
func (eap *AudioProvider) LoadSoundEffect(sfx string) (d2audio.SoundEffect, error) {
result := CreateSoundEffect(sfx, eap.audioContext, eap.sfxVolume) // TODO: Split
return result, nil
}

func (eap *EbitenAudioProvider) SetVolumes(bgmVolume, sfxVolume float64) {
func (eap *AudioProvider) SetVolumes(bgmVolume, sfxVolume float64) {
eap.sfxVolume = sfxVolume
eap.bgmVolume = bgmVolume
}
10 changes: 5 additions & 5 deletions d2core/d2audio/ebiten/ebiten_sound_effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/hajimehoshi/ebiten/audio/wav"
)

type EbitenSoundEffect struct {
type SoundEffect struct {
player *audio.Player
}

func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *EbitenSoundEffect {
result := &EbitenSoundEffect{}
func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *SoundEffect {
result := &SoundEffect{}
var soundFile string
if _, exists := d2datadict.Sounds[sfx]; exists {
soundEntry := d2datadict.Sounds[sfx]
Expand Down Expand Up @@ -42,11 +42,11 @@ func CreateSoundEffect(sfx string, context *audio.Context, volume float64) *Ebit
return result
}

func (v *EbitenSoundEffect) Play() {
func (v *SoundEffect) Play() {
v.player.Rewind()
v.player.Play()
}

func (v *EbitenSoundEffect) Stop() {
func (v *SoundEffect) Stop() {
v.player.Pause()
}
Loading

0 comments on commit 6606774

Please sign in to comment.