Author Topic: StrToInt (and StrToFloat) - WTF  (Read 6650 times)

JovankaB

  • Guest
StrToInt (and StrToFloat) - WTF
« on: August 03, 2014, 11:01:33 am »
OK I must be just tired and not see some obvious thing. Maybe someone else will.

Here is the file:
https://dl.dropboxusercontent.com/u/3880395/wtf.fos

It's very simple:
Code: [Select]
void wtf()
{
int i = 0;
StrToInt("666", i);
Log("" + i);
}

compile it with asscompiler:
Code: [Select]
ascompiler wtf.fos -run wtf
Log outputs 0. Why i doesn't become 666?
« Last Edit: August 03, 2014, 11:03:09 am by JovankaB »

Offline Berko

  • Tim Tom Ted
    • http://kabefis.deviantart.com/
Re: StrToInt (and StrToFloat) - WTF
« Reply #1 on: August 03, 2014, 02:02:56 pm »
Looks like we can't do everything with ASCompiler.
I test it with some old ascompiler with the same result, it's not a regression or something like that. And in game all works good.

To try to understand I tested another function from string :
"int   toInt( int defaultValue ) const"
Works good with ascompiler directly.

StrToInt is a binded function :
"bool StrToInt(string@+ text, int& result)" is binded to FOServer::SScriptFunc::Global_StrToInt

Where string.toInt isn't :
"ScriptString::toInt" -> "StriptString" is directly added on script engine as "string" object


Maybe that's the difference? All binded elements don't throw errors on compilation but don't necessary do what they are supposed to do?
You can try some other binded function like "Random" or "GetTick", they return me totally wrong value too.

So is it normal or not?

In fonline help we have :
Quote
compiler scripts
The program is designed to validate the written scripts.
Name of the executable file: "ASCompiler.exe".
It can be used from the command line or integrated into a variety of IDE (integration examples are described in the folder with the compiler).
Command line: <filename> <library AngelScript> <library gaming API> [-p <filename, where there will be code after preprocessing>] [-d <defined symbols>]
Example: C:\client_main.fos as.dll fo_client.dll -p prep.txt -d __CLIENT

So it look like that ASCompiler only test if code can compile, and all binded function isn't really working when code is evaluated.
~~~ Ashes of Phoenix project --> http://fonline-aop.net/ ~~~

JovankaB

  • Guest
Re: StrToInt (and StrToFloat) - WTF
« Reply #2 on: August 03, 2014, 02:54:07 pm »
So that's because StrToFloat is part of FOnline, not AngelScript?
I guess I will write my own StrToFloat then, it shouldn't be too difficult.

Offline Wipe

  • Rotator
  • Random is god
Re: StrToInt (and StrToFloat) - WTF
« Reply #3 on: August 03, 2014, 04:34:52 pm »
Maybe that's the difference? All binded elements don't throw errors on compilation but don't necessary do what they are supposed to do?
Basically this.

Each FOnline function have its copy just for the ASCompiler; all of these functions are empty, so they have no chance to work there. They just have to pass AngelScript compiler, and nothing more. Oh, why Log() works then? If program is started with "-run" argument, ASCompiler registers __CompilerLog(string&) function (which is nothing else than single printf() call) and masks original function by adding "#define Log __CompilerLog" to preprocessor before executing your script.

Then, as Berko said, we have some types and functions which are provided either by standard AS addons (string, array<T>, findFirst(), strlwr(), etc) or written like one of them (reflection namespace) and should work with any application using AngelScript. And, of course, during 'ASCompiler -run'

If that helps, current naming convention is:
FunctionName - FOnline function, dummy in ASCompiler (not counting Log())
functionName - generic function, should work on any target

I guess I will write my own StrToFloat then, it shouldn't be too difficult.
There was something about floats in 2238 dlls (can't check now, so only 95% sure)
Games are meant to be created, not played...

JovankaB

  • Guest
Re: StrToInt (and StrToFloat) - WTF
« Reply #4 on: August 03, 2014, 05:00:58 pm »
I already wrote my own version - StrToDouble:

Code: [Select]
double StrToDouble(string@ str)
{
int length = str.length();
int position = str.length() - 1;
int count = 0;
int symbol = 0;
int value = 0;
int exponent = 0;

while (position >= 0)
{
symbol = str[position]; // Comment for FOnline revision >= 400
//symbol = str.rawGet(position); // Uncomment for FOnline revision >= 400

switch (symbol)
{
case 43:  // + sign
{
break;
}
case 45:  // - sign
{
value = -value;
break;
}
case 46:  // . decimal point
{
exponent += -count;
break;
}
case 48:  // 0
case 49:  // 1
case 50:  // 2
case 51:  // 3
case 52:  // 4
case 53:  // 5
case 54:  // 6
case 55:  // 7
case 56:  // 8
case 57:  // 9
{
value += (symbol - 48) * pow(10, count);
count += 1;
break;
}
case 69:  // E
case 101: // e
{
exponent = value;
value = 0;
count = 0;
break;
}
default:
{
return 0;
}
}
position -= 1;
}
return double(value) * double(pow(10, exponent));
}

There is no checking if the string is a valid decimal number, except that if an unknown character is met, 0 is returned. So if you have weird stuff in the string, but made from characters that could be in a decimal number like "2.-..2", you will get some weird number (in this case 0.018).  But other than that, it seems to work fine. For my stuff I validate the numberish string in another place so this function is enough.
« Last Edit: August 03, 2014, 05:25:46 pm by JovankaB »

Offline Berko

  • Tim Tom Ted
    • http://kabefis.deviantart.com/
Re: StrToInt (and StrToFloat) - WTF
« Reply #5 on: August 03, 2014, 07:02:56 pm »
You can also do something like that :
Code: [Select]
void wtf2()
{
Log("Float : " + ("123.123".toFloat(0.0) + 542.877));
}
To output "Float : 666"
But maybe you can't/don't-want-to use these string method ? (as it's a recent feature rev428)
You are working on a tool? or just doing some test? just by curiosity :p
~~~ Ashes of Phoenix project --> http://fonline-aop.net/ ~~~

JovankaB

  • Guest
Re: StrToInt (and StrToFloat) - WTF
« Reply #6 on: August 03, 2014, 07:36:02 pm »
I'm writing a JSON parser in AngelScript. I didn't know string.toFloat existed, that's why I didn't use it.

Also, I want it to be back compatible with pre-rev400 (for compatibility with FOnline:2238) and that
means I can't use it at least  in "2238-compatible" mode. Now that I know about it, I will most likely
make some defines for pre-rev400 and post-rev399 and use toFloat when possible.

On the other hand I store JSON Numbers as doubles not floats, so maybe I will stick to my function.
I'm not sure if it makes any difference.

The parser is mostly done but it doesn't compile with rev400 and later yet. Apparently there is a lot
of stuff that changed. I should have start testing it with the latest asscompiler a bit earlier, because
now I have tons of errors to fix.

« Last Edit: August 03, 2014, 07:59:02 pm by JovankaB »

Offline Berko

  • Tim Tom Ted
    • http://kabefis.deviantart.com/
Re: StrToInt (and StrToFloat) - WTF
« Reply #7 on: August 13, 2014, 11:28:24 pm »
For information, looks like it has changed since the last revision (452, thanks Cvet)

Code: [Select]
Update script compiler (ASCompiler).
When using "-run" now in addition to the function
  void Log (string & text)
will work with the following functions
  int Random(int min, int max)
  bool StrToInt(string@+ text, int& result)
  bool StrToFloat(string@+ text, float& result)
  uint GetDistantion(uint16 hexX1, uint16 hexY1, uint16 hexX2, uint16 hexY2)
  uint8 GetDirection(uint16 fromHexX, uint16 fromHexY, uint16 toHexX, uint16 toHexY)
  uint8 GetOffsetDir(uint16 fromHexX, uint16 fromHexY, uint16 toHexX, uint16 toHexY, float offset)
  uint GetTick()
  uint GetAngelScriptProperty(int property)
  void SetAngelScriptProperty(int property, uint value)
  uint GetStrHash(string@+ str)
  uint DecodeUTF8(const string& text, uint& length)
  string@ EncodeUTF8(uint ucs)
~~~ Ashes of Phoenix project --> http://fonline-aop.net/ ~~~

JovankaB

  • Guest
Re: StrToInt (and StrToFloat) - WTF
« Reply #8 on: August 13, 2014, 11:40:00 pm »
\o/

Re: StrToInt (and StrToFloat) - WTF
« Reply #9 on: July 22, 2018, 01:34:10 pm »
Works at rev 412 and 2238/Reloaded upgraded to 412.