Raised This Month: $51 Target: $400
 12% 

Solved SourceMod INVALID_HANDLE - new syntax bug?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 04-25-2018 , 10:47   SourceMod INVALID_HANDLE - new syntax bug?
Reply With Quote #1

Please view the code below to see what I mean.

Sometimes "INVALID_HANDLE" is fine but sometimes you must use "view_as<KeyValues>(INVALID_HANDLE)" and sometimes both are fine. This inconsistency is annoying, and while we're at it, the whole new syntax, view_as stuff and methodmaps have made scripts ugly and difficult to quickly view and understand what's happening.

Tested on SM version 1.8.0.5973. You can compile and toggle the comments to test yourself.

PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

#define KV_INVALID_HANDLE view_as<KeyValues>(INVALID_HANDLE) // WTF SERIOUSLY

KeyValues g_hMapFile;

public 
void OnPluginStart()
{
  
g_hMapFile OpenConfig(false);

  
// if( g_hMapFile != KV_INVALID_HANDLE ) // No error or warning.
  
if( g_hMapFile != INVALID_HANDLE // No error or warning.
  
{
    
delete g_hMapFile;
    
// g_hMapFile = INVALID_HANDLE; // warning 213: tag mismatch
    
g_hMapFile KV_INVALID_HANDLE// No warning
  
}
}

// Single function to open and return Handle/KeyValues datatype to use in various functions for adding/deleting/editing a config.
KeyValues OpenConfig(bool create true)
{
  
// Create config if it does not exist
  
char sPath[PLATFORM_MAX_PATH];
  
BuildPath(Path_SMsPathsizeof(sPath), "%s""data/l4d2_jukebox_spawns.cfg");
  if( !
FileExists(sPath) )
  {
    if( 
create == false )
    {
      
// return INVALID_HANDLE; // warning 213: tag mismatch
      
return KV_INVALID_HANDLE// No warning
    
}

    
File hCfg OpenFile(sPath"w");
    
hCfg.WriteLine("");
    
delete hCfg;
  }

  
// Open the jukebox config
  
KeyValues hFile = new KeyValues("jukeboxes");
  if( !
hFile.ImportFromFile(sPath) )
  {
    
delete hFile;
    
// return INVALID_HANDLE; // warning 213: tag mismatch
    
return KV_INVALID_HANDLE// No warning
  
}

  return 
hFile;

__________________

Last edited by Silvers; 04-25-2018 at 11:33.
Silvers is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 04-25-2018 , 10:54   Re: SourceMod INVALID_HANDLE - new syntax bug?
Reply With Quote #2

Quote:
Originally Posted by Silvers View Post
Please view the code below to see what I mean.

Sometimes "INVALID_HANDLE" is fine but sometimes you must use "view_as<KeyValues>(INVALID_HANDLE)" and sometimes both are fine. This inconsistency is annoying, and while we're at it, the whole new syntax, view_as stuff and methodmaps have made scripts ugly and difficult to quickly view and understand what's happening.

Tested on SM version 1.8.0.5973. You can compile and toggle the comments to test yourself.

PHP Code:
#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

#define KV_INVALID_HANDLE view_as<KeyValues>(INVALID_HANDLE) // WTF SERIOUSLY

KeyValues g_hMapFile;

public 
void OnPluginStart()
{
  
g_hMapFile OpenConfig(false);

  
// if( g_hMapFile != KV_INVALID_HANDLE ) // No error or warning.
  
if( g_hMapFile != INVALID_HANDLE // No error or warning.
  
{
    
delete g_hMapFile;
    
// g_hMapFile = INVALID_HANDLE; // warning 213: tag mismatch
    
g_hMapFile KV_INVALID_HANDLE// No warning
  
}
}

// Single function to open and return Handle/KeyValues datatype to use in various functions for adding/deleting/editing a config.
KeyValues OpenConfig(bool create true)
{
  
// Create config if it does not exist
  
char sPath[PLATFORM_MAX_PATH];
  
BuildPath(Path_SMsPathsizeof(sPath), "%s""data/l4d2_jukebox_spawns.cfg");
  if( !
FileExists(sPath) )
  {
    if( 
create == false )
    {
      
// return INVALID_HANDLE; // warning 213: tag mismatch
      
return KV_INVALID_HANDLE// No warning
    
}

    
File hCfg OpenFile(sPath"w");
    
hCfg.WriteLine("");
    
delete hCfg;
  }

  
// Open the jukebox config
  
KeyValues hFile = new KeyValues("jukeboxes");
  if( !
hFile.ImportFromFile(sPath) )
  {
    
delete hFile;
    
// return INVALID_HANDLE; // warning 213: tag mismatch
    
return KV_INVALID_HANDLE// No warning
  
}

  return 
hFile;

instead of INVALID_HANDLE use null, also iirc you dont have to set the handle to null/invalid after delete. I believe delete does..

PHP Code:
if(handle)
{
CloseHandle(handle)
handle null

Dr!fter is offline
Silvers
SourceMod Plugin Approver
Join Date: Aug 2010
Location: SpaceX
Old 04-25-2018 , 11:23   Re: SourceMod INVALID_HANDLE - new syntax bug?
Reply With Quote #3

Quote:
Originally Posted by Dr!fter View Post
instead of INVALID_HANDLE use null, also iirc you dont have to set the handle to null/invalid after delete. I believe delete does..

PHP Code:
if(handle)
{
CloseHandle(handle)
handle null

Didn't realise about delete or null. Edit: tested and delete and it sets to null also. And replacing KV_/INVALID_HANDLE with null works.
(The code is converted from old projects where CloseHandle() was used and setting the global var to INVALID was required.)

Any good documentation or wiki about the new syntax and methods, etc?

Thanks Dr!fter
__________________

Last edited by Silvers; 04-25-2018 at 11:31.
Silvers is offline
Dr!fter
The Salt Boss
Join Date: Mar 2007
Old 04-25-2018 , 12:38   Re: SourceMod INVALID_HANDLE - new syntax bug?
Reply With Quote #4

Quote:
Originally Posted by Silvers View Post
Didn't realise about delete or null. Edit: tested and delete and it sets to null also. And replacing KV_/INVALID_HANDLE with null works.
(The code is converted from old projects where CloseHandle() was used and setting the global var to INVALID was required.)

Any good documentation or wiki about the new syntax and methods, etc?

Thanks Dr!fter
https://wiki.alliedmods.net/SourcePa...itional_Syntax
Dr!fter is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 13:00.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode