User Preferences file

Bug #1015938 reported by Pali
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Wargus
Fix Released
Critical
Joris Dauphin

Bug Description

Sometimes we add new options to preferences file. If user has valid preferences file and upgrade to new Wargus version (in which was change introduced) and do not delete or fix preferences file, Stratagus show similar lua error:

[string "/usr/share/games/stratagus/wargus/scripts/s..."]:348: bad argument #1 to 'SetMouseScrollSpeed' (number expected, got nil)
stack traceback:
        [C]: in function 'SetMouseScrollSpeed'
        [string "/usr/share/games/stratagus/wargus/scripts/s..."]:348: in main chunk

It is cased by script scripts/stratagus.lua where is:

Load("preferences.lua")
if (wc2.preferences == nil) then
  wc2.preferences = { ... }
end
...
SetMouseScrollSpeed(wc2.preferences.MouseScrollSpeed)

So with old config wc2.preferences.MouseScrollSpeed is nil.

What do you think, how to fix this problem? It is possible to patch lua functions (SetMouseScrollSpeed) that will also accept nil? Or should we add before each function condition like this? Or is there any other option?

if (wc2.preferences.MouseScrollSpeed == nil) then
  wc2.preferences.MouseScrollSpeed = 1
fi

This is blocker which should be fixed before 2.2.7. Also this affects all games (not only Wargus).

Revision history for this message
Joris Dauphin (joris-dauphin) wrote :

I think the best solution should be something like:

-- 8< stratagus.lua 8< --

wc2 = {}
wc2.preferences = {}

-- set default preference
wc2.preferences.option1 = value1
wc2.preferences.option2 = value2
wc2.preferences.option3 = value3
wc2.preferences.option4 = value4
-- ...

-- overwrite options with user preference.
Load("preferences.lua")

-- Use options now
UseOption1(wc2.preferences.option1)
-- ...

-- >8 --

and

-- 8< preference.lua 8< --
if (wc2 == nil) then wc2 = {} end
if (wc2.preferences == nil) then wc2.preferences = {} end
wc2.preferences.option1 = my_value1
wc2.preferences.option2 = my_value2
wc2.preferences.option3 = my_value3
wc2.preferences.option4 = my_value4
...
-- >8 --

Revision history for this message
Joris Dauphin (joris-dauphin) wrote :

Note that in my previous suggestion, script will be broken with current preference format,
but later, it will allow to add new options without writing boiler code for each option.

Revision history for this message
Joris Dauphin (joris-dauphin) wrote :

I changed Saving format according to my proposal. (stratagus revision 8326).
I have not changed game scripts.

I have now a new idea which should be back compatible:
this condition
-- 8< --
if (wc2.preferences.MouseScrollSpeed == nil) then
  wc2.preferences.MouseScrollSpeed = 1
end
-- >8 --
may be done in a function
-- 8< --
function CompleteMissingValue(table, defaultTable)
 for key, defaultValue in pairs(defaultTable)
  if table[key] == nil then table[key] = defaultValue end
 end
end
-- >8 --

Pali (pali)
Changed in wargus:
status: Confirmed → Fix Committed
assignee: nobody → Joris Dauphin (joris-dauphin)
Pali (pali)
Changed in wargus:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.