Click here to Skip to main content
15,176,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ok i have created code to search though a file but i want to add text in a function in a game script file this is the code i have done.

public void insidemain()
{
    // this is for all functions inside of main and there are a few so we start here
    string[] lines = null;
    try
    {

        lines = File.ReadAllLines(elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc");

    }
    catch (Exception ex)
    {
        XtraMessageBox.Show(ex.Message, "ERROR adding values to mapname.csc ERROR CODE 002");
    }

    if (lines != null)
    {


        using (StreamReader elfenlied = new StreamReader((elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc")))
        {
            string elfenliedtopfan5_program = elfenlied.ReadToEnd();
            if (elfenliedtopfan5_program.Contains("#precache( \"client_fx\", SNOW_FX );"))
            {
                //MessageBox.Show("SCAR-H ALREADY ADDED.");
                _MessageResult.Instance.Append("Already Added :  devine calls to : " + elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc" + Environment.NewLine);
            }
            if (!elfenliedtopfan5_program.Contains("#precache( \"client_fx\", SNOW_FX );"))
            {
                elfenlied.Dispose();
                if (File.Exists(elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc"))
                {
                    {


                        string s =
                                   "#define SNOW_FX \"dlc0/factory/fx_snow_player_os_factory\"\r\n"
                                  + "#precache( \"client_fx\", SNOW_FX );\r\n";


                        string file = elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc";
                        List<string> elf = new List<string>(System.IO.File.ReadAllLines(file));
                        int index = elf.FindIndex(item => item.Contains("function main()" + "{"));
                        if (index != -1)
                        {
                            elf.Insert(index + 1, s);//""
                        }
                        System.IO.File.WriteAllLines(file, elf);
                        //richTextBoxEx1.LoadFile(Properties.Settings.Default.mods + "//mod.csv", RichTextBoxStreamType.PlainText);
                        //MessageBox.Show("Added: " + s + " Mods.csv");
                        _MessageResult.Instance.Append("\r\nAdded:  " + s + elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc" + Environment.NewLine);

                    }
                }
            }

        }
    }

}


but the part im having issues with i can not get the text to go inside the brackets i need the text to be placed inside the mainfunction

but i i not sure how to accumplish this.

the file
#using scripts\codescripts\struct;
#using scripts\shared\audio_shared;
#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\exploder_shared;
#using scripts\shared\scene_shared;
#using scripts\shared\util_shared;

#insert scripts\shared\shared.gsh;
#insert scripts\shared\version.gsh;

#using scripts\zm\_load;
#using scripts\zm\_zm_weapons;

//Perks
#using scripts\zm\_zm_pack_a_punch;
#using scripts\zm\_zm_perk_additionalprimaryweapon;
#using scripts\zm\_zm_perk_doubletap2;
#using scripts\zm\_zm_perk_deadshot;
#using scripts\zm\_zm_perk_juggernaut;
#using scripts\zm\_zm_perk_quick_revive;
#using scripts\zm\_zm_perk_sleight_of_hand;
#using scripts\zm\_zm_perk_staminup;

//Powerups
#using scripts\zm\_zm_powerup_double_points;
#using scripts\zm\_zm_powerup_carpenter;
#using scripts\zm\_zm_powerup_fire_sale;
#using scripts\zm\_zm_powerup_free_perk;
#using scripts\zm\_zm_powerup_full_ammo;
#using scripts\zm\_zm_powerup_insta_kill;
#using scripts\zm\_zm_powerup_nuke;

//Traps
#using scripts\zm\_zm_trap_electric;

#using scripts\zm\zm_usermap;



function main()
{
	zm_usermap::main();

	include_weapons();
	
	util::waitforclient( 0 );
}


function include_weapons()
{
	zm_weapons::load_weapon_spec_from_table("gamedata/weapons/zm/zm_levelcommon_weapons.csv", 1);
}


also cant just do } as the user will have functions above the main function so in summary i need to look for the main function find that then find the } starting brace and add the text in there

thank you in advance elfenliedtopfan5

What I have tried:

did a lot of google searching on the issue plus doing what is discribed above.
Posted
Updated 20-Oct-18 3:27am

Hi, you can add a comment line in your file, for example:
//{newline}

now, whenever you want to add something to the file just do a replace:
myfile.Replace ("//{newline}", "newinstrution \n //{newline}");

so you'll always have a key to replace with a new instruction.
   
This is what i would do

after you find
function main

C#
var text = string.join("\n", elf);
int index = text.indexOf("function main");
if (index != -1){
int foundedIndex =-1;
while(index < elf.Count() -1 && foundIndex == -1){
index++;
char str = text [index];
if (str == '{'){
foundedIndex = index +1;
}
if (foundIndex)
text = text.Insert(foundedIndex, "your text after the bracket")
   
Comments
elfenliedtopfan5 20-Oct-18 10:42am
   
sorry i am somewhat new to this where about do i add this code because im not sure how to implument this into my program sorry to be a a pain
elfenliedtopfan5 20-Oct-18 11:42am
   
i get the following error here
https://i.imgur.com/wr7WNxc.png
Alen Toma 20-Oct-18 22:32pm
   
i cant see what the error says
Alen Toma 20-Oct-18 22:34pm
   
And after
if (foundIndex != -1)
text = text.Insert(foundedIndex, "your text after the bracket")
you should write the text to the file and not elf
Alen Toma 21-Oct-18 20:35pm
   
Did my answer solv your problem ? then mark it as answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900