...

Source file src/go.formulabun.club/srb2kart/addons/filename.go

Documentation: go.formulabun.club/srb2kart/addons

     1  package addons
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  	"unicode"
     7  )
     8  
     9  type AddonType uint8
    10  
    11  const (
    12  	KartFlag AddonType = 1 << iota
    13  	SinglePlayerFlag
    14  	MatchFlag
    15  	RaceFlag
    16  	FlagFlag
    17  	BattleFlag
    18  	CharFlag
    19  	LuaFlag
    20  )
    21  
    22  func GetAddonType(filename string) AddonType {
    23  	var result AddonType = 0
    24  	for _, c := range filename {
    25  		switch unicode.ToLower(c) {
    26  		case 'k', 'x':
    27  			result |= KartFlag
    28  		case 's':
    29  			result |= SinglePlayerFlag
    30  		case 'm':
    31  			result |= MatchFlag
    32  		case 'r':
    33  			result |= RaceFlag
    34  		case 'f':
    35  			result |= FlagFlag
    36  		case 'b':
    37  			result |= BattleFlag
    38  		case 'c':
    39  			result |= CharFlag
    40  		case 'l':
    41  			result |= LuaFlag
    42  		case '_':
    43  			return result
    44  		}
    45  	}
    46  	return result
    47  }
    48  
    49  // Get the addon version from the filename as an array of numbers.
    50  // This function does not catch non conforming version formats
    51  func GetAddonVersion(filename string) []uint {
    52  	lastSep := strings.LastIndexAny(filename, "-_")
    53  	result := make([]uint, 0, 0)
    54  	if lastSep < 0 {
    55  		return result
    56  	}
    57  	i := lastSep + 1
    58  	var digit uint
    59  	for i < len(filename) {
    60  		l := unicode.ToLower(rune(filename[i]))
    61  		switch {
    62  		case unicode.IsDigit(l):
    63  			digit *= 10
    64  			v, _ := strconv.Atoi(string(l))
    65  			digit += uint(v)
    66  			i++
    67  		default:
    68        if unicode.IsDigit(rune(filename[i-1])) {
    69          result = append(result, digit)
    70        }
    71  			digit = 0
    72        i++
    73  		}
    74  	}
    75  	return result
    76  }
    77  

View as plain text