summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/lua-addons/addons/GearSwap/refresh.lua
blob: 9fb5e95607aadf224d01e7283bd53b584abe9f7d (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
235
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
--Copyright (c) 2013~2016, Byrthnoth
--All rights reserved.

--Redistribution and use in source and binary forms, with or without
--modification, are permitted provided that the following conditions are met:

--    * Redistributions of source code must retain the above copyright
--      notice, this list of conditions and the following disclaimer.
--    * Redistributions in binary form must reproduce the above copyright
--      notice, this list of conditions and the following disclaimer in the
--      documentation and/or other materials provided with the distribution.
--    * Neither the name of <addon name> nor the
--      names of its contributors may be used to endorse or promote products
--      derived from this software without specific prior written permission.

--THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
--ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
--WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
--DISCLAIMED. IN NO EVENT SHALL <your name> BE LIABLE FOR ANY
--DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
--(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
--LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
--ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
--(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
--SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


-- Deals with refreshing player information and loading user settings --



-----------------------------------------------------------------------------------
--Name: refresh_globals()
--Args:
---- None
-----------------------------------------------------------------------------------
--Returns:
---- None
---- Updates all global variables to reflect the player's status. Generally run
---- before calling a player function.
-----------------------------------------------------------------------------------
function refresh_globals(user_event_flag)
    local current = os.clock()
    local dt = current - last_refresh
    if not user_event_flag or dt > 0.05 then
        refresh_player(dt,user_event_flag)
        refresh_ffxi_info(dt,user_event_flag)
        refresh_group_info(dt,user_event_flag)
        last_refresh = current
    end
end

-----------------------------------------------------------------------------------
--Name: load_user_files()
--Args:
---- None
-----------------------------------------------------------------------------------
--Returns:
---- user_env, a table of all of the player defined functions and their current
---- variables.
-----------------------------------------------------------------------------------
function load_user_files(job_id,user_file)
    job_id = tonumber(job_id)
    
    if current_file then
        user_pcall('file_unload',current_file)
    end
    
    for i in pairs(registered_user_events) do
        unregister_event_user(i)
    end
    
    for i in pairs(__raw.text.registry) do
        windower.text.delete(i)
    end
    
    for i in pairs(__raw.prim.registry) do
        windower.prim.delete(i)
    end
    
    current_file = nil
    sets = nil
    user_env = nil
    unhandled_command_events = {}
    --registered_user_events = {}
    include_user_path = nil
    
    language = 'english' -- Reset language to english when changing job files.
    refresh_globals()
    
    if job_id and res.jobs[job_id] then
        player.main_job_id = job_id
        update_job_names()
    end
    
    
    local path,base_dir,filename
    path,base_dir,filename = pathsearch({user_file})
    if not path then
        local long_job = res.jobs[job_id].english
        local short_job = res.jobs[job_id].english_short
        local tab = {player.name..'_'..short_job..'.lua',player.name..'-'..short_job..'.lua',
            player.name..'_'..long_job..'.lua',player.name..'-'..long_job..'.lua',
            player.name..'.lua',short_job..'.lua',long_job..'.lua','default.lua'}
        path,base_dir,filename = pathsearch(tab)
    end
    
    if not path then
        current_file = nil
        sets = nil
        return
    end

    user_env = {gearswap = _G, _global = _global, _settings = _settings,_addon=_addon,
        -- Player functions
        equip = equip, cancel_spell=cancel_spell, change_target=change_target, cast_delay=cast_delay,
        print_set=print_set,set_combine=set_combine,disable=disable,enable=user_enable,
        send_command=send_cmd_user,windower=user_windower,include=include_user,
        midaction=user_midaction,pet_midaction=user_pet_midaction,set_language=set_language,
        show_swaps = show_swaps,debug_mode=debug_mode,include_path=user_include_path,
        register_unhandled_command=user_unhandled_command,move_spell_target=move_spell_target,
        language=language,
        
        -- Library functions
        string=string,math=math,table=table,set=set,list=list,T=T,S=S,L=L,pack=pack,functions=functions,
        os=os,texts=texts,bit=bit,type=type,tostring=tostring,tonumber=tonumber,pairs=pairs,
        ipairs=ipairs, print=print, add_to_chat=add_to_chat_user,unpack=unpack,next=next,
        select=select,lua_base_path=windower.addon_path,empty=empty,file=file,
        loadstring=loadstring,assert=assert,error=error,pcall=pcall,io=io,dofile=dofile,
        
        debug=debug,coroutine=coroutine,setmetatable=setmetatable,getmetatable=getmetatable,
        rawset=rawset,rawget=rawget,require=include_user,
        _libs=_libs,
        
        -- Player environment things
        buffactive=buffactive,
        player=player,
        world=world,
        pet=pet,
        fellow=fellow,
        alliance=alliance,
        party=alliance[1],
        sets={naked = {main=empty,sub=empty,range=empty,ammo=empty,
                head=empty,neck=empty,ear1=empty,ear2=empty,
                body=empty,hands=empty,ring1=empty,ring2=empty,
                back=empty,waist=empty,legs=empty,feet=empty}}
        }
    
    user_env['_G'] = user_env
    
    -- Try to load data/<name>_<main job>.lua
    local funct, err = loadfile(path)
    
    -- If the file cannot be loaded, print the error and load the default.
    if funct == nil then
        print('User file problem: '..err)
        current_file = nil
        sets = nil
        return
    else
        current_file = filename
        print('GearSwap: Loaded your '..current_file..' file!')
    end
    
    setfenv(funct, user_env)
    
    -- Verify that funct contains functions.
    local status, plugin = pcall(funct)
    
    if not status then
        error('GearSwap: File failed to load: \n'..plugin)
        sets = nil
        return nil
    end
    
    _global.pretarget_cast_delay = 0
    _global.precast_cast_delay = 0
    _global.cancel_spell = false
    _global.current_event = 'get_sets'
    user_pcall('get_sets')
    
    gearswap_disabled = false
    sets = user_env.sets
end


-----------------------------------------------------------------------------------
--Name: refresh_player()
--Args:
---- None
-----------------------------------------------------------------------------------
--Returns:
---- None
----
---- Loads player from windower.ffxi.get_player().
---- Adds in a "job", "race", "equipment", "target", and "subtarget" field
---- Also updates "pet" and assigns isvalid and element fields.
---- Further converts player.buffs to buffactive.
-------- Indexes buffs by their buff name and assigns a value equal to the number
-------- of buffs with that name active.
-----------------------------------------------------------------------------------
function refresh_player(dt,user_event_flag)
    local pl, player_mob_table
    if not user_event_flag or dt > 0.5 then
        pl = windower.ffxi.get_player()
        if not pl or not pl.vitals then return end
                
        player_mob_table = windower.ffxi.get_mob_by_index(pl.index)
        if not player_mob_table then return end
        
        table.reassign(player,pl)
        for i,v in pairs(player.vitals) do
            player[i]=v
        end
        update_job_names()
        player.status_id = player.status
        if res.statuses[player.status] then
            player.status = res.statuses[player.status].english
        else
            print(player.status_id)
        end
        player.nation_id = player.nation
        player.nation = res.regions[player.nation_id][language] or 'None'
    
        for i,v in pairs(player_mob_table) do
            if i == 'name' then
                player.mob_name = v
            elseif i~= 'is_npc' and i~='tp' and i~='mpp' and i~='claim_id' and i~='status' then
                player[i] = v
            end
        end
    
        if player_mob_table.race ~= nil then
            player.race_id = player.race
            player.race = res.races[player.race][language]
        end
        
        -- If we have a pet, create or update the table info.
        if player_mob_table and player_mob_table.pet_index then
            local player_pet_table = windower.ffxi.get_mob_by_index(player_mob_table.pet_index)
            if player_pet_table then
                table.reassign(pet, target_complete(player_pet_table))
                pet.claim_id = nil
                pet.is_npc = nil
                pet.isvalid = true
                if pet.tp then pet.tp = pet.tp/10 end
                
                if avatar_element[pet.name] then
                    pet.element = res.elements[avatar_element[pet.name]][language]
                else
                    pet.element = res.elements[-1][language] -- Physical
                end
            else
                table.reassign(pet, {isvalid=false})
            end
        else
            table.reassign(pet, {isvalid=false})
        end
        
        if player.main_job_id == 18 or player.sub_job_id == 18 then
            local auto_tab
            if player.main_job_id == 18 then auto_tab = windower.ffxi.get_mjob_data()
            else auto_tab = windower.ffxi.get_sjob_data() end
            
            if auto_tab.name then
                for i,v in pairs(auto_tab) do
                    if not T{'available_heads','attachments','available_frames','available_attachments','frame','head'}:contains(i) then
                        pet[i] = v
                    end
                end
                pet.available_heads = make_user_table()
                pet.attachments = make_user_table()
                pet.available_frames = make_user_table()
                pet.available_attachments = make_user_table()

                -- available parts
                for i,id in pairs(auto_tab.available_heads) do
                    if res.items[id] and type(res.items[id]) == 'table' then
                        pet.available_heads[res.items[id][language]] = true
                    end
                end
                for i,id in pairs(auto_tab.available_frames) do
                    if res.items[id] and type(res.items[id]) == 'table' then
                        pet.available_frames[res.items[id][language]] = true
                    end
                end
                for i,id in pairs(auto_tab.available_attachments) do
                    if res.items[id] and type(res.items[id]) == 'table' then
                        pet.available_attachments[res.items[id][language]] = true
                    end
                end

                -- actual parts
                pet.head = res.items[auto_tab.head][language]
                pet.frame = res.items[auto_tab.frame][language]
                for i,id in pairs(auto_tab.attachments) do
                    if res.items[id] and type(res.items[id]) == 'table' then
                        pet.attachments[res.items[id][language]] = true
                    end
                end
                
                if pet.max_mp ~= 0 then
                    pet.mpp = math.floor(pet.mp/pet.max_mp*100)
                else
                    pet.mpp = 0
                end
            end
        elseif player.main_job_id == 23 then
            local species_id = windower.ffxi.get_mjob_data().species
            -- Should add instincts when they become available
            
            if species_id then
                player.species = {}
                for i,v in pairs(res.monstrosity[species_id]) do
                    player.species[i] = v
                end
                player.species.name = player.species[language] 
                player.species.tp_moves = copy_entry(res.monstrosity[species_id].tp_moves)
                for i,v in pairs(player.species.tp_moves) do
                    if v > player.main_job_level then
                        player.species.tp_moves[i] = nil
                    end
                end
            end
        else
            player.species = nil
        end
    end
    
    -- This being nil does not cause a return, but items should not really be changing when zoning.
    local cur_equip = table.reassign({},items.equipment)
            
    -- Assign player.equipment to be the gear that has been sent out and the server currently thinks
    -- you are wearing. (the sent_out_equip for loop above).
    player.equipment = make_user_table()
    table.reassign(player.equipment,to_names_set(cur_equip))
    
    -- Assign player.inventory to be keyed to item.inventory[i][language] and to have a value of count, similar to buffactive
    for i,bag in pairs(res.bags) do
        local bag_name = to_windower_bag_api(bag.en)
        if items[bag_name] then player[bag_name] = refresh_item_list(items[bag_name]) end
    end

    -- Monster tables for the target and subtarget.
    player.target = target_complete(windower.ffxi.get_mob_by_target('t'))
    player.subtarget = target_complete(windower.ffxi.get_mob_by_target('st'))
    player.last_subtarget = target_complete(windower.ffxi.get_mob_by_target('lastst'))
    
    
    
    table.reassign(fellow,target_complete(windower.ffxi.get_mob_by_target('<ft>')))
    if fellow.name then
        fellow.isvalid = true
    else
        fellow.isvalid=false
    end
    
    table.reassign(buffactive,convert_buff_list(player.buffs))
    
    for global_variable_name,extradatatable in pairs(_ExtraData) do
        if _G[global_variable_name] then
            for sub_variable_name,value in pairs(extradatatable) do
                _G[global_variable_name][sub_variable_name] = value
            end
        end
    end
end

-----------------------------------------------------------------------------------
--Name: refresh_ffxi_info()
--Args:
---- None
-----------------------------------------------------------------------------------
--Returns:
---- None
----
---- Updates the global "world" with windower.ffxi.get_info (ignores the target field).
---- Also sets windower.ffxi.get_info()['zone'] to be world.area for consistency with spellcast
-----------------------------------------------------------------------------------
function refresh_ffxi_info(dt,user_event_flag)
    local info = windower.ffxi.get_info()
    for i,v in pairs(info) do
        if i == 'zone' and res.zones[v] then
            world.zone = res.zones[v][language]
            world.area = world.zone
        elseif i == 'weather' and res.weather[v] then
            weather_update(v)
        elseif i == 'day' and res.days[v] then
            world.day = res.days[v][language]
            world.day_element = res.elements[res.days[v].element][language]
        elseif i == 'moon' then
            world.moon_pct = v
        elseif i == 'moon_phase' and res.moon_phases[v] then
            world.moon = res.moon_phases[v][language]
        elseif i ~= 'target' then
            world[i] = v
        end
    end
    
    for global_variable_name,extradatatable in pairs(_ExtraData) do
        if _G[global_variable_name] then
            for sub_variable_name,value in pairs(extradatatable) do
                _G[global_variable_name][sub_variable_name] = value
            end
        end
    end
end


-----------------------------------------------------------------------------------
--Name: weather_update(id)
--Args:
---- id  Current weather ID
-----------------------------------------------------------------------------------
--Returns:
---- None, updates the table.
-----------------------------------------------------------------------------------
function weather_update(id)
    world.weather_id = id
    world.real_weather_id = id
    world.real_weather = res.weather[id][language]
    world.real_weather_element = res.elements[res.weather[id].element][language]
    world.real_weather_intensity = res.weather[world.real_weather_id].intensity
    if buffactive[178] then
        world.weather_id = 4
    elseif buffactive[179] then
        world.weather_id = 12
    elseif buffactive[180] then
        world.weather_id = 10
    elseif buffactive[181] then
        world.weather_id = 8
    elseif buffactive[182] then
        world.weather_id = 14
    elseif buffactive[183] then
        world.weather_id = 6
    elseif buffactive[184] then
        world.weather_id = 16
    elseif buffactive[185] then
        world.weather_id = 18
    elseif buffactive[589] then
        world.weather_id = 5
    elseif buffactive[590] then
        world.weather_id = 13
    elseif buffactive[591] then
        world.weather_id = 11
    elseif buffactive[592] then
        world.weather_id = 9
    elseif buffactive[593] then
        world.weather_id = 15
    elseif buffactive[594] then
        world.weather_id = 7
    elseif buffactive[595] then
        world.weather_id = 17
    elseif buffactive[596] then
        world.weather_id = 19
    end
    world.weather = res.weather[world.weather_id][language]
    world.weather_element = res.elements[res.weather[world.weather_id].element][language]
    world.weather_intensity = res.weather[world.weather_id].intensity
end


-----------------------------------------------------------------------------------
--Name: refresh_group_info()
--Args:
---- None
-----------------------------------------------------------------------------------
--Returns:
---- None
----
---- Takes the mob arrays from windower.ffxi.get_party() and splits them from p0~5, a10~15, a20~25
---- into alliance[1][1~6], alliance[2][1~6], alliance[3][1~6], respectively.
---- Also adds a "count" field to alliance (total number of people in alliance) and
---- to the individual subtables (total number of people in each party.
-----------------------------------------------------------------------------------
function refresh_group_info(dt,user_event_flag)
    if not alliance or #alliance == 0 then
        alliance = make_alliance()
    end
    
    local c_alliance = make_alliance()
    
    local j = windower.ffxi.get_party() or {}
    
    c_alliance.leader = j.alliance_leader -- Test whether this works
    c_alliance[1].leader = j.party1_leader
    c_alliance[2].leader = j.party2_leader
    c_alliance[3].leader = j.party3_leader
    
    for i,v in pairs(j) do
        if type(v) == 'table' and v.mob and v.mob.race then
            v.mob.race_id = v.mob.race
            v.mob.race = res.races[v.mob.race][language]
        end
        
        local allyIndex
        local partyIndex
        
        -- For 'p#', ally index is 1, party index is the second char
        if i:sub(1,1) == 'p' and tonumber(i:sub(2)) then
            allyIndex = 1
            partyIndex = tonumber(i:sub(2))+1
        -- For 'a##', ally index is the second char, party index is the third char
        elseif tonumber(i:sub(2,2)) and tonumber(i:sub(3)) then
            allyIndex = tonumber(i:sub(2,2))+1
            partyIndex = tonumber(i:sub(3))+1
        end
        
        if allyIndex and partyIndex then
            if v.mob and partybuffs[v.mob.index] then
                v.buffactive = convert_buff_list(partybuffs[v.mob.index].buffs)
            elseif v.mob and v.mob.index == player.index then
                v.buffactive = buffactive
            end
            c_alliance[allyIndex][partyIndex] = v
            c_alliance[allyIndex].count = c_alliance[allyIndex].count + 1
            c_alliance.count = c_alliance.count + 1
            
            if v.mob then
                if v.mob.id == c_alliance[1].leader then
                    c_alliance[1].leader = v
                elseif v.mob.id == c_alliance[2].leader then
                    c_alliance[2].leader = v
                elseif v.mob.id == c_alliance[3].leader then
                    c_alliance[3].leader = v
                end
                
                if v.mob.id == c_alliance.leader then
                    c_alliance.leader = v
                end
            end
        end
    end
    
        
    -- Clear the old structure while maintaining the party references:
    for ally_party = 1,3 do
        for i,v in pairs(alliance[ally_party]) do
            alliance[ally_party][i] = nil
        end
        alliance[ally_party].count = 0
    end
    alliance.count = 0
    alliance.leader = nil
    
    -- Reassign to the new structure
    table.reassign(alliance[1],c_alliance[1])
    table.reassign(alliance[2],c_alliance[2])
    table.reassign(alliance[3],c_alliance[3])
    alliance.count = c_alliance.count
    alliance.leader = c_alliance.leader
end

-----------------------------------------------------------------------------------
--Name: make_alliance()
--Args:
---- none
-----------------------------------------------------------------------------------
--Returns:
---- one blank alliance structure
-----------------------------------------------------------------------------------
function make_alliance()
    local all = make_user_table()
    all[1]={count=0,leader=nil}
    all[2]={count=0,leader=nil}
    all[3]={count=0,leader=nil}
    all.count=0
    all.leader=nil
    return all
end

-----------------------------------------------------------------------------------
--Name: convert_buff_list(bufflist)
--Args:
---- bufflist (table): List of buffs from windower.ffxi.get_player()['buffs']
-----------------------------------------------------------------------------------
--Returns:
---- buffarr (table)
---- buffarr is indexed by the string buff name and has a value equal to the number
---- of that string present in the buff array. So two marches would give
---- buffarr.march==2.
-----------------------------------------------------------------------------------
function convert_buff_list(bufflist)
    local buffarr = {}
    for i,id in pairs(bufflist) do
        if res.buffs[id] then -- For some reason we always have buff 255 active, which doesn't have an entry.
            local buff = res.buffs[id][language]:lower()
            if buffarr[buff] then
                buffarr[buff] = buffarr[buff] +1
            else
                buffarr[buff] = 1
            end
            
            if buffarr[id] then
                buffarr[id] = buffarr[id] +1
            else
                buffarr[id] = 1
            end
        end
    end
    return buffarr
end

-----------------------------------------------------------------------------------
--Name: refresh_item_list(itemlist)
--Args:
---- itemlist (table): List of items from windower.ffxi.get_items().something
-----------------------------------------------------------------------------------
--Returns:
---- retarr (table)
---- retarr is indexed by the item name, and contains a table with the
---- item id, count, and short name.  If the long name for the item is
---- different than the short name, an additional entry is added for
---- that.
----
---- Overall, this allows doing simple existance checks for both short
---- and long name (eg: player.inventory["Theo. Cap +1"] and
---- player.inventory["Theopany Cap +1"] both return the same table,
---- and both would be 'true' in a conditional check)), and get the item
---- count (player.inventory["Orichalc. Bullet"].count)
---- It also allows one to check for the alternate spelling of an item.
-----------------------------------------------------------------------------------
function refresh_item_list(itemlist)
    retarr = make_user_table()
    for i,v in pairs(itemlist) do
        if type(v) == 'table' and v.id and v.id ~= 0 then
            -- If we don't already have the primary item name in the table, add it.
            if res.items[v.id] and res.items[v.id][language] and not retarr[res.items[v.id][language]] then
                retarr[res.items[v.id][language]] = table.copy(v)
                retarr[res.items[v.id][language]].shortname=res.items[v.id][language]:lower()
                -- If a long version of the name exists, and is different from the short version,
                -- add the long name to the info table and point the long name's key at that table.
                if res.items[v.id][language..'_log'] and res.items[v.id][language..'_log']:lower() ~= res.items[v.id][language]:lower() then
                    retarr[res.items[v.id][language]].longname = res.items[v.id][language..'_log']:lower()
                    retarr[res.items[v.id][language..'_log']] = retarr[res.items[v.id][language]]
                end
            elseif res.items[v.id] and res.items[v.id][language] then
                -- If there's already an entry for this item, all the hard work has already
                -- been done.  Just update the count on the subtable of the main item, and
                -- everything else will link together.
                retarr[res.items[v.id][language]].count = retarr[res.items[v.id][language]].count + v.count
            end
        end
    end
    return retarr
end

-----------------------------------------------------------------------------------
--Name: refresh_user_env()
--Args:
---- none
-----------------------------------------------------------------------------------
--Returns:
---- none, but loads user files if they exist.
-----------------------------------------------------------------------------------
function refresh_user_env(job_id)
    refresh_globals()
    if not job_id then job_id = windower.ffxi.get_player().main_job_id end
    
    if not job_id then
        windower.send_command('@wait 1;lua i '.._addon.name..' refresh_user_env')
    else
        load_user_files(job_id)
    end
end


-----------------------------------------------------------------------------------
--Name: pathsearch()
--Args:
---- files_list - table of strings of the file name to search.
-----------------------------------------------------------------------------------
--Returns:
---- path of a valid file, if it exists. False if it doesn't.
-----------------------------------------------------------------------------------
function pathsearch(files_list)

    -- base directory search order:
    -- windower
    -- %appdata%/Windower/GearSwap
    
    -- sub directory search order:
    -- libs-dev (only in windower addon path)
    -- libs (only in windower addon path)
    -- data/player.name
    -- data/common
    -- data
    
    local gearswap_data = windower.addon_path .. 'data/'
    local gearswap_appdata = (os.getenv('APPDATA') or '') .. '/Windower/GearSwap/'
    
    local search_path = {
        [1] = windower.addon_path .. 'libs-dev/',
        [2] = windower.addon_path .. 'libs/',
        [3] = gearswap_data .. player.name .. '/',
        [4] = gearswap_data .. 'common/',
        [5] = gearswap_data,
        [6] = gearswap_appdata .. player.name .. '/',
        [7] = gearswap_appdata .. 'common/',
        [8] = gearswap_appdata,
        [9] = windower.windower_path .. 'addons/libs/'
    }
    
    local user_path
    local normal_path

    for _,basepath in ipairs(search_path) do
        if windower.dir_exists(basepath) then
            for i,v in ipairs(files_list) do
                if v ~= '' then
                    if include_user_path then
                        user_path = basepath .. include_user_path .. '/' .. v
                    end
                    normal_path = basepath .. v
                    
                    if user_path and windower.file_exists(user_path) then
                        return user_path,basepath,v
                    elseif normal_path and windower.file_exists(normal_path) then
                        return normal_path,basepath,v
                    end
                end
            end
        end
    end
    
    return false
end