PDA

View Full Version : How to access code from various submissions from each other, using a dll



TWC_Flug
Oct-25-2018, 11:16
Many of us want to modularize our mission code used in CloD. One way to do that is to have various submissions that include code to handle various tasks. For example, we have submissions to handle stats, aircraft supply, various radar functions, handing AI aircraft movement, etc.

The problem is, the code from each of those submissions is a silo--you can't access any variables, methods, data, etc from submission #1 if you are in submission #2 code.

So, there is a pretty simple way to solve this using a compiled .dll that you put into parts/core.

Here is a thread on 1cpublishing that explains how, using a singleton class to exchange certain variables:

http://forum.1cpublishing.eu/showthread.php?t=32329

I took it a step further and had various submissions exchange their Mission class instance in the hopes I would be able to just access (for example) StatsMission.MyMethod() from MainMission without further ado.

It's not quite that easy as you have to define an interface for each Mission class in the .dll, then include that .dll as a //$reference in each submission.cs file, then implement that interface in your various submission Mission classes. Then you can access any methods, variables, etc defined in the interface in any of the submission files. (The submission .cs files are compiled into separate dlls, so without the interfaces defined in the dll--which is then loaded by each submission.cs--they have no idea about each other's publicly available methods or variables.)

So it's a bit complex to set up but pretty slick in the end.

The dll code we're currently using is here:

https://github.com/bhugh/TWC_Mission_Server/blob/master/CloDMissionCommunicator/CloDMissionCommunicator/CloDMissionCommunicator.cs

Using that code, here is how (for example) the Main and Stats submissions can communicate:

In the -main.cs initializer method:



...
//$reference parts/core/CloDMissionCommunicator.dll
...
public class Mission : AMission, IMainMission //Mission implements the interface defined in the dll.
{ ...

Public Mission() {
...
TWCComms.Communicator.Instance.Main = (IMainMission)this; //allows -stats.cs and other submissions to access this instance of Mission
...


public override void OnMissionLoaded(int missionNumber)
{
....
TWCStatsMission = TWCComms.Communicator.Instance.Stats;

...
}

}
Then in the -stats.cs


...
//$reference parts/core/CloDMissionCommunicator.dll
...
public class Mission : AMission, IMainMission //Mission implements the interface defined in the dll.
{ ...

Public Mission() {
...
TWCComms.Communicator.Instance.Stats = (IStatsMission)this; //Gives this instance to the communicator so -main.cs and other submissions can access it
TWCMainMission = TWCComms.Communicator.Instance.Main; //gets the instance of -main.cs Mission class so that this class can use it
...


public override void OnMissionLoaded(int missionNumber)
{
....

TWCSupplyMission = TWCComms.Communicator.Instance.Supply; //get the instance of another submission .cs Mission class that we will need to access elsewhere
//You can access -main.cs immediately in the class initializer but other submissions might load before or after this one, so you need to load their instances OnMissionLoaded to be sure you get them, if they load later than this submission.
...
}

Then I can access methods & variables from, for example, -stats.cs in -main.cs like this:


if (TWCStatsMission != null) TWCStatsMission.Display_AircraftAvailable_ByName(p layer, nextAC: false, display: true, html: false); //need to check TWCStatsMission != null because the -stats.cs loads somewhat late and their could be times very early in initialization where the variable will still be null

And I can access something from -main.cs in -stats.cs like this:


mission_id = TWCMainMission.MISSION_ID; //don't need to check TWCMainMission != null here because main mission always loads before the subsmission, so if TWCMainMission isn't available something is very seriously wrong here

So, that is a lot of code to do something pretty simple, but it does work.

ATAG_Torian
Oct-26-2018, 06:52
I admit that all of the above might as well be in Mandarin for me but I am soooo glad there are folk like you who put in all the effort to make these things happen.

Nephilim
Jan-10-2019, 17:07
Back in the days when I was more active solution for those submission problem was recompiled Strategy.dll, of course with a bit of help of MySql but it saved a lot of code.. and time.