Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 23:13

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Tut]Make your own simple C# Compiler

Discussion on [Tut]Make your own simple C# Compiler within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Nov 2010
Posts: 1,162
Received Thanks: 370
[Tut]Make your own simple C# Compiler

First of all create a new form.
Then we need to set it up simple.

First thing we do is making a panel.
Drag the panel to the form and set the dock to "Top".
Drag a toolstrip into the panel.
Now drag another panel into the form and set the dock to "Right".
And then a last panel into our form. The dock of that should be "Fill".

It should look something like this now:

The next thing we do is dragging a tabcontrol into the panel3.
Delete the 2tabs it have and make the dock "Fill".

Now in the panel2 put a label, a textbox, 2 radiobuttons and a listbox.
The label should have the text "Assembly name".
The first radiobutton should have the text "Class Library" and it should be checked. The second should have the text "Console application".
The listbox should have dock to "Bottom".

Now it should look something like this:

The next thing we do is adding a 2 dropdowns to our toolstrip.
One called "File" and one called "Project".

Set the File displaystyle to "Text".
Make sure ShowDropDownArrow is set to "false".
Following dropdownitems should be added.
Code:
New File
Open File
Save File
Set the Project displaystyle to "Text".
Make sure Make sure ShowDropDownArrow is set to "false".
Following dropdownitems should be added.
Code:
Compile And Run
Compile Only
Add Reference
It should all look like this now:

Now the base GUI is done and we will start on the coding.
First thing we do is adding the "New File".
Double click on it and you should get this code:
Code:
        private void newFileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
Replace it with:
Code:
        private void newFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage page = new TabPage();
            page.Name = "New File";
            page.Text = "Unnamed.cs";

            RichTextBox rtb = new RichTextBox();
            rtb.Name = "richTextBox1";
            rtb.Size = new System.Drawing.Size(100, 96);
            rtb.TabIndex = 5;
            rtb.Dock = DockStyle.Fill;
            rtb.BorderStyle = BorderStyle.None;
            rtb.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            rtb.Text = @"using System;

namespace Application
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(""Hello World"");
        }
    }
}";
            page.Controls.Add(rtb);
            tabControl1.Controls.Add(page);
        }
Now we can create new files.
Now click on the Open File.
You should get this code:
Code:
        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
Replace it with:
Code:
        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog opn = new OpenFileDialog();
            opn.Title = "Open File";
            opn.Filter = "CSharp Coding Files|*.cs";
            opn.Multiselect = false;

            if (opn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string Name = System.IO.Path.GetFileName(opn.FileName);
                TabPage page = new TabPage();
                page.Name = "New File";
                page.Text = Name;

                RichTextBox rtb = new RichTextBox();
                rtb.Name = opn.FileName;
                rtb.Size = new System.Drawing.Size(100, 96);
                rtb.TabIndex = 5;
                rtb.Dock = DockStyle.Fill;
                rtb.BorderStyle = BorderStyle.None;
                rtb.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

                rtb.Text = System.IO.File.ReadAllText(opn.FileName);
                page.Controls.Add(rtb);
                tabControl1.Controls.Add(page);
            }
        }
Now click the Save File and you should get this code:
Code:
        private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }
Replace it with:
Code:
  private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Title = "Save File";
            save.Filter = "CSharp Coding Files|*.cs";

            if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (save.CheckFileExists)
                {
                    DialogResult dlg = MessageBox.Show("Are you sure you want to overwrite this file?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (dlg == System.Windows.Forms.DialogResult.No || dlg == System.Windows.Forms.DialogResult.Cancel)
                        return;
                }

                FileStream fs;
                try
                {
                    fs = new FileStream(save.FileName, FileMode.Truncate);
                }
                catch
                {
                    fs = new FileStream(save.FileName, FileMode.CreateNew);
                }
                StreamWriter sw = new StreamWriter(fs);
                foreach (Control ctrl in tabControl1.Controls)
                    if (ctrl is TabPage)
                    {
                        foreach (Control Ctrl in ctrl.Controls)
                            if (Ctrl is RichTextBox)
                            {
                                sw.WriteLine(Ctrl.Text);
                            }
                    }
                sw.Close();
                fs.Close();
                tabControl1.SelectedTab.Text = System.IO.Path.GetFileName(save.FileName);
            }
        }
Now go to the dropdown Project and click on Add Reference.
You should get this code:
Code:
        private void addReferenceToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
Replace it with:
Code:
        private void addReferenceToolStripMenuItem_Click(object sender, EventArgs e)
        {
                OpenFileDialog opn = new OpenFileDialog();
                opn.Title = "Add Reference";
                opn.Filter = "Libraries|*.dll";
                opn.Multiselect = false;
                if (opn.ShowDialog() == DialogResult.OK)
                {
                    if (!Directory.Exists(Environment.CurrentDirectory + @"\References"))
                    {
                        DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory + @"\References");
                        dir.Create();
                    }
                    string[] FILE = opn.FileName.Split(new char[] { '\\' });
                    File.Copy(opn.FileName, Environment.CurrentDirectory + @"\References\" + FILE[FILE.Length - 1], true);
                    listBox1.Items.Add(FILE[FILE.Length - 1]);
                }
        }
Now make a new class and call it Compiler.
Replace it all with this:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

namespace CS_Compiler
{
    public class Compiler
    {
        public Compiler(string ApplicationName, bool EXE, string Code, string[] References, bool run)
        {
            ApplicationName = ApplicationName.Split('.')[0];//To make sure we won't use a wrong extension.
            if (EXE)
                ApplicationName += ".exe";
            else
                ApplicationName += ".dll";

            CSharpCodeProvider codeProvider = new CSharpCodeProvider();
            ICodeCompiler icc = codeProvider.CreateCompiler();
            System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = EXE;
            parameters.OutputAssembly = ApplicationName;
            if (References != null)
            {
                for (int i = 0; i < References.Length; i++)
                {
                    parameters.ReferencedAssemblies.Add(References[i]);
                }
            }
            CompilerResults results = icc.CompileAssemblyFromSource(parameters, Code);
            //Use results.Errors to get all the errors compiling.

            if (run && EXE)
                System.Diagnostics.Process.Start(ApplicationName);
        }
    }
}
If you want to get all the errors throwing at the compilation, then use results.Errors. You can make a textbox at the bottom of your IDE which can show the errors. The same goes for output.

Now we just need to make the compilation done.
Click on the Compile And Run.
You should get this code:
Code:
        private void compileAndRunToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
Replace it with:
Code:
        private void compileAndRunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool exe = false;
            bool run = true;

            if (radioButton2.Checked)
                exe = true;
            else
            {
                run = false;
                exe = false;
            }

            string[] References = new string[listBox1.Items.Count];
            for (int i = 0; i < References.Length; i++)
                References[i] = @"References\" + listBox1.Items[i].ToString();

            if (!Directory.Exists(Environment.CurrentDirectory + @"\Builds"))
            {
                DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory + @"\Builds");
                dir.Create();
            }

            string Code = "";
            foreach (Control ctrl in tabControl1.Controls)
                if (ctrl is TabPage)
                {
                    foreach (Control Ctrl in ctrl.Controls)
                        if (Ctrl is RichTextBox)
                        {
                            Code = Ctrl.Text;
                        }
                }

            Compiler compiler = new Compiler(@"Builds\" + textBox1.Text, exe, Code, References, run);
        }
Now click on the Compile Only and you should get this code:
Code:
        private void compileOnlyToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
Replace it with:
Code:
        private void compileOnlyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool exe = false;

            if (radioButton2.Checked)
                exe = true;
            else
                exe = false;

            string[] References = new string[listBox1.Items.Count];
            for (int i = 0; i < References.Length; i++)
                References[i] = @"References\" + listBox1.Items[i].ToString();

            if (!Directory.Exists(Environment.CurrentDirectory + @"\Builds"))
            {
                DirectoryInfo dir = new DirectoryInfo(Environment.CurrentDirectory + @"\Builds");
                dir.Create();
            }

            string Code = "";
            foreach (Control ctrl in tabControl1.Controls)
                if (ctrl is TabPage)
                {
                    foreach (Control Ctrl in ctrl.Controls)
                        if (Ctrl is RichTextBox)
                        {
                            Code = Ctrl.Text;
                        }
                }

            Compiler compiler = new Compiler(@"Builds\" + textBox1.Text, exe, Code, References, false);
        }
Now you have your own simple C# Compiler.
I hope you liked it and find it useful.
Syst3m_W1z4rd is offline  
Old 05/04/2011, 20:17   #2
 
w00tare's Avatar
 
elite*gold: 0
Join Date: May 2008
Posts: 248
Received Thanks: 279
Nice, did you make this tutorial?
w00tare is offline  
Old 05/04/2011, 21:25   #3


 
KraHen's Avatar
 
elite*gold: 0
Join Date: Jul 2006
Posts: 2,216
Received Thanks: 793
You`re not creating a compiler, you`re just using the Microsoft.CSharp.CSharpCodeprovider class to compile your program. The two things are waaay different.

Though I can see how this can be useful for those who don`t know about it.
KraHen is offline  
Old 05/04/2011, 22:13   #4
 
elite*gold: 0
Join Date: Nov 2010
Posts: 1,162
Received Thanks: 370
Quote:
Originally Posted by w00tare View Post
Nice, did you make this tutorial?
No I ripped. Seriously, what do you think?

Quote:
Originally Posted by KraHen View Post
You`re not creating a compiler, you`re just using the Microsoft.CSharp.CSharpCodeprovider class to compile your program. The two things are waaay different.

Though I can see how this can be useful for those who don`t know about it.
That's true. It's more an IDE, but it's still compiling the code. If it should be a real compiler, then you would be using only the CLI.

Syst3m_W1z4rd is offline  
Old 09/21/2012, 19:59   #5
 
elite*gold: 0
Join Date: Sep 2012
Posts: 1
Received Thanks: 0
Give me please the complete code(I have problems and errors!)
ruslan2012 is offline  
Old 09/22/2012, 00:23   #6
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
We're talking about an ancient bump to my thread, but uhmmm I don't have it. What is your error?
I don't have a username is offline  
Reply


Similar Threads Similar Threads
How to make simple dc-er (disconnect-er)
01/03/2011 - CO2 Programming - 1 Replies
I need help (how to for dummies) on making a simple program that will allow the client to disconnect when a blue-flashing/red/black named person appears in a certain area. I'm sick and tired of being pked in mining zones.
How to make your own V4 Compiler?
01/03/2011 - Grand Chase - 7 Replies
is this possible ? if so , what are the things we should do in order to create one ..
How to make your own Multiclient [Simple]
08/04/2010 - CO2 Guides & Templates - 4 Replies
First of all what your going to need, to get a hex editor, this file is not mine so i cannot guarantee there is no virus or not i do not know. HxD Hex Editor - Free software downloads and software reviews - CNET Download.com This is from download.com, it is one Hex editor file. Honestly tho you can just go to Google and just find any free hex editor. What you need to do next is get your Conquer.exe file, my suggestion is that people always make a back up file for this before they...
Need Help To Make Simple Bot With : Autoit :
05/21/2010 - General Coding - 9 Replies
Hey Every One.. I Just want Know How To Make Simple Bot With Autoit.. All I need To Know :- 1.How To make The Bot Run On Game Only Mean Work On Background 2.How To Insert HotKeys Like Ctrl+A - Insert - Shit - F1...F10 3.How To Control The Time...? 4.How To Make mouse Click left Or Right Click Into Cord (x,y).. All Of those Wanna Done Into Background... Any Way Thx For Reading My Request N For Help



All times are GMT +2. The time now is 23:13.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.