-
Notifications
You must be signed in to change notification settings - Fork 1
/
modmain.lua
512 lines (445 loc) · 16.5 KB
/
modmain.lua
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
--[[
Follow extra rules if you want to contain any codes of Glassic API in your DST mod:
1. You must not publish your mod to WeGame.
2. You must make your mods compatible with Glassic API.
--]]
Assets = {}
PrefabFiles = {}
PreloadAssets = {}
local ENV = env
GLOBAL.setfenv(1, GLOBAL)
GlassicAPI = {}
------------------------------------------------------------------------------------------------------------
-- Import Utils
------------------------------------------------------------------------------------------------------------
local utils =
{
"skinhandler",
"slaxml",
"upvalueutil",
}
for i = 1, #utils do
ENV.modimport("utils/" .. utils[i])
end
------------------------------------------------------------------------------------------------------------
-- RegisterItemAtlas helps you register inventory item atlas, so you don't need to specify each mod prefab's inventory image and atlas
-- It's suitable for a atlas that contains mulitiple images.
-- The root folder is "MODROOT/images"
-- e.g. GlassicAPI.RegisterItemAtlas("inventoryimages", Assets) will import "MODROOT/images/inventoryimges.xml" and register every element inside.
-- must set 'assets_table' to Assets.
---@param atlas_path string
---@param assets_table table
GlassicAPI.RegisterItemAtlas = function(atlas_path, assets_table)
atlas_path = resolvefilepath("images/"..(atlas_path:find(".xml") and atlas_path or atlas_path..".xml"))
local images = {}
local file = io.open(atlas_path, "r")
local parser = ENV.SLAXML:parser({
attribute = function(name, value)
if name == "name" then
table.insert(images, value)
end
end
})
parser:parse(file:read("*a"))
file:close()
if assets_table then
table.insert(assets_table, Asset("ATLAS", atlas_path))
table.insert(assets_table, Asset("ATLAS_BUILD", atlas_path, 256))
end
for _, image in ipairs(images) do
RegisterInventoryItemAtlas(atlas_path, image)
RegisterInventoryItemAtlas(atlas_path, hash(image))
end
end
------------------------------------------------------------------------------------------------------------
-- InitCharacterAssets helps you init assets that a mod character needs.
-- e.g. GlassicAPI.InitCharacterAssets("civi", "DUCK", Assets)
-- must set 'assets_table' to Assets.
-- crafting menu avatar is required since GA 4.2
---@param chat_name string
---@param gender string
---@param assets_table table
GlassicAPI.InitCharacterAssets = function(char_name, char_gender, assets_table)
table.insert(assets_table, Asset("ATLAS", "bigportraits/"..char_name..".xml"))
table.insert(assets_table, Asset("ATLAS", "bigportraits/"..char_name.."_none.xml"))
table.insert(assets_table, Asset("ATLAS", "images/names_"..char_name..".xml"))
table.insert(assets_table, Asset("ATLAS", "images/avatars/avatar_"..char_name..".xml"))
table.insert(assets_table, Asset("ATLAS", "images/avatars/avatar_ghost_"..char_name..".xml"))
table.insert(assets_table, Asset("ATLAS", "images/avatars/self_inspect_"..char_name..".xml"))
table.insert(assets_table, Asset("ATLAS", "images/saveslot_portraits/"..char_name..".xml"))
table.insert(assets_table, Asset("ATLAS", "images/crafting_menu_avatars/avatar_"..char_name..".xml"))
ENV.AddModCharacter(char_name, char_gender)
end
GlassicAPI.InitMinimapAtlas = function(path_to_file, assets_table)
local file = "images/"..path_to_file..".xml"
if assets_table then
table.insert(assets_table, Asset("ATLAS", file))
end
ENV.AddMinimapAtlas(file)
end
------------------------------------------------------------------------------------------------------------
GlassicAPI.SetExclusiveToTag = function(tag)
return function(skin_name, userid)
local player = GlassicAPI.SkinHandler.GetPlayerFromID(userid) or ThePlayer
if player then
return player:HasTag(tag)
end
return true
end
end
------------------------------------------------------------------------------------------------------------
-- use in skins' init_fn most.
-- before basic init, you need to specify skins' floating swap_data to make the anim switch looks normal.
---@param swap_data table
GlassicAPI.SetFloatData = function(inst, swap_data)
if inst.components.floater and swap_data then
inst.components.floater.swap_data = swap_data
end
end
GlassicAPI.UpdateFloaterAnim = function(inst)
local floater = inst.components.floater
if floater and floater:IsFloating() and not (floater.wateranim or floater.landanim) then -- IA Compatible :angri:
floater:SwitchToDefaultAnim(true)
floater:SwitchToFloatAnim()
end
end
------------------------------------------------------------------------------------------------------------
local function set_onquip_skin_item(symbol, symbol_override, frame)
return function(inst)
if not TheWorld.ismastersim then return end
local onequipfn = inst.components.equippable.onequipfn
if onequipfn then
inst.components.equippable:SetOnEquip(function(_inst, _owner)
onequipfn(_inst, _owner)
local skin_build = _inst:GetSkinBuild()
if skin_build then
_owner:PushEvent("equipskinneditem", _inst:GetSkinName())
_owner.AnimState:OverrideItemSkinSymbol(symbol, skin_build, symbol_override, _inst.GUID, frame)
end
end)
end
end
end
-- Set OverrideItemSkinSymbol for official items that has no skin.
-- This is an altanative way to hack into official items without skins.
-- For modded prefabs, you don't need to use this.
---@param name string prefab_name
---@param data table {symbol, symbol_override, frame}
--[[e.g.
if not rawget(_G, "moonglassaxe_clear_fn") then
moonglassaxe_clear_fn = function(inst)
inst.AnimState:SetBank("glassaxe")
GlassicAPI.SetFloatData(inst, { sym_build = "swap_glassaxe" })
basic_clear_fn(inst, "glassaxe")
end
GlassicAPI.SetOnequipSkinItem("moonglassaxe", {"swap_object", "swap_glassaxe", "swap_glassaxe"})
end
--]]
GlassicAPI.SetOnequipSkinItem = function(name, data)
ENV.AddPrefabPostInit(name, set_onquip_skin_item(unpack(data)))
end
-- The common init fn for skinned prefabs.
--[[usage:
CreatePrefabskin("skin_name", {
...,
init_fn = GlassicAPI.BasicInitFn,
...,
})
--]]
GlassicAPI.BasicInitFn = function(inst)
if inst.components.placer == nil and not TheWorld.ismastersim then return end
inst.AnimState:SetSkin(inst:GetSkinBuild())
if inst.components.inventoryitem then
inst.components.inventoryitem:ChangeImageName(inst:GetSkinName())
end
GlassicAPI.UpdateFloaterAnim(inst)
end
-- all actions require component. to avoid compatibility issues, it's better that we use a new component to set actions.
-- GlassicAPI.ShellComponent helps you create such a component.
-- e.g. "scripts/components/glasssocket.lua"
-- usage:
-- return GlassicAPI.ShellComponent
GlassicAPI.ShellComponent = Class(function(self, inst)
self.inst = inst
end)
------------------------------------------------------------------------------------------------------------
local TechTree = require("techtree")
local function rebuild_techtree(name)
TECH.NONE = TechTree.Create()
for k, v in pairs(AllRecipes) do
v.level = TechTree.Create(v.level)
end
for k, v in pairs(TUNING.PROTOTYPER_TREES) do
v = TechTree.Create(v)
TUNING.PROTOTYPER_TREES[k] = TUNING.PROTOTYPER_TREES[k] or {}
TUNING.PROTOTYPER_TREES[k][name] = TUNING.PROTOTYPER_TREES[k][name] or 0
end
end
-- custom tech allows you to build custom prototyper or allows muliti prototypers to bonus a tech simultaneously.
-- e.g. GlassicAPI.AddTech("FRIENDSHIPRING")
---@param name string
GlassicAPI.AddTech = function(name, bonus_available)
table.insert(TechTree.AVAILABLE_TECH, name)
if bonus_available then
table.insert(TechTree.BONUS_TECH, name)
end
rebuild_techtree(name)
end
-- custom prototyper trees.
-- e.g. GlassicAPI.AddPrototyperTrees("DUMMYSCIENCE", {DUMMYTECH = 2})
GlassicAPI.AddPrototyperTrees = function(name, t)
TUNING.PROTOTYPER_TREES[name] = TechTree.Create(t)
end
-- e.g.
-- GlassicAPI.MergeTechBonus("MOONORB_UPGRADED", "FRIENDSHIPRING", 2)
-- GlassicAPI.MergeTechBonus("MOON_ALTAR_FULL", "FRIENDSHIPRING", 2)
-- GlassicAPI.MergeTechBonus("OBSIDIAN_BENCH", "FRIENDSHIPRING", 2)
-- allows muliti prototypers to bonus a tech simultaneously.
GlassicAPI.MergeTechBonus = function(target, name, level)
scheduler:ExecuteInTime(0, function()
if TUNING.PROTOTYPER_TREES[target] then
TUNING.PROTOTYPER_TREES[target][name] = level
end
end)
end
------------------------------------------------------------------------------------------------------------
-- set a recipe not listed in search filter or "EVERYTHING".
local HIDDEN_RECIPES = {}
local CraftingMenuWidget = require("widgets/redux/craftingmenu_widget")
local is_recipe_valid_for_search = CraftingMenuWidget.IsRecipeValidForSearch
function CraftingMenuWidget:IsRecipeValidForSearch(name)
local ret = {is_recipe_valid_for_search(self, name)}
if HIDDEN_RECIPES[name] then
return
end
return unpack(ret)
end
local function init_recipe_print(...)
if KnownModIndex:IsModInitPrintEnabled() then
print("Glassic API", ...)
end
end
local add_recipe_to_filter = function(recipe_name, filter_name)
init_recipe_print("AddRecipeToFilter", recipe_name)
local filter = CRAFTING_FILTERS[filter_name]
if filter ~= nil and filter.default_sort_values[recipe_name] == nil then
table.insert(filter.recipes, recipe_name)
filter.default_sort_values[recipe_name] = #filter.recipes
end
end
-- a smarter way to add a mod recipe, and you don't need to think about filters too much.
-- also, with confog.hidden, you can set your recipe no searching or in "EVERYTHING" filter.
-- same format as AddRecipe2
GlassicAPI.AddRecipe = function(name, ingredients, tech, config, filters)
init_recipe_print("AddRecipe", name)
require("recipe")
mod_protect_Recipe = false
local rec = Recipe2(name, ingredients, tech, config)
if not rec.is_deconstruction_recipe then
if config and config.nounlock then
add_recipe_to_filter(name, CRAFTING_FILTERS.CRAFTING_STATION.name)
end
if config and config.builder_tag and config.nochar == nil then
add_recipe_to_filter(name, CRAFTING_FILTERS.CHARACTER.name)
end
if config and config.nomods == nil then
add_recipe_to_filter(name, CRAFTING_FILTERS.MODS.name)
end
if config and config.hidden then
HIDDEN_RECIPES[name] = true
elseif HIDDEN_RECIPES[name] then
HIDDEN_RECIPES[name] = nil
end
if filters then
for _, filter_name in ipairs(filters) do
add_recipe_to_filter(name, filter_name)
end
end
end
mod_protect_Recipe = true
rec:SetModRPCID()
return rec
end
------------------------------------------------------------------------------------------------------------
local function get_index(t, v)
for index, value in pairs(t) do
if value == v then
return index
end
end
end
local function do_sorting(a, b, filter_name, offset, force_sort)
local filter = CRAFTING_FILTERS[filter_name]
if filter and filter.recipes and type(filter.recipes) == "table" then
local target_position
local recipes = filter.recipes
if get_index(recipes, a) then
if force_sort or table.contains(recipes, b) then
table.remove(recipes, get_index(recipes, a))
target_position = #recipes + 1
end
end
if get_index(recipes, b) then
target_position = get_index(recipes, b) + offset
end
if type(target_position) == "number" then
table.insert(recipes, target_position, a)
filter.default_sort_values[a] = filter.default_sort_values[a] or #recipes
end
end
end
local function try_sorting(a, b, filter_type, offset)
if filter_type then
do_sorting(a, b, filter_type, offset, true)
elseif b then
for filter, data in pairs(CRAFTING_FILTERS) do
do_sorting(a, b, filter, offset)
end
end
end
-- a quick way to sort recipes before or after current recipes.
---@param a string - the recipe name that you want to sort
---@param b string - the target recipe name that we base on.
---@param filter_type string
-- e.g. GlassicAPI.RecipeSortAfter("darkcrystal", "purplegem") will sort "darkcrystal" after "purplegem" in all filters that "purplegem" is in.
-- e.g. GlassicAPI.RecipeSortAfter("darkcrystal", "purplegem", "MAGIC") will only sort "darkcrystal" after "purplegem" in "MAGIC" filter.
-- e.g. GlassicAPI.RecipeSortAfter("darkcrystal", "purplegem", "TOOLS") will only sort "darkcrystal" to the last in "TOOLS" because "purplegem" is not in "TOOLS".
-- e.g. GlassicAPI.RecipeSortAfter("darkcrystal", nil, "TOOLS") will also sort "darkcrystal" to the last in "TOOLS".
-- one of b and filter_type must not be nil.
GlassicAPI.RecipeSortBefore = function(a, b, filter_type)
try_sorting(a, b, filter_type, 0)
end
GlassicAPI.RecipeSortAfter = function(a, b, filter_type)
try_sorting(a, b, filter_type, 1)
end
------------------------------------------------------------------------------------------------------------
local function merge_internal(target, strings, no_override)
for k, v in pairs(strings) do
if type(v) == "table" then
if type(target[k]) == "string" then
if no_override then
error("MERGE PROTECTION: Can not merge a table to a string!")
else
target[k] = {}
end
elseif not target[k] then
target[k] = {}
end
merge_internal(target[k], v, no_override)
else
if not (no_override and target[k] ~= nil) then
target[k] = v
end
end
end
end
GlassicAPI.MergeStringsToGLOBAL = function(strings, custom_field, no_override)
merge_internal(custom_field or STRINGS, strings, no_override)
end
local CHS_CODES = {
zh = "chinese_s", -- Simplified Chinese
zht = "chinese_t", -- Traditional Chinese
chs = "chinese_s", -- Chinese Mod (workshop 367546858)
cht = "chinese_t",
sc = "chinese_s",
}
GlassicAPI.MergeTranslationFromPO = function(base_path, override_lang)
local _defaultlang = LanguageTranslator.defaultlang
local lang = override_lang or _defaultlang
if not CHS_CODES[lang] then return end
local filepath = base_path.."/"..CHS_CODES[lang]..".po"
if not resolvefilepath_soft(filepath) then
print("Could not find a language file matching "..filepath.." in any of the search paths.")
return
end
local temp_lang = lang.."_temp"
LanguageTranslator:LoadPOFile(filepath, temp_lang)
merge_internal(LanguageTranslator.languages[lang], LanguageTranslator.languages[temp_lang])
TranslateStringTable(STRINGS)
LanguageTranslator.languages[temp_lang] = nil
LanguageTranslator.defaultlang = _defaultlang
end
-- Basically LanguageTranslator.ConvertEscapeCharactersToString, but replace "\"s first
GlassicAPI.ConvertEscapeCharactersToString = function(str)
local newstr = string.gsub(str, "\\", "\\\\")
newstr = string.gsub(newstr, "\n", "\\n")
newstr = string.gsub(newstr, "\r", "\\r")
newstr = string.gsub(newstr, "\"", "\\\"")
return newstr
end
local function write_speech(file, base_strings, strings, indent)
indent = indent or 1
local str = ""
for i = 1, indent do
str = str .. "\t"
end
for _, k, v in sorted_pairs(strings) do
if type(v) == "table" then
file:write(str .. k .. " =\n" .. str .. "{\n")
write_speech(file, base_strings and base_strings[k], v, indent + 1)
file:write(str .. "}, \n")
else
local comment = base_strings and base_strings[k] and "" or "-- "
v = GlassicAPI.ConvertEscapeCharactersToString(v)
if tonumber(k) then
file:write(str .. comment .. "\"" .. v .. "\", \n" )
else
file:write(str .. comment .. k .. " = \"" .. v .. "\", \n" )
end
end
end
end
GlassicAPI.MergeSpeechFile = function(base_strings, file, source)
local speech = require(source or "speech_wilson")
file:write("return {\n")
merge_internal(speech, base_strings)
write_speech(file, base_strings, speech)
file:write("}")
file:write("\n")
file:close()
end
local function write_for_strings(base, data, file)
for _, k, v in sorted_pairs(data) do
local path = base.."."..k
if type(v) == "table" then
write_for_strings(path, v, file)
else
file:write('\n')
file:write('#. '..path..'\n')
file:write('msgctxt "'..path..'"\n')
file:write('msgid "'..GlassicAPI.ConvertEscapeCharactersToString(v)..'"\n')
file:write('msgstr ""\n')
end
end
end
GlassicAPI.MakePOTFromStrings = function(file, strings)
file:write("msgid \"\"\n")
file:write("msgstr \"\"\n")
file:write("\"Application: Don't Starve Together\\n\"")
file:write("\n")
file:write("\"POT Version: 2.0\\n\"")
file:write("\n")
write_for_strings("STRINGS", strings, file)
file:close()
end
local initialize_modmain = ModManager.InitializeModMain
ModManager.InitializeModMain = function(self, _modname, env, mainfile, ...)
if mainfile == "modmain.lua" then
env.GlassicAPI = GlassicAPI
end
return initialize_modmain(self, _modname, env, mainfile, ...)
end
-- GLOBAL.GlassicAPI = GlassicAPI
ENV.GlassicAPI = GlassicAPI
local main_files = {
"strings",
"glassicrarity",
}
if IsRail() then
error("Ban WeGame");
end
for i = 1, #main_files do
ENV.modimport("main/" .. main_files[i])
end
if ENV.is_mim_enabled then return end
ENV.modimport("main/reskin_tool")