-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
117 lines (92 loc) · 2.97 KB
/
main.lua
File metadata and controls
117 lines (92 loc) · 2.97 KB
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
if Config.EnsureRunningAsAdmin and not gcb.isRunningAsAdmin() then
print("Not running as admin, restarting with elevated privileges...")
gcb.restartAsAdmin()
return
end
-- Initial load of Custom LUA
gcb.loadCustomLua()
gcb.currentGames = {}
local askedForAdmin = false
gcb.onTick = function()
if Config.SetCpuAffinity then
for _, game in ipairs(gcb.currentGames) do
gcb.setGameCpuAffinity(game.pid, game.name)
end
end
gcb.reloadCustomLuaIfChanged()
end
local askedForAdmin = false
gcb.onGameStart = function(pid, name, binary)
table.insert(gcb.currentGames, {
pid = pid,
name = name,
binary = binary
})
print("Game started: " .. name .. " (" .. binary .. "), PID: " .. pid)
local gameData = gcb.getGame(name)
if gameData and gameData["Init-Wait"] and gameData["Init-Wait"].WaitMs then
print("Sleeping " .. gameData["Init-Wait"].WaitMs .. " ms")
gcb.sleepMs(gameData["Init-Wait"].WaitMs)
end
-- Always set affinity, even if the same game runs multiple times
if Config.SetCpuAffinity then
local code = gcb.setGameCpuAffinity(pid, name)
if code == gcb.SET_GAME_CPU_AFFINITY_PERMISSION_DENIED and not askedForAdmin then
askedForAdmin = true
if gcb.showYesNoBox("GCB: Permission Denied",
string.format("Failed to set CPU affinity for '%s'.\nRestart GCB as admin?", name)) then
gcb.restartAsAdmin()
end
end
end
-- Prevent duplicate handling if multiple instances are detected
if #gcb.currentGames >= 2 then
return
end
if Config.DisableDesktopEffects then
gcb.disableDesktopEffects()
end
if Config.DisableNonPrimaryDisplays then
print("Saving and disabling non-primary monitors...")
gcb.saveMonitorStates()
gcb.disableNonPrimaryMonitors()
end
if custom and type(custom.gameStart) == "function" then
custom.gameStart(pid, name, binary)
end
end
gcb.onGameStop = function(pid, name, binary)
print("Game stopped: " .. name .. " (" .. binary .. "), PID: " .. pid)
-- Only handle the first instance of a game
if not gcb.currentGames[1] or gcb.currentGames[1].pid ~= pid then
for i, game in ipairs(gcb.currentGames) do
if game.pid == pid then
table.remove(gcb.currentGames, i)
break
end
end
return
end
table.remove(gcb.currentGames, 1)
if Config.DisableNonPrimaryDisplays then
print("Restoring monitor state...")
gcb.enableNonPrimaryMonitors()
end
if Config.DisableDesktopEffects then
gcb.enableDesktopEffects()
end
if custom and type(custom.gameStop) == "function" then
custom.gameStop(pid, name, binary)
end
end
gcb.onGameForeground = function(pid, name, binary)
if custom and type(custom.gameForeground) == "function" then
custom.gameForeground(pid, name, binary)
end
end
gcb.onGameBackground = function(pid, name, binary)
if custom and type(custom.gameBackground) == "function" then
custom.gameBackground(pid, name, binary)
end
end
print("Waiting for games to be launched ...")