2018年12月25日火曜日

日本時間でのリセット設定


diggerアドオンを見ていたら以下のコードがあった。
local today = os.date('!%Y-%m-%d', os.time() + 32400)

32400秒は9時間であるためUTC+9つまり日本標準時JSTを指定している。なお、Luaのos.dateで!を使うとUTCで書式化される決まり。それを9時間ずらしてどのタイムゾーンでも日本時間の日付を取得できる。

たとえば、日本時間1月1日AM8時は、ニューヨークだと前日のPM6時。14時間の時差があり日付が異なる状態となる。でも、ニューヨークで稼働しているWindowerも新年1月1日の日付がtodayに格納される。


diggerアドオンは、このtodayの値をsettings(config)を使ってファイルに保存し、地球時間0時またぎのリセットが発生したかどうかを検出している。

res.itemsはnameエントリで言語差を吸収している


サメーさんのShopperアドオンの中身を見ていたら
function validate_item(npc_item)
 for i,v in pairs(res.items) do
  if v.name:lower() == npc_item then
   return v.id
  end
 end
end
こんなコードがあった。res.itemsはres\items.luaの中身が入っている。でも、その中にはnameというエントリは含まれていない。アイテムの名前はenやjaで登録されている。

v.nameとはいったい何だろうか。

addons\lib\resources.luaには以下のコードがある。
local language_string = _addon and _addon.language and _addon.language:lower() or windower.ffxi.get_info().language:lower()

local redict = {
 name = language_string,
 name_log = language_string_log,
 name_short = language_string_short,
 english = 'en',
 japanese = 'ja',
 english_log = 'enl',
 japanese_log = 'ja',
 english_short = 'ens',
 japanese_short = 'jas',
}

-- The metatable for a single resource item (an entry in a sub table of the root resource table)
local resource_entry_mt = {__index = function()
 return function(t, k)
  return redict[k] and t[redict[k]] or table[k]
 end
end()}

このコードから予想できるのは、windowerの言語設定がjapaneseである場合、nameはjaのエイリアスとして機能するということ。

つまり、
item['name'] = item[redict['name']] = item['japanese'] = item[redict['japanese']] = item['ja']


そして、Windowerのwikiにもそう書かれていた。
The general structure of the resources can be gleaned by checking out the respective Lua file in the resources directory. However, there are some deviations. For example, the Lua files use the ISO 639-1 language code for English (en), Japanese (ja), German (de), French (fr), however, in the parsed resources they will appear under their full english names (english, japanese, german and french). Also, a new field name will be added, which is an alias to whatever the addon's language is, or, if none is provided for the addon, what the user's POL language is.


2018年12月13日木曜日

true:fn()について

Windower4\libs\functions.luaの178行目に
function functions.loop(fn, interval, cond)
 ~
 cond = cond or true:fn()
 ~
というコードがある。「true:fn()」はその前後の文脈からすると、常にtrueを返す関数だと思われる。ここでfnは関数loopの引数のことではなく、23行目で定義されている関数となっている。

functions.luaの23行目
for _, t in pairs({functions, boolean, math, string, table}) do
    t.fn = function(val)
        return function()
            return val
        end
    end
end