FreeOrion

Forums for the FreeOrion project
It is currently Fri May 24, 2013 2:18 pm

All times are UTC




Post new topic Reply to topic  [ 54 posts ]  Go to page Previous  1, 2, 3, 4
Author Message
 Post subject: Re: AI empires don't do nothing
PostPosted: Tue Jan 31, 2012 10:55 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7898
Location: Vancouver, BC
bastion wrote:
@Geoff the Medio: I won't have time to try reinstalls (let alone compilations or overcoming my aversion of python) until this weekend, will report back when I do.
First thing to do is add the import sys at the top, and the print(sys.path) in initFreeOrionAI(). No recompiles needed.
Geoff the Medio wrote:
So maybe the sys.path should be printed just before importing AI?
If bastion can get the AI scripts to run by moving them, there's no need to add new C++ code to call or execute a Python function to do that; the AI script initialization can do so itself.


Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Thu Feb 02, 2012 3:52 pm 
Offline
Programmer and Packager
User avatar

Joined: Wed Nov 16, 2011 12:56 pm
Posts: 766
Location: Sol III
Geoff the Medio wrote:
...Since this is Python scripting, we don't need a new binary to output the contents of sys.path. Having bastion replace his FreeOrionAI.py with one that has a print(sys.path) line in initFreeOrionAI() and has import sys at the top should work.
You're right again of course. This actually makes things much easier :)
Quote:
I get in the AI log file:
Code:
2012-01-31 02:50:52,859 DEBUG AI : ['C:\\FreeOrion_VS2010_SDK\\FreeOrion\\python27.zip', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs', 'C:\\Python27\\Lib\\lib-tk', 'C:\\FreeOrion_VS2010_SDK\\FreeOrion', 'C:\\FreeOrion_VS2010_SDK\\FreeOrion\\default\\AI']
May I suggest a slight improvement for that? Because, especially if you have long pathnames, the output in the logfile is a bit of a pain if you want to check which directories are included. So I made some small modifications to use pprint instead of print, and the result is definitely easier on the eyes ;)

Patch for FreeOrionAI.py attached.


Attachments:
FreeOrionAI_py.patch [729 Bytes]
Downloaded 5 times
Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Thu Feb 02, 2012 4:00 pm 
Offline
Programmer and Packager
User avatar

Joined: Wed Nov 16, 2011 12:56 pm
Posts: 766
Location: Sol III
bastion wrote:
...@Geoff the Medio: I won't have time to try reinstalls (let alone compilations or overcoming my aversion of python) until this weekend, will report back when I do.
No compilations needed, as Geoff already pointed out, just some small additions to FreeOrionAI.py. To make things easier for you, I've attached a zip file containing a patched FreeOrionAI.py. Replace your FreeOrionAI.py with this patched one (make a backup of yours first), run a test game and post the logs. This is probably the quickest and simplest thing you can try before doing something more elaborate.


Attachments:
FreeOrionAI.py.zip [2.21 KiB]
Downloaded 6 times
Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Thu Feb 02, 2012 7:57 pm 
Offline
Space Krill

Joined: Mon Jan 02, 2012 2:27 pm
Posts: 11
Quick test with Vezzra's patch:
Code:
D:\\Games\\FreeOrion\x00_4-RC2\\default\\AI
Seems like it's getting interpreted somewhere without properly escaping the backslash, causing \0 to turn into a null character.
In that case all folders starting with characters from here http://docs.python.org/reference/lexical_analysis.html#strings (most notably \f = ASCII Formfeed (FF)) might cause problems.


Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Thu Feb 02, 2012 10:37 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7898
Location: Vancouver, BC
bastion wrote:
Seems like it's getting interpreted somewhere without properly escaping the backslash, causing \0 to turn into a null character.
Ugh. Well, I'm not going to try to fix that before a release. So as above, I suggest installing into a more-regular path like D:\Games\FreeOrion.


Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Fri Feb 03, 2012 4:55 pm 
Offline
Programmer and Packager
User avatar

Joined: Wed Nov 16, 2011 12:56 pm
Posts: 766
Location: Sol III
Geoff the Medio wrote:
bastion wrote:
Seems like it's getting interpreted somewhere without properly escaping the backslash, causing \0 to turn into a null character.
Ugh.
Yes, that was my first thought too. Fortunately it isn't as bad as it looks ;)
Quote:
Well, I'm not going to try to fix that before a release. So as above, I suggest installing into a more-regular path like D:\Games\FreeOrion.
After looking into the matter I tend to disagree here: This bug is quite serious, I wonder why there haven't been more people running into this problem on windows. To illustrate this let's take your suggestion for a more-regular path above: "D:\Games\FreeOrion". That one would work. But let's assume there is some guy who decides to use this path instead (the difference is really minimal!): "D:\Games\freeorion". This would turn out as "D:\\Games\x0creeorion" - uh-oh. The cause for this is the way string literals are handled in python, bastion already provided the link to the corresponding section in the python documentation in his post above (I won't quote the entire section here). As you can see there, there are quite some characters affected by this escape sequence parsing of string literals...

I don't think you want an unresolved issue of that kind in 0.4, do you? ;)

Fortunately, going through this part of the python doc also gave me clues how to solve the problem. The code section in question is this one from PythonAI.cpp:
Code:
        // tell Python the path in which to locate AI script file
        std::string AI_path = (GetResourceDir() / "AI").string();
        std::string path_command = "sys.path.append('" + AI_path + "')";
        object ignored = exec(path_command.c_str(), s_main_namespace, s_main_namespace);
The path to the AI modules is inserted as a string literal into the command string passed to exec, ergo being treated as such by the interpreter. This isn't a problem on platforms that use "/" as the path delimiter, but becomes a big one on windows, where "\" is the path delimiter. However, python allows you to prefix string literals with "r", which causes the interpreter to skip the escape sequence parsing for the most part (details see link above). So putting a "r" before the string literal constructed with AI_path should solve the problem:
Code:
        std::string path_command = "sys.path.append(r'" + AI_path + "')";
As I'm not able to build FO on the windows PCs at my disposal, I can't test my theory. However, I've been able to simulate the issue in the python shell on my old windows laptop, and the results look very promising. As the required change is a very small one, I think it's worth trying :D And as this change shouldn't affect the functionality of this piece of python code on other platforms, it shouldn't even be required to split the section into platform specific code parts.

This is going to be the smallest patch to any piece of code I've come up and will come up with for the rest of my life. I mean, how I'm going to go below one character...? :mrgreen:


Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Sat Feb 04, 2012 4:03 am 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7898
Location: Vancouver, BC
Vezzra wrote:
I don't think you want an unresolved issue of that kind in 0.4, do you? ;)
Given how delayed v0.4 is, and how easily user-fixable and relatively rare this issue is, I'd probably have be ok with leaving it in...


Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Sat Feb 04, 2012 3:24 pm 
Offline
Space Krill

Joined: Mon Jan 02, 2012 2:27 pm
Posts: 11
I've upgraded to RC3 (includes removing RC2 and config.xml) and did some tests:

Installed to D:\Games\FreeOrion, AI works ootb. (boring :wink: )
Changing \default to \fdefault (both the folder and in config.xml) causes the AI (and only the AI) to not be found. (failure as expected :| )
Changing \default to \x64efault (\x64 = d) and copying the AI folder to D:\Games\FreeOriondefault causes the AI (and only the AI) to be loaded from there. (success :mrgreen: )


Top
 Profile  
 
 Post subject: Re: AI empires don't do nothing
PostPosted: Mon Feb 06, 2012 8:54 am 
Offline
Programmer and Packager
User avatar

Joined: Wed Nov 16, 2011 12:56 pm
Posts: 766
Location: Sol III
bastion wrote:
I've upgraded to RC3 (includes removing RC2 and config.xml) and did some test...
Issues should be solved with RC4.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 54 posts ]  Go to page Previous  1, 2, 3, 4

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group