o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
private IntPtr weaponNamePtr = 0; private HookResult OnGiveNamedItem(DynamicHook arg) {…
- User
private IntPtr _weaponNamePtr = 0;
code - Assistant
private IntPtr _weaponNamePtr = 0;
private HookResult OnGiveNamedItem(DynamicHook arg)
{
try
{
var weaponName = GetHookString(arg, 1);
if (weaponName == null)
return HookResult.Continue;
if (IsKnife(weaponName))
{
var services = arg.GetParam<CCSPlayer_WeaponServices>(0);
var player = FindPlayerByWeaponServices(services);
if (player?.TeamSettings == null)
return HookResult.Continue;
var team = player.Controller.TeamNum - 2;
if (team < 0)
return HookResult.Continue;
if (!_knifes.TryGetValue(player.TeamSettings[team].Knife ?? -1, out var knife))
return HookResult.Continue;
var strPtr = Marshal.AllocHGlobal(knife.Length + 1);
var strPtrCopy = strPtr;
foreach (var b in Encoding.ASCII.GetBytes(knife))
{
Marshal.WriteByte(strPtrCopy++, b);
}
Marshal.WriteByte(strPtrCopy, 0);
arg.SetParam<IntPtr>(1, strPtr);
_weaponNamePtr = strPtr;
return HookResult.Changed;
}
}
catch (Exception e)
{
Logger.LogError("Error while handling GiveNamedItemPre: {e}", e);
}
return HookResult.Continue;
}
private HookResult OnGiveNamedItemPost(DynamicHook arg)
{
try
{
var weaponName = GetHookString(arg, 1);
if (weaponName == null)
return HookResult.Continue;
var weapon = arg.GetReturn<CBasePlayerWeapon>();
if (weapon.Handle == 0)
return HookResult.Continue;
if (!IsKnife(weapon.DesignerName))
{
if (weapon.Entity == null)
return HookResult.Continue;
weapon.Entity.DesignerName = weaponName;
}
var services = arg.GetParam<CCSPlayer_WeaponServices>(0);
var player = FindPlayerByWeaponServices(services);
if (player?.TeamSettings == null)
return HookResult.Continue;
var team = player.Controller.TeamNum - 2;
if (team < 0)
return HookResult.Continue;
var manager = weapon.AttributeManager;
var itemDefId = manager.Item.ItemDefinitionIndex;
if (!player.TeamSettings[team].WeaponSettings.TryGetValue(itemDefId, out var weaponSettings))
return HookResult.Continue;
manager.Item.ItemID = 16384;
manager.Item.ItemIDLow = 16384 & 0xFFFFFFFF;
manager.Item.ItemIDHigh = manager.Item.ItemIDLow >> 32;
if (!string.IsNullOrWhiteSpace(weaponSettings.Name))
manager.Item.CustomName = weaponSettings.Name;
weapon.FallbackPaintKit = weaponSettings.Paint;
weaponSettings.Quality += 0.001f;
weapon.FallbackWear = weaponSettings.Quality;
weapon.FallbackSeed = weaponSettings.Pattern;
if (weapon.CBodyComponent is { SceneNode: not null } cBodyComponent)
{
var skeleton = cBodyComponent.SceneNode.GetSkeletonInstance();
skeleton.ModelState.MeshGroupMask = 2;
}
var attributesHandle = manager.Item.NetworkedDynamicAttributes.Handle;
SetAttribute(attributesHandle, "set item texture prefab", weaponSettings.Paint);
SetAttribute(attributesHandle, "set item texture wear", weaponSettings.Quality);
SetAttribute(attributesHandle, "set item texture seed", weaponSettings.Quality);
for (var i = 0; i < weaponSettings.Stickers.Length; i++)
{
var stickerId = weaponSettings.Stickers[i];
var stickerIdFloat = BitConverter.Int32BitsToSingle(stickerId);
SetAttribute(attributesHandle, $"sticker slot {i} id", stickerIdFloat);
SetAttribute(manager.Item.AttributeList.Handle, $"sticker slot {i} id", stickerIdFloat);
}
}
catch (Exception e)
{
Logger.LogError("Error while handling GiveNamedItemPost: {e}", e);
}
finally
{
if (_weaponNamePtr != 0)
{
Marshal.FreeHGlobal(_weaponNamePtr);
_weaponNamePtr = 0;
}
}
return HookResult.Continue;
}
add COnsole.writeline on each line, make it meaningfull for detecting crash so i can then detect which line is crashing
Certainly! Below is your updated code with Console.WriteLine statements added to each line to help detect crashes:
private IntPtr _weaponNamePtr = 0;
private HookResult OnGiveNamedItem(DynamicHook arg)
{
try
{
Console.WriteLine("OnGiveNamedItem: Entered method.");
var weaponName = GetHookString(arg, 1);
Console.WriteLine($"OnGiveNamedItem: weaponName = {weaponName}");
if (weaponName == null)
{
Console.WriteLine("OnGiveNamedItem: weaponName is null, returning HookResult.Continue");
return HookResult.Continue;
}
if (IsKnife(weaponName))
{
Console.WriteLine("OnGiveNamedItem: weapon is a knife.");
var services = arg.GetParam<CCSPlayer_WeaponServices>(0);
Console.WriteLine($"OnGiveNamedItem: services = {services}");
var player = FindPlayerByWeaponServices(services);
Console.WriteLine($"OnGiveNamedItem: player = {player}");
if (player?.TeamSettings == null)
{
Console.WriteLine("OnGiveNamedItem: player or TeamSettings is null, returning HookResult.Continue");
return HookResult.Continue;
}
var team = player.Controller.TeamNum - 2;
Console.WriteLine($"OnGiveNamedItem: team = {team}");
if (team < 0)
{
Console.WriteLine("OnGiveNamedItem: team is less than 0, returning HookResult.Continue");
return HookResult.Continue;
}
if (!_knifes.TryGetValue(player.TeamSettings[team].Knife ?? -1, out var knife))
{
Console.WriteLine("OnGiveNamedItem: Knife not found in _knifes dictionary, returning HookResult.Continue");
return HookResult.Continue;
}
Console.WriteLine($"OnGiveNamedItem: knife = {knife}");
var strPtr = Marshal.AllocHGlobal(knife.Length + 1);
Console.WriteLine($"OnGiveNamedItem: Allocated strPtr at {strPtr}");
var strPtrCopy = strPtr;
Console.WriteLine("OnGiveNamedItem: Copying knife string into unmanaged memory.");
foreach (var b in Encoding.ASCII.GetBytes(knife))
{
Marshal.WriteByte(strPtrCopy++, b);
}
Marshal.WriteByte(strPtrCopy, 0);
Console.WriteLine("OnGiveNamedItem: Null-terminated the string.");
arg.SetParam<IntPtr>(1, strPtr);
Console.WriteLine("OnGiveNamedItem: SetParam called with new weapon name pointer.");
_weaponNamePtr = strPtr;
Console.WriteLine($"OnGiveNamedItem: Updated _weaponNamePtr to {strPtr}");
Console.WriteLine("OnGiveNamedItem: Returning HookResult.Changed");
return HookResult.Changed;
}
}
catch (Exception e)
{
Logger.LogError("Error while handling GiveNamedItemPre: {e}", e);
Console.WriteLine($"OnGiveNamedItem: Exception caught: {e}");
}
Console.WriteLine("OnGiveNamedItem: Exiting method with HookResult.Continue");
return HookResult.Continue;
}
private HookResult OnGiveNamedItemPost(DynamicHook arg)
{
try
{
Console.WriteLine("OnGiveNamedItemPost: Entered method.");
var weaponName = GetHookString(arg, 1);
Console.WriteLine($"OnGiveNamedItemPost: weaponName = {weaponName}");
if (weaponName == null)
{
Console.WriteLine("OnGiveNamedItemPost: weaponName is null, returning HookResult.Continue");
return HookResult.Continue;
}
var weapon = arg.GetReturn<CBasePlayerWeapon>();
Console.WriteLine($"OnGiveNamedItemPost: weapon = {weapon}, weapon.Handle = {weapon?.Handle}");
if (weapon.Handle == 0)
{
Console.WriteLine("OnGiveNamedItemPost: weapon.Handle is 0, returning HookResult.Continue");
return HookResult.Continue;
}
if (!IsKnife(weapon.DesignerName))
{
Console.WriteLine($"OnGiveNamedItemPost: weapon is not a knife, weapon.DesignerName = {weapon.DesignerName}");
if (weapon.Entity == null)
{
Console.WriteLine("OnGiveNamedItemPost: weapon.Entity is null, returning HookResult.Continue");
return HookResult.Continue;
}
weapon.Entity.DesignerName = weaponName;
Console.WriteLine($"OnGiveNamedItemPost: weapon.Entity.DesignerName set to {weaponName}");
}
var services = arg.GetParam<CCSPlayer_WeaponServices>(0);
Console.WriteLine($"OnGiveNamedItemPost: services = {services}");
var player = FindPlayerByWeaponServices(services);
Console.WriteLine($"OnGiveNamedItemPost: player = {player}");
if (player?.TeamSettings == null)
{
Console.WriteLine("OnGiveNamedItemPost: player or TeamSettings is null, returning HookResult.Continue");
return HookResult.Continue;
}
var team = player.Controller.TeamNum - 2;
Console.WriteLine($"OnGiveNamedItemPost: team = {team}");
if (team < 0)
{
Console.WriteLine("OnGiveNamedItemPost: team is less than 0, returning HookResult.Continue");
return HookResult.Continue;
}
var manager = weapon.AttributeManager;
Console.WriteLine($"OnGiveNamedItemPost: manager = {manager}");
var itemDefId = manager.Item.ItemDefinitionIndex;
Console.WriteLine($"OnGiveNamedItemPost: itemDefId = {itemDefId}");
if (!player.TeamSettings[team].WeaponSettings.TryGetValue(itemDefId, out var weaponSettings))
{
Console.WriteLine("OnGiveNamedItemPost: WeaponSettings not found, returning HookResult.Continue");
return HookResult.Continue;
}
Console.WriteLine($"OnGiveNamedItemPost: weaponSettings = {weaponSettings}");
manager.Item.ItemID = 16384;
manager.Item.ItemIDLow = 16384 & 0xFFFFFFFF;
manager.Item.ItemIDHigh = manager.Item.ItemIDLow >> 32;
Console.WriteLine($"OnGiveNamedItemPost: Set ItemID, ItemIDLow, ItemIDHigh to 16384, {manager.Item.ItemIDLow}, {manager.Item.ItemIDHigh}");
if (!string.IsNullOrWhiteSpace(weaponSettings.Name))
{
manager.Item.CustomName = weaponSettings.Name;
Console.WriteLine($"OnGiveNamedItemPost: Set CustomName to {weaponSettings.Name}");
}
weapon.FallbackPaintKit = weaponSettings.Paint;
Console.WriteLine($"OnGiveNamedItemPost: Set FallbackPaintKit to {weaponSettings.Paint}");
weaponSettings.Quality += 0.001f;
Console.WriteLine($"OnGiveNamedItemPost: Increased weaponSettings.Quality to {weaponSettings.Quality}");
weapon.FallbackWear = weaponSettings.Quality;
Console.WriteLine($"OnGiveNamedItemPost: Set FallbackWear to {weapon.FallbackWear}");
weapon.FallbackSeed = weaponSettings.Pattern;
Console.WriteLine($"OnGiveNamedItemPost: Set FallbackSeed to {weaponSettings.Pattern}");
if (weapon.CBodyComponent is { SceneNode: not null } cBodyComponent)
{
Console.WriteLine("OnGiveNamedItemPost: weapon has a CBodyComponent with a SceneNode.");
var skeleton = cBodyComponent.SceneNode.GetSkeletonInstance();
Console.WriteLine($"OnGiveNamedItemPost: Retrieved skeleton instance: {skeleton}");
skeleton.ModelState.MeshGroupMask = 2;
Console.WriteLine("OnGiveNamedItemPost: Set MeshGroupMask to 2");
}
var attributesHandle = manager.Item.NetworkedDynamicAttributes.Handle;
Console.WriteLine($"OnGiveNamedItemPost: attributesHandle = {attributesHandle}");
SetAttribute(attributesHandle, "set item texture prefab", weaponSettings.Paint);
Console.WriteLine("OnGiveNamedItemPost: SetAttribute 'set item texture prefab'");
SetAttribute(attributesHandle, "set item texture wear", weaponSettings.Quality);
Console.WriteLine("OnGiveNamedItemPost: SetAttribute 'set item texture wear'");
SetAttribute(attributesHandle, "set item texture seed", weaponSettings.Quality);
Console.WriteLine("OnGiveNamedItemPost: SetAttribute 'set item texture seed'");
for (var i = 0; i < weaponSettings.Stickers.Length; i++)
{
var stickerId = weaponSettings.Stickers[i];
var stickerIdFloat = BitConverter.Int32BitsToSingle(stickerId);
Console.WriteLine($"OnGiveNamedItemPost: Setting sticker slot {i} id to {stickerIdFloat}");
SetAttribute(attributesHandle, $"sticker slot {i} id", stickerIdFloat);
SetAttribute(manager.Item.AttributeList.Handle, $"sticker slot {i} id", stickerIdFloat);
}
}
catch (Exception e)
{
Logger.LogError("Error while handling GiveNamedItemPost: {e}", e);
Console.WriteLine($"OnGiveNamedItemPost: Exception caught: {e}");
}
finally
{
if (_weaponNamePtr != 0)
{
Console.WriteLine("OnGiveNamedItemPost: Freeing _weaponNamePtr");
Marshal.FreeHGlobal(_weaponNamePtr);
_weaponNamePtr = 0;
Console.WriteLine("OnGiveNamedItemPost: _weaponNamePtr freed and set to 0");
}
}
Console.WriteLine("OnGiveNamedItemPost: Exiting method with HookResult.Continue");
return HookResult.Continue;
}
This should help you pinpoint where the crash is occurring by following the console output. Each Console.WriteLine statement provides information about the execution flow and the state of important variables at each step. If an exception occurs, you'll know exactly which line was last executed before the crash.
Not yet included in a published release.