File: download_builds.lua

package info (click to toggle)
spring 106.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 55,260 kB
  • sloc: cpp: 543,946; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (146 lines) | stat: -rw-r--r-- 3,988 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
--  file:    download_builds.lua
--  brief:   downloaded unit buildOptions insertion
--  author:  Dave Rodgers
--
--  Copyright (C) 2007.
--  Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local TDF = TDFparser or VFS.Include('gamedata/parse_tdf.lua')

local dlBuilds

local section='download_builds.lua'

--------------------------------------------------------------------------------

local function SafeLower(str)
  if (type(str) == 'string') then
    return string.lower(str)
  end
  return nil
end


local function Load()
  dlBuilds = {}
  local files = VFS.DirList('download/', '*.tdf')
  for i, f in ipairs(files) do
    local tdf, err = TDF.Parse(f)
    if (tdf == nil) then
      Spring.Log(section, LOG.ERROR, err)
    else
      for menuEntry, menuTable in pairs(tdf) do
        if (type(menuTable) == 'table') then
          local unitMenu = SafeLower(menuTable.unitmenu)
          
          local dlMenu = dlBuilds[unitMenu]
          if (dlMenu == nil) then
            dlMenu = {}
            dlBuilds[unitMenu] = dlMenu
          end
          local unitName   = SafeLower(menuTable.unitname)
          local afterName  = SafeLower(menuTable.aftername)
          local beforeName = SafeLower(menuTable.beforename)
          local menu       = tonumber(menuTable.menu)
          local button     = tonumber(menuTable.button)
          if (unitName) then
            table.insert(dlMenu, {
              unitName   = unitName,
              afterName  = afterName,
              beforeName = beforeName,
              menu       = menu,
              button     = button,
            })
          end
          local dlMenu = dlBuilds[unitMenu] or {}
        end
      end
    end
  end
end


--------------------------------------------------------------------------------

local function FindNameIndex(unitName, buildOptions)
  for i, name in ipairs(buildOptions) do
    if (unitName == name) then
      return i
    end
  end
  return nil
end


local function LinearArray(t)
  if (t == nil) then
    return nil
  end
  local sorted = {}
  for k,v in pairs(t) do
    if (type(k) == 'number') then
      table.insert(sorted, { k, v })
    end
  end
  table.sort(sorted, function(a, b) return a[1] < b[1] end)
  local array = {}
  for i, kv in ipairs(sorted) do
    array[i] = kv[2]
  end
  return array
end


local function Execute(unitDefs)
  if (dlBuilds == nil) then
    Load()
  end

  for name, ud in pairs(unitDefs) do
    local dlMenu = dlBuilds[name]
    if (dlMenu) then

      ud.buildoptions = ud.buildoptions or {}
      for _, entry in ipairs(dlMenu) do
        local buildOptions = ud.buildoptions

        local index = nil
        if (entry.afterName) then
          index = FindNameIndex(entry.afterName, buildOptions)
          index = index and (index + 1) or nil
        elseif (entry.beforeName) then
          index = FindNameIndex(entry.beforeName, buildOptions)
        end

        if (index == nil) then
          if (entry.menu and entry.button) then
            index = ((entry.menu - 2) * 6) + entry.button + 1
          else
            index = (#buildOptions + 1) -- the end
          end
        end
        table.insert(buildOptions, index, entry.unitName)
      end
      ud.buildoptions = LinearArray(ud.buildoptions)

    end
  end
end


--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

return {
  Execute = Execute,
}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------