[C#] Converting pawn function to c# function
#1

Hey!

first sorry if i posted in wrrong section! since i didn't found a section for such things.

im having some issues with converting this function to c#

PHP код:
LoadStaticVehiclesFromFile(const filename[])
{
    new 
File:file_ptr;
    new 
line[256];
    new 
var_from_line[64];
    new 
vehicletype;
    new 
Float:SpawnX;
    new 
Float:SpawnY;
    new 
Float:SpawnZ;
    new 
Float:SpawnRot;
    new 
Color1Color2;
    new 
index;
    new 
vehicles_loaded;
    
file_ptr fopen(filename,filemode:io_read);
    if(!
file_ptr) return 0;
    
vehicles_loaded 0;
    while(
fread(file_ptr,line,256) > 0)
    {
      
index 0;
      
// Read type
         
index token_by_delim(line,var_from_line,',',index);
         if(
index == (-1)) continue;
         
vehicletype strval(var_from_line);
          if(
vehicletype 400 || vehicletype 611) continue;
         
// Read X, Y, Z, Rotation
         
index token_by_delim(line,var_from_line,',',index+1);
         if(
index == (-1)) continue;
         
SpawnX floatstr(var_from_line);
         
index token_by_delim(line,var_from_line,',',index+1);
         if(
index == (-1)) continue;
         
SpawnY floatstr(var_from_line);
         
index token_by_delim(line,var_from_line,',',index+1);
         if(
index == (-1)) continue;
         
SpawnZ floatstr(var_from_line);
         
index token_by_delim(line,var_from_line,',',index+1);
         if(
index == (-1)) continue;
         
SpawnRot floatstr(var_from_line);
         
// Read Color1, Color2
         
index token_by_delim(line,var_from_line,',',index+1);
         if(
index == (-1)) continue;
         
Color1 strval(var_from_line);
         
index token_by_delim(line,var_from_line,';',index+1);
         
Color2 strval(var_from_line);
         
//printf("%d|%f|%f|%f|%f|%d|%d",vehicletype,
           //SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2);
        
AddStaticVehicleEx(vehicletype,SpawnX,SpawnY,SpawnZ,SpawnRot,Color1,Color2,-1);
        
vehicles_loaded++;
    }
    
fclose(file_ptr);
    
printf("Loaded %d vehicles from: %s",vehicles_loaded,filename);
    return 
vehicles_loaded;
}
// Tokenise by a delimiter
// Return string and index of the end determined by the
// provided delimiter in delim
token_by_delim(const string[], return_str[], delimstart_index)
{
    new 
x=0;
    while(
string[start_index] != EOS && string[start_index] != delim) {
      
return_str[x] = string[start_index];
      
x++;
      
start_index++;
    }
    
return_str[x] = EOS;
    if(
string[start_index] == EOSstart_index = (-1);
    return 
start_index;

im planing to use it for loading weapons data from file. like this

PHP код:
1    BRASSKNUCKLE    0    MELEE            331    brassknuckle 
and ready every params onebyone,

Thank you!
Reply
#2

Why do you need it in C#?

In regards to your function, look up SSCANF.
Reply
#3

Quote:
Originally Posted by Paulice
Посмотреть сообщение
Why do you need it in C#?
Thanks!

but i did find a way more easier!

just by using Split

PHP код:
string[] str GetLine(@"weapons\info.config"id).Split(' ');
str[0] = 0
str
[1] = fist
.... 
Reply
#4

Quote:
Originally Posted by XeonMaster
Посмотреть сообщение
Thanks!

but i did find a way more easier!

just by using Split

PHP код:
string[] str GetLine(@"weapons\info.config"id).Split(' ');
str[0] = 0
str
[1] = fist
.... 
https://stackoverflow.com/questions/...anf-in-c-sharp
Reply
#5

Quote:
Originally Posted by Paulice
Посмотреть сообщение
sscanf needs regex. and i don't want to make it so complicated (laggy) also in the most voted post there he used same method as me!

Thanks anyways!
Reply
#6

Quote:
Originally Posted by XeonMaster
Посмотреть сообщение
sscanf needs regex. and i don't want to make it so complicated (laggy) also in the most voted post there he used same method as me!

Thanks anyways!
It is an independent plugin, no regex plugin needed.

SSCANF is a million times (exaggerating a bit, but it is a lot faster) faster than your token_by_delim function.

-----

The solution is on the page I sent you, it skips duplicate spaces. Just like SSCANF.

P.S. I'm talking about the PAWN version.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)