Post Pacific  

Go Back   Post Pacific > Восстановленный архив > Темы без сохранившегося раздела
FAQ Community Calendar Today's Posts Search

Notices

 
 
Thread Tools Search this Thread Display Modes
Old 16.06.2006, 11:11   #1
Bruenor
Архивный участник
 
Join Date: Jun 2006
Posts: 14
Default [Share] AI Function Table version x.x

The functions that are callable by AI.obj are stored in a function table in L2NPC. This share moves them to a text file in the script folder which is then loaded at server startup (with the aid of a dll that I wrote). Included is a patched L2NPC.exe in addition to the information required to patch it yourself in case yours is already heavily modified.

Installation instructions can be found in Readme.txt.

Since I haven't created any new AI-callable functions yet, this share is currently useless to leechers. :mrgreen:

There are now three ways you can create AI functions. I think option two is by far the simplest. :wink:
In all cases, the function should use the __cdecl calling convention, and the first argument needs to be a pointer to a CNPC object (void* will also work).
1) Write the function in machine language and hex it into the server. (It's not impossible; I've done it myself. :wink: )
2) Make a dll with the function (or edit mine) and import the function into L2NPC.exe using stud_pe. To get the address for the function table entry, convert the RVA (relative virtual address) of the thunk to the virtual address (or just add 0x400000 to the RVA). In the function table entry, make sure you use address_type = a_Thunk.
3) If you prefer dll injection to importing the function using stud_pe, reserve 4 bytes of memory in L2NPC.exe (such as in the .data2 section) to hold the address of the function. During dll initialization, inject the address of your function at that location. The function table entry is the same as in option 2, except that you will use the address of the memory holding the pointer to the function instead of the thunk. Like in option 2, you will need to set address_type to a_Thunk.

When making a function, you will need to stick to the types defined in ai_func_table_pch; otherwise you won't know what to put when adding your function to the function table. It should be possible to add new types, but it may require a significant amount of hexing of L2NPC.exe.

The next step is to add your function to ai_func_table.txt. The format is the same as other script files, so if you have worked with scripts before, it should seem familiar to you.

Sample entry in ai_func_table.txt:
Code:
function_begin                // beginning of entry
name = [AddMoveAroundDesire]  // function name
id = 0                        // function id (the number you use in AI.obj)
address = 0x0042E670          // address of the function
address_type = a_Absolute     // address type (a_Absolute or a_Thunk; the default type is a_Absolute)
return_type = t_void          // return type (defined in ai_func_table_pch.txt)
arg_count = 2                 // number of arguments
arg_type = {t_int;t_float}    // list of the argument types
function_end                  // end of entry
The final step is to call your function from an AI script. Several tutorials for this exist on PostPacific, such as [SHARE][TUTORIAL]AI.OBJ.

Code for a sample function can be found in New Functions.cpp and New Functions.h.


Enjoy.

Вложения из исходного сообщения (сами файлы в копии отсутствуют):
l2npc_with_ai_function_table_185.rar — 384393 байт
Bruenor is offline  
Old 16.06.2006, 12:15   #2
Alien003
Архивный участник
 
Join Date: Jun 2005
Posts: 143
Default

great share.

where are you calling the function inside l2npc?
Alien003 is offline  
Old 16.06.2006, 12:28   #3
Bruenor
Архивный участник
 
Join Date: Jun 2006
Posts: 14
Default

Which function are you referring to?
Bruenor is offline  
Old 16.06.2006, 12:59   #4
Phantom2005
Архивный участник
 
Join Date: Aug 2005
Posts: 394
Default

this is definatly good stuff thanks!
Phantom2005 is offline  
Old 16.06.2006, 13:02   #5
Decaz
Архивный участник
 
Join Date: Nov 2005
Posts: 32
Default

It's great! Thank you!
Decaz is offline  
Old 16.06.2006, 13:16   #6
Alien003
Архивный участник
 
Join Date: Jun 2005
Posts: 143
Default

Quote:
Originally Posted by Bruenor
Which function are you referring to?
the loadAIFuncTable
Alien003 is offline  
Old 16.06.2006, 13:31   #7
Bruenor
Архивный участник
 
Join Date: Jun 2006
Posts: 14
Default

You can find that information in L2NPC Fixes.txt.
Bruenor is offline  
Old 16.06.2006, 13:37   #8
mcrabben7
Архивный участник
 
Join Date: Apr 2006
Posts: 461
Default

Thanks for this share. I have no use for it yet, but it will be interesting to use if I ever try adding new NPC functions. Great work!
mcrabben7 is offline  
Old 16.06.2006, 18:23   #9
Johnson
Архивный участник
 
Join Date: May 2006
Posts: 26
Default

Would there be a way to save to the MS SQL VIA Creating a function?
Johnson is offline  
Old 16.06.2006, 20:09   #10
Bruenor
Архивный участник
 
Join Date: Jun 2006
Posts: 14
Default

If you are asking if a function you create can access the SQL database, then of course it can. The NPC server probably has functions for sending queries (possibly imported from a dll), so just figure out what function to use and call it (with the appropriate parameters, of course).
Bruenor is offline  
Old 16.06.2006, 21:52   #11
Bruenor
Архивный участник
 
Join Date: Jun 2006
Posts: 14
Default

Quote:
Originally Posted by talmage
Would you be willing/able to provide a few examples on how to create new functions using what you have posted? A tutorial of sorts...
As I said, it's not any different from adding any other sort of function. I'm not an expert at that, so I would not be the best person to ask for a tutorial.
Bruenor is offline  
Old 17.06.2006, 01:53   #12
Alien003
Архивный участник
 
Join Date: Jun 2005
Posts: 143
Default

Since the dll's addresses are dinamic we cant set the addresses of new functions in the txt file, well not directly

you could create a map like this

map<string, void (__cdecl *)(...)> mapName;

and a new function

void newfunction(...something);

then in runtime set the map values

mapName["newfunction"] = newfunction;

so when reading the "newfunction" string from the txt we have a correct address for it


this is just a sugestion ..anyone with a better idea, reply
Alien003 is offline  
Old 17.06.2006, 02:31   #13
Bruenor
Архивный участник
 
Join Date: Jun 2006
Posts: 14
Default

I'm working on a method for getting the function table to work with statically imported functions. My plans are for another field that determines the address type (absolute address or thunk). Absolute address is the default and is what all of the functions use right now. With the thunk type, when the function table is loaded, the program will go to the given address to find the address of the thunk, which will then be used in the in-memory copy of the function table.

Some information on statically importing functions:
You can import a function into L2NPC.exe using stud_pe. Since the address can change, programs use thunks (which are essentially tiny functions) to call the imported function. The thunk address shown by stud_pe is actually the address of the memory that is filled in at program startup with the address of the thunk. So, to call an imported function, you would dereference the thunk address and call that.
Bruenor is offline  
Old 18.06.2006, 02:15   #14
Bruenor
Архивный участник
 
Join Date: Jun 2006
Posts: 14
Default

I uploaded the dll-friendly version 1.2. 8) The first post has been updated to reflect the changes.

Release notes:
- Fixed a bug with multi-line comments in which **/ would not be recognized as the end of a comment.
- Except for the sample function being imported into L2NPC.exe, it hasn't been changed at all from my original release. (This means that you don't have to redo any hex fixes in case you chose to go that route.)
Bruenor is offline  
Old 20.06.2006, 00:30   #15
hamidouch
Архивный участник
 
Join Date: Mar 2005
Posts: 475
Default

nice share.
hamidouch is offline  
Old 20.06.2006, 05:43   #16
sab-
Архивный участник
 
Join Date: Jul 2005
Posts: 36
Default

thanks! very good share.
now have a time to take a cup of tea.
sab- is offline  
Old 20.06.2006, 10:50   #17
montejo1974
Архивный участник
 
Join Date: Aug 2005
Posts: 42
Default

mmmm very interesting

I'll study it just when I have some free time.
Thx
montejo1974 is offline  
Old 26.06.2006, 08:52   #18
squaddev
Архивный участник
 
Join Date: May 2006
Posts: 37
Default

Great work!

squaddev is offline  
Old 11.07.2006, 15:35   #19
kepler001
Архивный участник
 
Join Date: Jun 2005
Posts: 549
Default

great work
kepler001 is offline  
 


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. The time now is 20:56.