Let's say some programmer hands me a compiled program, without source code, that does some stuff, but has no graphical representation of what it does. It does however provide the ability to easily add that. But how will the programmer let people interact with things in a compiled program?
This demo program simply makes a random number, with a maximum, and then fires an event that is supposed to expose what happened to people who want to use this data in their own mods for the program.
public delegate void MadeRandomEventHandler(int previous,int current,int max);
class MakeRandom
{
private static int current = 350;
private static int max = 350;
public static event MadeRandomEventHandler MadeRandomEvent;
public static void dowork()
{
int curCopy = current;
Random random = new Random();
current = random.Next(0, max+1);
fireMadeRandomEvent(curCopy,current,max);
}
private static void fireMadeRandomEvent(int previous, int current, int max)
{
if (MadeRandomEvent != null)
MadeRandomEvent(previous,current,max);
}
}
And of course a Form with a button to call dowork()
public partial class Form_Main : Form
{
public Form_Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MakeRandom.dowork();
}
}
So now all I would have to do to create a mod that shows me what happened, is create a new form with a progressbar and handle the event the MakeRandom class fires.
The form:
public partial class Form_Mod : Form
{
public static Form_Mod frm;
public Form_Mod()
{
InitializeComponent();
}
private void Form_Mod_Load(object sender, EventArgs e)
{
frm = this;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}
public void updateProgress(int percent)
{
progressBar1.Value = percent;
}
}
And the event handling:
class Mod_HandleEvents
{
public static void attach()
{
MakeRandom.MadeRandomEvent += new MadeRandomEventHandler(RandomChange);
}
public static void detach()
{
MakeRandom.MadeRandomEvent -= new MadeRandomEventHandler(RandomChange);
}
private static void RandomChange(int previous, int current, int max)
{
float percent1 = (float)current / (float)max;
int percent = (int)(percent1 * 100);
Form_Mod frm = Form_Mod.frm;
frm.updateProgress(percent);
}
}
But how do I add this code the the program I was provided? I do not have the source code, so I cannot add it & then recompile. I have seen several programs that seem to compile .cs files on the fly, and those .cs files seem to interract with the main program. How do I do this?
And I would probably have to add another button to the main form to actually be able to open my "Form_Mod" form. So the programmer who handed me the main program would have to provide a way to do that, right? Or is there another way?
private void button2_Click(object sender, EventArgs e)
{
Form_Mod frm = new Form_Mod();
Mod_HandleEvents.attach();
frm.Show();
}