-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcustompresets.lua
226 lines (193 loc) · 8.62 KB
/
custompresets.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
local Levels = require("map/levels")
local Customize = require("map/customize")
local WORLD_PRESETS_FOLDER = "world_presets/"
local PRESET_PREFIX = "CUSTOM_"
local EXTENSIONS = {
[LEVELCATEGORY.SETTINGS] = ".wsp",
[LEVELCATEGORY.WORLDGEN] = ".wgp",
}
local function UpgradeCustomPresets(custompreset)
local upgraded = false
local savefileupgrades = require "savefileupgrades"
--[[
if custompreset.version == nil or custompreset.version == 1 then
savefileupgrades.utilities.UpgradeCustomPresetFromV1toV2(custompreset)
upgraded = true
end
]]
return upgraded
end
CustomPresets = Class(function(self)
self.presets = {
[LEVELCATEGORY.SETTINGS] = {},
[LEVELCATEGORY.WORLDGEN] = {},
}
self.presetIDs = {
[LEVELCATEGORY.SETTINGS] = {},
[LEVELCATEGORY.WORLDGEN] = {},
}
end)
function CustomPresets:Load()
self.presetIDs[LEVELCATEGORY.SETTINGS], self.presetIDs[LEVELCATEGORY.WORLDGEN] = TheSim:GetUserPresetFiles()
global("Profile")
if Profile then
local profilepresets = Profile:GetWorldCustomizationPresets()
if profilepresets ~= nil and not IsTableEmpty(profilepresets) then
for i, level in pairs(profilepresets) do
local basepreset = (level.location == "forest" and "SURVIVAL_TOGETHER") or (level.location == "cave" and "DST_CAVE") or (nil)
if basepreset and level.id and level.name and level.desc then
local id = "CUSTOM_"..(level.id):gsub("_", " ") --prepend CUSTOM_ because all custom presets start with that.
local defaultsettings = Customize.GetWorldSettingsOptionsWithLocationDefaults(level.location, level.location == "forest")
local settingsoverrides = {}
for _, v in ipairs(defaultsettings) do
settingsoverrides[v.name] = level.overrides[v.name] or v.default
end
self:SaveCustomPreset(LEVELCATEGORY.SETTINGS, id, basepreset, settingsoverrides, level.name, level.desc)
local defaultworldgen = Customize.GetWorldGenOptionsWithLocationDefaults(level.location, level.location == "forest")
local worldgenoverrides = {}
for _, v in ipairs(defaultworldgen) do
worldgenoverrides[v.name] = level.overrides[v.name] or v.default
end
self:SaveCustomPreset(LEVELCATEGORY.WORLDGEN, id, basepreset, worldgenoverrides, level.name, level.desc)
end
end
Profile:SetValue("customizationpresets", nil)
Profile.dirty = true
Profile:Save()
end
end
end
local function GetPathString(category, presetid)
return WORLD_PRESETS_FOLDER..string.upper(presetid)..EXTENSIONS[category]
end
function CustomPresets:LoadCustomPreset(category, presetid)
assert(string.sub(presetid, 1, 7) == PRESET_PREFIX)
if not table.contains(self.presetIDs[category], presetid) then
--loading presets requires them to be in the presetIDs table.
return
end
if self.presets[category][presetid] then
return self.presets[category][presetid]
end
local presetdata
TheSim:GetPersistentString(GetPathString(category, presetid), function(load_success, data)
if load_success and data ~= nil then
local success, custompreset = RunInSandbox(data)
if success and custompreset then
--[[
custompreset = {
baseid = presetid --"The basegame/modded preset this is based off of."
overrides = overrides --all the overrides in the preset
name = name --the fancy name of the preset
desc = desc --the fancy description of the preset
version = version --the version of the custom preset.
}
--]]
if custompreset.baseid == nil or custompreset.name == nil or custompreset.desc == nil or custompreset.overrides == nil then
return
end
local upgraded = UpgradeCustomPresets(custompreset)
if upgraded then
TheSim:SetPersistentString(GetPathString(category, presetid), DataDumper(custompreset, nil, false))
end
presetdata = deepcopy(Levels.GetDataForID(category, custompreset.baseid))
if presetdata == nil then
--if the preset is just missing, its probably a mod preset, so just do nothing.
return
end
presetdata:SetID(presetid)
presetdata:SetBaseID(custompreset.baseid)
presetdata:SetNameAndDesc(custompreset.name, custompreset.desc)
presetdata.overrides = custompreset.overrides
presetdata.playstyle = category == LEVELCATEGORY.SETTINGS and custompreset.playstyle or nil
end
end
end)
self.presets[category][presetid] = presetdata
return presetdata
end
function CustomPresets:IsValidPreset(category, presetid)
if category == LEVELCATEGORY.COMBINED then
return self:IsValidPreset(LEVELCATEGORY.SETTINGS, presetid) or self:IsValidPreset(LEVELCATEGORY.WORLDGEN, presetid)
end
assert(string.sub(presetid, 1, 7) == PRESET_PREFIX)
if not table.contains(self.presetIDs[category], presetid) then
--loading presets requires them to be in the presetIDs table.
return false
end
if self.presets[category][presetid] then
return true
end
local ret = false
TheSim:GetPersistentString(GetPathString(category, presetid), function(load_success, data)
if load_success and data ~= nil then
local success, custompreset = RunInSandbox(data)
if success and custompreset then
if custompreset.baseid == nil or custompreset.name == nil or custompreset.desc == nil or custompreset.overrides == nil then
return
end
ret = true
end
end
end)
return ret
end
function CustomPresets:SaveCustomPreset(category, presetid, basepreset, overrides, name, desc)
assert(string.sub(presetid, 1, 7) == PRESET_PREFIX)
assert(not self:IsCustomPreset(category, basepreset), "base preset cannot be a custom preset")
if presetid == nil or basepreset == nil or overrides == nil or name == nil or desc == nil then
return
end
local custompreset = {
baseid = basepreset,
overrides = overrides,
name = name,
playstyle = Levels.CalcPlaystyleForSettings(overrides),
desc = desc,
version = 1,
}
local presetdata = deepcopy(Levels.GetDataForID(category, custompreset.baseid))
if presetdata == nil then
return
end
presetdata:SetID(presetid)
presetdata:SetBaseID(custompreset.baseid)
presetdata:SetNameAndDesc(custompreset.name, custompreset.desc)
presetdata.overrides = custompreset.overrides
presetdata.playstyle = custompreset.playstyle
self.presets[category][presetid] = presetdata
if not table.contains(self.presetIDs[category], presetid) then
table.insert(self.presetIDs[category], presetid)
table.sort(self.presetIDs[category])
end
TheSim:SetPersistentString(GetPathString(category, presetid), DataDumper(custompreset, nil, false))
return true
end
function CustomPresets:MoveCustomPreset(category, oldid, presetid, name, desc)
assert(string.sub(oldid, 1, 7) == PRESET_PREFIX)
assert(string.sub(presetid, 1, 7) == PRESET_PREFIX)
local preset = self.presets[category][oldid]
if preset == nil then
return
end
if self:SaveCustomPreset(category, presetid, preset.baseid, preset.overrides, name, desc) then
if oldid ~= presetid then self:DeleteCustomPreset(category, oldid) end
end
return self.presets[category][presetid]
end
function CustomPresets:DeleteCustomPreset(category, presetid)
assert(string.sub(presetid, 1, 7) == PRESET_PREFIX)
self.presets[category][presetid] = nil
table.removearrayvalue(self.presetIDs[category], presetid)
TheSim:ErasePersistentString(GetPathString(category, presetid))
end
function CustomPresets:PresetIDExists(category, presetid)
if category == LEVELCATEGORY.COMBINED then
return self:PresetIDExists(LEVELCATEGORY.SETTINGS, presetid) or self:PresetIDExists(LEVELCATEGORY.WORLDGEN, presetid)
end
return table.contains(self.presetIDs[category], presetid)
end
CustomPresets.IsCustomPreset = CustomPresets.PresetIDExists
function CustomPresets:GetPresetIDs(category)
return self.presetIDs[category]
end