@@ -82,42 +82,6 @@ void hook_weapon_update_on_remove(sdk::C_BaseAttributableItem* thisptr) {
8282 }
8383}
8484
85- struct OrgModelData_s {
86- int ModelIndex = -1 ;
87- int Sequence = -1 ;
88- int LastNewSequence = -1 ;
89- };
90-
91- std::map<sdk::C_BaseEntity*, OrgModelData_s> g_weapon_to_org;
92- sdk::C_BaseEntity* g_lastviewmodel = nullptr ;
93- static std::map<void *, vmt_multi_hook*> C_BaseEntity_UpdateOnRemove_hooks;
94-
95- vmt_multi_hook* Get_ViewModel_Hook (sdk::C_BaseViewModel* This) {
96- return C_BaseEntity_UpdateOnRemove_hooks[*(void **)This];
97- }
98-
99- void __fastcall C_BaseEntity_UpdateOnRemove_Hook (sdk::C_BaseViewModel* This, void * Edx)
100- {
101- if (g_lastviewmodel == This) g_lastviewmodel = nullptr ;
102- g_weapon_to_org.erase (This);
103- auto pHook = Get_ViewModel_Hook (This);
104- auto org_fn = pHook->get_original_function <void (__fastcall*)(sdk::C_BaseEntity*, void *)>(127 );
105- org_fn (This, Edx);
106- }
107-
108- void hook_viewmodel (sdk::C_BaseViewModel* thisptr) {
109-
110- if (C_BaseEntity_UpdateOnRemove_hooks.contains (*(void **)thisptr)) return ;
111-
112- vmt_multi_hook* pHook = new vmt_multi_hook ();
113- if (pHook->initialize_and_hook_instance (thisptr)) {
114- pHook->hook_function (C_BaseEntity_UpdateOnRemove_Hook, 127 );
115- pHook->hook_function (&hooks::C_BaseViewModel_SetSequence::hooked, 219 );
116- C_BaseEntity_UpdateOnRemove_hooks[*(void **)thisptr] = pHook;
117- }
118- }
119-
120-
12185void apply_config_on_attributable_item (sdk::C_BaseAttributableItem* item, item_setting* config, int xuid_lo, int xuid_hi, bool isGlove)
12286{
12387 uint64_t xuid = (uint64_t )((uint32_t )xuid_lo) << 32 | (uint64_t )(uint32_t )xuid_hi;
@@ -242,59 +206,19 @@ bool GetPlayerIndexToViewModelHash(int index, fnv::hash & hash) {
242206 return true ;
243207}
244208
245- int do_sequence_remapping (int org_definition_index, int nSequence, int definition_override_index);
246-
247- void fixup_view_model (sdk::C_BaseViewModel* view_model) {
248- const auto view_model_weapon = get_entity_from_handle<sdk::C_BaseAttributableItem>(view_model->GetWeapon ());
209+ void patch_weapon (sdk::C_BaseAttributableItem* weapon) {
249210
250- if (view_model_weapon) {
251- auto item = (sdk::C_BaseAttributableItem*)view_model_weapon;
211+ auto & definition_index = weapon->GetItemDefinitionIndex ();
252212
253- auto & definition_index = view_model_weapon->GetItemDefinitionIndex ();
254- // All knives are terrorist knives.
255- const auto active_conf = g_config.get_from_xuid_by_definition_index (item->GetOriginalOwnerXuidLow (), item->GetOriginalOwnerXuidHigh (), is_knife (definition_index) ? WEAPON_KNIFE : definition_index);
256- if (active_conf && active_conf->definition_override_index ) {
257- hook_weapon_update_on_remove (view_model_weapon);
258- auto emplace_result = g_weapon_to_orgindex.emplace (view_model_weapon, definition_index);
259- view_model_weapon->GetItemDefinitionIndex () = active_conf->definition_override_index ;
260- const auto override_info = game_data::get_weapon_info (active_conf->definition_override_index );
261- if (override_info) {
262-
263- auto & org = g_weapon_to_org[view_model];
264-
265- int oldModelIndex = view_model->GetModelIndex ();
266- int oldSequence = view_model->GetSequence ();
267-
268- bool bMapSequence = org.LastNewSequence == -1 || oldModelIndex != org.ModelIndex || oldSequence != org.Sequence ;
269-
270- org.ModelIndex = oldModelIndex;
271- org.Sequence = oldSequence;
272-
273- int newIndex = g_model_info->GetModelIndex (override_info->viewModel );
274- view_model->GetModelIndex () = newIndex;
275-
276- if (bMapSequence) {
277- int orgIndex = definition_index;
278- auto it = g_weapon_to_orgindex.find (view_model_weapon);
279- if (it != g_weapon_to_orgindex.end ())
280- orgIndex = it->second ;
281- int newSequence = do_sequence_remapping (definition_index, oldSequence, active_conf->definition_override_index );
213+ // All knives are terrorist knives.
214+ if (const auto active_conf = g_config.get_from_xuid_by_definition_index (weapon->GetOriginalOwnerXuidLow (), weapon->GetOriginalOwnerXuidHigh (), is_knife (definition_index) ? WEAPON_KNIFE : definition_index))
215+ apply_config_on_attributable_item (weapon, active_conf, weapon->GetOriginalOwnerXuidLow (), weapon->GetOriginalOwnerXuidHigh (), false );
216+ else
217+ erase_override_if_exists_by_index (weapon->GetOriginalOwnerXuidLow (), weapon->GetOriginalOwnerXuidHigh (), definition_index);
218+ }
282219
283- view_model->GetSequence () = newSequence;
284- org.LastNewSequence = newSequence;
285- // view_model->SetSequence(newSequence);
286- }
287- else {
288- view_model->GetSequence () = org.LastNewSequence ;
289- }
290220
291- hook_viewmodel (view_model);
292- g_lastviewmodel = view_model;
293- }
294- }
295- }
296- view_model->ValidateModelIndex ();
297- }
221+ void hook_viewmodel (sdk::C_BaseViewModel* thisptr);
298222
299223void On_FRAME_NET_UPDATE_POSTDATAUPDATE_START (sdk::C_BasePlayer* local)
300224{
@@ -389,32 +313,34 @@ void On_FRAME_NET_UPDATE_POSTDATAUPDATE_START(sdk::C_BasePlayer* local)
389313 if (!weapon)
390314 continue ;
391315
392- auto item = (sdk::C_BaseAttributableItem*)weapon;
393-
394- auto & definition_index = weapon->GetItemDefinitionIndex ();
395-
396- // All knives are terrorist knives.
397- if (const auto active_conf = g_config.get_from_xuid_by_definition_index (item->GetOriginalOwnerXuidLow (), item->GetOriginalOwnerXuidHigh (), is_knife (definition_index) ? WEAPON_KNIFE : definition_index))
398- apply_config_on_attributable_item (weapon, active_conf, item->GetOriginalOwnerXuidLow (), item->GetOriginalOwnerXuidHigh (), false );
399- else
400- erase_override_if_exists_by_index (item->GetOriginalOwnerXuidLow (), item->GetOriginalOwnerXuidHigh (), definition_index);
316+ patch_weapon (weapon);
401317 }
402318 }
403319
404- const auto view_model = get_entity_from_handle<sdk::C_BaseViewModel>(local->GetViewModel ());
405- if (view_model) {
406- fixup_view_model (view_model);
407- }
408- }
320+ auto view_model = get_entity_from_handle<sdk::C_BaseViewModel>(local->GetViewModel ());
409321
410- void BeginFixUpViewmodel (sdk::C_BaseViewModel* view_model) {
411- fixup_view_model (view_model);
412- }
322+ if (nullptr == view_model) return ;
323+
324+ hook_viewmodel (view_model);
325+
326+ const auto view_model_weapon = get_entity_from_handle<sdk::C_BaseAttributableItem>(view_model->GetWeapon ());
413327
414- void EndFixUpViewmodel (sdk::C_BaseViewModel* view_model) {
415- auto it = g_weapon_to_org.find (view_model);
416- if (it != g_weapon_to_org.end ()) {
417- view_model->GetModelIndex () = it->second .ModelIndex ;
418- view_model->GetSequence () = it->second .Sequence ;
328+ if (view_model_weapon) {
329+ patch_weapon (view_model_weapon);
330+
331+ auto item = (sdk::C_BaseAttributableItem*)view_model_weapon;
332+
333+ auto & definition_index = view_model_weapon->GetItemDefinitionIndex ();
334+ // All knives are terrorist knives.
335+ const auto active_conf = g_config.get_from_xuid_by_definition_index (item->GetOriginalOwnerXuidLow (), item->GetOriginalOwnerXuidHigh (), is_knife (definition_index) ? WEAPON_KNIFE : definition_index);
336+ if (active_conf && active_conf->definition_override_index ) {
337+ hook_weapon_update_on_remove (view_model_weapon);
338+ auto emplace_result = g_weapon_to_orgindex.emplace (view_model_weapon, definition_index);
339+ view_model_weapon->GetItemDefinitionIndex () = active_conf->definition_override_index ;
340+ const auto override_info = game_data::get_weapon_info (active_conf->definition_override_index );
341+ if (override_info) {
342+ view_model->GetModelIndex () = g_model_info->GetModelIndex (override_info->viewModel );
343+ }
344+ }
419345 }
420346}
0 commit comments