1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-01-26 03:07:41 -05:00

fixed 'dupl' lint errors (#845)

* removed dupl lint errors in d2compression/huffman.go

* remove duplicate code for set/unique item properties

This is a more involved edit. I've added a `PropertyDescriptor`
which is a common struct that should have existed already. The
`PropertyDescriptor` is used to generate property instances, and is common
to item affixes, set items, sets, unique items, and runewords.

This was all to remove duplicate code in d2item/

* removed duplicate code for rare item affixes
This commit is contained in:
gravestench 2020-10-26 07:38:18 +00:00 committed by GitHub
parent 815cfa09cb
commit 622186e350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 140 additions and 201 deletions

View File

@ -88,7 +88,7 @@ func (v *linkedNode) insert(other *linkedNode) *linkedNode {
return v return v
} }
//nolint:funlen // Makes no sense to split //nolint:funlen,dupl // it's ok to have duplicates and a long func here
func getPrimes() [][]byte { func getPrimes() [][]byte {
return [][]byte{ return [][]byte{
{ {

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"sort" "sort"
"strconv"
"strings" "strings"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2records" "github.com/OpenDiablo2/OpenDiablo2/d2core/d2records"
@ -544,7 +545,12 @@ func (i *Item) generateAffixProperties(pool PropertyPool) []*Property {
for modIdx := range affix.Modifiers { for modIdx := range affix.Modifiers {
mod := affix.Modifiers[modIdx] mod := affix.Modifiers[modIdx]
prop := i.factory.NewProperty(mod.Code, mod.Parameter, mod.Min, mod.Max) paramInt, err := strconv.Atoi(mod.Parameter)
if err != nil {
paramInt = 0
}
prop := i.factory.NewProperty(mod.Code, paramInt, mod.Min, mod.Max)
if prop == nil { if prop == nil {
continue continue
} }
@ -557,48 +563,26 @@ func (i *Item) generateAffixProperties(pool PropertyPool) []*Property {
} }
func (i *Item) generateUniqueProperties() []*Property { func (i *Item) generateUniqueProperties() []*Property {
if i.UniqueRecord() == nil { if record := i.UniqueRecord(); record != nil {
return i.generateItemProperties(record.Properties[:])
}
return nil return nil
} }
result := make([]*Property, 0)
for propIdx := range i.UniqueRecord().Properties {
propInfo := i.UniqueRecord().Properties[propIdx]
// sketchy ass unique records, the param should be an int but sometimes it's the name
// of a skill, which needs to be converted to the skill index
paramStr := getStringComponent(propInfo.Parameter)
paramInt := getNumericComponent(propInfo.Parameter)
if paramStr != "" {
for skillID := range i.factory.asset.Records.Skill.Details {
if i.factory.asset.Records.Skill.Details[skillID].Skill == paramStr {
paramInt = skillID
}
}
}
prop := i.factory.NewProperty(propInfo.Code, paramInt, propInfo.Min, propInfo.Max)
if prop == nil {
continue
}
result = append(result, prop)
}
return result
}
func (i *Item) generateSetItemProperties() []*Property { func (i *Item) generateSetItemProperties() []*Property {
if i.SetItemRecord() == nil { if record := i.SetItemRecord(); record != nil {
return i.generateItemProperties(record.Properties[:])
}
return nil return nil
} }
func (i *Item) generateItemProperties(properties []*d2records.PropertyDescriptor) []*Property {
result := make([]*Property, 0) result := make([]*Property, 0)
for propIdx := range i.SetItemRecord().Properties { for propIdx := range properties {
setProp := i.SetItemRecord().Properties[propIdx] setProp := properties[propIdx]
// like with unique records, the property param is sometimes a skill name // like with unique records, the property param is sometimes a skill name
// as a string, not an integer index // as a string, not an integer index

View File

@ -113,7 +113,7 @@ func createItemAffixRecords(
maxKey := fmt.Sprintf("mod%dmax", i) maxKey := fmt.Sprintf("mod%dmax", i)
modifier := &ItemAffixCommonModifier{ modifier := &ItemAffixCommonModifier{
Code: d.String(codeKey), Code: d.String(codeKey),
Parameter: d.Number(paramKey), Parameter: d.String(paramKey),
Min: d.Number(minKey), Min: d.Number(minKey),
Max: d.Number(maxKey), Max: d.Number(maxKey),
} }

View File

@ -14,12 +14,7 @@ type ItemAffixGroups map[int]*ItemAffixCommonGroup
// ItemAffixCommonModifier is the generic modifier form that prefix/suffix shares // ItemAffixCommonModifier is the generic modifier form that prefix/suffix shares
// modifiers are like dynamic properties, they have a key that points to a property // modifiers are like dynamic properties, they have a key that points to a property
// a parameter for the property, and a min/max value // a parameter for the property, and a min/max value
type ItemAffixCommonModifier struct { type ItemAffixCommonModifier = PropertyDescriptor
Code string
Parameter int
Min int
Max int
}
// ItemAffixCommonGroup is a grouping that is common between prefix/suffix // ItemAffixCommonGroup is a grouping that is common between prefix/suffix
type ItemAffixCommonGroup struct { type ItemAffixCommonGroup struct {

View File

@ -0,0 +1,27 @@
package d2records
// PropertyDescriptor is a generic description of a property, used to create properties.
// Sets, SetItems, UniqueItems will all use this generic form of a property
type PropertyDescriptor struct {
// Code is an ID pointer of a property from Properties.txt,
// these columns control each of the eight different full set modifiers a set item can grant you
// at most.
Code string
// Param is the passed on to the associated property, this is used to pass skill IDs, state IDs,
// monster IDs, montype IDs and the like on to the properties that require them,
// these fields support calculations.
Parameter string
// Min value to assign to the associated property.
// Certain properties have special interpretations based on stat encoding (e.g.
// chance-to-cast and charged skills). See the File Guides for Properties.txt and ItemStatCost.
// txt for further details.
Min int
// Max value to assign to the associated property.
// Certain properties have special interpretations based on stat encoding (e.g.
// chance-to-cast and charged skills). See the File Guides for Properties.txt and ItemStatCost.
// txt for further details.
Max int
}

View File

@ -0,0 +1,8 @@
package d2records
// RareItemAffix described both rare prefixes and suffixes
type RareItemAffix struct {
Name string
IncludedTypes []string
ExcludedTypes []string
}

View File

@ -0,0 +1,45 @@
package d2records
import (
"fmt"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt"
)
const (
numRareAffixInclude = 7
fmtRareAffixInclude = "itype%d"
numRareAffixExclude = 4
fmtRareAffixExclude = "etype%d"
)
func rareItemAffixLoader(d *d2txt.DataDictionary) ([]*RareItemAffix, error) {
records := make([]*RareItemAffix, 0)
for d.Next() {
record := &RareItemPrefixRecord{
Name: d.String("name"),
IncludedTypes: make([]string, 0),
ExcludedTypes: make([]string, 0),
}
for idx := 1; idx <= numRareAffixInclude; idx++ {
column := fmt.Sprintf(fmtRareAffixInclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.IncludedTypes = append(record.IncludedTypes, typeCode)
}
}
for idx := 1; idx <= numRareAffixExclude; idx++ {
column := fmt.Sprintf(fmtRareAffixExclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.ExcludedTypes = append(record.ExcludedTypes, typeCode)
}
}
records = append(records, record)
}
return records, d.Err
}

View File

@ -1,49 +1,15 @@
package d2records package d2records
import ( import (
"fmt"
"log" "log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt"
) )
const (
numRarePrefixInclude = 7
fmtRarePrefixInclude = "itype%d"
numRarePrefixExclude = 4
fmtRarePrefixExclude = "etype%d"
)
func rareItemPrefixLoader(r *RecordManager, d *d2txt.DataDictionary) error { func rareItemPrefixLoader(r *RecordManager, d *d2txt.DataDictionary) error {
records := make(RarePrefixes, 0) records, err := rareItemAffixLoader(d)
if err != nil {
for d.Next() { return err
record := &RareItemPrefixRecord{
Name: d.String("name"),
IncludedTypes: make([]string, 0),
ExcludedTypes: make([]string, 0),
}
for idx := 1; idx <= numRarePrefixInclude; idx++ {
column := fmt.Sprintf(fmtRarePrefixInclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.IncludedTypes = append(record.IncludedTypes, typeCode)
}
}
for idx := 1; idx <= numRarePrefixExclude; idx++ {
column := fmt.Sprintf(fmtRarePrefixExclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.ExcludedTypes = append(record.ExcludedTypes, typeCode)
}
}
records = append(records, record)
}
if d.Err != nil {
return d.Err
} }
r.Item.Rare.Prefix = records r.Item.Rare.Prefix = records

View File

@ -4,8 +4,4 @@ package d2records
type RarePrefixes []*RareItemPrefixRecord type RarePrefixes []*RareItemPrefixRecord
// RareItemPrefixRecord is a name prefix for rare items (items with more than 2 affixes) // RareItemPrefixRecord is a name prefix for rare items (items with more than 2 affixes)
type RareItemPrefixRecord struct { type RareItemPrefixRecord = RareItemAffix
Name string
IncludedTypes []string
ExcludedTypes []string
}

View File

@ -1,49 +1,15 @@
package d2records package d2records
import ( import (
"fmt"
"log" "log"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt" "github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt"
) )
const (
numRareSuffixInclude = 7
fmtRareSuffixInclude = "itype%d"
numRareSuffixExclude = 4
fmtRareSuffixExclude = "etype%d"
)
func rareItemSuffixLoader(r *RecordManager, d *d2txt.DataDictionary) error { func rareItemSuffixLoader(r *RecordManager, d *d2txt.DataDictionary) error {
records := make([]*RareItemSuffixRecord, 0) records, err := rareItemAffixLoader(d)
if err != nil {
for d.Next() { return err
record := &RareItemSuffixRecord{
Name: d.String("name"),
IncludedTypes: make([]string, 0),
ExcludedTypes: make([]string, 0),
}
for idx := 1; idx <= numRareSuffixInclude; idx++ {
column := fmt.Sprintf(fmtRareSuffixInclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.IncludedTypes = append(record.IncludedTypes, typeCode)
}
}
for idx := 1; idx <= numRareSuffixExclude; idx++ {
column := fmt.Sprintf(fmtRareSuffixExclude, idx)
if typeCode := d.String(column); typeCode != "" {
record.ExcludedTypes = append(record.ExcludedTypes, typeCode)
}
}
records = append(records, record)
}
if d.Err != nil {
return d.Err
} }
log.Printf("Loaded %d RareSuffix records", len(records)) log.Printf("Loaded %d RareSuffix records", len(records))

View File

@ -4,8 +4,4 @@ package d2records
type RareSuffixes []*RareItemSuffixRecord type RareSuffixes []*RareItemSuffixRecord
// RareItemSuffixRecord is a name suffix for rare items (items with more than 2 affixes) // RareItemSuffixRecord is a name suffix for rare items (items with more than 2 affixes)
type RareItemSuffixRecord struct { type RareItemSuffixRecord = RareItemAffix
Name string
IncludedTypes []string
ExcludedTypes []string
}

View File

@ -25,16 +25,4 @@ type RunesRecord struct {
} }
// RunewordProperty is a representation of a stat possessed by this runeword // RunewordProperty is a representation of a stat possessed by this runeword
type RunewordProperty struct { type RunewordProperty = PropertyDescriptor
// Code is the property code
Code string
// Param is either string or int, parameter for the property
Param string
// Min is the minimum value for the property
Min int
// Max is the maximum value for the property
Max int
}

View File

@ -23,12 +23,7 @@ const (
) )
// SetItemProperty is describes a property of a set item // SetItemProperty is describes a property of a set item
type SetItemProperty struct { type SetItemProperty = PropertyDescriptor
Code string
Parameter string // depending on the property, this may be an int (usually), or a string
Min int
Max int
}
// Loadrecords loads all of the SetItemRecords from records.txt // Loadrecords loads all of the SetItemRecords from records.txt
func setItemLoader(r *RecordManager, d *d2txt.DataDictionary) error { func setItemLoader(r *RecordManager, d *d2txt.DataDictionary) error {

View File

@ -56,7 +56,7 @@ func setLoader(r *RecordManager, d *d2txt.DataDictionary) error {
propA := &SetProperty{ propA := &SetProperty{
Code: codeA, Code: codeA,
Param: d.String(paramColumn), Parameter: d.String(paramColumn),
Min: d.Number(minColumn), Min: d.Number(minColumn),
Max: d.Number(maxColumn), Max: d.Number(maxColumn),
} }
@ -71,7 +71,7 @@ func setLoader(r *RecordManager, d *d2txt.DataDictionary) error {
propB := &SetProperty{ propB := &SetProperty{
Code: codeB, Code: codeB,
Param: d.String(paramColumn), Parameter: d.String(paramColumn),
Min: d.Number(minColumn), Min: d.Number(minColumn),
Max: d.Number(maxColumn), Max: d.Number(maxColumn),
} }
@ -90,7 +90,7 @@ func setLoader(r *RecordManager, d *d2txt.DataDictionary) error {
if code := d.String(codeColumn); code != "" { if code := d.String(codeColumn); code != "" {
prop := &SetProperty{ prop := &SetProperty{
Code: code, Code: code,
Param: d.String(paramColumn), Parameter: d.String(paramColumn),
Min: d.Number(minColumn), Min: d.Number(minColumn),
Max: d.Number(maxColumn), Max: d.Number(maxColumn),
} }

View File

@ -31,26 +31,4 @@ type SetRecord struct {
} }
// SetProperty represents a property possessed by the set // SetProperty represents a property possessed by the set
type SetProperty struct { type SetProperty = PropertyDescriptor
// Code is an ID pointer of a property from Properties.txt,
// these columns control each of the eight different full set modifiers a set item can grant you
// at most.
Code string
// Param is the passed on to the associated property, this is used to pass skill IDs, state IDs,
// monster IDs, montype IDs and the like on to the properties that require them,
// these fields support calculations.
Param string
// Min value to assign to the associated property.
// Certain properties have special interpretations based on stat encoding (e.g.
// chance-to-cast and charged skills). See the File Guides for Properties.txt and ItemStatCost.
// txt for further details.
Min int
// Max value to assign to the associated property.
// Certain properties have special interpretations based on stat encoding (e.g.
// chance-to-cast and charged skills). See the File Guides for Properties.txt and ItemStatCost.
// txt for further details.
Max int
}

View File

@ -38,76 +38,76 @@ func uniqueItemsLoader(r *RecordManager, d *d2txt.DataDictionary) error {
DropSfxFrame: d.Number("dropsfxframe"), DropSfxFrame: d.Number("dropsfxframe"),
UseSound: d.String("usesound"), UseSound: d.String("usesound"),
Properties: [12]UniqueItemProperty{ Properties: [12]*UniqueItemProperty{
UniqueItemProperty{ {
Code: d.String("prop1"), Code: d.String("prop1"),
Parameter: d.String("par1"), Parameter: d.String("par1"),
Min: d.Number("min1"), Min: d.Number("min1"),
Max: d.Number("max1"), Max: d.Number("max1"),
}, },
UniqueItemProperty{ {
Code: d.String("prop2"), Code: d.String("prop2"),
Parameter: d.String("par2"), Parameter: d.String("par2"),
Min: d.Number("min2"), Min: d.Number("min2"),
Max: d.Number("max2"), Max: d.Number("max2"),
}, },
UniqueItemProperty{ {
Code: d.String("prop3"), Code: d.String("prop3"),
Parameter: d.String("par3"), Parameter: d.String("par3"),
Min: d.Number("min3"), Min: d.Number("min3"),
Max: d.Number("max3"), Max: d.Number("max3"),
}, },
UniqueItemProperty{ {
Code: d.String("prop4"), Code: d.String("prop4"),
Parameter: d.String("par4"), Parameter: d.String("par4"),
Min: d.Number("min4"), Min: d.Number("min4"),
Max: d.Number("max4"), Max: d.Number("max4"),
}, },
{
UniqueItemProperty{
Code: d.String("prop5"), Code: d.String("prop5"),
Parameter: d.String("par5"), Parameter: d.String("par5"),
Min: d.Number("min5"), Min: d.Number("min5"),
Max: d.Number("max5"), Max: d.Number("max5"),
}, },
UniqueItemProperty{
{
Code: d.String("prop6"), Code: d.String("prop6"),
Parameter: d.String("par6"), Parameter: d.String("par6"),
Min: d.Number("min6"), Min: d.Number("min6"),
Max: d.Number("max6"), Max: d.Number("max6"),
}, },
UniqueItemProperty{ {
Code: d.String("prop7"), Code: d.String("prop7"),
Parameter: d.String("par7"), Parameter: d.String("par7"),
Min: d.Number("min7"), Min: d.Number("min7"),
Max: d.Number("max7"), Max: d.Number("max7"),
}, },
UniqueItemProperty{ {
Code: d.String("prop8"), Code: d.String("prop8"),
Parameter: d.String("par8"), Parameter: d.String("par8"),
Min: d.Number("min8"), Min: d.Number("min8"),
Max: d.Number("max8"), Max: d.Number("max8"),
}, },
UniqueItemProperty{ {
Code: d.String("prop9"), Code: d.String("prop9"),
Parameter: d.String("par9"), Parameter: d.String("par9"),
Min: d.Number("min9"), Min: d.Number("min9"),
Max: d.Number("max9"), Max: d.Number("max9"),
}, },
UniqueItemProperty{ {
Code: d.String("prop10"), Code: d.String("prop10"),
Parameter: d.String("par10"), Parameter: d.String("par10"),
Min: d.Number("min10"), Min: d.Number("min10"),
Max: d.Number("max10"), Max: d.Number("max10"),
}, },
UniqueItemProperty{ {
Code: d.String("prop11"), Code: d.String("prop11"),
Parameter: d.String("par11"), Parameter: d.String("par11"),
Min: d.Number("min11"), Min: d.Number("min11"),
Max: d.Number("max11"), Max: d.Number("max11"),
}, },
UniqueItemProperty{ {
Code: d.String("prop12"), Code: d.String("prop12"),
Parameter: d.String("par12"), Parameter: d.String("par12"),
Min: d.Number("min12"), Min: d.Number("min12"),

View File

@ -5,7 +5,7 @@ type UniqueItems map[string]*UniqueItemRecord
// UniqueItemRecord is a representation of a row from uniqueitems.txt // UniqueItemRecord is a representation of a row from uniqueitems.txt
type UniqueItemRecord struct { type UniqueItemRecord struct {
Properties [12]UniqueItemProperty Properties [12]*UniqueItemProperty
Name string Name string
Code string // three letter code, points to a record in Weapons, Armor, or Misc Code string // three letter code, points to a record in Weapons, Armor, or Misc
@ -37,9 +37,4 @@ type UniqueItemRecord struct {
} }
// UniqueItemProperty is describes a property of a unique item // UniqueItemProperty is describes a property of a unique item
type UniqueItemProperty struct { type UniqueItemProperty = PropertyDescriptor
Code string
Parameter string // depending on the property, this may be an int (usually), or a string
Min int
Max int
}