-
Notifications
You must be signed in to change notification settings - Fork 33
/
GlobalCompanySettings.lua
165 lines (141 loc) · 5.15 KB
/
GlobalCompanySettings.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
--
-- GlobalCompany - Settings
--
-- @Interface: --
-- @Author: LS-Modcompany / kevink98
-- @Date: 16.03.2019
-- @Version: 1.0.0.0
--
-- @Support: LS-Modcompany
--
-- Changelog:
--
-- v1.0.0.0 (16.03.2019):
--
--
-- Notes:
--
--
-- ToDo:
--
--
--
GlobalCompanySettings = {}
GlobalCompanySettings._mt = Class(GlobalCompanySettings, g_company.gc_staticClass)
InitObjectClass(GlobalCompanySettings, "GlobalCompanySettings")
GlobalCompanySettings.debugIndex = g_company.debug:registerScriptName("GlobalCompany-Settings")
function GlobalCompanySettings:load()
local self = GlobalCompanySettings:superClass():new(GlobalCompanySettings._mt, g_server ~= nil, g_dedicatedServerInfo == nil)
self.debugData = g_company.debug:getDebugData(GlobalCompanySettings.debugIndex, g_company)
self.settings = {}
self.loadedSettings = {}
self.needSynch = false
g_company.addUpdateable(self, self.update);
self.eventId_setSetting = self:registerEvent(self, self.setSettingEvent, false, false)
self.eventId_loadSettings = self:registerEvent(self, self.loadSettingsEvent, false, true)
self.eventId_loadSettings2 = self:registerEvent(self, self.loadSettingsEvent2, true, true)
return self
end
function GlobalCompanySettings:initSetting(name, default)
if g_server == nil then
return;
end;
if self.settings[name] == nil then
if self.loadedSettings[name] ~= nil then
default = self.loadedSettings[name];
end;
self.settings[name] = default;
else
g_company.debug:writeDev(self.debugData, "InitSetting: Setting %s already exist", name);
end;
end
function GlobalCompanySettings:setSetting(name, value, noEventSend)
self:setSettingEvent({name, value}, noEventSend);
end
function GlobalCompanySettings:setSettingEvent(data, noEventSend)
self:raiseEvent(self.eventId_setSetting, data, noEventSend)
if self.settings[data[1]] ~= nil and data[2] ~= nil then
self.settings[data[1]] = data[2];
else
g_company.debug:writeDev(self.debugData, "SetSetting: Setting %s not exist", data[1]);
end;
end
function GlobalCompanySettings:getSetting(name)
return self.settings[name];
end
function GlobalCompanySettings:haveSetting(name)
return self.settings[name] ~= nil;
end
function GlobalCompanySettings:loadSettings()
if g_server == nil then
self.needSynch = true;
return;
end;
if g_server ~= nil then
local savegameIndex = g_currentMission.missionInfo.savegameIndex;
local savegameFolderPath = g_currentMission.missionInfo.savegameDirectory;
if savegameFolderPath == nil then
savegameFolderPath = ('%ssavegame%d'):format(getUserProfileAppPath(), savegameIndex);
end;
if fileExists(savegameFolderPath .. '/globalCompany.xml') then
local xmlFile = loadXMLFile("globalCompany", savegameFolderPath .. '/globalCompany.xml',"globalCompany");
local key = "globalCompany";
if hasXMLProperty(xmlFile, key) then
local i = 0;
while true do
local settingKey = string.format("%s.settings.setting(%d)", key, i);
if not hasXMLProperty(xmlFile, settingKey) then
break;
end;
local name = getXMLString(xmlFile, settingKey .. "#name");
local value = getXMLBool(xmlFile, settingKey .. "#value");
g_company.settings.loadedSettings[name] = value;
i = i + 1;
end;
end;
delete(xmlFile);
end;
end;
end
function GlobalCompanySettings:loadSettingsEvent(data, noEventSend)
if g_server == nil then
self:raiseEvent(self.eventId_loadSettings, {}, noEventSend)
else
self:loadSettingsEvent2();
end;
end;
function GlobalCompanySettings:loadSettingsEvent2(data, noEventSend)
if g_server ~= nil then
self:raiseEvent(self.eventId_loadSettings2, self.settings, noEventSend)
else
self.settings = data;
end;
end;
function GlobalCompanySettings:saveSettings()
if g_server ~= nil then
local savegameIndex = g_currentMission.missionInfo.savegameIndex;
local savegameFolderPath = g_currentMission.missionInfo.savegameDirectory;
if savegameFolderPath == nil then
savegameFolderPath = ('%ssavegame%d'):format(getUserProfileAppPath(), savegameIndex);
end;
--if fileExists(savegameFolderPath .. '/globalCompany.xml') then
--end;
local xmlFile = createXMLFile("globalCompany", savegameFolderPath .. '/globalCompany.xml',"globalCompany");
local key = "globalCompany";
local i = 0;
for name, value in pairs(g_company.settings.settings) do
setXMLString(xmlFile, string.format("%s.settings.setting(%d)#name", key, i), name);
setXMLBool(xmlFile, string.format("%s.settings.setting(%d)#value", key, i), value);
i = i + 1;
end;
saveXMLFile(xmlFile);
delete(xmlFile);
end;
end
function GlobalCompanySettings:update(dt)
if self.needSynch then
self:loadSettingsEvent();
self.needSynch = false;
end;
g_company.removeUpdateable(self, self.update);
end;