2020-09-19 14:33:40 -04:00
|
|
|
package d2records
|
|
|
|
|
|
|
|
import (
|
2020-11-21 05:33:22 -05:00
|
|
|
"fmt"
|
2020-09-19 14:33:40 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2txt"
|
|
|
|
)
|
|
|
|
|
|
|
|
func cubeRecipeLoader(r *RecordManager, d *d2txt.DataDictionary) error {
|
|
|
|
records := make([]*CubeRecipeRecord, 0)
|
|
|
|
|
|
|
|
// There are repeated fields and sections in this file, some
|
|
|
|
// of which have inconsistent naming conventions. These slices
|
|
|
|
// are a simple way to handle them.
|
|
|
|
var outputFields = []string{"output", "output b", "output c"}
|
|
|
|
|
|
|
|
var outputLabels = []string{"", "b ", "c "}
|
|
|
|
|
|
|
|
var propLabels = []string{"mod 1", "mod 2", "mod 3", "mod 4", "mod 5"}
|
|
|
|
|
|
|
|
var inputFields = []string{"input 1", "input 2", "input 3", "input 4", "input 5", "input 6", "input 7"}
|
|
|
|
|
|
|
|
for d.Next() {
|
2020-11-21 05:33:22 -05:00
|
|
|
class, err := classFieldToEnum(d.String("class"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-19 14:33:40 -04:00
|
|
|
record := &CubeRecipeRecord{
|
|
|
|
Description: d.String("description"),
|
|
|
|
|
|
|
|
Enabled: d.Bool("enabled"),
|
|
|
|
Ladder: d.Bool("ladder"),
|
|
|
|
|
|
|
|
MinDiff: d.Number("min diff"),
|
|
|
|
Version: d.Number("version"),
|
|
|
|
|
|
|
|
ReqStatID: d.Number("param"),
|
|
|
|
ReqOperation: d.Number("op"),
|
|
|
|
ReqValue: d.Number("value"),
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
Class: class,
|
2020-09-19 14:33:40 -04:00
|
|
|
|
|
|
|
NumInputs: d.Number("numinputs"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inputs - input 1-7
|
|
|
|
record.Inputs = make([]CubeRecipeItem, len(inputFields))
|
|
|
|
for i := range inputFields {
|
2020-11-21 05:33:22 -05:00
|
|
|
record.Inputs[i], err = newCubeRecipeItem(
|
2020-09-19 14:33:40 -04:00
|
|
|
d.String(inputFields[i]))
|
2020-11-21 05:33:22 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-19 14:33:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create outputs - output "", b, c
|
|
|
|
record.Outputs = make([]CubeRecipeResult, len(outputLabels))
|
2020-11-21 05:33:22 -05:00
|
|
|
|
2020-09-19 14:33:40 -04:00
|
|
|
for o, outLabel := range outputLabels {
|
2020-11-21 05:33:22 -05:00
|
|
|
item, err := newCubeRecipeItem(
|
|
|
|
d.String(outputFields[o]))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-19 14:33:40 -04:00
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
record.Outputs[o] = CubeRecipeResult{
|
|
|
|
Item: item,
|
2020-09-19 14:33:40 -04:00
|
|
|
Level: d.Number(outLabel + "lvl"),
|
|
|
|
ILevel: d.Number(outLabel + "plvl"),
|
|
|
|
PLevel: d.Number(outLabel + "ilvl"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create properties - mod 1-5
|
|
|
|
properties := make([]CubeRecipeItemProperty, len(propLabels))
|
|
|
|
for p, prop := range propLabels {
|
|
|
|
properties[p] = CubeRecipeItemProperty{
|
|
|
|
Code: d.String(outLabel + prop),
|
|
|
|
Chance: d.Number(outLabel + prop + " chance"),
|
|
|
|
Param: d.Number(outLabel + prop + " param"),
|
|
|
|
Min: d.Number(outLabel + prop + " min"),
|
|
|
|
Max: d.Number(outLabel + prop + " max"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
record.Outputs[o].Properties = properties
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Err != nil {
|
|
|
|
return d.Err
|
|
|
|
}
|
|
|
|
|
refactored logging in d2loader, d2record, and d2asset (#898)
* refactored logging in d2config, d2record, and d2asset
* asset manager, record manager, and file loader now utilitize d2util.Logger
* added colored logging to d2util.Logger (excluding windows platforms)
* removed mpq file verification from d2config; d2loader handles this
* record loaders now use the record manager's logger for printing info
* added command line argument for setting log level (`--loglevel 4`, `-l4`, or `-l 4`
* added `LogLevel` parameter to config file
* default log level will show errors, warnings, and info log messages
* specifying log level as an argument overrides setting from config file
* fixed log level tests
2020-11-02 21:23:07 -05:00
|
|
|
r.Logger.Infof("Loaded %d CubeMainRecord records", len(records))
|
2020-09-19 14:33:40 -04:00
|
|
|
|
2020-10-28 13:52:15 -04:00
|
|
|
r.Item.Cube.Recipes = records
|
2020-09-19 14:33:40 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newCubeRecipeItem constructs a CubeRecipeItem from a string of
|
|
|
|
// arguments. arguments include at least an item and sometimes
|
|
|
|
// parameters and/or a count (qty parameter). For example:
|
|
|
|
// "weap,sock,mag,qty=10"
|
2020-11-21 05:33:22 -05:00
|
|
|
func newCubeRecipeItem(f string) (CubeRecipeItem, error) {
|
2020-09-19 14:33:40 -04:00
|
|
|
args := splitFieldValue(f)
|
|
|
|
|
|
|
|
item := CubeRecipeItem{
|
|
|
|
Code: args[0], // the first argument is always the item count
|
|
|
|
Count: 1, // default to a count of 1 (no qty parameter)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore the first argument
|
|
|
|
args = args[1:]
|
|
|
|
|
|
|
|
// Find the qty parameter if it was provided,
|
|
|
|
// convert to int and assign to item.Count
|
|
|
|
for idx, arg := range args {
|
|
|
|
if !strings.HasPrefix(arg, "qty") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := strconv.Atoi(strings.Split(arg, "=")[1])
|
|
|
|
|
|
|
|
if err != nil {
|
2020-11-21 05:33:22 -05:00
|
|
|
// need to be verified
|
|
|
|
return item, fmt.Errorf("error parsing item count %e", err)
|
2020-09-19 14:33:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
item.Count = count
|
|
|
|
|
|
|
|
// Remove the qty parameter
|
|
|
|
if idx != len(args)-1 {
|
|
|
|
args[idx] = args[len(args)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
args = args[:len(args)-1]
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// No other arguments were provided
|
|
|
|
if len(args) == 0 {
|
2020-11-21 05:33:22 -05:00
|
|
|
return item, nil
|
2020-09-19 14:33:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Record the argument strings
|
|
|
|
item.Params = make([]string, len(args))
|
|
|
|
for idx, arg := range args {
|
|
|
|
item.Params[idx] = arg
|
|
|
|
}
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
return item, nil
|
2020-09-19 14:33:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// classFieldToEnum converts class tokens to s2enum.Hero.
|
2020-11-21 05:33:22 -05:00
|
|
|
func classFieldToEnum(f string) ([]d2enum.Hero, error) {
|
2020-09-19 14:33:40 -04:00
|
|
|
split := splitFieldValue(f)
|
|
|
|
enums := make([]d2enum.Hero, len(split))
|
|
|
|
|
|
|
|
for idx, class := range split {
|
|
|
|
if class == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch class {
|
|
|
|
case "bar":
|
|
|
|
enums[idx] = d2enum.HeroBarbarian
|
|
|
|
case "nec":
|
|
|
|
enums[idx] = d2enum.HeroNecromancer
|
|
|
|
case "pal":
|
|
|
|
enums[idx] = d2enum.HeroPaladin
|
|
|
|
case "ass":
|
|
|
|
enums[idx] = d2enum.HeroAssassin
|
|
|
|
case "sor":
|
|
|
|
enums[idx] = d2enum.HeroSorceress
|
|
|
|
case "ama":
|
|
|
|
enums[idx] = d2enum.HeroAmazon
|
|
|
|
case "dru":
|
|
|
|
enums[idx] = d2enum.HeroDruid
|
|
|
|
default:
|
2020-11-21 05:33:22 -05:00
|
|
|
return nil, fmt.Errorf("unknown hero token: '%s'", class)
|
2020-09-19 14:33:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 05:33:22 -05:00
|
|
|
return enums, nil
|
2020-09-19 14:33:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// splitFieldValue splits a string array from the following format:
|
|
|
|
// "one,two,three"
|
|
|
|
func splitFieldValue(s string) []string {
|
|
|
|
return strings.Split(strings.Trim(s, "\""), ",")
|
|
|
|
}
|