Class: Appsetting

Inherits:
ActiveRecord::Base show all
Defined in:
lib/six-updater-web/app/models/appsetting.rb

Direct Known Subclasses

RvAppsetting

Constant Summary collapse

OVERRIDE_MODEL =
Appsetting
FOLDER =
/(.*)\/(.*)/
SPECIFIC =

Defaults to ArmA2 atm…

false
FRIENDLY_NAME =
"ArmA 2 (Any)"
DEFAULT_EXE =
"arma2.exe"
DEFAULT_EXE_PATH =
""
DEFAULT_BETA_EXE =
"arma2.exe"
DEFAULT_BETA_EXE_PATH =
"beta"
DEFAULT_SERVER_EXE =
"arma2server.exe"
DEFAULT_SERVER_EXE_PATH =
DEFAULT_EXE_PATH
DEFAULT_PARAMS =

TODO: Auto beta mod even?

"-showScriptErrors -noSplash -noFilePatching"
DEFAULT_MODS_PARAM =

TODO

"-mod="
DEFAULT_BETA_MODS =
["beta"]
STATIC_TYPES =
[]
GAMESPY_TYPES =
["arma2pc"]
GAME_MIN_VERSIONS =
["1.0"]
MODS =
[]
FAMILY =

TODO: Auto enumurate from model properties? folders: expansion, common, addons files: arma2.exe, arma2server.exe (-.exe for linux), arma2oa.exe, arma2oaserver.exe, addons/chernarus.pbo

Hash.new
REGKEYS =

TODO: Windows-only

{"Arma2Oa" => ['SOFTWARE\\Bohemia Interactive Studio\\ArmA 2 OA', 'MAIN'], "Arma2" => ['SOFTWARE\\Bohemia Interactive Studio\\ArmA 2', 'MAIN']}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#associated_valid?, #no_errors_in_associated?, #save_associated, #save_associated!, #save_with_unsaved_flag, #to_label, #unsaved=, #unsaved?

Class Method Details

.beta_exeObject



122
123
124
# File 'lib/six-updater-web/app/models/appsetting.rb', line 122

def self.beta_exe()
  self::DEFAULT_BETA_EXE_PATH.empty? ? self::DEFAULT_BETA_EXE : "#{self::DEFAULT_BETA_EXE_PATH}/#{self::DEFAULT_BETA_EXE}"
end

.beta_server_exeObject

TODO: Linux auto cut of ‘.exe’ ?



114
115
116
# File 'lib/six-updater-web/app/models/appsetting.rb', line 114

def self.beta_server_exe()
  self::DEFAULT_BETA_EXE_PATH.empty? ? self::DEFAULT_SERVER_EXE : "#{self::DEFAULT_BETA_EXE_PATH}/#{self::DEFAULT_SERVER_EXE}"
end

.default_pathObject



83
84
85
86
87
88
89
90
91
# File 'lib/six-updater-web/app/models/appsetting.rb', line 83

def self.default_path
  return @mpath unless @mpath.nil?
  @mpath = nil
  self::REGKEYS.each_pair do |k, v|
    @mpath = begin; Win32::Registry.open(Win32::Registry::HKEY_LOCAL_MACHINE, v[0])[v[1]]; rescue; nil; end
    break if @mpath
  end
  @mpath
end

.labelObject



62
63
64
65
# File 'lib/six-updater-web/app/models/appsetting.rb', line 62

def self.label
  #self.to_s.sub("Appsetting", "")
  self::FRIENDLY_NAME
end

.normal_exeObject



126
127
128
# File 'lib/six-updater-web/app/models/appsetting.rb', line 126

def self.normal_exe()
  self::DEFAULT_EXE_PATH.empty? ? self::DEFAULT_EXE : "#{self::DEFAULT_EXE_PATH}/#{self::DEFAULT_EXE}"
end

.server_exeObject



118
119
120
# File 'lib/six-updater-web/app/models/appsetting.rb', line 118

def self.server_exe()
  self::DEFAULT_SERVER_EXE_PATH.empty? ? self::DEFAULT_SERVER_EXE : "#{self::DEFAULT_SERVER_EXE_PATH}/#{self::DEFAULT_SERVER_EXE}"
end

.shortObject



67
68
69
# File 'lib/six-updater-web/app/models/appsetting.rb', line 67

def self.short
  self.to_s.sub("Appsetting", "")
end

.typesObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/six-updater-web/app/models/appsetting.rb', line 71

def self.types
  return [Arma2Appsetting] if self == Appsetting
  t, e = [], self
  until e.nil? || [Appsetting, ActiveRecord::Base].include?(e) do
    t << e
    e = e.superclass
  end
  t += self::STATIC_TYPES
  t.uniq!
  t
end

Instance Method Details

#allowance(signatures) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/six-updater-web/app/models/appsetting.rb', line 236

def allowance(signatures)
  signatures = signatures.map{|e| e.downcase}
  allowed, disallowed, garbage = [], [], []
  path = self.real_path.gsub("\\", "/")
  pbos = Dir[File.join(path, "**", "*.pbo")]
  pbos.each do |p|
    inter = p.sub("#{path}/", "")
    unless inter =~ /\/(addons|dta)\//i || inter =~ /^common\//i || inter =~ /^(addons|dta)\//i
      garbage << inter
      next
    end
    if signatures.empty?
      allowed << inter
      next
    end
    sigs = Dir["#{p}*.bisign"]
    found = false
    sigs.each do |s|
      sig = s.sub("#{p}.", "").sub(".bisign", "").downcase
      if signatures.include?(sig)
        found = true
        allowed << inter
        break
      end
    end
    disallowed << inter unless found
  end
  [allowed, disallowed, garbage]
end

#any_exe?Boolean

Returns:

  • (Boolean)


130
131
132
133
# File 'lib/six-updater-web/app/models/appsetting.rb', line 130

def any_exe?
  ed = self.class::SPECIFIC ? self.class : self.edition
  (!self.exe.nil? && File.exists?(File.join(self.real_path, self.exe))) || File.exists?(File.join(self.real_path, ed.normal_exe)) || File.exists?(File.join(self.real_path, ed.server_exe)) || File.exists?(File.join(self.real_path, ed.beta_exe)) || File.exists?(File.join(self.real_path, ed.beta_server_exe))
end

#default_paramsObject



149
150
151
# File 'lib/six-updater-web/app/models/appsetting.rb', line 149

def default_params
  self.edition.nil? ? nil : self.edition::DEFAULT_PARAMS
end

#detect_editionObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/six-updater-web/app/models/appsetting.rb', line 174

def detect_edition
  # TODO: Read the appsetting.exe to figure out an edition, as a static setting by the user?
  return @det_ed unless @det_ed.nil?
  return nil if self.real_path.nil?
  return nil unless File.directory?(self.real_path)
  if self.class::SPECIFIC
    return self.any_exe? ? self.class : nil
  end
  game = nil
  self.class::FAMILY.each_pair do |key, value|
    found = false
    # Find game
    value[0].each do |e|
      case e
        when /\/$/ # folder
          found = File.directory?(File.join(self.real_path, e))
        else # file
          case RUBY_PLATFORM
            when /-mingw32$/, /-mswin32$/
            else
              e = e.clone
              e.sub!(/\.exe$/, "")
          end
          found = File.exists?(File.join(self.real_path, e))
      end
      break if found
    end
    next unless found
    game = key

    # Find edition
    ed, found, cond = nil, nil, true
    value[1].each_pair do |edition, conditions|
      ed = edition
      conditions.each do |condition|
        c = condition.clone
        reverse = c.sub!(/^!/, "")
        case c
          when /\/$/ # folder
            found = File.directory?(File.join(self.real_path, c))
          else # file
            case RUBY_PLATFORM
              when /-mingw32$/, /-mswin32$/
              else
                c = c.clone
                c.sub!(/\.exe$/, "")
            end
            found = File.exists?(File.join(self.real_path, c))
        end
        cond = reverse ? !found : found
        break if cond
      end
      game = ed if cond
      next unless cond
    end
  end

  return nil unless game
  @det_ed = Object.const_get("#{game}Appsetting")
  @det_ed
end

#edition(force = true) ⇒ Object



93
94
95
96
97
# File 'lib/six-updater-web/app/models/appsetting.rb', line 93

def edition(force = true)
  ed = self.detect_edition
  ed = self.class if ed.nil? && force
  ed
end

#exploreObject



99
100
101
# File 'lib/six-updater-web/app/models/appsetting.rb', line 99

def explore
  SixUpdaterWeb.run_program("explorer.exe", SixUpdaterWeb::BASE_PATH, self.real_path)
end

#found_typesObject



108
109
110
111
# File 'lib/six-updater-web/app/models/appsetting.rb', line 108

def found_types
  t = self.edition(false)
  t ? t.types : []
end

#full_pathObject



272
273
274
275
276
# File 'lib/six-updater-web/app/models/appsetting.rb', line 272

def full_path
  rp = self.real_path
  return nil if rp.nil?
  File.dirname(File.join(rp, self.real_exe)).gsub("\\", "/")
end

#kill!Object



286
287
288
# File 'lib/six-updater-web/app/models/appsetting.rb', line 286

def kill!
  Six::Appmanager.kill_by_name(self.process_name, self.full_path)
end

#labelObject



54
55
56
57
58
59
60
# File 'lib/six-updater-web/app/models/appsetting.rb', line 54

def label
  ar = []
  return self.name unless self.beta || self.server
  ar << "Beta" if self.beta
  ar << "Server" if self.server
  "#{self.name} (#{ar.join(", ")})"
end

#logfileObject



295
296
297
298
299
300
301
# File 'lib/six-updater-web/app/models/appsetting.rb', line 295

def logfile
  if File.directory?(real_logpath)
    File.join(real_logpath, rpt) if File.exists?(File.join(real_logpath, rpt))
  else
    nil
  end
end

#modfoldersObject



303
304
305
306
307
308
309
# File 'lib/six-updater-web/app/models/appsetting.rb', line 303

def modfolders
  if self.beta
    self.edition::DEFAULT_BETA_MODS
  else
    []
  end
end

#nice_editionObject



103
104
105
106
# File 'lib/six-updater-web/app/models/appsetting.rb', line 103

def nice_edition
  t = self.edition(false)
  t ? t.label : "ERROR: None detected"
end

#process_nameObject



266
267
268
269
270
# File 'lib/six-updater-web/app/models/appsetting.rb', line 266

def process_name
  ex = self.real_exe
  return nil unless ex
  File.basename(ex)
end

#processesObject



278
279
280
# File 'lib/six-updater-web/app/models/appsetting.rb', line 278

def processes
  Six::Appmanager.find_process(self.process_name, self.full_path)
end

#read_logfileObject



282
283
284
# File 'lib/six-updater-web/app/models/appsetting.rb', line 282

def read_logfile
  Six::Appmanager.read_logfile logfile unless logfile.nil?
end

#real_exeObject



135
136
137
138
139
140
141
142
143
# File 'lib/six-updater-web/app/models/appsetting.rb', line 135

def real_exe
  return self.exe unless self.exe.nil?
  return nil unless self.edition()
  if self.server
    self.beta ? self.edition.beta_server_exe : self.edition.server_exe
  else
    self.beta ? self.edition.beta_exe : self.edition.normal_exe
  end
end

#real_logpathObject



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/six-updater-web/app/models/appsetting.rb', line 161

def real_logpath
  if self.logpath.nil?
    case RUBY_PLATFORM
      when /-mingw32$/, /-mswin32$/
        "#{File.join(ENV['USERPROFILE'], 'appdata', 'local', 'arma 2')}"
      else
        nil
    end
  else
    self.logpath
  end
end

#real_modpathObject



157
158
159
# File 'lib/six-updater-web/app/models/appsetting.rb', line 157

def real_modpath
  self.modpath.nil? ? self.real_path : self.modpath
end

#real_paramsObject



153
154
155
# File 'lib/six-updater-web/app/models/appsetting.rb', line 153

def real_params
  self.params.nil? ? self.default_params : self.params
end

#real_pathObject



145
146
147
# File 'lib/six-updater-web/app/models/appsetting.rb', line 145

def real_path
  self.path.nil? ? self.class.default_path : self.path
end

#rptObject



290
291
292
293
# File 'lib/six-updater-web/app/models/appsetting.rb', line 290

def rpt
  process_name[/(.*)\./]
  "#{$1}.rpt"
end

#to_updater_ymlObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/six-updater-web/app/models/appsetting.rb', line 40

def to_updater_yml
  hash = Hash.new
  attribute_names.each do |at|
    i = at.to_s
    i = "app_#{i}"
    value = eval(at)
    hash[i.to_sym] = value unless value.nil? || value.to_s.size == 0
  end
  hash[:app_path] = self.real_path unless hash[:app_path]
  hash[:app_exe] = self.real_exe unless hash[:app_exe]
  hash[:mods] = self.modfolders.join(";").gsub("/", "\\")
  hash
end