-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
526 lines (451 loc) · 20.3 KB
/
init.lua
File metadata and controls
526 lines (451 loc) · 20.3 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
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
--[[
Copyright (c) 2024 [Marco4413](https://github.com/Marco4413/CP77-RenderPlaneFix)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
]]
local BetterUI = require "BetterUI"
---@enum CustomPatchType
local CustomPatchType = { Empty = 0, RenderPlane = 1 }
local RenderPlaneFix = {
showUI = false,
customPatch = false,
---@type table<string, CustomPatchType>
customPatchComponents = { },
CustomPatchType = CustomPatchType,
-- The whitelist is matched first, so it overrides any blacklist setting
componentNameWhitelist = {
["t0_000_pma_base__full_seamfix"] = true,
["t0_000_pwa_base__full_seamfix"] = true,
["t0_000_pma_base__full"] = true,
["t0_000_pwa_base__full"] = true,
["t0_000_pma_fpp__torso"] = true,
["t0_000_pwa_fpp__torso"] = true,
},
-- Anything that does not match "^[hlstg][012]_%d%d%d_"
-- should not be patched. However, most modded items would
-- not match against that pattern.
componentNamePatternsBlacklist = {
"_shadow$", "_shadowmesh$", "^beard_shadow",
"^[hn][ehtx01]b?_%d%d%d_p?[wm][abcf]a?_",
"^Morph",
},
componentNameBlacklist = {
["shoe_lights"] = true,
["shoes"] = true,
["feet"] = true,
["calves"] = true,
["thighs"] = true,
["legs"] = true,
["torso"] = true,
["body"] = true,
["beard"] = true,
},
patchedComponents = { },
unpatchedComponents = { },
_configInitialized = false,
_classesToPatch = { },
}
function RenderPlaneFix.Log(...)
print(table.concat{"[ ", os.date("%x %X"), " ][ RenderPlaneFix ]: ", ...})
end
function RenderPlaneFix:ResetConfig()
self.customPatch = false
self.customPatchComponents = {
["t0_005_pwa_body__t_bug7718"] = CustomPatchType.RenderPlane,
["t0_005_pwa_body__t_bug_shirt"] = CustomPatchType.RenderPlane,
["g1_014_pwa_gloves__ninja_gloves"] = CustomPatchType.RenderPlane,
["t2_084_pwa_jacket__short_sleeves_dec_nusa"] = CustomPatchType.RenderPlane,
}
self:MigrateConfigFromVersion(nil)
end
function RenderPlaneFix:SaveConfig()
local file = io.open("data/config.json", "w")
file:write(json.encode({
version = 1,
customPatch = self.customPatch,
customPatchComponents = self.customPatchComponents,
}))
io.close(file)
end
function RenderPlaneFix:MigrateConfigFromVersion(version)
if not version or type(version) ~= "number" then
-- Migrate from version 0 to 1
version = 1
self.customPatchComponents["t0_005_pma_body__t_bug5280"] = CustomPatchType.RenderPlane
self.customPatchComponents["t0_005_pma_body__t_bug_shirt2655"] = CustomPatchType.RenderPlane
self.customPatchComponents["g1_014_pma_gloves__ninja_gloves"] = CustomPatchType.RenderPlane
self.customPatchComponents["t2_084_pma__short_sleeves_dec_nusa"] = CustomPatchType.RenderPlane
end
-- Migrate from version x to latest
end
function RenderPlaneFix:LoadConfig()
local ok = pcall(function ()
local file = io.open("data/config.json", "r")
local configText = file:read("*a")
io.close(file)
local config = json.decode(configText)
if not config then return; end
if type(config.customPatch) == "boolean" then
self.customPatch = config.customPatch
end
if type(config.customPatchComponents) == "table" then
self.customPatchComponents = { }
for name, patch in next, config.customPatchComponents do
if patch == CustomPatchType.Empty or patch == CustomPatchType.RenderPlane then
self.customPatchComponents[name] = patch
end
end
end
self:MigrateConfigFromVersion(config.version)
end)
if not ok then self:SaveConfig(); end
end
function RenderPlaneFix:ShouldPatchComponentByName(componentName)
if self.componentNameWhitelist[componentName] then return true; end
if self.componentNameBlacklist[componentName] then return false; end
for _, pattern in next, self.componentNamePatternsBlacklist do
if componentName:find(pattern) then
return false
end
end
return true
end
function RenderPlaneFix:IsPatchableClass(classInstance)
local className = classInstance:GetClassName()
for _, toMatch in next, self._classesToPatch do
if className == toMatch then return true; end
end
return false
end
function RenderPlaneFix:RegisterPatch()
if not self:AreRequirementsMet() or self:IsPatchRegistered() then return false; end
RenderPlaneFix.Log("Creating EntityLifecycleEvent listener")
self._entityListener = NewProxy({
OnPlayerReassemble = {
args = { "handle:EntityLifecycleEvent" },
callback = function(event)
local player = event:GetEntity()
RenderPlaneFix:RunPatchOnEntity(player)
end
}
})
RenderPlaneFix.Log("Adding 'Entity/Reassemble' listener for PlayerPuppet")
Game.GetCallbackSystem()
:RegisterCallback(
"Entity/Reassemble",
self._entityListener:Target(),
self._entityListener:Function("OnPlayerReassemble"))
:AddTarget(EntityTarget.Type("PlayerPuppet"))
:SetLifetime(CallbackLifetime.Forever)
return true
end
function RenderPlaneFix:UnregisterPatch()
if not self:AreRequirementsMet() or not self:IsPatchRegistered() then return false; end
RenderPlaneFix.Log("Removing 'Entity/Reassemble' listener")
Game.GetCallbackSystem()
:UnregisterCallback(
"Entity/Reassemble",
self._entityListener:Target())
self._entityListener = nil
return true
end
function RenderPlaneFix:IsPatchRegistered()
return self._entityListener ~= nil
end
function RenderPlaneFix:AreRequirementsMet()
return Codeware ~= nil
end
function RenderPlaneFix:RunAutoPatchOnEntity(entity)
if not self:AreRequirementsMet() then return false; end
local emptyCName = CName.new()
local renderPlaneCName = CName.new("renderPlane")
self.patchedComponents = { }
self.unpatchedComponents = { }
local entityComponents = entity:GetComponents()
for _, component in next, entityComponents do
if self:IsPatchableClass(component) then
if (self:ShouldPatchComponentByName(component.name.value)
and component.renderingPlaneAnimationParam == emptyCName) then
component.renderingPlaneAnimationParam = renderPlaneCName
component:RefreshAppearance()
end
if component.renderingPlaneAnimationParam == renderPlaneCName then
table.insert(self.patchedComponents, component.name.value)
else
table.insert(self.unpatchedComponents, component.name.value)
end
end
end
return true
end
function RenderPlaneFix:RunCustomPatchOnEntity(entity)
if not self:AreRequirementsMet() then return false; end
local emptyCName = CName.new()
local renderPlaneCName = CName.new("renderPlane")
self.patchedComponents = { }
self.unpatchedComponents = { }
local entityComponents = entity:GetComponents()
for _, component in next, entityComponents do
if self:IsPatchableClass(component) then
local patch = self.customPatchComponents[component.name.value]
if patch then
component.renderingPlaneAnimationParam = (patch == CustomPatchType.RenderPlane and renderPlaneCName or emptyCName)
component:RefreshAppearance()
end
if component.renderingPlaneAnimationParam == renderPlaneCName then
table.insert(self.patchedComponents, component.name.value)
else
table.insert(self.unpatchedComponents, component.name.value)
end
end
end
return true
end
function RenderPlaneFix:RunPatchOnEntity(entity)
if RenderPlaneFix.customPatch then
return RenderPlaneFix:RunCustomPatchOnEntity(entity)
end
return RenderPlaneFix:RunAutoPatchOnEntity(entity)
end
function RenderPlaneFix:GetPatchableComponentsOfEntity(entity)
if not self:AreRequirementsMet() then return { }; end
local patchables = { }
local entityComponents = entity:GetComponents()
for _, component in next, entityComponents do
if self:IsPatchableClass(component) then
table.insert(patchables, component)
end
end
return patchables
end
local function Event_OnInit()
if not RenderPlaneFix:AreRequirementsMet() then
RenderPlaneFix.Log("Mod Requirements not met, please install Codeware")
return
end
RenderPlaneFix._classesToPatch = {
CName.new("entMeshComponent"),
CName.new("entSkinnedMeshComponent"),
CName.new("entGarmentSkinnedMeshComponent"),
CName.new("entMorphTargetSkinnedMeshComponent"),
}
RenderPlaneFix:ResetConfig()
RenderPlaneFix:LoadConfig()
RenderPlaneFix._configInitialized = true
RenderPlaneFix:RegisterPatch()
end
local function Event_OnShutdown()
if RenderPlaneFix._configInitialized then
RenderPlaneFix:SaveConfig()
end
RenderPlaneFix:UnregisterPatch()
end
local function Event_OnDraw()
if not RenderPlaneFix.showUI then return; end
if ImGui.Begin("Render Plane Fix") then
ImGui.TextWrapped(table.concat{
"Mod Requirements Met: ", RenderPlaneFix:AreRequirementsMet() and "Yes" or "No"
})
if not RenderPlaneFix:AreRequirementsMet() then
return
end
ImGui.TextWrapped(table.concat{
"Patch Registered: ", RenderPlaneFix:IsPatchRegistered() and "Yes" or "No"
})
ImGui.Separator()
if ImGui.CollapsingHeader("Patched Components") then
ImGui.TextWrapped(table.concat{
"ALL components that have 'renderPlane' set are shown here.",
" Which does not strictly mean that they were patched by this mod."
})
for i=1, #RenderPlaneFix.patchedComponents do
ImGui.Bullet()
ImGui.TextWrapped(RenderPlaneFix.patchedComponents[i])
end
end
ImGui.Separator()
if ImGui.CollapsingHeader("Unpatched Components") then
ImGui.TextWrapped(table.concat{
"All components that can be patched are shown here.",
" This section is mainly used to see what components were filtered out."
})
for i=1, #RenderPlaneFix.unpatchedComponents do
ImGui.Bullet()
ImGui.TextWrapped(RenderPlaneFix.unpatchedComponents[i])
end
end
ImGui.Separator()
RenderPlaneFix.customPatch = ImGui.Checkbox("Use Custom Patch", RenderPlaneFix.customPatch)
ImGui.TextWrapped("Press the arrow if the sub-menu does not open.")
if ImGui.CollapsingHeader("Custom Patch") then
ImGui.TextWrapped(table.concat{
"If something does not work try reloading the save file first (some changes are not real-time).",
" Especially if you loaded a save with Custom Patch disabled."
})
ImGui.TextWrapped(table.concat{
"You can manage and build your custom patch here.",
" The automatic patch has some issues which cannot easily be fixed.",
" You can try to work around those by manually defining what to patch."
})
ImGui.PushStyleColor(ImGuiCol.Text, .9, .1, 0, 1)
ImGui.TextWrapped(table.concat{
"<!> You can share your custom patch by giving this mod's config file to others.",
" The config file is found within this mod's CET folder ('Cyberpunk2077/bin/x64/plugins/cyber_engine_tweaks/mods/RenderPlaneFix/data/config.json').",
})
ImGui.PopStyleColor()
ImGui.Separator()
if ImGui.CollapsingHeader("Component Selector") then
ImGui.TextWrapped("This menu shows all components (equipped by the player) which can be patched.")
ImGui.Bullet(); ImGui.SameLine(); ImGui.TextWrapped("Pressing the + or - buttons will add or remove the item from the patch list.")
ImGui.Bullet(); ImGui.SameLine(); ImGui.TextWrapped(
"If the item is on the patch list, pressing the R or E buttons will set the component to either 'renderPlane' or 'None'.")
ImGui.Bullet(); ImGui.SameLine(); ImGui.TextWrapped(table.concat{
"If the item is on the patch list, pressing the A button will simulate the patch being applied.",
" Once pressed, you should swap weapons or aim to see the changes."
})
ImGui.Bullet(); ImGui.SameLine(); ImGui.TextWrapped(
"If the auto-patch would apply on the item, its name will be shown in green.")
ImGui.Bullet(); ImGui.SameLine(); ImGui.TextWrapped(
"If the auto-patch would apply on the item but the item is already set to 'renderPlane', its name will be shown in yellow.")
ImGui.Separator()
local player = Game.GetPlayer()
if not player then
ImGui.TextWrapped("No player found, please load a game.")
else
local emptyCName = CName.new()
local renderPlaneCName = CName.new("renderPlane")
local patchables = RenderPlaneFix:GetPatchableComponentsOfEntity(player)
ImGui.PushID("custom-components")
for _, component in next, patchables do
ImGui.PushID(component.name.value)
local patch = RenderPlaneFix.customPatchComponents[component.name.value]
if patch then
if BetterUI.ButtonRemove() then
RenderPlaneFix.customPatchComponents[component.name.value] = nil
end
if patch == CustomPatchType.Empty then
ImGui.SameLine()
if BetterUI.SquareButton("R") then
RenderPlaneFix.customPatchComponents[component.name.value] = CustomPatchType.RenderPlane
end
ImGui.SameLine()
if BetterUI.SquareButton("A") then
component.renderingPlaneAnimationParam = emptyCName
component:RefreshAppearance()
end
elseif patch == CustomPatchType.RenderPlane then
ImGui.SameLine()
if BetterUI.SquareButton("E") then
RenderPlaneFix.customPatchComponents[component.name.value] = CustomPatchType.Empty
end
ImGui.SameLine()
if BetterUI.SquareButton("A") then
component.renderingPlaneAnimationParam = renderPlaneCName
component:RefreshAppearance()
end
end
elseif BetterUI.ButtonAdd() then
RenderPlaneFix.customPatchComponents[component.name.value] = CustomPatchType.RenderPlane
end
ImGui.SameLine()
local shouldBePatched = RenderPlaneFix:ShouldPatchComponentByName(component.name.value)
if shouldBePatched then
if component.renderingPlaneAnimationParam == emptyCName then
ImGui.PushStyleColor(ImGuiCol.Text, .1, .9, 0, 1)
else
ImGui.PushStyleColor(ImGuiCol.Text, .9, .9, 0, 1)
end
end
ImGui.Text(component.name.value)
if shouldBePatched then ImGui.PopStyleColor(); end
ImGui.PopID()
end
ImGui.PopID()
end
end
ImGui.Separator()
if ImGui.CollapsingHeader("Patch Manager") then
ImGui.TextWrapped("This menu shows all components which will be patched (if found).")
ImGui.Bullet(); ImGui.SameLine(); ImGui.TextWrapped("Pressing the - button will remove the item from the patch list.")
ImGui.Bullet(); ImGui.SameLine(); ImGui.TextWrapped(
"If the item is on the patch list, pressing the R or E buttons will set the component to either 'renderPlane' or 'None'.")
ImGui.Separator()
ImGui.PushID("custom-patch")
for name, patch in next, RenderPlaneFix.customPatchComponents do
ImGui.PushID(name)
if BetterUI.ButtonRemove() then
RenderPlaneFix.customPatchComponents[name] = nil
end
if patch == CustomPatchType.Empty then
ImGui.SameLine()
if BetterUI.SquareButton("R") then
RenderPlaneFix.customPatchComponents[name] = CustomPatchType.RenderPlane
end
elseif patch == CustomPatchType.RenderPlane then
ImGui.SameLine()
if BetterUI.SquareButton("E") then
RenderPlaneFix.customPatchComponents[name] = CustomPatchType.Empty
end
end
ImGui.SameLine()
ImGui.Text(name)
ImGui.PopID()
end
ImGui.PopID()
end
end
ImGui.Separator()
ImGui.Text("Patch |")
ImGui.SameLine()
if BetterUI.FitButtonN(3, "Register") then RenderPlaneFix:RegisterPatch(); end
ImGui.SameLine()
if BetterUI.FitButtonN(2, "Unregister") then RenderPlaneFix:UnregisterPatch(); end
ImGui.SameLine()
if BetterUI.FitButtonN(1, "Run") then
local player = Game.GetPlayer()
if player then
RenderPlaneFix:RunPatchOnEntity(player)
end
end
ImGui.Separator()
ImGui.Text("Config |")
ImGui.SameLine()
if BetterUI.FitButtonN(3, "Load") then RenderPlaneFix:LoadConfig(); end
ImGui.SameLine()
if BetterUI.FitButtonN(2, "Save") then RenderPlaneFix:SaveConfig(); end
ImGui.SameLine()
if BetterUI.FitButtonN(1, "Reset") then RenderPlaneFix:ResetConfig(); end
ImGui.Separator()
end
end
local function Event_OnOverlayOpen()
RenderPlaneFix.showUI = true
end
local function Event_OnOverlayClose()
RenderPlaneFix.showUI = false
end
function RenderPlaneFix:Init()
registerForEvent("onInit", Event_OnInit)
registerForEvent("onShutdown", Event_OnShutdown)
registerForEvent("onDraw", Event_OnDraw)
registerForEvent("onOverlayOpen", Event_OnOverlayOpen)
registerForEvent("onOverlayClose", Event_OnOverlayClose)
return self
end
return RenderPlaneFix:Init()